Print this page
4510 Lose SIOC*IPSECONFIG ioctl definitions
Reviewed by: Robert Mustacchi <rm@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
|
↓ open down ↓ |
15 lines elided |
↑ open up ↑ |
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 * Copyright (c) 1990 Mentat Inc.
25 25 * Copyright (c) 2012 Joyent, Inc. All rights reserved.
26 + * Copyright (c) 2014, OmniTI Computer Consulting, Inc. All rights reserved.
26 27 */
27 28
28 29 #include <sys/types.h>
29 30 #include <sys/stream.h>
30 31 #include <sys/dlpi.h>
31 32 #include <sys/stropts.h>
32 33 #include <sys/sysmacros.h>
33 34 #include <sys/strsubr.h>
34 35 #include <sys/strlog.h>
35 36 #include <sys/strsun.h>
36 37 #include <sys/zone.h>
37 38 #define _SUN_TPI_VERSION 2
38 39 #include <sys/tihdr.h>
39 40 #include <sys/xti_inet.h>
40 41 #include <sys/ddi.h>
41 42 #include <sys/suntpi.h>
42 43 #include <sys/cmn_err.h>
43 44 #include <sys/debug.h>
44 45 #include <sys/kobj.h>
45 46 #include <sys/modctl.h>
46 47 #include <sys/atomic.h>
47 48 #include <sys/policy.h>
48 49 #include <sys/priv.h>
49 50 #include <sys/taskq.h>
50 51
51 52 #include <sys/systm.h>
52 53 #include <sys/param.h>
53 54 #include <sys/kmem.h>
54 55 #include <sys/sdt.h>
55 56 #include <sys/socket.h>
56 57 #include <sys/vtrace.h>
57 58 #include <sys/isa_defs.h>
58 59 #include <sys/mac.h>
59 60 #include <net/if.h>
60 61 #include <net/if_arp.h>
61 62 #include <net/route.h>
62 63 #include <sys/sockio.h>
63 64 #include <netinet/in.h>
64 65 #include <net/if_dl.h>
65 66
66 67 #include <inet/common.h>
67 68 #include <inet/mi.h>
68 69 #include <inet/mib2.h>
69 70 #include <inet/nd.h>
70 71 #include <inet/arp.h>
71 72 #include <inet/snmpcom.h>
72 73 #include <inet/optcom.h>
73 74 #include <inet/kstatcom.h>
74 75
75 76 #include <netinet/igmp_var.h>
76 77 #include <netinet/ip6.h>
77 78 #include <netinet/icmp6.h>
78 79 #include <netinet/sctp.h>
79 80
80 81 #include <inet/ip.h>
81 82 #include <inet/ip_impl.h>
82 83 #include <inet/ip6.h>
83 84 #include <inet/ip6_asp.h>
84 85 #include <inet/tcp.h>
85 86 #include <inet/tcp_impl.h>
86 87 #include <inet/ip_multi.h>
87 88 #include <inet/ip_if.h>
88 89 #include <inet/ip_ire.h>
89 90 #include <inet/ip_ftable.h>
90 91 #include <inet/ip_rts.h>
91 92 #include <inet/ip_ndp.h>
92 93 #include <inet/ip_listutils.h>
93 94 #include <netinet/igmp.h>
94 95 #include <netinet/ip_mroute.h>
95 96 #include <inet/ipp_common.h>
96 97
97 98 #include <net/pfkeyv2.h>
98 99 #include <inet/sadb.h>
99 100 #include <inet/ipsec_impl.h>
100 101 #include <inet/iptun/iptun_impl.h>
101 102 #include <inet/ipdrop.h>
102 103 #include <inet/ip_netinfo.h>
103 104 #include <inet/ilb_ip.h>
104 105
105 106 #include <sys/ethernet.h>
106 107 #include <net/if_types.h>
107 108 #include <sys/cpuvar.h>
108 109
109 110 #include <ipp/ipp.h>
110 111 #include <ipp/ipp_impl.h>
111 112 #include <ipp/ipgpc/ipgpc.h>
112 113
113 114 #include <sys/pattr.h>
114 115 #include <inet/ipclassifier.h>
115 116 #include <inet/sctp_ip.h>
116 117 #include <inet/sctp/sctp_impl.h>
117 118 #include <inet/udp_impl.h>
118 119 #include <inet/rawip_impl.h>
119 120 #include <inet/rts_impl.h>
120 121
121 122 #include <sys/tsol/label.h>
122 123 #include <sys/tsol/tnet.h>
123 124
124 125 #include <sys/squeue_impl.h>
125 126 #include <inet/ip_arp.h>
126 127
127 128 #include <sys/clock_impl.h> /* For LBOLT_FASTPATH{,64} */
128 129
129 130 /*
130 131 * Values for squeue switch:
131 132 * IP_SQUEUE_ENTER_NODRAIN: SQ_NODRAIN
132 133 * IP_SQUEUE_ENTER: SQ_PROCESS
133 134 * IP_SQUEUE_FILL: SQ_FILL
134 135 */
135 136 int ip_squeue_enter = IP_SQUEUE_ENTER; /* Setable in /etc/system */
136 137
137 138 int ip_squeue_flag;
138 139
139 140 /*
140 141 * Setable in /etc/system
141 142 */
142 143 int ip_poll_normal_ms = 100;
143 144 int ip_poll_normal_ticks = 0;
144 145 int ip_modclose_ackwait_ms = 3000;
145 146
146 147 /*
147 148 * It would be nice to have these present only in DEBUG systems, but the
148 149 * current design of the global symbol checking logic requires them to be
149 150 * unconditionally present.
150 151 */
151 152 uint_t ip_thread_data; /* TSD key for debug support */
152 153 krwlock_t ip_thread_rwlock;
153 154 list_t ip_thread_list;
154 155
155 156 /*
156 157 * Structure to represent a linked list of msgblks. Used by ip_snmp_ functions.
157 158 */
158 159
159 160 struct listptr_s {
160 161 mblk_t *lp_head; /* pointer to the head of the list */
161 162 mblk_t *lp_tail; /* pointer to the tail of the list */
162 163 };
163 164
164 165 typedef struct listptr_s listptr_t;
165 166
166 167 /*
167 168 * This is used by ip_snmp_get_mib2_ip_route_media and
168 169 * ip_snmp_get_mib2_ip6_route_media to carry the lists of return data.
169 170 */
170 171 typedef struct iproutedata_s {
171 172 uint_t ird_idx;
172 173 uint_t ird_flags; /* see below */
173 174 listptr_t ird_route; /* ipRouteEntryTable */
174 175 listptr_t ird_netmedia; /* ipNetToMediaEntryTable */
175 176 listptr_t ird_attrs; /* ipRouteAttributeTable */
176 177 } iproutedata_t;
177 178
178 179 /* Include ire_testhidden and IRE_IF_CLONE routes */
179 180 #define IRD_REPORT_ALL 0x01
180 181
181 182 /*
182 183 * Cluster specific hooks. These should be NULL when booted as a non-cluster
183 184 */
184 185
185 186 /*
186 187 * Hook functions to enable cluster networking
187 188 * On non-clustered systems these vectors must always be NULL.
188 189 *
189 190 * Hook function to Check ip specified ip address is a shared ip address
190 191 * in the cluster
191 192 *
192 193 */
193 194 int (*cl_inet_isclusterwide)(netstackid_t stack_id, uint8_t protocol,
194 195 sa_family_t addr_family, uint8_t *laddrp, void *args) = NULL;
195 196
196 197 /*
197 198 * Hook function to generate cluster wide ip fragment identifier
198 199 */
199 200 uint32_t (*cl_inet_ipident)(netstackid_t stack_id, uint8_t protocol,
200 201 sa_family_t addr_family, uint8_t *laddrp, uint8_t *faddrp,
201 202 void *args) = NULL;
202 203
203 204 /*
204 205 * Hook function to generate cluster wide SPI.
205 206 */
206 207 void (*cl_inet_getspi)(netstackid_t, uint8_t, uint8_t *, size_t,
207 208 void *) = NULL;
208 209
209 210 /*
210 211 * Hook function to verify if the SPI is already utlized.
211 212 */
212 213
213 214 int (*cl_inet_checkspi)(netstackid_t, uint8_t, uint32_t, void *) = NULL;
214 215
215 216 /*
216 217 * Hook function to delete the SPI from the cluster wide repository.
217 218 */
218 219
219 220 void (*cl_inet_deletespi)(netstackid_t, uint8_t, uint32_t, void *) = NULL;
220 221
221 222 /*
222 223 * Hook function to inform the cluster when packet received on an IDLE SA
223 224 */
224 225
225 226 void (*cl_inet_idlesa)(netstackid_t, uint8_t, uint32_t, sa_family_t,
226 227 in6_addr_t, in6_addr_t, void *) = NULL;
227 228
228 229 /*
229 230 * Synchronization notes:
230 231 *
231 232 * IP is a fully D_MP STREAMS module/driver. Thus it does not depend on any
232 233 * MT level protection given by STREAMS. IP uses a combination of its own
233 234 * internal serialization mechanism and standard Solaris locking techniques.
234 235 * The internal serialization is per phyint. This is used to serialize
235 236 * plumbing operations, IPMP operations, most set ioctls, etc.
236 237 *
237 238 * Plumbing is a long sequence of operations involving message
238 239 * exchanges between IP, ARP and device drivers. Many set ioctls are typically
239 240 * involved in plumbing operations. A natural model is to serialize these
240 241 * ioctls one per ill. For example plumbing of hme0 and qfe0 can go on in
241 242 * parallel without any interference. But various set ioctls on hme0 are best
242 243 * serialized, along with IPMP operations and processing of DLPI control
243 244 * messages received from drivers on a per phyint basis. This serialization is
244 245 * provided by the ipsq_t and primitives operating on this. Details can
245 246 * be found in ip_if.c above the core primitives operating on ipsq_t.
246 247 *
247 248 * Lookups of an ipif or ill by a thread return a refheld ipif / ill.
248 249 * Simiarly lookup of an ire by a thread also returns a refheld ire.
249 250 * In addition ipif's and ill's referenced by the ire are also indirectly
250 251 * refheld. Thus no ipif or ill can vanish as long as an ipif is refheld
251 252 * directly or indirectly. For example an SIOCSLIFADDR ioctl that changes the
252 253 * address of an ipif has to go through the ipsq_t. This ensures that only
253 254 * one such exclusive operation proceeds at any time on the ipif. It then
254 255 * waits for all refcnts
255 256 * associated with this ipif to come down to zero. The address is changed
256 257 * only after the ipif has been quiesced. Then the ipif is brought up again.
257 258 * More details are described above the comment in ip_sioctl_flags.
258 259 *
259 260 * Packet processing is based mostly on IREs and are fully multi-threaded
260 261 * using standard Solaris MT techniques.
261 262 *
262 263 * There are explicit locks in IP to handle:
263 264 * - The ip_g_head list maintained by mi_open_link() and friends.
264 265 *
265 266 * - The reassembly data structures (one lock per hash bucket)
266 267 *
267 268 * - conn_lock is meant to protect conn_t fields. The fields actually
268 269 * protected by conn_lock are documented in the conn_t definition.
269 270 *
270 271 * - ire_lock to protect some of the fields of the ire, IRE tables
271 272 * (one lock per hash bucket). Refer to ip_ire.c for details.
272 273 *
273 274 * - ndp_g_lock and ncec_lock for protecting NCEs.
274 275 *
275 276 * - ill_lock protects fields of the ill and ipif. Details in ip.h
276 277 *
277 278 * - ill_g_lock: This is a global reader/writer lock. Protects the following
278 279 * * The AVL tree based global multi list of all ills.
279 280 * * The linked list of all ipifs of an ill
280 281 * * The <ipsq-xop> mapping
281 282 * * <ill-phyint> association
282 283 * Insertion/deletion of an ill in the system, insertion/deletion of an ipif
283 284 * into an ill, changing the <ipsq-xop> mapping of an ill, changing the
284 285 * <ill-phyint> assoc of an ill will all have to hold the ill_g_lock as
285 286 * writer for the actual duration of the insertion/deletion/change.
286 287 *
287 288 * - ill_lock: This is a per ill mutex.
288 289 * It protects some members of the ill_t struct; see ip.h for details.
289 290 * It also protects the <ill-phyint> assoc.
290 291 * It also protects the list of ipifs hanging off the ill.
291 292 *
292 293 * - ipsq_lock: This is a per ipsq_t mutex lock.
293 294 * This protects some members of the ipsq_t struct; see ip.h for details.
294 295 * It also protects the <ipsq-ipxop> mapping
295 296 *
296 297 * - ipx_lock: This is a per ipxop_t mutex lock.
297 298 * This protects some members of the ipxop_t struct; see ip.h for details.
298 299 *
299 300 * - phyint_lock: This is a per phyint mutex lock. Protects just the
300 301 * phyint_flags
301 302 *
302 303 * - ip_addr_avail_lock: This is used to ensure the uniqueness of IP addresses.
303 304 * This lock is held in ipif_up_done and the ipif is marked IPIF_UP and the
304 305 * uniqueness check also done atomically.
305 306 *
306 307 * - ill_g_usesrc_lock: This readers/writer lock protects the usesrc
307 308 * group list linked by ill_usesrc_grp_next. It also protects the
308 309 * ill_usesrc_ifindex field. It is taken as a writer when a member of the
309 310 * group is being added or deleted. This lock is taken as a reader when
310 311 * walking the list/group(eg: to get the number of members in a usesrc group).
311 312 * Note, it is only necessary to take this lock if the ill_usesrc_grp_next
312 313 * field is changing state i.e from NULL to non-NULL or vice-versa. For
313 314 * example, it is not necessary to take this lock in the initial portion
314 315 * of ip_sioctl_slifusesrc or at all in ip_sioctl_flags since these
315 316 * operations are executed exclusively and that ensures that the "usesrc
316 317 * group state" cannot change. The "usesrc group state" change can happen
317 318 * only in the latter part of ip_sioctl_slifusesrc and in ill_delete.
318 319 *
319 320 * Changing <ill-phyint>, <ipsq-xop> assocications:
320 321 *
321 322 * To change the <ill-phyint> association, the ill_g_lock must be held
322 323 * as writer, and the ill_locks of both the v4 and v6 instance of the ill
323 324 * must be held.
324 325 *
325 326 * To change the <ipsq-xop> association, the ill_g_lock must be held as
326 327 * writer, the ipsq_lock must be held, and one must be writer on the ipsq.
327 328 * This is only done when ills are added or removed from IPMP groups.
328 329 *
329 330 * To add or delete an ipif from the list of ipifs hanging off the ill,
330 331 * ill_g_lock (writer) and ill_lock must be held and the thread must be
331 332 * a writer on the associated ipsq.
332 333 *
333 334 * To add or delete an ill to the system, the ill_g_lock must be held as
334 335 * writer and the thread must be a writer on the associated ipsq.
335 336 *
336 337 * To add or delete an ilm to an ill, the ill_lock must be held and the thread
337 338 * must be a writer on the associated ipsq.
338 339 *
339 340 * Lock hierarchy
340 341 *
341 342 * Some lock hierarchy scenarios are listed below.
342 343 *
343 344 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock -> ipx_lock
344 345 * ill_g_lock -> ill_lock(s) -> phyint_lock
345 346 * ill_g_lock -> ndp_g_lock -> ill_lock -> ncec_lock
346 347 * ill_g_lock -> ip_addr_avail_lock
347 348 * conn_lock -> irb_lock -> ill_lock -> ire_lock
348 349 * ill_g_lock -> ip_g_nd_lock
349 350 * ill_g_lock -> ips_ipmp_lock -> ill_lock -> nce_lock
350 351 * ill_g_lock -> ndp_g_lock -> ill_lock -> ncec_lock -> nce_lock
351 352 * arl_lock -> ill_lock
352 353 * ips_ire_dep_lock -> irb_lock
353 354 *
354 355 * When more than 1 ill lock is needed to be held, all ill lock addresses
355 356 * are sorted on address and locked starting from highest addressed lock
356 357 * downward.
357 358 *
358 359 * Multicast scenarios
359 360 * ips_ill_g_lock -> ill_mcast_lock
360 361 * conn_ilg_lock -> ips_ill_g_lock -> ill_lock
361 362 * ill_mcast_serializer -> ill_mcast_lock -> ips_ipmp_lock -> ill_lock
362 363 * ill_mcast_serializer -> ill_mcast_lock -> connf_lock -> conn_lock
363 364 * ill_mcast_serializer -> ill_mcast_lock -> conn_ilg_lock
364 365 * ill_mcast_serializer -> ill_mcast_lock -> ips_igmp_timer_lock
365 366 *
366 367 * IPsec scenarios
367 368 *
368 369 * ipsa_lock -> ill_g_lock -> ill_lock
369 370 * ill_g_usesrc_lock -> ill_g_lock -> ill_lock
370 371 *
371 372 * Trusted Solaris scenarios
372 373 *
373 374 * igsa_lock -> gcgrp_rwlock -> gcgrp_lock
374 375 * igsa_lock -> gcdb_lock
375 376 * gcgrp_rwlock -> ire_lock
376 377 * gcgrp_rwlock -> gcdb_lock
377 378 *
378 379 * squeue(sq_lock), flow related (ft_lock, fe_lock) locking
379 380 *
380 381 * cpu_lock --> ill_lock --> sqset_lock --> sq_lock
381 382 * sq_lock -> conn_lock -> QLOCK(q)
382 383 * ill_lock -> ft_lock -> fe_lock
383 384 *
384 385 * Routing/forwarding table locking notes:
385 386 *
386 387 * Lock acquisition order: Radix tree lock, irb_lock.
387 388 * Requirements:
388 389 * i. Walker must not hold any locks during the walker callback.
389 390 * ii Walker must not see a truncated tree during the walk because of any node
390 391 * deletion.
391 392 * iii Existing code assumes ire_bucket is valid if it is non-null and is used
392 393 * in many places in the code to walk the irb list. Thus even if all the
393 394 * ires in a bucket have been deleted, we still can't free the radix node
394 395 * until the ires have actually been inactive'd (freed).
395 396 *
396 397 * Tree traversal - Need to hold the global tree lock in read mode.
397 398 * Before dropping the global tree lock, need to either increment the ire_refcnt
398 399 * to ensure that the radix node can't be deleted.
399 400 *
400 401 * Tree add - Need to hold the global tree lock in write mode to add a
401 402 * radix node. To prevent the node from being deleted, increment the
402 403 * irb_refcnt, after the node is added to the tree. The ire itself is
403 404 * added later while holding the irb_lock, but not the tree lock.
404 405 *
405 406 * Tree delete - Need to hold the global tree lock and irb_lock in write mode.
406 407 * All associated ires must be inactive (i.e. freed), and irb_refcnt
407 408 * must be zero.
408 409 *
409 410 * Walker - Increment irb_refcnt before calling the walker callback. Hold the
410 411 * global tree lock (read mode) for traversal.
411 412 *
412 413 * IRE dependencies - In some cases we hold ips_ire_dep_lock across ire_refrele
413 414 * hence we will acquire irb_lock while holding ips_ire_dep_lock.
414 415 *
415 416 * IPsec notes :
416 417 *
417 418 * IP interacts with the IPsec code (AH/ESP) by storing IPsec attributes
418 419 * in the ip_xmit_attr_t ip_recv_attr_t. For outbound datagrams, the
419 420 * ip_xmit_attr_t has the
420 421 * information used by the IPsec code for applying the right level of
421 422 * protection. The information initialized by IP in the ip_xmit_attr_t
422 423 * is determined by the per-socket policy or global policy in the system.
423 424 * For inbound datagrams, the ip_recv_attr_t
424 425 * starts out with nothing in it. It gets filled
425 426 * with the right information if it goes through the AH/ESP code, which
426 427 * happens if the incoming packet is secure. The information initialized
427 428 * by AH/ESP, is later used by IP (during fanouts to ULP) to see whether
428 429 * the policy requirements needed by per-socket policy or global policy
429 430 * is met or not.
430 431 *
431 432 * For fully connected sockets i.e dst, src [addr, port] is known,
432 433 * conn_policy_cached is set indicating that policy has been cached.
433 434 * conn_in_enforce_policy may or may not be set depending on whether
434 435 * there is a global policy match or per-socket policy match.
435 436 * Policy inheriting happpens in ip_policy_set once the destination is known.
436 437 * Once the right policy is set on the conn_t, policy cannot change for
437 438 * this socket. This makes life simpler for TCP (UDP ?) where
438 439 * re-transmissions go out with the same policy. For symmetry, policy
439 440 * is cached for fully connected UDP sockets also. Thus if policy is cached,
440 441 * it also implies that policy is latched i.e policy cannot change
441 442 * on these sockets. As we have the right policy on the conn, we don't
442 443 * have to lookup global policy for every outbound and inbound datagram
443 444 * and thus serving as an optimization. Note that a global policy change
444 445 * does not affect fully connected sockets if they have policy. If fully
445 446 * connected sockets did not have any policy associated with it, global
446 447 * policy change may affect them.
447 448 *
448 449 * IP Flow control notes:
449 450 * ---------------------
450 451 * Non-TCP streams are flow controlled by IP. The way this is accomplished
451 452 * differs when ILL_CAPAB_DLD_DIRECT is enabled for that IP instance. When
452 453 * ILL_DIRECT_CAPABLE(ill) is TRUE, IP can do direct function calls into
453 454 * GLDv3. Otherwise packets are sent down to lower layers using STREAMS
454 455 * functions.
455 456 *
456 457 * Per Tx ring udp flow control:
457 458 * This is applicable only when ILL_CAPAB_DLD_DIRECT capability is set in
458 459 * the ill (i.e. ILL_DIRECT_CAPABLE(ill) is true).
459 460 *
460 461 * The underlying link can expose multiple Tx rings to the GLDv3 mac layer.
461 462 * To achieve best performance, outgoing traffic need to be fanned out among
462 463 * these Tx ring. mac_tx() is called (via str_mdata_fastpath_put()) to send
463 464 * traffic out of the NIC and it takes a fanout hint. UDP connections pass
464 465 * the address of connp as fanout hint to mac_tx(). Under flow controlled
465 466 * condition, mac_tx() returns a non-NULL cookie (ip_mac_tx_cookie_t). This
466 467 * cookie points to a specific Tx ring that is blocked. The cookie is used to
467 468 * hash into an idl_tx_list[] entry in idl_tx_list[] array. Each idl_tx_list_t
468 469 * point to drain_lists (idl_t's). These drain list will store the blocked UDP
469 470 * connp's. The drain list is not a single list but a configurable number of
470 471 * lists.
471 472 *
472 473 * The diagram below shows idl_tx_list_t's and their drain_lists. ip_stack_t
473 474 * has an array of idl_tx_list_t. The size of the array is TX_FANOUT_SIZE
474 475 * which is equal to 128. This array in turn contains a pointer to idl_t[],
475 476 * the ip drain list. The idl_t[] array size is MIN(max_ncpus, 8). The drain
476 477 * list will point to the list of connp's that are flow controlled.
477 478 *
478 479 * --------------- ------- ------- -------
479 480 * |->|drain_list[0]|-->|connp|-->|connp|-->|connp|-->
480 481 * | --------------- ------- ------- -------
481 482 * | --------------- ------- ------- -------
482 483 * |->|drain_list[1]|-->|connp|-->|connp|-->|connp|-->
483 484 * ---------------- | --------------- ------- ------- -------
484 485 * |idl_tx_list[0]|->| --------------- ------- ------- -------
485 486 * ---------------- |->|drain_list[2]|-->|connp|-->|connp|-->|connp|-->
486 487 * | --------------- ------- ------- -------
487 488 * . . . . .
488 489 * | --------------- ------- ------- -------
489 490 * |->|drain_list[n]|-->|connp|-->|connp|-->|connp|-->
490 491 * --------------- ------- ------- -------
491 492 * --------------- ------- ------- -------
492 493 * |->|drain_list[0]|-->|connp|-->|connp|-->|connp|-->
493 494 * | --------------- ------- ------- -------
494 495 * | --------------- ------- ------- -------
495 496 * ---------------- |->|drain_list[1]|-->|connp|-->|connp|-->|connp|-->
496 497 * |idl_tx_list[1]|->| --------------- ------- ------- -------
497 498 * ---------------- | . . . .
498 499 * | --------------- ------- ------- -------
499 500 * |->|drain_list[n]|-->|connp|-->|connp|-->|connp|-->
500 501 * --------------- ------- ------- -------
501 502 * .....
502 503 * ----------------
503 504 * |idl_tx_list[n]|-> ...
504 505 * ----------------
505 506 *
506 507 * When mac_tx() returns a cookie, the cookie is hashed into an index into
507 508 * ips_idl_tx_list[], and conn_drain_insert() is called with the idl_tx_list
508 509 * to insert the conn onto. conn_drain_insert() asserts flow control for the
509 510 * sockets via su_txq_full() (non-STREAMS) or QFULL on conn_wq (STREAMS).
510 511 * Further, conn_blocked is set to indicate that the conn is blocked.
511 512 *
512 513 * GLDv3 calls ill_flow_enable() when flow control is relieved. The cookie
513 514 * passed in the call to ill_flow_enable() identifies the blocked Tx ring and
514 515 * is again hashed to locate the appropriate idl_tx_list, which is then
515 516 * drained via conn_walk_drain(). conn_walk_drain() goes through each conn in
516 517 * the drain list and calls conn_drain_remove() to clear flow control (via
517 518 * calling su_txq_full() or clearing QFULL), and remove the conn from the
518 519 * drain list.
519 520 *
520 521 * Note that the drain list is not a single list but a (configurable) array of
521 522 * lists (8 elements by default). Synchronization between drain insertion and
522 523 * flow control wakeup is handled by using idl_txl->txl_lock, and only
523 524 * conn_drain_insert() and conn_drain_remove() manipulate the drain list.
524 525 *
525 526 * Flow control via STREAMS is used when ILL_DIRECT_CAPABLE() returns FALSE.
526 527 * On the send side, if the packet cannot be sent down to the driver by IP
527 528 * (canput() fails), ip_xmit() drops the packet and returns EWOULDBLOCK to the
528 529 * caller, who may then invoke ixa_check_drain_insert() to insert the conn on
529 530 * the 0'th drain list. When ip_wsrv() runs on the ill_wq because flow
530 531 * control has been relieved, the blocked conns in the 0'th drain list are
531 532 * drained as in the non-STREAMS case.
532 533 *
533 534 * In both the STREAMS and non-STREAMS cases, the sockfs upcall to set QFULL
534 535 * is done when the conn is inserted into the drain list (conn_drain_insert())
535 536 * and cleared when the conn is removed from the it (conn_drain_remove()).
536 537 *
537 538 * IPQOS notes:
538 539 *
539 540 * IPQoS Policies are applied to packets using IPPF (IP Policy framework)
540 541 * and IPQoS modules. IPPF includes hooks in IP at different control points
541 542 * (callout positions) which direct packets to IPQoS modules for policy
542 543 * processing. Policies, if present, are global.
543 544 *
544 545 * The callout positions are located in the following paths:
545 546 * o local_in (packets destined for this host)
546 547 * o local_out (packets orginating from this host )
547 548 * o fwd_in (packets forwarded by this m/c - inbound)
548 549 * o fwd_out (packets forwarded by this m/c - outbound)
549 550 * Hooks at these callout points can be enabled/disabled using the ndd variable
550 551 * ip_policy_mask (a bit mask with the 4 LSB indicating the callout positions).
551 552 * By default all the callout positions are enabled.
552 553 *
553 554 * Outbound (local_out)
554 555 * Hooks are placed in ire_send_wire_v4 and ire_send_wire_v6.
555 556 *
556 557 * Inbound (local_in)
557 558 * Hooks are placed in ip_fanout_v4 and ip_fanout_v6.
558 559 *
559 560 * Forwarding (in and out)
560 561 * Hooks are placed in ire_recv_forward_v4/v6.
561 562 *
562 563 * IP Policy Framework processing (IPPF processing)
563 564 * Policy processing for a packet is initiated by ip_process, which ascertains
564 565 * that the classifier (ipgpc) is loaded and configured, failing which the
565 566 * packet resumes normal processing in IP. If the clasifier is present, the
566 567 * packet is acted upon by one or more IPQoS modules (action instances), per
567 568 * filters configured in ipgpc and resumes normal IP processing thereafter.
568 569 * An action instance can drop a packet in course of its processing.
569 570 *
570 571 * Zones notes:
571 572 *
572 573 * The partitioning rules for networking are as follows:
573 574 * 1) Packets coming from a zone must have a source address belonging to that
574 575 * zone.
575 576 * 2) Packets coming from a zone can only be sent on a physical interface on
576 577 * which the zone has an IP address.
577 578 * 3) Between two zones on the same machine, packet delivery is only allowed if
578 579 * there's a matching route for the destination and zone in the forwarding
579 580 * table.
580 581 * 4) The TCP and UDP port spaces are per-zone; that is, two processes in
581 582 * different zones can bind to the same port with the wildcard address
582 583 * (INADDR_ANY).
583 584 *
584 585 * The granularity of interface partitioning is at the logical interface level.
585 586 * Therefore, every zone has its own IP addresses, and incoming packets can be
586 587 * attributed to a zone unambiguously. A logical interface is placed into a zone
587 588 * using the SIOCSLIFZONE ioctl; this sets the ipif_zoneid field in the ipif_t
588 589 * structure. Rule (1) is implemented by modifying the source address selection
589 590 * algorithm so that the list of eligible addresses is filtered based on the
590 591 * sending process zone.
591 592 *
592 593 * The Internet Routing Entries (IREs) are either exclusive to a zone or shared
593 594 * across all zones, depending on their type. Here is the break-up:
594 595 *
595 596 * IRE type Shared/exclusive
596 597 * -------- ----------------
597 598 * IRE_BROADCAST Exclusive
598 599 * IRE_DEFAULT (default routes) Shared (*)
599 600 * IRE_LOCAL Exclusive (x)
600 601 * IRE_LOOPBACK Exclusive
601 602 * IRE_PREFIX (net routes) Shared (*)
602 603 * IRE_IF_NORESOLVER (interface routes) Exclusive
603 604 * IRE_IF_RESOLVER (interface routes) Exclusive
604 605 * IRE_IF_CLONE (interface routes) Exclusive
605 606 * IRE_HOST (host routes) Shared (*)
606 607 *
607 608 * (*) A zone can only use a default or off-subnet route if the gateway is
608 609 * directly reachable from the zone, that is, if the gateway's address matches
609 610 * one of the zone's logical interfaces.
610 611 *
611 612 * (x) IRE_LOCAL are handled a bit differently.
612 613 * When ip_restrict_interzone_loopback is set (the default),
613 614 * ire_route_recursive restricts loopback using an IRE_LOCAL
614 615 * between zone to the case when L2 would have conceptually looped the packet
615 616 * back, i.e. the loopback which is required since neither Ethernet drivers
616 617 * nor Ethernet hardware loops them back. This is the case when the normal
617 618 * routes (ignoring IREs with different zoneids) would send out the packet on
618 619 * the same ill as the ill with which is IRE_LOCAL is associated.
619 620 *
620 621 * Multiple zones can share a common broadcast address; typically all zones
621 622 * share the 255.255.255.255 address. Incoming as well as locally originated
622 623 * broadcast packets must be dispatched to all the zones on the broadcast
623 624 * network. For directed broadcasts (e.g. 10.16.72.255) this is not trivial
624 625 * since some zones may not be on the 10.16.72/24 network. To handle this, each
625 626 * zone has its own set of IRE_BROADCAST entries; then, broadcast packets are
626 627 * sent to every zone that has an IRE_BROADCAST entry for the destination
627 628 * address on the input ill, see ip_input_broadcast().
628 629 *
629 630 * Applications in different zones can join the same multicast group address.
630 631 * The same logic applies for multicast as for broadcast. ip_input_multicast
631 632 * dispatches packets to all zones that have members on the physical interface.
632 633 */
633 634
634 635 /*
635 636 * Squeue Fanout flags:
636 637 * 0: No fanout.
637 638 * 1: Fanout across all squeues
638 639 */
639 640 boolean_t ip_squeue_fanout = 0;
640 641
641 642 /*
642 643 * Maximum dups allowed per packet.
643 644 */
644 645 uint_t ip_max_frag_dups = 10;
645 646
646 647 static int ip_open(queue_t *q, dev_t *devp, int flag, int sflag,
647 648 cred_t *credp, boolean_t isv6);
648 649 static mblk_t *ip_xmit_attach_llhdr(mblk_t *, nce_t *);
649 650
650 651 static boolean_t icmp_inbound_verify_v4(mblk_t *, icmph_t *, ip_recv_attr_t *);
651 652 static void icmp_inbound_too_big_v4(icmph_t *, ip_recv_attr_t *);
652 653 static void icmp_inbound_error_fanout_v4(mblk_t *, icmph_t *,
653 654 ip_recv_attr_t *);
654 655 static void icmp_options_update(ipha_t *);
655 656 static void icmp_param_problem(mblk_t *, uint8_t, ip_recv_attr_t *);
656 657 static void icmp_pkt(mblk_t *, void *, size_t, ip_recv_attr_t *);
657 658 static mblk_t *icmp_pkt_err_ok(mblk_t *, ip_recv_attr_t *);
658 659 static void icmp_redirect_v4(mblk_t *mp, ipha_t *, icmph_t *,
659 660 ip_recv_attr_t *);
660 661 static void icmp_send_redirect(mblk_t *, ipaddr_t, ip_recv_attr_t *);
661 662 static void icmp_send_reply_v4(mblk_t *, ipha_t *, icmph_t *,
662 663 ip_recv_attr_t *);
663 664
664 665 mblk_t *ip_dlpi_alloc(size_t, t_uscalar_t);
665 666 char *ip_dot_addr(ipaddr_t, char *);
666 667 mblk_t *ip_carve_mp(mblk_t **, ssize_t);
667 668 int ip_close(queue_t *, int);
668 669 static char *ip_dot_saddr(uchar_t *, char *);
669 670 static void ip_lrput(queue_t *, mblk_t *);
670 671 ipaddr_t ip_net_mask(ipaddr_t);
671 672 char *ip_nv_lookup(nv_t *, int);
672 673 void ip_rput(queue_t *, mblk_t *);
673 674 static void ip_rput_dlpi_writer(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp,
674 675 void *dummy_arg);
675 676 int ip_snmp_get(queue_t *, mblk_t *, int, boolean_t);
676 677 static mblk_t *ip_snmp_get_mib2_ip(queue_t *, mblk_t *,
677 678 mib2_ipIfStatsEntry_t *, ip_stack_t *, boolean_t);
678 679 static mblk_t *ip_snmp_get_mib2_ip_traffic_stats(queue_t *, mblk_t *,
679 680 ip_stack_t *, boolean_t);
680 681 static mblk_t *ip_snmp_get_mib2_ip6(queue_t *, mblk_t *, ip_stack_t *,
681 682 boolean_t);
682 683 static mblk_t *ip_snmp_get_mib2_icmp(queue_t *, mblk_t *, ip_stack_t *ipst);
683 684 static mblk_t *ip_snmp_get_mib2_icmp6(queue_t *, mblk_t *, ip_stack_t *ipst);
684 685 static mblk_t *ip_snmp_get_mib2_igmp(queue_t *, mblk_t *, ip_stack_t *ipst);
685 686 static mblk_t *ip_snmp_get_mib2_multi(queue_t *, mblk_t *, ip_stack_t *ipst);
686 687 static mblk_t *ip_snmp_get_mib2_ip_addr(queue_t *, mblk_t *,
687 688 ip_stack_t *ipst, boolean_t);
688 689 static mblk_t *ip_snmp_get_mib2_ip6_addr(queue_t *, mblk_t *,
689 690 ip_stack_t *ipst, boolean_t);
690 691 static mblk_t *ip_snmp_get_mib2_ip_group_src(queue_t *, mblk_t *,
691 692 ip_stack_t *ipst);
692 693 static mblk_t *ip_snmp_get_mib2_ip6_group_src(queue_t *, mblk_t *,
693 694 ip_stack_t *ipst);
694 695 static mblk_t *ip_snmp_get_mib2_ip_group_mem(queue_t *, mblk_t *,
695 696 ip_stack_t *ipst);
696 697 static mblk_t *ip_snmp_get_mib2_ip6_group_mem(queue_t *, mblk_t *,
697 698 ip_stack_t *ipst);
698 699 static mblk_t *ip_snmp_get_mib2_virt_multi(queue_t *, mblk_t *,
699 700 ip_stack_t *ipst);
700 701 static mblk_t *ip_snmp_get_mib2_multi_rtable(queue_t *, mblk_t *,
701 702 ip_stack_t *ipst);
702 703 static mblk_t *ip_snmp_get_mib2_ip_route_media(queue_t *, mblk_t *, int,
703 704 ip_stack_t *ipst);
704 705 static mblk_t *ip_snmp_get_mib2_ip6_route_media(queue_t *, mblk_t *, int,
705 706 ip_stack_t *ipst);
706 707 static void ip_snmp_get2_v4(ire_t *, iproutedata_t *);
707 708 static void ip_snmp_get2_v6_route(ire_t *, iproutedata_t *);
708 709 static int ip_snmp_get2_v4_media(ncec_t *, iproutedata_t *);
709 710 static int ip_snmp_get2_v6_media(ncec_t *, iproutedata_t *);
710 711 int ip_snmp_set(queue_t *, int, int, uchar_t *, int);
711 712
712 713 static mblk_t *ip_fragment_copyhdr(uchar_t *, int, int, ip_stack_t *,
713 714 mblk_t *);
714 715
715 716 static void conn_drain_init(ip_stack_t *);
716 717 static void conn_drain_fini(ip_stack_t *);
717 718 static void conn_drain(conn_t *connp, boolean_t closing);
718 719
719 720 static void conn_walk_drain(ip_stack_t *, idl_tx_list_t *);
720 721 static void conn_walk_sctp(pfv_t, void *, zoneid_t, netstack_t *);
721 722
722 723 static void *ip_stack_init(netstackid_t stackid, netstack_t *ns);
723 724 static void ip_stack_shutdown(netstackid_t stackid, void *arg);
724 725 static void ip_stack_fini(netstackid_t stackid, void *arg);
725 726
726 727 static int ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t,
727 728 const in6_addr_t *, ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *),
728 729 ire_t *, conn_t *, boolean_t, const in6_addr_t *, mcast_record_t,
729 730 const in6_addr_t *);
730 731
731 732 static int ip_squeue_switch(int);
732 733
733 734 static void *ip_kstat_init(netstackid_t, ip_stack_t *);
734 735 static void ip_kstat_fini(netstackid_t, kstat_t *);
735 736 static int ip_kstat_update(kstat_t *kp, int rw);
736 737 static void *icmp_kstat_init(netstackid_t);
737 738 static void icmp_kstat_fini(netstackid_t, kstat_t *);
738 739 static int icmp_kstat_update(kstat_t *kp, int rw);
739 740 static void *ip_kstat2_init(netstackid_t, ip_stat_t *);
740 741 static void ip_kstat2_fini(netstackid_t, kstat_t *);
741 742
742 743 static void ipobs_init(ip_stack_t *);
743 744 static void ipobs_fini(ip_stack_t *);
744 745
745 746 static int ip_tp_cpu_update(cpu_setup_t, int, void *);
746 747
747 748 ipaddr_t ip_g_all_ones = IP_HOST_MASK;
748 749
749 750 static long ip_rput_pullups;
750 751 int dohwcksum = 1; /* use h/w cksum if supported by the hardware */
751 752
752 753 vmem_t *ip_minor_arena_sa; /* for minor nos. from INET_MIN_DEV+2 thru 2^^18-1 */
753 754 vmem_t *ip_minor_arena_la; /* for minor nos. from 2^^18 thru 2^^32-1 */
754 755
755 756 int ip_debug;
756 757
757 758 /*
758 759 * Multirouting/CGTP stuff
759 760 */
760 761 int ip_cgtp_filter_rev = CGTP_FILTER_REV; /* CGTP hooks version */
761 762
762 763 /*
763 764 * IP tunables related declarations. Definitions are in ip_tunables.c
764 765 */
765 766 extern mod_prop_info_t ip_propinfo_tbl[];
766 767 extern int ip_propinfo_count;
767 768
768 769 /*
769 770 * Table of IP ioctls encoding the various properties of the ioctl and
770 771 * indexed based on the last byte of the ioctl command. Occasionally there
771 772 * is a clash, and there is more than 1 ioctl with the same last byte.
772 773 * In such a case 1 ioctl is encoded in the ndx table and the remaining
773 774 * ioctls are encoded in the misc table. An entry in the ndx table is
774 775 * retrieved by indexing on the last byte of the ioctl command and comparing
775 776 * the ioctl command with the value in the ndx table. In the event of a
776 777 * mismatch the misc table is then searched sequentially for the desired
777 778 * ioctl command.
778 779 *
779 780 * Entry: <command> <copyin_size> <flags> <cmd_type> <function> <restart_func>
780 781 */
781 782 ip_ioctl_cmd_t ip_ndx_ioctl_table[] = {
782 783 /* 000 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
783 784 /* 001 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
784 785 /* 002 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
785 786 /* 003 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
786 787 /* 004 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
787 788 /* 005 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
788 789 /* 006 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
789 790 /* 007 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
790 791 /* 008 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
791 792 /* 009 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
792 793
793 794 /* 010 */ { SIOCADDRT, sizeof (struct rtentry), IPI_PRIV,
794 795 MISC_CMD, ip_siocaddrt, NULL },
795 796 /* 011 */ { SIOCDELRT, sizeof (struct rtentry), IPI_PRIV,
796 797 MISC_CMD, ip_siocdelrt, NULL },
797 798
798 799 /* 012 */ { SIOCSIFADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
799 800 IF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart },
800 801 /* 013 */ { SIOCGIFADDR, sizeof (struct ifreq), IPI_GET_CMD,
801 802 IF_CMD, ip_sioctl_get_addr, NULL },
802 803
803 804 /* 014 */ { SIOCSIFDSTADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
804 805 IF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart },
805 806 /* 015 */ { SIOCGIFDSTADDR, sizeof (struct ifreq),
806 807 IPI_GET_CMD, IF_CMD, ip_sioctl_get_dstaddr, NULL },
807 808
808 809 /* 016 */ { SIOCSIFFLAGS, sizeof (struct ifreq),
809 810 IPI_PRIV | IPI_WR,
810 811 IF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart },
811 812 /* 017 */ { SIOCGIFFLAGS, sizeof (struct ifreq),
812 813 IPI_MODOK | IPI_GET_CMD,
813 814 IF_CMD, ip_sioctl_get_flags, NULL },
814 815
815 816 /* 018 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
816 817 /* 019 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
817 818
818 819 /* copyin size cannot be coded for SIOCGIFCONF */
819 820 /* 020 */ { O_SIOCGIFCONF, 0, IPI_GET_CMD,
820 821 MISC_CMD, ip_sioctl_get_ifconf, NULL },
821 822
822 823 /* 021 */ { SIOCSIFMTU, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
823 824 IF_CMD, ip_sioctl_mtu, NULL },
824 825 /* 022 */ { SIOCGIFMTU, sizeof (struct ifreq), IPI_GET_CMD,
825 826 IF_CMD, ip_sioctl_get_mtu, NULL },
826 827 /* 023 */ { SIOCGIFBRDADDR, sizeof (struct ifreq),
827 828 IPI_GET_CMD, IF_CMD, ip_sioctl_get_brdaddr, NULL },
828 829 /* 024 */ { SIOCSIFBRDADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
829 830 IF_CMD, ip_sioctl_brdaddr, NULL },
830 831 /* 025 */ { SIOCGIFNETMASK, sizeof (struct ifreq),
831 832 IPI_GET_CMD, IF_CMD, ip_sioctl_get_netmask, NULL },
832 833 /* 026 */ { SIOCSIFNETMASK, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
833 834 IF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart },
834 835 /* 027 */ { SIOCGIFMETRIC, sizeof (struct ifreq),
835 836 IPI_GET_CMD, IF_CMD, ip_sioctl_get_metric, NULL },
836 837 /* 028 */ { SIOCSIFMETRIC, sizeof (struct ifreq), IPI_PRIV,
837 838 IF_CMD, ip_sioctl_metric, NULL },
838 839 /* 029 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
839 840
840 841 /* See 166-168 below for extended SIOC*XARP ioctls */
841 842 /* 030 */ { SIOCSARP, sizeof (struct arpreq), IPI_PRIV | IPI_WR,
842 843 ARP_CMD, ip_sioctl_arp, NULL },
843 844 /* 031 */ { SIOCGARP, sizeof (struct arpreq), IPI_GET_CMD,
844 845 ARP_CMD, ip_sioctl_arp, NULL },
845 846 /* 032 */ { SIOCDARP, sizeof (struct arpreq), IPI_PRIV | IPI_WR,
846 847 ARP_CMD, ip_sioctl_arp, NULL },
847 848
848 849 /* 033 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
849 850 /* 034 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
850 851 /* 035 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
851 852 /* 036 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
852 853 /* 037 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
853 854 /* 038 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
854 855 /* 039 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
855 856 /* 040 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
856 857 /* 041 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
857 858 /* 042 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
858 859 /* 043 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
859 860 /* 044 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
860 861 /* 045 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
861 862 /* 046 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
862 863 /* 047 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
863 864 /* 048 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
864 865 /* 049 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
865 866 /* 050 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
866 867 /* 051 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
867 868 /* 052 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
868 869 /* 053 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
869 870
870 871 /* 054 */ { IF_UNITSEL, sizeof (int), IPI_PRIV | IPI_WR | IPI_MODOK,
871 872 MISC_CMD, if_unitsel, if_unitsel_restart },
872 873
873 874 /* 055 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
874 875 /* 056 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
875 876 /* 057 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
876 877 /* 058 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
877 878 /* 059 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
878 879 /* 060 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
879 880 /* 061 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
880 881 /* 062 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
881 882 /* 063 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
882 883 /* 064 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
883 884 /* 065 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
884 885 /* 066 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
885 886 /* 067 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
886 887 /* 068 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
887 888 /* 069 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
888 889 /* 070 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
889 890 /* 071 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
890 891 /* 072 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
891 892
892 893 /* 073 */ { SIOCSIFNAME, sizeof (struct ifreq),
893 894 IPI_PRIV | IPI_WR | IPI_MODOK,
894 895 IF_CMD, ip_sioctl_sifname, NULL },
895 896
896 897 /* 074 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
897 898 /* 075 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
898 899 /* 076 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
899 900 /* 077 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
900 901 /* 078 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
901 902 /* 079 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
902 903 /* 080 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
903 904 /* 081 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
904 905 /* 082 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
905 906 /* 083 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
906 907 /* 084 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
907 908 /* 085 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
908 909 /* 086 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
909 910
910 911 /* 087 */ { SIOCGIFNUM, sizeof (int), IPI_GET_CMD,
911 912 MISC_CMD, ip_sioctl_get_ifnum, NULL },
912 913 /* 088 */ { SIOCGIFMUXID, sizeof (struct ifreq), IPI_GET_CMD,
913 914 IF_CMD, ip_sioctl_get_muxid, NULL },
914 915 /* 089 */ { SIOCSIFMUXID, sizeof (struct ifreq),
915 916 IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_muxid, NULL },
916 917
917 918 /* Both if and lif variants share same func */
918 919 /* 090 */ { SIOCGIFINDEX, sizeof (struct ifreq), IPI_GET_CMD,
919 920 IF_CMD, ip_sioctl_get_lifindex, NULL },
920 921 /* Both if and lif variants share same func */
921 922 /* 091 */ { SIOCSIFINDEX, sizeof (struct ifreq),
922 923 IPI_PRIV | IPI_WR, IF_CMD, ip_sioctl_slifindex, NULL },
923 924
924 925 /* copyin size cannot be coded for SIOCGIFCONF */
925 926 /* 092 */ { SIOCGIFCONF, 0, IPI_GET_CMD,
926 927 MISC_CMD, ip_sioctl_get_ifconf, NULL },
927 928 /* 093 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
928 929 /* 094 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
929 930 /* 095 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
930 931 /* 096 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
931 932 /* 097 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
932 933 /* 098 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
933 934 /* 099 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
934 935 /* 100 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
935 936 /* 101 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
936 937 /* 102 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
937 938 /* 103 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
938 939 /* 104 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
939 940 /* 105 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
940 941 /* 106 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
941 942 /* 107 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
942 943 /* 108 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
943 944 /* 109 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
944 945
945 946 /* 110 */ { SIOCLIFREMOVEIF, sizeof (struct lifreq),
946 947 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_removeif,
947 948 ip_sioctl_removeif_restart },
948 949 /* 111 */ { SIOCLIFADDIF, sizeof (struct lifreq),
949 950 IPI_GET_CMD | IPI_PRIV | IPI_WR,
950 951 LIF_CMD, ip_sioctl_addif, NULL },
951 952 #define SIOCLIFADDR_NDX 112
952 953 /* 112 */ { SIOCSLIFADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
953 954 LIF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart },
954 955 /* 113 */ { SIOCGLIFADDR, sizeof (struct lifreq),
955 956 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_addr, NULL },
956 957 /* 114 */ { SIOCSLIFDSTADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
957 958 LIF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart },
958 959 /* 115 */ { SIOCGLIFDSTADDR, sizeof (struct lifreq),
959 960 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_dstaddr, NULL },
960 961 /* 116 */ { SIOCSLIFFLAGS, sizeof (struct lifreq),
961 962 IPI_PRIV | IPI_WR,
962 963 LIF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart },
963 964 /* 117 */ { SIOCGLIFFLAGS, sizeof (struct lifreq),
964 965 IPI_GET_CMD | IPI_MODOK,
965 966 LIF_CMD, ip_sioctl_get_flags, NULL },
966 967
967 968 /* 118 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
968 969 /* 119 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
969 970
970 971 /* 120 */ { O_SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD,
971 972 ip_sioctl_get_lifconf, NULL },
972 973 /* 121 */ { SIOCSLIFMTU, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
973 974 LIF_CMD, ip_sioctl_mtu, NULL },
974 975 /* 122 */ { SIOCGLIFMTU, sizeof (struct lifreq), IPI_GET_CMD,
975 976 LIF_CMD, ip_sioctl_get_mtu, NULL },
976 977 /* 123 */ { SIOCGLIFBRDADDR, sizeof (struct lifreq),
977 978 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_brdaddr, NULL },
978 979 /* 124 */ { SIOCSLIFBRDADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
979 980 LIF_CMD, ip_sioctl_brdaddr, NULL },
980 981 /* 125 */ { SIOCGLIFNETMASK, sizeof (struct lifreq),
981 982 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_netmask, NULL },
982 983 /* 126 */ { SIOCSLIFNETMASK, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
983 984 LIF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart },
984 985 /* 127 */ { SIOCGLIFMETRIC, sizeof (struct lifreq),
985 986 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_metric, NULL },
986 987 /* 128 */ { SIOCSLIFMETRIC, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
987 988 LIF_CMD, ip_sioctl_metric, NULL },
988 989 /* 129 */ { SIOCSLIFNAME, sizeof (struct lifreq),
989 990 IPI_PRIV | IPI_WR | IPI_MODOK,
990 991 LIF_CMD, ip_sioctl_slifname,
991 992 ip_sioctl_slifname_restart },
992 993
993 994 /* 130 */ { SIOCGLIFNUM, sizeof (struct lifnum), IPI_GET_CMD,
994 995 MISC_CMD, ip_sioctl_get_lifnum, NULL },
995 996 /* 131 */ { SIOCGLIFMUXID, sizeof (struct lifreq),
996 997 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_muxid, NULL },
997 998 /* 132 */ { SIOCSLIFMUXID, sizeof (struct lifreq),
998 999 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_muxid, NULL },
999 1000 /* 133 */ { SIOCGLIFINDEX, sizeof (struct lifreq),
1000 1001 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifindex, 0 },
1001 1002 /* 134 */ { SIOCSLIFINDEX, sizeof (struct lifreq),
1002 1003 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifindex, 0 },
1003 1004 /* 135 */ { SIOCSLIFTOKEN, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1004 1005 LIF_CMD, ip_sioctl_token, NULL },
1005 1006 /* 136 */ { SIOCGLIFTOKEN, sizeof (struct lifreq),
1006 1007 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_token, NULL },
1007 1008 /* 137 */ { SIOCSLIFSUBNET, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1008 1009 LIF_CMD, ip_sioctl_subnet, ip_sioctl_subnet_restart },
1009 1010 /* 138 */ { SIOCGLIFSUBNET, sizeof (struct lifreq),
1010 1011 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_subnet, NULL },
1011 1012 /* 139 */ { SIOCSLIFLNKINFO, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1012 1013 LIF_CMD, ip_sioctl_lnkinfo, NULL },
1013 1014
1014 1015 /* 140 */ { SIOCGLIFLNKINFO, sizeof (struct lifreq),
1015 1016 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lnkinfo, NULL },
1016 1017 /* 141 */ { SIOCLIFDELND, sizeof (struct lifreq), IPI_PRIV,
1017 1018 LIF_CMD, ip_siocdelndp_v6, NULL },
1018 1019 /* 142 */ { SIOCLIFGETND, sizeof (struct lifreq), IPI_GET_CMD,
1019 1020 LIF_CMD, ip_siocqueryndp_v6, NULL },
|
↓ open down ↓ |
984 lines elided |
↑ open up ↑ |
1020 1021 /* 143 */ { SIOCLIFSETND, sizeof (struct lifreq), IPI_PRIV,
1021 1022 LIF_CMD, ip_siocsetndp_v6, NULL },
1022 1023 /* 144 */ { SIOCTMYADDR, sizeof (struct sioc_addrreq), IPI_GET_CMD,
1023 1024 MISC_CMD, ip_sioctl_tmyaddr, NULL },
1024 1025 /* 145 */ { SIOCTONLINK, sizeof (struct sioc_addrreq), IPI_GET_CMD,
1025 1026 MISC_CMD, ip_sioctl_tonlink, NULL },
1026 1027 /* 146 */ { SIOCTMYSITE, sizeof (struct sioc_addrreq), 0,
1027 1028 MISC_CMD, ip_sioctl_tmysite, NULL },
1028 1029 /* 147 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1029 1030 /* 148 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1030 - /* IPSECioctls handled in ip_sioctl_copyin_setup itself */
1031 - /* 149 */ { SIOCFIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL },
1032 - /* 150 */ { SIOCSIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL },
1033 - /* 151 */ { SIOCDIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL },
1034 - /* 152 */ { SIOCLIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL },
1035 1031
1032 + /* Old *IPSECONFIG ioctls are now deprecated, now see spdsock.c */
1033 + /* 149 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1034 + /* 150 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1035 + /* 151 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1036 + /* 152 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1037 +
1036 1038 /* 153 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1037 1039
1038 1040 /* 154 */ { SIOCGLIFBINDING, sizeof (struct lifreq), IPI_GET_CMD,
1039 1041 LIF_CMD, ip_sioctl_get_binding, NULL },
1040 1042 /* 155 */ { SIOCSLIFGROUPNAME, sizeof (struct lifreq),
1041 1043 IPI_PRIV | IPI_WR,
1042 1044 LIF_CMD, ip_sioctl_groupname, ip_sioctl_groupname },
1043 1045 /* 156 */ { SIOCGLIFGROUPNAME, sizeof (struct lifreq),
1044 1046 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_groupname, NULL },
1045 1047 /* 157 */ { SIOCGLIFGROUPINFO, sizeof (lifgroupinfo_t),
1046 1048 IPI_GET_CMD, MISC_CMD, ip_sioctl_groupinfo, NULL },
1047 1049
1048 1050 /* Leave 158-160 unused; used to be SIOC*IFARP ioctls */
1049 1051 /* 158 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1050 1052 /* 159 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1051 1053 /* 160 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1052 1054
1053 1055 /* 161 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1054 1056
1055 1057 /* These are handled in ip_sioctl_copyin_setup itself */
1056 1058 /* 162 */ { SIOCGIP6ADDRPOLICY, 0, IPI_NULL_BCONT,
1057 1059 MISC_CMD, NULL, NULL },
1058 1060 /* 163 */ { SIOCSIP6ADDRPOLICY, 0, IPI_PRIV | IPI_NULL_BCONT,
1059 1061 MISC_CMD, NULL, NULL },
1060 1062 /* 164 */ { SIOCGDSTINFO, 0, IPI_GET_CMD, MISC_CMD, NULL, NULL },
1061 1063
1062 1064 /* 165 */ { SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD,
1063 1065 ip_sioctl_get_lifconf, NULL },
1064 1066
1065 1067 /* 166 */ { SIOCSXARP, sizeof (struct xarpreq), IPI_PRIV | IPI_WR,
1066 1068 XARP_CMD, ip_sioctl_arp, NULL },
1067 1069 /* 167 */ { SIOCGXARP, sizeof (struct xarpreq), IPI_GET_CMD,
1068 1070 XARP_CMD, ip_sioctl_arp, NULL },
1069 1071 /* 168 */ { SIOCDXARP, sizeof (struct xarpreq), IPI_PRIV | IPI_WR,
1070 1072 XARP_CMD, ip_sioctl_arp, NULL },
1071 1073
1072 1074 /* SIOCPOPSOCKFS is not handled by IP */
1073 1075 /* 169 */ { IPI_DONTCARE /* SIOCPOPSOCKFS */, 0, 0, 0, NULL, NULL },
1074 1076
1075 1077 /* 170 */ { SIOCGLIFZONE, sizeof (struct lifreq),
1076 1078 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_lifzone, NULL },
1077 1079 /* 171 */ { SIOCSLIFZONE, sizeof (struct lifreq),
1078 1080 IPI_PRIV | IPI_WR, LIF_CMD, ip_sioctl_slifzone,
1079 1081 ip_sioctl_slifzone_restart },
1080 1082 /* 172-174 are SCTP ioctls and not handled by IP */
1081 1083 /* 172 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1082 1084 /* 173 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1083 1085 /* 174 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1084 1086 /* 175 */ { SIOCGLIFUSESRC, sizeof (struct lifreq),
1085 1087 IPI_GET_CMD, LIF_CMD,
1086 1088 ip_sioctl_get_lifusesrc, 0 },
1087 1089 /* 176 */ { SIOCSLIFUSESRC, sizeof (struct lifreq),
1088 1090 IPI_PRIV | IPI_WR,
1089 1091 LIF_CMD, ip_sioctl_slifusesrc,
1090 1092 NULL },
1091 1093 /* 177 */ { SIOCGLIFSRCOF, 0, IPI_GET_CMD, MISC_CMD,
1092 1094 ip_sioctl_get_lifsrcof, NULL },
1093 1095 /* 178 */ { SIOCGMSFILTER, sizeof (struct group_filter), IPI_GET_CMD,
1094 1096 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1095 1097 /* 179 */ { SIOCSMSFILTER, sizeof (struct group_filter), 0,
1096 1098 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1097 1099 /* 180 */ { SIOCGIPMSFILTER, sizeof (struct ip_msfilter), IPI_GET_CMD,
1098 1100 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1099 1101 /* 181 */ { SIOCSIPMSFILTER, sizeof (struct ip_msfilter), 0,
1100 1102 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1101 1103 /* 182 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1102 1104 /* SIOCSENABLESDP is handled by SDP */
1103 1105 /* 183 */ { IPI_DONTCARE /* SIOCSENABLESDP */, 0, 0, 0, NULL, NULL },
1104 1106 /* 184 */ { IPI_DONTCARE /* SIOCSQPTR */, 0, 0, 0, NULL, NULL },
1105 1107 /* 185 */ { SIOCGIFHWADDR, sizeof (struct ifreq), IPI_GET_CMD,
1106 1108 IF_CMD, ip_sioctl_get_ifhwaddr, NULL },
1107 1109 /* 186 */ { IPI_DONTCARE /* SIOCGSTAMP */, 0, 0, 0, NULL, NULL },
1108 1110 /* 187 */ { SIOCILB, 0, IPI_PRIV | IPI_GET_CMD, MISC_CMD,
1109 1111 ip_sioctl_ilb_cmd, NULL },
1110 1112 /* 188 */ { SIOCGETPROP, 0, IPI_GET_CMD, 0, NULL, NULL },
1111 1113 /* 189 */ { SIOCSETPROP, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL},
1112 1114 /* 190 */ { SIOCGLIFDADSTATE, sizeof (struct lifreq),
1113 1115 IPI_GET_CMD, LIF_CMD, ip_sioctl_get_dadstate, NULL },
1114 1116 /* 191 */ { SIOCSLIFPREFIX, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1115 1117 LIF_CMD, ip_sioctl_prefix, ip_sioctl_prefix_restart },
1116 1118 /* 192 */ { SIOCGLIFHWADDR, sizeof (struct lifreq), IPI_GET_CMD,
1117 1119 LIF_CMD, ip_sioctl_get_lifhwaddr, NULL }
1118 1120 };
1119 1121
1120 1122 int ip_ndx_ioctl_count = sizeof (ip_ndx_ioctl_table) / sizeof (ip_ioctl_cmd_t);
1121 1123
1122 1124 ip_ioctl_cmd_t ip_misc_ioctl_table[] = {
1123 1125 { I_LINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1124 1126 { I_UNLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1125 1127 { I_PLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1126 1128 { I_PUNLINK, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1127 1129 { ND_GET, 0, 0, 0, NULL, NULL },
1128 1130 { ND_SET, 0, IPI_PRIV | IPI_WR, 0, NULL, NULL },
1129 1131 { IP_IOCTL, 0, 0, 0, NULL, NULL },
1130 1132 { SIOCGETVIFCNT, sizeof (struct sioc_vif_req), IPI_GET_CMD,
1131 1133 MISC_CMD, mrt_ioctl},
1132 1134 { SIOCGETSGCNT, sizeof (struct sioc_sg_req), IPI_GET_CMD,
1133 1135 MISC_CMD, mrt_ioctl},
1134 1136 { SIOCGETLSGCNT, sizeof (struct sioc_lsg_req), IPI_GET_CMD,
1135 1137 MISC_CMD, mrt_ioctl}
1136 1138 };
1137 1139
1138 1140 int ip_misc_ioctl_count =
1139 1141 sizeof (ip_misc_ioctl_table) / sizeof (ip_ioctl_cmd_t);
1140 1142
1141 1143 int conn_drain_nthreads; /* Number of drainers reqd. */
1142 1144 /* Settable in /etc/system */
1143 1145 /* Defined in ip_ire.c */
1144 1146 extern uint32_t ip_ire_max_bucket_cnt, ip6_ire_max_bucket_cnt;
1145 1147 extern uint32_t ip_ire_min_bucket_cnt, ip6_ire_min_bucket_cnt;
1146 1148 extern uint32_t ip_ire_mem_ratio, ip_ire_cpu_ratio;
1147 1149
1148 1150 static nv_t ire_nv_arr[] = {
1149 1151 { IRE_BROADCAST, "BROADCAST" },
1150 1152 { IRE_LOCAL, "LOCAL" },
1151 1153 { IRE_LOOPBACK, "LOOPBACK" },
1152 1154 { IRE_DEFAULT, "DEFAULT" },
1153 1155 { IRE_PREFIX, "PREFIX" },
1154 1156 { IRE_IF_NORESOLVER, "IF_NORESOL" },
1155 1157 { IRE_IF_RESOLVER, "IF_RESOLV" },
1156 1158 { IRE_IF_CLONE, "IF_CLONE" },
1157 1159 { IRE_HOST, "HOST" },
1158 1160 { IRE_MULTICAST, "MULTICAST" },
1159 1161 { IRE_NOROUTE, "NOROUTE" },
1160 1162 { 0 }
1161 1163 };
1162 1164
1163 1165 nv_t *ire_nv_tbl = ire_nv_arr;
1164 1166
1165 1167 /* Simple ICMP IP Header Template */
1166 1168 static ipha_t icmp_ipha = {
1167 1169 IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
1168 1170 };
1169 1171
1170 1172 struct module_info ip_mod_info = {
1171 1173 IP_MOD_ID, IP_MOD_NAME, IP_MOD_MINPSZ, IP_MOD_MAXPSZ, IP_MOD_HIWAT,
1172 1174 IP_MOD_LOWAT
1173 1175 };
1174 1176
1175 1177 /*
1176 1178 * Duplicate static symbols within a module confuses mdb; so we avoid the
1177 1179 * problem by making the symbols here distinct from those in udp.c.
1178 1180 */
1179 1181
1180 1182 /*
1181 1183 * Entry points for IP as a device and as a module.
1182 1184 * We have separate open functions for the /dev/ip and /dev/ip6 devices.
1183 1185 */
1184 1186 static struct qinit iprinitv4 = {
1185 1187 (pfi_t)ip_rput, NULL, ip_openv4, ip_close, NULL,
1186 1188 &ip_mod_info
1187 1189 };
1188 1190
1189 1191 struct qinit iprinitv6 = {
1190 1192 (pfi_t)ip_rput_v6, NULL, ip_openv6, ip_close, NULL,
1191 1193 &ip_mod_info
1192 1194 };
1193 1195
1194 1196 static struct qinit ipwinit = {
1195 1197 (pfi_t)ip_wput_nondata, (pfi_t)ip_wsrv, NULL, NULL, NULL,
1196 1198 &ip_mod_info
1197 1199 };
1198 1200
1199 1201 static struct qinit iplrinit = {
1200 1202 (pfi_t)ip_lrput, NULL, ip_openv4, ip_close, NULL,
1201 1203 &ip_mod_info
1202 1204 };
1203 1205
1204 1206 static struct qinit iplwinit = {
1205 1207 (pfi_t)ip_lwput, NULL, NULL, NULL, NULL,
1206 1208 &ip_mod_info
1207 1209 };
1208 1210
1209 1211 /* For AF_INET aka /dev/ip */
1210 1212 struct streamtab ipinfov4 = {
1211 1213 &iprinitv4, &ipwinit, &iplrinit, &iplwinit
1212 1214 };
1213 1215
1214 1216 /* For AF_INET6 aka /dev/ip6 */
1215 1217 struct streamtab ipinfov6 = {
1216 1218 &iprinitv6, &ipwinit, &iplrinit, &iplwinit
1217 1219 };
1218 1220
1219 1221 #ifdef DEBUG
1220 1222 boolean_t skip_sctp_cksum = B_FALSE;
1221 1223 #endif
1222 1224
1223 1225 /*
1224 1226 * Generate an ICMP fragmentation needed message.
1225 1227 * When called from ip_output side a minimal ip_recv_attr_t needs to be
1226 1228 * constructed by the caller.
1227 1229 */
1228 1230 void
1229 1231 icmp_frag_needed(mblk_t *mp, int mtu, ip_recv_attr_t *ira)
1230 1232 {
1231 1233 icmph_t icmph;
1232 1234 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
1233 1235
1234 1236 mp = icmp_pkt_err_ok(mp, ira);
1235 1237 if (mp == NULL)
1236 1238 return;
1237 1239
1238 1240 bzero(&icmph, sizeof (icmph_t));
1239 1241 icmph.icmph_type = ICMP_DEST_UNREACHABLE;
1240 1242 icmph.icmph_code = ICMP_FRAGMENTATION_NEEDED;
1241 1243 icmph.icmph_du_mtu = htons((uint16_t)mtu);
1242 1244 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutFragNeeded);
1243 1245 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs);
1244 1246
1245 1247 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
1246 1248 }
1247 1249
1248 1250 /*
1249 1251 * icmp_inbound_v4 deals with ICMP messages that are handled by IP.
1250 1252 * If the ICMP message is consumed by IP, i.e., it should not be delivered
1251 1253 * to any IPPROTO_ICMP raw sockets, then it returns NULL.
1252 1254 * Likewise, if the ICMP error is misformed (too short, etc), then it
1253 1255 * returns NULL. The caller uses this to determine whether or not to send
1254 1256 * to raw sockets.
1255 1257 *
1256 1258 * All error messages are passed to the matching transport stream.
1257 1259 *
1258 1260 * The following cases are handled by icmp_inbound:
1259 1261 * 1) It needs to send a reply back and possibly delivering it
1260 1262 * to the "interested" upper clients.
1261 1263 * 2) Return the mblk so that the caller can pass it to the RAW socket clients.
1262 1264 * 3) It needs to change some values in IP only.
1263 1265 * 4) It needs to change some values in IP and upper layers e.g TCP
1264 1266 * by delivering an error to the upper layers.
1265 1267 *
1266 1268 * We handle the above three cases in the context of IPsec in the
1267 1269 * following way :
1268 1270 *
1269 1271 * 1) Send the reply back in the same way as the request came in.
1270 1272 * If it came in encrypted, it goes out encrypted. If it came in
1271 1273 * clear, it goes out in clear. Thus, this will prevent chosen
1272 1274 * plain text attack.
1273 1275 * 2) The client may or may not expect things to come in secure.
1274 1276 * If it comes in secure, the policy constraints are checked
1275 1277 * before delivering it to the upper layers. If it comes in
1276 1278 * clear, ipsec_inbound_accept_clear will decide whether to
1277 1279 * accept this in clear or not. In both the cases, if the returned
1278 1280 * message (IP header + 8 bytes) that caused the icmp message has
1279 1281 * AH/ESP headers, it is sent up to AH/ESP for validation before
1280 1282 * sending up. If there are only 8 bytes of returned message, then
1281 1283 * upper client will not be notified.
1282 1284 * 3) Check with global policy to see whether it matches the constaints.
1283 1285 * But this will be done only if icmp_accept_messages_in_clear is
1284 1286 * zero.
1285 1287 * 4) If we need to change both in IP and ULP, then the decision taken
1286 1288 * while affecting the values in IP and while delivering up to TCP
1287 1289 * should be the same.
1288 1290 *
1289 1291 * There are two cases.
1290 1292 *
1291 1293 * a) If we reject data at the IP layer (ipsec_check_global_policy()
1292 1294 * failed), we will not deliver it to the ULP, even though they
1293 1295 * are *willing* to accept in *clear*. This is fine as our global
1294 1296 * disposition to icmp messages asks us reject the datagram.
1295 1297 *
1296 1298 * b) If we accept data at the IP layer (ipsec_check_global_policy()
1297 1299 * succeeded or icmp_accept_messages_in_clear is 1), and not able
1298 1300 * to deliver it to ULP (policy failed), it can lead to
1299 1301 * consistency problems. The cases known at this time are
1300 1302 * ICMP_DESTINATION_UNREACHABLE messages with following code
1301 1303 * values :
1302 1304 *
1303 1305 * - ICMP_FRAGMENTATION_NEEDED : IP adapts to the new value
1304 1306 * and Upper layer rejects. Then the communication will
1305 1307 * come to a stop. This is solved by making similar decisions
1306 1308 * at both levels. Currently, when we are unable to deliver
1307 1309 * to the Upper Layer (due to policy failures) while IP has
1308 1310 * adjusted dce_pmtu, the next outbound datagram would
1309 1311 * generate a local ICMP_FRAGMENTATION_NEEDED message - which
1310 1312 * will be with the right level of protection. Thus the right
1311 1313 * value will be communicated even if we are not able to
1312 1314 * communicate when we get from the wire initially. But this
1313 1315 * assumes there would be at least one outbound datagram after
1314 1316 * IP has adjusted its dce_pmtu value. To make things
1315 1317 * simpler, we accept in clear after the validation of
1316 1318 * AH/ESP headers.
1317 1319 *
1318 1320 * - Other ICMP ERRORS : We may not be able to deliver it to the
1319 1321 * upper layer depending on the level of protection the upper
1320 1322 * layer expects and the disposition in ipsec_inbound_accept_clear().
1321 1323 * ipsec_inbound_accept_clear() decides whether a given ICMP error
1322 1324 * should be accepted in clear when the Upper layer expects secure.
1323 1325 * Thus the communication may get aborted by some bad ICMP
1324 1326 * packets.
1325 1327 */
1326 1328 mblk_t *
1327 1329 icmp_inbound_v4(mblk_t *mp, ip_recv_attr_t *ira)
1328 1330 {
1329 1331 icmph_t *icmph;
1330 1332 ipha_t *ipha; /* Outer header */
1331 1333 int ip_hdr_length; /* Outer header length */
1332 1334 boolean_t interested;
1333 1335 ipif_t *ipif;
1334 1336 uint32_t ts;
1335 1337 uint32_t *tsp;
1336 1338 timestruc_t now;
1337 1339 ill_t *ill = ira->ira_ill;
1338 1340 ip_stack_t *ipst = ill->ill_ipst;
1339 1341 zoneid_t zoneid = ira->ira_zoneid;
1340 1342 int len_needed;
1341 1343 mblk_t *mp_ret = NULL;
1342 1344
1343 1345 ipha = (ipha_t *)mp->b_rptr;
1344 1346
1345 1347 BUMP_MIB(&ipst->ips_icmp_mib, icmpInMsgs);
1346 1348
1347 1349 ip_hdr_length = ira->ira_ip_hdr_length;
1348 1350 if ((mp->b_wptr - mp->b_rptr) < (ip_hdr_length + ICMPH_SIZE)) {
1349 1351 if (ira->ira_pktlen < (ip_hdr_length + ICMPH_SIZE)) {
1350 1352 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
1351 1353 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
1352 1354 freemsg(mp);
1353 1355 return (NULL);
1354 1356 }
1355 1357 /* Last chance to get real. */
1356 1358 ipha = ip_pullup(mp, ip_hdr_length + ICMPH_SIZE, ira);
1357 1359 if (ipha == NULL) {
1358 1360 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
1359 1361 freemsg(mp);
1360 1362 return (NULL);
1361 1363 }
1362 1364 }
1363 1365
1364 1366 /* The IP header will always be a multiple of four bytes */
1365 1367 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1366 1368 ip2dbg(("icmp_inbound_v4: type %d code %d\n", icmph->icmph_type,
1367 1369 icmph->icmph_code));
1368 1370
1369 1371 /*
1370 1372 * We will set "interested" to "true" if we should pass a copy to
1371 1373 * the transport or if we handle the packet locally.
1372 1374 */
1373 1375 interested = B_FALSE;
1374 1376 switch (icmph->icmph_type) {
1375 1377 case ICMP_ECHO_REPLY:
1376 1378 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchoReps);
1377 1379 break;
1378 1380 case ICMP_DEST_UNREACHABLE:
1379 1381 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED)
1380 1382 BUMP_MIB(&ipst->ips_icmp_mib, icmpInFragNeeded);
1381 1383 interested = B_TRUE; /* Pass up to transport */
1382 1384 BUMP_MIB(&ipst->ips_icmp_mib, icmpInDestUnreachs);
1383 1385 break;
1384 1386 case ICMP_SOURCE_QUENCH:
1385 1387 interested = B_TRUE; /* Pass up to transport */
1386 1388 BUMP_MIB(&ipst->ips_icmp_mib, icmpInSrcQuenchs);
1387 1389 break;
1388 1390 case ICMP_REDIRECT:
1389 1391 if (!ipst->ips_ip_ignore_redirect)
1390 1392 interested = B_TRUE;
1391 1393 BUMP_MIB(&ipst->ips_icmp_mib, icmpInRedirects);
1392 1394 break;
1393 1395 case ICMP_ECHO_REQUEST:
1394 1396 /*
1395 1397 * Whether to respond to echo requests that come in as IP
1396 1398 * broadcasts or as IP multicast is subject to debate
1397 1399 * (what isn't?). We aim to please, you pick it.
1398 1400 * Default is do it.
1399 1401 */
1400 1402 if (ira->ira_flags & IRAF_MULTICAST) {
1401 1403 /* multicast: respond based on tunable */
1402 1404 interested = ipst->ips_ip_g_resp_to_echo_mcast;
1403 1405 } else if (ira->ira_flags & IRAF_BROADCAST) {
1404 1406 /* broadcast: respond based on tunable */
1405 1407 interested = ipst->ips_ip_g_resp_to_echo_bcast;
1406 1408 } else {
1407 1409 /* unicast: always respond */
1408 1410 interested = B_TRUE;
1409 1411 }
1410 1412 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchos);
1411 1413 if (!interested) {
1412 1414 /* We never pass these to RAW sockets */
1413 1415 freemsg(mp);
1414 1416 return (NULL);
1415 1417 }
1416 1418
1417 1419 /* Check db_ref to make sure we can modify the packet. */
1418 1420 if (mp->b_datap->db_ref > 1) {
1419 1421 mblk_t *mp1;
1420 1422
1421 1423 mp1 = copymsg(mp);
1422 1424 freemsg(mp);
1423 1425 if (!mp1) {
1424 1426 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1425 1427 return (NULL);
1426 1428 }
1427 1429 mp = mp1;
1428 1430 ipha = (ipha_t *)mp->b_rptr;
1429 1431 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1430 1432 }
1431 1433 icmph->icmph_type = ICMP_ECHO_REPLY;
1432 1434 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutEchoReps);
1433 1435 icmp_send_reply_v4(mp, ipha, icmph, ira);
1434 1436 return (NULL);
1435 1437
1436 1438 case ICMP_ROUTER_ADVERTISEMENT:
1437 1439 case ICMP_ROUTER_SOLICITATION:
1438 1440 break;
1439 1441 case ICMP_TIME_EXCEEDED:
1440 1442 interested = B_TRUE; /* Pass up to transport */
1441 1443 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimeExcds);
1442 1444 break;
1443 1445 case ICMP_PARAM_PROBLEM:
1444 1446 interested = B_TRUE; /* Pass up to transport */
1445 1447 BUMP_MIB(&ipst->ips_icmp_mib, icmpInParmProbs);
1446 1448 break;
1447 1449 case ICMP_TIME_STAMP_REQUEST:
1448 1450 /* Response to Time Stamp Requests is local policy. */
1449 1451 if (ipst->ips_ip_g_resp_to_timestamp) {
1450 1452 if (ira->ira_flags & IRAF_MULTIBROADCAST)
1451 1453 interested =
1452 1454 ipst->ips_ip_g_resp_to_timestamp_bcast;
1453 1455 else
1454 1456 interested = B_TRUE;
1455 1457 }
1456 1458 if (!interested) {
1457 1459 /* We never pass these to RAW sockets */
1458 1460 freemsg(mp);
1459 1461 return (NULL);
1460 1462 }
1461 1463
1462 1464 /* Make sure we have enough of the packet */
1463 1465 len_needed = ip_hdr_length + ICMPH_SIZE +
1464 1466 3 * sizeof (uint32_t);
1465 1467
1466 1468 if (mp->b_wptr - mp->b_rptr < len_needed) {
1467 1469 ipha = ip_pullup(mp, len_needed, ira);
1468 1470 if (ipha == NULL) {
1469 1471 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1470 1472 ip_drop_input("ipIfStatsInDiscards - ip_pullup",
1471 1473 mp, ill);
1472 1474 freemsg(mp);
1473 1475 return (NULL);
1474 1476 }
1475 1477 /* Refresh following the pullup. */
1476 1478 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1477 1479 }
1478 1480 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestamps);
1479 1481 /* Check db_ref to make sure we can modify the packet. */
1480 1482 if (mp->b_datap->db_ref > 1) {
1481 1483 mblk_t *mp1;
1482 1484
1483 1485 mp1 = copymsg(mp);
1484 1486 freemsg(mp);
1485 1487 if (!mp1) {
1486 1488 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1487 1489 return (NULL);
1488 1490 }
1489 1491 mp = mp1;
1490 1492 ipha = (ipha_t *)mp->b_rptr;
1491 1493 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1492 1494 }
1493 1495 icmph->icmph_type = ICMP_TIME_STAMP_REPLY;
1494 1496 tsp = (uint32_t *)&icmph[1];
1495 1497 tsp++; /* Skip past 'originate time' */
1496 1498 /* Compute # of milliseconds since midnight */
1497 1499 gethrestime(&now);
1498 1500 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
1499 1501 now.tv_nsec / (NANOSEC / MILLISEC);
1500 1502 *tsp++ = htonl(ts); /* Lay in 'receive time' */
1501 1503 *tsp++ = htonl(ts); /* Lay in 'send time' */
1502 1504 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimestampReps);
1503 1505 icmp_send_reply_v4(mp, ipha, icmph, ira);
1504 1506 return (NULL);
1505 1507
1506 1508 case ICMP_TIME_STAMP_REPLY:
1507 1509 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestampReps);
1508 1510 break;
1509 1511 case ICMP_INFO_REQUEST:
1510 1512 /* Per RFC 1122 3.2.2.7, ignore this. */
1511 1513 case ICMP_INFO_REPLY:
1512 1514 break;
1513 1515 case ICMP_ADDRESS_MASK_REQUEST:
1514 1516 if (ira->ira_flags & IRAF_MULTIBROADCAST) {
1515 1517 interested =
1516 1518 ipst->ips_ip_respond_to_address_mask_broadcast;
1517 1519 } else {
1518 1520 interested = B_TRUE;
1519 1521 }
1520 1522 if (!interested) {
1521 1523 /* We never pass these to RAW sockets */
1522 1524 freemsg(mp);
1523 1525 return (NULL);
1524 1526 }
1525 1527 len_needed = ip_hdr_length + ICMPH_SIZE + IP_ADDR_LEN;
1526 1528 if (mp->b_wptr - mp->b_rptr < len_needed) {
1527 1529 ipha = ip_pullup(mp, len_needed, ira);
1528 1530 if (ipha == NULL) {
1529 1531 BUMP_MIB(ill->ill_ip_mib,
1530 1532 ipIfStatsInTruncatedPkts);
1531 1533 ip_drop_input("ipIfStatsInTruncatedPkts", mp,
1532 1534 ill);
1533 1535 freemsg(mp);
1534 1536 return (NULL);
1535 1537 }
1536 1538 /* Refresh following the pullup. */
1537 1539 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1538 1540 }
1539 1541 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMasks);
1540 1542 /* Check db_ref to make sure we can modify the packet. */
1541 1543 if (mp->b_datap->db_ref > 1) {
1542 1544 mblk_t *mp1;
1543 1545
1544 1546 mp1 = copymsg(mp);
1545 1547 freemsg(mp);
1546 1548 if (!mp1) {
1547 1549 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1548 1550 return (NULL);
1549 1551 }
1550 1552 mp = mp1;
1551 1553 ipha = (ipha_t *)mp->b_rptr;
1552 1554 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1553 1555 }
1554 1556 /*
1555 1557 * Need the ipif with the mask be the same as the source
1556 1558 * address of the mask reply. For unicast we have a specific
1557 1559 * ipif. For multicast/broadcast we only handle onlink
1558 1560 * senders, and use the source address to pick an ipif.
1559 1561 */
1560 1562 ipif = ipif_lookup_addr(ipha->ipha_dst, ill, zoneid, ipst);
1561 1563 if (ipif == NULL) {
1562 1564 /* Broadcast or multicast */
1563 1565 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid);
1564 1566 if (ipif == NULL) {
1565 1567 freemsg(mp);
1566 1568 return (NULL);
1567 1569 }
1568 1570 }
1569 1571 icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
1570 1572 bcopy(&ipif->ipif_net_mask, &icmph[1], IP_ADDR_LEN);
1571 1573 ipif_refrele(ipif);
1572 1574 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutAddrMaskReps);
1573 1575 icmp_send_reply_v4(mp, ipha, icmph, ira);
1574 1576 return (NULL);
1575 1577
1576 1578 case ICMP_ADDRESS_MASK_REPLY:
1577 1579 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMaskReps);
1578 1580 break;
1579 1581 default:
1580 1582 interested = B_TRUE; /* Pass up to transport */
1581 1583 BUMP_MIB(&ipst->ips_icmp_mib, icmpInUnknowns);
1582 1584 break;
1583 1585 }
1584 1586 /*
1585 1587 * See if there is an ICMP client to avoid an extra copymsg/freemsg
1586 1588 * if there isn't one.
1587 1589 */
1588 1590 if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_ICMP].connf_head != NULL) {
1589 1591 /* If there is an ICMP client and we want one too, copy it. */
1590 1592
1591 1593 if (!interested) {
1592 1594 /* Caller will deliver to RAW sockets */
1593 1595 return (mp);
1594 1596 }
1595 1597 mp_ret = copymsg(mp);
1596 1598 if (mp_ret == NULL) {
1597 1599 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1598 1600 ip_drop_input("ipIfStatsInDiscards - copymsg", mp, ill);
1599 1601 }
1600 1602 } else if (!interested) {
1601 1603 /* Neither we nor raw sockets are interested. Drop packet now */
1602 1604 freemsg(mp);
1603 1605 return (NULL);
1604 1606 }
1605 1607
1606 1608 /*
1607 1609 * ICMP error or redirect packet. Make sure we have enough of
1608 1610 * the header and that db_ref == 1 since we might end up modifying
1609 1611 * the packet.
1610 1612 */
1611 1613 if (mp->b_cont != NULL) {
1612 1614 if (ip_pullup(mp, -1, ira) == NULL) {
1613 1615 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1614 1616 ip_drop_input("ipIfStatsInDiscards - ip_pullup",
1615 1617 mp, ill);
1616 1618 freemsg(mp);
1617 1619 return (mp_ret);
1618 1620 }
1619 1621 }
1620 1622
1621 1623 if (mp->b_datap->db_ref > 1) {
1622 1624 mblk_t *mp1;
1623 1625
1624 1626 mp1 = copymsg(mp);
1625 1627 if (mp1 == NULL) {
1626 1628 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1627 1629 ip_drop_input("ipIfStatsInDiscards - copymsg", mp, ill);
1628 1630 freemsg(mp);
1629 1631 return (mp_ret);
1630 1632 }
1631 1633 freemsg(mp);
1632 1634 mp = mp1;
1633 1635 }
1634 1636
1635 1637 /*
1636 1638 * In case mp has changed, verify the message before any further
1637 1639 * processes.
1638 1640 */
1639 1641 ipha = (ipha_t *)mp->b_rptr;
1640 1642 icmph = (icmph_t *)&mp->b_rptr[ip_hdr_length];
1641 1643 if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
1642 1644 freemsg(mp);
1643 1645 return (mp_ret);
1644 1646 }
1645 1647
1646 1648 switch (icmph->icmph_type) {
1647 1649 case ICMP_REDIRECT:
1648 1650 icmp_redirect_v4(mp, ipha, icmph, ira);
1649 1651 break;
1650 1652 case ICMP_DEST_UNREACHABLE:
1651 1653 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) {
1652 1654 /* Update DCE and adjust MTU is icmp header if needed */
1653 1655 icmp_inbound_too_big_v4(icmph, ira);
1654 1656 }
1655 1657 /* FALLTHRU */
1656 1658 default:
1657 1659 icmp_inbound_error_fanout_v4(mp, icmph, ira);
1658 1660 break;
1659 1661 }
1660 1662 return (mp_ret);
1661 1663 }
1662 1664
1663 1665 /*
1664 1666 * Send an ICMP echo, timestamp or address mask reply.
1665 1667 * The caller has already updated the payload part of the packet.
1666 1668 * We handle the ICMP checksum, IP source address selection and feed
1667 1669 * the packet into ip_output_simple.
1668 1670 */
1669 1671 static void
1670 1672 icmp_send_reply_v4(mblk_t *mp, ipha_t *ipha, icmph_t *icmph,
1671 1673 ip_recv_attr_t *ira)
1672 1674 {
1673 1675 uint_t ip_hdr_length = ira->ira_ip_hdr_length;
1674 1676 ill_t *ill = ira->ira_ill;
1675 1677 ip_stack_t *ipst = ill->ill_ipst;
1676 1678 ip_xmit_attr_t ixas;
1677 1679
1678 1680 /* Send out an ICMP packet */
1679 1681 icmph->icmph_checksum = 0;
1680 1682 icmph->icmph_checksum = IP_CSUM(mp, ip_hdr_length, 0);
1681 1683 /* Reset time to live. */
1682 1684 ipha->ipha_ttl = ipst->ips_ip_def_ttl;
1683 1685 {
1684 1686 /* Swap source and destination addresses */
1685 1687 ipaddr_t tmp;
1686 1688
1687 1689 tmp = ipha->ipha_src;
1688 1690 ipha->ipha_src = ipha->ipha_dst;
1689 1691 ipha->ipha_dst = tmp;
1690 1692 }
1691 1693 ipha->ipha_ident = 0;
1692 1694 if (!IS_SIMPLE_IPH(ipha))
1693 1695 icmp_options_update(ipha);
1694 1696
1695 1697 bzero(&ixas, sizeof (ixas));
1696 1698 ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
1697 1699 ixas.ixa_zoneid = ira->ira_zoneid;
1698 1700 ixas.ixa_cred = kcred;
1699 1701 ixas.ixa_cpid = NOPID;
1700 1702 ixas.ixa_tsl = ira->ira_tsl; /* Behave as a multi-level responder */
1701 1703 ixas.ixa_ifindex = 0;
1702 1704 ixas.ixa_ipst = ipst;
1703 1705 ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
1704 1706
1705 1707 if (!(ira->ira_flags & IRAF_IPSEC_SECURE)) {
1706 1708 /*
1707 1709 * This packet should go out the same way as it
1708 1710 * came in i.e in clear, independent of the IPsec policy
1709 1711 * for transmitting packets.
1710 1712 */
1711 1713 ixas.ixa_flags |= IXAF_NO_IPSEC;
1712 1714 } else {
1713 1715 if (!ipsec_in_to_out(ira, &ixas, mp, ipha, NULL)) {
1714 1716 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1715 1717 /* Note: mp already consumed and ip_drop_packet done */
1716 1718 return;
1717 1719 }
1718 1720 }
1719 1721 if (ira->ira_flags & IRAF_MULTIBROADCAST) {
1720 1722 /*
1721 1723 * Not one or our addresses (IRE_LOCALs), thus we let
1722 1724 * ip_output_simple pick the source.
1723 1725 */
1724 1726 ipha->ipha_src = INADDR_ANY;
1725 1727 ixas.ixa_flags |= IXAF_SET_SOURCE;
1726 1728 }
1727 1729 /* Should we send with DF and use dce_pmtu? */
1728 1730 if (ipst->ips_ipv4_icmp_return_pmtu) {
1729 1731 ixas.ixa_flags |= IXAF_PMTU_DISCOVERY;
1730 1732 ipha->ipha_fragment_offset_and_flags |= IPH_DF_HTONS;
1731 1733 }
1732 1734
1733 1735 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs);
1734 1736
1735 1737 (void) ip_output_simple(mp, &ixas);
1736 1738 ixa_cleanup(&ixas);
1737 1739 }
1738 1740
1739 1741 /*
1740 1742 * Verify the ICMP messages for either for ICMP error or redirect packet.
1741 1743 * The caller should have fully pulled up the message. If it's a redirect
1742 1744 * packet, only basic checks on IP header will be done; otherwise, verify
1743 1745 * the packet by looking at the included ULP header.
1744 1746 *
1745 1747 * Called before icmp_inbound_error_fanout_v4 is called.
1746 1748 */
1747 1749 static boolean_t
1748 1750 icmp_inbound_verify_v4(mblk_t *mp, icmph_t *icmph, ip_recv_attr_t *ira)
1749 1751 {
1750 1752 ill_t *ill = ira->ira_ill;
1751 1753 int hdr_length;
1752 1754 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
1753 1755 conn_t *connp;
1754 1756 ipha_t *ipha; /* Inner IP header */
1755 1757
1756 1758 ipha = (ipha_t *)&icmph[1];
1757 1759 if ((uchar_t *)ipha + IP_SIMPLE_HDR_LENGTH > mp->b_wptr)
1758 1760 goto truncated;
1759 1761
1760 1762 hdr_length = IPH_HDR_LENGTH(ipha);
1761 1763
1762 1764 if ((IPH_HDR_VERSION(ipha) != IPV4_VERSION))
1763 1765 goto discard_pkt;
1764 1766
1765 1767 if (hdr_length < sizeof (ipha_t))
1766 1768 goto truncated;
1767 1769
1768 1770 if ((uchar_t *)ipha + hdr_length > mp->b_wptr)
1769 1771 goto truncated;
1770 1772
1771 1773 /*
1772 1774 * Stop here for ICMP_REDIRECT.
1773 1775 */
1774 1776 if (icmph->icmph_type == ICMP_REDIRECT)
1775 1777 return (B_TRUE);
1776 1778
1777 1779 /*
1778 1780 * ICMP errors only.
1779 1781 */
1780 1782 switch (ipha->ipha_protocol) {
1781 1783 case IPPROTO_UDP:
1782 1784 /*
1783 1785 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1784 1786 * transport header.
1785 1787 */
1786 1788 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1787 1789 mp->b_wptr)
1788 1790 goto truncated;
1789 1791 break;
1790 1792 case IPPROTO_TCP: {
1791 1793 tcpha_t *tcpha;
1792 1794
1793 1795 /*
1794 1796 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1795 1797 * transport header.
1796 1798 */
1797 1799 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1798 1800 mp->b_wptr)
1799 1801 goto truncated;
1800 1802
1801 1803 tcpha = (tcpha_t *)((uchar_t *)ipha + hdr_length);
1802 1804 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcpha, TCPS_LISTEN,
1803 1805 ipst);
1804 1806 if (connp == NULL)
1805 1807 goto discard_pkt;
1806 1808
1807 1809 if ((connp->conn_verifyicmp != NULL) &&
1808 1810 !connp->conn_verifyicmp(connp, tcpha, icmph, NULL, ira)) {
1809 1811 CONN_DEC_REF(connp);
1810 1812 goto discard_pkt;
1811 1813 }
1812 1814 CONN_DEC_REF(connp);
1813 1815 break;
1814 1816 }
1815 1817 case IPPROTO_SCTP:
1816 1818 /*
1817 1819 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
1818 1820 * transport header.
1819 1821 */
1820 1822 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
1821 1823 mp->b_wptr)
1822 1824 goto truncated;
1823 1825 break;
1824 1826 case IPPROTO_ESP:
1825 1827 case IPPROTO_AH:
1826 1828 break;
1827 1829 case IPPROTO_ENCAP:
1828 1830 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) >
1829 1831 mp->b_wptr)
1830 1832 goto truncated;
1831 1833 break;
1832 1834 default:
1833 1835 break;
1834 1836 }
1835 1837
1836 1838 return (B_TRUE);
1837 1839
1838 1840 discard_pkt:
1839 1841 /* Bogus ICMP error. */
1840 1842 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1841 1843 return (B_FALSE);
1842 1844
1843 1845 truncated:
1844 1846 /* We pulled up everthing already. Must be truncated */
1845 1847 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
1846 1848 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
1847 1849 return (B_FALSE);
1848 1850 }
1849 1851
1850 1852 /* Table from RFC 1191 */
1851 1853 static int icmp_frag_size_table[] =
1852 1854 { 32000, 17914, 8166, 4352, 2002, 1496, 1006, 508, 296, 68 };
1853 1855
1854 1856 /*
1855 1857 * Process received ICMP Packet too big.
1856 1858 * Just handles the DCE create/update, including using the above table of
1857 1859 * PMTU guesses. The caller is responsible for validating the packet before
1858 1860 * passing it in and also to fanout the ICMP error to any matching transport
1859 1861 * conns. Assumes the message has been fully pulled up and verified.
1860 1862 *
1861 1863 * Before getting here, the caller has called icmp_inbound_verify_v4()
1862 1864 * that should have verified with ULP to prevent undoing the changes we're
1863 1865 * going to make to DCE. For example, TCP might have verified that the packet
1864 1866 * which generated error is in the send window.
1865 1867 *
1866 1868 * In some cases modified this MTU in the ICMP header packet; the caller
1867 1869 * should pass to the matching ULP after this returns.
1868 1870 */
1869 1871 static void
1870 1872 icmp_inbound_too_big_v4(icmph_t *icmph, ip_recv_attr_t *ira)
1871 1873 {
1872 1874 dce_t *dce;
1873 1875 int old_mtu;
1874 1876 int mtu, orig_mtu;
1875 1877 ipaddr_t dst;
1876 1878 boolean_t disable_pmtud;
1877 1879 ill_t *ill = ira->ira_ill;
1878 1880 ip_stack_t *ipst = ill->ill_ipst;
1879 1881 uint_t hdr_length;
1880 1882 ipha_t *ipha;
1881 1883
1882 1884 /* Caller already pulled up everything. */
1883 1885 ipha = (ipha_t *)&icmph[1];
1884 1886 ASSERT(icmph->icmph_type == ICMP_DEST_UNREACHABLE &&
1885 1887 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED);
1886 1888 ASSERT(ill != NULL);
1887 1889
1888 1890 hdr_length = IPH_HDR_LENGTH(ipha);
1889 1891
1890 1892 /*
1891 1893 * We handle path MTU for source routed packets since the DCE
1892 1894 * is looked up using the final destination.
1893 1895 */
1894 1896 dst = ip_get_dst(ipha);
1895 1897
1896 1898 dce = dce_lookup_and_add_v4(dst, ipst);
1897 1899 if (dce == NULL) {
1898 1900 /* Couldn't add a unique one - ENOMEM */
1899 1901 ip1dbg(("icmp_inbound_too_big_v4: no dce for 0x%x\n",
1900 1902 ntohl(dst)));
1901 1903 return;
1902 1904 }
1903 1905
1904 1906 /* Check for MTU discovery advice as described in RFC 1191 */
1905 1907 mtu = ntohs(icmph->icmph_du_mtu);
1906 1908 orig_mtu = mtu;
1907 1909 disable_pmtud = B_FALSE;
1908 1910
1909 1911 mutex_enter(&dce->dce_lock);
1910 1912 if (dce->dce_flags & DCEF_PMTU)
1911 1913 old_mtu = dce->dce_pmtu;
1912 1914 else
1913 1915 old_mtu = ill->ill_mtu;
1914 1916
1915 1917 if (icmph->icmph_du_zero != 0 || mtu < ipst->ips_ip_pmtu_min) {
1916 1918 uint32_t length;
1917 1919 int i;
1918 1920
1919 1921 /*
1920 1922 * Use the table from RFC 1191 to figure out
1921 1923 * the next "plateau" based on the length in
1922 1924 * the original IP packet.
1923 1925 */
1924 1926 length = ntohs(ipha->ipha_length);
1925 1927 DTRACE_PROBE2(ip4__pmtu__guess, dce_t *, dce,
1926 1928 uint32_t, length);
1927 1929 if (old_mtu <= length &&
1928 1930 old_mtu >= length - hdr_length) {
1929 1931 /*
1930 1932 * Handle broken BSD 4.2 systems that
1931 1933 * return the wrong ipha_length in ICMP
1932 1934 * errors.
1933 1935 */
1934 1936 ip1dbg(("Wrong mtu: sent %d, dce %d\n",
1935 1937 length, old_mtu));
1936 1938 length -= hdr_length;
1937 1939 }
1938 1940 for (i = 0; i < A_CNT(icmp_frag_size_table); i++) {
1939 1941 if (length > icmp_frag_size_table[i])
1940 1942 break;
1941 1943 }
1942 1944 if (i == A_CNT(icmp_frag_size_table)) {
1943 1945 /* Smaller than IP_MIN_MTU! */
1944 1946 ip1dbg(("Too big for packet size %d\n",
1945 1947 length));
1946 1948 disable_pmtud = B_TRUE;
1947 1949 mtu = ipst->ips_ip_pmtu_min;
1948 1950 } else {
1949 1951 mtu = icmp_frag_size_table[i];
1950 1952 ip1dbg(("Calculated mtu %d, packet size %d, "
1951 1953 "before %d\n", mtu, length, old_mtu));
1952 1954 if (mtu < ipst->ips_ip_pmtu_min) {
1953 1955 mtu = ipst->ips_ip_pmtu_min;
1954 1956 disable_pmtud = B_TRUE;
1955 1957 }
1956 1958 }
1957 1959 }
1958 1960 if (disable_pmtud)
1959 1961 dce->dce_flags |= DCEF_TOO_SMALL_PMTU;
1960 1962 else
1961 1963 dce->dce_flags &= ~DCEF_TOO_SMALL_PMTU;
1962 1964
1963 1965 dce->dce_pmtu = MIN(old_mtu, mtu);
1964 1966 /* Prepare to send the new max frag size for the ULP. */
1965 1967 icmph->icmph_du_zero = 0;
1966 1968 icmph->icmph_du_mtu = htons((uint16_t)dce->dce_pmtu);
1967 1969 DTRACE_PROBE4(ip4__pmtu__change, icmph_t *, icmph, dce_t *,
1968 1970 dce, int, orig_mtu, int, mtu);
1969 1971
1970 1972 /* We now have a PMTU for sure */
1971 1973 dce->dce_flags |= DCEF_PMTU;
1972 1974 dce->dce_last_change_time = TICK_TO_SEC(ddi_get_lbolt64());
1973 1975 mutex_exit(&dce->dce_lock);
1974 1976 /*
1975 1977 * After dropping the lock the new value is visible to everyone.
1976 1978 * Then we bump the generation number so any cached values reinspect
1977 1979 * the dce_t.
1978 1980 */
1979 1981 dce_increment_generation(dce);
1980 1982 dce_refrele(dce);
1981 1983 }
1982 1984
1983 1985 /*
1984 1986 * If the packet in error is Self-Encapsulated, icmp_inbound_error_fanout_v4
1985 1987 * calls this function.
1986 1988 */
1987 1989 static mblk_t *
1988 1990 icmp_inbound_self_encap_error_v4(mblk_t *mp, ipha_t *ipha, ipha_t *in_ipha)
1989 1991 {
1990 1992 int length;
1991 1993
1992 1994 ASSERT(mp->b_datap->db_type == M_DATA);
1993 1995
1994 1996 /* icmp_inbound_v4 has already pulled up the whole error packet */
1995 1997 ASSERT(mp->b_cont == NULL);
1996 1998
1997 1999 /*
1998 2000 * The length that we want to overlay is the inner header
1999 2001 * and what follows it.
2000 2002 */
2001 2003 length = msgdsize(mp) - ((uchar_t *)in_ipha - mp->b_rptr);
2002 2004
2003 2005 /*
2004 2006 * Overlay the inner header and whatever follows it over the
2005 2007 * outer header.
2006 2008 */
2007 2009 bcopy((uchar_t *)in_ipha, (uchar_t *)ipha, length);
2008 2010
2009 2011 /* Adjust for what we removed */
2010 2012 mp->b_wptr -= (uchar_t *)in_ipha - (uchar_t *)ipha;
2011 2013 return (mp);
2012 2014 }
2013 2015
2014 2016 /*
2015 2017 * Try to pass the ICMP message upstream in case the ULP cares.
2016 2018 *
2017 2019 * If the packet that caused the ICMP error is secure, we send
2018 2020 * it to AH/ESP to make sure that the attached packet has a
2019 2021 * valid association. ipha in the code below points to the
2020 2022 * IP header of the packet that caused the error.
2021 2023 *
2022 2024 * For IPsec cases, we let the next-layer-up (which has access to
2023 2025 * cached policy on the conn_t, or can query the SPD directly)
2024 2026 * subtract out any IPsec overhead if they must. We therefore make no
2025 2027 * adjustments here for IPsec overhead.
2026 2028 *
2027 2029 * IFN could have been generated locally or by some router.
2028 2030 *
2029 2031 * LOCAL : ire_send_wire (before calling ipsec_out_process) can call
2030 2032 * icmp_frag_needed/icmp_pkt2big_v6 to generated a local IFN.
2031 2033 * This happens because IP adjusted its value of MTU on an
2032 2034 * earlier IFN message and could not tell the upper layer,
2033 2035 * the new adjusted value of MTU e.g. Packet was encrypted
2034 2036 * or there was not enough information to fanout to upper
2035 2037 * layers. Thus on the next outbound datagram, ire_send_wire
2036 2038 * generates the IFN, where IPsec processing has *not* been
2037 2039 * done.
2038 2040 *
2039 2041 * Note that we retain ixa_fragsize across IPsec thus once
2040 2042 * we have picking ixa_fragsize and entered ipsec_out_process we do
2041 2043 * no change the fragsize even if the path MTU changes before
2042 2044 * we reach ip_output_post_ipsec.
2043 2045 *
2044 2046 * In the local case, IRAF_LOOPBACK will be set indicating
2045 2047 * that IFN was generated locally.
2046 2048 *
2047 2049 * ROUTER : IFN could be secure or non-secure.
2048 2050 *
2049 2051 * * SECURE : We use the IPSEC_IN to fanout to AH/ESP if the
2050 2052 * packet in error has AH/ESP headers to validate the AH/ESP
2051 2053 * headers. AH/ESP will verify whether there is a valid SA or
2052 2054 * not and send it back. We will fanout again if we have more
2053 2055 * data in the packet.
2054 2056 *
2055 2057 * If the packet in error does not have AH/ESP, we handle it
2056 2058 * like any other case.
2057 2059 *
2058 2060 * * NON_SECURE : If the packet in error has AH/ESP headers, we send it
2059 2061 * up to AH/ESP for validation. AH/ESP will verify whether there is a
2060 2062 * valid SA or not and send it back. We will fanout again if
2061 2063 * we have more data in the packet.
2062 2064 *
2063 2065 * If the packet in error does not have AH/ESP, we handle it
2064 2066 * like any other case.
2065 2067 *
2066 2068 * The caller must have called icmp_inbound_verify_v4.
2067 2069 */
2068 2070 static void
2069 2071 icmp_inbound_error_fanout_v4(mblk_t *mp, icmph_t *icmph, ip_recv_attr_t *ira)
2070 2072 {
2071 2073 uint16_t *up; /* Pointer to ports in ULP header */
2072 2074 uint32_t ports; /* reversed ports for fanout */
2073 2075 ipha_t ripha; /* With reversed addresses */
2074 2076 ipha_t *ipha; /* Inner IP header */
2075 2077 uint_t hdr_length; /* Inner IP header length */
2076 2078 tcpha_t *tcpha;
2077 2079 conn_t *connp;
2078 2080 ill_t *ill = ira->ira_ill;
2079 2081 ip_stack_t *ipst = ill->ill_ipst;
2080 2082 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
2081 2083 ill_t *rill = ira->ira_rill;
2082 2084
2083 2085 /* Caller already pulled up everything. */
2084 2086 ipha = (ipha_t *)&icmph[1];
2085 2087 ASSERT((uchar_t *)&ipha[1] <= mp->b_wptr);
2086 2088 ASSERT(mp->b_cont == NULL);
2087 2089
2088 2090 hdr_length = IPH_HDR_LENGTH(ipha);
2089 2091 ira->ira_protocol = ipha->ipha_protocol;
2090 2092
2091 2093 /*
2092 2094 * We need a separate IP header with the source and destination
2093 2095 * addresses reversed to do fanout/classification because the ipha in
2094 2096 * the ICMP error is in the form we sent it out.
2095 2097 */
2096 2098 ripha.ipha_src = ipha->ipha_dst;
2097 2099 ripha.ipha_dst = ipha->ipha_src;
2098 2100 ripha.ipha_protocol = ipha->ipha_protocol;
2099 2101 ripha.ipha_version_and_hdr_length = ipha->ipha_version_and_hdr_length;
2100 2102
2101 2103 ip2dbg(("icmp_inbound_error_v4: proto %d %x to %x: %d/%d\n",
2102 2104 ripha.ipha_protocol, ntohl(ipha->ipha_src),
2103 2105 ntohl(ipha->ipha_dst),
2104 2106 icmph->icmph_type, icmph->icmph_code));
2105 2107
2106 2108 switch (ipha->ipha_protocol) {
2107 2109 case IPPROTO_UDP:
2108 2110 up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2109 2111
2110 2112 /* Attempt to find a client stream based on port. */
2111 2113 ip2dbg(("icmp_inbound_error_v4: UDP ports %d to %d\n",
2112 2114 ntohs(up[0]), ntohs(up[1])));
2113 2115
2114 2116 /* Note that we send error to all matches. */
2115 2117 ira->ira_flags |= IRAF_ICMP_ERROR;
2116 2118 ip_fanout_udp_multi_v4(mp, &ripha, up[0], up[1], ira);
2117 2119 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2118 2120 return;
2119 2121
2120 2122 case IPPROTO_TCP:
2121 2123 /*
2122 2124 * Find a TCP client stream for this packet.
2123 2125 * Note that we do a reverse lookup since the header is
2124 2126 * in the form we sent it out.
2125 2127 */
2126 2128 tcpha = (tcpha_t *)((uchar_t *)ipha + hdr_length);
2127 2129 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcpha, TCPS_LISTEN,
2128 2130 ipst);
2129 2131 if (connp == NULL)
2130 2132 goto discard_pkt;
2131 2133
2132 2134 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) ||
2133 2135 (ira->ira_flags & IRAF_IPSEC_SECURE)) {
2134 2136 mp = ipsec_check_inbound_policy(mp, connp,
2135 2137 ipha, NULL, ira);
2136 2138 if (mp == NULL) {
2137 2139 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2138 2140 /* Note that mp is NULL */
2139 2141 ip_drop_input("ipIfStatsInDiscards", mp, ill);
2140 2142 CONN_DEC_REF(connp);
2141 2143 return;
2142 2144 }
2143 2145 }
2144 2146
2145 2147 ira->ira_flags |= IRAF_ICMP_ERROR;
2146 2148 ira->ira_ill = ira->ira_rill = NULL;
2147 2149 if (IPCL_IS_TCP(connp)) {
2148 2150 SQUEUE_ENTER_ONE(connp->conn_sqp, mp,
2149 2151 connp->conn_recvicmp, connp, ira, SQ_FILL,
2150 2152 SQTAG_TCP_INPUT_ICMP_ERR);
2151 2153 } else {
2152 2154 /* Not TCP; must be SOCK_RAW, IPPROTO_TCP */
2153 2155 (connp->conn_recv)(connp, mp, NULL, ira);
2154 2156 CONN_DEC_REF(connp);
2155 2157 }
2156 2158 ira->ira_ill = ill;
2157 2159 ira->ira_rill = rill;
2158 2160 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2159 2161 return;
2160 2162
2161 2163 case IPPROTO_SCTP:
2162 2164 up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2163 2165 /* Find a SCTP client stream for this packet. */
2164 2166 ((uint16_t *)&ports)[0] = up[1];
2165 2167 ((uint16_t *)&ports)[1] = up[0];
2166 2168
2167 2169 ira->ira_flags |= IRAF_ICMP_ERROR;
2168 2170 ip_fanout_sctp(mp, &ripha, NULL, ports, ira);
2169 2171 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2170 2172 return;
2171 2173
2172 2174 case IPPROTO_ESP:
2173 2175 case IPPROTO_AH:
2174 2176 if (!ipsec_loaded(ipss)) {
2175 2177 ip_proto_not_sup(mp, ira);
2176 2178 return;
2177 2179 }
2178 2180
2179 2181 if (ipha->ipha_protocol == IPPROTO_ESP)
2180 2182 mp = ipsecesp_icmp_error(mp, ira);
2181 2183 else
2182 2184 mp = ipsecah_icmp_error(mp, ira);
2183 2185 if (mp == NULL)
2184 2186 return;
2185 2187
2186 2188 /* Just in case ipsec didn't preserve the NULL b_cont */
2187 2189 if (mp->b_cont != NULL) {
2188 2190 if (!pullupmsg(mp, -1))
2189 2191 goto discard_pkt;
2190 2192 }
2191 2193
2192 2194 /*
2193 2195 * Note that ira_pktlen and ira_ip_hdr_length are no longer
2194 2196 * correct, but we don't use them any more here.
2195 2197 *
2196 2198 * If succesful, the mp has been modified to not include
2197 2199 * the ESP/AH header so we can fanout to the ULP's icmp
2198 2200 * error handler.
2199 2201 */
2200 2202 if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH)
2201 2203 goto truncated;
2202 2204
2203 2205 /* Verify the modified message before any further processes. */
2204 2206 ipha = (ipha_t *)mp->b_rptr;
2205 2207 hdr_length = IPH_HDR_LENGTH(ipha);
2206 2208 icmph = (icmph_t *)&mp->b_rptr[hdr_length];
2207 2209 if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
2208 2210 freemsg(mp);
2209 2211 return;
2210 2212 }
2211 2213
2212 2214 icmp_inbound_error_fanout_v4(mp, icmph, ira);
2213 2215 return;
2214 2216
2215 2217 case IPPROTO_ENCAP: {
2216 2218 /* Look for self-encapsulated packets that caused an error */
2217 2219 ipha_t *in_ipha;
2218 2220
2219 2221 /*
2220 2222 * Caller has verified that length has to be
2221 2223 * at least the size of IP header.
2222 2224 */
2223 2225 ASSERT(hdr_length >= sizeof (ipha_t));
2224 2226 /*
2225 2227 * Check the sanity of the inner IP header like
2226 2228 * we did for the outer header.
2227 2229 */
2228 2230 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length);
2229 2231 if ((IPH_HDR_VERSION(in_ipha) != IPV4_VERSION)) {
2230 2232 goto discard_pkt;
2231 2233 }
2232 2234 if (IPH_HDR_LENGTH(in_ipha) < sizeof (ipha_t)) {
2233 2235 goto discard_pkt;
2234 2236 }
2235 2237 /* Check for Self-encapsulated tunnels */
2236 2238 if (in_ipha->ipha_src == ipha->ipha_src &&
2237 2239 in_ipha->ipha_dst == ipha->ipha_dst) {
2238 2240
2239 2241 mp = icmp_inbound_self_encap_error_v4(mp, ipha,
2240 2242 in_ipha);
2241 2243 if (mp == NULL)
2242 2244 goto discard_pkt;
2243 2245
2244 2246 /*
2245 2247 * Just in case self_encap didn't preserve the NULL
2246 2248 * b_cont
2247 2249 */
2248 2250 if (mp->b_cont != NULL) {
2249 2251 if (!pullupmsg(mp, -1))
2250 2252 goto discard_pkt;
2251 2253 }
2252 2254 /*
2253 2255 * Note that ira_pktlen and ira_ip_hdr_length are no
2254 2256 * longer correct, but we don't use them any more here.
2255 2257 */
2256 2258 if (mp->b_wptr - mp->b_rptr < IP_SIMPLE_HDR_LENGTH)
2257 2259 goto truncated;
2258 2260
2259 2261 /*
2260 2262 * Verify the modified message before any further
2261 2263 * processes.
2262 2264 */
2263 2265 ipha = (ipha_t *)mp->b_rptr;
2264 2266 hdr_length = IPH_HDR_LENGTH(ipha);
2265 2267 icmph = (icmph_t *)&mp->b_rptr[hdr_length];
2266 2268 if (!icmp_inbound_verify_v4(mp, icmph, ira)) {
2267 2269 freemsg(mp);
2268 2270 return;
2269 2271 }
2270 2272
2271 2273 /*
2272 2274 * The packet in error is self-encapsualted.
2273 2275 * And we are finding it further encapsulated
2274 2276 * which we could not have possibly generated.
2275 2277 */
2276 2278 if (ipha->ipha_protocol == IPPROTO_ENCAP) {
2277 2279 goto discard_pkt;
2278 2280 }
2279 2281 icmp_inbound_error_fanout_v4(mp, icmph, ira);
2280 2282 return;
2281 2283 }
2282 2284 /* No self-encapsulated */
2283 2285 /* FALLTHRU */
2284 2286 }
2285 2287 case IPPROTO_IPV6:
2286 2288 if ((connp = ipcl_iptun_classify_v4(&ripha.ipha_src,
2287 2289 &ripha.ipha_dst, ipst)) != NULL) {
2288 2290 ira->ira_flags |= IRAF_ICMP_ERROR;
2289 2291 connp->conn_recvicmp(connp, mp, NULL, ira);
2290 2292 CONN_DEC_REF(connp);
2291 2293 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2292 2294 return;
2293 2295 }
2294 2296 /*
2295 2297 * No IP tunnel is interested, fallthrough and see
2296 2298 * if a raw socket will want it.
2297 2299 */
2298 2300 /* FALLTHRU */
2299 2301 default:
2300 2302 ira->ira_flags |= IRAF_ICMP_ERROR;
2301 2303 ip_fanout_proto_v4(mp, &ripha, ira);
2302 2304 ira->ira_flags &= ~IRAF_ICMP_ERROR;
2303 2305 return;
2304 2306 }
2305 2307 /* NOTREACHED */
2306 2308 discard_pkt:
2307 2309 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2308 2310 ip1dbg(("icmp_inbound_error_fanout_v4: drop pkt\n"));
2309 2311 ip_drop_input("ipIfStatsInDiscards", mp, ill);
2310 2312 freemsg(mp);
2311 2313 return;
2312 2314
2313 2315 truncated:
2314 2316 /* We pulled up everthing already. Must be truncated */
2315 2317 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
2316 2318 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
2317 2319 freemsg(mp);
2318 2320 }
2319 2321
2320 2322 /*
2321 2323 * Common IP options parser.
2322 2324 *
2323 2325 * Setup routine: fill in *optp with options-parsing state, then
2324 2326 * tail-call ipoptp_next to return the first option.
2325 2327 */
2326 2328 uint8_t
2327 2329 ipoptp_first(ipoptp_t *optp, ipha_t *ipha)
2328 2330 {
2329 2331 uint32_t totallen; /* total length of all options */
2330 2332
2331 2333 totallen = ipha->ipha_version_and_hdr_length -
2332 2334 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
2333 2335 totallen <<= 2;
2334 2336 optp->ipoptp_next = (uint8_t *)(&ipha[1]);
2335 2337 optp->ipoptp_end = optp->ipoptp_next + totallen;
2336 2338 optp->ipoptp_flags = 0;
2337 2339 return (ipoptp_next(optp));
2338 2340 }
2339 2341
2340 2342 /* Like above but without an ipha_t */
2341 2343 uint8_t
2342 2344 ipoptp_first2(ipoptp_t *optp, uint32_t totallen, uint8_t *opt)
2343 2345 {
2344 2346 optp->ipoptp_next = opt;
2345 2347 optp->ipoptp_end = optp->ipoptp_next + totallen;
2346 2348 optp->ipoptp_flags = 0;
2347 2349 return (ipoptp_next(optp));
2348 2350 }
2349 2351
2350 2352 /*
2351 2353 * Common IP options parser: extract next option.
2352 2354 */
2353 2355 uint8_t
2354 2356 ipoptp_next(ipoptp_t *optp)
2355 2357 {
2356 2358 uint8_t *end = optp->ipoptp_end;
2357 2359 uint8_t *cur = optp->ipoptp_next;
2358 2360 uint8_t opt, len, pointer;
2359 2361
2360 2362 /*
2361 2363 * If cur > end already, then the ipoptp_end or ipoptp_next pointer
2362 2364 * has been corrupted.
2363 2365 */
2364 2366 ASSERT(cur <= end);
2365 2367
2366 2368 if (cur == end)
2367 2369 return (IPOPT_EOL);
2368 2370
2369 2371 opt = cur[IPOPT_OPTVAL];
2370 2372
2371 2373 /*
2372 2374 * Skip any NOP options.
2373 2375 */
2374 2376 while (opt == IPOPT_NOP) {
2375 2377 cur++;
2376 2378 if (cur == end)
2377 2379 return (IPOPT_EOL);
2378 2380 opt = cur[IPOPT_OPTVAL];
2379 2381 }
2380 2382
2381 2383 if (opt == IPOPT_EOL)
2382 2384 return (IPOPT_EOL);
2383 2385
2384 2386 /*
2385 2387 * Option requiring a length.
2386 2388 */
2387 2389 if ((cur + 1) >= end) {
2388 2390 optp->ipoptp_flags |= IPOPTP_ERROR;
2389 2391 return (IPOPT_EOL);
2390 2392 }
2391 2393 len = cur[IPOPT_OLEN];
2392 2394 if (len < 2) {
2393 2395 optp->ipoptp_flags |= IPOPTP_ERROR;
2394 2396 return (IPOPT_EOL);
2395 2397 }
2396 2398 optp->ipoptp_cur = cur;
2397 2399 optp->ipoptp_len = len;
2398 2400 optp->ipoptp_next = cur + len;
2399 2401 if (cur + len > end) {
2400 2402 optp->ipoptp_flags |= IPOPTP_ERROR;
2401 2403 return (IPOPT_EOL);
2402 2404 }
2403 2405
2404 2406 /*
2405 2407 * For the options which require a pointer field, make sure
2406 2408 * its there, and make sure it points to either something
2407 2409 * inside this option, or the end of the option.
2408 2410 */
2409 2411 switch (opt) {
2410 2412 case IPOPT_RR:
2411 2413 case IPOPT_TS:
2412 2414 case IPOPT_LSRR:
2413 2415 case IPOPT_SSRR:
2414 2416 if (len <= IPOPT_OFFSET) {
2415 2417 optp->ipoptp_flags |= IPOPTP_ERROR;
2416 2418 return (opt);
2417 2419 }
2418 2420 pointer = cur[IPOPT_OFFSET];
2419 2421 if (pointer - 1 > len) {
2420 2422 optp->ipoptp_flags |= IPOPTP_ERROR;
2421 2423 return (opt);
2422 2424 }
2423 2425 break;
2424 2426 }
2425 2427
2426 2428 /*
2427 2429 * Sanity check the pointer field based on the type of the
2428 2430 * option.
2429 2431 */
2430 2432 switch (opt) {
2431 2433 case IPOPT_RR:
2432 2434 case IPOPT_SSRR:
2433 2435 case IPOPT_LSRR:
2434 2436 if (pointer < IPOPT_MINOFF_SR)
2435 2437 optp->ipoptp_flags |= IPOPTP_ERROR;
2436 2438 break;
2437 2439 case IPOPT_TS:
2438 2440 if (pointer < IPOPT_MINOFF_IT)
2439 2441 optp->ipoptp_flags |= IPOPTP_ERROR;
2440 2442 /*
2441 2443 * Note that the Internet Timestamp option also
2442 2444 * contains two four bit fields (the Overflow field,
2443 2445 * and the Flag field), which follow the pointer
2444 2446 * field. We don't need to check that these fields
2445 2447 * fall within the length of the option because this
2446 2448 * was implicitely done above. We've checked that the
2447 2449 * pointer value is at least IPOPT_MINOFF_IT, and that
2448 2450 * it falls within the option. Since IPOPT_MINOFF_IT >
2449 2451 * IPOPT_POS_OV_FLG, we don't need the explicit check.
2450 2452 */
2451 2453 ASSERT(len > IPOPT_POS_OV_FLG);
2452 2454 break;
2453 2455 }
2454 2456
2455 2457 return (opt);
2456 2458 }
2457 2459
2458 2460 /*
2459 2461 * Use the outgoing IP header to create an IP_OPTIONS option the way
2460 2462 * it was passed down from the application.
2461 2463 *
2462 2464 * This is compatible with BSD in that it returns
2463 2465 * the reverse source route with the final destination
2464 2466 * as the last entry. The first 4 bytes of the option
2465 2467 * will contain the final destination.
2466 2468 */
2467 2469 int
2468 2470 ip_opt_get_user(conn_t *connp, uchar_t *buf)
2469 2471 {
2470 2472 ipoptp_t opts;
2471 2473 uchar_t *opt;
2472 2474 uint8_t optval;
2473 2475 uint8_t optlen;
2474 2476 uint32_t len = 0;
2475 2477 uchar_t *buf1 = buf;
2476 2478 uint32_t totallen;
2477 2479 ipaddr_t dst;
2478 2480 ip_pkt_t *ipp = &connp->conn_xmit_ipp;
2479 2481
2480 2482 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
2481 2483 return (0);
2482 2484
2483 2485 totallen = ipp->ipp_ipv4_options_len;
2484 2486 if (totallen & 0x3)
2485 2487 return (0);
2486 2488
2487 2489 buf += IP_ADDR_LEN; /* Leave room for final destination */
2488 2490 len += IP_ADDR_LEN;
2489 2491 bzero(buf1, IP_ADDR_LEN);
2490 2492
2491 2493 dst = connp->conn_faddr_v4;
2492 2494
2493 2495 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
2494 2496 optval != IPOPT_EOL;
2495 2497 optval = ipoptp_next(&opts)) {
2496 2498 int off;
2497 2499
2498 2500 opt = opts.ipoptp_cur;
2499 2501 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
2500 2502 break;
2501 2503 }
2502 2504 optlen = opts.ipoptp_len;
2503 2505
2504 2506 switch (optval) {
2505 2507 case IPOPT_SSRR:
2506 2508 case IPOPT_LSRR:
2507 2509
2508 2510 /*
2509 2511 * Insert destination as the first entry in the source
2510 2512 * route and move down the entries on step.
2511 2513 * The last entry gets placed at buf1.
2512 2514 */
2513 2515 buf[IPOPT_OPTVAL] = optval;
2514 2516 buf[IPOPT_OLEN] = optlen;
2515 2517 buf[IPOPT_OFFSET] = optlen;
2516 2518
2517 2519 off = optlen - IP_ADDR_LEN;
2518 2520 if (off < 0) {
2519 2521 /* No entries in source route */
2520 2522 break;
2521 2523 }
2522 2524 /* Last entry in source route if not already set */
2523 2525 if (dst == INADDR_ANY)
2524 2526 bcopy(opt + off, buf1, IP_ADDR_LEN);
2525 2527 off -= IP_ADDR_LEN;
2526 2528
2527 2529 while (off > 0) {
2528 2530 bcopy(opt + off,
2529 2531 buf + off + IP_ADDR_LEN,
2530 2532 IP_ADDR_LEN);
2531 2533 off -= IP_ADDR_LEN;
2532 2534 }
2533 2535 /* ipha_dst into first slot */
2534 2536 bcopy(&dst, buf + off + IP_ADDR_LEN,
2535 2537 IP_ADDR_LEN);
2536 2538 buf += optlen;
2537 2539 len += optlen;
2538 2540 break;
2539 2541
2540 2542 default:
2541 2543 bcopy(opt, buf, optlen);
2542 2544 buf += optlen;
2543 2545 len += optlen;
2544 2546 break;
2545 2547 }
2546 2548 }
2547 2549 done:
2548 2550 /* Pad the resulting options */
2549 2551 while (len & 0x3) {
2550 2552 *buf++ = IPOPT_EOL;
2551 2553 len++;
2552 2554 }
2553 2555 return (len);
2554 2556 }
2555 2557
2556 2558 /*
2557 2559 * Update any record route or timestamp options to include this host.
2558 2560 * Reverse any source route option.
2559 2561 * This routine assumes that the options are well formed i.e. that they
2560 2562 * have already been checked.
2561 2563 */
2562 2564 static void
2563 2565 icmp_options_update(ipha_t *ipha)
2564 2566 {
2565 2567 ipoptp_t opts;
2566 2568 uchar_t *opt;
2567 2569 uint8_t optval;
2568 2570 ipaddr_t src; /* Our local address */
2569 2571 ipaddr_t dst;
2570 2572
2571 2573 ip2dbg(("icmp_options_update\n"));
2572 2574 src = ipha->ipha_src;
2573 2575 dst = ipha->ipha_dst;
2574 2576
2575 2577 for (optval = ipoptp_first(&opts, ipha);
2576 2578 optval != IPOPT_EOL;
2577 2579 optval = ipoptp_next(&opts)) {
2578 2580 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
2579 2581 opt = opts.ipoptp_cur;
2580 2582 ip2dbg(("icmp_options_update: opt %d, len %d\n",
2581 2583 optval, opts.ipoptp_len));
2582 2584 switch (optval) {
2583 2585 int off1, off2;
2584 2586 case IPOPT_SSRR:
2585 2587 case IPOPT_LSRR:
2586 2588 /*
2587 2589 * Reverse the source route. The first entry
2588 2590 * should be the next to last one in the current
2589 2591 * source route (the last entry is our address).
2590 2592 * The last entry should be the final destination.
2591 2593 */
2592 2594 off1 = IPOPT_MINOFF_SR - 1;
2593 2595 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1;
2594 2596 if (off2 < 0) {
2595 2597 /* No entries in source route */
2596 2598 ip1dbg((
2597 2599 "icmp_options_update: bad src route\n"));
2598 2600 break;
2599 2601 }
2600 2602 bcopy((char *)opt + off2, &dst, IP_ADDR_LEN);
2601 2603 bcopy(&ipha->ipha_dst, (char *)opt + off2, IP_ADDR_LEN);
2602 2604 bcopy(&dst, &ipha->ipha_dst, IP_ADDR_LEN);
2603 2605 off2 -= IP_ADDR_LEN;
2604 2606
2605 2607 while (off1 < off2) {
2606 2608 bcopy((char *)opt + off1, &src, IP_ADDR_LEN);
2607 2609 bcopy((char *)opt + off2, (char *)opt + off1,
2608 2610 IP_ADDR_LEN);
2609 2611 bcopy(&src, (char *)opt + off2, IP_ADDR_LEN);
2610 2612 off1 += IP_ADDR_LEN;
2611 2613 off2 -= IP_ADDR_LEN;
2612 2614 }
2613 2615 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR;
2614 2616 break;
2615 2617 }
2616 2618 }
2617 2619 }
2618 2620
2619 2621 /*
2620 2622 * Process received ICMP Redirect messages.
2621 2623 * Assumes the caller has verified that the headers are in the pulled up mblk.
2622 2624 * Consumes mp.
2623 2625 */
2624 2626 static void
2625 2627 icmp_redirect_v4(mblk_t *mp, ipha_t *ipha, icmph_t *icmph, ip_recv_attr_t *ira)
2626 2628 {
2627 2629 ire_t *ire, *nire;
2628 2630 ire_t *prev_ire;
2629 2631 ipaddr_t src, dst, gateway;
2630 2632 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2631 2633 ipha_t *inner_ipha; /* Inner IP header */
2632 2634
2633 2635 /* Caller already pulled up everything. */
2634 2636 inner_ipha = (ipha_t *)&icmph[1];
2635 2637 src = ipha->ipha_src;
2636 2638 dst = inner_ipha->ipha_dst;
2637 2639 gateway = icmph->icmph_rd_gateway;
2638 2640 /* Make sure the new gateway is reachable somehow. */
2639 2641 ire = ire_ftable_lookup_v4(gateway, 0, 0, IRE_ONLINK, NULL,
2640 2642 ALL_ZONES, NULL, MATCH_IRE_TYPE, 0, ipst, NULL);
2641 2643 /*
2642 2644 * Make sure we had a route for the dest in question and that
2643 2645 * that route was pointing to the old gateway (the source of the
2644 2646 * redirect packet.)
2645 2647 * We do longest match and then compare ire_gateway_addr below.
2646 2648 */
2647 2649 prev_ire = ire_ftable_lookup_v4(dst, 0, 0, 0, NULL, ALL_ZONES,
2648 2650 NULL, MATCH_IRE_DSTONLY, 0, ipst, NULL);
2649 2651 /*
2650 2652 * Check that
2651 2653 * the redirect was not from ourselves
2652 2654 * the new gateway and the old gateway are directly reachable
2653 2655 */
2654 2656 if (prev_ire == NULL || ire == NULL ||
2655 2657 (prev_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) ||
2656 2658 (prev_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) ||
2657 2659 !(ire->ire_type & IRE_IF_ALL) ||
2658 2660 prev_ire->ire_gateway_addr != src) {
2659 2661 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects);
2660 2662 ip_drop_input("icmpInBadRedirects - ire", mp, ira->ira_ill);
2661 2663 freemsg(mp);
2662 2664 if (ire != NULL)
2663 2665 ire_refrele(ire);
2664 2666 if (prev_ire != NULL)
2665 2667 ire_refrele(prev_ire);
2666 2668 return;
2667 2669 }
2668 2670
2669 2671 ire_refrele(prev_ire);
2670 2672 ire_refrele(ire);
2671 2673
2672 2674 /*
2673 2675 * TODO: more precise handling for cases 0, 2, 3, the latter two
2674 2676 * require TOS routing
2675 2677 */
2676 2678 switch (icmph->icmph_code) {
2677 2679 case 0:
2678 2680 case 1:
2679 2681 /* TODO: TOS specificity for cases 2 and 3 */
2680 2682 case 2:
2681 2683 case 3:
2682 2684 break;
2683 2685 default:
2684 2686 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects);
2685 2687 ip_drop_input("icmpInBadRedirects - code", mp, ira->ira_ill);
2686 2688 freemsg(mp);
2687 2689 return;
2688 2690 }
2689 2691 /*
2690 2692 * Create a Route Association. This will allow us to remember that
2691 2693 * someone we believe told us to use the particular gateway.
2692 2694 */
2693 2695 ire = ire_create(
2694 2696 (uchar_t *)&dst, /* dest addr */
2695 2697 (uchar_t *)&ip_g_all_ones, /* mask */
2696 2698 (uchar_t *)&gateway, /* gateway addr */
2697 2699 IRE_HOST,
2698 2700 NULL, /* ill */
2699 2701 ALL_ZONES,
2700 2702 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST),
2701 2703 NULL, /* tsol_gc_t */
2702 2704 ipst);
2703 2705
2704 2706 if (ire == NULL) {
2705 2707 freemsg(mp);
2706 2708 return;
2707 2709 }
2708 2710 nire = ire_add(ire);
2709 2711 /* Check if it was a duplicate entry */
2710 2712 if (nire != NULL && nire != ire) {
2711 2713 ASSERT(nire->ire_identical_ref > 1);
2712 2714 ire_delete(nire);
2713 2715 ire_refrele(nire);
2714 2716 nire = NULL;
2715 2717 }
2716 2718 ire = nire;
2717 2719 if (ire != NULL) {
2718 2720 ire_refrele(ire); /* Held in ire_add */
2719 2721
2720 2722 /* tell routing sockets that we received a redirect */
2721 2723 ip_rts_change(RTM_REDIRECT, dst, gateway, IP_HOST_MASK, 0, src,
2722 2724 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0,
2723 2725 (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR), ipst);
2724 2726 }
2725 2727
2726 2728 /*
2727 2729 * Delete any existing IRE_HOST type redirect ires for this destination.
2728 2730 * This together with the added IRE has the effect of
2729 2731 * modifying an existing redirect.
2730 2732 */
2731 2733 prev_ire = ire_ftable_lookup_v4(dst, 0, src, IRE_HOST, NULL,
2732 2734 ALL_ZONES, NULL, (MATCH_IRE_GW | MATCH_IRE_TYPE), 0, ipst, NULL);
2733 2735 if (prev_ire != NULL) {
2734 2736 if (prev_ire ->ire_flags & RTF_DYNAMIC)
2735 2737 ire_delete(prev_ire);
2736 2738 ire_refrele(prev_ire);
2737 2739 }
2738 2740
2739 2741 freemsg(mp);
2740 2742 }
2741 2743
2742 2744 /*
2743 2745 * Generate an ICMP parameter problem message.
2744 2746 * When called from ip_output side a minimal ip_recv_attr_t needs to be
2745 2747 * constructed by the caller.
2746 2748 */
2747 2749 static void
2748 2750 icmp_param_problem(mblk_t *mp, uint8_t ptr, ip_recv_attr_t *ira)
2749 2751 {
2750 2752 icmph_t icmph;
2751 2753 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2752 2754
2753 2755 mp = icmp_pkt_err_ok(mp, ira);
2754 2756 if (mp == NULL)
2755 2757 return;
2756 2758
2757 2759 bzero(&icmph, sizeof (icmph_t));
2758 2760 icmph.icmph_type = ICMP_PARAM_PROBLEM;
2759 2761 icmph.icmph_pp_ptr = ptr;
2760 2762 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutParmProbs);
2761 2763 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
2762 2764 }
2763 2765
2764 2766 /*
2765 2767 * Build and ship an IPv4 ICMP message using the packet data in mp, and
2766 2768 * the ICMP header pointed to by "stuff". (May be called as writer.)
2767 2769 * Note: assumes that icmp_pkt_err_ok has been called to verify that
2768 2770 * an icmp error packet can be sent.
2769 2771 * Assigns an appropriate source address to the packet. If ipha_dst is
2770 2772 * one of our addresses use it for source. Otherwise let ip_output_simple
2771 2773 * pick the source address.
2772 2774 */
2773 2775 static void
2774 2776 icmp_pkt(mblk_t *mp, void *stuff, size_t len, ip_recv_attr_t *ira)
2775 2777 {
2776 2778 ipaddr_t dst;
2777 2779 icmph_t *icmph;
2778 2780 ipha_t *ipha;
2779 2781 uint_t len_needed;
2780 2782 size_t msg_len;
2781 2783 mblk_t *mp1;
2782 2784 ipaddr_t src;
2783 2785 ire_t *ire;
2784 2786 ip_xmit_attr_t ixas;
2785 2787 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2786 2788
2787 2789 ipha = (ipha_t *)mp->b_rptr;
2788 2790
2789 2791 bzero(&ixas, sizeof (ixas));
2790 2792 ixas.ixa_flags = IXAF_BASIC_SIMPLE_V4;
2791 2793 ixas.ixa_zoneid = ira->ira_zoneid;
2792 2794 ixas.ixa_ifindex = 0;
2793 2795 ixas.ixa_ipst = ipst;
2794 2796 ixas.ixa_cred = kcred;
2795 2797 ixas.ixa_cpid = NOPID;
2796 2798 ixas.ixa_tsl = ira->ira_tsl; /* Behave as a multi-level responder */
2797 2799 ixas.ixa_multicast_ttl = IP_DEFAULT_MULTICAST_TTL;
2798 2800
2799 2801 if (ira->ira_flags & IRAF_IPSEC_SECURE) {
2800 2802 /*
2801 2803 * Apply IPsec based on how IPsec was applied to
2802 2804 * the packet that had the error.
2803 2805 *
2804 2806 * If it was an outbound packet that caused the ICMP
2805 2807 * error, then the caller will have setup the IRA
2806 2808 * appropriately.
2807 2809 */
2808 2810 if (!ipsec_in_to_out(ira, &ixas, mp, ipha, NULL)) {
2809 2811 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
2810 2812 /* Note: mp already consumed and ip_drop_packet done */
2811 2813 return;
2812 2814 }
2813 2815 } else {
2814 2816 /*
2815 2817 * This is in clear. The icmp message we are building
2816 2818 * here should go out in clear, independent of our policy.
2817 2819 */
2818 2820 ixas.ixa_flags |= IXAF_NO_IPSEC;
2819 2821 }
2820 2822
2821 2823 /* Remember our eventual destination */
2822 2824 dst = ipha->ipha_src;
2823 2825
2824 2826 /*
2825 2827 * If the packet was for one of our unicast addresses, make
2826 2828 * sure we respond with that as the source. Otherwise
2827 2829 * have ip_output_simple pick the source address.
2828 2830 */
2829 2831 ire = ire_ftable_lookup_v4(ipha->ipha_dst, 0, 0,
2830 2832 (IRE_LOCAL|IRE_LOOPBACK), NULL, ira->ira_zoneid, NULL,
2831 2833 MATCH_IRE_TYPE|MATCH_IRE_ZONEONLY, 0, ipst, NULL);
2832 2834 if (ire != NULL) {
2833 2835 ire_refrele(ire);
2834 2836 src = ipha->ipha_dst;
2835 2837 } else {
2836 2838 src = INADDR_ANY;
2837 2839 ixas.ixa_flags |= IXAF_SET_SOURCE;
2838 2840 }
2839 2841
2840 2842 /*
2841 2843 * Check if we can send back more then 8 bytes in addition to
2842 2844 * the IP header. We try to send 64 bytes of data and the internal
2843 2845 * header in the special cases of ipv4 encapsulated ipv4 or ipv6.
2844 2846 */
2845 2847 len_needed = IPH_HDR_LENGTH(ipha);
2846 2848 if (ipha->ipha_protocol == IPPROTO_ENCAP ||
2847 2849 ipha->ipha_protocol == IPPROTO_IPV6) {
2848 2850 if (!pullupmsg(mp, -1)) {
2849 2851 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
2850 2852 ip_drop_output("ipIfStatsOutDiscards", mp, NULL);
2851 2853 freemsg(mp);
2852 2854 return;
2853 2855 }
2854 2856 ipha = (ipha_t *)mp->b_rptr;
2855 2857
2856 2858 if (ipha->ipha_protocol == IPPROTO_ENCAP) {
2857 2859 len_needed += IPH_HDR_LENGTH(((uchar_t *)ipha +
2858 2860 len_needed));
2859 2861 } else {
2860 2862 ip6_t *ip6h = (ip6_t *)((uchar_t *)ipha + len_needed);
2861 2863
2862 2864 ASSERT(ipha->ipha_protocol == IPPROTO_IPV6);
2863 2865 len_needed += ip_hdr_length_v6(mp, ip6h);
2864 2866 }
2865 2867 }
2866 2868 len_needed += ipst->ips_ip_icmp_return;
2867 2869 msg_len = msgdsize(mp);
2868 2870 if (msg_len > len_needed) {
2869 2871 (void) adjmsg(mp, len_needed - msg_len);
2870 2872 msg_len = len_needed;
2871 2873 }
2872 2874 mp1 = allocb(sizeof (icmp_ipha) + len, BPRI_MED);
2873 2875 if (mp1 == NULL) {
2874 2876 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutErrors);
2875 2877 freemsg(mp);
2876 2878 return;
2877 2879 }
2878 2880 mp1->b_cont = mp;
2879 2881 mp = mp1;
2880 2882
2881 2883 /*
2882 2884 * Set IXAF_TRUSTED_ICMP so we can let the ICMP messages this
2883 2885 * node generates be accepted in peace by all on-host destinations.
2884 2886 * If we do NOT assume that all on-host destinations trust
2885 2887 * self-generated ICMP messages, then rework here, ip6.c, and spd.c.
2886 2888 * (Look for IXAF_TRUSTED_ICMP).
2887 2889 */
2888 2890 ixas.ixa_flags |= IXAF_TRUSTED_ICMP;
2889 2891
2890 2892 ipha = (ipha_t *)mp->b_rptr;
2891 2893 mp1->b_wptr = (uchar_t *)ipha + (sizeof (icmp_ipha) + len);
2892 2894 *ipha = icmp_ipha;
2893 2895 ipha->ipha_src = src;
2894 2896 ipha->ipha_dst = dst;
2895 2897 ipha->ipha_ttl = ipst->ips_ip_def_ttl;
2896 2898 msg_len += sizeof (icmp_ipha) + len;
2897 2899 if (msg_len > IP_MAXPACKET) {
2898 2900 (void) adjmsg(mp, IP_MAXPACKET - msg_len);
2899 2901 msg_len = IP_MAXPACKET;
2900 2902 }
2901 2903 ipha->ipha_length = htons((uint16_t)msg_len);
2902 2904 icmph = (icmph_t *)&ipha[1];
2903 2905 bcopy(stuff, icmph, len);
2904 2906 icmph->icmph_checksum = 0;
2905 2907 icmph->icmph_checksum = IP_CSUM(mp, (int32_t)sizeof (ipha_t), 0);
2906 2908 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs);
2907 2909
2908 2910 (void) ip_output_simple(mp, &ixas);
2909 2911 ixa_cleanup(&ixas);
2910 2912 }
2911 2913
2912 2914 /*
2913 2915 * Determine if an ICMP error packet can be sent given the rate limit.
2914 2916 * The limit consists of an average frequency (icmp_pkt_err_interval measured
2915 2917 * in milliseconds) and a burst size. Burst size number of packets can
2916 2918 * be sent arbitrarely closely spaced.
2917 2919 * The state is tracked using two variables to implement an approximate
2918 2920 * token bucket filter:
2919 2921 * icmp_pkt_err_last - lbolt value when the last burst started
2920 2922 * icmp_pkt_err_sent - number of packets sent in current burst
2921 2923 */
2922 2924 boolean_t
2923 2925 icmp_err_rate_limit(ip_stack_t *ipst)
2924 2926 {
2925 2927 clock_t now = TICK_TO_MSEC(ddi_get_lbolt());
2926 2928 uint_t refilled; /* Number of packets refilled in tbf since last */
2927 2929 /* Guard against changes by loading into local variable */
2928 2930 uint_t err_interval = ipst->ips_ip_icmp_err_interval;
2929 2931
2930 2932 if (err_interval == 0)
2931 2933 return (B_FALSE);
2932 2934
2933 2935 if (ipst->ips_icmp_pkt_err_last > now) {
2934 2936 /* 100HZ lbolt in ms for 32bit arch wraps every 49.7 days */
2935 2937 ipst->ips_icmp_pkt_err_last = 0;
2936 2938 ipst->ips_icmp_pkt_err_sent = 0;
2937 2939 }
2938 2940 /*
2939 2941 * If we are in a burst update the token bucket filter.
2940 2942 * Update the "last" time to be close to "now" but make sure
2941 2943 * we don't loose precision.
2942 2944 */
2943 2945 if (ipst->ips_icmp_pkt_err_sent != 0) {
2944 2946 refilled = (now - ipst->ips_icmp_pkt_err_last)/err_interval;
2945 2947 if (refilled > ipst->ips_icmp_pkt_err_sent) {
2946 2948 ipst->ips_icmp_pkt_err_sent = 0;
2947 2949 } else {
2948 2950 ipst->ips_icmp_pkt_err_sent -= refilled;
2949 2951 ipst->ips_icmp_pkt_err_last += refilled * err_interval;
2950 2952 }
2951 2953 }
2952 2954 if (ipst->ips_icmp_pkt_err_sent == 0) {
2953 2955 /* Start of new burst */
2954 2956 ipst->ips_icmp_pkt_err_last = now;
2955 2957 }
2956 2958 if (ipst->ips_icmp_pkt_err_sent < ipst->ips_ip_icmp_err_burst) {
2957 2959 ipst->ips_icmp_pkt_err_sent++;
2958 2960 ip1dbg(("icmp_err_rate_limit: %d sent in burst\n",
2959 2961 ipst->ips_icmp_pkt_err_sent));
2960 2962 return (B_FALSE);
2961 2963 }
2962 2964 ip1dbg(("icmp_err_rate_limit: dropped\n"));
2963 2965 return (B_TRUE);
2964 2966 }
2965 2967
2966 2968 /*
2967 2969 * Check if it is ok to send an IPv4 ICMP error packet in
2968 2970 * response to the IPv4 packet in mp.
2969 2971 * Free the message and return null if no
2970 2972 * ICMP error packet should be sent.
2971 2973 */
2972 2974 static mblk_t *
2973 2975 icmp_pkt_err_ok(mblk_t *mp, ip_recv_attr_t *ira)
2974 2976 {
2975 2977 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
2976 2978 icmph_t *icmph;
2977 2979 ipha_t *ipha;
2978 2980 uint_t len_needed;
2979 2981
2980 2982 if (!mp)
2981 2983 return (NULL);
2982 2984 ipha = (ipha_t *)mp->b_rptr;
2983 2985 if (ip_csum_hdr(ipha)) {
2984 2986 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInCksumErrs);
2985 2987 ip_drop_input("ipIfStatsInCksumErrs", mp, NULL);
2986 2988 freemsg(mp);
2987 2989 return (NULL);
2988 2990 }
2989 2991 if (ip_type_v4(ipha->ipha_dst, ipst) == IRE_BROADCAST ||
2990 2992 ip_type_v4(ipha->ipha_src, ipst) == IRE_BROADCAST ||
2991 2993 CLASSD(ipha->ipha_dst) ||
2992 2994 CLASSD(ipha->ipha_src) ||
2993 2995 (ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET)) {
2994 2996 /* Note: only errors to the fragment with offset 0 */
2995 2997 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
2996 2998 freemsg(mp);
2997 2999 return (NULL);
2998 3000 }
2999 3001 if (ipha->ipha_protocol == IPPROTO_ICMP) {
3000 3002 /*
3001 3003 * Check the ICMP type. RFC 1122 sez: don't send ICMP
3002 3004 * errors in response to any ICMP errors.
3003 3005 */
3004 3006 len_needed = IPH_HDR_LENGTH(ipha) + ICMPH_SIZE;
3005 3007 if (mp->b_wptr - mp->b_rptr < len_needed) {
3006 3008 if (!pullupmsg(mp, len_needed)) {
3007 3009 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
3008 3010 freemsg(mp);
3009 3011 return (NULL);
3010 3012 }
3011 3013 ipha = (ipha_t *)mp->b_rptr;
3012 3014 }
3013 3015 icmph = (icmph_t *)
3014 3016 (&((char *)ipha)[IPH_HDR_LENGTH(ipha)]);
3015 3017 switch (icmph->icmph_type) {
3016 3018 case ICMP_DEST_UNREACHABLE:
3017 3019 case ICMP_SOURCE_QUENCH:
3018 3020 case ICMP_TIME_EXCEEDED:
3019 3021 case ICMP_PARAM_PROBLEM:
3020 3022 case ICMP_REDIRECT:
3021 3023 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
3022 3024 freemsg(mp);
3023 3025 return (NULL);
3024 3026 default:
3025 3027 break;
3026 3028 }
3027 3029 }
3028 3030 /*
3029 3031 * If this is a labeled system, then check to see if we're allowed to
3030 3032 * send a response to this particular sender. If not, then just drop.
3031 3033 */
3032 3034 if (is_system_labeled() && !tsol_can_reply_error(mp, ira)) {
3033 3035 ip2dbg(("icmp_pkt_err_ok: can't respond to packet\n"));
3034 3036 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
3035 3037 freemsg(mp);
3036 3038 return (NULL);
3037 3039 }
3038 3040 if (icmp_err_rate_limit(ipst)) {
3039 3041 /*
3040 3042 * Only send ICMP error packets every so often.
3041 3043 * This should be done on a per port/source basis,
3042 3044 * but for now this will suffice.
3043 3045 */
3044 3046 freemsg(mp);
3045 3047 return (NULL);
3046 3048 }
3047 3049 return (mp);
3048 3050 }
3049 3051
3050 3052 /*
3051 3053 * Called when a packet was sent out the same link that it arrived on.
3052 3054 * Check if it is ok to send a redirect and then send it.
3053 3055 */
3054 3056 void
3055 3057 ip_send_potential_redirect_v4(mblk_t *mp, ipha_t *ipha, ire_t *ire,
3056 3058 ip_recv_attr_t *ira)
3057 3059 {
3058 3060 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3059 3061 ipaddr_t src, nhop;
3060 3062 mblk_t *mp1;
3061 3063 ire_t *nhop_ire;
3062 3064
3063 3065 /*
3064 3066 * Check the source address to see if it originated
3065 3067 * on the same logical subnet it is going back out on.
3066 3068 * If so, we should be able to send it a redirect.
3067 3069 * Avoid sending a redirect if the destination
3068 3070 * is directly connected (i.e., we matched an IRE_ONLINK),
3069 3071 * or if the packet was source routed out this interface.
3070 3072 *
3071 3073 * We avoid sending a redirect if the
3072 3074 * destination is directly connected
3073 3075 * because it is possible that multiple
3074 3076 * IP subnets may have been configured on
3075 3077 * the link, and the source may not
3076 3078 * be on the same subnet as ip destination,
3077 3079 * even though they are on the same
3078 3080 * physical link.
3079 3081 */
3080 3082 if ((ire->ire_type & IRE_ONLINK) ||
3081 3083 ip_source_routed(ipha, ipst))
3082 3084 return;
3083 3085
3084 3086 nhop_ire = ire_nexthop(ire);
3085 3087 if (nhop_ire == NULL)
3086 3088 return;
3087 3089
3088 3090 nhop = nhop_ire->ire_addr;
3089 3091
3090 3092 if (nhop_ire->ire_type & IRE_IF_CLONE) {
3091 3093 ire_t *ire2;
3092 3094
3093 3095 /* Follow ire_dep_parent to find non-clone IRE_INTERFACE */
3094 3096 mutex_enter(&nhop_ire->ire_lock);
3095 3097 ire2 = nhop_ire->ire_dep_parent;
3096 3098 if (ire2 != NULL)
3097 3099 ire_refhold(ire2);
3098 3100 mutex_exit(&nhop_ire->ire_lock);
3099 3101 ire_refrele(nhop_ire);
3100 3102 nhop_ire = ire2;
3101 3103 }
3102 3104 if (nhop_ire == NULL)
3103 3105 return;
3104 3106
3105 3107 ASSERT(!(nhop_ire->ire_type & IRE_IF_CLONE));
3106 3108
3107 3109 src = ipha->ipha_src;
3108 3110
3109 3111 /*
3110 3112 * We look at the interface ire for the nexthop,
3111 3113 * to see if ipha_src is in the same subnet
3112 3114 * as the nexthop.
3113 3115 */
3114 3116 if ((src & nhop_ire->ire_mask) == (nhop & nhop_ire->ire_mask)) {
3115 3117 /*
3116 3118 * The source is directly connected.
3117 3119 */
3118 3120 mp1 = copymsg(mp);
3119 3121 if (mp1 != NULL) {
3120 3122 icmp_send_redirect(mp1, nhop, ira);
3121 3123 }
3122 3124 }
3123 3125 ire_refrele(nhop_ire);
3124 3126 }
3125 3127
3126 3128 /*
3127 3129 * Generate an ICMP redirect message.
3128 3130 */
3129 3131 static void
3130 3132 icmp_send_redirect(mblk_t *mp, ipaddr_t gateway, ip_recv_attr_t *ira)
3131 3133 {
3132 3134 icmph_t icmph;
3133 3135 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3134 3136
3135 3137 mp = icmp_pkt_err_ok(mp, ira);
3136 3138 if (mp == NULL)
3137 3139 return;
3138 3140
3139 3141 bzero(&icmph, sizeof (icmph_t));
3140 3142 icmph.icmph_type = ICMP_REDIRECT;
3141 3143 icmph.icmph_code = 1;
3142 3144 icmph.icmph_rd_gateway = gateway;
3143 3145 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutRedirects);
3144 3146 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3145 3147 }
3146 3148
3147 3149 /*
3148 3150 * Generate an ICMP time exceeded message.
3149 3151 */
3150 3152 void
3151 3153 icmp_time_exceeded(mblk_t *mp, uint8_t code, ip_recv_attr_t *ira)
3152 3154 {
3153 3155 icmph_t icmph;
3154 3156 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3155 3157
3156 3158 mp = icmp_pkt_err_ok(mp, ira);
3157 3159 if (mp == NULL)
3158 3160 return;
3159 3161
3160 3162 bzero(&icmph, sizeof (icmph_t));
3161 3163 icmph.icmph_type = ICMP_TIME_EXCEEDED;
3162 3164 icmph.icmph_code = code;
3163 3165 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimeExcds);
3164 3166 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3165 3167 }
3166 3168
3167 3169 /*
3168 3170 * Generate an ICMP unreachable message.
3169 3171 * When called from ip_output side a minimal ip_recv_attr_t needs to be
3170 3172 * constructed by the caller.
3171 3173 */
3172 3174 void
3173 3175 icmp_unreachable(mblk_t *mp, uint8_t code, ip_recv_attr_t *ira)
3174 3176 {
3175 3177 icmph_t icmph;
3176 3178 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
3177 3179
3178 3180 mp = icmp_pkt_err_ok(mp, ira);
3179 3181 if (mp == NULL)
3180 3182 return;
3181 3183
3182 3184 bzero(&icmph, sizeof (icmph_t));
3183 3185 icmph.icmph_type = ICMP_DEST_UNREACHABLE;
3184 3186 icmph.icmph_code = code;
3185 3187 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs);
3186 3188 icmp_pkt(mp, &icmph, sizeof (icmph_t), ira);
3187 3189 }
3188 3190
3189 3191 /*
3190 3192 * Latch in the IPsec state for a stream based the policy in the listener
3191 3193 * and the actions in the ip_recv_attr_t.
3192 3194 * Called directly from TCP and SCTP.
3193 3195 */
3194 3196 boolean_t
3195 3197 ip_ipsec_policy_inherit(conn_t *connp, conn_t *lconnp, ip_recv_attr_t *ira)
3196 3198 {
3197 3199 ASSERT(lconnp->conn_policy != NULL);
3198 3200 ASSERT(connp->conn_policy == NULL);
3199 3201
3200 3202 IPPH_REFHOLD(lconnp->conn_policy);
3201 3203 connp->conn_policy = lconnp->conn_policy;
3202 3204
3203 3205 if (ira->ira_ipsec_action != NULL) {
3204 3206 if (connp->conn_latch == NULL) {
3205 3207 connp->conn_latch = iplatch_create();
3206 3208 if (connp->conn_latch == NULL)
3207 3209 return (B_FALSE);
3208 3210 }
3209 3211 ipsec_latch_inbound(connp, ira);
3210 3212 }
3211 3213 return (B_TRUE);
3212 3214 }
3213 3215
3214 3216 /*
3215 3217 * Verify whether or not the IP address is a valid local address.
3216 3218 * Could be a unicast, including one for a down interface.
3217 3219 * If allow_mcbc then a multicast or broadcast address is also
3218 3220 * acceptable.
3219 3221 *
3220 3222 * In the case of a broadcast/multicast address, however, the
3221 3223 * upper protocol is expected to reset the src address
3222 3224 * to zero when we return IPVL_MCAST/IPVL_BCAST so that
3223 3225 * no packets are emitted with broadcast/multicast address as
3224 3226 * source address (that violates hosts requirements RFC 1122)
3225 3227 * The addresses valid for bind are:
3226 3228 * (1) - INADDR_ANY (0)
3227 3229 * (2) - IP address of an UP interface
3228 3230 * (3) - IP address of a DOWN interface
3229 3231 * (4) - valid local IP broadcast addresses. In this case
3230 3232 * the conn will only receive packets destined to
3231 3233 * the specified broadcast address.
3232 3234 * (5) - a multicast address. In this case
3233 3235 * the conn will only receive packets destined to
3234 3236 * the specified multicast address. Note: the
3235 3237 * application still has to issue an
3236 3238 * IP_ADD_MEMBERSHIP socket option.
3237 3239 *
3238 3240 * In all the above cases, the bound address must be valid in the current zone.
3239 3241 * When the address is loopback, multicast or broadcast, there might be many
3240 3242 * matching IREs so bind has to look up based on the zone.
3241 3243 */
3242 3244 ip_laddr_t
3243 3245 ip_laddr_verify_v4(ipaddr_t src_addr, zoneid_t zoneid,
3244 3246 ip_stack_t *ipst, boolean_t allow_mcbc)
3245 3247 {
3246 3248 ire_t *src_ire;
3247 3249
3248 3250 ASSERT(src_addr != INADDR_ANY);
3249 3251
3250 3252 src_ire = ire_ftable_lookup_v4(src_addr, 0, 0, 0,
3251 3253 NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, 0, ipst, NULL);
3252 3254
3253 3255 /*
3254 3256 * If an address other than in6addr_any is requested,
3255 3257 * we verify that it is a valid address for bind
3256 3258 * Note: Following code is in if-else-if form for
3257 3259 * readability compared to a condition check.
3258 3260 */
3259 3261 if (src_ire != NULL && (src_ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK))) {
3260 3262 /*
3261 3263 * (2) Bind to address of local UP interface
3262 3264 */
3263 3265 ire_refrele(src_ire);
3264 3266 return (IPVL_UNICAST_UP);
3265 3267 } else if (src_ire != NULL && src_ire->ire_type & IRE_BROADCAST) {
3266 3268 /*
3267 3269 * (4) Bind to broadcast address
3268 3270 */
3269 3271 ire_refrele(src_ire);
3270 3272 if (allow_mcbc)
3271 3273 return (IPVL_BCAST);
3272 3274 else
3273 3275 return (IPVL_BAD);
3274 3276 } else if (CLASSD(src_addr)) {
3275 3277 /* (5) bind to multicast address. */
3276 3278 if (src_ire != NULL)
3277 3279 ire_refrele(src_ire);
3278 3280
3279 3281 if (allow_mcbc)
3280 3282 return (IPVL_MCAST);
3281 3283 else
3282 3284 return (IPVL_BAD);
3283 3285 } else {
3284 3286 ipif_t *ipif;
3285 3287
3286 3288 /*
3287 3289 * (3) Bind to address of local DOWN interface?
3288 3290 * (ipif_lookup_addr() looks up all interfaces
3289 3291 * but we do not get here for UP interfaces
3290 3292 * - case (2) above)
3291 3293 */
3292 3294 if (src_ire != NULL)
3293 3295 ire_refrele(src_ire);
3294 3296
3295 3297 ipif = ipif_lookup_addr(src_addr, NULL, zoneid, ipst);
3296 3298 if (ipif == NULL)
3297 3299 return (IPVL_BAD);
3298 3300
3299 3301 /* Not a useful source? */
3300 3302 if (ipif->ipif_flags & (IPIF_NOLOCAL | IPIF_ANYCAST)) {
3301 3303 ipif_refrele(ipif);
3302 3304 return (IPVL_BAD);
3303 3305 }
3304 3306 ipif_refrele(ipif);
3305 3307 return (IPVL_UNICAST_DOWN);
3306 3308 }
3307 3309 }
3308 3310
3309 3311 /*
3310 3312 * Insert in the bind fanout for IPv4 and IPv6.
3311 3313 * The caller should already have used ip_laddr_verify_v*() before calling
3312 3314 * this.
3313 3315 */
3314 3316 int
3315 3317 ip_laddr_fanout_insert(conn_t *connp)
3316 3318 {
3317 3319 int error;
3318 3320
3319 3321 /*
3320 3322 * Allow setting new policies. For example, disconnects result
3321 3323 * in us being called. As we would have set conn_policy_cached
3322 3324 * to B_TRUE before, we should set it to B_FALSE, so that policy
3323 3325 * can change after the disconnect.
3324 3326 */
3325 3327 connp->conn_policy_cached = B_FALSE;
3326 3328
3327 3329 error = ipcl_bind_insert(connp);
3328 3330 if (error != 0) {
3329 3331 if (connp->conn_anon_port) {
3330 3332 (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
3331 3333 connp->conn_mlp_type, connp->conn_proto,
3332 3334 ntohs(connp->conn_lport), B_FALSE);
3333 3335 }
3334 3336 connp->conn_mlp_type = mlptSingle;
3335 3337 }
3336 3338 return (error);
3337 3339 }
3338 3340
3339 3341 /*
3340 3342 * Verify that both the source and destination addresses are valid. If
3341 3343 * IPDF_VERIFY_DST is not set, then the destination address may be unreachable,
3342 3344 * i.e. have no route to it. Protocols like TCP want to verify destination
3343 3345 * reachability, while tunnels do not.
3344 3346 *
3345 3347 * Determine the route, the interface, and (optionally) the source address
3346 3348 * to use to reach a given destination.
3347 3349 * Note that we allow connect to broadcast and multicast addresses when
3348 3350 * IPDF_ALLOW_MCBC is set.
3349 3351 * first_hop and dst_addr are normally the same, but if source routing
3350 3352 * they will differ; in that case the first_hop is what we'll use for the
3351 3353 * routing lookup but the dce and label checks will be done on dst_addr,
3352 3354 *
3353 3355 * If uinfo is set, then we fill in the best available information
3354 3356 * we have for the destination. This is based on (in priority order) any
3355 3357 * metrics and path MTU stored in a dce_t, route metrics, and finally the
3356 3358 * ill_mtu/ill_mc_mtu.
3357 3359 *
3358 3360 * Tsol note: If we have a source route then dst_addr != firsthop. But we
3359 3361 * always do the label check on dst_addr.
3360 3362 */
3361 3363 int
3362 3364 ip_set_destination_v4(ipaddr_t *src_addrp, ipaddr_t dst_addr, ipaddr_t firsthop,
3363 3365 ip_xmit_attr_t *ixa, iulp_t *uinfo, uint32_t flags, uint_t mac_mode)
3364 3366 {
3365 3367 ire_t *ire = NULL;
3366 3368 int error = 0;
3367 3369 ipaddr_t setsrc; /* RTF_SETSRC */
3368 3370 zoneid_t zoneid = ixa->ixa_zoneid; /* Honors SO_ALLZONES */
3369 3371 ip_stack_t *ipst = ixa->ixa_ipst;
3370 3372 dce_t *dce;
3371 3373 uint_t pmtu;
3372 3374 uint_t generation;
3373 3375 nce_t *nce;
3374 3376 ill_t *ill = NULL;
3375 3377 boolean_t multirt = B_FALSE;
3376 3378
3377 3379 ASSERT(ixa->ixa_flags & IXAF_IS_IPV4);
3378 3380
3379 3381 /*
3380 3382 * We never send to zero; the ULPs map it to the loopback address.
3381 3383 * We can't allow it since we use zero to mean unitialized in some
3382 3384 * places.
3383 3385 */
3384 3386 ASSERT(dst_addr != INADDR_ANY);
3385 3387
3386 3388 if (is_system_labeled()) {
3387 3389 ts_label_t *tsl = NULL;
3388 3390
3389 3391 error = tsol_check_dest(ixa->ixa_tsl, &dst_addr, IPV4_VERSION,
3390 3392 mac_mode, (flags & IPDF_ZONE_IS_GLOBAL) != 0, &tsl);
3391 3393 if (error != 0)
3392 3394 return (error);
3393 3395 if (tsl != NULL) {
3394 3396 /* Update the label */
3395 3397 ip_xmit_attr_replace_tsl(ixa, tsl);
3396 3398 }
3397 3399 }
3398 3400
3399 3401 setsrc = INADDR_ANY;
3400 3402 /*
3401 3403 * Select a route; For IPMP interfaces, we would only select
3402 3404 * a "hidden" route (i.e., going through a specific under_ill)
3403 3405 * if ixa_ifindex has been specified.
3404 3406 */
3405 3407 ire = ip_select_route_v4(firsthop, *src_addrp, ixa,
3406 3408 &generation, &setsrc, &error, &multirt);
3407 3409 ASSERT(ire != NULL); /* IRE_NOROUTE if none found */
3408 3410 if (error != 0)
3409 3411 goto bad_addr;
3410 3412
3411 3413 /*
3412 3414 * ire can't be a broadcast or multicast unless IPDF_ALLOW_MCBC is set.
3413 3415 * If IPDF_VERIFY_DST is set, the destination must be reachable;
3414 3416 * Otherwise the destination needn't be reachable.
3415 3417 *
3416 3418 * If we match on a reject or black hole, then we've got a
3417 3419 * local failure. May as well fail out the connect() attempt,
3418 3420 * since it's never going to succeed.
3419 3421 */
3420 3422 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
3421 3423 /*
3422 3424 * If we're verifying destination reachability, we always want
3423 3425 * to complain here.
3424 3426 *
3425 3427 * If we're not verifying destination reachability but the
3426 3428 * destination has a route, we still want to fail on the
3427 3429 * temporary address and broadcast address tests.
3428 3430 *
3429 3431 * In both cases do we let the code continue so some reasonable
3430 3432 * information is returned to the caller. That enables the
3431 3433 * caller to use (and even cache) the IRE. conn_ip_ouput will
3432 3434 * use the generation mismatch path to check for the unreachable
3433 3435 * case thereby avoiding any specific check in the main path.
3434 3436 */
3435 3437 ASSERT(generation == IRE_GENERATION_VERIFY);
3436 3438 if (flags & IPDF_VERIFY_DST) {
3437 3439 /*
3438 3440 * Set errno but continue to set up ixa_ire to be
3439 3441 * the RTF_REJECT|RTF_BLACKHOLE IRE.
3440 3442 * That allows callers to use ip_output to get an
3441 3443 * ICMP error back.
3442 3444 */
3443 3445 if (!(ire->ire_type & IRE_HOST))
3444 3446 error = ENETUNREACH;
3445 3447 else
3446 3448 error = EHOSTUNREACH;
3447 3449 }
3448 3450 }
3449 3451
3450 3452 if ((ire->ire_type & (IRE_BROADCAST|IRE_MULTICAST)) &&
3451 3453 !(flags & IPDF_ALLOW_MCBC)) {
3452 3454 ire_refrele(ire);
3453 3455 ire = ire_reject(ipst, B_FALSE);
3454 3456 generation = IRE_GENERATION_VERIFY;
3455 3457 error = ENETUNREACH;
3456 3458 }
3457 3459
3458 3460 /* Cache things */
3459 3461 if (ixa->ixa_ire != NULL)
3460 3462 ire_refrele_notr(ixa->ixa_ire);
3461 3463 #ifdef DEBUG
3462 3464 ire_refhold_notr(ire);
3463 3465 ire_refrele(ire);
3464 3466 #endif
3465 3467 ixa->ixa_ire = ire;
3466 3468 ixa->ixa_ire_generation = generation;
3467 3469
3468 3470 /*
3469 3471 * Ensure that ixa_dce is always set any time that ixa_ire is set,
3470 3472 * since some callers will send a packet to conn_ip_output() even if
3471 3473 * there's an error.
3472 3474 */
3473 3475 if (flags & IPDF_UNIQUE_DCE) {
3474 3476 /* Fallback to the default dce if allocation fails */
3475 3477 dce = dce_lookup_and_add_v4(dst_addr, ipst);
3476 3478 if (dce != NULL)
3477 3479 generation = dce->dce_generation;
3478 3480 else
3479 3481 dce = dce_lookup_v4(dst_addr, ipst, &generation);
3480 3482 } else {
3481 3483 dce = dce_lookup_v4(dst_addr, ipst, &generation);
3482 3484 }
3483 3485 ASSERT(dce != NULL);
3484 3486 if (ixa->ixa_dce != NULL)
3485 3487 dce_refrele_notr(ixa->ixa_dce);
3486 3488 #ifdef DEBUG
3487 3489 dce_refhold_notr(dce);
3488 3490 dce_refrele(dce);
3489 3491 #endif
3490 3492 ixa->ixa_dce = dce;
3491 3493 ixa->ixa_dce_generation = generation;
3492 3494
3493 3495 /*
3494 3496 * For multicast with multirt we have a flag passed back from
3495 3497 * ire_lookup_multi_ill_v4 since we don't have an IRE for each
3496 3498 * possible multicast address.
3497 3499 * We also need a flag for multicast since we can't check
3498 3500 * whether RTF_MULTIRT is set in ixa_ire for multicast.
3499 3501 */
3500 3502 if (multirt) {
3501 3503 ixa->ixa_postfragfn = ip_postfrag_multirt_v4;
3502 3504 ixa->ixa_flags |= IXAF_MULTIRT_MULTICAST;
3503 3505 } else {
3504 3506 ixa->ixa_postfragfn = ire->ire_postfragfn;
3505 3507 ixa->ixa_flags &= ~IXAF_MULTIRT_MULTICAST;
3506 3508 }
3507 3509 if (!(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) {
3508 3510 /* Get an nce to cache. */
3509 3511 nce = ire_to_nce(ire, firsthop, NULL);
3510 3512 if (nce == NULL) {
3511 3513 /* Allocation failure? */
3512 3514 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3513 3515 } else {
3514 3516 if (ixa->ixa_nce != NULL)
3515 3517 nce_refrele(ixa->ixa_nce);
3516 3518 ixa->ixa_nce = nce;
3517 3519 }
3518 3520 }
3519 3521
3520 3522 /*
3521 3523 * If the source address is a loopback address, the
3522 3524 * destination had best be local or multicast.
3523 3525 * If we are sending to an IRE_LOCAL using a loopback source then
3524 3526 * it had better be the same zoneid.
3525 3527 */
3526 3528 if (*src_addrp == htonl(INADDR_LOOPBACK)) {
3527 3529 if ((ire->ire_type & IRE_LOCAL) && ire->ire_zoneid != zoneid) {
3528 3530 ire = NULL; /* Stored in ixa_ire */
3529 3531 error = EADDRNOTAVAIL;
3530 3532 goto bad_addr;
3531 3533 }
3532 3534 if (!(ire->ire_type & (IRE_LOOPBACK|IRE_LOCAL|IRE_MULTICAST))) {
3533 3535 ire = NULL; /* Stored in ixa_ire */
3534 3536 error = EADDRNOTAVAIL;
3535 3537 goto bad_addr;
3536 3538 }
3537 3539 }
3538 3540 if (ire->ire_type & IRE_BROADCAST) {
3539 3541 /*
3540 3542 * If the ULP didn't have a specified source, then we
3541 3543 * make sure we reselect the source when sending
3542 3544 * broadcasts out different interfaces.
3543 3545 */
3544 3546 if (flags & IPDF_SELECT_SRC)
3545 3547 ixa->ixa_flags |= IXAF_SET_SOURCE;
3546 3548 else
3547 3549 ixa->ixa_flags &= ~IXAF_SET_SOURCE;
3548 3550 }
3549 3551
3550 3552 /*
3551 3553 * Does the caller want us to pick a source address?
3552 3554 */
3553 3555 if (flags & IPDF_SELECT_SRC) {
3554 3556 ipaddr_t src_addr;
3555 3557
3556 3558 /*
3557 3559 * We use use ire_nexthop_ill to avoid the under ipmp
3558 3560 * interface for source address selection. Note that for ipmp
3559 3561 * probe packets, ixa_ifindex would have been specified, and
3560 3562 * the ip_select_route() invocation would have picked an ire
3561 3563 * will ire_ill pointing at an under interface.
3562 3564 */
3563 3565 ill = ire_nexthop_ill(ire);
3564 3566
3565 3567 /* If unreachable we have no ill but need some source */
3566 3568 if (ill == NULL) {
3567 3569 src_addr = htonl(INADDR_LOOPBACK);
3568 3570 /* Make sure we look for a better source address */
3569 3571 generation = SRC_GENERATION_VERIFY;
3570 3572 } else {
3571 3573 error = ip_select_source_v4(ill, setsrc, dst_addr,
3572 3574 ixa->ixa_multicast_ifaddr, zoneid,
3573 3575 ipst, &src_addr, &generation, NULL);
3574 3576 if (error != 0) {
3575 3577 ire = NULL; /* Stored in ixa_ire */
3576 3578 goto bad_addr;
3577 3579 }
3578 3580 }
3579 3581
3580 3582 /*
3581 3583 * We allow the source address to to down.
3582 3584 * However, we check that we don't use the loopback address
3583 3585 * as a source when sending out on the wire.
3584 3586 */
3585 3587 if ((src_addr == htonl(INADDR_LOOPBACK)) &&
3586 3588 !(ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK|IRE_MULTICAST)) &&
3587 3589 !(ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))) {
3588 3590 ire = NULL; /* Stored in ixa_ire */
3589 3591 error = EADDRNOTAVAIL;
3590 3592 goto bad_addr;
3591 3593 }
3592 3594
3593 3595 *src_addrp = src_addr;
3594 3596 ixa->ixa_src_generation = generation;
3595 3597 }
3596 3598
3597 3599 /*
3598 3600 * Make sure we don't leave an unreachable ixa_nce in place
3599 3601 * since ip_select_route is used when we unplumb i.e., remove
3600 3602 * references on ixa_ire, ixa_nce, and ixa_dce.
3601 3603 */
3602 3604 nce = ixa->ixa_nce;
3603 3605 if (nce != NULL && nce->nce_is_condemned) {
3604 3606 nce_refrele(nce);
3605 3607 ixa->ixa_nce = NULL;
3606 3608 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3607 3609 }
3608 3610
3609 3611 /*
3610 3612 * The caller has set IXAF_PMTU_DISCOVERY if path MTU is desired.
3611 3613 * However, we can't do it for IPv4 multicast or broadcast.
3612 3614 */
3613 3615 if (ire->ire_type & (IRE_BROADCAST|IRE_MULTICAST))
3614 3616 ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY;
3615 3617
3616 3618 /*
3617 3619 * Set initial value for fragmentation limit. Either conn_ip_output
3618 3620 * or ULP might updates it when there are routing changes.
3619 3621 * Handles a NULL ixa_ire->ire_ill or a NULL ixa_nce for RTF_REJECT.
3620 3622 */
3621 3623 pmtu = ip_get_pmtu(ixa);
3622 3624 ixa->ixa_fragsize = pmtu;
3623 3625 /* Make sure ixa_fragsize and ixa_pmtu remain identical */
3624 3626 if (ixa->ixa_flags & IXAF_VERIFY_PMTU)
3625 3627 ixa->ixa_pmtu = pmtu;
3626 3628
3627 3629 /*
3628 3630 * Extract information useful for some transports.
3629 3631 * First we look for DCE metrics. Then we take what we have in
3630 3632 * the metrics in the route, where the offlink is used if we have
3631 3633 * one.
3632 3634 */
3633 3635 if (uinfo != NULL) {
3634 3636 bzero(uinfo, sizeof (*uinfo));
3635 3637
3636 3638 if (dce->dce_flags & DCEF_UINFO)
3637 3639 *uinfo = dce->dce_uinfo;
3638 3640
3639 3641 rts_merge_metrics(uinfo, &ire->ire_metrics);
3640 3642
3641 3643 /* Allow ire_metrics to decrease the path MTU from above */
3642 3644 if (uinfo->iulp_mtu == 0 || uinfo->iulp_mtu > pmtu)
3643 3645 uinfo->iulp_mtu = pmtu;
3644 3646
3645 3647 uinfo->iulp_localnet = (ire->ire_type & IRE_ONLINK) != 0;
3646 3648 uinfo->iulp_loopback = (ire->ire_type & IRE_LOOPBACK) != 0;
3647 3649 uinfo->iulp_local = (ire->ire_type & IRE_LOCAL) != 0;
3648 3650 }
3649 3651
3650 3652 if (ill != NULL)
3651 3653 ill_refrele(ill);
3652 3654
3653 3655 return (error);
3654 3656
3655 3657 bad_addr:
3656 3658 if (ire != NULL)
3657 3659 ire_refrele(ire);
3658 3660
3659 3661 if (ill != NULL)
3660 3662 ill_refrele(ill);
3661 3663
3662 3664 /*
3663 3665 * Make sure we don't leave an unreachable ixa_nce in place
3664 3666 * since ip_select_route is used when we unplumb i.e., remove
3665 3667 * references on ixa_ire, ixa_nce, and ixa_dce.
3666 3668 */
3667 3669 nce = ixa->ixa_nce;
3668 3670 if (nce != NULL && nce->nce_is_condemned) {
3669 3671 nce_refrele(nce);
3670 3672 ixa->ixa_nce = NULL;
3671 3673 ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
3672 3674 }
3673 3675
3674 3676 return (error);
3675 3677 }
3676 3678
3677 3679
3678 3680 /*
3679 3681 * Get the base MTU for the case when path MTU discovery is not used.
3680 3682 * Takes the MTU of the IRE into account.
3681 3683 */
3682 3684 uint_t
3683 3685 ip_get_base_mtu(ill_t *ill, ire_t *ire)
3684 3686 {
3685 3687 uint_t mtu;
3686 3688 uint_t iremtu = ire->ire_metrics.iulp_mtu;
3687 3689
3688 3690 if (ire->ire_type & (IRE_MULTICAST|IRE_BROADCAST))
3689 3691 mtu = ill->ill_mc_mtu;
3690 3692 else
3691 3693 mtu = ill->ill_mtu;
3692 3694
3693 3695 if (iremtu != 0 && iremtu < mtu)
3694 3696 mtu = iremtu;
3695 3697
3696 3698 return (mtu);
3697 3699 }
3698 3700
3699 3701 /*
3700 3702 * Get the PMTU for the attributes. Handles both IPv4 and IPv6.
3701 3703 * Assumes that ixa_ire, dce, and nce have already been set up.
3702 3704 *
3703 3705 * The caller has set IXAF_PMTU_DISCOVERY if path MTU discovery is desired.
3704 3706 * We avoid path MTU discovery if it is disabled with ndd.
3705 3707 * Furtermore, if the path MTU is too small, then we don't set DF for IPv4.
3706 3708 *
3707 3709 * NOTE: We also used to turn it off for source routed packets. That
3708 3710 * is no longer required since the dce is per final destination.
3709 3711 */
3710 3712 uint_t
3711 3713 ip_get_pmtu(ip_xmit_attr_t *ixa)
3712 3714 {
3713 3715 ip_stack_t *ipst = ixa->ixa_ipst;
3714 3716 dce_t *dce;
3715 3717 nce_t *nce;
3716 3718 ire_t *ire;
3717 3719 uint_t pmtu;
3718 3720
3719 3721 ire = ixa->ixa_ire;
3720 3722 dce = ixa->ixa_dce;
3721 3723 nce = ixa->ixa_nce;
3722 3724
3723 3725 /*
3724 3726 * If path MTU discovery has been turned off by ndd, then we ignore
3725 3727 * any dce_pmtu and for IPv4 we will not set DF.
3726 3728 */
3727 3729 if (!ipst->ips_ip_path_mtu_discovery)
3728 3730 ixa->ixa_flags &= ~IXAF_PMTU_DISCOVERY;
3729 3731
3730 3732 pmtu = IP_MAXPACKET;
3731 3733 /*
3732 3734 * Decide whether whether IPv4 sets DF
3733 3735 * For IPv6 "no DF" means to use the 1280 mtu
3734 3736 */
3735 3737 if (ixa->ixa_flags & IXAF_PMTU_DISCOVERY) {
3736 3738 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3737 3739 } else {
3738 3740 ixa->ixa_flags &= ~IXAF_PMTU_IPV4_DF;
3739 3741 if (!(ixa->ixa_flags & IXAF_IS_IPV4))
3740 3742 pmtu = IPV6_MIN_MTU;
3741 3743 }
3742 3744
3743 3745 /* Check if the PMTU is to old before we use it */
3744 3746 if ((dce->dce_flags & DCEF_PMTU) &&
3745 3747 TICK_TO_SEC(ddi_get_lbolt64()) - dce->dce_last_change_time >
3746 3748 ipst->ips_ip_pathmtu_interval) {
3747 3749 /*
3748 3750 * Older than 20 minutes. Drop the path MTU information.
3749 3751 */
3750 3752 mutex_enter(&dce->dce_lock);
3751 3753 dce->dce_flags &= ~(DCEF_PMTU|DCEF_TOO_SMALL_PMTU);
3752 3754 dce->dce_last_change_time = TICK_TO_SEC(ddi_get_lbolt64());
3753 3755 mutex_exit(&dce->dce_lock);
3754 3756 dce_increment_generation(dce);
3755 3757 }
3756 3758
3757 3759 /* The metrics on the route can lower the path MTU */
3758 3760 if (ire->ire_metrics.iulp_mtu != 0 &&
3759 3761 ire->ire_metrics.iulp_mtu < pmtu)
3760 3762 pmtu = ire->ire_metrics.iulp_mtu;
3761 3763
3762 3764 /*
3763 3765 * If the path MTU is smaller than some minimum, we still use dce_pmtu
3764 3766 * above (would be 576 for IPv4 and 1280 for IPv6), but we clear
3765 3767 * IXAF_PMTU_IPV4_DF so that we avoid setting DF for IPv4.
3766 3768 */
3767 3769 if (ixa->ixa_flags & IXAF_PMTU_DISCOVERY) {
3768 3770 if (dce->dce_flags & DCEF_PMTU) {
3769 3771 if (dce->dce_pmtu < pmtu)
3770 3772 pmtu = dce->dce_pmtu;
3771 3773
3772 3774 if (dce->dce_flags & DCEF_TOO_SMALL_PMTU) {
3773 3775 ixa->ixa_flags |= IXAF_PMTU_TOO_SMALL;
3774 3776 ixa->ixa_flags &= ~IXAF_PMTU_IPV4_DF;
3775 3777 } else {
3776 3778 ixa->ixa_flags &= ~IXAF_PMTU_TOO_SMALL;
3777 3779 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3778 3780 }
3779 3781 } else {
3780 3782 ixa->ixa_flags &= ~IXAF_PMTU_TOO_SMALL;
3781 3783 ixa->ixa_flags |= IXAF_PMTU_IPV4_DF;
3782 3784 }
3783 3785 }
3784 3786
3785 3787 /*
3786 3788 * If we have an IRE_LOCAL we use the loopback mtu instead of
3787 3789 * the ill for going out the wire i.e., IRE_LOCAL gets the same
3788 3790 * mtu as IRE_LOOPBACK.
3789 3791 */
3790 3792 if (ire->ire_type & (IRE_LOCAL|IRE_LOOPBACK)) {
3791 3793 uint_t loopback_mtu;
3792 3794
3793 3795 loopback_mtu = (ire->ire_ipversion == IPV6_VERSION) ?
3794 3796 ip_loopback_mtu_v6plus : ip_loopback_mtuplus;
3795 3797
3796 3798 if (loopback_mtu < pmtu)
3797 3799 pmtu = loopback_mtu;
3798 3800 } else if (nce != NULL) {
3799 3801 /*
3800 3802 * Make sure we don't exceed the interface MTU.
3801 3803 * In the case of RTF_REJECT or RTF_BLACKHOLE we might not have
3802 3804 * an ill. We'd use the above IP_MAXPACKET in that case just
3803 3805 * to tell the transport something larger than zero.
3804 3806 */
3805 3807 if (ire->ire_type & (IRE_MULTICAST|IRE_BROADCAST)) {
3806 3808 if (nce->nce_common->ncec_ill->ill_mc_mtu < pmtu)
3807 3809 pmtu = nce->nce_common->ncec_ill->ill_mc_mtu;
3808 3810 if (nce->nce_common->ncec_ill != nce->nce_ill &&
3809 3811 nce->nce_ill->ill_mc_mtu < pmtu) {
3810 3812 /*
3811 3813 * for interfaces in an IPMP group, the mtu of
3812 3814 * the nce_ill (under_ill) could be different
3813 3815 * from the mtu of the ncec_ill, so we take the
3814 3816 * min of the two.
3815 3817 */
3816 3818 pmtu = nce->nce_ill->ill_mc_mtu;
3817 3819 }
3818 3820 } else {
3819 3821 if (nce->nce_common->ncec_ill->ill_mtu < pmtu)
3820 3822 pmtu = nce->nce_common->ncec_ill->ill_mtu;
3821 3823 if (nce->nce_common->ncec_ill != nce->nce_ill &&
3822 3824 nce->nce_ill->ill_mtu < pmtu) {
3823 3825 /*
3824 3826 * for interfaces in an IPMP group, the mtu of
3825 3827 * the nce_ill (under_ill) could be different
3826 3828 * from the mtu of the ncec_ill, so we take the
3827 3829 * min of the two.
3828 3830 */
3829 3831 pmtu = nce->nce_ill->ill_mtu;
3830 3832 }
3831 3833 }
3832 3834 }
3833 3835
3834 3836 /*
3835 3837 * Handle the IPV6_USE_MIN_MTU socket option or ancillary data.
3836 3838 * Only applies to IPv6.
3837 3839 */
3838 3840 if (!(ixa->ixa_flags & IXAF_IS_IPV4)) {
3839 3841 if (ixa->ixa_flags & IXAF_USE_MIN_MTU) {
3840 3842 switch (ixa->ixa_use_min_mtu) {
3841 3843 case IPV6_USE_MIN_MTU_MULTICAST:
3842 3844 if (ire->ire_type & IRE_MULTICAST)
3843 3845 pmtu = IPV6_MIN_MTU;
3844 3846 break;
3845 3847 case IPV6_USE_MIN_MTU_ALWAYS:
3846 3848 pmtu = IPV6_MIN_MTU;
3847 3849 break;
3848 3850 case IPV6_USE_MIN_MTU_NEVER:
3849 3851 break;
3850 3852 }
3851 3853 } else {
3852 3854 /* Default is IPV6_USE_MIN_MTU_MULTICAST */
3853 3855 if (ire->ire_type & IRE_MULTICAST)
3854 3856 pmtu = IPV6_MIN_MTU;
3855 3857 }
3856 3858 }
3857 3859
3858 3860 /*
3859 3861 * After receiving an ICMPv6 "packet too big" message with a
3860 3862 * MTU < 1280, and for multirouted IPv6 packets, the IP layer
3861 3863 * will insert a 8-byte fragment header in every packet. We compensate
3862 3864 * for those cases by returning a smaller path MTU to the ULP.
3863 3865 *
3864 3866 * In the case of CGTP then ip_output will add a fragment header.
3865 3867 * Make sure there is room for it by telling a smaller number
3866 3868 * to the transport.
3867 3869 *
3868 3870 * When IXAF_IPV6_ADDR_FRAGHDR we subtract the frag hdr here
3869 3871 * so the ULPs consistently see a iulp_pmtu and ip_get_pmtu()
3870 3872 * which is the size of the packets it can send.
3871 3873 */
3872 3874 if (!(ixa->ixa_flags & IXAF_IS_IPV4)) {
3873 3875 if ((dce->dce_flags & DCEF_TOO_SMALL_PMTU) ||
3874 3876 (ire->ire_flags & RTF_MULTIRT) ||
3875 3877 (ixa->ixa_flags & IXAF_MULTIRT_MULTICAST)) {
3876 3878 pmtu -= sizeof (ip6_frag_t);
3877 3879 ixa->ixa_flags |= IXAF_IPV6_ADD_FRAGHDR;
3878 3880 }
3879 3881 }
3880 3882
3881 3883 return (pmtu);
3882 3884 }
3883 3885
3884 3886 /*
3885 3887 * Carve "len" bytes out of an mblk chain, consuming any we empty, and duping
3886 3888 * the final piece where we don't. Return a pointer to the first mblk in the
3887 3889 * result, and update the pointer to the next mblk to chew on. If anything
3888 3890 * goes wrong (i.e., dupb fails), we waste everything in sight and return a
3889 3891 * NULL pointer.
3890 3892 */
3891 3893 mblk_t *
3892 3894 ip_carve_mp(mblk_t **mpp, ssize_t len)
3893 3895 {
3894 3896 mblk_t *mp0;
3895 3897 mblk_t *mp1;
3896 3898 mblk_t *mp2;
3897 3899
3898 3900 if (!len || !mpp || !(mp0 = *mpp))
3899 3901 return (NULL);
3900 3902 /* If we aren't going to consume the first mblk, we need a dup. */
3901 3903 if (mp0->b_wptr - mp0->b_rptr > len) {
3902 3904 mp1 = dupb(mp0);
3903 3905 if (mp1) {
3904 3906 /* Partition the data between the two mblks. */
3905 3907 mp1->b_wptr = mp1->b_rptr + len;
3906 3908 mp0->b_rptr = mp1->b_wptr;
3907 3909 /*
3908 3910 * after adjustments if mblk not consumed is now
3909 3911 * unaligned, try to align it. If this fails free
3910 3912 * all messages and let upper layer recover.
3911 3913 */
3912 3914 if (!OK_32PTR(mp0->b_rptr)) {
3913 3915 if (!pullupmsg(mp0, -1)) {
3914 3916 freemsg(mp0);
3915 3917 freemsg(mp1);
3916 3918 *mpp = NULL;
3917 3919 return (NULL);
3918 3920 }
3919 3921 }
3920 3922 }
3921 3923 return (mp1);
3922 3924 }
3923 3925 /* Eat through as many mblks as we need to get len bytes. */
3924 3926 len -= mp0->b_wptr - mp0->b_rptr;
3925 3927 for (mp2 = mp1 = mp0; (mp2 = mp2->b_cont) != 0 && len; mp1 = mp2) {
3926 3928 if (mp2->b_wptr - mp2->b_rptr > len) {
3927 3929 /*
3928 3930 * We won't consume the entire last mblk. Like
3929 3931 * above, dup and partition it.
3930 3932 */
3931 3933 mp1->b_cont = dupb(mp2);
3932 3934 mp1 = mp1->b_cont;
3933 3935 if (!mp1) {
3934 3936 /*
3935 3937 * Trouble. Rather than go to a lot of
3936 3938 * trouble to clean up, we free the messages.
3937 3939 * This won't be any worse than losing it on
3938 3940 * the wire.
3939 3941 */
3940 3942 freemsg(mp0);
3941 3943 freemsg(mp2);
3942 3944 *mpp = NULL;
3943 3945 return (NULL);
3944 3946 }
3945 3947 mp1->b_wptr = mp1->b_rptr + len;
3946 3948 mp2->b_rptr = mp1->b_wptr;
3947 3949 /*
3948 3950 * after adjustments if mblk not consumed is now
3949 3951 * unaligned, try to align it. If this fails free
3950 3952 * all messages and let upper layer recover.
3951 3953 */
3952 3954 if (!OK_32PTR(mp2->b_rptr)) {
3953 3955 if (!pullupmsg(mp2, -1)) {
3954 3956 freemsg(mp0);
3955 3957 freemsg(mp2);
3956 3958 *mpp = NULL;
3957 3959 return (NULL);
3958 3960 }
3959 3961 }
3960 3962 *mpp = mp2;
3961 3963 return (mp0);
3962 3964 }
3963 3965 /* Decrement len by the amount we just got. */
3964 3966 len -= mp2->b_wptr - mp2->b_rptr;
3965 3967 }
3966 3968 /*
3967 3969 * len should be reduced to zero now. If not our caller has
3968 3970 * screwed up.
3969 3971 */
3970 3972 if (len) {
3971 3973 /* Shouldn't happen! */
3972 3974 freemsg(mp0);
3973 3975 *mpp = NULL;
3974 3976 return (NULL);
3975 3977 }
3976 3978 /*
3977 3979 * We consumed up to exactly the end of an mblk. Detach the part
3978 3980 * we are returning from the rest of the chain.
3979 3981 */
3980 3982 mp1->b_cont = NULL;
3981 3983 *mpp = mp2;
3982 3984 return (mp0);
3983 3985 }
3984 3986
3985 3987 /* The ill stream is being unplumbed. Called from ip_close */
3986 3988 int
3987 3989 ip_modclose(ill_t *ill)
3988 3990 {
3989 3991 boolean_t success;
3990 3992 ipsq_t *ipsq;
3991 3993 ipif_t *ipif;
3992 3994 queue_t *q = ill->ill_rq;
3993 3995 ip_stack_t *ipst = ill->ill_ipst;
3994 3996 int i;
3995 3997 arl_ill_common_t *ai = ill->ill_common;
3996 3998
3997 3999 /*
3998 4000 * The punlink prior to this may have initiated a capability
3999 4001 * negotiation. But ipsq_enter will block until that finishes or
4000 4002 * times out.
4001 4003 */
4002 4004 success = ipsq_enter(ill, B_FALSE, NEW_OP);
4003 4005
4004 4006 /*
4005 4007 * Open/close/push/pop is guaranteed to be single threaded
4006 4008 * per stream by STREAMS. FS guarantees that all references
4007 4009 * from top are gone before close is called. So there can't
4008 4010 * be another close thread that has set CONDEMNED on this ill.
4009 4011 * and cause ipsq_enter to return failure.
4010 4012 */
4011 4013 ASSERT(success);
4012 4014 ipsq = ill->ill_phyint->phyint_ipsq;
4013 4015
4014 4016 /*
4015 4017 * Mark it condemned. No new reference will be made to this ill.
4016 4018 * Lookup functions will return an error. Threads that try to
4017 4019 * increment the refcnt must check for ILL_CAN_LOOKUP. This ensures
4018 4020 * that the refcnt will drop down to zero.
4019 4021 */
4020 4022 mutex_enter(&ill->ill_lock);
4021 4023 ill->ill_state_flags |= ILL_CONDEMNED;
4022 4024 for (ipif = ill->ill_ipif; ipif != NULL;
4023 4025 ipif = ipif->ipif_next) {
4024 4026 ipif->ipif_state_flags |= IPIF_CONDEMNED;
4025 4027 }
4026 4028 /*
4027 4029 * Wake up anybody waiting to enter the ipsq. ipsq_enter
4028 4030 * returns error if ILL_CONDEMNED is set
4029 4031 */
4030 4032 cv_broadcast(&ill->ill_cv);
4031 4033 mutex_exit(&ill->ill_lock);
4032 4034
4033 4035 /*
4034 4036 * Send all the deferred DLPI messages downstream which came in
4035 4037 * during the small window right before ipsq_enter(). We do this
4036 4038 * without waiting for the ACKs because all the ACKs for M_PROTO
4037 4039 * messages are ignored in ip_rput() when ILL_CONDEMNED is set.
4038 4040 */
4039 4041 ill_dlpi_send_deferred(ill);
4040 4042
4041 4043 /*
4042 4044 * Shut down fragmentation reassembly.
4043 4045 * ill_frag_timer won't start a timer again.
4044 4046 * Now cancel any existing timer
4045 4047 */
4046 4048 (void) untimeout(ill->ill_frag_timer_id);
4047 4049 (void) ill_frag_timeout(ill, 0);
4048 4050
4049 4051 /*
4050 4052 * Call ill_delete to bring down the ipifs, ilms and ill on
4051 4053 * this ill. Then wait for the refcnts to drop to zero.
4052 4054 * ill_is_freeable checks whether the ill is really quiescent.
4053 4055 * Then make sure that threads that are waiting to enter the
4054 4056 * ipsq have seen the error returned by ipsq_enter and have
4055 4057 * gone away. Then we call ill_delete_tail which does the
4056 4058 * DL_UNBIND_REQ with the driver and then qprocsoff.
4057 4059 */
4058 4060 ill_delete(ill);
4059 4061 mutex_enter(&ill->ill_lock);
4060 4062 while (!ill_is_freeable(ill))
4061 4063 cv_wait(&ill->ill_cv, &ill->ill_lock);
4062 4064
4063 4065 while (ill->ill_waiters)
4064 4066 cv_wait(&ill->ill_cv, &ill->ill_lock);
4065 4067
4066 4068 mutex_exit(&ill->ill_lock);
4067 4069
4068 4070 /*
4069 4071 * ill_delete_tail drops reference on ill_ipst, but we need to keep
4070 4072 * it held until the end of the function since the cleanup
4071 4073 * below needs to be able to use the ip_stack_t.
4072 4074 */
4073 4075 netstack_hold(ipst->ips_netstack);
4074 4076
4075 4077 /* qprocsoff is done via ill_delete_tail */
4076 4078 ill_delete_tail(ill);
4077 4079 /*
4078 4080 * synchronously wait for arp stream to unbind. After this, we
4079 4081 * cannot get any data packets up from the driver.
4080 4082 */
4081 4083 arp_unbind_complete(ill);
4082 4084 ASSERT(ill->ill_ipst == NULL);
4083 4085
4084 4086 /*
4085 4087 * Walk through all conns and qenable those that have queued data.
4086 4088 * Close synchronization needs this to
4087 4089 * be done to ensure that all upper layers blocked
4088 4090 * due to flow control to the closing device
4089 4091 * get unblocked.
4090 4092 */
4091 4093 ip1dbg(("ip_wsrv: walking\n"));
4092 4094 for (i = 0; i < TX_FANOUT_SIZE; i++) {
4093 4095 conn_walk_drain(ipst, &ipst->ips_idl_tx_list[i]);
4094 4096 }
4095 4097
4096 4098 /*
4097 4099 * ai can be null if this is an IPv6 ill, or if the IPv4
4098 4100 * stream is being torn down before ARP was plumbed (e.g.,
4099 4101 * /sbin/ifconfig plumbing a stream twice, and encountering
4100 4102 * an error
4101 4103 */
4102 4104 if (ai != NULL) {
4103 4105 ASSERT(!ill->ill_isv6);
4104 4106 mutex_enter(&ai->ai_lock);
4105 4107 ai->ai_ill = NULL;
4106 4108 if (ai->ai_arl == NULL) {
4107 4109 mutex_destroy(&ai->ai_lock);
4108 4110 kmem_free(ai, sizeof (*ai));
4109 4111 } else {
4110 4112 cv_signal(&ai->ai_ill_unplumb_done);
4111 4113 mutex_exit(&ai->ai_lock);
4112 4114 }
4113 4115 }
4114 4116
4115 4117 mutex_enter(&ipst->ips_ip_mi_lock);
4116 4118 mi_close_unlink(&ipst->ips_ip_g_head, (IDP)ill);
4117 4119 mutex_exit(&ipst->ips_ip_mi_lock);
4118 4120
4119 4121 /*
4120 4122 * credp could be null if the open didn't succeed and ip_modopen
4121 4123 * itself calls ip_close.
4122 4124 */
4123 4125 if (ill->ill_credp != NULL)
4124 4126 crfree(ill->ill_credp);
4125 4127
4126 4128 mutex_destroy(&ill->ill_saved_ire_lock);
4127 4129 mutex_destroy(&ill->ill_lock);
4128 4130 rw_destroy(&ill->ill_mcast_lock);
4129 4131 mutex_destroy(&ill->ill_mcast_serializer);
4130 4132 list_destroy(&ill->ill_nce);
4131 4133
4132 4134 /*
4133 4135 * Now we are done with the module close pieces that
4134 4136 * need the netstack_t.
4135 4137 */
4136 4138 netstack_rele(ipst->ips_netstack);
4137 4139
4138 4140 mi_close_free((IDP)ill);
4139 4141 q->q_ptr = WR(q)->q_ptr = NULL;
4140 4142
4141 4143 ipsq_exit(ipsq);
4142 4144
4143 4145 return (0);
4144 4146 }
4145 4147
4146 4148 /*
4147 4149 * This is called as part of close() for IP, UDP, ICMP, and RTS
4148 4150 * in order to quiesce the conn.
4149 4151 */
4150 4152 void
4151 4153 ip_quiesce_conn(conn_t *connp)
4152 4154 {
4153 4155 boolean_t drain_cleanup_reqd = B_FALSE;
4154 4156 boolean_t conn_ioctl_cleanup_reqd = B_FALSE;
4155 4157 boolean_t ilg_cleanup_reqd = B_FALSE;
4156 4158 ip_stack_t *ipst;
4157 4159
4158 4160 ASSERT(!IPCL_IS_TCP(connp));
4159 4161 ipst = connp->conn_netstack->netstack_ip;
4160 4162
4161 4163 /*
4162 4164 * Mark the conn as closing, and this conn must not be
4163 4165 * inserted in future into any list. Eg. conn_drain_insert(),
4164 4166 * won't insert this conn into the conn_drain_list.
4165 4167 *
4166 4168 * conn_idl, and conn_ilg cannot get set henceforth.
4167 4169 */
4168 4170 mutex_enter(&connp->conn_lock);
4169 4171 ASSERT(!(connp->conn_state_flags & CONN_QUIESCED));
4170 4172 connp->conn_state_flags |= CONN_CLOSING;
4171 4173 if (connp->conn_idl != NULL)
4172 4174 drain_cleanup_reqd = B_TRUE;
4173 4175 if (connp->conn_oper_pending_ill != NULL)
4174 4176 conn_ioctl_cleanup_reqd = B_TRUE;
4175 4177 if (connp->conn_dhcpinit_ill != NULL) {
4176 4178 ASSERT(connp->conn_dhcpinit_ill->ill_dhcpinit != 0);
4177 4179 atomic_dec_32(&connp->conn_dhcpinit_ill->ill_dhcpinit);
4178 4180 ill_set_inputfn(connp->conn_dhcpinit_ill);
4179 4181 connp->conn_dhcpinit_ill = NULL;
4180 4182 }
4181 4183 if (connp->conn_ilg != NULL)
4182 4184 ilg_cleanup_reqd = B_TRUE;
4183 4185 mutex_exit(&connp->conn_lock);
4184 4186
4185 4187 if (conn_ioctl_cleanup_reqd)
4186 4188 conn_ioctl_cleanup(connp);
4187 4189
4188 4190 if (is_system_labeled() && connp->conn_anon_port) {
4189 4191 (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
4190 4192 connp->conn_mlp_type, connp->conn_proto,
4191 4193 ntohs(connp->conn_lport), B_FALSE);
4192 4194 connp->conn_anon_port = 0;
4193 4195 }
4194 4196 connp->conn_mlp_type = mlptSingle;
4195 4197
4196 4198 /*
4197 4199 * Remove this conn from any fanout list it is on.
4198 4200 * and then wait for any threads currently operating
4199 4201 * on this endpoint to finish
4200 4202 */
4201 4203 ipcl_hash_remove(connp);
4202 4204
4203 4205 /*
4204 4206 * Remove this conn from the drain list, and do any other cleanup that
4205 4207 * may be required. (TCP conns are never flow controlled, and
4206 4208 * conn_idl will be NULL.)
4207 4209 */
4208 4210 if (drain_cleanup_reqd && connp->conn_idl != NULL) {
4209 4211 idl_t *idl = connp->conn_idl;
4210 4212
4211 4213 mutex_enter(&idl->idl_lock);
4212 4214 conn_drain(connp, B_TRUE);
4213 4215 mutex_exit(&idl->idl_lock);
4214 4216 }
4215 4217
4216 4218 if (connp == ipst->ips_ip_g_mrouter)
4217 4219 (void) ip_mrouter_done(ipst);
4218 4220
4219 4221 if (ilg_cleanup_reqd)
4220 4222 ilg_delete_all(connp);
4221 4223
4222 4224 /*
4223 4225 * Now conn refcnt can increase only thru CONN_INC_REF_LOCKED.
4224 4226 * callers from write side can't be there now because close
4225 4227 * is in progress. The only other caller is ipcl_walk
4226 4228 * which checks for the condemned flag.
4227 4229 */
4228 4230 mutex_enter(&connp->conn_lock);
4229 4231 connp->conn_state_flags |= CONN_CONDEMNED;
4230 4232 while (connp->conn_ref != 1)
4231 4233 cv_wait(&connp->conn_cv, &connp->conn_lock);
4232 4234 connp->conn_state_flags |= CONN_QUIESCED;
4233 4235 mutex_exit(&connp->conn_lock);
4234 4236 }
4235 4237
4236 4238 /* ARGSUSED */
4237 4239 int
4238 4240 ip_close(queue_t *q, int flags)
4239 4241 {
4240 4242 conn_t *connp;
4241 4243
4242 4244 /*
4243 4245 * Call the appropriate delete routine depending on whether this is
4244 4246 * a module or device.
4245 4247 */
4246 4248 if (WR(q)->q_next != NULL) {
4247 4249 /* This is a module close */
4248 4250 return (ip_modclose((ill_t *)q->q_ptr));
4249 4251 }
4250 4252
4251 4253 connp = q->q_ptr;
4252 4254 ip_quiesce_conn(connp);
4253 4255
4254 4256 qprocsoff(q);
4255 4257
4256 4258 /*
4257 4259 * Now we are truly single threaded on this stream, and can
4258 4260 * delete the things hanging off the connp, and finally the connp.
4259 4261 * We removed this connp from the fanout list, it cannot be
4260 4262 * accessed thru the fanouts, and we already waited for the
4261 4263 * conn_ref to drop to 0. We are already in close, so
4262 4264 * there cannot be any other thread from the top. qprocsoff
4263 4265 * has completed, and service has completed or won't run in
4264 4266 * future.
4265 4267 */
4266 4268 ASSERT(connp->conn_ref == 1);
4267 4269
4268 4270 inet_minor_free(connp->conn_minor_arena, connp->conn_dev);
4269 4271
4270 4272 connp->conn_ref--;
4271 4273 ipcl_conn_destroy(connp);
4272 4274
4273 4275 q->q_ptr = WR(q)->q_ptr = NULL;
4274 4276 return (0);
4275 4277 }
4276 4278
4277 4279 /*
4278 4280 * Wapper around putnext() so that ip_rts_request can merely use
4279 4281 * conn_recv.
4280 4282 */
4281 4283 /*ARGSUSED2*/
4282 4284 static void
4283 4285 ip_conn_input(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
4284 4286 {
4285 4287 conn_t *connp = (conn_t *)arg1;
4286 4288
4287 4289 putnext(connp->conn_rq, mp);
4288 4290 }
4289 4291
4290 4292 /* Dummy in case ICMP error delivery is attempted to a /dev/ip instance */
4291 4293 /* ARGSUSED */
4292 4294 static void
4293 4295 ip_conn_input_icmp(void *arg1, mblk_t *mp, void *arg2, ip_recv_attr_t *ira)
4294 4296 {
4295 4297 freemsg(mp);
4296 4298 }
4297 4299
4298 4300 /*
4299 4301 * Called when the module is about to be unloaded
4300 4302 */
4301 4303 void
4302 4304 ip_ddi_destroy(void)
4303 4305 {
4304 4306 /* This needs to be called before destroying any transports. */
4305 4307 mutex_enter(&cpu_lock);
4306 4308 unregister_cpu_setup_func(ip_tp_cpu_update, NULL);
4307 4309 mutex_exit(&cpu_lock);
4308 4310
4309 4311 tnet_fini();
4310 4312
4311 4313 icmp_ddi_g_destroy();
4312 4314 rts_ddi_g_destroy();
4313 4315 udp_ddi_g_destroy();
4314 4316 sctp_ddi_g_destroy();
4315 4317 tcp_ddi_g_destroy();
4316 4318 ilb_ddi_g_destroy();
4317 4319 dce_g_destroy();
4318 4320 ipsec_policy_g_destroy();
4319 4321 ipcl_g_destroy();
4320 4322 ip_net_g_destroy();
4321 4323 ip_ire_g_fini();
4322 4324 inet_minor_destroy(ip_minor_arena_sa);
4323 4325 #if defined(_LP64)
4324 4326 inet_minor_destroy(ip_minor_arena_la);
4325 4327 #endif
4326 4328
4327 4329 #ifdef DEBUG
4328 4330 list_destroy(&ip_thread_list);
4329 4331 rw_destroy(&ip_thread_rwlock);
4330 4332 tsd_destroy(&ip_thread_data);
4331 4333 #endif
4332 4334
4333 4335 netstack_unregister(NS_IP);
4334 4336 }
4335 4337
4336 4338 /*
4337 4339 * First step in cleanup.
4338 4340 */
4339 4341 /* ARGSUSED */
4340 4342 static void
4341 4343 ip_stack_shutdown(netstackid_t stackid, void *arg)
4342 4344 {
4343 4345 ip_stack_t *ipst = (ip_stack_t *)arg;
4344 4346 kt_did_t ktid;
4345 4347
4346 4348 #ifdef NS_DEBUG
4347 4349 printf("ip_stack_shutdown(%p, stack %d)\n", (void *)ipst, stackid);
4348 4350 #endif
4349 4351
4350 4352 /*
4351 4353 * Perform cleanup for special interfaces (loopback and IPMP).
4352 4354 */
4353 4355 ip_interface_cleanup(ipst);
4354 4356
4355 4357 /*
4356 4358 * The *_hook_shutdown()s start the process of notifying any
4357 4359 * consumers that things are going away.... nothing is destroyed.
4358 4360 */
4359 4361 ipv4_hook_shutdown(ipst);
4360 4362 ipv6_hook_shutdown(ipst);
4361 4363 arp_hook_shutdown(ipst);
4362 4364
4363 4365 mutex_enter(&ipst->ips_capab_taskq_lock);
4364 4366 ktid = ipst->ips_capab_taskq_thread->t_did;
4365 4367 ipst->ips_capab_taskq_quit = B_TRUE;
4366 4368 cv_signal(&ipst->ips_capab_taskq_cv);
4367 4369 mutex_exit(&ipst->ips_capab_taskq_lock);
4368 4370
4369 4371 /*
4370 4372 * In rare occurrences, particularly on virtual hardware where CPUs can
4371 4373 * be de-scheduled, the thread that we just signaled will not run until
4372 4374 * after we have gotten through parts of ip_stack_fini. If that happens
4373 4375 * then we'll try to grab the ips_capab_taskq_lock as part of returning
4374 4376 * from cv_wait which no longer exists.
4375 4377 */
4376 4378 thread_join(ktid);
4377 4379 }
4378 4380
4379 4381 /*
4380 4382 * Free the IP stack instance.
4381 4383 */
4382 4384 static void
4383 4385 ip_stack_fini(netstackid_t stackid, void *arg)
4384 4386 {
4385 4387 ip_stack_t *ipst = (ip_stack_t *)arg;
4386 4388 int ret;
4387 4389
4388 4390 #ifdef NS_DEBUG
4389 4391 printf("ip_stack_fini(%p, stack %d)\n", (void *)ipst, stackid);
4390 4392 #endif
4391 4393 /*
4392 4394 * At this point, all of the notifications that the events and
4393 4395 * protocols are going away have been run, meaning that we can
4394 4396 * now set about starting to clean things up.
4395 4397 */
4396 4398 ipobs_fini(ipst);
4397 4399 ipv4_hook_destroy(ipst);
4398 4400 ipv6_hook_destroy(ipst);
4399 4401 arp_hook_destroy(ipst);
4400 4402 ip_net_destroy(ipst);
4401 4403
4402 4404 ipmp_destroy(ipst);
4403 4405
4404 4406 ip_kstat_fini(stackid, ipst->ips_ip_mibkp);
4405 4407 ipst->ips_ip_mibkp = NULL;
4406 4408 icmp_kstat_fini(stackid, ipst->ips_icmp_mibkp);
4407 4409 ipst->ips_icmp_mibkp = NULL;
4408 4410 ip_kstat2_fini(stackid, ipst->ips_ip_kstat);
4409 4411 ipst->ips_ip_kstat = NULL;
4410 4412 bzero(&ipst->ips_ip_statistics, sizeof (ipst->ips_ip_statistics));
4411 4413 ip6_kstat_fini(stackid, ipst->ips_ip6_kstat);
4412 4414 ipst->ips_ip6_kstat = NULL;
4413 4415 bzero(&ipst->ips_ip6_statistics, sizeof (ipst->ips_ip6_statistics));
4414 4416
4415 4417 kmem_free(ipst->ips_propinfo_tbl,
4416 4418 ip_propinfo_count * sizeof (mod_prop_info_t));
4417 4419 ipst->ips_propinfo_tbl = NULL;
4418 4420
4419 4421 dce_stack_destroy(ipst);
4420 4422 ip_mrouter_stack_destroy(ipst);
4421 4423
4422 4424 ret = untimeout(ipst->ips_igmp_timeout_id);
4423 4425 if (ret == -1) {
4424 4426 ASSERT(ipst->ips_igmp_timeout_id == 0);
4425 4427 } else {
4426 4428 ASSERT(ipst->ips_igmp_timeout_id != 0);
4427 4429 ipst->ips_igmp_timeout_id = 0;
4428 4430 }
4429 4431 ret = untimeout(ipst->ips_igmp_slowtimeout_id);
4430 4432 if (ret == -1) {
4431 4433 ASSERT(ipst->ips_igmp_slowtimeout_id == 0);
4432 4434 } else {
4433 4435 ASSERT(ipst->ips_igmp_slowtimeout_id != 0);
4434 4436 ipst->ips_igmp_slowtimeout_id = 0;
4435 4437 }
4436 4438 ret = untimeout(ipst->ips_mld_timeout_id);
4437 4439 if (ret == -1) {
4438 4440 ASSERT(ipst->ips_mld_timeout_id == 0);
4439 4441 } else {
4440 4442 ASSERT(ipst->ips_mld_timeout_id != 0);
4441 4443 ipst->ips_mld_timeout_id = 0;
4442 4444 }
4443 4445 ret = untimeout(ipst->ips_mld_slowtimeout_id);
4444 4446 if (ret == -1) {
4445 4447 ASSERT(ipst->ips_mld_slowtimeout_id == 0);
4446 4448 } else {
4447 4449 ASSERT(ipst->ips_mld_slowtimeout_id != 0);
4448 4450 ipst->ips_mld_slowtimeout_id = 0;
4449 4451 }
4450 4452
4451 4453 ip_ire_fini(ipst);
4452 4454 ip6_asp_free(ipst);
4453 4455 conn_drain_fini(ipst);
4454 4456 ipcl_destroy(ipst);
4455 4457
4456 4458 mutex_destroy(&ipst->ips_ndp4->ndp_g_lock);
4457 4459 mutex_destroy(&ipst->ips_ndp6->ndp_g_lock);
4458 4460 kmem_free(ipst->ips_ndp4, sizeof (ndp_g_t));
4459 4461 ipst->ips_ndp4 = NULL;
4460 4462 kmem_free(ipst->ips_ndp6, sizeof (ndp_g_t));
4461 4463 ipst->ips_ndp6 = NULL;
4462 4464
4463 4465 if (ipst->ips_loopback_ksp != NULL) {
4464 4466 kstat_delete_netstack(ipst->ips_loopback_ksp, stackid);
4465 4467 ipst->ips_loopback_ksp = NULL;
4466 4468 }
4467 4469
4468 4470 mutex_destroy(&ipst->ips_capab_taskq_lock);
4469 4471 cv_destroy(&ipst->ips_capab_taskq_cv);
4470 4472
4471 4473 rw_destroy(&ipst->ips_srcid_lock);
4472 4474
4473 4475 mutex_destroy(&ipst->ips_ip_mi_lock);
4474 4476 rw_destroy(&ipst->ips_ill_g_usesrc_lock);
4475 4477
4476 4478 mutex_destroy(&ipst->ips_igmp_timer_lock);
4477 4479 mutex_destroy(&ipst->ips_mld_timer_lock);
4478 4480 mutex_destroy(&ipst->ips_igmp_slowtimeout_lock);
4479 4481 mutex_destroy(&ipst->ips_mld_slowtimeout_lock);
4480 4482 mutex_destroy(&ipst->ips_ip_addr_avail_lock);
4481 4483 rw_destroy(&ipst->ips_ill_g_lock);
4482 4484
4483 4485 kmem_free(ipst->ips_phyint_g_list, sizeof (phyint_list_t));
4484 4486 ipst->ips_phyint_g_list = NULL;
4485 4487 kmem_free(ipst->ips_ill_g_heads, sizeof (ill_g_head_t) * MAX_G_HEADS);
4486 4488 ipst->ips_ill_g_heads = NULL;
4487 4489
4488 4490 ldi_ident_release(ipst->ips_ldi_ident);
4489 4491 kmem_free(ipst, sizeof (*ipst));
4490 4492 }
4491 4493
4492 4494 /*
4493 4495 * This function is called from the TSD destructor, and is used to debug
4494 4496 * reference count issues in IP. See block comment in <inet/ip_if.h> for
4495 4497 * details.
4496 4498 */
4497 4499 static void
4498 4500 ip_thread_exit(void *phash)
4499 4501 {
4500 4502 th_hash_t *thh = phash;
4501 4503
4502 4504 rw_enter(&ip_thread_rwlock, RW_WRITER);
4503 4505 list_remove(&ip_thread_list, thh);
4504 4506 rw_exit(&ip_thread_rwlock);
4505 4507 mod_hash_destroy_hash(thh->thh_hash);
4506 4508 kmem_free(thh, sizeof (*thh));
4507 4509 }
4508 4510
4509 4511 /*
4510 4512 * Called when the IP kernel module is loaded into the kernel
4511 4513 */
4512 4514 void
4513 4515 ip_ddi_init(void)
4514 4516 {
4515 4517 ip_squeue_flag = ip_squeue_switch(ip_squeue_enter);
4516 4518
4517 4519 /*
4518 4520 * For IP and TCP the minor numbers should start from 2 since we have 4
4519 4521 * initial devices: ip, ip6, tcp, tcp6.
4520 4522 */
4521 4523 /*
4522 4524 * If this is a 64-bit kernel, then create two separate arenas -
4523 4525 * one for TLIs in the range of INET_MIN_DEV+2 through 2^^18-1, and the
4524 4526 * other for socket apps in the range 2^^18 through 2^^32-1.
4525 4527 */
4526 4528 ip_minor_arena_la = NULL;
4527 4529 ip_minor_arena_sa = NULL;
4528 4530 #if defined(_LP64)
4529 4531 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa",
4530 4532 INET_MIN_DEV + 2, MAXMIN32, KM_SLEEP)) == NULL) {
4531 4533 cmn_err(CE_PANIC,
4532 4534 "ip_ddi_init: ip_minor_arena_sa creation failed\n");
4533 4535 }
4534 4536 if ((ip_minor_arena_la = inet_minor_create("ip_minor_arena_la",
4535 4537 MAXMIN32 + 1, MAXMIN64, KM_SLEEP)) == NULL) {
4536 4538 cmn_err(CE_PANIC,
4537 4539 "ip_ddi_init: ip_minor_arena_la creation failed\n");
4538 4540 }
4539 4541 #else
4540 4542 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa",
4541 4543 INET_MIN_DEV + 2, MAXMIN, KM_SLEEP)) == NULL) {
4542 4544 cmn_err(CE_PANIC,
4543 4545 "ip_ddi_init: ip_minor_arena_sa creation failed\n");
4544 4546 }
4545 4547 #endif
4546 4548 ip_poll_normal_ticks = MSEC_TO_TICK_ROUNDUP(ip_poll_normal_ms);
4547 4549
4548 4550 ipcl_g_init();
4549 4551 ip_ire_g_init();
4550 4552 ip_net_g_init();
4551 4553
4552 4554 #ifdef DEBUG
4553 4555 tsd_create(&ip_thread_data, ip_thread_exit);
4554 4556 rw_init(&ip_thread_rwlock, NULL, RW_DEFAULT, NULL);
4555 4557 list_create(&ip_thread_list, sizeof (th_hash_t),
4556 4558 offsetof(th_hash_t, thh_link));
4557 4559 #endif
4558 4560 ipsec_policy_g_init();
4559 4561 tcp_ddi_g_init();
4560 4562 sctp_ddi_g_init();
4561 4563 dce_g_init();
4562 4564
4563 4565 /*
4564 4566 * We want to be informed each time a stack is created or
4565 4567 * destroyed in the kernel, so we can maintain the
4566 4568 * set of udp_stack_t's.
4567 4569 */
4568 4570 netstack_register(NS_IP, ip_stack_init, ip_stack_shutdown,
4569 4571 ip_stack_fini);
4570 4572
4571 4573 tnet_init();
4572 4574
4573 4575 udp_ddi_g_init();
4574 4576 rts_ddi_g_init();
4575 4577 icmp_ddi_g_init();
4576 4578 ilb_ddi_g_init();
4577 4579
4578 4580 /* This needs to be called after all transports are initialized. */
4579 4581 mutex_enter(&cpu_lock);
4580 4582 register_cpu_setup_func(ip_tp_cpu_update, NULL);
4581 4583 mutex_exit(&cpu_lock);
4582 4584 }
4583 4585
4584 4586 /*
4585 4587 * Initialize the IP stack instance.
4586 4588 */
4587 4589 static void *
4588 4590 ip_stack_init(netstackid_t stackid, netstack_t *ns)
4589 4591 {
4590 4592 ip_stack_t *ipst;
4591 4593 size_t arrsz;
4592 4594 major_t major;
4593 4595
4594 4596 #ifdef NS_DEBUG
4595 4597 printf("ip_stack_init(stack %d)\n", stackid);
4596 4598 #endif
4597 4599
4598 4600 ipst = (ip_stack_t *)kmem_zalloc(sizeof (*ipst), KM_SLEEP);
4599 4601 ipst->ips_netstack = ns;
4600 4602
4601 4603 ipst->ips_ill_g_heads = kmem_zalloc(sizeof (ill_g_head_t) * MAX_G_HEADS,
4602 4604 KM_SLEEP);
4603 4605 ipst->ips_phyint_g_list = kmem_zalloc(sizeof (phyint_list_t),
4604 4606 KM_SLEEP);
4605 4607 ipst->ips_ndp4 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP);
4606 4608 ipst->ips_ndp6 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP);
4607 4609 mutex_init(&ipst->ips_ndp4->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL);
4608 4610 mutex_init(&ipst->ips_ndp6->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL);
4609 4611
4610 4612 mutex_init(&ipst->ips_igmp_timer_lock, NULL, MUTEX_DEFAULT, NULL);
4611 4613 ipst->ips_igmp_deferred_next = INFINITY;
4612 4614 mutex_init(&ipst->ips_mld_timer_lock, NULL, MUTEX_DEFAULT, NULL);
4613 4615 ipst->ips_mld_deferred_next = INFINITY;
4614 4616 mutex_init(&ipst->ips_igmp_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL);
4615 4617 mutex_init(&ipst->ips_mld_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL);
4616 4618 mutex_init(&ipst->ips_ip_mi_lock, NULL, MUTEX_DEFAULT, NULL);
4617 4619 mutex_init(&ipst->ips_ip_addr_avail_lock, NULL, MUTEX_DEFAULT, NULL);
4618 4620 rw_init(&ipst->ips_ill_g_lock, NULL, RW_DEFAULT, NULL);
4619 4621 rw_init(&ipst->ips_ill_g_usesrc_lock, NULL, RW_DEFAULT, NULL);
4620 4622
4621 4623 ipcl_init(ipst);
4622 4624 ip_ire_init(ipst);
4623 4625 ip6_asp_init(ipst);
4624 4626 ipif_init(ipst);
4625 4627 conn_drain_init(ipst);
4626 4628 ip_mrouter_stack_init(ipst);
4627 4629 dce_stack_init(ipst);
4628 4630
4629 4631 ipst->ips_ip_multirt_log_interval = 1000;
4630 4632
4631 4633 ipst->ips_ill_index = 1;
4632 4634
4633 4635 ipst->ips_saved_ip_forwarding = -1;
4634 4636 ipst->ips_reg_vif_num = ALL_VIFS; /* Index to Register vif */
4635 4637
4636 4638 arrsz = ip_propinfo_count * sizeof (mod_prop_info_t);
4637 4639 ipst->ips_propinfo_tbl = (mod_prop_info_t *)kmem_alloc(arrsz, KM_SLEEP);
4638 4640 bcopy(ip_propinfo_tbl, ipst->ips_propinfo_tbl, arrsz);
4639 4641
4640 4642 ipst->ips_ip_mibkp = ip_kstat_init(stackid, ipst);
4641 4643 ipst->ips_icmp_mibkp = icmp_kstat_init(stackid);
4642 4644 ipst->ips_ip_kstat = ip_kstat2_init(stackid, &ipst->ips_ip_statistics);
4643 4645 ipst->ips_ip6_kstat =
4644 4646 ip6_kstat_init(stackid, &ipst->ips_ip6_statistics);
4645 4647
4646 4648 ipst->ips_ip_src_id = 1;
4647 4649 rw_init(&ipst->ips_srcid_lock, NULL, RW_DEFAULT, NULL);
4648 4650
4649 4651 ipst->ips_src_generation = SRC_GENERATION_INITIAL;
4650 4652
4651 4653 ip_net_init(ipst, ns);
4652 4654 ipv4_hook_init(ipst);
4653 4655 ipv6_hook_init(ipst);
4654 4656 arp_hook_init(ipst);
4655 4657 ipmp_init(ipst);
4656 4658 ipobs_init(ipst);
4657 4659
4658 4660 /*
4659 4661 * Create the taskq dispatcher thread and initialize related stuff.
4660 4662 */
4661 4663 mutex_init(&ipst->ips_capab_taskq_lock, NULL, MUTEX_DEFAULT, NULL);
4662 4664 cv_init(&ipst->ips_capab_taskq_cv, NULL, CV_DEFAULT, NULL);
4663 4665 ipst->ips_capab_taskq_thread = thread_create(NULL, 0,
4664 4666 ill_taskq_dispatch, ipst, 0, &p0, TS_RUN, minclsyspri);
4665 4667
4666 4668 major = mod_name_to_major(INET_NAME);
4667 4669 (void) ldi_ident_from_major(major, &ipst->ips_ldi_ident);
4668 4670 return (ipst);
4669 4671 }
4670 4672
4671 4673 /*
4672 4674 * Allocate and initialize a DLPI template of the specified length. (May be
4673 4675 * called as writer.)
4674 4676 */
4675 4677 mblk_t *
4676 4678 ip_dlpi_alloc(size_t len, t_uscalar_t prim)
4677 4679 {
4678 4680 mblk_t *mp;
4679 4681
4680 4682 mp = allocb(len, BPRI_MED);
4681 4683 if (!mp)
4682 4684 return (NULL);
4683 4685
4684 4686 /*
4685 4687 * DLPIv2 says that DL_INFO_REQ and DL_TOKEN_REQ (the latter
4686 4688 * of which we don't seem to use) are sent with M_PCPROTO, and
4687 4689 * that other DLPI are M_PROTO.
4688 4690 */
4689 4691 if (prim == DL_INFO_REQ) {
4690 4692 mp->b_datap->db_type = M_PCPROTO;
4691 4693 } else {
4692 4694 mp->b_datap->db_type = M_PROTO;
4693 4695 }
4694 4696
4695 4697 mp->b_wptr = mp->b_rptr + len;
4696 4698 bzero(mp->b_rptr, len);
4697 4699 ((dl_unitdata_req_t *)mp->b_rptr)->dl_primitive = prim;
4698 4700 return (mp);
4699 4701 }
4700 4702
4701 4703 /*
4702 4704 * Allocate and initialize a DLPI notification. (May be called as writer.)
4703 4705 */
4704 4706 mblk_t *
4705 4707 ip_dlnotify_alloc(uint_t notification, uint_t data)
4706 4708 {
4707 4709 dl_notify_ind_t *notifyp;
4708 4710 mblk_t *mp;
4709 4711
4710 4712 if ((mp = ip_dlpi_alloc(DL_NOTIFY_IND_SIZE, DL_NOTIFY_IND)) == NULL)
4711 4713 return (NULL);
4712 4714
4713 4715 notifyp = (dl_notify_ind_t *)mp->b_rptr;
4714 4716 notifyp->dl_notification = notification;
4715 4717 notifyp->dl_data = data;
4716 4718 return (mp);
4717 4719 }
4718 4720
4719 4721 mblk_t *
4720 4722 ip_dlnotify_alloc2(uint_t notification, uint_t data1, uint_t data2)
4721 4723 {
4722 4724 dl_notify_ind_t *notifyp;
4723 4725 mblk_t *mp;
4724 4726
4725 4727 if ((mp = ip_dlpi_alloc(DL_NOTIFY_IND_SIZE, DL_NOTIFY_IND)) == NULL)
4726 4728 return (NULL);
4727 4729
4728 4730 notifyp = (dl_notify_ind_t *)mp->b_rptr;
4729 4731 notifyp->dl_notification = notification;
4730 4732 notifyp->dl_data1 = data1;
4731 4733 notifyp->dl_data2 = data2;
4732 4734 return (mp);
4733 4735 }
4734 4736
4735 4737 /*
4736 4738 * Debug formatting routine. Returns a character string representation of the
4737 4739 * addr in buf, of the form xxx.xxx.xxx.xxx. This routine takes the address
4738 4740 * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer.
4739 4741 *
4740 4742 * Once the ndd table-printing interfaces are removed, this can be changed to
4741 4743 * standard dotted-decimal form.
4742 4744 */
4743 4745 char *
4744 4746 ip_dot_addr(ipaddr_t addr, char *buf)
4745 4747 {
4746 4748 uint8_t *ap = (uint8_t *)&addr;
4747 4749
4748 4750 (void) mi_sprintf(buf, "%03d.%03d.%03d.%03d",
4749 4751 ap[0] & 0xFF, ap[1] & 0xFF, ap[2] & 0xFF, ap[3] & 0xFF);
4750 4752 return (buf);
4751 4753 }
4752 4754
4753 4755 /*
4754 4756 * Write the given MAC address as a printable string in the usual colon-
4755 4757 * separated format.
4756 4758 */
4757 4759 const char *
4758 4760 mac_colon_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen)
4759 4761 {
4760 4762 char *bp;
4761 4763
4762 4764 if (alen == 0 || buflen < 4)
4763 4765 return ("?");
4764 4766 bp = buf;
4765 4767 for (;;) {
4766 4768 /*
4767 4769 * If there are more MAC address bytes available, but we won't
4768 4770 * have any room to print them, then add "..." to the string
4769 4771 * instead. See below for the 'magic number' explanation.
4770 4772 */
4771 4773 if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) {
4772 4774 (void) strcpy(bp, "...");
4773 4775 break;
4774 4776 }
4775 4777 (void) sprintf(bp, "%02x", *addr++);
4776 4778 bp += 2;
4777 4779 if (--alen == 0)
4778 4780 break;
4779 4781 *bp++ = ':';
4780 4782 buflen -= 3;
4781 4783 /*
4782 4784 * At this point, based on the first 'if' statement above,
4783 4785 * either alen == 1 and buflen >= 3, or alen > 1 and
4784 4786 * buflen >= 4. The first case leaves room for the final "xx"
4785 4787 * number and trailing NUL byte. The second leaves room for at
4786 4788 * least "...". Thus the apparently 'magic' numbers chosen for
4787 4789 * that statement.
4788 4790 */
4789 4791 }
4790 4792 return (buf);
4791 4793 }
4792 4794
4793 4795 /*
4794 4796 * Called when it is conceptually a ULP that would sent the packet
4795 4797 * e.g., port unreachable and protocol unreachable. Check that the packet
4796 4798 * would have passed the IPsec global policy before sending the error.
4797 4799 *
4798 4800 * Send an ICMP error after patching up the packet appropriately.
4799 4801 * Uses ip_drop_input and bumps the appropriate MIB.
4800 4802 */
4801 4803 void
4802 4804 ip_fanout_send_icmp_v4(mblk_t *mp, uint_t icmp_type, uint_t icmp_code,
4803 4805 ip_recv_attr_t *ira)
4804 4806 {
4805 4807 ipha_t *ipha;
4806 4808 boolean_t secure;
4807 4809 ill_t *ill = ira->ira_ill;
4808 4810 ip_stack_t *ipst = ill->ill_ipst;
4809 4811 netstack_t *ns = ipst->ips_netstack;
4810 4812 ipsec_stack_t *ipss = ns->netstack_ipsec;
4811 4813
4812 4814 secure = ira->ira_flags & IRAF_IPSEC_SECURE;
4813 4815
4814 4816 /*
4815 4817 * We are generating an icmp error for some inbound packet.
4816 4818 * Called from all ip_fanout_(udp, tcp, proto) functions.
4817 4819 * Before we generate an error, check with global policy
4818 4820 * to see whether this is allowed to enter the system. As
4819 4821 * there is no "conn", we are checking with global policy.
4820 4822 */
4821 4823 ipha = (ipha_t *)mp->b_rptr;
4822 4824 if (secure || ipss->ipsec_inbound_v4_policy_present) {
4823 4825 mp = ipsec_check_global_policy(mp, NULL, ipha, NULL, ira, ns);
4824 4826 if (mp == NULL)
4825 4827 return;
4826 4828 }
4827 4829
4828 4830 /* We never send errors for protocols that we do implement */
4829 4831 if (ira->ira_protocol == IPPROTO_ICMP ||
4830 4832 ira->ira_protocol == IPPROTO_IGMP) {
4831 4833 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
4832 4834 ip_drop_input("ip_fanout_send_icmp_v4", mp, ill);
4833 4835 freemsg(mp);
4834 4836 return;
4835 4837 }
4836 4838 /*
4837 4839 * Have to correct checksum since
4838 4840 * the packet might have been
4839 4841 * fragmented and the reassembly code in ip_rput
4840 4842 * does not restore the IP checksum.
4841 4843 */
4842 4844 ipha->ipha_hdr_checksum = 0;
4843 4845 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
4844 4846
4845 4847 switch (icmp_type) {
4846 4848 case ICMP_DEST_UNREACHABLE:
4847 4849 switch (icmp_code) {
4848 4850 case ICMP_PROTOCOL_UNREACHABLE:
4849 4851 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInUnknownProtos);
4850 4852 ip_drop_input("ipIfStatsInUnknownProtos", mp, ill);
4851 4853 break;
4852 4854 case ICMP_PORT_UNREACHABLE:
4853 4855 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts);
4854 4856 ip_drop_input("ipIfStatsNoPorts", mp, ill);
4855 4857 break;
4856 4858 }
4857 4859
4858 4860 icmp_unreachable(mp, icmp_code, ira);
4859 4861 break;
4860 4862 default:
4861 4863 #ifdef DEBUG
4862 4864 panic("ip_fanout_send_icmp_v4: wrong type");
4863 4865 /*NOTREACHED*/
4864 4866 #else
4865 4867 freemsg(mp);
4866 4868 break;
4867 4869 #endif
4868 4870 }
4869 4871 }
4870 4872
4871 4873 /*
4872 4874 * Used to send an ICMP error message when a packet is received for
4873 4875 * a protocol that is not supported. The mblk passed as argument
4874 4876 * is consumed by this function.
4875 4877 */
4876 4878 void
4877 4879 ip_proto_not_sup(mblk_t *mp, ip_recv_attr_t *ira)
4878 4880 {
4879 4881 ipha_t *ipha;
4880 4882
4881 4883 ipha = (ipha_t *)mp->b_rptr;
4882 4884 if (ira->ira_flags & IRAF_IS_IPV4) {
4883 4885 ASSERT(IPH_HDR_VERSION(ipha) == IP_VERSION);
4884 4886 ip_fanout_send_icmp_v4(mp, ICMP_DEST_UNREACHABLE,
4885 4887 ICMP_PROTOCOL_UNREACHABLE, ira);
4886 4888 } else {
4887 4889 ASSERT(IPH_HDR_VERSION(ipha) == IPV6_VERSION);
4888 4890 ip_fanout_send_icmp_v6(mp, ICMP6_PARAM_PROB,
4889 4891 ICMP6_PARAMPROB_NEXTHEADER, ira);
4890 4892 }
4891 4893 }
4892 4894
4893 4895 /*
4894 4896 * Deliver a rawip packet to the given conn, possibly applying ipsec policy.
4895 4897 * Handles IPv4 and IPv6.
4896 4898 * We are responsible for disposing of mp, such as by freemsg() or putnext()
4897 4899 * Caller is responsible for dropping references to the conn.
4898 4900 */
4899 4901 void
4900 4902 ip_fanout_proto_conn(conn_t *connp, mblk_t *mp, ipha_t *ipha, ip6_t *ip6h,
4901 4903 ip_recv_attr_t *ira)
4902 4904 {
4903 4905 ill_t *ill = ira->ira_ill;
4904 4906 ip_stack_t *ipst = ill->ill_ipst;
4905 4907 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
4906 4908 boolean_t secure;
4907 4909 uint_t protocol = ira->ira_protocol;
4908 4910 iaflags_t iraflags = ira->ira_flags;
4909 4911 queue_t *rq;
4910 4912
4911 4913 secure = iraflags & IRAF_IPSEC_SECURE;
4912 4914
4913 4915 rq = connp->conn_rq;
4914 4916 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld : !canputnext(rq)) {
4915 4917 switch (protocol) {
4916 4918 case IPPROTO_ICMPV6:
4917 4919 BUMP_MIB(ill->ill_icmp6_mib, ipv6IfIcmpInOverflows);
4918 4920 break;
4919 4921 case IPPROTO_ICMP:
4920 4922 BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows);
4921 4923 break;
4922 4924 default:
4923 4925 BUMP_MIB(ill->ill_ip_mib, rawipIfStatsInOverflows);
4924 4926 break;
4925 4927 }
4926 4928 freemsg(mp);
4927 4929 return;
4928 4930 }
4929 4931
4930 4932 ASSERT(!(IPCL_IS_IPTUN(connp)));
4931 4933
4932 4934 if (((iraflags & IRAF_IS_IPV4) ?
4933 4935 CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
4934 4936 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
4935 4937 secure) {
4936 4938 mp = ipsec_check_inbound_policy(mp, connp, ipha,
4937 4939 ip6h, ira);
4938 4940 if (mp == NULL) {
4939 4941 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
4940 4942 /* Note that mp is NULL */
4941 4943 ip_drop_input("ipIfStatsInDiscards", mp, ill);
4942 4944 return;
4943 4945 }
4944 4946 }
4945 4947
4946 4948 if (iraflags & IRAF_ICMP_ERROR) {
4947 4949 (connp->conn_recvicmp)(connp, mp, NULL, ira);
4948 4950 } else {
4949 4951 ill_t *rill = ira->ira_rill;
4950 4952
4951 4953 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
4952 4954 ira->ira_ill = ira->ira_rill = NULL;
4953 4955 /* Send it upstream */
4954 4956 (connp->conn_recv)(connp, mp, NULL, ira);
4955 4957 ira->ira_ill = ill;
4956 4958 ira->ira_rill = rill;
4957 4959 }
4958 4960 }
4959 4961
4960 4962 /*
4961 4963 * Handle protocols with which IP is less intimate. There
4962 4964 * can be more than one stream bound to a particular
4963 4965 * protocol. When this is the case, normally each one gets a copy
4964 4966 * of any incoming packets.
4965 4967 *
4966 4968 * IPsec NOTE :
4967 4969 *
4968 4970 * Don't allow a secure packet going up a non-secure connection.
4969 4971 * We don't allow this because
4970 4972 *
4971 4973 * 1) Reply might go out in clear which will be dropped at
4972 4974 * the sending side.
4973 4975 * 2) If the reply goes out in clear it will give the
4974 4976 * adversary enough information for getting the key in
4975 4977 * most of the cases.
4976 4978 *
4977 4979 * Moreover getting a secure packet when we expect clear
4978 4980 * implies that SA's were added without checking for
4979 4981 * policy on both ends. This should not happen once ISAKMP
4980 4982 * is used to negotiate SAs as SAs will be added only after
4981 4983 * verifying the policy.
4982 4984 *
4983 4985 * Zones notes:
4984 4986 * Earlier in ip_input on a system with multiple shared-IP zones we
4985 4987 * duplicate the multicast and broadcast packets and send them up
4986 4988 * with each explicit zoneid that exists on that ill.
4987 4989 * This means that here we can match the zoneid with SO_ALLZONES being special.
4988 4990 */
4989 4991 void
4990 4992 ip_fanout_proto_v4(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
4991 4993 {
4992 4994 mblk_t *mp1;
4993 4995 ipaddr_t laddr;
4994 4996 conn_t *connp, *first_connp, *next_connp;
4995 4997 connf_t *connfp;
4996 4998 ill_t *ill = ira->ira_ill;
4997 4999 ip_stack_t *ipst = ill->ill_ipst;
4998 5000
4999 5001 laddr = ipha->ipha_dst;
5000 5002
5001 5003 connfp = &ipst->ips_ipcl_proto_fanout_v4[ira->ira_protocol];
5002 5004 mutex_enter(&connfp->connf_lock);
5003 5005 connp = connfp->connf_head;
5004 5006 for (connp = connfp->connf_head; connp != NULL;
5005 5007 connp = connp->conn_next) {
5006 5008 /* Note: IPCL_PROTO_MATCH includes conn_wantpacket */
5007 5009 if (IPCL_PROTO_MATCH(connp, ira, ipha) &&
5008 5010 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5009 5011 tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp))) {
5010 5012 break;
5011 5013 }
5012 5014 }
5013 5015
5014 5016 if (connp == NULL) {
5015 5017 /*
5016 5018 * No one bound to these addresses. Is
5017 5019 * there a client that wants all
5018 5020 * unclaimed datagrams?
5019 5021 */
5020 5022 mutex_exit(&connfp->connf_lock);
5021 5023 ip_fanout_send_icmp_v4(mp, ICMP_DEST_UNREACHABLE,
5022 5024 ICMP_PROTOCOL_UNREACHABLE, ira);
5023 5025 return;
5024 5026 }
5025 5027
5026 5028 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_rq != NULL);
5027 5029
5028 5030 CONN_INC_REF(connp);
5029 5031 first_connp = connp;
5030 5032 connp = connp->conn_next;
5031 5033
5032 5034 for (;;) {
5033 5035 while (connp != NULL) {
5034 5036 /* Note: IPCL_PROTO_MATCH includes conn_wantpacket */
5035 5037 if (IPCL_PROTO_MATCH(connp, ira, ipha) &&
5036 5038 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5037 5039 tsol_receive_local(mp, &laddr, IPV4_VERSION,
5038 5040 ira, connp)))
5039 5041 break;
5040 5042 connp = connp->conn_next;
5041 5043 }
5042 5044
5043 5045 if (connp == NULL) {
5044 5046 /* No more interested clients */
5045 5047 connp = first_connp;
5046 5048 break;
5047 5049 }
5048 5050 if (((mp1 = dupmsg(mp)) == NULL) &&
5049 5051 ((mp1 = copymsg(mp)) == NULL)) {
5050 5052 /* Memory allocation failed */
5051 5053 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5052 5054 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5053 5055 connp = first_connp;
5054 5056 break;
5055 5057 }
5056 5058
5057 5059 CONN_INC_REF(connp);
5058 5060 mutex_exit(&connfp->connf_lock);
5059 5061
5060 5062 ip_fanout_proto_conn(connp, mp1, (ipha_t *)mp1->b_rptr, NULL,
5061 5063 ira);
5062 5064
5063 5065 mutex_enter(&connfp->connf_lock);
5064 5066 /* Follow the next pointer before releasing the conn. */
5065 5067 next_connp = connp->conn_next;
5066 5068 CONN_DEC_REF(connp);
5067 5069 connp = next_connp;
5068 5070 }
5069 5071
5070 5072 /* Last one. Send it upstream. */
5071 5073 mutex_exit(&connfp->connf_lock);
5072 5074
5073 5075 ip_fanout_proto_conn(connp, mp, ipha, NULL, ira);
5074 5076
5075 5077 CONN_DEC_REF(connp);
5076 5078 }
5077 5079
5078 5080 /*
5079 5081 * If we have a IPsec NAT-Traversal packet, strip the zero-SPI or
5080 5082 * pass it along to ESP if the SPI is non-zero. Returns the mblk if the mblk
5081 5083 * is not consumed.
5082 5084 *
5083 5085 * One of three things can happen, all of which affect the passed-in mblk:
5084 5086 *
5085 5087 * 1.) The packet is stock UDP and gets its zero-SPI stripped. Return mblk..
5086 5088 *
5087 5089 * 2.) The packet is ESP-in-UDP, gets transformed into an equivalent
5088 5090 * ESP packet, and is passed along to ESP for consumption. Return NULL.
5089 5091 *
5090 5092 * 3.) The packet is an ESP-in-UDP Keepalive. Drop it and return NULL.
5091 5093 */
5092 5094 mblk_t *
5093 5095 zero_spi_check(mblk_t *mp, ip_recv_attr_t *ira)
5094 5096 {
5095 5097 int shift, plen, iph_len;
5096 5098 ipha_t *ipha;
5097 5099 udpha_t *udpha;
5098 5100 uint32_t *spi;
5099 5101 uint32_t esp_ports;
5100 5102 uint8_t *orptr;
5101 5103 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
5102 5104 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
5103 5105
5104 5106 ipha = (ipha_t *)mp->b_rptr;
5105 5107 iph_len = ira->ira_ip_hdr_length;
5106 5108 plen = ira->ira_pktlen;
5107 5109
5108 5110 if (plen - iph_len - sizeof (udpha_t) < sizeof (uint32_t)) {
5109 5111 /*
5110 5112 * Most likely a keepalive for the benefit of an intervening
5111 5113 * NAT. These aren't for us, per se, so drop it.
5112 5114 *
5113 5115 * RFC 3947/8 doesn't say for sure what to do for 2-3
5114 5116 * byte packets (keepalives are 1-byte), but we'll drop them
5115 5117 * also.
5116 5118 */
5117 5119 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
5118 5120 DROPPER(ipss, ipds_esp_nat_t_ka), &ipss->ipsec_dropper);
5119 5121 return (NULL);
5120 5122 }
5121 5123
5122 5124 if (MBLKL(mp) < iph_len + sizeof (udpha_t) + sizeof (*spi)) {
5123 5125 /* might as well pull it all up - it might be ESP. */
5124 5126 if (!pullupmsg(mp, -1)) {
5125 5127 ip_drop_packet(mp, B_TRUE, ira->ira_ill,
5126 5128 DROPPER(ipss, ipds_esp_nomem),
5127 5129 &ipss->ipsec_dropper);
5128 5130 return (NULL);
5129 5131 }
5130 5132
5131 5133 ipha = (ipha_t *)mp->b_rptr;
5132 5134 }
5133 5135 spi = (uint32_t *)(mp->b_rptr + iph_len + sizeof (udpha_t));
5134 5136 if (*spi == 0) {
5135 5137 /* UDP packet - remove 0-spi. */
5136 5138 shift = sizeof (uint32_t);
5137 5139 } else {
5138 5140 /* ESP-in-UDP packet - reduce to ESP. */
5139 5141 ipha->ipha_protocol = IPPROTO_ESP;
5140 5142 shift = sizeof (udpha_t);
5141 5143 }
5142 5144
5143 5145 /* Fix IP header */
5144 5146 ira->ira_pktlen = (plen - shift);
5145 5147 ipha->ipha_length = htons(ira->ira_pktlen);
5146 5148 ipha->ipha_hdr_checksum = 0;
5147 5149
5148 5150 orptr = mp->b_rptr;
5149 5151 mp->b_rptr += shift;
5150 5152
5151 5153 udpha = (udpha_t *)(orptr + iph_len);
5152 5154 if (*spi == 0) {
5153 5155 ASSERT((uint8_t *)ipha == orptr);
5154 5156 udpha->uha_length = htons(plen - shift - iph_len);
5155 5157 iph_len += sizeof (udpha_t); /* For the call to ovbcopy(). */
5156 5158 esp_ports = 0;
5157 5159 } else {
5158 5160 esp_ports = *((uint32_t *)udpha);
5159 5161 ASSERT(esp_ports != 0);
5160 5162 }
5161 5163 ovbcopy(orptr, orptr + shift, iph_len);
5162 5164 if (esp_ports != 0) /* Punt up for ESP processing. */ {
5163 5165 ipha = (ipha_t *)(orptr + shift);
5164 5166
5165 5167 ira->ira_flags |= IRAF_ESP_UDP_PORTS;
5166 5168 ira->ira_esp_udp_ports = esp_ports;
5167 5169 ip_fanout_v4(mp, ipha, ira);
5168 5170 return (NULL);
5169 5171 }
5170 5172 return (mp);
5171 5173 }
5172 5174
5173 5175 /*
5174 5176 * Deliver a udp packet to the given conn, possibly applying ipsec policy.
5175 5177 * Handles IPv4 and IPv6.
5176 5178 * We are responsible for disposing of mp, such as by freemsg() or putnext()
5177 5179 * Caller is responsible for dropping references to the conn.
5178 5180 */
5179 5181 void
5180 5182 ip_fanout_udp_conn(conn_t *connp, mblk_t *mp, ipha_t *ipha, ip6_t *ip6h,
5181 5183 ip_recv_attr_t *ira)
5182 5184 {
5183 5185 ill_t *ill = ira->ira_ill;
5184 5186 ip_stack_t *ipst = ill->ill_ipst;
5185 5187 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
5186 5188 boolean_t secure;
5187 5189 iaflags_t iraflags = ira->ira_flags;
5188 5190
5189 5191 secure = iraflags & IRAF_IPSEC_SECURE;
5190 5192
5191 5193 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld :
5192 5194 !canputnext(connp->conn_rq)) {
5193 5195 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows);
5194 5196 freemsg(mp);
5195 5197 return;
5196 5198 }
5197 5199
5198 5200 if (((iraflags & IRAF_IS_IPV4) ?
5199 5201 CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
5200 5202 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
5201 5203 secure) {
5202 5204 mp = ipsec_check_inbound_policy(mp, connp, ipha,
5203 5205 ip6h, ira);
5204 5206 if (mp == NULL) {
5205 5207 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5206 5208 /* Note that mp is NULL */
5207 5209 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5208 5210 return;
5209 5211 }
5210 5212 }
5211 5213
5212 5214 /*
5213 5215 * Since this code is not used for UDP unicast we don't need a NAT_T
5214 5216 * check. Only ip_fanout_v4 has that check.
5215 5217 */
5216 5218 if (ira->ira_flags & IRAF_ICMP_ERROR) {
5217 5219 (connp->conn_recvicmp)(connp, mp, NULL, ira);
5218 5220 } else {
5219 5221 ill_t *rill = ira->ira_rill;
5220 5222
5221 5223 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
5222 5224 ira->ira_ill = ira->ira_rill = NULL;
5223 5225 /* Send it upstream */
5224 5226 (connp->conn_recv)(connp, mp, NULL, ira);
5225 5227 ira->ira_ill = ill;
5226 5228 ira->ira_rill = rill;
5227 5229 }
5228 5230 }
5229 5231
5230 5232 /*
5231 5233 * Fanout for UDP packets that are multicast or broadcast, and ICMP errors.
5232 5234 * (Unicast fanout is handled in ip_input_v4.)
5233 5235 *
5234 5236 * If SO_REUSEADDR is set all multicast and broadcast packets
5235 5237 * will be delivered to all conns bound to the same port.
5236 5238 *
5237 5239 * If there is at least one matching AF_INET receiver, then we will
5238 5240 * ignore any AF_INET6 receivers.
5239 5241 * In the special case where an AF_INET socket binds to 0.0.0.0/<port> and an
5240 5242 * AF_INET6 socket binds to ::/<port>, only the AF_INET socket receives the IPv4
5241 5243 * packets.
5242 5244 *
5243 5245 * Zones notes:
5244 5246 * Earlier in ip_input on a system with multiple shared-IP zones we
5245 5247 * duplicate the multicast and broadcast packets and send them up
5246 5248 * with each explicit zoneid that exists on that ill.
5247 5249 * This means that here we can match the zoneid with SO_ALLZONES being special.
5248 5250 */
5249 5251 void
5250 5252 ip_fanout_udp_multi_v4(mblk_t *mp, ipha_t *ipha, uint16_t lport, uint16_t fport,
5251 5253 ip_recv_attr_t *ira)
5252 5254 {
5253 5255 ipaddr_t laddr;
5254 5256 in6_addr_t v6faddr;
5255 5257 conn_t *connp;
5256 5258 connf_t *connfp;
5257 5259 ipaddr_t faddr;
5258 5260 ill_t *ill = ira->ira_ill;
5259 5261 ip_stack_t *ipst = ill->ill_ipst;
5260 5262
5261 5263 ASSERT(ira->ira_flags & (IRAF_MULTIBROADCAST|IRAF_ICMP_ERROR));
5262 5264
5263 5265 laddr = ipha->ipha_dst;
5264 5266 faddr = ipha->ipha_src;
5265 5267
5266 5268 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
5267 5269 mutex_enter(&connfp->connf_lock);
5268 5270 connp = connfp->connf_head;
5269 5271
5270 5272 /*
5271 5273 * If SO_REUSEADDR has been set on the first we send the
5272 5274 * packet to all clients that have joined the group and
5273 5275 * match the port.
5274 5276 */
5275 5277 while (connp != NULL) {
5276 5278 if ((IPCL_UDP_MATCH(connp, lport, laddr, fport, faddr)) &&
5277 5279 conn_wantpacket(connp, ira, ipha) &&
5278 5280 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5279 5281 tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp)))
5280 5282 break;
5281 5283 connp = connp->conn_next;
5282 5284 }
5283 5285
5284 5286 if (connp == NULL)
5285 5287 goto notfound;
5286 5288
5287 5289 CONN_INC_REF(connp);
5288 5290
5289 5291 if (connp->conn_reuseaddr) {
5290 5292 conn_t *first_connp = connp;
5291 5293 conn_t *next_connp;
5292 5294 mblk_t *mp1;
5293 5295
5294 5296 connp = connp->conn_next;
5295 5297 for (;;) {
5296 5298 while (connp != NULL) {
5297 5299 if (IPCL_UDP_MATCH(connp, lport, laddr,
5298 5300 fport, faddr) &&
5299 5301 conn_wantpacket(connp, ira, ipha) &&
5300 5302 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5301 5303 tsol_receive_local(mp, &laddr, IPV4_VERSION,
5302 5304 ira, connp)))
5303 5305 break;
5304 5306 connp = connp->conn_next;
5305 5307 }
5306 5308 if (connp == NULL) {
5307 5309 /* No more interested clients */
5308 5310 connp = first_connp;
5309 5311 break;
5310 5312 }
5311 5313 if (((mp1 = dupmsg(mp)) == NULL) &&
5312 5314 ((mp1 = copymsg(mp)) == NULL)) {
5313 5315 /* Memory allocation failed */
5314 5316 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5315 5317 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5316 5318 connp = first_connp;
5317 5319 break;
5318 5320 }
5319 5321 CONN_INC_REF(connp);
5320 5322 mutex_exit(&connfp->connf_lock);
5321 5323
5322 5324 IP_STAT(ipst, ip_udp_fanmb);
5323 5325 ip_fanout_udp_conn(connp, mp1, (ipha_t *)mp1->b_rptr,
5324 5326 NULL, ira);
5325 5327 mutex_enter(&connfp->connf_lock);
5326 5328 /* Follow the next pointer before releasing the conn */
5327 5329 next_connp = connp->conn_next;
5328 5330 CONN_DEC_REF(connp);
5329 5331 connp = next_connp;
5330 5332 }
5331 5333 }
5332 5334
5333 5335 /* Last one. Send it upstream. */
5334 5336 mutex_exit(&connfp->connf_lock);
5335 5337 IP_STAT(ipst, ip_udp_fanmb);
5336 5338 ip_fanout_udp_conn(connp, mp, ipha, NULL, ira);
5337 5339 CONN_DEC_REF(connp);
5338 5340 return;
5339 5341
5340 5342 notfound:
5341 5343 mutex_exit(&connfp->connf_lock);
5342 5344 /*
5343 5345 * IPv6 endpoints bound to multicast IPv4-mapped addresses
5344 5346 * have already been matched above, since they live in the IPv4
5345 5347 * fanout tables. This implies we only need to
5346 5348 * check for IPv6 in6addr_any endpoints here.
5347 5349 * Thus we compare using ipv6_all_zeros instead of the destination
5348 5350 * address, except for the multicast group membership lookup which
5349 5351 * uses the IPv4 destination.
5350 5352 */
5351 5353 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6faddr);
5352 5354 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(lport, ipst)];
5353 5355 mutex_enter(&connfp->connf_lock);
5354 5356 connp = connfp->connf_head;
5355 5357 /*
5356 5358 * IPv4 multicast packet being delivered to an AF_INET6
5357 5359 * in6addr_any endpoint.
5358 5360 * Need to check conn_wantpacket(). Note that we use conn_wantpacket()
5359 5361 * and not conn_wantpacket_v6() since any multicast membership is
5360 5362 * for an IPv4-mapped multicast address.
5361 5363 */
5362 5364 while (connp != NULL) {
5363 5365 if (IPCL_UDP_MATCH_V6(connp, lport, ipv6_all_zeros,
5364 5366 fport, v6faddr) &&
5365 5367 conn_wantpacket(connp, ira, ipha) &&
5366 5368 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5367 5369 tsol_receive_local(mp, &laddr, IPV4_VERSION, ira, connp)))
5368 5370 break;
5369 5371 connp = connp->conn_next;
5370 5372 }
5371 5373
5372 5374 if (connp == NULL) {
5373 5375 /*
5374 5376 * No one bound to this port. Is
5375 5377 * there a client that wants all
5376 5378 * unclaimed datagrams?
5377 5379 */
5378 5380 mutex_exit(&connfp->connf_lock);
5379 5381
5380 5382 if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_UDP].connf_head !=
5381 5383 NULL) {
5382 5384 ASSERT(ira->ira_protocol == IPPROTO_UDP);
5383 5385 ip_fanout_proto_v4(mp, ipha, ira);
5384 5386 } else {
5385 5387 /*
5386 5388 * We used to attempt to send an icmp error here, but
5387 5389 * since this is known to be a multicast packet
5388 5390 * and we don't send icmp errors in response to
5389 5391 * multicast, just drop the packet and give up sooner.
5390 5392 */
5391 5393 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts);
5392 5394 freemsg(mp);
5393 5395 }
5394 5396 return;
5395 5397 }
5396 5398 ASSERT(IPCL_IS_NONSTR(connp) || connp->conn_rq != NULL);
5397 5399
5398 5400 /*
5399 5401 * If SO_REUSEADDR has been set on the first we send the
5400 5402 * packet to all clients that have joined the group and
5401 5403 * match the port.
5402 5404 */
5403 5405 if (connp->conn_reuseaddr) {
5404 5406 conn_t *first_connp = connp;
5405 5407 conn_t *next_connp;
5406 5408 mblk_t *mp1;
5407 5409
5408 5410 CONN_INC_REF(connp);
5409 5411 connp = connp->conn_next;
5410 5412 for (;;) {
5411 5413 while (connp != NULL) {
5412 5414 if (IPCL_UDP_MATCH_V6(connp, lport,
5413 5415 ipv6_all_zeros, fport, v6faddr) &&
5414 5416 conn_wantpacket(connp, ira, ipha) &&
5415 5417 (!(ira->ira_flags & IRAF_SYSTEM_LABELED) ||
5416 5418 tsol_receive_local(mp, &laddr, IPV4_VERSION,
5417 5419 ira, connp)))
5418 5420 break;
5419 5421 connp = connp->conn_next;
5420 5422 }
5421 5423 if (connp == NULL) {
5422 5424 /* No more interested clients */
5423 5425 connp = first_connp;
5424 5426 break;
5425 5427 }
5426 5428 if (((mp1 = dupmsg(mp)) == NULL) &&
5427 5429 ((mp1 = copymsg(mp)) == NULL)) {
5428 5430 /* Memory allocation failed */
5429 5431 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
5430 5432 ip_drop_input("ipIfStatsInDiscards", mp, ill);
5431 5433 connp = first_connp;
5432 5434 break;
5433 5435 }
5434 5436 CONN_INC_REF(connp);
5435 5437 mutex_exit(&connfp->connf_lock);
5436 5438
5437 5439 IP_STAT(ipst, ip_udp_fanmb);
5438 5440 ip_fanout_udp_conn(connp, mp1, (ipha_t *)mp1->b_rptr,
5439 5441 NULL, ira);
5440 5442 mutex_enter(&connfp->connf_lock);
5441 5443 /* Follow the next pointer before releasing the conn */
5442 5444 next_connp = connp->conn_next;
5443 5445 CONN_DEC_REF(connp);
5444 5446 connp = next_connp;
5445 5447 }
5446 5448 }
5447 5449
5448 5450 /* Last one. Send it upstream. */
5449 5451 mutex_exit(&connfp->connf_lock);
5450 5452 IP_STAT(ipst, ip_udp_fanmb);
5451 5453 ip_fanout_udp_conn(connp, mp, ipha, NULL, ira);
5452 5454 CONN_DEC_REF(connp);
5453 5455 }
5454 5456
5455 5457 /*
5456 5458 * Split an incoming packet's IPv4 options into the label and the other options.
5457 5459 * If 'allocate' is set it does memory allocation for the ip_pkt_t, including
5458 5460 * clearing out any leftover label or options.
5459 5461 * Otherwise it just makes ipp point into the packet.
5460 5462 *
5461 5463 * Returns zero if ok; ENOMEM if the buffer couldn't be allocated.
5462 5464 */
5463 5465 int
5464 5466 ip_find_hdr_v4(ipha_t *ipha, ip_pkt_t *ipp, boolean_t allocate)
5465 5467 {
5466 5468 uchar_t *opt;
5467 5469 uint32_t totallen;
5468 5470 uint32_t optval;
5469 5471 uint32_t optlen;
5470 5472
5471 5473 ipp->ipp_fields |= IPPF_HOPLIMIT | IPPF_TCLASS | IPPF_ADDR;
5472 5474 ipp->ipp_hoplimit = ipha->ipha_ttl;
5473 5475 ipp->ipp_type_of_service = ipha->ipha_type_of_service;
5474 5476 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &ipp->ipp_addr);
5475 5477
5476 5478 /*
5477 5479 * Get length (in 4 byte octets) of IP header options.
5478 5480 */
5479 5481 totallen = ipha->ipha_version_and_hdr_length -
5480 5482 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
5481 5483
5482 5484 if (totallen == 0) {
5483 5485 if (!allocate)
5484 5486 return (0);
5485 5487
5486 5488 /* Clear out anything from a previous packet */
5487 5489 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
5488 5490 kmem_free(ipp->ipp_ipv4_options,
5489 5491 ipp->ipp_ipv4_options_len);
5490 5492 ipp->ipp_ipv4_options = NULL;
5491 5493 ipp->ipp_ipv4_options_len = 0;
5492 5494 ipp->ipp_fields &= ~IPPF_IPV4_OPTIONS;
5493 5495 }
5494 5496 if (ipp->ipp_fields & IPPF_LABEL_V4) {
5495 5497 kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4);
5496 5498 ipp->ipp_label_v4 = NULL;
5497 5499 ipp->ipp_label_len_v4 = 0;
5498 5500 ipp->ipp_fields &= ~IPPF_LABEL_V4;
5499 5501 }
5500 5502 return (0);
5501 5503 }
5502 5504
5503 5505 totallen <<= 2;
5504 5506 opt = (uchar_t *)&ipha[1];
5505 5507 if (!is_system_labeled()) {
5506 5508
5507 5509 copyall:
5508 5510 if (!allocate) {
5509 5511 if (totallen != 0) {
5510 5512 ipp->ipp_ipv4_options = opt;
5511 5513 ipp->ipp_ipv4_options_len = totallen;
5512 5514 ipp->ipp_fields |= IPPF_IPV4_OPTIONS;
5513 5515 }
5514 5516 return (0);
5515 5517 }
5516 5518 /* Just copy all of options */
5517 5519 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
5518 5520 if (totallen == ipp->ipp_ipv4_options_len) {
5519 5521 bcopy(opt, ipp->ipp_ipv4_options, totallen);
5520 5522 return (0);
5521 5523 }
5522 5524 kmem_free(ipp->ipp_ipv4_options,
5523 5525 ipp->ipp_ipv4_options_len);
5524 5526 ipp->ipp_ipv4_options = NULL;
5525 5527 ipp->ipp_ipv4_options_len = 0;
5526 5528 ipp->ipp_fields &= ~IPPF_IPV4_OPTIONS;
5527 5529 }
5528 5530 if (totallen == 0)
5529 5531 return (0);
5530 5532
5531 5533 ipp->ipp_ipv4_options = kmem_alloc(totallen, KM_NOSLEEP);
5532 5534 if (ipp->ipp_ipv4_options == NULL)
5533 5535 return (ENOMEM);
5534 5536 ipp->ipp_ipv4_options_len = totallen;
5535 5537 ipp->ipp_fields |= IPPF_IPV4_OPTIONS;
5536 5538 bcopy(opt, ipp->ipp_ipv4_options, totallen);
5537 5539 return (0);
5538 5540 }
5539 5541
5540 5542 if (allocate && (ipp->ipp_fields & IPPF_LABEL_V4)) {
5541 5543 kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4);
5542 5544 ipp->ipp_label_v4 = NULL;
5543 5545 ipp->ipp_label_len_v4 = 0;
5544 5546 ipp->ipp_fields &= ~IPPF_LABEL_V4;
5545 5547 }
5546 5548
5547 5549 /*
5548 5550 * Search for CIPSO option.
5549 5551 * We assume CIPSO is first in options if it is present.
5550 5552 * If it isn't, then ipp_opt_ipv4_options will not include the options
5551 5553 * prior to the CIPSO option.
5552 5554 */
5553 5555 while (totallen != 0) {
5554 5556 switch (optval = opt[IPOPT_OPTVAL]) {
5555 5557 case IPOPT_EOL:
5556 5558 return (0);
5557 5559 case IPOPT_NOP:
5558 5560 optlen = 1;
5559 5561 break;
5560 5562 default:
5561 5563 if (totallen <= IPOPT_OLEN)
5562 5564 return (EINVAL);
5563 5565 optlen = opt[IPOPT_OLEN];
5564 5566 if (optlen < 2)
5565 5567 return (EINVAL);
5566 5568 }
5567 5569 if (optlen > totallen)
5568 5570 return (EINVAL);
5569 5571
5570 5572 switch (optval) {
5571 5573 case IPOPT_COMSEC:
5572 5574 if (!allocate) {
5573 5575 ipp->ipp_label_v4 = opt;
5574 5576 ipp->ipp_label_len_v4 = optlen;
5575 5577 ipp->ipp_fields |= IPPF_LABEL_V4;
5576 5578 } else {
5577 5579 ipp->ipp_label_v4 = kmem_alloc(optlen,
5578 5580 KM_NOSLEEP);
5579 5581 if (ipp->ipp_label_v4 == NULL)
5580 5582 return (ENOMEM);
5581 5583 ipp->ipp_label_len_v4 = optlen;
5582 5584 ipp->ipp_fields |= IPPF_LABEL_V4;
5583 5585 bcopy(opt, ipp->ipp_label_v4, optlen);
5584 5586 }
5585 5587 totallen -= optlen;
5586 5588 opt += optlen;
5587 5589
5588 5590 /* Skip padding bytes until we get to a multiple of 4 */
5589 5591 while ((totallen & 3) != 0 && opt[0] == IPOPT_NOP) {
5590 5592 totallen--;
5591 5593 opt++;
5592 5594 }
5593 5595 /* Remaining as ipp_ipv4_options */
5594 5596 goto copyall;
5595 5597 }
5596 5598 totallen -= optlen;
5597 5599 opt += optlen;
5598 5600 }
5599 5601 /* No CIPSO found; return everything as ipp_ipv4_options */
5600 5602 totallen = ipha->ipha_version_and_hdr_length -
5601 5603 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
5602 5604 totallen <<= 2;
5603 5605 opt = (uchar_t *)&ipha[1];
5604 5606 goto copyall;
5605 5607 }
5606 5608
5607 5609 /*
5608 5610 * Efficient versions of lookup for an IRE when we only
5609 5611 * match the address.
5610 5612 * For RTF_REJECT or BLACKHOLE we return IRE_NOROUTE.
5611 5613 * Does not handle multicast addresses.
5612 5614 */
5613 5615 uint_t
5614 5616 ip_type_v4(ipaddr_t addr, ip_stack_t *ipst)
5615 5617 {
5616 5618 ire_t *ire;
5617 5619 uint_t result;
5618 5620
5619 5621 ire = ire_ftable_lookup_simple_v4(addr, 0, ipst, NULL);
5620 5622 ASSERT(ire != NULL);
5621 5623 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
5622 5624 result = IRE_NOROUTE;
5623 5625 else
5624 5626 result = ire->ire_type;
5625 5627 ire_refrele(ire);
5626 5628 return (result);
5627 5629 }
5628 5630
5629 5631 /*
5630 5632 * Efficient versions of lookup for an IRE when we only
5631 5633 * match the address.
5632 5634 * For RTF_REJECT or BLACKHOLE we return IRE_NOROUTE.
5633 5635 * Does not handle multicast addresses.
5634 5636 */
5635 5637 uint_t
5636 5638 ip_type_v6(const in6_addr_t *addr, ip_stack_t *ipst)
5637 5639 {
5638 5640 ire_t *ire;
5639 5641 uint_t result;
5640 5642
5641 5643 ire = ire_ftable_lookup_simple_v6(addr, 0, ipst, NULL);
5642 5644 ASSERT(ire != NULL);
5643 5645 if (ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE))
5644 5646 result = IRE_NOROUTE;
5645 5647 else
5646 5648 result = ire->ire_type;
5647 5649 ire_refrele(ire);
5648 5650 return (result);
5649 5651 }
5650 5652
5651 5653 /*
5652 5654 * Nobody should be sending
5653 5655 * packets up this stream
5654 5656 */
5655 5657 static void
5656 5658 ip_lrput(queue_t *q, mblk_t *mp)
5657 5659 {
5658 5660 switch (mp->b_datap->db_type) {
5659 5661 case M_FLUSH:
5660 5662 /* Turn around */
5661 5663 if (*mp->b_rptr & FLUSHW) {
5662 5664 *mp->b_rptr &= ~FLUSHR;
5663 5665 qreply(q, mp);
5664 5666 return;
5665 5667 }
5666 5668 break;
5667 5669 }
5668 5670 freemsg(mp);
5669 5671 }
5670 5672
5671 5673 /* Nobody should be sending packets down this stream */
5672 5674 /* ARGSUSED */
5673 5675 void
5674 5676 ip_lwput(queue_t *q, mblk_t *mp)
5675 5677 {
5676 5678 freemsg(mp);
5677 5679 }
5678 5680
5679 5681 /*
5680 5682 * Move the first hop in any source route to ipha_dst and remove that part of
5681 5683 * the source route. Called by other protocols. Errors in option formatting
5682 5684 * are ignored - will be handled by ip_output_options. Return the final
5683 5685 * destination (either ipha_dst or the last entry in a source route.)
5684 5686 */
5685 5687 ipaddr_t
5686 5688 ip_massage_options(ipha_t *ipha, netstack_t *ns)
5687 5689 {
5688 5690 ipoptp_t opts;
5689 5691 uchar_t *opt;
5690 5692 uint8_t optval;
5691 5693 uint8_t optlen;
5692 5694 ipaddr_t dst;
5693 5695 int i;
5694 5696 ip_stack_t *ipst = ns->netstack_ip;
5695 5697
5696 5698 ip2dbg(("ip_massage_options\n"));
5697 5699 dst = ipha->ipha_dst;
5698 5700 for (optval = ipoptp_first(&opts, ipha);
5699 5701 optval != IPOPT_EOL;
5700 5702 optval = ipoptp_next(&opts)) {
5701 5703 opt = opts.ipoptp_cur;
5702 5704 switch (optval) {
5703 5705 uint8_t off;
5704 5706 case IPOPT_SSRR:
5705 5707 case IPOPT_LSRR:
5706 5708 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
5707 5709 ip1dbg(("ip_massage_options: bad src route\n"));
5708 5710 break;
5709 5711 }
5710 5712 optlen = opts.ipoptp_len;
5711 5713 off = opt[IPOPT_OFFSET];
5712 5714 off--;
5713 5715 redo_srr:
5714 5716 if (optlen < IP_ADDR_LEN ||
5715 5717 off > optlen - IP_ADDR_LEN) {
5716 5718 /* End of source route */
5717 5719 ip1dbg(("ip_massage_options: end of SR\n"));
5718 5720 break;
5719 5721 }
5720 5722 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
5721 5723 ip1dbg(("ip_massage_options: next hop 0x%x\n",
5722 5724 ntohl(dst)));
5723 5725 /*
5724 5726 * Check if our address is present more than
5725 5727 * once as consecutive hops in source route.
5726 5728 * XXX verify per-interface ip_forwarding
5727 5729 * for source route?
5728 5730 */
5729 5731 if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
5730 5732 off += IP_ADDR_LEN;
5731 5733 goto redo_srr;
5732 5734 }
5733 5735 if (dst == htonl(INADDR_LOOPBACK)) {
5734 5736 ip1dbg(("ip_massage_options: loopback addr in "
5735 5737 "source route!\n"));
5736 5738 break;
5737 5739 }
5738 5740 /*
5739 5741 * Update ipha_dst to be the first hop and remove the
5740 5742 * first hop from the source route (by overwriting
5741 5743 * part of the option with NOP options).
5742 5744 */
5743 5745 ipha->ipha_dst = dst;
5744 5746 /* Put the last entry in dst */
5745 5747 off = ((optlen - IP_ADDR_LEN - 3) & ~(IP_ADDR_LEN-1)) +
5746 5748 3;
5747 5749 bcopy(&opt[off], &dst, IP_ADDR_LEN);
5748 5750
5749 5751 ip1dbg(("ip_massage_options: last hop 0x%x\n",
5750 5752 ntohl(dst)));
5751 5753 /* Move down and overwrite */
5752 5754 opt[IP_ADDR_LEN] = opt[0];
5753 5755 opt[IP_ADDR_LEN+1] = opt[IPOPT_OLEN] - IP_ADDR_LEN;
5754 5756 opt[IP_ADDR_LEN+2] = opt[IPOPT_OFFSET];
5755 5757 for (i = 0; i < IP_ADDR_LEN; i++)
5756 5758 opt[i] = IPOPT_NOP;
5757 5759 break;
5758 5760 }
5759 5761 }
5760 5762 return (dst);
5761 5763 }
5762 5764
5763 5765 /*
5764 5766 * Return the network mask
5765 5767 * associated with the specified address.
5766 5768 */
5767 5769 ipaddr_t
5768 5770 ip_net_mask(ipaddr_t addr)
5769 5771 {
5770 5772 uchar_t *up = (uchar_t *)&addr;
5771 5773 ipaddr_t mask = 0;
5772 5774 uchar_t *maskp = (uchar_t *)&mask;
5773 5775
5774 5776 #if defined(__i386) || defined(__amd64)
5775 5777 #define TOTALLY_BRAIN_DAMAGED_C_COMPILER
5776 5778 #endif
5777 5779 #ifdef TOTALLY_BRAIN_DAMAGED_C_COMPILER
5778 5780 maskp[0] = maskp[1] = maskp[2] = maskp[3] = 0;
5779 5781 #endif
5780 5782 if (CLASSD(addr)) {
5781 5783 maskp[0] = 0xF0;
5782 5784 return (mask);
5783 5785 }
5784 5786
5785 5787 /* We assume Class E default netmask to be 32 */
5786 5788 if (CLASSE(addr))
5787 5789 return (0xffffffffU);
5788 5790
5789 5791 if (addr == 0)
5790 5792 return (0);
5791 5793 maskp[0] = 0xFF;
5792 5794 if ((up[0] & 0x80) == 0)
5793 5795 return (mask);
5794 5796
5795 5797 maskp[1] = 0xFF;
5796 5798 if ((up[0] & 0xC0) == 0x80)
5797 5799 return (mask);
5798 5800
5799 5801 maskp[2] = 0xFF;
5800 5802 if ((up[0] & 0xE0) == 0xC0)
5801 5803 return (mask);
5802 5804
5803 5805 /* Otherwise return no mask */
5804 5806 return ((ipaddr_t)0);
5805 5807 }
5806 5808
5807 5809 /* Name/Value Table Lookup Routine */
5808 5810 char *
5809 5811 ip_nv_lookup(nv_t *nv, int value)
5810 5812 {
5811 5813 if (!nv)
5812 5814 return (NULL);
5813 5815 for (; nv->nv_name; nv++) {
5814 5816 if (nv->nv_value == value)
5815 5817 return (nv->nv_name);
5816 5818 }
5817 5819 return ("unknown");
5818 5820 }
5819 5821
5820 5822 static int
5821 5823 ip_wait_for_info_ack(ill_t *ill)
5822 5824 {
5823 5825 int err;
5824 5826
5825 5827 mutex_enter(&ill->ill_lock);
5826 5828 while (ill->ill_state_flags & ILL_LL_SUBNET_PENDING) {
5827 5829 /*
5828 5830 * Return value of 0 indicates a pending signal.
5829 5831 */
5830 5832 err = cv_wait_sig(&ill->ill_cv, &ill->ill_lock);
5831 5833 if (err == 0) {
5832 5834 mutex_exit(&ill->ill_lock);
5833 5835 return (EINTR);
5834 5836 }
5835 5837 }
5836 5838 mutex_exit(&ill->ill_lock);
5837 5839 /*
5838 5840 * ip_rput_other could have set an error in ill_error on
5839 5841 * receipt of M_ERROR.
5840 5842 */
5841 5843 return (ill->ill_error);
5842 5844 }
5843 5845
5844 5846 /*
5845 5847 * This is a module open, i.e. this is a control stream for access
5846 5848 * to a DLPI device. We allocate an ill_t as the instance data in
5847 5849 * this case.
5848 5850 */
5849 5851 static int
5850 5852 ip_modopen(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5851 5853 {
5852 5854 ill_t *ill;
5853 5855 int err;
5854 5856 zoneid_t zoneid;
5855 5857 netstack_t *ns;
5856 5858 ip_stack_t *ipst;
5857 5859
5858 5860 /*
5859 5861 * Prevent unprivileged processes from pushing IP so that
5860 5862 * they can't send raw IP.
5861 5863 */
5862 5864 if (secpolicy_net_rawaccess(credp) != 0)
5863 5865 return (EPERM);
5864 5866
5865 5867 ns = netstack_find_by_cred(credp);
5866 5868 ASSERT(ns != NULL);
5867 5869 ipst = ns->netstack_ip;
5868 5870 ASSERT(ipst != NULL);
5869 5871
5870 5872 /*
5871 5873 * For exclusive stacks we set the zoneid to zero
5872 5874 * to make IP operate as if in the global zone.
5873 5875 */
5874 5876 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
5875 5877 zoneid = GLOBAL_ZONEID;
5876 5878 else
5877 5879 zoneid = crgetzoneid(credp);
5878 5880
5879 5881 ill = (ill_t *)mi_open_alloc_sleep(sizeof (ill_t));
5880 5882 q->q_ptr = WR(q)->q_ptr = ill;
5881 5883 ill->ill_ipst = ipst;
5882 5884 ill->ill_zoneid = zoneid;
5883 5885
5884 5886 /*
5885 5887 * ill_init initializes the ill fields and then sends down
5886 5888 * down a DL_INFO_REQ after calling qprocson.
5887 5889 */
5888 5890 err = ill_init(q, ill);
5889 5891
5890 5892 if (err != 0) {
5891 5893 mi_free(ill);
5892 5894 netstack_rele(ipst->ips_netstack);
5893 5895 q->q_ptr = NULL;
5894 5896 WR(q)->q_ptr = NULL;
5895 5897 return (err);
5896 5898 }
5897 5899
5898 5900 /*
5899 5901 * Wait for the DL_INFO_ACK if a DL_INFO_REQ was sent.
5900 5902 *
5901 5903 * ill_init initializes the ipsq marking this thread as
5902 5904 * writer
5903 5905 */
5904 5906 ipsq_exit(ill->ill_phyint->phyint_ipsq);
5905 5907 err = ip_wait_for_info_ack(ill);
5906 5908 if (err == 0)
5907 5909 ill->ill_credp = credp;
5908 5910 else
5909 5911 goto fail;
5910 5912
5911 5913 crhold(credp);
5912 5914
5913 5915 mutex_enter(&ipst->ips_ip_mi_lock);
5914 5916 err = mi_open_link(&ipst->ips_ip_g_head, (IDP)q->q_ptr, devp, flag,
5915 5917 sflag, credp);
5916 5918 mutex_exit(&ipst->ips_ip_mi_lock);
5917 5919 fail:
5918 5920 if (err) {
5919 5921 (void) ip_close(q, 0);
5920 5922 return (err);
5921 5923 }
5922 5924 return (0);
5923 5925 }
5924 5926
5925 5927 /* For /dev/ip aka AF_INET open */
5926 5928 int
5927 5929 ip_openv4(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5928 5930 {
5929 5931 return (ip_open(q, devp, flag, sflag, credp, B_FALSE));
5930 5932 }
5931 5933
5932 5934 /* For /dev/ip6 aka AF_INET6 open */
5933 5935 int
5934 5936 ip_openv6(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp)
5935 5937 {
5936 5938 return (ip_open(q, devp, flag, sflag, credp, B_TRUE));
5937 5939 }
5938 5940
5939 5941 /* IP open routine. */
5940 5942 int
5941 5943 ip_open(queue_t *q, dev_t *devp, int flag, int sflag, cred_t *credp,
5942 5944 boolean_t isv6)
5943 5945 {
5944 5946 conn_t *connp;
5945 5947 major_t maj;
5946 5948 zoneid_t zoneid;
5947 5949 netstack_t *ns;
5948 5950 ip_stack_t *ipst;
5949 5951
5950 5952 /* Allow reopen. */
5951 5953 if (q->q_ptr != NULL)
5952 5954 return (0);
5953 5955
5954 5956 if (sflag & MODOPEN) {
5955 5957 /* This is a module open */
5956 5958 return (ip_modopen(q, devp, flag, sflag, credp));
5957 5959 }
5958 5960
5959 5961 if ((flag & ~(FKLYR)) == IP_HELPER_STR) {
5960 5962 /*
5961 5963 * Non streams based socket looking for a stream
5962 5964 * to access IP
5963 5965 */
5964 5966 return (ip_helper_stream_setup(q, devp, flag, sflag,
5965 5967 credp, isv6));
5966 5968 }
5967 5969
5968 5970 ns = netstack_find_by_cred(credp);
5969 5971 ASSERT(ns != NULL);
5970 5972 ipst = ns->netstack_ip;
5971 5973 ASSERT(ipst != NULL);
5972 5974
5973 5975 /*
5974 5976 * For exclusive stacks we set the zoneid to zero
5975 5977 * to make IP operate as if in the global zone.
5976 5978 */
5977 5979 if (ipst->ips_netstack->netstack_stackid != GLOBAL_NETSTACKID)
5978 5980 zoneid = GLOBAL_ZONEID;
5979 5981 else
5980 5982 zoneid = crgetzoneid(credp);
5981 5983
5982 5984 /*
5983 5985 * We are opening as a device. This is an IP client stream, and we
5984 5986 * allocate an conn_t as the instance data.
5985 5987 */
5986 5988 connp = ipcl_conn_create(IPCL_IPCCONN, KM_SLEEP, ipst->ips_netstack);
5987 5989
5988 5990 /*
5989 5991 * ipcl_conn_create did a netstack_hold. Undo the hold that was
5990 5992 * done by netstack_find_by_cred()
5991 5993 */
5992 5994 netstack_rele(ipst->ips_netstack);
5993 5995
5994 5996 connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP | IXAF_SET_ULP_CKSUM;
5995 5997 /* conn_allzones can not be set this early, hence no IPCL_ZONEID */
5996 5998 connp->conn_ixa->ixa_zoneid = zoneid;
5997 5999 connp->conn_zoneid = zoneid;
5998 6000
5999 6001 connp->conn_rq = q;
6000 6002 q->q_ptr = WR(q)->q_ptr = connp;
6001 6003
6002 6004 /* Minor tells us which /dev entry was opened */
6003 6005 if (isv6) {
6004 6006 connp->conn_family = AF_INET6;
6005 6007 connp->conn_ipversion = IPV6_VERSION;
6006 6008 connp->conn_ixa->ixa_flags &= ~IXAF_IS_IPV4;
6007 6009 connp->conn_ixa->ixa_src_preferences = IPV6_PREFER_SRC_DEFAULT;
6008 6010 } else {
6009 6011 connp->conn_family = AF_INET;
6010 6012 connp->conn_ipversion = IPV4_VERSION;
6011 6013 connp->conn_ixa->ixa_flags |= IXAF_IS_IPV4;
6012 6014 }
6013 6015
6014 6016 if ((ip_minor_arena_la != NULL) && (flag & SO_SOCKSTR) &&
6015 6017 ((connp->conn_dev = inet_minor_alloc(ip_minor_arena_la)) != 0)) {
6016 6018 connp->conn_minor_arena = ip_minor_arena_la;
6017 6019 } else {
6018 6020 /*
6019 6021 * Either minor numbers in the large arena were exhausted
6020 6022 * or a non socket application is doing the open.
6021 6023 * Try to allocate from the small arena.
6022 6024 */
6023 6025 if ((connp->conn_dev =
6024 6026 inet_minor_alloc(ip_minor_arena_sa)) == 0) {
6025 6027 /* CONN_DEC_REF takes care of netstack_rele() */
6026 6028 q->q_ptr = WR(q)->q_ptr = NULL;
6027 6029 CONN_DEC_REF(connp);
6028 6030 return (EBUSY);
6029 6031 }
6030 6032 connp->conn_minor_arena = ip_minor_arena_sa;
6031 6033 }
6032 6034
6033 6035 maj = getemajor(*devp);
6034 6036 *devp = makedevice(maj, (minor_t)connp->conn_dev);
6035 6037
6036 6038 /*
6037 6039 * connp->conn_cred is crfree()ed in ipcl_conn_destroy()
6038 6040 */
6039 6041 connp->conn_cred = credp;
6040 6042 connp->conn_cpid = curproc->p_pid;
6041 6043 /* Cache things in ixa without an extra refhold */
6042 6044 ASSERT(!(connp->conn_ixa->ixa_free_flags & IXA_FREE_CRED));
6043 6045 connp->conn_ixa->ixa_cred = connp->conn_cred;
6044 6046 connp->conn_ixa->ixa_cpid = connp->conn_cpid;
6045 6047 if (is_system_labeled())
6046 6048 connp->conn_ixa->ixa_tsl = crgetlabel(connp->conn_cred);
6047 6049
6048 6050 /*
6049 6051 * Handle IP_IOC_RTS_REQUEST and other ioctls which use conn_recv
6050 6052 */
6051 6053 connp->conn_recv = ip_conn_input;
6052 6054 connp->conn_recvicmp = ip_conn_input_icmp;
6053 6055
6054 6056 crhold(connp->conn_cred);
6055 6057
6056 6058 /*
6057 6059 * If the caller has the process-wide flag set, then default to MAC
6058 6060 * exempt mode. This allows read-down to unlabeled hosts.
6059 6061 */
6060 6062 if (getpflags(NET_MAC_AWARE, credp) != 0)
6061 6063 connp->conn_mac_mode = CONN_MAC_AWARE;
6062 6064
6063 6065 connp->conn_zone_is_global = (crgetzoneid(credp) == GLOBAL_ZONEID);
6064 6066
6065 6067 connp->conn_rq = q;
6066 6068 connp->conn_wq = WR(q);
6067 6069
6068 6070 /* Non-zero default values */
6069 6071 connp->conn_ixa->ixa_flags |= IXAF_MULTICAST_LOOP;
6070 6072
6071 6073 /*
6072 6074 * Make the conn globally visible to walkers
6073 6075 */
6074 6076 ASSERT(connp->conn_ref == 1);
6075 6077 mutex_enter(&connp->conn_lock);
6076 6078 connp->conn_state_flags &= ~CONN_INCIPIENT;
6077 6079 mutex_exit(&connp->conn_lock);
6078 6080
6079 6081 qprocson(q);
6080 6082
6081 6083 return (0);
6082 6084 }
6083 6085
6084 6086 /*
6085 6087 * Set IPsec policy from an ipsec_req_t. If the req is not "zero" and valid,
6086 6088 * all of them are copied to the conn_t. If the req is "zero", the policy is
6087 6089 * zeroed out. A "zero" policy has zero ipsr_{ah,req,self_encap}_req
6088 6090 * fields.
6089 6091 * We keep only the latest setting of the policy and thus policy setting
6090 6092 * is not incremental/cumulative.
6091 6093 *
6092 6094 * Requests to set policies with multiple alternative actions will
6093 6095 * go through a different API.
6094 6096 */
6095 6097 int
6096 6098 ipsec_set_req(cred_t *cr, conn_t *connp, ipsec_req_t *req)
6097 6099 {
6098 6100 uint_t ah_req = 0;
6099 6101 uint_t esp_req = 0;
6100 6102 uint_t se_req = 0;
6101 6103 ipsec_act_t *actp = NULL;
6102 6104 uint_t nact;
6103 6105 ipsec_policy_head_t *ph;
6104 6106 boolean_t is_pol_reset, is_pol_inserted = B_FALSE;
6105 6107 int error = 0;
6106 6108 netstack_t *ns = connp->conn_netstack;
6107 6109 ip_stack_t *ipst = ns->netstack_ip;
6108 6110 ipsec_stack_t *ipss = ns->netstack_ipsec;
6109 6111
6110 6112 #define REQ_MASK (IPSEC_PREF_REQUIRED|IPSEC_PREF_NEVER)
6111 6113
6112 6114 /*
6113 6115 * The IP_SEC_OPT option does not allow variable length parameters,
6114 6116 * hence a request cannot be NULL.
6115 6117 */
6116 6118 if (req == NULL)
6117 6119 return (EINVAL);
6118 6120
6119 6121 ah_req = req->ipsr_ah_req;
6120 6122 esp_req = req->ipsr_esp_req;
6121 6123 se_req = req->ipsr_self_encap_req;
6122 6124
6123 6125 /* Don't allow setting self-encap without one or more of AH/ESP. */
6124 6126 if (se_req != 0 && esp_req == 0 && ah_req == 0)
6125 6127 return (EINVAL);
6126 6128
6127 6129 /*
6128 6130 * Are we dealing with a request to reset the policy (i.e.
6129 6131 * zero requests).
6130 6132 */
6131 6133 is_pol_reset = ((ah_req & REQ_MASK) == 0 &&
6132 6134 (esp_req & REQ_MASK) == 0 &&
6133 6135 (se_req & REQ_MASK) == 0);
6134 6136
6135 6137 if (!is_pol_reset) {
6136 6138 /*
6137 6139 * If we couldn't load IPsec, fail with "protocol
6138 6140 * not supported".
6139 6141 * IPsec may not have been loaded for a request with zero
6140 6142 * policies, so we don't fail in this case.
6141 6143 */
6142 6144 mutex_enter(&ipss->ipsec_loader_lock);
6143 6145 if (ipss->ipsec_loader_state != IPSEC_LOADER_SUCCEEDED) {
6144 6146 mutex_exit(&ipss->ipsec_loader_lock);
6145 6147 return (EPROTONOSUPPORT);
6146 6148 }
6147 6149 mutex_exit(&ipss->ipsec_loader_lock);
6148 6150
6149 6151 /*
6150 6152 * Test for valid requests. Invalid algorithms
6151 6153 * need to be tested by IPsec code because new
6152 6154 * algorithms can be added dynamically.
6153 6155 */
6154 6156 if ((ah_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 ||
6155 6157 (esp_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0 ||
6156 6158 (se_req & ~(REQ_MASK|IPSEC_PREF_UNIQUE)) != 0) {
6157 6159 return (EINVAL);
6158 6160 }
6159 6161
6160 6162 /*
6161 6163 * Only privileged users can issue these
6162 6164 * requests.
6163 6165 */
6164 6166 if (((ah_req & IPSEC_PREF_NEVER) ||
6165 6167 (esp_req & IPSEC_PREF_NEVER) ||
6166 6168 (se_req & IPSEC_PREF_NEVER)) &&
6167 6169 secpolicy_ip_config(cr, B_FALSE) != 0) {
6168 6170 return (EPERM);
6169 6171 }
6170 6172
6171 6173 /*
6172 6174 * The IPSEC_PREF_REQUIRED and IPSEC_PREF_NEVER
6173 6175 * are mutually exclusive.
6174 6176 */
6175 6177 if (((ah_req & REQ_MASK) == REQ_MASK) ||
6176 6178 ((esp_req & REQ_MASK) == REQ_MASK) ||
6177 6179 ((se_req & REQ_MASK) == REQ_MASK)) {
6178 6180 /* Both of them are set */
6179 6181 return (EINVAL);
6180 6182 }
6181 6183 }
6182 6184
6183 6185 ASSERT(MUTEX_HELD(&connp->conn_lock));
6184 6186
6185 6187 /*
6186 6188 * If we have already cached policies in conn_connect(), don't
6187 6189 * let them change now. We cache policies for connections
6188 6190 * whose src,dst [addr, port] is known.
6189 6191 */
6190 6192 if (connp->conn_policy_cached) {
6191 6193 return (EINVAL);
6192 6194 }
6193 6195
6194 6196 /*
6195 6197 * We have a zero policies, reset the connection policy if already
6196 6198 * set. This will cause the connection to inherit the
6197 6199 * global policy, if any.
6198 6200 */
6199 6201 if (is_pol_reset) {
6200 6202 if (connp->conn_policy != NULL) {
6201 6203 IPPH_REFRELE(connp->conn_policy, ipst->ips_netstack);
6202 6204 connp->conn_policy = NULL;
6203 6205 }
6204 6206 connp->conn_in_enforce_policy = B_FALSE;
6205 6207 connp->conn_out_enforce_policy = B_FALSE;
6206 6208 return (0);
6207 6209 }
6208 6210
6209 6211 ph = connp->conn_policy = ipsec_polhead_split(connp->conn_policy,
6210 6212 ipst->ips_netstack);
6211 6213 if (ph == NULL)
6212 6214 goto enomem;
6213 6215
6214 6216 ipsec_actvec_from_req(req, &actp, &nact, ipst->ips_netstack);
6215 6217 if (actp == NULL)
6216 6218 goto enomem;
6217 6219
6218 6220 /*
6219 6221 * Always insert IPv4 policy entries, since they can also apply to
6220 6222 * ipv6 sockets being used in ipv4-compat mode.
6221 6223 */
6222 6224 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V4,
6223 6225 IPSEC_TYPE_INBOUND, ns))
6224 6226 goto enomem;
6225 6227 is_pol_inserted = B_TRUE;
6226 6228 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V4,
6227 6229 IPSEC_TYPE_OUTBOUND, ns))
6228 6230 goto enomem;
6229 6231
6230 6232 /*
6231 6233 * We're looking at a v6 socket, also insert the v6-specific
6232 6234 * entries.
6233 6235 */
6234 6236 if (connp->conn_family == AF_INET6) {
6235 6237 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V6,
6236 6238 IPSEC_TYPE_INBOUND, ns))
6237 6239 goto enomem;
6238 6240 if (!ipsec_polhead_insert(ph, actp, nact, IPSEC_AF_V6,
6239 6241 IPSEC_TYPE_OUTBOUND, ns))
6240 6242 goto enomem;
6241 6243 }
6242 6244
6243 6245 ipsec_actvec_free(actp, nact);
6244 6246
6245 6247 /*
6246 6248 * If the requests need security, set enforce_policy.
6247 6249 * If the requests are IPSEC_PREF_NEVER, one should
6248 6250 * still set conn_out_enforce_policy so that ip_set_destination
6249 6251 * marks the ip_xmit_attr_t appropriatly. This is needed so that
6250 6252 * for connections that we don't cache policy in at connect time,
6251 6253 * if global policy matches in ip_output_attach_policy, we
6252 6254 * don't wrongly inherit global policy. Similarly, we need
6253 6255 * to set conn_in_enforce_policy also so that we don't verify
6254 6256 * policy wrongly.
6255 6257 */
6256 6258 if ((ah_req & REQ_MASK) != 0 ||
6257 6259 (esp_req & REQ_MASK) != 0 ||
6258 6260 (se_req & REQ_MASK) != 0) {
6259 6261 connp->conn_in_enforce_policy = B_TRUE;
6260 6262 connp->conn_out_enforce_policy = B_TRUE;
6261 6263 }
6262 6264
6263 6265 return (error);
6264 6266 #undef REQ_MASK
6265 6267
6266 6268 /*
6267 6269 * Common memory-allocation-failure exit path.
6268 6270 */
6269 6271 enomem:
6270 6272 if (actp != NULL)
6271 6273 ipsec_actvec_free(actp, nact);
6272 6274 if (is_pol_inserted)
6273 6275 ipsec_polhead_flush(ph, ns);
6274 6276 return (ENOMEM);
6275 6277 }
6276 6278
6277 6279 /*
6278 6280 * Set socket options for joining and leaving multicast groups.
6279 6281 * Common to IPv4 and IPv6; inet6 indicates the type of socket.
6280 6282 * The caller has already check that the option name is consistent with
6281 6283 * the address family of the socket.
6282 6284 */
6283 6285 int
6284 6286 ip_opt_set_multicast_group(conn_t *connp, t_scalar_t name,
6285 6287 uchar_t *invalp, boolean_t inet6, boolean_t checkonly)
6286 6288 {
6287 6289 int *i1 = (int *)invalp;
6288 6290 int error = 0;
6289 6291 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
6290 6292 struct ip_mreq *v4_mreqp;
6291 6293 struct ipv6_mreq *v6_mreqp;
6292 6294 struct group_req *greqp;
6293 6295 ire_t *ire;
6294 6296 boolean_t done = B_FALSE;
6295 6297 ipaddr_t ifaddr;
6296 6298 in6_addr_t v6group;
6297 6299 uint_t ifindex;
6298 6300 boolean_t mcast_opt = B_TRUE;
6299 6301 mcast_record_t fmode;
6300 6302 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *,
6301 6303 ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *);
6302 6304
6303 6305 switch (name) {
6304 6306 case IP_ADD_MEMBERSHIP:
6305 6307 case IPV6_JOIN_GROUP:
6306 6308 mcast_opt = B_FALSE;
6307 6309 /* FALLTHRU */
6308 6310 case MCAST_JOIN_GROUP:
6309 6311 fmode = MODE_IS_EXCLUDE;
6310 6312 optfn = ip_opt_add_group;
6311 6313 break;
6312 6314
6313 6315 case IP_DROP_MEMBERSHIP:
6314 6316 case IPV6_LEAVE_GROUP:
6315 6317 mcast_opt = B_FALSE;
6316 6318 /* FALLTHRU */
6317 6319 case MCAST_LEAVE_GROUP:
6318 6320 fmode = MODE_IS_INCLUDE;
6319 6321 optfn = ip_opt_delete_group;
6320 6322 break;
6321 6323 default:
6322 6324 ASSERT(0);
6323 6325 }
6324 6326
6325 6327 if (mcast_opt) {
6326 6328 struct sockaddr_in *sin;
6327 6329 struct sockaddr_in6 *sin6;
6328 6330
6329 6331 greqp = (struct group_req *)i1;
6330 6332 if (greqp->gr_group.ss_family == AF_INET) {
6331 6333 sin = (struct sockaddr_in *)&(greqp->gr_group);
6332 6334 IN6_INADDR_TO_V4MAPPED(&sin->sin_addr, &v6group);
6333 6335 } else {
6334 6336 if (!inet6)
6335 6337 return (EINVAL); /* Not on INET socket */
6336 6338
6337 6339 sin6 = (struct sockaddr_in6 *)&(greqp->gr_group);
6338 6340 v6group = sin6->sin6_addr;
6339 6341 }
6340 6342 ifaddr = INADDR_ANY;
6341 6343 ifindex = greqp->gr_interface;
6342 6344 } else if (inet6) {
6343 6345 v6_mreqp = (struct ipv6_mreq *)i1;
6344 6346 v6group = v6_mreqp->ipv6mr_multiaddr;
6345 6347 ifaddr = INADDR_ANY;
6346 6348 ifindex = v6_mreqp->ipv6mr_interface;
6347 6349 } else {
6348 6350 v4_mreqp = (struct ip_mreq *)i1;
6349 6351 IN6_INADDR_TO_V4MAPPED(&v4_mreqp->imr_multiaddr, &v6group);
6350 6352 ifaddr = (ipaddr_t)v4_mreqp->imr_interface.s_addr;
6351 6353 ifindex = 0;
6352 6354 }
6353 6355
6354 6356 /*
6355 6357 * In the multirouting case, we need to replicate
6356 6358 * the request on all interfaces that will take part
6357 6359 * in replication. We do so because multirouting is
6358 6360 * reflective, thus we will probably receive multi-
6359 6361 * casts on those interfaces.
6360 6362 * The ip_multirt_apply_membership() succeeds if
6361 6363 * the operation succeeds on at least one interface.
6362 6364 */
6363 6365 if (IN6_IS_ADDR_V4MAPPED(&v6group)) {
6364 6366 ipaddr_t group;
6365 6367
6366 6368 IN6_V4MAPPED_TO_IPADDR(&v6group, group);
6367 6369
6368 6370 ire = ire_ftable_lookup_v4(group, IP_HOST_MASK, 0,
6369 6371 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6370 6372 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6371 6373 } else {
6372 6374 ire = ire_ftable_lookup_v6(&v6group, &ipv6_all_ones, 0,
6373 6375 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6374 6376 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6375 6377 }
6376 6378 if (ire != NULL) {
6377 6379 if (ire->ire_flags & RTF_MULTIRT) {
6378 6380 error = ip_multirt_apply_membership(optfn, ire, connp,
6379 6381 checkonly, &v6group, fmode, &ipv6_all_zeros);
6380 6382 done = B_TRUE;
6381 6383 }
6382 6384 ire_refrele(ire);
6383 6385 }
6384 6386
6385 6387 if (!done) {
6386 6388 error = optfn(connp, checkonly, &v6group, ifaddr, ifindex,
6387 6389 fmode, &ipv6_all_zeros);
6388 6390 }
6389 6391 return (error);
6390 6392 }
6391 6393
6392 6394 /*
6393 6395 * Set socket options for joining and leaving multicast groups
6394 6396 * for specific sources.
6395 6397 * Common to IPv4 and IPv6; inet6 indicates the type of socket.
6396 6398 * The caller has already check that the option name is consistent with
6397 6399 * the address family of the socket.
6398 6400 */
6399 6401 int
6400 6402 ip_opt_set_multicast_sources(conn_t *connp, t_scalar_t name,
6401 6403 uchar_t *invalp, boolean_t inet6, boolean_t checkonly)
6402 6404 {
6403 6405 int *i1 = (int *)invalp;
6404 6406 int error = 0;
6405 6407 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
6406 6408 struct ip_mreq_source *imreqp;
6407 6409 struct group_source_req *gsreqp;
6408 6410 in6_addr_t v6group, v6src;
6409 6411 uint32_t ifindex;
6410 6412 ipaddr_t ifaddr;
6411 6413 boolean_t mcast_opt = B_TRUE;
6412 6414 mcast_record_t fmode;
6413 6415 ire_t *ire;
6414 6416 boolean_t done = B_FALSE;
6415 6417 int (*optfn)(conn_t *, boolean_t, const in6_addr_t *,
6416 6418 ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *);
6417 6419
6418 6420 switch (name) {
6419 6421 case IP_BLOCK_SOURCE:
6420 6422 mcast_opt = B_FALSE;
6421 6423 /* FALLTHRU */
6422 6424 case MCAST_BLOCK_SOURCE:
6423 6425 fmode = MODE_IS_EXCLUDE;
6424 6426 optfn = ip_opt_add_group;
6425 6427 break;
6426 6428
6427 6429 case IP_UNBLOCK_SOURCE:
6428 6430 mcast_opt = B_FALSE;
6429 6431 /* FALLTHRU */
6430 6432 case MCAST_UNBLOCK_SOURCE:
6431 6433 fmode = MODE_IS_EXCLUDE;
6432 6434 optfn = ip_opt_delete_group;
6433 6435 break;
6434 6436
6435 6437 case IP_ADD_SOURCE_MEMBERSHIP:
6436 6438 mcast_opt = B_FALSE;
6437 6439 /* FALLTHRU */
6438 6440 case MCAST_JOIN_SOURCE_GROUP:
6439 6441 fmode = MODE_IS_INCLUDE;
6440 6442 optfn = ip_opt_add_group;
6441 6443 break;
6442 6444
6443 6445 case IP_DROP_SOURCE_MEMBERSHIP:
6444 6446 mcast_opt = B_FALSE;
6445 6447 /* FALLTHRU */
6446 6448 case MCAST_LEAVE_SOURCE_GROUP:
6447 6449 fmode = MODE_IS_INCLUDE;
6448 6450 optfn = ip_opt_delete_group;
6449 6451 break;
6450 6452 default:
6451 6453 ASSERT(0);
6452 6454 }
6453 6455
6454 6456 if (mcast_opt) {
6455 6457 gsreqp = (struct group_source_req *)i1;
6456 6458 ifindex = gsreqp->gsr_interface;
6457 6459 if (gsreqp->gsr_group.ss_family == AF_INET) {
6458 6460 struct sockaddr_in *s;
6459 6461 s = (struct sockaddr_in *)&gsreqp->gsr_group;
6460 6462 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6group);
6461 6463 s = (struct sockaddr_in *)&gsreqp->gsr_source;
6462 6464 IN6_INADDR_TO_V4MAPPED(&s->sin_addr, &v6src);
6463 6465 } else {
6464 6466 struct sockaddr_in6 *s6;
6465 6467
6466 6468 if (!inet6)
6467 6469 return (EINVAL); /* Not on INET socket */
6468 6470
6469 6471 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_group;
6470 6472 v6group = s6->sin6_addr;
6471 6473 s6 = (struct sockaddr_in6 *)&gsreqp->gsr_source;
6472 6474 v6src = s6->sin6_addr;
6473 6475 }
6474 6476 ifaddr = INADDR_ANY;
6475 6477 } else {
6476 6478 imreqp = (struct ip_mreq_source *)i1;
6477 6479 IN6_INADDR_TO_V4MAPPED(&imreqp->imr_multiaddr, &v6group);
6478 6480 IN6_INADDR_TO_V4MAPPED(&imreqp->imr_sourceaddr, &v6src);
6479 6481 ifaddr = (ipaddr_t)imreqp->imr_interface.s_addr;
6480 6482 ifindex = 0;
6481 6483 }
6482 6484
6483 6485 /*
6484 6486 * Handle src being mapped INADDR_ANY by changing it to unspecified.
6485 6487 */
6486 6488 if (IN6_IS_ADDR_V4MAPPED_ANY(&v6src))
6487 6489 v6src = ipv6_all_zeros;
6488 6490
6489 6491 /*
6490 6492 * In the multirouting case, we need to replicate
6491 6493 * the request as noted in the mcast cases above.
6492 6494 */
6493 6495 if (IN6_IS_ADDR_V4MAPPED(&v6group)) {
6494 6496 ipaddr_t group;
6495 6497
6496 6498 IN6_V4MAPPED_TO_IPADDR(&v6group, group);
6497 6499
6498 6500 ire = ire_ftable_lookup_v4(group, IP_HOST_MASK, 0,
6499 6501 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6500 6502 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6501 6503 } else {
6502 6504 ire = ire_ftable_lookup_v6(&v6group, &ipv6_all_ones, 0,
6503 6505 IRE_HOST | IRE_INTERFACE, NULL, ALL_ZONES, NULL,
6504 6506 MATCH_IRE_MASK | MATCH_IRE_TYPE, 0, ipst, NULL);
6505 6507 }
6506 6508 if (ire != NULL) {
6507 6509 if (ire->ire_flags & RTF_MULTIRT) {
6508 6510 error = ip_multirt_apply_membership(optfn, ire, connp,
6509 6511 checkonly, &v6group, fmode, &v6src);
6510 6512 done = B_TRUE;
6511 6513 }
6512 6514 ire_refrele(ire);
6513 6515 }
6514 6516 if (!done) {
6515 6517 error = optfn(connp, checkonly, &v6group, ifaddr, ifindex,
6516 6518 fmode, &v6src);
6517 6519 }
6518 6520 return (error);
6519 6521 }
6520 6522
6521 6523 /*
6522 6524 * Given a destination address and a pointer to where to put the information
6523 6525 * this routine fills in the mtuinfo.
6524 6526 * The socket must be connected.
6525 6527 * For sctp conn_faddr is the primary address.
6526 6528 */
6527 6529 int
6528 6530 ip_fill_mtuinfo(conn_t *connp, ip_xmit_attr_t *ixa, struct ip6_mtuinfo *mtuinfo)
6529 6531 {
6530 6532 uint32_t pmtu = IP_MAXPACKET;
6531 6533 uint_t scopeid;
6532 6534
6533 6535 if (IN6_IS_ADDR_UNSPECIFIED(&connp->conn_faddr_v6))
6534 6536 return (-1);
6535 6537
6536 6538 /* In case we never sent or called ip_set_destination_v4/v6 */
6537 6539 if (ixa->ixa_ire != NULL)
6538 6540 pmtu = ip_get_pmtu(ixa);
6539 6541
6540 6542 if (ixa->ixa_flags & IXAF_SCOPEID_SET)
6541 6543 scopeid = ixa->ixa_scopeid;
6542 6544 else
6543 6545 scopeid = 0;
6544 6546
6545 6547 bzero(mtuinfo, sizeof (*mtuinfo));
6546 6548 mtuinfo->ip6m_addr.sin6_family = AF_INET6;
6547 6549 mtuinfo->ip6m_addr.sin6_port = connp->conn_fport;
6548 6550 mtuinfo->ip6m_addr.sin6_addr = connp->conn_faddr_v6;
6549 6551 mtuinfo->ip6m_addr.sin6_scope_id = scopeid;
6550 6552 mtuinfo->ip6m_mtu = pmtu;
6551 6553
6552 6554 return (sizeof (struct ip6_mtuinfo));
6553 6555 }
6554 6556
6555 6557 /*
6556 6558 * When the src multihoming is changed from weak to [strong, preferred]
6557 6559 * ip_ire_rebind_walker is called to walk the list of all ire_t entries
6558 6560 * and identify routes that were created by user-applications in the
6559 6561 * unbound state (i.e., without RTA_IFP), and for which an ire_ill is not
6560 6562 * currently defined. These routes are then 'rebound', i.e., their ire_ill
6561 6563 * is selected by finding an interface route for the gateway.
6562 6564 */
6563 6565 /* ARGSUSED */
6564 6566 void
6565 6567 ip_ire_rebind_walker(ire_t *ire, void *notused)
6566 6568 {
6567 6569 if (!ire->ire_unbound || ire->ire_ill != NULL)
6568 6570 return;
6569 6571 ire_rebind(ire);
6570 6572 ire_delete(ire);
6571 6573 }
6572 6574
6573 6575 /*
6574 6576 * When the src multihoming is changed from [strong, preferred] to weak,
6575 6577 * ip_ire_unbind_walker is called to walk the list of all ire_t entries, and
6576 6578 * set any entries that were created by user-applications in the unbound state
6577 6579 * (i.e., without RTA_IFP) back to having a NULL ire_ill.
6578 6580 */
6579 6581 /* ARGSUSED */
6580 6582 void
6581 6583 ip_ire_unbind_walker(ire_t *ire, void *notused)
6582 6584 {
6583 6585 ire_t *new_ire;
6584 6586
6585 6587 if (!ire->ire_unbound || ire->ire_ill == NULL)
6586 6588 return;
6587 6589 if (ire->ire_ipversion == IPV6_VERSION) {
6588 6590 new_ire = ire_create_v6(&ire->ire_addr_v6, &ire->ire_mask_v6,
6589 6591 &ire->ire_gateway_addr_v6, ire->ire_type, NULL,
6590 6592 ire->ire_zoneid, ire->ire_flags, NULL, ire->ire_ipst);
6591 6593 } else {
6592 6594 new_ire = ire_create((uchar_t *)&ire->ire_addr,
6593 6595 (uchar_t *)&ire->ire_mask,
6594 6596 (uchar_t *)&ire->ire_gateway_addr, ire->ire_type, NULL,
6595 6597 ire->ire_zoneid, ire->ire_flags, NULL, ire->ire_ipst);
6596 6598 }
6597 6599 if (new_ire == NULL)
6598 6600 return;
6599 6601 new_ire->ire_unbound = B_TRUE;
6600 6602 /*
6601 6603 * The bound ire must first be deleted so that we don't return
6602 6604 * the existing one on the attempt to add the unbound new_ire.
6603 6605 */
6604 6606 ire_delete(ire);
6605 6607 new_ire = ire_add(new_ire);
6606 6608 if (new_ire != NULL)
6607 6609 ire_refrele(new_ire);
6608 6610 }
6609 6611
6610 6612 /*
6611 6613 * When the settings of ip*_strict_src_multihoming tunables are changed,
6612 6614 * all cached routes need to be recomputed. This recomputation needs to be
6613 6615 * done when going from weaker to stronger modes so that the cached ire
6614 6616 * for the connection does not violate the current ip*_strict_src_multihoming
6615 6617 * setting. It also needs to be done when going from stronger to weaker modes,
6616 6618 * so that we fall back to matching on the longest-matching-route (as opposed
6617 6619 * to a shorter match that may have been selected in the strong mode
6618 6620 * to satisfy src_multihoming settings).
6619 6621 *
6620 6622 * The cached ixa_ire entires for all conn_t entries are marked as
6621 6623 * "verify" so that they will be recomputed for the next packet.
6622 6624 */
6623 6625 void
6624 6626 conn_ire_revalidate(conn_t *connp, void *arg)
6625 6627 {
6626 6628 boolean_t isv6 = (boolean_t)arg;
6627 6629
6628 6630 if ((isv6 && connp->conn_ipversion != IPV6_VERSION) ||
6629 6631 (!isv6 && connp->conn_ipversion != IPV4_VERSION))
6630 6632 return;
6631 6633 connp->conn_ixa->ixa_ire_generation = IRE_GENERATION_VERIFY;
6632 6634 }
6633 6635
6634 6636 /*
6635 6637 * Handles both IPv4 and IPv6 reassembly - doing the out-of-order cases,
6636 6638 * When an ipf is passed here for the first time, if
6637 6639 * we already have in-order fragments on the queue, we convert from the fast-
6638 6640 * path reassembly scheme to the hard-case scheme. From then on, additional
6639 6641 * fragments are reassembled here. We keep track of the start and end offsets
6640 6642 * of each piece, and the number of holes in the chain. When the hole count
6641 6643 * goes to zero, we are done!
6642 6644 *
6643 6645 * The ipf_count will be updated to account for any mblk(s) added (pointed to
6644 6646 * by mp) or subtracted (freeb()ed dups), upon return the caller must update
6645 6647 * ipfb_count and ill_frag_count by the difference of ipf_count before and
6646 6648 * after the call to ip_reassemble().
6647 6649 */
6648 6650 int
6649 6651 ip_reassemble(mblk_t *mp, ipf_t *ipf, uint_t start, boolean_t more, ill_t *ill,
6650 6652 size_t msg_len)
6651 6653 {
6652 6654 uint_t end;
6653 6655 mblk_t *next_mp;
6654 6656 mblk_t *mp1;
6655 6657 uint_t offset;
6656 6658 boolean_t incr_dups = B_TRUE;
6657 6659 boolean_t offset_zero_seen = B_FALSE;
6658 6660 boolean_t pkt_boundary_checked = B_FALSE;
6659 6661
6660 6662 /* If start == 0 then ipf_nf_hdr_len has to be set. */
6661 6663 ASSERT(start != 0 || ipf->ipf_nf_hdr_len != 0);
6662 6664
6663 6665 /* Add in byte count */
6664 6666 ipf->ipf_count += msg_len;
6665 6667 if (ipf->ipf_end) {
6666 6668 /*
6667 6669 * We were part way through in-order reassembly, but now there
6668 6670 * is a hole. We walk through messages already queued, and
6669 6671 * mark them for hard case reassembly. We know that up till
6670 6672 * now they were in order starting from offset zero.
6671 6673 */
6672 6674 offset = 0;
6673 6675 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) {
6674 6676 IP_REASS_SET_START(mp1, offset);
6675 6677 if (offset == 0) {
6676 6678 ASSERT(ipf->ipf_nf_hdr_len != 0);
6677 6679 offset = -ipf->ipf_nf_hdr_len;
6678 6680 }
6679 6681 offset += mp1->b_wptr - mp1->b_rptr;
6680 6682 IP_REASS_SET_END(mp1, offset);
6681 6683 }
6682 6684 /* One hole at the end. */
6683 6685 ipf->ipf_hole_cnt = 1;
6684 6686 /* Brand it as a hard case, forever. */
6685 6687 ipf->ipf_end = 0;
6686 6688 }
6687 6689 /* Walk through all the new pieces. */
6688 6690 do {
6689 6691 end = start + (mp->b_wptr - mp->b_rptr);
6690 6692 /*
6691 6693 * If start is 0, decrease 'end' only for the first mblk of
6692 6694 * the fragment. Otherwise 'end' can get wrong value in the
6693 6695 * second pass of the loop if first mblk is exactly the
6694 6696 * size of ipf_nf_hdr_len.
6695 6697 */
6696 6698 if (start == 0 && !offset_zero_seen) {
6697 6699 /* First segment */
6698 6700 ASSERT(ipf->ipf_nf_hdr_len != 0);
6699 6701 end -= ipf->ipf_nf_hdr_len;
6700 6702 offset_zero_seen = B_TRUE;
6701 6703 }
6702 6704 next_mp = mp->b_cont;
6703 6705 /*
6704 6706 * We are checking to see if there is any interesing data
6705 6707 * to process. If there isn't and the mblk isn't the
6706 6708 * one which carries the unfragmentable header then we
6707 6709 * drop it. It's possible to have just the unfragmentable
6708 6710 * header come through without any data. That needs to be
6709 6711 * saved.
6710 6712 *
6711 6713 * If the assert at the top of this function holds then the
6712 6714 * term "ipf->ipf_nf_hdr_len != 0" isn't needed. This code
6713 6715 * is infrequently traveled enough that the test is left in
6714 6716 * to protect against future code changes which break that
6715 6717 * invariant.
6716 6718 */
6717 6719 if (start == end && start != 0 && ipf->ipf_nf_hdr_len != 0) {
6718 6720 /* Empty. Blast it. */
6719 6721 IP_REASS_SET_START(mp, 0);
6720 6722 IP_REASS_SET_END(mp, 0);
6721 6723 /*
6722 6724 * If the ipf points to the mblk we are about to free,
6723 6725 * update ipf to point to the next mblk (or NULL
6724 6726 * if none).
6725 6727 */
6726 6728 if (ipf->ipf_mp->b_cont == mp)
6727 6729 ipf->ipf_mp->b_cont = next_mp;
6728 6730 freeb(mp);
6729 6731 continue;
6730 6732 }
6731 6733 mp->b_cont = NULL;
6732 6734 IP_REASS_SET_START(mp, start);
6733 6735 IP_REASS_SET_END(mp, end);
6734 6736 if (!ipf->ipf_tail_mp) {
6735 6737 ipf->ipf_tail_mp = mp;
6736 6738 ipf->ipf_mp->b_cont = mp;
6737 6739 if (start == 0 || !more) {
6738 6740 ipf->ipf_hole_cnt = 1;
6739 6741 /*
6740 6742 * if the first fragment comes in more than one
6741 6743 * mblk, this loop will be executed for each
6742 6744 * mblk. Need to adjust hole count so exiting
6743 6745 * this routine will leave hole count at 1.
6744 6746 */
6745 6747 if (next_mp)
6746 6748 ipf->ipf_hole_cnt++;
6747 6749 } else
6748 6750 ipf->ipf_hole_cnt = 2;
6749 6751 continue;
6750 6752 } else if (ipf->ipf_last_frag_seen && !more &&
6751 6753 !pkt_boundary_checked) {
6752 6754 /*
6753 6755 * We check datagram boundary only if this fragment
6754 6756 * claims to be the last fragment and we have seen a
6755 6757 * last fragment in the past too. We do this only
6756 6758 * once for a given fragment.
6757 6759 *
6758 6760 * start cannot be 0 here as fragments with start=0
6759 6761 * and MF=0 gets handled as a complete packet. These
6760 6762 * fragments should not reach here.
6761 6763 */
6762 6764
6763 6765 if (start + msgdsize(mp) !=
6764 6766 IP_REASS_END(ipf->ipf_tail_mp)) {
6765 6767 /*
6766 6768 * We have two fragments both of which claim
6767 6769 * to be the last fragment but gives conflicting
6768 6770 * information about the whole datagram size.
6769 6771 * Something fishy is going on. Drop the
6770 6772 * fragment and free up the reassembly list.
6771 6773 */
6772 6774 return (IP_REASS_FAILED);
6773 6775 }
6774 6776
6775 6777 /*
6776 6778 * We shouldn't come to this code block again for this
6777 6779 * particular fragment.
6778 6780 */
6779 6781 pkt_boundary_checked = B_TRUE;
6780 6782 }
6781 6783
6782 6784 /* New stuff at or beyond tail? */
6783 6785 offset = IP_REASS_END(ipf->ipf_tail_mp);
6784 6786 if (start >= offset) {
6785 6787 if (ipf->ipf_last_frag_seen) {
6786 6788 /* current fragment is beyond last fragment */
6787 6789 return (IP_REASS_FAILED);
6788 6790 }
6789 6791 /* Link it on end. */
6790 6792 ipf->ipf_tail_mp->b_cont = mp;
6791 6793 ipf->ipf_tail_mp = mp;
6792 6794 if (more) {
6793 6795 if (start != offset)
6794 6796 ipf->ipf_hole_cnt++;
6795 6797 } else if (start == offset && next_mp == NULL)
6796 6798 ipf->ipf_hole_cnt--;
6797 6799 continue;
6798 6800 }
6799 6801 mp1 = ipf->ipf_mp->b_cont;
6800 6802 offset = IP_REASS_START(mp1);
6801 6803 /* New stuff at the front? */
6802 6804 if (start < offset) {
6803 6805 if (start == 0) {
6804 6806 if (end >= offset) {
6805 6807 /* Nailed the hole at the begining. */
6806 6808 ipf->ipf_hole_cnt--;
6807 6809 }
6808 6810 } else if (end < offset) {
6809 6811 /*
6810 6812 * A hole, stuff, and a hole where there used
6811 6813 * to be just a hole.
6812 6814 */
6813 6815 ipf->ipf_hole_cnt++;
6814 6816 }
6815 6817 mp->b_cont = mp1;
6816 6818 /* Check for overlap. */
6817 6819 while (end > offset) {
6818 6820 if (end < IP_REASS_END(mp1)) {
6819 6821 mp->b_wptr -= end - offset;
6820 6822 IP_REASS_SET_END(mp, offset);
6821 6823 BUMP_MIB(ill->ill_ip_mib,
6822 6824 ipIfStatsReasmPartDups);
6823 6825 break;
6824 6826 }
6825 6827 /* Did we cover another hole? */
6826 6828 if ((mp1->b_cont &&
6827 6829 IP_REASS_END(mp1) !=
6828 6830 IP_REASS_START(mp1->b_cont) &&
6829 6831 end >= IP_REASS_START(mp1->b_cont)) ||
6830 6832 (!ipf->ipf_last_frag_seen && !more)) {
6831 6833 ipf->ipf_hole_cnt--;
6832 6834 }
6833 6835 /* Clip out mp1. */
6834 6836 if ((mp->b_cont = mp1->b_cont) == NULL) {
6835 6837 /*
6836 6838 * After clipping out mp1, this guy
6837 6839 * is now hanging off the end.
6838 6840 */
6839 6841 ipf->ipf_tail_mp = mp;
6840 6842 }
6841 6843 IP_REASS_SET_START(mp1, 0);
6842 6844 IP_REASS_SET_END(mp1, 0);
6843 6845 /* Subtract byte count */
6844 6846 ipf->ipf_count -= mp1->b_datap->db_lim -
6845 6847 mp1->b_datap->db_base;
6846 6848 freeb(mp1);
6847 6849 BUMP_MIB(ill->ill_ip_mib,
6848 6850 ipIfStatsReasmPartDups);
6849 6851 mp1 = mp->b_cont;
6850 6852 if (!mp1)
6851 6853 break;
6852 6854 offset = IP_REASS_START(mp1);
6853 6855 }
6854 6856 ipf->ipf_mp->b_cont = mp;
6855 6857 continue;
6856 6858 }
6857 6859 /*
6858 6860 * The new piece starts somewhere between the start of the head
6859 6861 * and before the end of the tail.
6860 6862 */
6861 6863 for (; mp1; mp1 = mp1->b_cont) {
6862 6864 offset = IP_REASS_END(mp1);
6863 6865 if (start < offset) {
6864 6866 if (end <= offset) {
6865 6867 /* Nothing new. */
6866 6868 IP_REASS_SET_START(mp, 0);
6867 6869 IP_REASS_SET_END(mp, 0);
6868 6870 /* Subtract byte count */
6869 6871 ipf->ipf_count -= mp->b_datap->db_lim -
6870 6872 mp->b_datap->db_base;
6871 6873 if (incr_dups) {
6872 6874 ipf->ipf_num_dups++;
6873 6875 incr_dups = B_FALSE;
6874 6876 }
6875 6877 freeb(mp);
6876 6878 BUMP_MIB(ill->ill_ip_mib,
6877 6879 ipIfStatsReasmDuplicates);
6878 6880 break;
6879 6881 }
6880 6882 /*
6881 6883 * Trim redundant stuff off beginning of new
6882 6884 * piece.
6883 6885 */
6884 6886 IP_REASS_SET_START(mp, offset);
6885 6887 mp->b_rptr += offset - start;
6886 6888 BUMP_MIB(ill->ill_ip_mib,
6887 6889 ipIfStatsReasmPartDups);
6888 6890 start = offset;
6889 6891 if (!mp1->b_cont) {
6890 6892 /*
6891 6893 * After trimming, this guy is now
6892 6894 * hanging off the end.
6893 6895 */
6894 6896 mp1->b_cont = mp;
6895 6897 ipf->ipf_tail_mp = mp;
6896 6898 if (!more) {
6897 6899 ipf->ipf_hole_cnt--;
6898 6900 }
6899 6901 break;
6900 6902 }
6901 6903 }
6902 6904 if (start >= IP_REASS_START(mp1->b_cont))
6903 6905 continue;
6904 6906 /* Fill a hole */
6905 6907 if (start > offset)
6906 6908 ipf->ipf_hole_cnt++;
6907 6909 mp->b_cont = mp1->b_cont;
6908 6910 mp1->b_cont = mp;
6909 6911 mp1 = mp->b_cont;
6910 6912 offset = IP_REASS_START(mp1);
6911 6913 if (end >= offset) {
6912 6914 ipf->ipf_hole_cnt--;
6913 6915 /* Check for overlap. */
6914 6916 while (end > offset) {
6915 6917 if (end < IP_REASS_END(mp1)) {
6916 6918 mp->b_wptr -= end - offset;
6917 6919 IP_REASS_SET_END(mp, offset);
6918 6920 /*
6919 6921 * TODO we might bump
6920 6922 * this up twice if there is
6921 6923 * overlap at both ends.
6922 6924 */
6923 6925 BUMP_MIB(ill->ill_ip_mib,
6924 6926 ipIfStatsReasmPartDups);
6925 6927 break;
6926 6928 }
6927 6929 /* Did we cover another hole? */
6928 6930 if ((mp1->b_cont &&
6929 6931 IP_REASS_END(mp1)
6930 6932 != IP_REASS_START(mp1->b_cont) &&
6931 6933 end >=
6932 6934 IP_REASS_START(mp1->b_cont)) ||
6933 6935 (!ipf->ipf_last_frag_seen &&
6934 6936 !more)) {
6935 6937 ipf->ipf_hole_cnt--;
6936 6938 }
6937 6939 /* Clip out mp1. */
6938 6940 if ((mp->b_cont = mp1->b_cont) ==
6939 6941 NULL) {
6940 6942 /*
6941 6943 * After clipping out mp1,
6942 6944 * this guy is now hanging
6943 6945 * off the end.
6944 6946 */
6945 6947 ipf->ipf_tail_mp = mp;
6946 6948 }
6947 6949 IP_REASS_SET_START(mp1, 0);
6948 6950 IP_REASS_SET_END(mp1, 0);
6949 6951 /* Subtract byte count */
6950 6952 ipf->ipf_count -=
6951 6953 mp1->b_datap->db_lim -
6952 6954 mp1->b_datap->db_base;
6953 6955 freeb(mp1);
6954 6956 BUMP_MIB(ill->ill_ip_mib,
6955 6957 ipIfStatsReasmPartDups);
6956 6958 mp1 = mp->b_cont;
6957 6959 if (!mp1)
6958 6960 break;
6959 6961 offset = IP_REASS_START(mp1);
6960 6962 }
6961 6963 }
6962 6964 break;
6963 6965 }
6964 6966 } while (start = end, mp = next_mp);
6965 6967
6966 6968 /* Fragment just processed could be the last one. Remember this fact */
6967 6969 if (!more)
6968 6970 ipf->ipf_last_frag_seen = B_TRUE;
6969 6971
6970 6972 /* Still got holes? */
6971 6973 if (ipf->ipf_hole_cnt)
6972 6974 return (IP_REASS_PARTIAL);
6973 6975 /* Clean up overloaded fields to avoid upstream disasters. */
6974 6976 for (mp1 = ipf->ipf_mp->b_cont; mp1; mp1 = mp1->b_cont) {
6975 6977 IP_REASS_SET_START(mp1, 0);
6976 6978 IP_REASS_SET_END(mp1, 0);
6977 6979 }
6978 6980 return (IP_REASS_COMPLETE);
6979 6981 }
6980 6982
6981 6983 /*
6982 6984 * Fragmentation reassembly. Each ILL has a hash table for
6983 6985 * queuing packets undergoing reassembly for all IPIFs
6984 6986 * associated with the ILL. The hash is based on the packet
6985 6987 * IP ident field. The ILL frag hash table was allocated
6986 6988 * as a timer block at the time the ILL was created. Whenever
6987 6989 * there is anything on the reassembly queue, the timer will
6988 6990 * be running. Returns the reassembled packet if reassembly completes.
6989 6991 */
6990 6992 mblk_t *
6991 6993 ip_input_fragment(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
6992 6994 {
6993 6995 uint32_t frag_offset_flags;
6994 6996 mblk_t *t_mp;
6995 6997 ipaddr_t dst;
6996 6998 uint8_t proto = ipha->ipha_protocol;
6997 6999 uint32_t sum_val;
6998 7000 uint16_t sum_flags;
6999 7001 ipf_t *ipf;
7000 7002 ipf_t **ipfp;
7001 7003 ipfb_t *ipfb;
7002 7004 uint16_t ident;
7003 7005 uint32_t offset;
7004 7006 ipaddr_t src;
7005 7007 uint_t hdr_length;
7006 7008 uint32_t end;
7007 7009 mblk_t *mp1;
7008 7010 mblk_t *tail_mp;
7009 7011 size_t count;
7010 7012 size_t msg_len;
7011 7013 uint8_t ecn_info = 0;
7012 7014 uint32_t packet_size;
7013 7015 boolean_t pruned = B_FALSE;
7014 7016 ill_t *ill = ira->ira_ill;
7015 7017 ip_stack_t *ipst = ill->ill_ipst;
7016 7018
7017 7019 /*
7018 7020 * Drop the fragmented as early as possible, if
7019 7021 * we don't have resource(s) to re-assemble.
7020 7022 */
7021 7023 if (ipst->ips_ip_reass_queue_bytes == 0) {
7022 7024 freemsg(mp);
7023 7025 return (NULL);
7024 7026 }
7025 7027
7026 7028 /* Check for fragmentation offset; return if there's none */
7027 7029 if ((frag_offset_flags = ntohs(ipha->ipha_fragment_offset_and_flags) &
7028 7030 (IPH_MF | IPH_OFFSET)) == 0)
7029 7031 return (mp);
7030 7032
7031 7033 /*
7032 7034 * We utilize hardware computed checksum info only for UDP since
7033 7035 * IP fragmentation is a normal occurrence for the protocol. In
7034 7036 * addition, checksum offload support for IP fragments carrying
7035 7037 * UDP payload is commonly implemented across network adapters.
7036 7038 */
7037 7039 ASSERT(ira->ira_rill != NULL);
7038 7040 if (proto == IPPROTO_UDP && dohwcksum &&
7039 7041 ILL_HCKSUM_CAPABLE(ira->ira_rill) &&
7040 7042 (DB_CKSUMFLAGS(mp) & (HCK_FULLCKSUM | HCK_PARTIALCKSUM))) {
7041 7043 mblk_t *mp1 = mp->b_cont;
7042 7044 int32_t len;
7043 7045
7044 7046 /* Record checksum information from the packet */
7045 7047 sum_val = (uint32_t)DB_CKSUM16(mp);
7046 7048 sum_flags = DB_CKSUMFLAGS(mp);
7047 7049
7048 7050 /* IP payload offset from beginning of mblk */
7049 7051 offset = ((uchar_t *)ipha + IPH_HDR_LENGTH(ipha)) - mp->b_rptr;
7050 7052
7051 7053 if ((sum_flags & HCK_PARTIALCKSUM) &&
7052 7054 (mp1 == NULL || mp1->b_cont == NULL) &&
7053 7055 offset >= DB_CKSUMSTART(mp) &&
7054 7056 ((len = offset - DB_CKSUMSTART(mp)) & 1) == 0) {
7055 7057 uint32_t adj;
7056 7058 /*
7057 7059 * Partial checksum has been calculated by hardware
7058 7060 * and attached to the packet; in addition, any
7059 7061 * prepended extraneous data is even byte aligned.
7060 7062 * If any such data exists, we adjust the checksum;
7061 7063 * this would also handle any postpended data.
7062 7064 */
7063 7065 IP_ADJCKSUM_PARTIAL(mp->b_rptr + DB_CKSUMSTART(mp),
7064 7066 mp, mp1, len, adj);
7065 7067
7066 7068 /* One's complement subtract extraneous checksum */
7067 7069 if (adj >= sum_val)
7068 7070 sum_val = ~(adj - sum_val) & 0xFFFF;
7069 7071 else
7070 7072 sum_val -= adj;
7071 7073 }
7072 7074 } else {
7073 7075 sum_val = 0;
7074 7076 sum_flags = 0;
7075 7077 }
7076 7078
7077 7079 /* Clear hardware checksumming flag */
7078 7080 DB_CKSUMFLAGS(mp) = 0;
7079 7081
7080 7082 ident = ipha->ipha_ident;
7081 7083 offset = (frag_offset_flags << 3) & 0xFFFF;
7082 7084 src = ipha->ipha_src;
7083 7085 dst = ipha->ipha_dst;
7084 7086 hdr_length = IPH_HDR_LENGTH(ipha);
7085 7087 end = ntohs(ipha->ipha_length) - hdr_length;
7086 7088
7087 7089 /* If end == 0 then we have a packet with no data, so just free it */
7088 7090 if (end == 0) {
7089 7091 freemsg(mp);
7090 7092 return (NULL);
7091 7093 }
7092 7094
7093 7095 /* Record the ECN field info. */
7094 7096 ecn_info = (ipha->ipha_type_of_service & 0x3);
7095 7097 if (offset != 0) {
7096 7098 /*
7097 7099 * If this isn't the first piece, strip the header, and
7098 7100 * add the offset to the end value.
7099 7101 */
7100 7102 mp->b_rptr += hdr_length;
7101 7103 end += offset;
7102 7104 }
7103 7105
7104 7106 /* Handle vnic loopback of fragments */
7105 7107 if (mp->b_datap->db_ref > 2)
7106 7108 msg_len = 0;
7107 7109 else
7108 7110 msg_len = MBLKSIZE(mp);
7109 7111
7110 7112 tail_mp = mp;
7111 7113 while (tail_mp->b_cont != NULL) {
7112 7114 tail_mp = tail_mp->b_cont;
7113 7115 if (tail_mp->b_datap->db_ref <= 2)
7114 7116 msg_len += MBLKSIZE(tail_mp);
7115 7117 }
7116 7118
7117 7119 /* If the reassembly list for this ILL will get too big, prune it */
7118 7120 if ((msg_len + sizeof (*ipf) + ill->ill_frag_count) >=
7119 7121 ipst->ips_ip_reass_queue_bytes) {
7120 7122 DTRACE_PROBE3(ip_reass_queue_bytes, uint_t, msg_len,
7121 7123 uint_t, ill->ill_frag_count,
7122 7124 uint_t, ipst->ips_ip_reass_queue_bytes);
7123 7125 ill_frag_prune(ill,
7124 7126 (ipst->ips_ip_reass_queue_bytes < msg_len) ? 0 :
7125 7127 (ipst->ips_ip_reass_queue_bytes - msg_len));
7126 7128 pruned = B_TRUE;
7127 7129 }
7128 7130
7129 7131 ipfb = &ill->ill_frag_hash_tbl[ILL_FRAG_HASH(src, ident)];
7130 7132 mutex_enter(&ipfb->ipfb_lock);
7131 7133
7132 7134 ipfp = &ipfb->ipfb_ipf;
7133 7135 /* Try to find an existing fragment queue for this packet. */
7134 7136 for (;;) {
7135 7137 ipf = ipfp[0];
7136 7138 if (ipf != NULL) {
7137 7139 /*
7138 7140 * It has to match on ident and src/dst address.
7139 7141 */
7140 7142 if (ipf->ipf_ident == ident &&
7141 7143 ipf->ipf_src == src &&
7142 7144 ipf->ipf_dst == dst &&
7143 7145 ipf->ipf_protocol == proto) {
7144 7146 /*
7145 7147 * If we have received too many
7146 7148 * duplicate fragments for this packet
7147 7149 * free it.
7148 7150 */
7149 7151 if (ipf->ipf_num_dups > ip_max_frag_dups) {
7150 7152 ill_frag_free_pkts(ill, ipfb, ipf, 1);
7151 7153 freemsg(mp);
7152 7154 mutex_exit(&ipfb->ipfb_lock);
7153 7155 return (NULL);
7154 7156 }
7155 7157 /* Found it. */
7156 7158 break;
7157 7159 }
7158 7160 ipfp = &ipf->ipf_hash_next;
7159 7161 continue;
7160 7162 }
7161 7163
7162 7164 /*
7163 7165 * If we pruned the list, do we want to store this new
7164 7166 * fragment?. We apply an optimization here based on the
7165 7167 * fact that most fragments will be received in order.
7166 7168 * So if the offset of this incoming fragment is zero,
7167 7169 * it is the first fragment of a new packet. We will
7168 7170 * keep it. Otherwise drop the fragment, as we have
7169 7171 * probably pruned the packet already (since the
7170 7172 * packet cannot be found).
7171 7173 */
7172 7174 if (pruned && offset != 0) {
7173 7175 mutex_exit(&ipfb->ipfb_lock);
7174 7176 freemsg(mp);
7175 7177 return (NULL);
7176 7178 }
7177 7179
7178 7180 if (ipfb->ipfb_frag_pkts >= MAX_FRAG_PKTS(ipst)) {
7179 7181 /*
7180 7182 * Too many fragmented packets in this hash
7181 7183 * bucket. Free the oldest.
7182 7184 */
7183 7185 ill_frag_free_pkts(ill, ipfb, ipfb->ipfb_ipf, 1);
7184 7186 }
7185 7187
7186 7188 /* New guy. Allocate a frag message. */
7187 7189 mp1 = allocb(sizeof (*ipf), BPRI_MED);
7188 7190 if (mp1 == NULL) {
7189 7191 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7190 7192 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7191 7193 freemsg(mp);
7192 7194 reass_done:
7193 7195 mutex_exit(&ipfb->ipfb_lock);
7194 7196 return (NULL);
7195 7197 }
7196 7198
7197 7199 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmReqds);
7198 7200 mp1->b_cont = mp;
7199 7201
7200 7202 /* Initialize the fragment header. */
7201 7203 ipf = (ipf_t *)mp1->b_rptr;
7202 7204 ipf->ipf_mp = mp1;
7203 7205 ipf->ipf_ptphn = ipfp;
7204 7206 ipfp[0] = ipf;
7205 7207 ipf->ipf_hash_next = NULL;
7206 7208 ipf->ipf_ident = ident;
7207 7209 ipf->ipf_protocol = proto;
7208 7210 ipf->ipf_src = src;
7209 7211 ipf->ipf_dst = dst;
7210 7212 ipf->ipf_nf_hdr_len = 0;
7211 7213 /* Record reassembly start time. */
7212 7214 ipf->ipf_timestamp = gethrestime_sec();
7213 7215 /* Record ipf generation and account for frag header */
7214 7216 ipf->ipf_gen = ill->ill_ipf_gen++;
7215 7217 ipf->ipf_count = MBLKSIZE(mp1);
7216 7218 ipf->ipf_last_frag_seen = B_FALSE;
7217 7219 ipf->ipf_ecn = ecn_info;
7218 7220 ipf->ipf_num_dups = 0;
7219 7221 ipfb->ipfb_frag_pkts++;
7220 7222 ipf->ipf_checksum = 0;
7221 7223 ipf->ipf_checksum_flags = 0;
7222 7224
7223 7225 /* Store checksum value in fragment header */
7224 7226 if (sum_flags != 0) {
7225 7227 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7226 7228 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7227 7229 ipf->ipf_checksum = sum_val;
7228 7230 ipf->ipf_checksum_flags = sum_flags;
7229 7231 }
7230 7232
7231 7233 /*
7232 7234 * We handle reassembly two ways. In the easy case,
7233 7235 * where all the fragments show up in order, we do
7234 7236 * minimal bookkeeping, and just clip new pieces on
7235 7237 * the end. If we ever see a hole, then we go off
7236 7238 * to ip_reassemble which has to mark the pieces and
7237 7239 * keep track of the number of holes, etc. Obviously,
7238 7240 * the point of having both mechanisms is so we can
7239 7241 * handle the easy case as efficiently as possible.
7240 7242 */
7241 7243 if (offset == 0) {
7242 7244 /* Easy case, in-order reassembly so far. */
7243 7245 ipf->ipf_count += msg_len;
7244 7246 ipf->ipf_tail_mp = tail_mp;
7245 7247 /*
7246 7248 * Keep track of next expected offset in
7247 7249 * ipf_end.
7248 7250 */
7249 7251 ipf->ipf_end = end;
7250 7252 ipf->ipf_nf_hdr_len = hdr_length;
7251 7253 } else {
7252 7254 /* Hard case, hole at the beginning. */
7253 7255 ipf->ipf_tail_mp = NULL;
7254 7256 /*
7255 7257 * ipf_end == 0 means that we have given up
7256 7258 * on easy reassembly.
7257 7259 */
7258 7260 ipf->ipf_end = 0;
7259 7261
7260 7262 /* Forget checksum offload from now on */
7261 7263 ipf->ipf_checksum_flags = 0;
7262 7264
7263 7265 /*
7264 7266 * ipf_hole_cnt is set by ip_reassemble.
7265 7267 * ipf_count is updated by ip_reassemble.
7266 7268 * No need to check for return value here
7267 7269 * as we don't expect reassembly to complete
7268 7270 * or fail for the first fragment itself.
7269 7271 */
7270 7272 (void) ip_reassemble(mp, ipf,
7271 7273 (frag_offset_flags & IPH_OFFSET) << 3,
7272 7274 (frag_offset_flags & IPH_MF), ill, msg_len);
7273 7275 }
7274 7276 /* Update per ipfb and ill byte counts */
7275 7277 ipfb->ipfb_count += ipf->ipf_count;
7276 7278 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7277 7279 atomic_add_32(&ill->ill_frag_count, ipf->ipf_count);
7278 7280 /* If the frag timer wasn't already going, start it. */
7279 7281 mutex_enter(&ill->ill_lock);
7280 7282 ill_frag_timer_start(ill);
7281 7283 mutex_exit(&ill->ill_lock);
7282 7284 goto reass_done;
7283 7285 }
7284 7286
7285 7287 /*
7286 7288 * If the packet's flag has changed (it could be coming up
7287 7289 * from an interface different than the previous, therefore
7288 7290 * possibly different checksum capability), then forget about
7289 7291 * any stored checksum states. Otherwise add the value to
7290 7292 * the existing one stored in the fragment header.
7291 7293 */
7292 7294 if (sum_flags != 0 && sum_flags == ipf->ipf_checksum_flags) {
7293 7295 sum_val += ipf->ipf_checksum;
7294 7296 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7295 7297 sum_val = (sum_val & 0xFFFF) + (sum_val >> 16);
7296 7298 ipf->ipf_checksum = sum_val;
7297 7299 } else if (ipf->ipf_checksum_flags != 0) {
7298 7300 /* Forget checksum offload from now on */
7299 7301 ipf->ipf_checksum_flags = 0;
7300 7302 }
7301 7303
7302 7304 /*
7303 7305 * We have a new piece of a datagram which is already being
7304 7306 * reassembled. Update the ECN info if all IP fragments
7305 7307 * are ECN capable. If there is one which is not, clear
7306 7308 * all the info. If there is at least one which has CE
7307 7309 * code point, IP needs to report that up to transport.
7308 7310 */
7309 7311 if (ecn_info != IPH_ECN_NECT && ipf->ipf_ecn != IPH_ECN_NECT) {
7310 7312 if (ecn_info == IPH_ECN_CE)
7311 7313 ipf->ipf_ecn = IPH_ECN_CE;
7312 7314 } else {
7313 7315 ipf->ipf_ecn = IPH_ECN_NECT;
7314 7316 }
7315 7317 if (offset && ipf->ipf_end == offset) {
7316 7318 /* The new fragment fits at the end */
7317 7319 ipf->ipf_tail_mp->b_cont = mp;
7318 7320 /* Update the byte count */
7319 7321 ipf->ipf_count += msg_len;
7320 7322 /* Update per ipfb and ill byte counts */
7321 7323 ipfb->ipfb_count += msg_len;
7322 7324 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7323 7325 atomic_add_32(&ill->ill_frag_count, msg_len);
7324 7326 if (frag_offset_flags & IPH_MF) {
7325 7327 /* More to come. */
7326 7328 ipf->ipf_end = end;
7327 7329 ipf->ipf_tail_mp = tail_mp;
7328 7330 goto reass_done;
7329 7331 }
7330 7332 } else {
7331 7333 /* Go do the hard cases. */
7332 7334 int ret;
7333 7335
7334 7336 if (offset == 0)
7335 7337 ipf->ipf_nf_hdr_len = hdr_length;
7336 7338
7337 7339 /* Save current byte count */
7338 7340 count = ipf->ipf_count;
7339 7341 ret = ip_reassemble(mp, ipf,
7340 7342 (frag_offset_flags & IPH_OFFSET) << 3,
7341 7343 (frag_offset_flags & IPH_MF), ill, msg_len);
7342 7344 /* Count of bytes added and subtracted (freeb()ed) */
7343 7345 count = ipf->ipf_count - count;
7344 7346 if (count) {
7345 7347 /* Update per ipfb and ill byte counts */
7346 7348 ipfb->ipfb_count += count;
7347 7349 ASSERT(ipfb->ipfb_count > 0); /* Wraparound */
7348 7350 atomic_add_32(&ill->ill_frag_count, count);
7349 7351 }
7350 7352 if (ret == IP_REASS_PARTIAL) {
7351 7353 goto reass_done;
7352 7354 } else if (ret == IP_REASS_FAILED) {
7353 7355 /* Reassembly failed. Free up all resources */
7354 7356 ill_frag_free_pkts(ill, ipfb, ipf, 1);
7355 7357 for (t_mp = mp; t_mp != NULL; t_mp = t_mp->b_cont) {
7356 7358 IP_REASS_SET_START(t_mp, 0);
7357 7359 IP_REASS_SET_END(t_mp, 0);
7358 7360 }
7359 7361 freemsg(mp);
7360 7362 goto reass_done;
7361 7363 }
7362 7364 /* We will reach here iff 'ret' is IP_REASS_COMPLETE */
7363 7365 }
7364 7366 /*
7365 7367 * We have completed reassembly. Unhook the frag header from
7366 7368 * the reassembly list.
7367 7369 *
7368 7370 * Before we free the frag header, record the ECN info
7369 7371 * to report back to the transport.
7370 7372 */
7371 7373 ecn_info = ipf->ipf_ecn;
7372 7374 BUMP_MIB(ill->ill_ip_mib, ipIfStatsReasmOKs);
7373 7375 ipfp = ipf->ipf_ptphn;
7374 7376
7375 7377 /* We need to supply these to caller */
7376 7378 if ((sum_flags = ipf->ipf_checksum_flags) != 0)
7377 7379 sum_val = ipf->ipf_checksum;
7378 7380 else
7379 7381 sum_val = 0;
7380 7382
7381 7383 mp1 = ipf->ipf_mp;
7382 7384 count = ipf->ipf_count;
7383 7385 ipf = ipf->ipf_hash_next;
7384 7386 if (ipf != NULL)
7385 7387 ipf->ipf_ptphn = ipfp;
7386 7388 ipfp[0] = ipf;
7387 7389 atomic_add_32(&ill->ill_frag_count, -count);
7388 7390 ASSERT(ipfb->ipfb_count >= count);
7389 7391 ipfb->ipfb_count -= count;
7390 7392 ipfb->ipfb_frag_pkts--;
7391 7393 mutex_exit(&ipfb->ipfb_lock);
7392 7394 /* Ditch the frag header. */
7393 7395 mp = mp1->b_cont;
7394 7396
7395 7397 freeb(mp1);
7396 7398
7397 7399 /* Restore original IP length in header. */
7398 7400 packet_size = (uint32_t)msgdsize(mp);
7399 7401 if (packet_size > IP_MAXPACKET) {
7400 7402 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7401 7403 ip_drop_input("Reassembled packet too large", mp, ill);
7402 7404 freemsg(mp);
7403 7405 return (NULL);
7404 7406 }
7405 7407
7406 7408 if (DB_REF(mp) > 1) {
7407 7409 mblk_t *mp2 = copymsg(mp);
7408 7410
7409 7411 if (mp2 == NULL) {
7410 7412 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7411 7413 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7412 7414 freemsg(mp);
7413 7415 return (NULL);
7414 7416 }
7415 7417 freemsg(mp);
7416 7418 mp = mp2;
7417 7419 }
7418 7420 ipha = (ipha_t *)mp->b_rptr;
7419 7421
7420 7422 ipha->ipha_length = htons((uint16_t)packet_size);
7421 7423 /* We're now complete, zip the frag state */
7422 7424 ipha->ipha_fragment_offset_and_flags = 0;
7423 7425 /* Record the ECN info. */
7424 7426 ipha->ipha_type_of_service &= 0xFC;
7425 7427 ipha->ipha_type_of_service |= ecn_info;
7426 7428
7427 7429 /* Update the receive attributes */
7428 7430 ira->ira_pktlen = packet_size;
7429 7431 ira->ira_ip_hdr_length = IPH_HDR_LENGTH(ipha);
7430 7432
7431 7433 /* Reassembly is successful; set checksum information in packet */
7432 7434 DB_CKSUM16(mp) = (uint16_t)sum_val;
7433 7435 DB_CKSUMFLAGS(mp) = sum_flags;
7434 7436 DB_CKSUMSTART(mp) = ira->ira_ip_hdr_length;
7435 7437
7436 7438 return (mp);
7437 7439 }
7438 7440
7439 7441 /*
7440 7442 * Pullup function that should be used for IP input in order to
7441 7443 * ensure we do not loose the L2 source address; we need the l2 source
7442 7444 * address for IP_RECVSLLA and for ndp_input.
7443 7445 *
7444 7446 * We return either NULL or b_rptr.
7445 7447 */
7446 7448 void *
7447 7449 ip_pullup(mblk_t *mp, ssize_t len, ip_recv_attr_t *ira)
7448 7450 {
7449 7451 ill_t *ill = ira->ira_ill;
7450 7452
7451 7453 if (ip_rput_pullups++ == 0) {
7452 7454 (void) mi_strlog(ill->ill_rq, 1, SL_ERROR|SL_TRACE,
7453 7455 "ip_pullup: %s forced us to "
7454 7456 " pullup pkt, hdr len %ld, hdr addr %p",
7455 7457 ill->ill_name, len, (void *)mp->b_rptr);
7456 7458 }
7457 7459 if (!(ira->ira_flags & IRAF_L2SRC_SET))
7458 7460 ip_setl2src(mp, ira, ira->ira_rill);
7459 7461 ASSERT(ira->ira_flags & IRAF_L2SRC_SET);
7460 7462 if (!pullupmsg(mp, len))
7461 7463 return (NULL);
7462 7464 else
7463 7465 return (mp->b_rptr);
7464 7466 }
7465 7467
7466 7468 /*
7467 7469 * Make sure ira_l2src has an address. If we don't have one fill with zeros.
7468 7470 * When called from the ULP ira_rill will be NULL hence the caller has to
7469 7471 * pass in the ill.
7470 7472 */
7471 7473 /* ARGSUSED */
7472 7474 void
7473 7475 ip_setl2src(mblk_t *mp, ip_recv_attr_t *ira, ill_t *ill)
7474 7476 {
7475 7477 const uchar_t *addr;
7476 7478 int alen;
7477 7479
7478 7480 if (ira->ira_flags & IRAF_L2SRC_SET)
7479 7481 return;
7480 7482
7481 7483 ASSERT(ill != NULL);
7482 7484 alen = ill->ill_phys_addr_length;
7483 7485 ASSERT(alen <= sizeof (ira->ira_l2src));
7484 7486 if (ira->ira_mhip != NULL &&
7485 7487 (addr = ira->ira_mhip->mhi_saddr) != NULL) {
7486 7488 bcopy(addr, ira->ira_l2src, alen);
7487 7489 } else if ((ira->ira_flags & IRAF_L2SRC_LOOPBACK) &&
7488 7490 (addr = ill->ill_phys_addr) != NULL) {
7489 7491 bcopy(addr, ira->ira_l2src, alen);
7490 7492 } else {
7491 7493 bzero(ira->ira_l2src, alen);
7492 7494 }
7493 7495 ira->ira_flags |= IRAF_L2SRC_SET;
7494 7496 }
7495 7497
7496 7498 /*
7497 7499 * check ip header length and align it.
7498 7500 */
7499 7501 mblk_t *
7500 7502 ip_check_and_align_header(mblk_t *mp, uint_t min_size, ip_recv_attr_t *ira)
7501 7503 {
7502 7504 ill_t *ill = ira->ira_ill;
7503 7505 ssize_t len;
7504 7506
7505 7507 len = MBLKL(mp);
7506 7508
7507 7509 if (!OK_32PTR(mp->b_rptr))
7508 7510 IP_STAT(ill->ill_ipst, ip_notaligned);
7509 7511 else
7510 7512 IP_STAT(ill->ill_ipst, ip_recv_pullup);
7511 7513
7512 7514 /* Guard against bogus device drivers */
7513 7515 if (len < 0) {
7514 7516 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7515 7517 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7516 7518 freemsg(mp);
7517 7519 return (NULL);
7518 7520 }
7519 7521
7520 7522 if (len == 0) {
7521 7523 /* GLD sometimes sends up mblk with b_rptr == b_wptr! */
7522 7524 mblk_t *mp1 = mp->b_cont;
7523 7525
7524 7526 if (!(ira->ira_flags & IRAF_L2SRC_SET))
7525 7527 ip_setl2src(mp, ira, ira->ira_rill);
7526 7528 ASSERT(ira->ira_flags & IRAF_L2SRC_SET);
7527 7529
7528 7530 freeb(mp);
7529 7531 mp = mp1;
7530 7532 if (mp == NULL)
7531 7533 return (NULL);
7532 7534
7533 7535 if (OK_32PTR(mp->b_rptr) && MBLKL(mp) >= min_size)
7534 7536 return (mp);
7535 7537 }
7536 7538 if (ip_pullup(mp, min_size, ira) == NULL) {
7537 7539 if (msgdsize(mp) < min_size) {
7538 7540 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7539 7541 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7540 7542 } else {
7541 7543 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7542 7544 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7543 7545 }
7544 7546 freemsg(mp);
7545 7547 return (NULL);
7546 7548 }
7547 7549 return (mp);
7548 7550 }
7549 7551
7550 7552 /*
7551 7553 * Common code for IPv4 and IPv6 to check and pullup multi-mblks
7552 7554 */
7553 7555 mblk_t *
7554 7556 ip_check_length(mblk_t *mp, uchar_t *rptr, ssize_t len, uint_t pkt_len,
7555 7557 uint_t min_size, ip_recv_attr_t *ira)
7556 7558 {
7557 7559 ill_t *ill = ira->ira_ill;
7558 7560
7559 7561 /*
7560 7562 * Make sure we have data length consistent
7561 7563 * with the IP header.
7562 7564 */
7563 7565 if (mp->b_cont == NULL) {
7564 7566 /* pkt_len is based on ipha_len, not the mblk length */
7565 7567 if (pkt_len < min_size) {
7566 7568 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7567 7569 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7568 7570 freemsg(mp);
7569 7571 return (NULL);
7570 7572 }
7571 7573 if (len < 0) {
7572 7574 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
7573 7575 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
7574 7576 freemsg(mp);
7575 7577 return (NULL);
7576 7578 }
7577 7579 /* Drop any pad */
7578 7580 mp->b_wptr = rptr + pkt_len;
7579 7581 } else if ((len += msgdsize(mp->b_cont)) != 0) {
7580 7582 ASSERT(pkt_len >= min_size);
7581 7583 if (pkt_len < min_size) {
7582 7584 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7583 7585 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7584 7586 freemsg(mp);
7585 7587 return (NULL);
7586 7588 }
7587 7589 if (len < 0) {
7588 7590 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
7589 7591 ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
7590 7592 freemsg(mp);
7591 7593 return (NULL);
7592 7594 }
7593 7595 /* Drop any pad */
7594 7596 (void) adjmsg(mp, -len);
7595 7597 /*
7596 7598 * adjmsg may have freed an mblk from the chain, hence
7597 7599 * invalidate any hw checksum here. This will force IP to
7598 7600 * calculate the checksum in sw, but only for this packet.
7599 7601 */
7600 7602 DB_CKSUMFLAGS(mp) = 0;
7601 7603 IP_STAT(ill->ill_ipst, ip_multimblk);
7602 7604 }
7603 7605 return (mp);
7604 7606 }
7605 7607
7606 7608 /*
7607 7609 * Check that the IPv4 opt_len is consistent with the packet and pullup
7608 7610 * the options.
7609 7611 */
7610 7612 mblk_t *
7611 7613 ip_check_optlen(mblk_t *mp, ipha_t *ipha, uint_t opt_len, uint_t pkt_len,
7612 7614 ip_recv_attr_t *ira)
7613 7615 {
7614 7616 ill_t *ill = ira->ira_ill;
7615 7617 ssize_t len;
7616 7618
7617 7619 /* Assume no IPv6 packets arrive over the IPv4 queue */
7618 7620 if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
7619 7621 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7620 7622 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInWrongIPVersion);
7621 7623 ip_drop_input("IPvN packet on IPv4 ill", mp, ill);
7622 7624 freemsg(mp);
7623 7625 return (NULL);
7624 7626 }
7625 7627
7626 7628 if (opt_len > (15 - IP_SIMPLE_HDR_LENGTH_IN_WORDS)) {
7627 7629 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7628 7630 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7629 7631 freemsg(mp);
7630 7632 return (NULL);
7631 7633 }
7632 7634 /*
7633 7635 * Recompute complete header length and make sure we
7634 7636 * have access to all of it.
7635 7637 */
7636 7638 len = ((size_t)opt_len + IP_SIMPLE_HDR_LENGTH_IN_WORDS) << 2;
7637 7639 if (len > (mp->b_wptr - mp->b_rptr)) {
7638 7640 if (len > pkt_len) {
7639 7641 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
7640 7642 ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
7641 7643 freemsg(mp);
7642 7644 return (NULL);
7643 7645 }
7644 7646 if (ip_pullup(mp, len, ira) == NULL) {
7645 7647 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
7646 7648 ip_drop_input("ipIfStatsInDiscards", mp, ill);
7647 7649 freemsg(mp);
7648 7650 return (NULL);
7649 7651 }
7650 7652 }
7651 7653 return (mp);
7652 7654 }
7653 7655
7654 7656 /*
7655 7657 * Returns a new ire, or the same ire, or NULL.
7656 7658 * If a different IRE is returned, then it is held; the caller
7657 7659 * needs to release it.
7658 7660 * In no case is there any hold/release on the ire argument.
7659 7661 */
7660 7662 ire_t *
7661 7663 ip_check_multihome(void *addr, ire_t *ire, ill_t *ill)
7662 7664 {
7663 7665 ire_t *new_ire;
7664 7666 ill_t *ire_ill;
7665 7667 uint_t ifindex;
7666 7668 ip_stack_t *ipst = ill->ill_ipst;
7667 7669 boolean_t strict_check = B_FALSE;
7668 7670
7669 7671 /*
7670 7672 * IPMP common case: if IRE and ILL are in the same group, there's no
7671 7673 * issue (e.g. packet received on an underlying interface matched an
7672 7674 * IRE_LOCAL on its associated group interface).
7673 7675 */
7674 7676 ASSERT(ire->ire_ill != NULL);
7675 7677 if (IS_IN_SAME_ILLGRP(ill, ire->ire_ill))
7676 7678 return (ire);
7677 7679
7678 7680 /*
7679 7681 * Do another ire lookup here, using the ingress ill, to see if the
7680 7682 * interface is in a usesrc group.
7681 7683 * As long as the ills belong to the same group, we don't consider
7682 7684 * them to be arriving on the wrong interface. Thus, if the switch
7683 7685 * is doing inbound load spreading, we won't drop packets when the
7684 7686 * ip*_strict_dst_multihoming switch is on.
7685 7687 * We also need to check for IPIF_UNNUMBERED point2point interfaces
7686 7688 * where the local address may not be unique. In this case we were
7687 7689 * at the mercy of the initial ire lookup and the IRE_LOCAL it
7688 7690 * actually returned. The new lookup, which is more specific, should
7689 7691 * only find the IRE_LOCAL associated with the ingress ill if one
7690 7692 * exists.
7691 7693 */
7692 7694 if (ire->ire_ipversion == IPV4_VERSION) {
7693 7695 if (ipst->ips_ip_strict_dst_multihoming)
7694 7696 strict_check = B_TRUE;
7695 7697 new_ire = ire_ftable_lookup_v4(*((ipaddr_t *)addr), 0, 0,
7696 7698 IRE_LOCAL, ill, ALL_ZONES, NULL,
7697 7699 (MATCH_IRE_TYPE|MATCH_IRE_ILL), 0, ipst, NULL);
7698 7700 } else {
7699 7701 ASSERT(!IN6_IS_ADDR_MULTICAST((in6_addr_t *)addr));
7700 7702 if (ipst->ips_ipv6_strict_dst_multihoming)
7701 7703 strict_check = B_TRUE;
7702 7704 new_ire = ire_ftable_lookup_v6((in6_addr_t *)addr, NULL, NULL,
7703 7705 IRE_LOCAL, ill, ALL_ZONES, NULL,
7704 7706 (MATCH_IRE_TYPE|MATCH_IRE_ILL), 0, ipst, NULL);
7705 7707 }
7706 7708 /*
7707 7709 * If the same ire that was returned in ip_input() is found then this
7708 7710 * is an indication that usesrc groups are in use. The packet
7709 7711 * arrived on a different ill in the group than the one associated with
7710 7712 * the destination address. If a different ire was found then the same
7711 7713 * IP address must be hosted on multiple ills. This is possible with
7712 7714 * unnumbered point2point interfaces. We switch to use this new ire in
7713 7715 * order to have accurate interface statistics.
7714 7716 */
7715 7717 if (new_ire != NULL) {
7716 7718 /* Note: held in one case but not the other? Caller handles */
7717 7719 if (new_ire != ire)
7718 7720 return (new_ire);
7719 7721 /* Unchanged */
7720 7722 ire_refrele(new_ire);
7721 7723 return (ire);
7722 7724 }
7723 7725
7724 7726 /*
7725 7727 * Chase pointers once and store locally.
7726 7728 */
7727 7729 ASSERT(ire->ire_ill != NULL);
7728 7730 ire_ill = ire->ire_ill;
7729 7731 ifindex = ill->ill_usesrc_ifindex;
7730 7732
7731 7733 /*
7732 7734 * Check if it's a legal address on the 'usesrc' interface.
7733 7735 * For IPMP data addresses the IRE_LOCAL is the upper, hence we
7734 7736 * can just check phyint_ifindex.
7735 7737 */
7736 7738 if (ifindex != 0 && ifindex == ire_ill->ill_phyint->phyint_ifindex) {
7737 7739 return (ire);
7738 7740 }
7739 7741
7740 7742 /*
7741 7743 * If the ip*_strict_dst_multihoming switch is on then we can
7742 7744 * only accept this packet if the interface is marked as routing.
7743 7745 */
7744 7746 if (!(strict_check))
7745 7747 return (ire);
7746 7748
7747 7749 if ((ill->ill_flags & ire->ire_ill->ill_flags & ILLF_ROUTER) != 0) {
7748 7750 return (ire);
7749 7751 }
7750 7752 return (NULL);
7751 7753 }
7752 7754
7753 7755 /*
7754 7756 * This function is used to construct a mac_header_info_s from a
7755 7757 * DL_UNITDATA_IND message.
7756 7758 * The address fields in the mhi structure points into the message,
7757 7759 * thus the caller can't use those fields after freeing the message.
7758 7760 *
7759 7761 * We determine whether the packet received is a non-unicast packet
7760 7762 * and in doing so, determine whether or not it is broadcast vs multicast.
7761 7763 * For it to be a broadcast packet, we must have the appropriate mblk_t
7762 7764 * hanging off the ill_t. If this is either not present or doesn't match
7763 7765 * the destination mac address in the DL_UNITDATA_IND, the packet is deemed
7764 7766 * to be multicast. Thus NICs that have no broadcast address (or no
7765 7767 * capability for one, such as point to point links) cannot return as
7766 7768 * the packet being broadcast.
7767 7769 */
7768 7770 void
7769 7771 ip_dlur_to_mhi(ill_t *ill, mblk_t *mb, struct mac_header_info_s *mhip)
7770 7772 {
7771 7773 dl_unitdata_ind_t *ind = (dl_unitdata_ind_t *)mb->b_rptr;
7772 7774 mblk_t *bmp;
7773 7775 uint_t extra_offset;
7774 7776
7775 7777 bzero(mhip, sizeof (struct mac_header_info_s));
7776 7778
7777 7779 mhip->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
7778 7780
7779 7781 if (ill->ill_sap_length < 0)
7780 7782 extra_offset = 0;
7781 7783 else
7782 7784 extra_offset = ill->ill_sap_length;
7783 7785
7784 7786 mhip->mhi_daddr = (uchar_t *)ind + ind->dl_dest_addr_offset +
7785 7787 extra_offset;
7786 7788 mhip->mhi_saddr = (uchar_t *)ind + ind->dl_src_addr_offset +
7787 7789 extra_offset;
7788 7790
7789 7791 if (!ind->dl_group_address)
7790 7792 return;
7791 7793
7792 7794 /* Multicast or broadcast */
7793 7795 mhip->mhi_dsttype = MAC_ADDRTYPE_MULTICAST;
7794 7796
7795 7797 if (ind->dl_dest_addr_offset > sizeof (*ind) &&
7796 7798 ind->dl_dest_addr_offset + ind->dl_dest_addr_length < MBLKL(mb) &&
7797 7799 (bmp = ill->ill_bcast_mp) != NULL) {
7798 7800 dl_unitdata_req_t *dlur;
7799 7801 uint8_t *bphys_addr;
7800 7802
7801 7803 dlur = (dl_unitdata_req_t *)bmp->b_rptr;
7802 7804 bphys_addr = (uchar_t *)dlur + dlur->dl_dest_addr_offset +
7803 7805 extra_offset;
7804 7806
7805 7807 if (bcmp(mhip->mhi_daddr, bphys_addr,
7806 7808 ind->dl_dest_addr_length) == 0)
7807 7809 mhip->mhi_dsttype = MAC_ADDRTYPE_BROADCAST;
7808 7810 }
7809 7811 }
7810 7812
7811 7813 /*
7812 7814 * This function is used to construct a mac_header_info_s from a
7813 7815 * M_DATA fastpath message from a DLPI driver.
7814 7816 * The address fields in the mhi structure points into the message,
7815 7817 * thus the caller can't use those fields after freeing the message.
7816 7818 *
7817 7819 * We determine whether the packet received is a non-unicast packet
7818 7820 * and in doing so, determine whether or not it is broadcast vs multicast.
7819 7821 * For it to be a broadcast packet, we must have the appropriate mblk_t
7820 7822 * hanging off the ill_t. If this is either not present or doesn't match
7821 7823 * the destination mac address in the DL_UNITDATA_IND, the packet is deemed
7822 7824 * to be multicast. Thus NICs that have no broadcast address (or no
7823 7825 * capability for one, such as point to point links) cannot return as
7824 7826 * the packet being broadcast.
7825 7827 */
7826 7828 void
7827 7829 ip_mdata_to_mhi(ill_t *ill, mblk_t *mp, struct mac_header_info_s *mhip)
7828 7830 {
7829 7831 mblk_t *bmp;
7830 7832 struct ether_header *pether;
7831 7833
7832 7834 bzero(mhip, sizeof (struct mac_header_info_s));
7833 7835
7834 7836 mhip->mhi_dsttype = MAC_ADDRTYPE_UNICAST;
7835 7837
7836 7838 pether = (struct ether_header *)((char *)mp->b_rptr
7837 7839 - sizeof (struct ether_header));
7838 7840
7839 7841 /*
7840 7842 * Make sure the interface is an ethernet type, since we don't
7841 7843 * know the header format for anything but Ethernet. Also make
7842 7844 * sure we are pointing correctly above db_base.
7843 7845 */
7844 7846 if (ill->ill_type != IFT_ETHER)
7845 7847 return;
7846 7848
7847 7849 retry:
7848 7850 if ((uchar_t *)pether < mp->b_datap->db_base)
7849 7851 return;
7850 7852
7851 7853 /* Is there a VLAN tag? */
7852 7854 if (ill->ill_isv6) {
7853 7855 if (pether->ether_type != htons(ETHERTYPE_IPV6)) {
7854 7856 pether = (struct ether_header *)((char *)pether - 4);
7855 7857 goto retry;
7856 7858 }
7857 7859 } else {
7858 7860 if (pether->ether_type != htons(ETHERTYPE_IP)) {
7859 7861 pether = (struct ether_header *)((char *)pether - 4);
7860 7862 goto retry;
7861 7863 }
7862 7864 }
7863 7865 mhip->mhi_daddr = (uchar_t *)&pether->ether_dhost;
7864 7866 mhip->mhi_saddr = (uchar_t *)&pether->ether_shost;
7865 7867
7866 7868 if (!(mhip->mhi_daddr[0] & 0x01))
7867 7869 return;
7868 7870
7869 7871 /* Multicast or broadcast */
7870 7872 mhip->mhi_dsttype = MAC_ADDRTYPE_MULTICAST;
7871 7873
7872 7874 if ((bmp = ill->ill_bcast_mp) != NULL) {
7873 7875 dl_unitdata_req_t *dlur;
7874 7876 uint8_t *bphys_addr;
7875 7877 uint_t addrlen;
7876 7878
7877 7879 dlur = (dl_unitdata_req_t *)bmp->b_rptr;
7878 7880 addrlen = dlur->dl_dest_addr_length;
7879 7881 if (ill->ill_sap_length < 0) {
7880 7882 bphys_addr = (uchar_t *)dlur +
7881 7883 dlur->dl_dest_addr_offset;
7882 7884 addrlen += ill->ill_sap_length;
7883 7885 } else {
7884 7886 bphys_addr = (uchar_t *)dlur +
7885 7887 dlur->dl_dest_addr_offset +
7886 7888 ill->ill_sap_length;
7887 7889 addrlen -= ill->ill_sap_length;
7888 7890 }
7889 7891 if (bcmp(mhip->mhi_daddr, bphys_addr, addrlen) == 0)
7890 7892 mhip->mhi_dsttype = MAC_ADDRTYPE_BROADCAST;
7891 7893 }
7892 7894 }
7893 7895
7894 7896 /*
7895 7897 * Handle anything but M_DATA messages
7896 7898 * We see the DL_UNITDATA_IND which are part
7897 7899 * of the data path, and also the other messages from the driver.
7898 7900 */
7899 7901 void
7900 7902 ip_rput_notdata(ill_t *ill, mblk_t *mp)
7901 7903 {
7902 7904 mblk_t *first_mp;
7903 7905 struct iocblk *iocp;
7904 7906 struct mac_header_info_s mhi;
7905 7907
7906 7908 switch (DB_TYPE(mp)) {
7907 7909 case M_PROTO:
7908 7910 case M_PCPROTO: {
7909 7911 if (((dl_unitdata_ind_t *)mp->b_rptr)->dl_primitive !=
7910 7912 DL_UNITDATA_IND) {
7911 7913 /* Go handle anything other than data elsewhere. */
7912 7914 ip_rput_dlpi(ill, mp);
7913 7915 return;
7914 7916 }
7915 7917
7916 7918 first_mp = mp;
7917 7919 mp = first_mp->b_cont;
7918 7920 first_mp->b_cont = NULL;
7919 7921
7920 7922 if (mp == NULL) {
7921 7923 freeb(first_mp);
7922 7924 return;
7923 7925 }
7924 7926 ip_dlur_to_mhi(ill, first_mp, &mhi);
7925 7927 if (ill->ill_isv6)
7926 7928 ip_input_v6(ill, NULL, mp, &mhi);
7927 7929 else
7928 7930 ip_input(ill, NULL, mp, &mhi);
7929 7931
7930 7932 /* Ditch the DLPI header. */
7931 7933 freeb(first_mp);
7932 7934 return;
7933 7935 }
7934 7936 case M_IOCACK:
7935 7937 iocp = (struct iocblk *)mp->b_rptr;
7936 7938 switch (iocp->ioc_cmd) {
7937 7939 case DL_IOC_HDR_INFO:
7938 7940 ill_fastpath_ack(ill, mp);
7939 7941 return;
7940 7942 default:
7941 7943 putnext(ill->ill_rq, mp);
7942 7944 return;
7943 7945 }
7944 7946 /* FALLTHRU */
7945 7947 case M_ERROR:
7946 7948 case M_HANGUP:
7947 7949 mutex_enter(&ill->ill_lock);
7948 7950 if (ill->ill_state_flags & ILL_CONDEMNED) {
7949 7951 mutex_exit(&ill->ill_lock);
7950 7952 freemsg(mp);
7951 7953 return;
7952 7954 }
7953 7955 ill_refhold_locked(ill);
7954 7956 mutex_exit(&ill->ill_lock);
7955 7957 qwriter_ip(ill, ill->ill_rq, mp, ip_rput_other, CUR_OP,
7956 7958 B_FALSE);
7957 7959 return;
7958 7960 case M_CTL:
7959 7961 putnext(ill->ill_rq, mp);
7960 7962 return;
7961 7963 case M_IOCNAK:
7962 7964 ip1dbg(("got iocnak "));
7963 7965 iocp = (struct iocblk *)mp->b_rptr;
7964 7966 switch (iocp->ioc_cmd) {
7965 7967 case DL_IOC_HDR_INFO:
7966 7968 ip_rput_other(NULL, ill->ill_rq, mp, NULL);
7967 7969 return;
7968 7970 default:
7969 7971 break;
7970 7972 }
7971 7973 /* FALLTHRU */
7972 7974 default:
7973 7975 putnext(ill->ill_rq, mp);
7974 7976 return;
7975 7977 }
7976 7978 }
7977 7979
7978 7980 /* Read side put procedure. Packets coming from the wire arrive here. */
7979 7981 void
7980 7982 ip_rput(queue_t *q, mblk_t *mp)
7981 7983 {
7982 7984 ill_t *ill;
7983 7985 union DL_primitives *dl;
7984 7986
7985 7987 ill = (ill_t *)q->q_ptr;
7986 7988
7987 7989 if (ill->ill_state_flags & (ILL_CONDEMNED | ILL_LL_SUBNET_PENDING)) {
7988 7990 /*
7989 7991 * If things are opening or closing, only accept high-priority
7990 7992 * DLPI messages. (On open ill->ill_ipif has not yet been
7991 7993 * created; on close, things hanging off the ill may have been
7992 7994 * freed already.)
7993 7995 */
7994 7996 dl = (union DL_primitives *)mp->b_rptr;
7995 7997 if (DB_TYPE(mp) != M_PCPROTO ||
7996 7998 dl->dl_primitive == DL_UNITDATA_IND) {
7997 7999 inet_freemsg(mp);
7998 8000 return;
7999 8001 }
8000 8002 }
8001 8003 if (DB_TYPE(mp) == M_DATA) {
8002 8004 struct mac_header_info_s mhi;
8003 8005
8004 8006 ip_mdata_to_mhi(ill, mp, &mhi);
8005 8007 ip_input(ill, NULL, mp, &mhi);
8006 8008 } else {
8007 8009 ip_rput_notdata(ill, mp);
8008 8010 }
8009 8011 }
8010 8012
8011 8013 /*
8012 8014 * Move the information to a copy.
8013 8015 */
8014 8016 mblk_t *
8015 8017 ip_fix_dbref(mblk_t *mp, ip_recv_attr_t *ira)
8016 8018 {
8017 8019 mblk_t *mp1;
8018 8020 ill_t *ill = ira->ira_ill;
8019 8021 ip_stack_t *ipst = ill->ill_ipst;
8020 8022
8021 8023 IP_STAT(ipst, ip_db_ref);
8022 8024
8023 8025 /* Make sure we have ira_l2src before we loose the original mblk */
8024 8026 if (!(ira->ira_flags & IRAF_L2SRC_SET))
8025 8027 ip_setl2src(mp, ira, ira->ira_rill);
8026 8028
8027 8029 mp1 = copymsg(mp);
8028 8030 if (mp1 == NULL) {
8029 8031 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
8030 8032 ip_drop_input("ipIfStatsInDiscards", mp, ill);
8031 8033 freemsg(mp);
8032 8034 return (NULL);
8033 8035 }
8034 8036 /* preserve the hardware checksum flags and data, if present */
8035 8037 if (DB_CKSUMFLAGS(mp) != 0) {
8036 8038 DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp);
8037 8039 DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp);
8038 8040 DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp);
8039 8041 DB_CKSUMEND(mp1) = DB_CKSUMEND(mp);
8040 8042 DB_CKSUM16(mp1) = DB_CKSUM16(mp);
8041 8043 }
8042 8044 freemsg(mp);
8043 8045 return (mp1);
8044 8046 }
8045 8047
8046 8048 static void
8047 8049 ip_dlpi_error(ill_t *ill, t_uscalar_t prim, t_uscalar_t dl_err,
8048 8050 t_uscalar_t err)
8049 8051 {
8050 8052 if (dl_err == DL_SYSERR) {
8051 8053 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
8052 8054 "%s: %s failed: DL_SYSERR (errno %u)\n",
8053 8055 ill->ill_name, dl_primstr(prim), err);
8054 8056 return;
8055 8057 }
8056 8058
8057 8059 (void) mi_strlog(ill->ill_rq, 1, SL_CONSOLE|SL_ERROR|SL_TRACE,
8058 8060 "%s: %s failed: %s\n", ill->ill_name, dl_primstr(prim),
8059 8061 dl_errstr(dl_err));
8060 8062 }
8061 8063
8062 8064 /*
8063 8065 * ip_rput_dlpi is called by ip_rput to handle all DLPI messages other
8064 8066 * than DL_UNITDATA_IND messages. If we need to process this message
8065 8067 * exclusively, we call qwriter_ip, in which case we also need to call
8066 8068 * ill_refhold before that, since qwriter_ip does an ill_refrele.
8067 8069 */
8068 8070 void
8069 8071 ip_rput_dlpi(ill_t *ill, mblk_t *mp)
8070 8072 {
8071 8073 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr;
8072 8074 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa;
8073 8075 queue_t *q = ill->ill_rq;
8074 8076 t_uscalar_t prim = dloa->dl_primitive;
8075 8077 t_uscalar_t reqprim = DL_PRIM_INVAL;
8076 8078
8077 8079 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi",
8078 8080 char *, dl_primstr(prim), ill_t *, ill);
8079 8081 ip1dbg(("ip_rput_dlpi"));
8080 8082
8081 8083 /*
8082 8084 * If we received an ACK but didn't send a request for it, then it
8083 8085 * can't be part of any pending operation; discard up-front.
8084 8086 */
8085 8087 switch (prim) {
8086 8088 case DL_ERROR_ACK:
8087 8089 reqprim = dlea->dl_error_primitive;
8088 8090 ip2dbg(("ip_rput_dlpi(%s): DL_ERROR_ACK for %s (0x%x): %s "
8089 8091 "(0x%x), unix %u\n", ill->ill_name, dl_primstr(reqprim),
8090 8092 reqprim, dl_errstr(dlea->dl_errno), dlea->dl_errno,
8091 8093 dlea->dl_unix_errno));
8092 8094 break;
8093 8095 case DL_OK_ACK:
8094 8096 reqprim = dloa->dl_correct_primitive;
8095 8097 break;
8096 8098 case DL_INFO_ACK:
8097 8099 reqprim = DL_INFO_REQ;
8098 8100 break;
8099 8101 case DL_BIND_ACK:
8100 8102 reqprim = DL_BIND_REQ;
8101 8103 break;
8102 8104 case DL_PHYS_ADDR_ACK:
8103 8105 reqprim = DL_PHYS_ADDR_REQ;
8104 8106 break;
8105 8107 case DL_NOTIFY_ACK:
8106 8108 reqprim = DL_NOTIFY_REQ;
8107 8109 break;
8108 8110 case DL_CAPABILITY_ACK:
8109 8111 reqprim = DL_CAPABILITY_REQ;
8110 8112 break;
8111 8113 }
8112 8114
8113 8115 if (prim != DL_NOTIFY_IND) {
8114 8116 if (reqprim == DL_PRIM_INVAL ||
8115 8117 !ill_dlpi_pending(ill, reqprim)) {
8116 8118 /* Not a DLPI message we support or expected */
8117 8119 freemsg(mp);
8118 8120 return;
8119 8121 }
8120 8122 ip1dbg(("ip_rput: received %s for %s\n", dl_primstr(prim),
8121 8123 dl_primstr(reqprim)));
8122 8124 }
8123 8125
8124 8126 switch (reqprim) {
8125 8127 case DL_UNBIND_REQ:
8126 8128 /*
8127 8129 * NOTE: we mark the unbind as complete even if we got a
8128 8130 * DL_ERROR_ACK, since there's not much else we can do.
8129 8131 */
8130 8132 mutex_enter(&ill->ill_lock);
8131 8133 ill->ill_state_flags &= ~ILL_DL_UNBIND_IN_PROGRESS;
8132 8134 cv_signal(&ill->ill_cv);
8133 8135 mutex_exit(&ill->ill_lock);
8134 8136 break;
8135 8137
8136 8138 case DL_ENABMULTI_REQ:
8137 8139 if (prim == DL_OK_ACK) {
8138 8140 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS)
8139 8141 ill->ill_dlpi_multicast_state = IDS_OK;
8140 8142 }
8141 8143 break;
8142 8144 }
8143 8145
8144 8146 /*
8145 8147 * The message is one we're waiting for (or DL_NOTIFY_IND), but we
8146 8148 * need to become writer to continue to process it. Because an
8147 8149 * exclusive operation doesn't complete until replies to all queued
8148 8150 * DLPI messages have been received, we know we're in the middle of an
8149 8151 * exclusive operation and pass CUR_OP (except for DL_NOTIFY_IND).
8150 8152 *
8151 8153 * As required by qwriter_ip(), we refhold the ill; it will refrele.
8152 8154 * Since this is on the ill stream we unconditionally bump up the
8153 8155 * refcount without doing ILL_CAN_LOOKUP().
8154 8156 */
8155 8157 ill_refhold(ill);
8156 8158 if (prim == DL_NOTIFY_IND)
8157 8159 qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, NEW_OP, B_FALSE);
8158 8160 else
8159 8161 qwriter_ip(ill, q, mp, ip_rput_dlpi_writer, CUR_OP, B_FALSE);
8160 8162 }
8161 8163
8162 8164 /*
8163 8165 * Handling of DLPI messages that require exclusive access to the ipsq.
8164 8166 *
8165 8167 * Need to do ipsq_pending_mp_get on ioctl completion, which could
8166 8168 * happen here. (along with mi_copy_done)
8167 8169 */
8168 8170 /* ARGSUSED */
8169 8171 static void
8170 8172 ip_rput_dlpi_writer(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8171 8173 {
8172 8174 dl_ok_ack_t *dloa = (dl_ok_ack_t *)mp->b_rptr;
8173 8175 dl_error_ack_t *dlea = (dl_error_ack_t *)dloa;
8174 8176 int err = 0;
8175 8177 ill_t *ill = (ill_t *)q->q_ptr;
8176 8178 ipif_t *ipif = NULL;
8177 8179 mblk_t *mp1 = NULL;
8178 8180 conn_t *connp = NULL;
8179 8181 t_uscalar_t paddrreq;
8180 8182 mblk_t *mp_hw;
8181 8183 boolean_t success;
8182 8184 boolean_t ioctl_aborted = B_FALSE;
8183 8185 boolean_t log = B_TRUE;
8184 8186
8185 8187 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer",
8186 8188 char *, dl_primstr(dloa->dl_primitive), ill_t *, ill);
8187 8189
8188 8190 ip1dbg(("ip_rput_dlpi_writer .."));
8189 8191 ASSERT(ipsq->ipsq_xop == ill->ill_phyint->phyint_ipsq->ipsq_xop);
8190 8192 ASSERT(IAM_WRITER_ILL(ill));
8191 8193
8192 8194 ipif = ipsq->ipsq_xop->ipx_pending_ipif;
8193 8195 /*
8194 8196 * The current ioctl could have been aborted by the user and a new
8195 8197 * ioctl to bring up another ill could have started. We could still
8196 8198 * get a response from the driver later.
8197 8199 */
8198 8200 if (ipif != NULL && ipif->ipif_ill != ill)
8199 8201 ioctl_aborted = B_TRUE;
8200 8202
8201 8203 switch (dloa->dl_primitive) {
8202 8204 case DL_ERROR_ACK:
8203 8205 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for %s\n",
8204 8206 dl_primstr(dlea->dl_error_primitive)));
8205 8207
8206 8208 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer error",
8207 8209 char *, dl_primstr(dlea->dl_error_primitive),
8208 8210 ill_t *, ill);
8209 8211
8210 8212 switch (dlea->dl_error_primitive) {
8211 8213 case DL_DISABMULTI_REQ:
8212 8214 ill_dlpi_done(ill, dlea->dl_error_primitive);
8213 8215 break;
8214 8216 case DL_PROMISCON_REQ:
8215 8217 case DL_PROMISCOFF_REQ:
8216 8218 case DL_UNBIND_REQ:
8217 8219 case DL_ATTACH_REQ:
8218 8220 case DL_INFO_REQ:
8219 8221 ill_dlpi_done(ill, dlea->dl_error_primitive);
8220 8222 break;
8221 8223 case DL_NOTIFY_REQ:
8222 8224 ill_dlpi_done(ill, DL_NOTIFY_REQ);
8223 8225 log = B_FALSE;
8224 8226 break;
8225 8227 case DL_PHYS_ADDR_REQ:
8226 8228 /*
8227 8229 * For IPv6 only, there are two additional
8228 8230 * phys_addr_req's sent to the driver to get the
8229 8231 * IPv6 token and lla. This allows IP to acquire
8230 8232 * the hardware address format for a given interface
8231 8233 * without having built in knowledge of the hardware
8232 8234 * address. ill_phys_addr_pend keeps track of the last
8233 8235 * DL_PAR sent so we know which response we are
8234 8236 * dealing with. ill_dlpi_done will update
8235 8237 * ill_phys_addr_pend when it sends the next req.
8236 8238 * We don't complete the IOCTL until all three DL_PARs
8237 8239 * have been attempted, so set *_len to 0 and break.
8238 8240 */
8239 8241 paddrreq = ill->ill_phys_addr_pend;
8240 8242 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ);
8241 8243 if (paddrreq == DL_IPV6_TOKEN) {
8242 8244 ill->ill_token_length = 0;
8243 8245 log = B_FALSE;
8244 8246 break;
8245 8247 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) {
8246 8248 ill->ill_nd_lla_len = 0;
8247 8249 log = B_FALSE;
8248 8250 break;
8249 8251 }
8250 8252 /*
8251 8253 * Something went wrong with the DL_PHYS_ADDR_REQ.
8252 8254 * We presumably have an IOCTL hanging out waiting
8253 8255 * for completion. Find it and complete the IOCTL
8254 8256 * with the error noted.
8255 8257 * However, ill_dl_phys was called on an ill queue
8256 8258 * (from SIOCSLIFNAME), thus conn_pending_ill is not
8257 8259 * set. But the ioctl is known to be pending on ill_wq.
8258 8260 */
8259 8261 if (!ill->ill_ifname_pending)
8260 8262 break;
8261 8263 ill->ill_ifname_pending = 0;
8262 8264 if (!ioctl_aborted)
8263 8265 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8264 8266 if (mp1 != NULL) {
8265 8267 /*
8266 8268 * This operation (SIOCSLIFNAME) must have
8267 8269 * happened on the ill. Assert there is no conn
8268 8270 */
8269 8271 ASSERT(connp == NULL);
8270 8272 q = ill->ill_wq;
8271 8273 }
8272 8274 break;
8273 8275 case DL_BIND_REQ:
8274 8276 ill_dlpi_done(ill, DL_BIND_REQ);
8275 8277 if (ill->ill_ifname_pending)
8276 8278 break;
8277 8279 mutex_enter(&ill->ill_lock);
8278 8280 ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
8279 8281 mutex_exit(&ill->ill_lock);
8280 8282 /*
8281 8283 * Something went wrong with the bind. We presumably
8282 8284 * have an IOCTL hanging out waiting for completion.
8283 8285 * Find it, take down the interface that was coming
8284 8286 * up, and complete the IOCTL with the error noted.
8285 8287 */
8286 8288 if (!ioctl_aborted)
8287 8289 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8288 8290 if (mp1 != NULL) {
8289 8291 /*
8290 8292 * This might be a result of a DL_NOTE_REPLUMB
8291 8293 * notification. In that case, connp is NULL.
8292 8294 */
8293 8295 if (connp != NULL)
8294 8296 q = CONNP_TO_WQ(connp);
8295 8297
8296 8298 (void) ipif_down(ipif, NULL, NULL);
8297 8299 /* error is set below the switch */
8298 8300 }
8299 8301 break;
8300 8302 case DL_ENABMULTI_REQ:
8301 8303 ill_dlpi_done(ill, DL_ENABMULTI_REQ);
8302 8304
8303 8305 if (ill->ill_dlpi_multicast_state == IDS_INPROGRESS)
8304 8306 ill->ill_dlpi_multicast_state = IDS_FAILED;
8305 8307 if (ill->ill_dlpi_multicast_state == IDS_FAILED) {
8306 8308
8307 8309 printf("ip: joining multicasts failed (%d)"
8308 8310 " on %s - will use link layer "
8309 8311 "broadcasts for multicast\n",
8310 8312 dlea->dl_errno, ill->ill_name);
8311 8313
8312 8314 /*
8313 8315 * Set up for multi_bcast; We are the
8314 8316 * writer, so ok to access ill->ill_ipif
8315 8317 * without any lock.
8316 8318 */
8317 8319 mutex_enter(&ill->ill_phyint->phyint_lock);
8318 8320 ill->ill_phyint->phyint_flags |=
8319 8321 PHYI_MULTI_BCAST;
8320 8322 mutex_exit(&ill->ill_phyint->phyint_lock);
8321 8323
8322 8324 }
8323 8325 freemsg(mp); /* Don't want to pass this up */
8324 8326 return;
8325 8327 case DL_CAPABILITY_REQ:
8326 8328 ip1dbg(("ip_rput_dlpi_writer: got DL_ERROR_ACK for "
8327 8329 "DL_CAPABILITY REQ\n"));
8328 8330 if (ill->ill_dlpi_capab_state == IDCS_PROBE_SENT)
8329 8331 ill->ill_dlpi_capab_state = IDCS_FAILED;
8330 8332 ill_capability_done(ill);
8331 8333 freemsg(mp);
8332 8334 return;
8333 8335 }
8334 8336 /*
8335 8337 * Note the error for IOCTL completion (mp1 is set when
8336 8338 * ready to complete ioctl). If ill_ifname_pending_err is
8337 8339 * set, an error occured during plumbing (ill_ifname_pending),
8338 8340 * so we want to report that error.
8339 8341 *
8340 8342 * NOTE: there are two addtional DL_PHYS_ADDR_REQ's
8341 8343 * (DL_IPV6_TOKEN and DL_IPV6_LINK_LAYER_ADDR) that are
8342 8344 * expected to get errack'd if the driver doesn't support
8343 8345 * these flags (e.g. ethernet). log will be set to B_FALSE
8344 8346 * if these error conditions are encountered.
8345 8347 */
8346 8348 if (mp1 != NULL) {
8347 8349 if (ill->ill_ifname_pending_err != 0) {
8348 8350 err = ill->ill_ifname_pending_err;
8349 8351 ill->ill_ifname_pending_err = 0;
8350 8352 } else {
8351 8353 err = dlea->dl_unix_errno ?
8352 8354 dlea->dl_unix_errno : ENXIO;
8353 8355 }
8354 8356 /*
8355 8357 * If we're plumbing an interface and an error hasn't already
8356 8358 * been saved, set ill_ifname_pending_err to the error passed
8357 8359 * up. Ignore the error if log is B_FALSE (see comment above).
8358 8360 */
8359 8361 } else if (log && ill->ill_ifname_pending &&
8360 8362 ill->ill_ifname_pending_err == 0) {
8361 8363 ill->ill_ifname_pending_err = dlea->dl_unix_errno ?
8362 8364 dlea->dl_unix_errno : ENXIO;
8363 8365 }
8364 8366
8365 8367 if (log)
8366 8368 ip_dlpi_error(ill, dlea->dl_error_primitive,
8367 8369 dlea->dl_errno, dlea->dl_unix_errno);
8368 8370 break;
8369 8371 case DL_CAPABILITY_ACK:
8370 8372 ill_capability_ack(ill, mp);
8371 8373 /*
8372 8374 * The message has been handed off to ill_capability_ack
8373 8375 * and must not be freed below
8374 8376 */
8375 8377 mp = NULL;
8376 8378 break;
8377 8379
8378 8380 case DL_INFO_ACK:
8379 8381 /* Call a routine to handle this one. */
8380 8382 ill_dlpi_done(ill, DL_INFO_REQ);
8381 8383 ip_ll_subnet_defaults(ill, mp);
8382 8384 ASSERT(!MUTEX_HELD(&ill->ill_phyint->phyint_ipsq->ipsq_lock));
8383 8385 return;
8384 8386 case DL_BIND_ACK:
8385 8387 /*
8386 8388 * We should have an IOCTL waiting on this unless
8387 8389 * sent by ill_dl_phys, in which case just return
8388 8390 */
8389 8391 ill_dlpi_done(ill, DL_BIND_REQ);
8390 8392
8391 8393 if (ill->ill_ifname_pending) {
8392 8394 DTRACE_PROBE2(ip__rput__dlpi__ifname__pending,
8393 8395 ill_t *, ill, mblk_t *, mp);
8394 8396 break;
8395 8397 }
8396 8398 mutex_enter(&ill->ill_lock);
8397 8399 ill->ill_dl_up = 1;
8398 8400 ill->ill_state_flags &= ~ILL_DOWN_IN_PROGRESS;
8399 8401 mutex_exit(&ill->ill_lock);
8400 8402
8401 8403 if (!ioctl_aborted)
8402 8404 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8403 8405 if (mp1 == NULL) {
8404 8406 DTRACE_PROBE1(ip__rput__dlpi__no__mblk, ill_t *, ill);
8405 8407 break;
8406 8408 }
8407 8409 /*
8408 8410 * mp1 was added by ill_dl_up(). if that is a result of
8409 8411 * a DL_NOTE_REPLUMB notification, connp could be NULL.
8410 8412 */
8411 8413 if (connp != NULL)
8412 8414 q = CONNP_TO_WQ(connp);
8413 8415 /*
8414 8416 * We are exclusive. So nothing can change even after
8415 8417 * we get the pending mp.
8416 8418 */
8417 8419 ip1dbg(("ip_rput_dlpi: bind_ack %s\n", ill->ill_name));
8418 8420 DTRACE_PROBE1(ip__rput__dlpi__bind__ack, ill_t *, ill);
8419 8421 ill_nic_event_dispatch(ill, 0, NE_UP, NULL, 0);
8420 8422
8421 8423 /*
8422 8424 * Now bring up the resolver; when that is complete, we'll
8423 8425 * create IREs. Note that we intentionally mirror what
8424 8426 * ipif_up() would have done, because we got here by way of
8425 8427 * ill_dl_up(), which stopped ipif_up()'s processing.
8426 8428 */
8427 8429 if (ill->ill_isv6) {
8428 8430 /*
8429 8431 * v6 interfaces.
8430 8432 * Unlike ARP which has to do another bind
8431 8433 * and attach, once we get here we are
8432 8434 * done with NDP
8433 8435 */
8434 8436 (void) ipif_resolver_up(ipif, Res_act_initial);
8435 8437 if ((err = ipif_ndp_up(ipif, B_TRUE)) == 0)
8436 8438 err = ipif_up_done_v6(ipif);
8437 8439 } else if (ill->ill_net_type == IRE_IF_RESOLVER) {
8438 8440 /*
8439 8441 * ARP and other v4 external resolvers.
8440 8442 * Leave the pending mblk intact so that
8441 8443 * the ioctl completes in ip_rput().
8442 8444 */
8443 8445 if (connp != NULL)
8444 8446 mutex_enter(&connp->conn_lock);
8445 8447 mutex_enter(&ill->ill_lock);
8446 8448 success = ipsq_pending_mp_add(connp, ipif, q, mp1, 0);
8447 8449 mutex_exit(&ill->ill_lock);
8448 8450 if (connp != NULL)
8449 8451 mutex_exit(&connp->conn_lock);
8450 8452 if (success) {
8451 8453 err = ipif_resolver_up(ipif, Res_act_initial);
8452 8454 if (err == EINPROGRESS) {
8453 8455 freemsg(mp);
8454 8456 return;
8455 8457 }
8456 8458 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8457 8459 } else {
8458 8460 /* The conn has started closing */
8459 8461 err = EINTR;
8460 8462 }
8461 8463 } else {
8462 8464 /*
8463 8465 * This one is complete. Reply to pending ioctl.
8464 8466 */
8465 8467 (void) ipif_resolver_up(ipif, Res_act_initial);
8466 8468 err = ipif_up_done(ipif);
8467 8469 }
8468 8470
8469 8471 if ((err == 0) && (ill->ill_up_ipifs)) {
8470 8472 err = ill_up_ipifs(ill, q, mp1);
8471 8473 if (err == EINPROGRESS) {
8472 8474 freemsg(mp);
8473 8475 return;
8474 8476 }
8475 8477 }
8476 8478
8477 8479 /*
8478 8480 * If we have a moved ipif to bring up, and everything has
8479 8481 * succeeded to this point, bring it up on the IPMP ill.
8480 8482 * Otherwise, leave it down -- the admin can try to bring it
8481 8483 * up by hand if need be.
8482 8484 */
8483 8485 if (ill->ill_move_ipif != NULL) {
8484 8486 if (err != 0) {
8485 8487 ill->ill_move_ipif = NULL;
8486 8488 } else {
8487 8489 ipif = ill->ill_move_ipif;
8488 8490 ill->ill_move_ipif = NULL;
8489 8491 err = ipif_up(ipif, q, mp1);
8490 8492 if (err == EINPROGRESS) {
8491 8493 freemsg(mp);
8492 8494 return;
8493 8495 }
8494 8496 }
8495 8497 }
8496 8498 break;
8497 8499
8498 8500 case DL_NOTIFY_IND: {
8499 8501 dl_notify_ind_t *notify = (dl_notify_ind_t *)mp->b_rptr;
8500 8502 uint_t orig_mtu, orig_mc_mtu;
8501 8503
8502 8504 switch (notify->dl_notification) {
8503 8505 case DL_NOTE_PHYS_ADDR:
8504 8506 err = ill_set_phys_addr(ill, mp);
8505 8507 break;
8506 8508
8507 8509 case DL_NOTE_REPLUMB:
8508 8510 /*
8509 8511 * Directly return after calling ill_replumb().
8510 8512 * Note that we should not free mp as it is reused
8511 8513 * in the ill_replumb() function.
8512 8514 */
8513 8515 err = ill_replumb(ill, mp);
8514 8516 return;
8515 8517
8516 8518 case DL_NOTE_FASTPATH_FLUSH:
8517 8519 nce_flush(ill, B_FALSE);
8518 8520 break;
8519 8521
8520 8522 case DL_NOTE_SDU_SIZE:
8521 8523 case DL_NOTE_SDU_SIZE2:
8522 8524 /*
8523 8525 * The dce and fragmentation code can cope with
8524 8526 * this changing while packets are being sent.
8525 8527 * When packets are sent ip_output will discover
8526 8528 * a change.
8527 8529 *
8528 8530 * Change the MTU size of the interface.
8529 8531 */
8530 8532 mutex_enter(&ill->ill_lock);
8531 8533 orig_mtu = ill->ill_mtu;
8532 8534 orig_mc_mtu = ill->ill_mc_mtu;
8533 8535 switch (notify->dl_notification) {
8534 8536 case DL_NOTE_SDU_SIZE:
8535 8537 ill->ill_current_frag =
8536 8538 (uint_t)notify->dl_data;
8537 8539 ill->ill_mc_mtu = (uint_t)notify->dl_data;
8538 8540 break;
8539 8541 case DL_NOTE_SDU_SIZE2:
8540 8542 ill->ill_current_frag =
8541 8543 (uint_t)notify->dl_data1;
8542 8544 ill->ill_mc_mtu = (uint_t)notify->dl_data2;
8543 8545 break;
8544 8546 }
8545 8547 if (ill->ill_current_frag > ill->ill_max_frag)
8546 8548 ill->ill_max_frag = ill->ill_current_frag;
8547 8549
8548 8550 if (!(ill->ill_flags & ILLF_FIXEDMTU)) {
8549 8551 ill->ill_mtu = ill->ill_current_frag;
8550 8552
8551 8553 /*
8552 8554 * If ill_user_mtu was set (via
8553 8555 * SIOCSLIFLNKINFO), clamp ill_mtu at it.
8554 8556 */
8555 8557 if (ill->ill_user_mtu != 0 &&
8556 8558 ill->ill_user_mtu < ill->ill_mtu)
8557 8559 ill->ill_mtu = ill->ill_user_mtu;
8558 8560
8559 8561 if (ill->ill_user_mtu != 0 &&
8560 8562 ill->ill_user_mtu < ill->ill_mc_mtu)
8561 8563 ill->ill_mc_mtu = ill->ill_user_mtu;
8562 8564
8563 8565 if (ill->ill_isv6) {
8564 8566 if (ill->ill_mtu < IPV6_MIN_MTU)
8565 8567 ill->ill_mtu = IPV6_MIN_MTU;
8566 8568 if (ill->ill_mc_mtu < IPV6_MIN_MTU)
8567 8569 ill->ill_mc_mtu = IPV6_MIN_MTU;
8568 8570 } else {
8569 8571 if (ill->ill_mtu < IP_MIN_MTU)
8570 8572 ill->ill_mtu = IP_MIN_MTU;
8571 8573 if (ill->ill_mc_mtu < IP_MIN_MTU)
8572 8574 ill->ill_mc_mtu = IP_MIN_MTU;
8573 8575 }
8574 8576 } else if (ill->ill_mc_mtu > ill->ill_mtu) {
8575 8577 ill->ill_mc_mtu = ill->ill_mtu;
8576 8578 }
8577 8579
8578 8580 mutex_exit(&ill->ill_lock);
8579 8581 /*
8580 8582 * Make sure all dce_generation checks find out
8581 8583 * that ill_mtu/ill_mc_mtu has changed.
8582 8584 */
8583 8585 if (orig_mtu != ill->ill_mtu ||
8584 8586 orig_mc_mtu != ill->ill_mc_mtu) {
8585 8587 dce_increment_all_generations(ill->ill_isv6,
8586 8588 ill->ill_ipst);
8587 8589 }
8588 8590
8589 8591 /*
8590 8592 * Refresh IPMP meta-interface MTU if necessary.
8591 8593 */
8592 8594 if (IS_UNDER_IPMP(ill))
8593 8595 ipmp_illgrp_refresh_mtu(ill->ill_grp);
8594 8596 break;
8595 8597
8596 8598 case DL_NOTE_LINK_UP:
8597 8599 case DL_NOTE_LINK_DOWN: {
8598 8600 /*
8599 8601 * We are writer. ill / phyint / ipsq assocs stable.
8600 8602 * The RUNNING flag reflects the state of the link.
8601 8603 */
8602 8604 phyint_t *phyint = ill->ill_phyint;
8603 8605 uint64_t new_phyint_flags;
8604 8606 boolean_t changed = B_FALSE;
8605 8607 boolean_t went_up;
8606 8608
8607 8609 went_up = notify->dl_notification == DL_NOTE_LINK_UP;
8608 8610 mutex_enter(&phyint->phyint_lock);
8609 8611
8610 8612 new_phyint_flags = went_up ?
8611 8613 phyint->phyint_flags | PHYI_RUNNING :
8612 8614 phyint->phyint_flags & ~PHYI_RUNNING;
8613 8615
8614 8616 if (IS_IPMP(ill)) {
8615 8617 new_phyint_flags = went_up ?
8616 8618 new_phyint_flags & ~PHYI_FAILED :
8617 8619 new_phyint_flags | PHYI_FAILED;
8618 8620 }
8619 8621
8620 8622 if (new_phyint_flags != phyint->phyint_flags) {
8621 8623 phyint->phyint_flags = new_phyint_flags;
8622 8624 changed = B_TRUE;
8623 8625 }
8624 8626 mutex_exit(&phyint->phyint_lock);
8625 8627 /*
8626 8628 * ill_restart_dad handles the DAD restart and routing
8627 8629 * socket notification logic.
8628 8630 */
8629 8631 if (changed) {
8630 8632 ill_restart_dad(phyint->phyint_illv4, went_up);
8631 8633 ill_restart_dad(phyint->phyint_illv6, went_up);
8632 8634 }
8633 8635 break;
8634 8636 }
8635 8637 case DL_NOTE_PROMISC_ON_PHYS: {
8636 8638 phyint_t *phyint = ill->ill_phyint;
8637 8639
8638 8640 mutex_enter(&phyint->phyint_lock);
8639 8641 phyint->phyint_flags |= PHYI_PROMISC;
8640 8642 mutex_exit(&phyint->phyint_lock);
8641 8643 break;
8642 8644 }
8643 8645 case DL_NOTE_PROMISC_OFF_PHYS: {
8644 8646 phyint_t *phyint = ill->ill_phyint;
8645 8647
8646 8648 mutex_enter(&phyint->phyint_lock);
8647 8649 phyint->phyint_flags &= ~PHYI_PROMISC;
8648 8650 mutex_exit(&phyint->phyint_lock);
8649 8651 break;
8650 8652 }
8651 8653 case DL_NOTE_CAPAB_RENEG:
8652 8654 /*
8653 8655 * Something changed on the driver side.
8654 8656 * It wants us to renegotiate the capabilities
8655 8657 * on this ill. One possible cause is the aggregation
8656 8658 * interface under us where a port got added or
8657 8659 * went away.
8658 8660 *
8659 8661 * If the capability negotiation is already done
8660 8662 * or is in progress, reset the capabilities and
8661 8663 * mark the ill's ill_capab_reneg to be B_TRUE,
8662 8664 * so that when the ack comes back, we can start
8663 8665 * the renegotiation process.
8664 8666 *
8665 8667 * Note that if ill_capab_reneg is already B_TRUE
8666 8668 * (ill_dlpi_capab_state is IDS_UNKNOWN in this case),
8667 8669 * the capability resetting request has been sent
8668 8670 * and the renegotiation has not been started yet;
8669 8671 * nothing needs to be done in this case.
8670 8672 */
8671 8673 ipsq_current_start(ipsq, ill->ill_ipif, 0);
8672 8674 ill_capability_reset(ill, B_TRUE);
8673 8675 ipsq_current_finish(ipsq);
8674 8676 break;
8675 8677
8676 8678 case DL_NOTE_ALLOWED_IPS:
8677 8679 ill_set_allowed_ips(ill, mp);
8678 8680 break;
8679 8681 default:
8680 8682 ip0dbg(("ip_rput_dlpi_writer: unknown notification "
8681 8683 "type 0x%x for DL_NOTIFY_IND\n",
8682 8684 notify->dl_notification));
8683 8685 break;
8684 8686 }
8685 8687
8686 8688 /*
8687 8689 * As this is an asynchronous operation, we
8688 8690 * should not call ill_dlpi_done
8689 8691 */
8690 8692 break;
8691 8693 }
8692 8694 case DL_NOTIFY_ACK: {
8693 8695 dl_notify_ack_t *noteack = (dl_notify_ack_t *)mp->b_rptr;
8694 8696
8695 8697 if (noteack->dl_notifications & DL_NOTE_LINK_UP)
8696 8698 ill->ill_note_link = 1;
8697 8699 ill_dlpi_done(ill, DL_NOTIFY_REQ);
8698 8700 break;
8699 8701 }
8700 8702 case DL_PHYS_ADDR_ACK: {
8701 8703 /*
8702 8704 * As part of plumbing the interface via SIOCSLIFNAME,
8703 8705 * ill_dl_phys() will queue a series of DL_PHYS_ADDR_REQs,
8704 8706 * whose answers we receive here. As each answer is received,
8705 8707 * we call ill_dlpi_done() to dispatch the next request as
8706 8708 * we're processing the current one. Once all answers have
8707 8709 * been received, we use ipsq_pending_mp_get() to dequeue the
8708 8710 * outstanding IOCTL and reply to it. (Because ill_dl_phys()
8709 8711 * is invoked from an ill queue, conn_oper_pending_ill is not
8710 8712 * available, but we know the ioctl is pending on ill_wq.)
8711 8713 */
8712 8714 uint_t paddrlen, paddroff;
8713 8715 uint8_t *addr;
8714 8716
8715 8717 paddrreq = ill->ill_phys_addr_pend;
8716 8718 paddrlen = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_length;
8717 8719 paddroff = ((dl_phys_addr_ack_t *)mp->b_rptr)->dl_addr_offset;
8718 8720 addr = mp->b_rptr + paddroff;
8719 8721
8720 8722 ill_dlpi_done(ill, DL_PHYS_ADDR_REQ);
8721 8723 if (paddrreq == DL_IPV6_TOKEN) {
8722 8724 /*
8723 8725 * bcopy to low-order bits of ill_token
8724 8726 *
8725 8727 * XXX Temporary hack - currently, all known tokens
8726 8728 * are 64 bits, so I'll cheat for the moment.
8727 8729 */
8728 8730 bcopy(addr, &ill->ill_token.s6_addr32[2], paddrlen);
8729 8731 ill->ill_token_length = paddrlen;
8730 8732 break;
8731 8733 } else if (paddrreq == DL_IPV6_LINK_LAYER_ADDR) {
8732 8734 ASSERT(ill->ill_nd_lla_mp == NULL);
8733 8735 ill_set_ndmp(ill, mp, paddroff, paddrlen);
8734 8736 mp = NULL;
8735 8737 break;
8736 8738 } else if (paddrreq == DL_CURR_DEST_ADDR) {
8737 8739 ASSERT(ill->ill_dest_addr_mp == NULL);
8738 8740 ill->ill_dest_addr_mp = mp;
8739 8741 ill->ill_dest_addr = addr;
8740 8742 mp = NULL;
8741 8743 if (ill->ill_isv6) {
8742 8744 ill_setdesttoken(ill);
8743 8745 ipif_setdestlinklocal(ill->ill_ipif);
8744 8746 }
8745 8747 break;
8746 8748 }
8747 8749
8748 8750 ASSERT(paddrreq == DL_CURR_PHYS_ADDR);
8749 8751 ASSERT(ill->ill_phys_addr_mp == NULL);
8750 8752 if (!ill->ill_ifname_pending)
8751 8753 break;
8752 8754 ill->ill_ifname_pending = 0;
8753 8755 if (!ioctl_aborted)
8754 8756 mp1 = ipsq_pending_mp_get(ipsq, &connp);
8755 8757 if (mp1 != NULL) {
8756 8758 ASSERT(connp == NULL);
8757 8759 q = ill->ill_wq;
8758 8760 }
8759 8761 /*
8760 8762 * If any error acks received during the plumbing sequence,
8761 8763 * ill_ifname_pending_err will be set. Break out and send up
8762 8764 * the error to the pending ioctl.
8763 8765 */
8764 8766 if (ill->ill_ifname_pending_err != 0) {
8765 8767 err = ill->ill_ifname_pending_err;
8766 8768 ill->ill_ifname_pending_err = 0;
8767 8769 break;
8768 8770 }
8769 8771
8770 8772 ill->ill_phys_addr_mp = mp;
8771 8773 ill->ill_phys_addr = (paddrlen == 0 ? NULL : addr);
8772 8774 mp = NULL;
8773 8775
8774 8776 /*
8775 8777 * If paddrlen or ill_phys_addr_length is zero, the DLPI
8776 8778 * provider doesn't support physical addresses. We check both
8777 8779 * paddrlen and ill_phys_addr_length because sppp (PPP) does
8778 8780 * not have physical addresses, but historically adversises a
8779 8781 * physical address length of 0 in its DL_INFO_ACK, but 6 in
8780 8782 * its DL_PHYS_ADDR_ACK.
8781 8783 */
8782 8784 if (paddrlen == 0 || ill->ill_phys_addr_length == 0) {
8783 8785 ill->ill_phys_addr = NULL;
8784 8786 } else if (paddrlen != ill->ill_phys_addr_length) {
8785 8787 ip0dbg(("DL_PHYS_ADDR_ACK: got addrlen %d, expected %d",
8786 8788 paddrlen, ill->ill_phys_addr_length));
8787 8789 err = EINVAL;
8788 8790 break;
8789 8791 }
8790 8792
8791 8793 if (ill->ill_nd_lla_mp == NULL) {
8792 8794 if ((mp_hw = copyb(ill->ill_phys_addr_mp)) == NULL) {
8793 8795 err = ENOMEM;
8794 8796 break;
8795 8797 }
8796 8798 ill_set_ndmp(ill, mp_hw, paddroff, paddrlen);
8797 8799 }
8798 8800
8799 8801 if (ill->ill_isv6) {
8800 8802 ill_setdefaulttoken(ill);
8801 8803 ipif_setlinklocal(ill->ill_ipif);
8802 8804 }
8803 8805 break;
8804 8806 }
8805 8807 case DL_OK_ACK:
8806 8808 ip2dbg(("DL_OK_ACK %s (0x%x)\n",
8807 8809 dl_primstr((int)dloa->dl_correct_primitive),
8808 8810 dloa->dl_correct_primitive));
8809 8811 DTRACE_PROBE3(ill__dlpi, char *, "ip_rput_dlpi_writer ok",
8810 8812 char *, dl_primstr(dloa->dl_correct_primitive),
8811 8813 ill_t *, ill);
8812 8814
8813 8815 switch (dloa->dl_correct_primitive) {
8814 8816 case DL_ENABMULTI_REQ:
8815 8817 case DL_DISABMULTI_REQ:
8816 8818 ill_dlpi_done(ill, dloa->dl_correct_primitive);
8817 8819 break;
8818 8820 case DL_PROMISCON_REQ:
8819 8821 case DL_PROMISCOFF_REQ:
8820 8822 case DL_UNBIND_REQ:
8821 8823 case DL_ATTACH_REQ:
8822 8824 ill_dlpi_done(ill, dloa->dl_correct_primitive);
8823 8825 break;
8824 8826 }
8825 8827 break;
8826 8828 default:
8827 8829 break;
8828 8830 }
8829 8831
8830 8832 freemsg(mp);
8831 8833 if (mp1 == NULL)
8832 8834 return;
8833 8835
8834 8836 /*
8835 8837 * The operation must complete without EINPROGRESS since
8836 8838 * ipsq_pending_mp_get() has removed the mblk (mp1). Otherwise,
8837 8839 * the operation will be stuck forever inside the IPSQ.
8838 8840 */
8839 8841 ASSERT(err != EINPROGRESS);
8840 8842
8841 8843 DTRACE_PROBE4(ipif__ioctl, char *, "ip_rput_dlpi_writer finish",
8842 8844 int, ipsq->ipsq_xop->ipx_current_ioctl, ill_t *, ill,
8843 8845 ipif_t *, NULL);
8844 8846
8845 8847 switch (ipsq->ipsq_xop->ipx_current_ioctl) {
8846 8848 case 0:
8847 8849 ipsq_current_finish(ipsq);
8848 8850 break;
8849 8851
8850 8852 case SIOCSLIFNAME:
8851 8853 case IF_UNITSEL: {
8852 8854 ill_t *ill_other = ILL_OTHER(ill);
8853 8855
8854 8856 /*
8855 8857 * If SIOCSLIFNAME or IF_UNITSEL is about to succeed, and the
8856 8858 * ill has a peer which is in an IPMP group, then place ill
8857 8859 * into the same group. One catch: although ifconfig plumbs
8858 8860 * the appropriate IPMP meta-interface prior to plumbing this
8859 8861 * ill, it is possible for multiple ifconfig applications to
8860 8862 * race (or for another application to adjust plumbing), in
8861 8863 * which case the IPMP meta-interface we need will be missing.
8862 8864 * If so, kick the phyint out of the group.
8863 8865 */
8864 8866 if (err == 0 && ill_other != NULL && IS_UNDER_IPMP(ill_other)) {
8865 8867 ipmp_grp_t *grp = ill->ill_phyint->phyint_grp;
8866 8868 ipmp_illgrp_t *illg;
8867 8869
8868 8870 illg = ill->ill_isv6 ? grp->gr_v6 : grp->gr_v4;
8869 8871 if (illg == NULL)
8870 8872 ipmp_phyint_leave_grp(ill->ill_phyint);
8871 8873 else
8872 8874 ipmp_ill_join_illgrp(ill, illg);
8873 8875 }
8874 8876
8875 8877 if (ipsq->ipsq_xop->ipx_current_ioctl == IF_UNITSEL)
8876 8878 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
8877 8879 else
8878 8880 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq);
8879 8881 break;
8880 8882 }
8881 8883 case SIOCLIFADDIF:
8882 8884 ip_ioctl_finish(q, mp1, err, COPYOUT, ipsq);
8883 8885 break;
8884 8886
8885 8887 default:
8886 8888 ip_ioctl_finish(q, mp1, err, NO_COPYOUT, ipsq);
8887 8889 break;
8888 8890 }
8889 8891 }
8890 8892
8891 8893 /*
8892 8894 * ip_rput_other is called by ip_rput to handle messages modifying the global
8893 8895 * state in IP. If 'ipsq' is non-NULL, caller is writer on it.
8894 8896 */
8895 8897 /* ARGSUSED */
8896 8898 void
8897 8899 ip_rput_other(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
8898 8900 {
8899 8901 ill_t *ill = q->q_ptr;
8900 8902 struct iocblk *iocp;
8901 8903
8902 8904 ip1dbg(("ip_rput_other "));
8903 8905 if (ipsq != NULL) {
8904 8906 ASSERT(IAM_WRITER_IPSQ(ipsq));
8905 8907 ASSERT(ipsq->ipsq_xop ==
8906 8908 ill->ill_phyint->phyint_ipsq->ipsq_xop);
8907 8909 }
8908 8910
8909 8911 switch (mp->b_datap->db_type) {
8910 8912 case M_ERROR:
8911 8913 case M_HANGUP:
8912 8914 /*
8913 8915 * The device has a problem. We force the ILL down. It can
8914 8916 * be brought up again manually using SIOCSIFFLAGS (via
8915 8917 * ifconfig or equivalent).
8916 8918 */
8917 8919 ASSERT(ipsq != NULL);
8918 8920 if (mp->b_rptr < mp->b_wptr)
8919 8921 ill->ill_error = (int)(*mp->b_rptr & 0xFF);
8920 8922 if (ill->ill_error == 0)
8921 8923 ill->ill_error = ENXIO;
8922 8924 if (!ill_down_start(q, mp))
8923 8925 return;
8924 8926 ipif_all_down_tail(ipsq, q, mp, NULL);
8925 8927 break;
8926 8928 case M_IOCNAK: {
8927 8929 iocp = (struct iocblk *)mp->b_rptr;
8928 8930
8929 8931 ASSERT(iocp->ioc_cmd == DL_IOC_HDR_INFO);
8930 8932 /*
8931 8933 * If this was the first attempt, turn off the fastpath
8932 8934 * probing.
8933 8935 */
8934 8936 mutex_enter(&ill->ill_lock);
8935 8937 if (ill->ill_dlpi_fastpath_state == IDS_INPROGRESS) {
8936 8938 ill->ill_dlpi_fastpath_state = IDS_FAILED;
8937 8939 mutex_exit(&ill->ill_lock);
8938 8940 /*
8939 8941 * don't flush the nce_t entries: we use them
8940 8942 * as an index to the ncec itself.
8941 8943 */
8942 8944 ip1dbg(("ip_rput: DLPI fastpath off on interface %s\n",
8943 8945 ill->ill_name));
8944 8946 } else {
8945 8947 mutex_exit(&ill->ill_lock);
8946 8948 }
8947 8949 freemsg(mp);
8948 8950 break;
8949 8951 }
8950 8952 default:
8951 8953 ASSERT(0);
8952 8954 break;
8953 8955 }
8954 8956 }
8955 8957
8956 8958 /*
8957 8959 * Update any source route, record route or timestamp options
8958 8960 * When it fails it has consumed the message and BUMPed the MIB.
8959 8961 */
8960 8962 boolean_t
8961 8963 ip_forward_options(mblk_t *mp, ipha_t *ipha, ill_t *dst_ill,
8962 8964 ip_recv_attr_t *ira)
8963 8965 {
8964 8966 ipoptp_t opts;
8965 8967 uchar_t *opt;
8966 8968 uint8_t optval;
8967 8969 uint8_t optlen;
8968 8970 ipaddr_t dst;
8969 8971 ipaddr_t ifaddr;
8970 8972 uint32_t ts;
8971 8973 timestruc_t now;
8972 8974 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
8973 8975
8974 8976 ip2dbg(("ip_forward_options\n"));
8975 8977 dst = ipha->ipha_dst;
8976 8978 for (optval = ipoptp_first(&opts, ipha);
8977 8979 optval != IPOPT_EOL;
8978 8980 optval = ipoptp_next(&opts)) {
8979 8981 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
8980 8982 opt = opts.ipoptp_cur;
8981 8983 optlen = opts.ipoptp_len;
8982 8984 ip2dbg(("ip_forward_options: opt %d, len %d\n",
8983 8985 optval, opts.ipoptp_len));
8984 8986 switch (optval) {
8985 8987 uint32_t off;
8986 8988 case IPOPT_SSRR:
8987 8989 case IPOPT_LSRR:
8988 8990 /* Check if adminstratively disabled */
8989 8991 if (!ipst->ips_ip_forward_src_routed) {
8990 8992 BUMP_MIB(dst_ill->ill_ip_mib,
8991 8993 ipIfStatsForwProhibits);
8992 8994 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED",
8993 8995 mp, dst_ill);
8994 8996 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED,
8995 8997 ira);
8996 8998 return (B_FALSE);
8997 8999 }
8998 9000 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
8999 9001 /*
9000 9002 * Must be partial since ip_input_options
9001 9003 * checked for strict.
9002 9004 */
9003 9005 break;
9004 9006 }
9005 9007 off = opt[IPOPT_OFFSET];
9006 9008 off--;
9007 9009 redo_srr:
9008 9010 if (optlen < IP_ADDR_LEN ||
9009 9011 off > optlen - IP_ADDR_LEN) {
9010 9012 /* End of source route */
9011 9013 ip1dbg((
9012 9014 "ip_forward_options: end of SR\n"));
9013 9015 break;
9014 9016 }
9015 9017 /* Pick a reasonable address on the outbound if */
9016 9018 ASSERT(dst_ill != NULL);
9017 9019 if (ip_select_source_v4(dst_ill, INADDR_ANY, dst,
9018 9020 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
9019 9021 NULL) != 0) {
9020 9022 /* No source! Shouldn't happen */
9021 9023 ifaddr = INADDR_ANY;
9022 9024 }
9023 9025 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9024 9026 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9025 9027 ip1dbg(("ip_forward_options: next hop 0x%x\n",
9026 9028 ntohl(dst)));
9027 9029
9028 9030 /*
9029 9031 * Check if our address is present more than
9030 9032 * once as consecutive hops in source route.
9031 9033 */
9032 9034 if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
9033 9035 off += IP_ADDR_LEN;
9034 9036 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9035 9037 goto redo_srr;
9036 9038 }
9037 9039 ipha->ipha_dst = dst;
9038 9040 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9039 9041 break;
9040 9042 case IPOPT_RR:
9041 9043 off = opt[IPOPT_OFFSET];
9042 9044 off--;
9043 9045 if (optlen < IP_ADDR_LEN ||
9044 9046 off > optlen - IP_ADDR_LEN) {
9045 9047 /* No more room - ignore */
9046 9048 ip1dbg((
9047 9049 "ip_forward_options: end of RR\n"));
9048 9050 break;
9049 9051 }
9050 9052 /* Pick a reasonable address on the outbound if */
9051 9053 ASSERT(dst_ill != NULL);
9052 9054 if (ip_select_source_v4(dst_ill, INADDR_ANY, dst,
9053 9055 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
9054 9056 NULL) != 0) {
9055 9057 /* No source! Shouldn't happen */
9056 9058 ifaddr = INADDR_ANY;
9057 9059 }
9058 9060 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9059 9061 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9060 9062 break;
9061 9063 case IPOPT_TS:
9062 9064 /* Insert timestamp if there is room */
9063 9065 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9064 9066 case IPOPT_TS_TSONLY:
9065 9067 off = IPOPT_TS_TIMELEN;
9066 9068 break;
9067 9069 case IPOPT_TS_PRESPEC:
9068 9070 case IPOPT_TS_PRESPEC_RFC791:
9069 9071 /* Verify that the address matched */
9070 9072 off = opt[IPOPT_OFFSET] - 1;
9071 9073 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9072 9074 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9073 9075 /* Not for us */
9074 9076 break;
9075 9077 }
9076 9078 /* FALLTHRU */
9077 9079 case IPOPT_TS_TSANDADDR:
9078 9080 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9079 9081 break;
9080 9082 default:
9081 9083 /*
9082 9084 * ip_*put_options should have already
9083 9085 * dropped this packet.
9084 9086 */
9085 9087 cmn_err(CE_PANIC, "ip_forward_options: "
9086 9088 "unknown IT - bug in ip_input_options?\n");
9087 9089 return (B_TRUE); /* Keep "lint" happy */
9088 9090 }
9089 9091 if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
9090 9092 /* Increase overflow counter */
9091 9093 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
9092 9094 opt[IPOPT_POS_OV_FLG] =
9093 9095 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) |
9094 9096 (off << 4));
9095 9097 break;
9096 9098 }
9097 9099 off = opt[IPOPT_OFFSET] - 1;
9098 9100 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9099 9101 case IPOPT_TS_PRESPEC:
9100 9102 case IPOPT_TS_PRESPEC_RFC791:
9101 9103 case IPOPT_TS_TSANDADDR:
9102 9104 /* Pick a reasonable addr on the outbound if */
9103 9105 ASSERT(dst_ill != NULL);
9104 9106 if (ip_select_source_v4(dst_ill, INADDR_ANY,
9105 9107 dst, INADDR_ANY, ALL_ZONES, ipst, &ifaddr,
9106 9108 NULL, NULL) != 0) {
9107 9109 /* No source! Shouldn't happen */
9108 9110 ifaddr = INADDR_ANY;
9109 9111 }
9110 9112 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9111 9113 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9112 9114 /* FALLTHRU */
9113 9115 case IPOPT_TS_TSONLY:
9114 9116 off = opt[IPOPT_OFFSET] - 1;
9115 9117 /* Compute # of milliseconds since midnight */
9116 9118 gethrestime(&now);
9117 9119 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
9118 9120 now.tv_nsec / (NANOSEC / MILLISEC);
9119 9121 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
9120 9122 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
9121 9123 break;
9122 9124 }
9123 9125 break;
9124 9126 }
9125 9127 }
9126 9128 return (B_TRUE);
9127 9129 }
9128 9130
9129 9131 /*
9130 9132 * Call ill_frag_timeout to do garbage collection. ill_frag_timeout
9131 9133 * returns 'true' if there are still fragments left on the queue, in
9132 9134 * which case we restart the timer.
9133 9135 */
9134 9136 void
9135 9137 ill_frag_timer(void *arg)
9136 9138 {
9137 9139 ill_t *ill = (ill_t *)arg;
9138 9140 boolean_t frag_pending;
9139 9141 ip_stack_t *ipst = ill->ill_ipst;
9140 9142 time_t timeout;
9141 9143
9142 9144 mutex_enter(&ill->ill_lock);
9143 9145 ASSERT(!ill->ill_fragtimer_executing);
9144 9146 if (ill->ill_state_flags & ILL_CONDEMNED) {
9145 9147 ill->ill_frag_timer_id = 0;
9146 9148 mutex_exit(&ill->ill_lock);
9147 9149 return;
9148 9150 }
9149 9151 ill->ill_fragtimer_executing = 1;
9150 9152 mutex_exit(&ill->ill_lock);
9151 9153
9152 9154 timeout = (ill->ill_isv6 ? ipst->ips_ipv6_reassembly_timeout :
9153 9155 ipst->ips_ip_reassembly_timeout);
9154 9156
9155 9157 frag_pending = ill_frag_timeout(ill, timeout);
9156 9158
9157 9159 /*
9158 9160 * Restart the timer, if we have fragments pending or if someone
9159 9161 * wanted us to be scheduled again.
9160 9162 */
9161 9163 mutex_enter(&ill->ill_lock);
9162 9164 ill->ill_fragtimer_executing = 0;
9163 9165 ill->ill_frag_timer_id = 0;
9164 9166 if (frag_pending || ill->ill_fragtimer_needrestart)
9165 9167 ill_frag_timer_start(ill);
9166 9168 mutex_exit(&ill->ill_lock);
9167 9169 }
9168 9170
9169 9171 void
9170 9172 ill_frag_timer_start(ill_t *ill)
9171 9173 {
9172 9174 ip_stack_t *ipst = ill->ill_ipst;
9173 9175 clock_t timeo_ms;
9174 9176
9175 9177 ASSERT(MUTEX_HELD(&ill->ill_lock));
9176 9178
9177 9179 /* If the ill is closing or opening don't proceed */
9178 9180 if (ill->ill_state_flags & ILL_CONDEMNED)
9179 9181 return;
9180 9182
9181 9183 if (ill->ill_fragtimer_executing) {
9182 9184 /*
9183 9185 * ill_frag_timer is currently executing. Just record the
9184 9186 * the fact that we want the timer to be restarted.
9185 9187 * ill_frag_timer will post a timeout before it returns,
9186 9188 * ensuring it will be called again.
9187 9189 */
9188 9190 ill->ill_fragtimer_needrestart = 1;
9189 9191 return;
9190 9192 }
9191 9193
9192 9194 if (ill->ill_frag_timer_id == 0) {
9193 9195 timeo_ms = (ill->ill_isv6 ? ipst->ips_ipv6_reassembly_timeout :
9194 9196 ipst->ips_ip_reassembly_timeout) * SECONDS;
9195 9197
9196 9198 /*
9197 9199 * The timer is neither running nor is the timeout handler
9198 9200 * executing. Post a timeout so that ill_frag_timer will be
9199 9201 * called
9200 9202 */
9201 9203 ill->ill_frag_timer_id = timeout(ill_frag_timer, ill,
9202 9204 MSEC_TO_TICK(timeo_ms >> 1));
9203 9205 ill->ill_fragtimer_needrestart = 0;
9204 9206 }
9205 9207 }
9206 9208
9207 9209 /*
9208 9210 * Update any source route, record route or timestamp options.
9209 9211 * Check that we are at end of strict source route.
9210 9212 * The options have already been checked for sanity in ip_input_options().
9211 9213 */
9212 9214 boolean_t
9213 9215 ip_input_local_options(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
9214 9216 {
9215 9217 ipoptp_t opts;
9216 9218 uchar_t *opt;
9217 9219 uint8_t optval;
9218 9220 uint8_t optlen;
9219 9221 ipaddr_t dst;
9220 9222 ipaddr_t ifaddr;
9221 9223 uint32_t ts;
9222 9224 timestruc_t now;
9223 9225 ill_t *ill = ira->ira_ill;
9224 9226 ip_stack_t *ipst = ill->ill_ipst;
9225 9227
9226 9228 ip2dbg(("ip_input_local_options\n"));
9227 9229
9228 9230 for (optval = ipoptp_first(&opts, ipha);
9229 9231 optval != IPOPT_EOL;
9230 9232 optval = ipoptp_next(&opts)) {
9231 9233 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
9232 9234 opt = opts.ipoptp_cur;
9233 9235 optlen = opts.ipoptp_len;
9234 9236 ip2dbg(("ip_input_local_options: opt %d, len %d\n",
9235 9237 optval, optlen));
9236 9238 switch (optval) {
9237 9239 uint32_t off;
9238 9240 case IPOPT_SSRR:
9239 9241 case IPOPT_LSRR:
9240 9242 off = opt[IPOPT_OFFSET];
9241 9243 off--;
9242 9244 if (optlen < IP_ADDR_LEN ||
9243 9245 off > optlen - IP_ADDR_LEN) {
9244 9246 /* End of source route */
9245 9247 ip1dbg(("ip_input_local_options: end of SR\n"));
9246 9248 break;
9247 9249 }
9248 9250 /*
9249 9251 * This will only happen if two consecutive entries
9250 9252 * in the source route contains our address or if
9251 9253 * it is a packet with a loose source route which
9252 9254 * reaches us before consuming the whole source route
9253 9255 */
9254 9256 ip1dbg(("ip_input_local_options: not end of SR\n"));
9255 9257 if (optval == IPOPT_SSRR) {
9256 9258 goto bad_src_route;
9257 9259 }
9258 9260 /*
9259 9261 * Hack: instead of dropping the packet truncate the
9260 9262 * source route to what has been used by filling the
9261 9263 * rest with IPOPT_NOP.
9262 9264 */
9263 9265 opt[IPOPT_OLEN] = (uint8_t)off;
9264 9266 while (off < optlen) {
9265 9267 opt[off++] = IPOPT_NOP;
9266 9268 }
9267 9269 break;
9268 9270 case IPOPT_RR:
9269 9271 off = opt[IPOPT_OFFSET];
9270 9272 off--;
9271 9273 if (optlen < IP_ADDR_LEN ||
9272 9274 off > optlen - IP_ADDR_LEN) {
9273 9275 /* No more room - ignore */
9274 9276 ip1dbg((
9275 9277 "ip_input_local_options: end of RR\n"));
9276 9278 break;
9277 9279 }
9278 9280 /* Pick a reasonable address on the outbound if */
9279 9281 if (ip_select_source_v4(ill, INADDR_ANY, ipha->ipha_dst,
9280 9282 INADDR_ANY, ALL_ZONES, ipst, &ifaddr, NULL,
9281 9283 NULL) != 0) {
9282 9284 /* No source! Shouldn't happen */
9283 9285 ifaddr = INADDR_ANY;
9284 9286 }
9285 9287 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9286 9288 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9287 9289 break;
9288 9290 case IPOPT_TS:
9289 9291 /* Insert timestamp if there is romm */
9290 9292 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9291 9293 case IPOPT_TS_TSONLY:
9292 9294 off = IPOPT_TS_TIMELEN;
9293 9295 break;
9294 9296 case IPOPT_TS_PRESPEC:
9295 9297 case IPOPT_TS_PRESPEC_RFC791:
9296 9298 /* Verify that the address matched */
9297 9299 off = opt[IPOPT_OFFSET] - 1;
9298 9300 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9299 9301 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9300 9302 /* Not for us */
9301 9303 break;
9302 9304 }
9303 9305 /* FALLTHRU */
9304 9306 case IPOPT_TS_TSANDADDR:
9305 9307 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9306 9308 break;
9307 9309 default:
9308 9310 /*
9309 9311 * ip_*put_options should have already
9310 9312 * dropped this packet.
9311 9313 */
9312 9314 cmn_err(CE_PANIC, "ip_input_local_options: "
9313 9315 "unknown IT - bug in ip_input_options?\n");
9314 9316 return (B_TRUE); /* Keep "lint" happy */
9315 9317 }
9316 9318 if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
9317 9319 /* Increase overflow counter */
9318 9320 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
9319 9321 opt[IPOPT_POS_OV_FLG] =
9320 9322 (uint8_t)((opt[IPOPT_POS_OV_FLG] & 0x0F) |
9321 9323 (off << 4));
9322 9324 break;
9323 9325 }
9324 9326 off = opt[IPOPT_OFFSET] - 1;
9325 9327 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9326 9328 case IPOPT_TS_PRESPEC:
9327 9329 case IPOPT_TS_PRESPEC_RFC791:
9328 9330 case IPOPT_TS_TSANDADDR:
9329 9331 /* Pick a reasonable addr on the outbound if */
9330 9332 if (ip_select_source_v4(ill, INADDR_ANY,
9331 9333 ipha->ipha_dst, INADDR_ANY, ALL_ZONES, ipst,
9332 9334 &ifaddr, NULL, NULL) != 0) {
9333 9335 /* No source! Shouldn't happen */
9334 9336 ifaddr = INADDR_ANY;
9335 9337 }
9336 9338 bcopy(&ifaddr, (char *)opt + off, IP_ADDR_LEN);
9337 9339 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
9338 9340 /* FALLTHRU */
9339 9341 case IPOPT_TS_TSONLY:
9340 9342 off = opt[IPOPT_OFFSET] - 1;
9341 9343 /* Compute # of milliseconds since midnight */
9342 9344 gethrestime(&now);
9343 9345 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
9344 9346 now.tv_nsec / (NANOSEC / MILLISEC);
9345 9347 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
9346 9348 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
9347 9349 break;
9348 9350 }
9349 9351 break;
9350 9352 }
9351 9353 }
9352 9354 return (B_TRUE);
9353 9355
9354 9356 bad_src_route:
9355 9357 /* make sure we clear any indication of a hardware checksum */
9356 9358 DB_CKSUMFLAGS(mp) = 0;
9357 9359 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ill);
9358 9360 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira);
9359 9361 return (B_FALSE);
9360 9362
9361 9363 }
9362 9364
9363 9365 /*
9364 9366 * Process IP options in an inbound packet. Always returns the nexthop.
9365 9367 * Normally this is the passed in nexthop, but if there is an option
9366 9368 * that effects the nexthop (such as a source route) that will be returned.
9367 9369 * Sets *errorp if there is an error, in which case an ICMP error has been sent
9368 9370 * and mp freed.
9369 9371 */
9370 9372 ipaddr_t
9371 9373 ip_input_options(ipha_t *ipha, ipaddr_t dst, mblk_t *mp,
9372 9374 ip_recv_attr_t *ira, int *errorp)
9373 9375 {
9374 9376 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
9375 9377 ipoptp_t opts;
9376 9378 uchar_t *opt;
9377 9379 uint8_t optval;
9378 9380 uint8_t optlen;
9379 9381 intptr_t code = 0;
9380 9382 ire_t *ire;
9381 9383
9382 9384 ip2dbg(("ip_input_options\n"));
9383 9385 *errorp = 0;
9384 9386 for (optval = ipoptp_first(&opts, ipha);
9385 9387 optval != IPOPT_EOL;
9386 9388 optval = ipoptp_next(&opts)) {
9387 9389 opt = opts.ipoptp_cur;
9388 9390 optlen = opts.ipoptp_len;
9389 9391 ip2dbg(("ip_input_options: opt %d, len %d\n",
9390 9392 optval, optlen));
9391 9393 /*
9392 9394 * Note: we need to verify the checksum before we
9393 9395 * modify anything thus this routine only extracts the next
9394 9396 * hop dst from any source route.
9395 9397 */
9396 9398 switch (optval) {
9397 9399 uint32_t off;
9398 9400 case IPOPT_SSRR:
9399 9401 case IPOPT_LSRR:
9400 9402 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
9401 9403 if (optval == IPOPT_SSRR) {
9402 9404 ip1dbg(("ip_input_options: not next"
9403 9405 " strict source route 0x%x\n",
9404 9406 ntohl(dst)));
9405 9407 code = (char *)&ipha->ipha_dst -
9406 9408 (char *)ipha;
9407 9409 goto param_prob; /* RouterReq's */
9408 9410 }
9409 9411 ip2dbg(("ip_input_options: "
9410 9412 "not next source route 0x%x\n",
9411 9413 ntohl(dst)));
9412 9414 break;
9413 9415 }
9414 9416
9415 9417 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9416 9418 ip1dbg((
9417 9419 "ip_input_options: bad option offset\n"));
9418 9420 code = (char *)&opt[IPOPT_OLEN] -
9419 9421 (char *)ipha;
9420 9422 goto param_prob;
9421 9423 }
9422 9424 off = opt[IPOPT_OFFSET];
9423 9425 off--;
9424 9426 redo_srr:
9425 9427 if (optlen < IP_ADDR_LEN ||
9426 9428 off > optlen - IP_ADDR_LEN) {
9427 9429 /* End of source route */
9428 9430 ip1dbg(("ip_input_options: end of SR\n"));
9429 9431 break;
9430 9432 }
9431 9433 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
9432 9434 ip1dbg(("ip_input_options: next hop 0x%x\n",
9433 9435 ntohl(dst)));
9434 9436
9435 9437 /*
9436 9438 * Check if our address is present more than
9437 9439 * once as consecutive hops in source route.
9438 9440 * XXX verify per-interface ip_forwarding
9439 9441 * for source route?
9440 9442 */
9441 9443 if (ip_type_v4(dst, ipst) == IRE_LOCAL) {
9442 9444 off += IP_ADDR_LEN;
9443 9445 goto redo_srr;
9444 9446 }
9445 9447
9446 9448 if (dst == htonl(INADDR_LOOPBACK)) {
9447 9449 ip1dbg(("ip_input_options: loopback addr in "
9448 9450 "source route!\n"));
9449 9451 goto bad_src_route;
9450 9452 }
9451 9453 /*
9452 9454 * For strict: verify that dst is directly
9453 9455 * reachable.
9454 9456 */
9455 9457 if (optval == IPOPT_SSRR) {
9456 9458 ire = ire_ftable_lookup_v4(dst, 0, 0,
9457 9459 IRE_INTERFACE, NULL, ALL_ZONES,
9458 9460 ira->ira_tsl,
9459 9461 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, 0, ipst,
9460 9462 NULL);
9461 9463 if (ire == NULL) {
9462 9464 ip1dbg(("ip_input_options: SSRR not "
9463 9465 "directly reachable: 0x%x\n",
9464 9466 ntohl(dst)));
9465 9467 goto bad_src_route;
9466 9468 }
9467 9469 ire_refrele(ire);
9468 9470 }
9469 9471 /*
9470 9472 * Defer update of the offset and the record route
9471 9473 * until the packet is forwarded.
9472 9474 */
9473 9475 break;
9474 9476 case IPOPT_RR:
9475 9477 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9476 9478 ip1dbg((
9477 9479 "ip_input_options: bad option offset\n"));
9478 9480 code = (char *)&opt[IPOPT_OLEN] -
9479 9481 (char *)ipha;
9480 9482 goto param_prob;
9481 9483 }
9482 9484 break;
9483 9485 case IPOPT_TS:
9484 9486 /*
9485 9487 * Verify that length >= 5 and that there is either
9486 9488 * room for another timestamp or that the overflow
9487 9489 * counter is not maxed out.
9488 9490 */
9489 9491 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha;
9490 9492 if (optlen < IPOPT_MINLEN_IT) {
9491 9493 goto param_prob;
9492 9494 }
9493 9495 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
9494 9496 ip1dbg((
9495 9497 "ip_input_options: bad option offset\n"));
9496 9498 code = (char *)&opt[IPOPT_OFFSET] -
9497 9499 (char *)ipha;
9498 9500 goto param_prob;
9499 9501 }
9500 9502 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
9501 9503 case IPOPT_TS_TSONLY:
9502 9504 off = IPOPT_TS_TIMELEN;
9503 9505 break;
9504 9506 case IPOPT_TS_TSANDADDR:
9505 9507 case IPOPT_TS_PRESPEC:
9506 9508 case IPOPT_TS_PRESPEC_RFC791:
9507 9509 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
9508 9510 break;
9509 9511 default:
9510 9512 code = (char *)&opt[IPOPT_POS_OV_FLG] -
9511 9513 (char *)ipha;
9512 9514 goto param_prob;
9513 9515 }
9514 9516 if (opt[IPOPT_OFFSET] - 1 + off > optlen &&
9515 9517 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) {
9516 9518 /*
9517 9519 * No room and the overflow counter is 15
9518 9520 * already.
9519 9521 */
9520 9522 goto param_prob;
9521 9523 }
9522 9524 break;
9523 9525 }
9524 9526 }
9525 9527
9526 9528 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0) {
9527 9529 return (dst);
9528 9530 }
9529 9531
9530 9532 ip1dbg(("ip_input_options: error processing IP options."));
9531 9533 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha;
9532 9534
9533 9535 param_prob:
9534 9536 /* make sure we clear any indication of a hardware checksum */
9535 9537 DB_CKSUMFLAGS(mp) = 0;
9536 9538 ip_drop_input("ICMP_PARAM_PROBLEM", mp, ira->ira_ill);
9537 9539 icmp_param_problem(mp, (uint8_t)code, ira);
9538 9540 *errorp = -1;
9539 9541 return (dst);
9540 9542
9541 9543 bad_src_route:
9542 9544 /* make sure we clear any indication of a hardware checksum */
9543 9545 DB_CKSUMFLAGS(mp) = 0;
9544 9546 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ira->ira_ill);
9545 9547 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira);
9546 9548 *errorp = -1;
9547 9549 return (dst);
9548 9550 }
9549 9551
9550 9552 /*
9551 9553 * IP & ICMP info in >=14 msg's ...
9552 9554 * - ip fixed part (mib2_ip_t)
9553 9555 * - icmp fixed part (mib2_icmp_t)
9554 9556 * - ipAddrEntryTable (ip 20) all IPv4 ipifs
9555 9557 * - ipRouteEntryTable (ip 21) all IPv4 IREs
9556 9558 * - ipNetToMediaEntryTable (ip 22) all IPv4 Neighbor Cache entries
9557 9559 * - ipRouteAttributeTable (ip 102) labeled routes
9558 9560 * - ip multicast membership (ip_member_t)
9559 9561 * - ip multicast source filtering (ip_grpsrc_t)
9560 9562 * - igmp fixed part (struct igmpstat)
9561 9563 * - multicast routing stats (struct mrtstat)
9562 9564 * - multicast routing vifs (array of struct vifctl)
9563 9565 * - multicast routing routes (array of struct mfcctl)
9564 9566 * - ip6 fixed part (mib2_ipv6IfStatsEntry_t)
9565 9567 * One per ill plus one generic
9566 9568 * - icmp6 fixed part (mib2_ipv6IfIcmpEntry_t)
9567 9569 * One per ill plus one generic
9568 9570 * - ipv6RouteEntry all IPv6 IREs
9569 9571 * - ipv6RouteAttributeTable (ip6 102) labeled routes
9570 9572 * - ipv6NetToMediaEntry all IPv6 Neighbor Cache entries
9571 9573 * - ipv6AddrEntry all IPv6 ipifs
9572 9574 * - ipv6 multicast membership (ipv6_member_t)
9573 9575 * - ipv6 multicast source filtering (ipv6_grpsrc_t)
9574 9576 *
9575 9577 * NOTE: original mpctl is copied for msg's 2..N, since its ctl part is
9576 9578 * already filled in by the caller.
9577 9579 * If legacy_req is true then MIB structures needs to be truncated to their
9578 9580 * legacy sizes before being returned.
9579 9581 * Return value of 0 indicates that no messages were sent and caller
9580 9582 * should free mpctl.
9581 9583 */
9582 9584 int
9583 9585 ip_snmp_get(queue_t *q, mblk_t *mpctl, int level, boolean_t legacy_req)
9584 9586 {
9585 9587 ip_stack_t *ipst;
9586 9588 sctp_stack_t *sctps;
9587 9589
9588 9590 if (q->q_next != NULL) {
9589 9591 ipst = ILLQ_TO_IPST(q);
9590 9592 } else {
9591 9593 ipst = CONNQ_TO_IPST(q);
9592 9594 }
9593 9595 ASSERT(ipst != NULL);
9594 9596 sctps = ipst->ips_netstack->netstack_sctp;
9595 9597
9596 9598 if (mpctl == NULL || mpctl->b_cont == NULL) {
9597 9599 return (0);
9598 9600 }
9599 9601
9600 9602 /*
9601 9603 * For the purposes of the (broken) packet shell use
9602 9604 * of the level we make sure MIB2_TCP/MIB2_UDP can be used
9603 9605 * to make TCP and UDP appear first in the list of mib items.
9604 9606 * TBD: We could expand this and use it in netstat so that
9605 9607 * the kernel doesn't have to produce large tables (connections,
9606 9608 * routes, etc) when netstat only wants the statistics or a particular
9607 9609 * table.
9608 9610 */
9609 9611 if (!(level == MIB2_TCP || level == MIB2_UDP)) {
9610 9612 if ((mpctl = icmp_snmp_get(q, mpctl)) == NULL) {
9611 9613 return (1);
9612 9614 }
9613 9615 }
9614 9616
9615 9617 if (level != MIB2_TCP) {
9616 9618 if ((mpctl = udp_snmp_get(q, mpctl, legacy_req)) == NULL) {
9617 9619 return (1);
9618 9620 }
9619 9621 }
9620 9622
9621 9623 if (level != MIB2_UDP) {
9622 9624 if ((mpctl = tcp_snmp_get(q, mpctl, legacy_req)) == NULL) {
9623 9625 return (1);
9624 9626 }
9625 9627 }
9626 9628
9627 9629 if ((mpctl = ip_snmp_get_mib2_ip_traffic_stats(q, mpctl,
9628 9630 ipst, legacy_req)) == NULL) {
9629 9631 return (1);
9630 9632 }
9631 9633
9632 9634 if ((mpctl = ip_snmp_get_mib2_ip6(q, mpctl, ipst,
9633 9635 legacy_req)) == NULL) {
9634 9636 return (1);
9635 9637 }
9636 9638
9637 9639 if ((mpctl = ip_snmp_get_mib2_icmp(q, mpctl, ipst)) == NULL) {
9638 9640 return (1);
9639 9641 }
9640 9642
9641 9643 if ((mpctl = ip_snmp_get_mib2_icmp6(q, mpctl, ipst)) == NULL) {
9642 9644 return (1);
9643 9645 }
9644 9646
9645 9647 if ((mpctl = ip_snmp_get_mib2_igmp(q, mpctl, ipst)) == NULL) {
9646 9648 return (1);
9647 9649 }
9648 9650
9649 9651 if ((mpctl = ip_snmp_get_mib2_multi(q, mpctl, ipst)) == NULL) {
9650 9652 return (1);
9651 9653 }
9652 9654
9653 9655 if ((mpctl = ip_snmp_get_mib2_ip_addr(q, mpctl, ipst,
9654 9656 legacy_req)) == NULL) {
9655 9657 return (1);
9656 9658 }
9657 9659
9658 9660 if ((mpctl = ip_snmp_get_mib2_ip6_addr(q, mpctl, ipst,
9659 9661 legacy_req)) == NULL) {
9660 9662 return (1);
9661 9663 }
9662 9664
9663 9665 if ((mpctl = ip_snmp_get_mib2_ip_group_mem(q, mpctl, ipst)) == NULL) {
9664 9666 return (1);
9665 9667 }
9666 9668
9667 9669 if ((mpctl = ip_snmp_get_mib2_ip6_group_mem(q, mpctl, ipst)) == NULL) {
9668 9670 return (1);
9669 9671 }
9670 9672
9671 9673 if ((mpctl = ip_snmp_get_mib2_ip_group_src(q, mpctl, ipst)) == NULL) {
9672 9674 return (1);
9673 9675 }
9674 9676
9675 9677 if ((mpctl = ip_snmp_get_mib2_ip6_group_src(q, mpctl, ipst)) == NULL) {
9676 9678 return (1);
9677 9679 }
9678 9680
9679 9681 if ((mpctl = ip_snmp_get_mib2_virt_multi(q, mpctl, ipst)) == NULL) {
9680 9682 return (1);
9681 9683 }
9682 9684
9683 9685 if ((mpctl = ip_snmp_get_mib2_multi_rtable(q, mpctl, ipst)) == NULL) {
9684 9686 return (1);
9685 9687 }
9686 9688
9687 9689 mpctl = ip_snmp_get_mib2_ip_route_media(q, mpctl, level, ipst);
9688 9690 if (mpctl == NULL)
9689 9691 return (1);
9690 9692
9691 9693 mpctl = ip_snmp_get_mib2_ip6_route_media(q, mpctl, level, ipst);
9692 9694 if (mpctl == NULL)
9693 9695 return (1);
9694 9696
9695 9697 if ((mpctl = sctp_snmp_get_mib2(q, mpctl, sctps)) == NULL) {
9696 9698 return (1);
9697 9699 }
9698 9700 if ((mpctl = ip_snmp_get_mib2_ip_dce(q, mpctl, ipst)) == NULL) {
9699 9701 return (1);
9700 9702 }
9701 9703 freemsg(mpctl);
9702 9704 return (1);
9703 9705 }
9704 9706
9705 9707 /* Get global (legacy) IPv4 statistics */
9706 9708 static mblk_t *
9707 9709 ip_snmp_get_mib2_ip(queue_t *q, mblk_t *mpctl, mib2_ipIfStatsEntry_t *ipmib,
9708 9710 ip_stack_t *ipst, boolean_t legacy_req)
9709 9711 {
9710 9712 mib2_ip_t old_ip_mib;
9711 9713 struct opthdr *optp;
9712 9714 mblk_t *mp2ctl;
9713 9715 mib2_ipAddrEntry_t mae;
9714 9716
9715 9717 /*
9716 9718 * make a copy of the original message
9717 9719 */
9718 9720 mp2ctl = copymsg(mpctl);
9719 9721
9720 9722 /* fixed length IP structure... */
9721 9723 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9722 9724 optp->level = MIB2_IP;
9723 9725 optp->name = 0;
9724 9726 SET_MIB(old_ip_mib.ipForwarding,
9725 9727 (WE_ARE_FORWARDING(ipst) ? 1 : 2));
9726 9728 SET_MIB(old_ip_mib.ipDefaultTTL,
9727 9729 (uint32_t)ipst->ips_ip_def_ttl);
9728 9730 SET_MIB(old_ip_mib.ipReasmTimeout,
9729 9731 ipst->ips_ip_reassembly_timeout);
9730 9732 SET_MIB(old_ip_mib.ipAddrEntrySize,
9731 9733 (legacy_req) ? LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t) :
9732 9734 sizeof (mib2_ipAddrEntry_t));
9733 9735 SET_MIB(old_ip_mib.ipRouteEntrySize,
9734 9736 sizeof (mib2_ipRouteEntry_t));
9735 9737 SET_MIB(old_ip_mib.ipNetToMediaEntrySize,
9736 9738 sizeof (mib2_ipNetToMediaEntry_t));
9737 9739 SET_MIB(old_ip_mib.ipMemberEntrySize, sizeof (ip_member_t));
9738 9740 SET_MIB(old_ip_mib.ipGroupSourceEntrySize, sizeof (ip_grpsrc_t));
9739 9741 SET_MIB(old_ip_mib.ipRouteAttributeSize,
9740 9742 sizeof (mib2_ipAttributeEntry_t));
9741 9743 SET_MIB(old_ip_mib.transportMLPSize, sizeof (mib2_transportMLPEntry_t));
9742 9744 SET_MIB(old_ip_mib.ipDestEntrySize, sizeof (dest_cache_entry_t));
9743 9745
9744 9746 /*
9745 9747 * Grab the statistics from the new IP MIB
9746 9748 */
9747 9749 SET_MIB(old_ip_mib.ipInReceives,
9748 9750 (uint32_t)ipmib->ipIfStatsHCInReceives);
9749 9751 SET_MIB(old_ip_mib.ipInHdrErrors, ipmib->ipIfStatsInHdrErrors);
9750 9752 SET_MIB(old_ip_mib.ipInAddrErrors, ipmib->ipIfStatsInAddrErrors);
9751 9753 SET_MIB(old_ip_mib.ipForwDatagrams,
9752 9754 (uint32_t)ipmib->ipIfStatsHCOutForwDatagrams);
9753 9755 SET_MIB(old_ip_mib.ipInUnknownProtos,
9754 9756 ipmib->ipIfStatsInUnknownProtos);
9755 9757 SET_MIB(old_ip_mib.ipInDiscards, ipmib->ipIfStatsInDiscards);
9756 9758 SET_MIB(old_ip_mib.ipInDelivers,
9757 9759 (uint32_t)ipmib->ipIfStatsHCInDelivers);
9758 9760 SET_MIB(old_ip_mib.ipOutRequests,
9759 9761 (uint32_t)ipmib->ipIfStatsHCOutRequests);
9760 9762 SET_MIB(old_ip_mib.ipOutDiscards, ipmib->ipIfStatsOutDiscards);
9761 9763 SET_MIB(old_ip_mib.ipOutNoRoutes, ipmib->ipIfStatsOutNoRoutes);
9762 9764 SET_MIB(old_ip_mib.ipReasmReqds, ipmib->ipIfStatsReasmReqds);
9763 9765 SET_MIB(old_ip_mib.ipReasmOKs, ipmib->ipIfStatsReasmOKs);
9764 9766 SET_MIB(old_ip_mib.ipReasmFails, ipmib->ipIfStatsReasmFails);
9765 9767 SET_MIB(old_ip_mib.ipFragOKs, ipmib->ipIfStatsOutFragOKs);
9766 9768 SET_MIB(old_ip_mib.ipFragFails, ipmib->ipIfStatsOutFragFails);
9767 9769 SET_MIB(old_ip_mib.ipFragCreates, ipmib->ipIfStatsOutFragCreates);
9768 9770
9769 9771 /* ipRoutingDiscards is not being used */
9770 9772 SET_MIB(old_ip_mib.ipRoutingDiscards, 0);
9771 9773 SET_MIB(old_ip_mib.tcpInErrs, ipmib->tcpIfStatsInErrs);
9772 9774 SET_MIB(old_ip_mib.udpNoPorts, ipmib->udpIfStatsNoPorts);
9773 9775 SET_MIB(old_ip_mib.ipInCksumErrs, ipmib->ipIfStatsInCksumErrs);
9774 9776 SET_MIB(old_ip_mib.ipReasmDuplicates,
9775 9777 ipmib->ipIfStatsReasmDuplicates);
9776 9778 SET_MIB(old_ip_mib.ipReasmPartDups, ipmib->ipIfStatsReasmPartDups);
9777 9779 SET_MIB(old_ip_mib.ipForwProhibits, ipmib->ipIfStatsForwProhibits);
9778 9780 SET_MIB(old_ip_mib.udpInCksumErrs, ipmib->udpIfStatsInCksumErrs);
9779 9781 SET_MIB(old_ip_mib.udpInOverflows, ipmib->udpIfStatsInOverflows);
9780 9782 SET_MIB(old_ip_mib.rawipInOverflows,
9781 9783 ipmib->rawipIfStatsInOverflows);
9782 9784
9783 9785 SET_MIB(old_ip_mib.ipsecInSucceeded, ipmib->ipsecIfStatsInSucceeded);
9784 9786 SET_MIB(old_ip_mib.ipsecInFailed, ipmib->ipsecIfStatsInFailed);
9785 9787 SET_MIB(old_ip_mib.ipInIPv6, ipmib->ipIfStatsInWrongIPVersion);
9786 9788 SET_MIB(old_ip_mib.ipOutIPv6, ipmib->ipIfStatsOutWrongIPVersion);
9787 9789 SET_MIB(old_ip_mib.ipOutSwitchIPv6,
9788 9790 ipmib->ipIfStatsOutSwitchIPVersion);
9789 9791
9790 9792 if (!snmp_append_data(mpctl->b_cont, (char *)&old_ip_mib,
9791 9793 (int)sizeof (old_ip_mib))) {
9792 9794 ip1dbg(("ip_snmp_get_mib2_ip: failed to allocate %u bytes\n",
9793 9795 (uint_t)sizeof (old_ip_mib)));
9794 9796 }
9795 9797
9796 9798 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9797 9799 ip3dbg(("ip_snmp_get_mib2_ip: level %d, name %d, len %d\n",
9798 9800 (int)optp->level, (int)optp->name, (int)optp->len));
9799 9801 qreply(q, mpctl);
9800 9802 return (mp2ctl);
9801 9803 }
9802 9804
9803 9805 /* Per interface IPv4 statistics */
9804 9806 static mblk_t *
9805 9807 ip_snmp_get_mib2_ip_traffic_stats(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
9806 9808 boolean_t legacy_req)
9807 9809 {
9808 9810 struct opthdr *optp;
9809 9811 mblk_t *mp2ctl;
9810 9812 ill_t *ill;
9811 9813 ill_walk_context_t ctx;
9812 9814 mblk_t *mp_tail = NULL;
9813 9815 mib2_ipIfStatsEntry_t global_ip_mib;
9814 9816 mib2_ipAddrEntry_t mae;
9815 9817
9816 9818 /*
9817 9819 * Make a copy of the original message
9818 9820 */
9819 9821 mp2ctl = copymsg(mpctl);
9820 9822
9821 9823 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9822 9824 optp->level = MIB2_IP;
9823 9825 optp->name = MIB2_IP_TRAFFIC_STATS;
9824 9826 /* Include "unknown interface" ip_mib */
9825 9827 ipst->ips_ip_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv4;
9826 9828 ipst->ips_ip_mib.ipIfStatsIfIndex =
9827 9829 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */
9828 9830 SET_MIB(ipst->ips_ip_mib.ipIfStatsForwarding,
9829 9831 (ipst->ips_ip_forwarding ? 1 : 2));
9830 9832 SET_MIB(ipst->ips_ip_mib.ipIfStatsDefaultTTL,
9831 9833 (uint32_t)ipst->ips_ip_def_ttl);
9832 9834 SET_MIB(ipst->ips_ip_mib.ipIfStatsEntrySize,
9833 9835 sizeof (mib2_ipIfStatsEntry_t));
9834 9836 SET_MIB(ipst->ips_ip_mib.ipIfStatsAddrEntrySize,
9835 9837 sizeof (mib2_ipAddrEntry_t));
9836 9838 SET_MIB(ipst->ips_ip_mib.ipIfStatsRouteEntrySize,
9837 9839 sizeof (mib2_ipRouteEntry_t));
9838 9840 SET_MIB(ipst->ips_ip_mib.ipIfStatsNetToMediaEntrySize,
9839 9841 sizeof (mib2_ipNetToMediaEntry_t));
9840 9842 SET_MIB(ipst->ips_ip_mib.ipIfStatsMemberEntrySize,
9841 9843 sizeof (ip_member_t));
9842 9844 SET_MIB(ipst->ips_ip_mib.ipIfStatsGroupSourceEntrySize,
9843 9845 sizeof (ip_grpsrc_t));
9844 9846
9845 9847 bcopy(&ipst->ips_ip_mib, &global_ip_mib, sizeof (global_ip_mib));
9846 9848
9847 9849 if (legacy_req) {
9848 9850 SET_MIB(global_ip_mib.ipIfStatsAddrEntrySize,
9849 9851 LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t));
9850 9852 }
9851 9853
9852 9854 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9853 9855 (char *)&global_ip_mib, (int)sizeof (global_ip_mib))) {
9854 9856 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9855 9857 "failed to allocate %u bytes\n",
9856 9858 (uint_t)sizeof (global_ip_mib)));
9857 9859 }
9858 9860
9859 9861 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
9860 9862 ill = ILL_START_WALK_V4(&ctx, ipst);
9861 9863 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
9862 9864 ill->ill_ip_mib->ipIfStatsIfIndex =
9863 9865 ill->ill_phyint->phyint_ifindex;
9864 9866 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding,
9865 9867 (ipst->ips_ip_forwarding ? 1 : 2));
9866 9868 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultTTL,
9867 9869 (uint32_t)ipst->ips_ip_def_ttl);
9868 9870
9869 9871 ip_mib2_add_ip_stats(&global_ip_mib, ill->ill_ip_mib);
9870 9872 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
9871 9873 (char *)ill->ill_ip_mib,
9872 9874 (int)sizeof (*ill->ill_ip_mib))) {
9873 9875 ip1dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9874 9876 "failed to allocate %u bytes\n",
9875 9877 (uint_t)sizeof (*ill->ill_ip_mib)));
9876 9878 }
9877 9879 }
9878 9880 rw_exit(&ipst->ips_ill_g_lock);
9879 9881
9880 9882 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9881 9883 ip3dbg(("ip_snmp_get_mib2_ip_traffic_stats: "
9882 9884 "level %d, name %d, len %d\n",
9883 9885 (int)optp->level, (int)optp->name, (int)optp->len));
9884 9886 qreply(q, mpctl);
9885 9887
9886 9888 if (mp2ctl == NULL)
9887 9889 return (NULL);
9888 9890
9889 9891 return (ip_snmp_get_mib2_ip(q, mp2ctl, &global_ip_mib, ipst,
9890 9892 legacy_req));
9891 9893 }
9892 9894
9893 9895 /* Global IPv4 ICMP statistics */
9894 9896 static mblk_t *
9895 9897 ip_snmp_get_mib2_icmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9896 9898 {
9897 9899 struct opthdr *optp;
9898 9900 mblk_t *mp2ctl;
9899 9901
9900 9902 /*
9901 9903 * Make a copy of the original message
9902 9904 */
9903 9905 mp2ctl = copymsg(mpctl);
9904 9906
9905 9907 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9906 9908 optp->level = MIB2_ICMP;
9907 9909 optp->name = 0;
9908 9910 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_icmp_mib,
9909 9911 (int)sizeof (ipst->ips_icmp_mib))) {
9910 9912 ip1dbg(("ip_snmp_get_mib2_icmp: failed to allocate %u bytes\n",
9911 9913 (uint_t)sizeof (ipst->ips_icmp_mib)));
9912 9914 }
9913 9915 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9914 9916 ip3dbg(("ip_snmp_get_mib2_icmp: level %d, name %d, len %d\n",
9915 9917 (int)optp->level, (int)optp->name, (int)optp->len));
9916 9918 qreply(q, mpctl);
9917 9919 return (mp2ctl);
9918 9920 }
9919 9921
9920 9922 /* Global IPv4 IGMP statistics */
9921 9923 static mblk_t *
9922 9924 ip_snmp_get_mib2_igmp(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9923 9925 {
9924 9926 struct opthdr *optp;
9925 9927 mblk_t *mp2ctl;
9926 9928
9927 9929 /*
9928 9930 * make a copy of the original message
9929 9931 */
9930 9932 mp2ctl = copymsg(mpctl);
9931 9933
9932 9934 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9933 9935 optp->level = EXPER_IGMP;
9934 9936 optp->name = 0;
9935 9937 if (!snmp_append_data(mpctl->b_cont, (char *)&ipst->ips_igmpstat,
9936 9938 (int)sizeof (ipst->ips_igmpstat))) {
9937 9939 ip1dbg(("ip_snmp_get_mib2_igmp: failed to allocate %u bytes\n",
9938 9940 (uint_t)sizeof (ipst->ips_igmpstat)));
9939 9941 }
9940 9942 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9941 9943 ip3dbg(("ip_snmp_get_mib2_igmp: level %d, name %d, len %d\n",
9942 9944 (int)optp->level, (int)optp->name, (int)optp->len));
9943 9945 qreply(q, mpctl);
9944 9946 return (mp2ctl);
9945 9947 }
9946 9948
9947 9949 /* Global IPv4 Multicast Routing statistics */
9948 9950 static mblk_t *
9949 9951 ip_snmp_get_mib2_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
9950 9952 {
9951 9953 struct opthdr *optp;
9952 9954 mblk_t *mp2ctl;
9953 9955
9954 9956 /*
9955 9957 * make a copy of the original message
9956 9958 */
9957 9959 mp2ctl = copymsg(mpctl);
9958 9960
9959 9961 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9960 9962 optp->level = EXPER_DVMRP;
9961 9963 optp->name = 0;
9962 9964 if (!ip_mroute_stats(mpctl->b_cont, ipst)) {
9963 9965 ip0dbg(("ip_mroute_stats: failed\n"));
9964 9966 }
9965 9967 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
9966 9968 ip3dbg(("ip_snmp_get_mib2_multi: level %d, name %d, len %d\n",
9967 9969 (int)optp->level, (int)optp->name, (int)optp->len));
9968 9970 qreply(q, mpctl);
9969 9971 return (mp2ctl);
9970 9972 }
9971 9973
9972 9974 /* IPv4 address information */
9973 9975 static mblk_t *
9974 9976 ip_snmp_get_mib2_ip_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
9975 9977 boolean_t legacy_req)
9976 9978 {
9977 9979 struct opthdr *optp;
9978 9980 mblk_t *mp2ctl;
9979 9981 mblk_t *mp_tail = NULL;
9980 9982 ill_t *ill;
9981 9983 ipif_t *ipif;
9982 9984 uint_t bitval;
9983 9985 mib2_ipAddrEntry_t mae;
9984 9986 size_t mae_size;
9985 9987 zoneid_t zoneid;
9986 9988 ill_walk_context_t ctx;
9987 9989
9988 9990 /*
9989 9991 * make a copy of the original message
9990 9992 */
9991 9993 mp2ctl = copymsg(mpctl);
9992 9994
9993 9995 mae_size = (legacy_req) ? LEGACY_MIB_SIZE(&mae, mib2_ipAddrEntry_t) :
9994 9996 sizeof (mib2_ipAddrEntry_t);
9995 9997
9996 9998 /* ipAddrEntryTable */
9997 9999
9998 10000 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
9999 10001 optp->level = MIB2_IP;
10000 10002 optp->name = MIB2_IP_ADDR;
10001 10003 zoneid = Q_TO_CONN(q)->conn_zoneid;
10002 10004
10003 10005 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10004 10006 ill = ILL_START_WALK_V4(&ctx, ipst);
10005 10007 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10006 10008 for (ipif = ill->ill_ipif; ipif != NULL;
10007 10009 ipif = ipif->ipif_next) {
10008 10010 if (ipif->ipif_zoneid != zoneid &&
10009 10011 ipif->ipif_zoneid != ALL_ZONES)
10010 10012 continue;
10011 10013 /* Sum of count from dead IRE_LO* and our current */
10012 10014 mae.ipAdEntInfo.ae_ibcnt = ipif->ipif_ib_pkt_count;
10013 10015 if (ipif->ipif_ire_local != NULL) {
10014 10016 mae.ipAdEntInfo.ae_ibcnt +=
10015 10017 ipif->ipif_ire_local->ire_ib_pkt_count;
10016 10018 }
10017 10019 mae.ipAdEntInfo.ae_obcnt = 0;
10018 10020 mae.ipAdEntInfo.ae_focnt = 0;
10019 10021
10020 10022 ipif_get_name(ipif, mae.ipAdEntIfIndex.o_bytes,
10021 10023 OCTET_LENGTH);
10022 10024 mae.ipAdEntIfIndex.o_length =
10023 10025 mi_strlen(mae.ipAdEntIfIndex.o_bytes);
10024 10026 mae.ipAdEntAddr = ipif->ipif_lcl_addr;
10025 10027 mae.ipAdEntNetMask = ipif->ipif_net_mask;
10026 10028 mae.ipAdEntInfo.ae_subnet = ipif->ipif_subnet;
10027 10029 mae.ipAdEntInfo.ae_subnet_len =
10028 10030 ip_mask_to_plen(ipif->ipif_net_mask);
10029 10031 mae.ipAdEntInfo.ae_src_addr = ipif->ipif_lcl_addr;
10030 10032 for (bitval = 1;
10031 10033 bitval &&
10032 10034 !(bitval & ipif->ipif_brd_addr);
10033 10035 bitval <<= 1)
10034 10036 noop;
10035 10037 mae.ipAdEntBcastAddr = bitval;
10036 10038 mae.ipAdEntReasmMaxSize = IP_MAXPACKET;
10037 10039 mae.ipAdEntInfo.ae_mtu = ipif->ipif_ill->ill_mtu;
10038 10040 mae.ipAdEntInfo.ae_metric = ipif->ipif_ill->ill_metric;
10039 10041 mae.ipAdEntInfo.ae_broadcast_addr =
10040 10042 ipif->ipif_brd_addr;
10041 10043 mae.ipAdEntInfo.ae_pp_dst_addr =
10042 10044 ipif->ipif_pp_dst_addr;
10043 10045 mae.ipAdEntInfo.ae_flags = ipif->ipif_flags |
10044 10046 ill->ill_flags | ill->ill_phyint->phyint_flags;
10045 10047 mae.ipAdEntRetransmitTime =
10046 10048 ill->ill_reachable_retrans_time;
10047 10049
10048 10050 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10049 10051 (char *)&mae, (int)mae_size)) {
10050 10052 ip1dbg(("ip_snmp_get_mib2_ip_addr: failed to "
10051 10053 "allocate %u bytes\n", (uint_t)mae_size));
10052 10054 }
10053 10055 }
10054 10056 }
10055 10057 rw_exit(&ipst->ips_ill_g_lock);
10056 10058
10057 10059 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10058 10060 ip3dbg(("ip_snmp_get_mib2_ip_addr: level %d, name %d, len %d\n",
10059 10061 (int)optp->level, (int)optp->name, (int)optp->len));
10060 10062 qreply(q, mpctl);
10061 10063 return (mp2ctl);
10062 10064 }
10063 10065
10064 10066 /* IPv6 address information */
10065 10067 static mblk_t *
10066 10068 ip_snmp_get_mib2_ip6_addr(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
10067 10069 boolean_t legacy_req)
10068 10070 {
10069 10071 struct opthdr *optp;
10070 10072 mblk_t *mp2ctl;
10071 10073 mblk_t *mp_tail = NULL;
10072 10074 ill_t *ill;
10073 10075 ipif_t *ipif;
10074 10076 mib2_ipv6AddrEntry_t mae6;
10075 10077 size_t mae6_size;
10076 10078 zoneid_t zoneid;
10077 10079 ill_walk_context_t ctx;
10078 10080
10079 10081 /*
10080 10082 * make a copy of the original message
10081 10083 */
10082 10084 mp2ctl = copymsg(mpctl);
10083 10085
10084 10086 mae6_size = (legacy_req) ?
10085 10087 LEGACY_MIB_SIZE(&mae6, mib2_ipv6AddrEntry_t) :
10086 10088 sizeof (mib2_ipv6AddrEntry_t);
10087 10089
10088 10090 /* ipv6AddrEntryTable */
10089 10091
10090 10092 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10091 10093 optp->level = MIB2_IP6;
10092 10094 optp->name = MIB2_IP6_ADDR;
10093 10095 zoneid = Q_TO_CONN(q)->conn_zoneid;
10094 10096
10095 10097 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10096 10098 ill = ILL_START_WALK_V6(&ctx, ipst);
10097 10099 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10098 10100 for (ipif = ill->ill_ipif; ipif != NULL;
10099 10101 ipif = ipif->ipif_next) {
10100 10102 if (ipif->ipif_zoneid != zoneid &&
10101 10103 ipif->ipif_zoneid != ALL_ZONES)
10102 10104 continue;
10103 10105 /* Sum of count from dead IRE_LO* and our current */
10104 10106 mae6.ipv6AddrInfo.ae_ibcnt = ipif->ipif_ib_pkt_count;
10105 10107 if (ipif->ipif_ire_local != NULL) {
10106 10108 mae6.ipv6AddrInfo.ae_ibcnt +=
10107 10109 ipif->ipif_ire_local->ire_ib_pkt_count;
10108 10110 }
10109 10111 mae6.ipv6AddrInfo.ae_obcnt = 0;
10110 10112 mae6.ipv6AddrInfo.ae_focnt = 0;
10111 10113
10112 10114 ipif_get_name(ipif, mae6.ipv6AddrIfIndex.o_bytes,
10113 10115 OCTET_LENGTH);
10114 10116 mae6.ipv6AddrIfIndex.o_length =
10115 10117 mi_strlen(mae6.ipv6AddrIfIndex.o_bytes);
10116 10118 mae6.ipv6AddrAddress = ipif->ipif_v6lcl_addr;
10117 10119 mae6.ipv6AddrPfxLength =
10118 10120 ip_mask_to_plen_v6(&ipif->ipif_v6net_mask);
10119 10121 mae6.ipv6AddrInfo.ae_subnet = ipif->ipif_v6subnet;
10120 10122 mae6.ipv6AddrInfo.ae_subnet_len =
10121 10123 mae6.ipv6AddrPfxLength;
10122 10124 mae6.ipv6AddrInfo.ae_src_addr = ipif->ipif_v6lcl_addr;
10123 10125
10124 10126 /* Type: stateless(1), stateful(2), unknown(3) */
10125 10127 if (ipif->ipif_flags & IPIF_ADDRCONF)
10126 10128 mae6.ipv6AddrType = 1;
10127 10129 else
10128 10130 mae6.ipv6AddrType = 2;
10129 10131 /* Anycast: true(1), false(2) */
10130 10132 if (ipif->ipif_flags & IPIF_ANYCAST)
10131 10133 mae6.ipv6AddrAnycastFlag = 1;
10132 10134 else
10133 10135 mae6.ipv6AddrAnycastFlag = 2;
10134 10136
10135 10137 /*
10136 10138 * Address status: preferred(1), deprecated(2),
10137 10139 * invalid(3), inaccessible(4), unknown(5)
10138 10140 */
10139 10141 if (ipif->ipif_flags & IPIF_NOLOCAL)
10140 10142 mae6.ipv6AddrStatus = 3;
10141 10143 else if (ipif->ipif_flags & IPIF_DEPRECATED)
10142 10144 mae6.ipv6AddrStatus = 2;
10143 10145 else
10144 10146 mae6.ipv6AddrStatus = 1;
10145 10147 mae6.ipv6AddrInfo.ae_mtu = ipif->ipif_ill->ill_mtu;
10146 10148 mae6.ipv6AddrInfo.ae_metric =
10147 10149 ipif->ipif_ill->ill_metric;
10148 10150 mae6.ipv6AddrInfo.ae_pp_dst_addr =
10149 10151 ipif->ipif_v6pp_dst_addr;
10150 10152 mae6.ipv6AddrInfo.ae_flags = ipif->ipif_flags |
10151 10153 ill->ill_flags | ill->ill_phyint->phyint_flags;
10152 10154 mae6.ipv6AddrReasmMaxSize = IP_MAXPACKET;
10153 10155 mae6.ipv6AddrIdentifier = ill->ill_token;
10154 10156 mae6.ipv6AddrIdentifierLen = ill->ill_token_length;
10155 10157 mae6.ipv6AddrReachableTime = ill->ill_reachable_time;
10156 10158 mae6.ipv6AddrRetransmitTime =
10157 10159 ill->ill_reachable_retrans_time;
10158 10160 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10159 10161 (char *)&mae6, (int)mae6_size)) {
10160 10162 ip1dbg(("ip_snmp_get_mib2_ip6_addr: failed to "
10161 10163 "allocate %u bytes\n",
10162 10164 (uint_t)mae6_size));
10163 10165 }
10164 10166 }
10165 10167 }
10166 10168 rw_exit(&ipst->ips_ill_g_lock);
10167 10169
10168 10170 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10169 10171 ip3dbg(("ip_snmp_get_mib2_ip6_addr: level %d, name %d, len %d\n",
10170 10172 (int)optp->level, (int)optp->name, (int)optp->len));
10171 10173 qreply(q, mpctl);
10172 10174 return (mp2ctl);
10173 10175 }
10174 10176
10175 10177 /* IPv4 multicast group membership. */
10176 10178 static mblk_t *
10177 10179 ip_snmp_get_mib2_ip_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10178 10180 {
10179 10181 struct opthdr *optp;
10180 10182 mblk_t *mp2ctl;
10181 10183 ill_t *ill;
10182 10184 ipif_t *ipif;
10183 10185 ilm_t *ilm;
10184 10186 ip_member_t ipm;
10185 10187 mblk_t *mp_tail = NULL;
10186 10188 ill_walk_context_t ctx;
10187 10189 zoneid_t zoneid;
10188 10190
10189 10191 /*
10190 10192 * make a copy of the original message
10191 10193 */
10192 10194 mp2ctl = copymsg(mpctl);
10193 10195 zoneid = Q_TO_CONN(q)->conn_zoneid;
10194 10196
10195 10197 /* ipGroupMember table */
10196 10198 optp = (struct opthdr *)&mpctl->b_rptr[
10197 10199 sizeof (struct T_optmgmt_ack)];
10198 10200 optp->level = MIB2_IP;
10199 10201 optp->name = EXPER_IP_GROUP_MEMBERSHIP;
10200 10202
10201 10203 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10202 10204 ill = ILL_START_WALK_V4(&ctx, ipst);
10203 10205 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10204 10206 /* Make sure the ill isn't going away. */
10205 10207 if (!ill_check_and_refhold(ill))
10206 10208 continue;
10207 10209 rw_exit(&ipst->ips_ill_g_lock);
10208 10210 rw_enter(&ill->ill_mcast_lock, RW_READER);
10209 10211 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10210 10212 if (ilm->ilm_zoneid != zoneid &&
10211 10213 ilm->ilm_zoneid != ALL_ZONES)
10212 10214 continue;
10213 10215
10214 10216 /* Is there an ipif for ilm_ifaddr? */
10215 10217 for (ipif = ill->ill_ipif; ipif != NULL;
10216 10218 ipif = ipif->ipif_next) {
10217 10219 if (!IPIF_IS_CONDEMNED(ipif) &&
10218 10220 ipif->ipif_lcl_addr == ilm->ilm_ifaddr &&
10219 10221 ilm->ilm_ifaddr != INADDR_ANY)
10220 10222 break;
10221 10223 }
10222 10224 if (ipif != NULL) {
10223 10225 ipif_get_name(ipif,
10224 10226 ipm.ipGroupMemberIfIndex.o_bytes,
10225 10227 OCTET_LENGTH);
10226 10228 } else {
10227 10229 ill_get_name(ill,
10228 10230 ipm.ipGroupMemberIfIndex.o_bytes,
10229 10231 OCTET_LENGTH);
10230 10232 }
10231 10233 ipm.ipGroupMemberIfIndex.o_length =
10232 10234 mi_strlen(ipm.ipGroupMemberIfIndex.o_bytes);
10233 10235
10234 10236 ipm.ipGroupMemberAddress = ilm->ilm_addr;
10235 10237 ipm.ipGroupMemberRefCnt = ilm->ilm_refcnt;
10236 10238 ipm.ipGroupMemberFilterMode = ilm->ilm_fmode;
10237 10239 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10238 10240 (char *)&ipm, (int)sizeof (ipm))) {
10239 10241 ip1dbg(("ip_snmp_get_mib2_ip_group: "
10240 10242 "failed to allocate %u bytes\n",
10241 10243 (uint_t)sizeof (ipm)));
10242 10244 }
10243 10245 }
10244 10246 rw_exit(&ill->ill_mcast_lock);
10245 10247 ill_refrele(ill);
10246 10248 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10247 10249 }
10248 10250 rw_exit(&ipst->ips_ill_g_lock);
10249 10251 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10250 10252 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10251 10253 (int)optp->level, (int)optp->name, (int)optp->len));
10252 10254 qreply(q, mpctl);
10253 10255 return (mp2ctl);
10254 10256 }
10255 10257
10256 10258 /* IPv6 multicast group membership. */
10257 10259 static mblk_t *
10258 10260 ip_snmp_get_mib2_ip6_group_mem(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10259 10261 {
10260 10262 struct opthdr *optp;
10261 10263 mblk_t *mp2ctl;
10262 10264 ill_t *ill;
10263 10265 ilm_t *ilm;
10264 10266 ipv6_member_t ipm6;
10265 10267 mblk_t *mp_tail = NULL;
10266 10268 ill_walk_context_t ctx;
10267 10269 zoneid_t zoneid;
10268 10270
10269 10271 /*
10270 10272 * make a copy of the original message
10271 10273 */
10272 10274 mp2ctl = copymsg(mpctl);
10273 10275 zoneid = Q_TO_CONN(q)->conn_zoneid;
10274 10276
10275 10277 /* ip6GroupMember table */
10276 10278 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10277 10279 optp->level = MIB2_IP6;
10278 10280 optp->name = EXPER_IP6_GROUP_MEMBERSHIP;
10279 10281
10280 10282 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10281 10283 ill = ILL_START_WALK_V6(&ctx, ipst);
10282 10284 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10283 10285 /* Make sure the ill isn't going away. */
10284 10286 if (!ill_check_and_refhold(ill))
10285 10287 continue;
10286 10288 rw_exit(&ipst->ips_ill_g_lock);
10287 10289 /*
10288 10290 * Normally we don't have any members on under IPMP interfaces.
10289 10291 * We report them as a debugging aid.
10290 10292 */
10291 10293 rw_enter(&ill->ill_mcast_lock, RW_READER);
10292 10294 ipm6.ipv6GroupMemberIfIndex = ill->ill_phyint->phyint_ifindex;
10293 10295 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10294 10296 if (ilm->ilm_zoneid != zoneid &&
10295 10297 ilm->ilm_zoneid != ALL_ZONES)
10296 10298 continue; /* not this zone */
10297 10299 ipm6.ipv6GroupMemberAddress = ilm->ilm_v6addr;
10298 10300 ipm6.ipv6GroupMemberRefCnt = ilm->ilm_refcnt;
10299 10301 ipm6.ipv6GroupMemberFilterMode = ilm->ilm_fmode;
10300 10302 if (!snmp_append_data2(mpctl->b_cont,
10301 10303 &mp_tail,
10302 10304 (char *)&ipm6, (int)sizeof (ipm6))) {
10303 10305 ip1dbg(("ip_snmp_get_mib2_ip6_group: "
10304 10306 "failed to allocate %u bytes\n",
10305 10307 (uint_t)sizeof (ipm6)));
10306 10308 }
10307 10309 }
10308 10310 rw_exit(&ill->ill_mcast_lock);
10309 10311 ill_refrele(ill);
10310 10312 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10311 10313 }
10312 10314 rw_exit(&ipst->ips_ill_g_lock);
10313 10315
10314 10316 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10315 10317 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10316 10318 (int)optp->level, (int)optp->name, (int)optp->len));
10317 10319 qreply(q, mpctl);
10318 10320 return (mp2ctl);
10319 10321 }
10320 10322
10321 10323 /* IP multicast filtered sources */
10322 10324 static mblk_t *
10323 10325 ip_snmp_get_mib2_ip_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10324 10326 {
10325 10327 struct opthdr *optp;
10326 10328 mblk_t *mp2ctl;
10327 10329 ill_t *ill;
10328 10330 ipif_t *ipif;
10329 10331 ilm_t *ilm;
10330 10332 ip_grpsrc_t ips;
10331 10333 mblk_t *mp_tail = NULL;
10332 10334 ill_walk_context_t ctx;
10333 10335 zoneid_t zoneid;
10334 10336 int i;
10335 10337 slist_t *sl;
10336 10338
10337 10339 /*
10338 10340 * make a copy of the original message
10339 10341 */
10340 10342 mp2ctl = copymsg(mpctl);
10341 10343 zoneid = Q_TO_CONN(q)->conn_zoneid;
10342 10344
10343 10345 /* ipGroupSource table */
10344 10346 optp = (struct opthdr *)&mpctl->b_rptr[
10345 10347 sizeof (struct T_optmgmt_ack)];
10346 10348 optp->level = MIB2_IP;
10347 10349 optp->name = EXPER_IP_GROUP_SOURCES;
10348 10350
10349 10351 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10350 10352 ill = ILL_START_WALK_V4(&ctx, ipst);
10351 10353 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10352 10354 /* Make sure the ill isn't going away. */
10353 10355 if (!ill_check_and_refhold(ill))
10354 10356 continue;
10355 10357 rw_exit(&ipst->ips_ill_g_lock);
10356 10358 rw_enter(&ill->ill_mcast_lock, RW_READER);
10357 10359 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10358 10360 sl = ilm->ilm_filter;
10359 10361 if (ilm->ilm_zoneid != zoneid &&
10360 10362 ilm->ilm_zoneid != ALL_ZONES)
10361 10363 continue;
10362 10364 if (SLIST_IS_EMPTY(sl))
10363 10365 continue;
10364 10366
10365 10367 /* Is there an ipif for ilm_ifaddr? */
10366 10368 for (ipif = ill->ill_ipif; ipif != NULL;
10367 10369 ipif = ipif->ipif_next) {
10368 10370 if (!IPIF_IS_CONDEMNED(ipif) &&
10369 10371 ipif->ipif_lcl_addr == ilm->ilm_ifaddr &&
10370 10372 ilm->ilm_ifaddr != INADDR_ANY)
10371 10373 break;
10372 10374 }
10373 10375 if (ipif != NULL) {
10374 10376 ipif_get_name(ipif,
10375 10377 ips.ipGroupSourceIfIndex.o_bytes,
10376 10378 OCTET_LENGTH);
10377 10379 } else {
10378 10380 ill_get_name(ill,
10379 10381 ips.ipGroupSourceIfIndex.o_bytes,
10380 10382 OCTET_LENGTH);
10381 10383 }
10382 10384 ips.ipGroupSourceIfIndex.o_length =
10383 10385 mi_strlen(ips.ipGroupSourceIfIndex.o_bytes);
10384 10386
10385 10387 ips.ipGroupSourceGroup = ilm->ilm_addr;
10386 10388 for (i = 0; i < sl->sl_numsrc; i++) {
10387 10389 if (!IN6_IS_ADDR_V4MAPPED(&sl->sl_addr[i]))
10388 10390 continue;
10389 10391 IN6_V4MAPPED_TO_IPADDR(&sl->sl_addr[i],
10390 10392 ips.ipGroupSourceAddress);
10391 10393 if (snmp_append_data2(mpctl->b_cont, &mp_tail,
10392 10394 (char *)&ips, (int)sizeof (ips)) == 0) {
10393 10395 ip1dbg(("ip_snmp_get_mib2_ip_group_src:"
10394 10396 " failed to allocate %u bytes\n",
10395 10397 (uint_t)sizeof (ips)));
10396 10398 }
10397 10399 }
10398 10400 }
10399 10401 rw_exit(&ill->ill_mcast_lock);
10400 10402 ill_refrele(ill);
10401 10403 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10402 10404 }
10403 10405 rw_exit(&ipst->ips_ill_g_lock);
10404 10406 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10405 10407 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10406 10408 (int)optp->level, (int)optp->name, (int)optp->len));
10407 10409 qreply(q, mpctl);
10408 10410 return (mp2ctl);
10409 10411 }
10410 10412
10411 10413 /* IPv6 multicast filtered sources. */
10412 10414 static mblk_t *
10413 10415 ip_snmp_get_mib2_ip6_group_src(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10414 10416 {
10415 10417 struct opthdr *optp;
10416 10418 mblk_t *mp2ctl;
10417 10419 ill_t *ill;
10418 10420 ilm_t *ilm;
10419 10421 ipv6_grpsrc_t ips6;
10420 10422 mblk_t *mp_tail = NULL;
10421 10423 ill_walk_context_t ctx;
10422 10424 zoneid_t zoneid;
10423 10425 int i;
10424 10426 slist_t *sl;
10425 10427
10426 10428 /*
10427 10429 * make a copy of the original message
10428 10430 */
10429 10431 mp2ctl = copymsg(mpctl);
10430 10432 zoneid = Q_TO_CONN(q)->conn_zoneid;
10431 10433
10432 10434 /* ip6GroupMember table */
10433 10435 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10434 10436 optp->level = MIB2_IP6;
10435 10437 optp->name = EXPER_IP6_GROUP_SOURCES;
10436 10438
10437 10439 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10438 10440 ill = ILL_START_WALK_V6(&ctx, ipst);
10439 10441 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10440 10442 /* Make sure the ill isn't going away. */
10441 10443 if (!ill_check_and_refhold(ill))
10442 10444 continue;
10443 10445 rw_exit(&ipst->ips_ill_g_lock);
10444 10446 /*
10445 10447 * Normally we don't have any members on under IPMP interfaces.
10446 10448 * We report them as a debugging aid.
10447 10449 */
10448 10450 rw_enter(&ill->ill_mcast_lock, RW_READER);
10449 10451 ips6.ipv6GroupSourceIfIndex = ill->ill_phyint->phyint_ifindex;
10450 10452 for (ilm = ill->ill_ilm; ilm; ilm = ilm->ilm_next) {
10451 10453 sl = ilm->ilm_filter;
10452 10454 if (ilm->ilm_zoneid != zoneid &&
10453 10455 ilm->ilm_zoneid != ALL_ZONES)
10454 10456 continue;
10455 10457 if (SLIST_IS_EMPTY(sl))
10456 10458 continue;
10457 10459 ips6.ipv6GroupSourceGroup = ilm->ilm_v6addr;
10458 10460 for (i = 0; i < sl->sl_numsrc; i++) {
10459 10461 ips6.ipv6GroupSourceAddress = sl->sl_addr[i];
10460 10462 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10461 10463 (char *)&ips6, (int)sizeof (ips6))) {
10462 10464 ip1dbg(("ip_snmp_get_mib2_ip6_"
10463 10465 "group_src: failed to allocate "
10464 10466 "%u bytes\n",
10465 10467 (uint_t)sizeof (ips6)));
10466 10468 }
10467 10469 }
10468 10470 }
10469 10471 rw_exit(&ill->ill_mcast_lock);
10470 10472 ill_refrele(ill);
10471 10473 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10472 10474 }
10473 10475 rw_exit(&ipst->ips_ill_g_lock);
10474 10476
10475 10477 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10476 10478 ip3dbg(("ip_snmp_get: level %d, name %d, len %d\n",
10477 10479 (int)optp->level, (int)optp->name, (int)optp->len));
10478 10480 qreply(q, mpctl);
10479 10481 return (mp2ctl);
10480 10482 }
10481 10483
10482 10484 /* Multicast routing virtual interface table. */
10483 10485 static mblk_t *
10484 10486 ip_snmp_get_mib2_virt_multi(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10485 10487 {
10486 10488 struct opthdr *optp;
10487 10489 mblk_t *mp2ctl;
10488 10490
10489 10491 /*
10490 10492 * make a copy of the original message
10491 10493 */
10492 10494 mp2ctl = copymsg(mpctl);
10493 10495
10494 10496 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10495 10497 optp->level = EXPER_DVMRP;
10496 10498 optp->name = EXPER_DVMRP_VIF;
10497 10499 if (!ip_mroute_vif(mpctl->b_cont, ipst)) {
10498 10500 ip0dbg(("ip_mroute_vif: failed\n"));
10499 10501 }
10500 10502 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10501 10503 ip3dbg(("ip_snmp_get_mib2_virt_multi: level %d, name %d, len %d\n",
10502 10504 (int)optp->level, (int)optp->name, (int)optp->len));
10503 10505 qreply(q, mpctl);
10504 10506 return (mp2ctl);
10505 10507 }
10506 10508
10507 10509 /* Multicast routing table. */
10508 10510 static mblk_t *
10509 10511 ip_snmp_get_mib2_multi_rtable(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10510 10512 {
10511 10513 struct opthdr *optp;
10512 10514 mblk_t *mp2ctl;
10513 10515
10514 10516 /*
10515 10517 * make a copy of the original message
10516 10518 */
10517 10519 mp2ctl = copymsg(mpctl);
10518 10520
10519 10521 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10520 10522 optp->level = EXPER_DVMRP;
10521 10523 optp->name = EXPER_DVMRP_MRT;
10522 10524 if (!ip_mroute_mrt(mpctl->b_cont, ipst)) {
10523 10525 ip0dbg(("ip_mroute_mrt: failed\n"));
10524 10526 }
10525 10527 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10526 10528 ip3dbg(("ip_snmp_get_mib2_multi_rtable: level %d, name %d, len %d\n",
10527 10529 (int)optp->level, (int)optp->name, (int)optp->len));
10528 10530 qreply(q, mpctl);
10529 10531 return (mp2ctl);
10530 10532 }
10531 10533
10532 10534 /*
10533 10535 * Return ipRouteEntryTable, ipNetToMediaEntryTable, and ipRouteAttributeTable
10534 10536 * in one IRE walk.
10535 10537 */
10536 10538 static mblk_t *
10537 10539 ip_snmp_get_mib2_ip_route_media(queue_t *q, mblk_t *mpctl, int level,
10538 10540 ip_stack_t *ipst)
10539 10541 {
10540 10542 struct opthdr *optp;
10541 10543 mblk_t *mp2ctl; /* Returned */
10542 10544 mblk_t *mp3ctl; /* nettomedia */
10543 10545 mblk_t *mp4ctl; /* routeattrs */
10544 10546 iproutedata_t ird;
10545 10547 zoneid_t zoneid;
10546 10548
10547 10549 /*
10548 10550 * make copies of the original message
10549 10551 * - mp2ctl is returned unchanged to the caller for his use
10550 10552 * - mpctl is sent upstream as ipRouteEntryTable
10551 10553 * - mp3ctl is sent upstream as ipNetToMediaEntryTable
10552 10554 * - mp4ctl is sent upstream as ipRouteAttributeTable
10553 10555 */
10554 10556 mp2ctl = copymsg(mpctl);
10555 10557 mp3ctl = copymsg(mpctl);
10556 10558 mp4ctl = copymsg(mpctl);
10557 10559 if (mp3ctl == NULL || mp4ctl == NULL) {
10558 10560 freemsg(mp4ctl);
10559 10561 freemsg(mp3ctl);
10560 10562 freemsg(mp2ctl);
10561 10563 freemsg(mpctl);
10562 10564 return (NULL);
10563 10565 }
10564 10566
10565 10567 bzero(&ird, sizeof (ird));
10566 10568
10567 10569 ird.ird_route.lp_head = mpctl->b_cont;
10568 10570 ird.ird_netmedia.lp_head = mp3ctl->b_cont;
10569 10571 ird.ird_attrs.lp_head = mp4ctl->b_cont;
10570 10572 /*
10571 10573 * If the level has been set the special EXPER_IP_AND_ALL_IRES value,
10572 10574 * then also include ire_testhidden IREs and IRE_IF_CLONE. This is
10573 10575 * intended a temporary solution until a proper MIB API is provided
10574 10576 * that provides complete filtering/caller-opt-in.
10575 10577 */
10576 10578 if (level == EXPER_IP_AND_ALL_IRES)
10577 10579 ird.ird_flags |= IRD_REPORT_ALL;
10578 10580
10579 10581 zoneid = Q_TO_CONN(q)->conn_zoneid;
10580 10582 ire_walk_v4(ip_snmp_get2_v4, &ird, zoneid, ipst);
10581 10583
10582 10584 /* ipRouteEntryTable in mpctl */
10583 10585 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10584 10586 optp->level = MIB2_IP;
10585 10587 optp->name = MIB2_IP_ROUTE;
10586 10588 optp->len = msgdsize(ird.ird_route.lp_head);
10587 10589 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10588 10590 (int)optp->level, (int)optp->name, (int)optp->len));
10589 10591 qreply(q, mpctl);
10590 10592
10591 10593 /* ipNetToMediaEntryTable in mp3ctl */
10592 10594 ncec_walk(NULL, ip_snmp_get2_v4_media, &ird, ipst);
10593 10595
10594 10596 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10595 10597 optp->level = MIB2_IP;
10596 10598 optp->name = MIB2_IP_MEDIA;
10597 10599 optp->len = msgdsize(ird.ird_netmedia.lp_head);
10598 10600 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10599 10601 (int)optp->level, (int)optp->name, (int)optp->len));
10600 10602 qreply(q, mp3ctl);
10601 10603
10602 10604 /* ipRouteAttributeTable in mp4ctl */
10603 10605 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10604 10606 optp->level = MIB2_IP;
10605 10607 optp->name = EXPER_IP_RTATTR;
10606 10608 optp->len = msgdsize(ird.ird_attrs.lp_head);
10607 10609 ip3dbg(("ip_snmp_get_mib2_ip_route_media: level %d, name %d, len %d\n",
10608 10610 (int)optp->level, (int)optp->name, (int)optp->len));
10609 10611 if (optp->len == 0)
10610 10612 freemsg(mp4ctl);
10611 10613 else
10612 10614 qreply(q, mp4ctl);
10613 10615
10614 10616 return (mp2ctl);
10615 10617 }
10616 10618
10617 10619 /*
10618 10620 * Return ipv6RouteEntryTable and ipv6RouteAttributeTable in one IRE walk, and
10619 10621 * ipv6NetToMediaEntryTable in an NDP walk.
10620 10622 */
10621 10623 static mblk_t *
10622 10624 ip_snmp_get_mib2_ip6_route_media(queue_t *q, mblk_t *mpctl, int level,
10623 10625 ip_stack_t *ipst)
10624 10626 {
10625 10627 struct opthdr *optp;
10626 10628 mblk_t *mp2ctl; /* Returned */
10627 10629 mblk_t *mp3ctl; /* nettomedia */
10628 10630 mblk_t *mp4ctl; /* routeattrs */
10629 10631 iproutedata_t ird;
10630 10632 zoneid_t zoneid;
10631 10633
10632 10634 /*
10633 10635 * make copies of the original message
10634 10636 * - mp2ctl is returned unchanged to the caller for his use
10635 10637 * - mpctl is sent upstream as ipv6RouteEntryTable
10636 10638 * - mp3ctl is sent upstream as ipv6NetToMediaEntryTable
10637 10639 * - mp4ctl is sent upstream as ipv6RouteAttributeTable
10638 10640 */
10639 10641 mp2ctl = copymsg(mpctl);
10640 10642 mp3ctl = copymsg(mpctl);
10641 10643 mp4ctl = copymsg(mpctl);
10642 10644 if (mp3ctl == NULL || mp4ctl == NULL) {
10643 10645 freemsg(mp4ctl);
10644 10646 freemsg(mp3ctl);
10645 10647 freemsg(mp2ctl);
10646 10648 freemsg(mpctl);
10647 10649 return (NULL);
10648 10650 }
10649 10651
10650 10652 bzero(&ird, sizeof (ird));
10651 10653
10652 10654 ird.ird_route.lp_head = mpctl->b_cont;
10653 10655 ird.ird_netmedia.lp_head = mp3ctl->b_cont;
10654 10656 ird.ird_attrs.lp_head = mp4ctl->b_cont;
10655 10657 /*
10656 10658 * If the level has been set the special EXPER_IP_AND_ALL_IRES value,
10657 10659 * then also include ire_testhidden IREs and IRE_IF_CLONE. This is
10658 10660 * intended a temporary solution until a proper MIB API is provided
10659 10661 * that provides complete filtering/caller-opt-in.
10660 10662 */
10661 10663 if (level == EXPER_IP_AND_ALL_IRES)
10662 10664 ird.ird_flags |= IRD_REPORT_ALL;
10663 10665
10664 10666 zoneid = Q_TO_CONN(q)->conn_zoneid;
10665 10667 ire_walk_v6(ip_snmp_get2_v6_route, &ird, zoneid, ipst);
10666 10668
10667 10669 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10668 10670 optp->level = MIB2_IP6;
10669 10671 optp->name = MIB2_IP6_ROUTE;
10670 10672 optp->len = msgdsize(ird.ird_route.lp_head);
10671 10673 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10672 10674 (int)optp->level, (int)optp->name, (int)optp->len));
10673 10675 qreply(q, mpctl);
10674 10676
10675 10677 /* ipv6NetToMediaEntryTable in mp3ctl */
10676 10678 ncec_walk(NULL, ip_snmp_get2_v6_media, &ird, ipst);
10677 10679
10678 10680 optp = (struct opthdr *)&mp3ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10679 10681 optp->level = MIB2_IP6;
10680 10682 optp->name = MIB2_IP6_MEDIA;
10681 10683 optp->len = msgdsize(ird.ird_netmedia.lp_head);
10682 10684 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10683 10685 (int)optp->level, (int)optp->name, (int)optp->len));
10684 10686 qreply(q, mp3ctl);
10685 10687
10686 10688 /* ipv6RouteAttributeTable in mp4ctl */
10687 10689 optp = (struct opthdr *)&mp4ctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10688 10690 optp->level = MIB2_IP6;
10689 10691 optp->name = EXPER_IP_RTATTR;
10690 10692 optp->len = msgdsize(ird.ird_attrs.lp_head);
10691 10693 ip3dbg(("ip_snmp_get_mib2_ip6_route_media: level %d, name %d, len %d\n",
10692 10694 (int)optp->level, (int)optp->name, (int)optp->len));
10693 10695 if (optp->len == 0)
10694 10696 freemsg(mp4ctl);
10695 10697 else
10696 10698 qreply(q, mp4ctl);
10697 10699
10698 10700 return (mp2ctl);
10699 10701 }
10700 10702
10701 10703 /*
10702 10704 * IPv6 mib: One per ill
10703 10705 */
10704 10706 static mblk_t *
10705 10707 ip_snmp_get_mib2_ip6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst,
10706 10708 boolean_t legacy_req)
10707 10709 {
10708 10710 struct opthdr *optp;
10709 10711 mblk_t *mp2ctl;
10710 10712 ill_t *ill;
10711 10713 ill_walk_context_t ctx;
10712 10714 mblk_t *mp_tail = NULL;
10713 10715 mib2_ipv6AddrEntry_t mae6;
10714 10716 mib2_ipIfStatsEntry_t *ise;
10715 10717 size_t ise_size, iae_size;
10716 10718
10717 10719 /*
10718 10720 * Make a copy of the original message
10719 10721 */
10720 10722 mp2ctl = copymsg(mpctl);
10721 10723
10722 10724 /* fixed length IPv6 structure ... */
10723 10725
10724 10726 if (legacy_req) {
10725 10727 ise_size = LEGACY_MIB_SIZE(&ipst->ips_ip6_mib,
10726 10728 mib2_ipIfStatsEntry_t);
10727 10729 iae_size = LEGACY_MIB_SIZE(&mae6, mib2_ipv6AddrEntry_t);
10728 10730 } else {
10729 10731 ise_size = sizeof (mib2_ipIfStatsEntry_t);
10730 10732 iae_size = sizeof (mib2_ipv6AddrEntry_t);
10731 10733 }
10732 10734
10733 10735 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10734 10736 optp->level = MIB2_IP6;
10735 10737 optp->name = 0;
10736 10738 /* Include "unknown interface" ip6_mib */
10737 10739 ipst->ips_ip6_mib.ipIfStatsIPVersion = MIB2_INETADDRESSTYPE_ipv6;
10738 10740 ipst->ips_ip6_mib.ipIfStatsIfIndex =
10739 10741 MIB2_UNKNOWN_INTERFACE; /* Flag to netstat */
10740 10742 SET_MIB(ipst->ips_ip6_mib.ipIfStatsForwarding,
10741 10743 ipst->ips_ipv6_forwarding ? 1 : 2);
10742 10744 SET_MIB(ipst->ips_ip6_mib.ipIfStatsDefaultHopLimit,
10743 10745 ipst->ips_ipv6_def_hops);
10744 10746 SET_MIB(ipst->ips_ip6_mib.ipIfStatsEntrySize,
10745 10747 sizeof (mib2_ipIfStatsEntry_t));
10746 10748 SET_MIB(ipst->ips_ip6_mib.ipIfStatsAddrEntrySize,
10747 10749 sizeof (mib2_ipv6AddrEntry_t));
10748 10750 SET_MIB(ipst->ips_ip6_mib.ipIfStatsRouteEntrySize,
10749 10751 sizeof (mib2_ipv6RouteEntry_t));
10750 10752 SET_MIB(ipst->ips_ip6_mib.ipIfStatsNetToMediaEntrySize,
10751 10753 sizeof (mib2_ipv6NetToMediaEntry_t));
10752 10754 SET_MIB(ipst->ips_ip6_mib.ipIfStatsMemberEntrySize,
10753 10755 sizeof (ipv6_member_t));
10754 10756 SET_MIB(ipst->ips_ip6_mib.ipIfStatsGroupSourceEntrySize,
10755 10757 sizeof (ipv6_grpsrc_t));
10756 10758
10757 10759 /*
10758 10760 * Synchronize 64- and 32-bit counters
10759 10761 */
10760 10762 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInReceives,
10761 10763 ipIfStatsHCInReceives);
10762 10764 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInDelivers,
10763 10765 ipIfStatsHCInDelivers);
10764 10766 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutRequests,
10765 10767 ipIfStatsHCOutRequests);
10766 10768 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutForwDatagrams,
10767 10769 ipIfStatsHCOutForwDatagrams);
10768 10770 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsOutMcastPkts,
10769 10771 ipIfStatsHCOutMcastPkts);
10770 10772 SYNC32_MIB(&ipst->ips_ip6_mib, ipIfStatsInMcastPkts,
10771 10773 ipIfStatsHCInMcastPkts);
10772 10774
10773 10775 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10774 10776 (char *)&ipst->ips_ip6_mib, (int)ise_size)) {
10775 10777 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate %u bytes\n",
10776 10778 (uint_t)ise_size));
10777 10779 } else if (legacy_req) {
10778 10780 /* Adjust the EntrySize fields for legacy requests. */
10779 10781 ise =
10780 10782 (mib2_ipIfStatsEntry_t *)(mp_tail->b_wptr - (int)ise_size);
10781 10783 SET_MIB(ise->ipIfStatsEntrySize, ise_size);
10782 10784 SET_MIB(ise->ipIfStatsAddrEntrySize, iae_size);
10783 10785 }
10784 10786
10785 10787 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10786 10788 ill = ILL_START_WALK_V6(&ctx, ipst);
10787 10789 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10788 10790 ill->ill_ip_mib->ipIfStatsIfIndex =
10789 10791 ill->ill_phyint->phyint_ifindex;
10790 10792 SET_MIB(ill->ill_ip_mib->ipIfStatsForwarding,
10791 10793 ipst->ips_ipv6_forwarding ? 1 : 2);
10792 10794 SET_MIB(ill->ill_ip_mib->ipIfStatsDefaultHopLimit,
10793 10795 ill->ill_max_hops);
10794 10796
10795 10797 /*
10796 10798 * Synchronize 64- and 32-bit counters
10797 10799 */
10798 10800 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInReceives,
10799 10801 ipIfStatsHCInReceives);
10800 10802 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInDelivers,
10801 10803 ipIfStatsHCInDelivers);
10802 10804 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutRequests,
10803 10805 ipIfStatsHCOutRequests);
10804 10806 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutForwDatagrams,
10805 10807 ipIfStatsHCOutForwDatagrams);
10806 10808 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsOutMcastPkts,
10807 10809 ipIfStatsHCOutMcastPkts);
10808 10810 SYNC32_MIB(ill->ill_ip_mib, ipIfStatsInMcastPkts,
10809 10811 ipIfStatsHCInMcastPkts);
10810 10812
10811 10813 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10812 10814 (char *)ill->ill_ip_mib, (int)ise_size)) {
10813 10815 ip1dbg(("ip_snmp_get_mib2_ip6: failed to allocate "
10814 10816 "%u bytes\n", (uint_t)ise_size));
10815 10817 } else if (legacy_req) {
10816 10818 /* Adjust the EntrySize fields for legacy requests. */
10817 10819 ise = (mib2_ipIfStatsEntry_t *)(mp_tail->b_wptr -
10818 10820 (int)ise_size);
10819 10821 SET_MIB(ise->ipIfStatsEntrySize, ise_size);
10820 10822 SET_MIB(ise->ipIfStatsAddrEntrySize, iae_size);
10821 10823 }
10822 10824 }
10823 10825 rw_exit(&ipst->ips_ill_g_lock);
10824 10826
10825 10827 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10826 10828 ip3dbg(("ip_snmp_get_mib2_ip6: level %d, name %d, len %d\n",
10827 10829 (int)optp->level, (int)optp->name, (int)optp->len));
10828 10830 qreply(q, mpctl);
10829 10831 return (mp2ctl);
10830 10832 }
10831 10833
10832 10834 /*
10833 10835 * ICMPv6 mib: One per ill
10834 10836 */
10835 10837 static mblk_t *
10836 10838 ip_snmp_get_mib2_icmp6(queue_t *q, mblk_t *mpctl, ip_stack_t *ipst)
10837 10839 {
10838 10840 struct opthdr *optp;
10839 10841 mblk_t *mp2ctl;
10840 10842 ill_t *ill;
10841 10843 ill_walk_context_t ctx;
10842 10844 mblk_t *mp_tail = NULL;
10843 10845 /*
10844 10846 * Make a copy of the original message
10845 10847 */
10846 10848 mp2ctl = copymsg(mpctl);
10847 10849
10848 10850 /* fixed length ICMPv6 structure ... */
10849 10851
10850 10852 optp = (struct opthdr *)&mpctl->b_rptr[sizeof (struct T_optmgmt_ack)];
10851 10853 optp->level = MIB2_ICMP6;
10852 10854 optp->name = 0;
10853 10855 /* Include "unknown interface" icmp6_mib */
10854 10856 ipst->ips_icmp6_mib.ipv6IfIcmpIfIndex =
10855 10857 MIB2_UNKNOWN_INTERFACE; /* netstat flag */
10856 10858 ipst->ips_icmp6_mib.ipv6IfIcmpEntrySize =
10857 10859 sizeof (mib2_ipv6IfIcmpEntry_t);
10858 10860 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10859 10861 (char *)&ipst->ips_icmp6_mib,
10860 10862 (int)sizeof (ipst->ips_icmp6_mib))) {
10861 10863 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate %u bytes\n",
10862 10864 (uint_t)sizeof (ipst->ips_icmp6_mib)));
10863 10865 }
10864 10866
10865 10867 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
10866 10868 ill = ILL_START_WALK_V6(&ctx, ipst);
10867 10869 for (; ill != NULL; ill = ill_next(&ctx, ill)) {
10868 10870 ill->ill_icmp6_mib->ipv6IfIcmpIfIndex =
10869 10871 ill->ill_phyint->phyint_ifindex;
10870 10872 if (!snmp_append_data2(mpctl->b_cont, &mp_tail,
10871 10873 (char *)ill->ill_icmp6_mib,
10872 10874 (int)sizeof (*ill->ill_icmp6_mib))) {
10873 10875 ip1dbg(("ip_snmp_get_mib2_icmp6: failed to allocate "
10874 10876 "%u bytes\n",
10875 10877 (uint_t)sizeof (*ill->ill_icmp6_mib)));
10876 10878 }
10877 10879 }
10878 10880 rw_exit(&ipst->ips_ill_g_lock);
10879 10881
10880 10882 optp->len = (t_uscalar_t)msgdsize(mpctl->b_cont);
10881 10883 ip3dbg(("ip_snmp_get_mib2_icmp6: level %d, name %d, len %d\n",
10882 10884 (int)optp->level, (int)optp->name, (int)optp->len));
10883 10885 qreply(q, mpctl);
10884 10886 return (mp2ctl);
10885 10887 }
10886 10888
10887 10889 /*
10888 10890 * ire_walk routine to create both ipRouteEntryTable and
10889 10891 * ipRouteAttributeTable in one IRE walk
10890 10892 */
10891 10893 static void
10892 10894 ip_snmp_get2_v4(ire_t *ire, iproutedata_t *ird)
10893 10895 {
10894 10896 ill_t *ill;
10895 10897 mib2_ipRouteEntry_t *re;
10896 10898 mib2_ipAttributeEntry_t iaes;
10897 10899 tsol_ire_gw_secattr_t *attrp;
10898 10900 tsol_gc_t *gc = NULL;
10899 10901 tsol_gcgrp_t *gcgrp = NULL;
10900 10902 ip_stack_t *ipst = ire->ire_ipst;
10901 10903
10902 10904 ASSERT(ire->ire_ipversion == IPV4_VERSION);
10903 10905
10904 10906 if (!(ird->ird_flags & IRD_REPORT_ALL)) {
10905 10907 if (ire->ire_testhidden)
10906 10908 return;
10907 10909 if (ire->ire_type & IRE_IF_CLONE)
10908 10910 return;
10909 10911 }
10910 10912
10911 10913 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL)
10912 10914 return;
10913 10915
10914 10916 if ((attrp = ire->ire_gw_secattr) != NULL) {
10915 10917 mutex_enter(&attrp->igsa_lock);
10916 10918 if ((gc = attrp->igsa_gc) != NULL) {
10917 10919 gcgrp = gc->gc_grp;
10918 10920 ASSERT(gcgrp != NULL);
10919 10921 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER);
10920 10922 }
10921 10923 mutex_exit(&attrp->igsa_lock);
10922 10924 }
10923 10925 /*
10924 10926 * Return all IRE types for route table... let caller pick and choose
10925 10927 */
10926 10928 re->ipRouteDest = ire->ire_addr;
10927 10929 ill = ire->ire_ill;
10928 10930 re->ipRouteIfIndex.o_length = 0;
10929 10931 if (ill != NULL) {
10930 10932 ill_get_name(ill, re->ipRouteIfIndex.o_bytes, OCTET_LENGTH);
10931 10933 re->ipRouteIfIndex.o_length =
10932 10934 mi_strlen(re->ipRouteIfIndex.o_bytes);
10933 10935 }
10934 10936 re->ipRouteMetric1 = -1;
10935 10937 re->ipRouteMetric2 = -1;
10936 10938 re->ipRouteMetric3 = -1;
10937 10939 re->ipRouteMetric4 = -1;
10938 10940
10939 10941 re->ipRouteNextHop = ire->ire_gateway_addr;
10940 10942 /* indirect(4), direct(3), or invalid(2) */
10941 10943 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))
10942 10944 re->ipRouteType = 2;
10943 10945 else if (ire->ire_type & IRE_ONLINK)
10944 10946 re->ipRouteType = 3;
10945 10947 else
10946 10948 re->ipRouteType = 4;
10947 10949
10948 10950 re->ipRouteProto = -1;
10949 10951 re->ipRouteAge = gethrestime_sec() - ire->ire_create_time;
10950 10952 re->ipRouteMask = ire->ire_mask;
10951 10953 re->ipRouteMetric5 = -1;
10952 10954 re->ipRouteInfo.re_max_frag = ire->ire_metrics.iulp_mtu;
10953 10955 if (ire->ire_ill != NULL && re->ipRouteInfo.re_max_frag == 0)
10954 10956 re->ipRouteInfo.re_max_frag = ire->ire_ill->ill_mtu;
10955 10957
10956 10958 re->ipRouteInfo.re_frag_flag = 0;
10957 10959 re->ipRouteInfo.re_rtt = 0;
10958 10960 re->ipRouteInfo.re_src_addr = 0;
10959 10961 re->ipRouteInfo.re_ref = ire->ire_refcnt;
10960 10962 re->ipRouteInfo.re_obpkt = ire->ire_ob_pkt_count;
10961 10963 re->ipRouteInfo.re_ibpkt = ire->ire_ib_pkt_count;
10962 10964 re->ipRouteInfo.re_flags = ire->ire_flags;
10963 10965
10964 10966 /* Add the IRE_IF_CLONE's counters to their parent IRE_INTERFACE */
10965 10967 if (ire->ire_type & IRE_INTERFACE) {
10966 10968 ire_t *child;
10967 10969
10968 10970 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
10969 10971 child = ire->ire_dep_children;
10970 10972 while (child != NULL) {
10971 10973 re->ipRouteInfo.re_obpkt += child->ire_ob_pkt_count;
10972 10974 re->ipRouteInfo.re_ibpkt += child->ire_ib_pkt_count;
10973 10975 child = child->ire_dep_sib_next;
10974 10976 }
10975 10977 rw_exit(&ipst->ips_ire_dep_lock);
10976 10978 }
10977 10979
10978 10980 if (ire->ire_flags & RTF_DYNAMIC) {
10979 10981 re->ipRouteInfo.re_ire_type = IRE_HOST_REDIRECT;
10980 10982 } else {
10981 10983 re->ipRouteInfo.re_ire_type = ire->ire_type;
10982 10984 }
10983 10985
10984 10986 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail,
10985 10987 (char *)re, (int)sizeof (*re))) {
10986 10988 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u bytes\n",
10987 10989 (uint_t)sizeof (*re)));
10988 10990 }
10989 10991
10990 10992 if (gc != NULL) {
10991 10993 iaes.iae_routeidx = ird->ird_idx;
10992 10994 iaes.iae_doi = gc->gc_db->gcdb_doi;
10993 10995 iaes.iae_slrange = gc->gc_db->gcdb_slrange;
10994 10996
10995 10997 if (!snmp_append_data2(ird->ird_attrs.lp_head,
10996 10998 &ird->ird_attrs.lp_tail, (char *)&iaes, sizeof (iaes))) {
10997 10999 ip1dbg(("ip_snmp_get2_v4: failed to allocate %u "
10998 11000 "bytes\n", (uint_t)sizeof (iaes)));
10999 11001 }
11000 11002 }
11001 11003
11002 11004 /* bump route index for next pass */
11003 11005 ird->ird_idx++;
11004 11006
11005 11007 kmem_free(re, sizeof (*re));
11006 11008 if (gcgrp != NULL)
11007 11009 rw_exit(&gcgrp->gcgrp_rwlock);
11008 11010 }
11009 11011
11010 11012 /*
11011 11013 * ire_walk routine to create ipv6RouteEntryTable and ipRouteEntryTable.
11012 11014 */
11013 11015 static void
11014 11016 ip_snmp_get2_v6_route(ire_t *ire, iproutedata_t *ird)
11015 11017 {
11016 11018 ill_t *ill;
11017 11019 mib2_ipv6RouteEntry_t *re;
11018 11020 mib2_ipAttributeEntry_t iaes;
11019 11021 tsol_ire_gw_secattr_t *attrp;
11020 11022 tsol_gc_t *gc = NULL;
11021 11023 tsol_gcgrp_t *gcgrp = NULL;
11022 11024 ip_stack_t *ipst = ire->ire_ipst;
11023 11025
11024 11026 ASSERT(ire->ire_ipversion == IPV6_VERSION);
11025 11027
11026 11028 if (!(ird->ird_flags & IRD_REPORT_ALL)) {
11027 11029 if (ire->ire_testhidden)
11028 11030 return;
11029 11031 if (ire->ire_type & IRE_IF_CLONE)
11030 11032 return;
11031 11033 }
11032 11034
11033 11035 if ((re = kmem_zalloc(sizeof (*re), KM_NOSLEEP)) == NULL)
11034 11036 return;
11035 11037
11036 11038 if ((attrp = ire->ire_gw_secattr) != NULL) {
11037 11039 mutex_enter(&attrp->igsa_lock);
11038 11040 if ((gc = attrp->igsa_gc) != NULL) {
11039 11041 gcgrp = gc->gc_grp;
11040 11042 ASSERT(gcgrp != NULL);
11041 11043 rw_enter(&gcgrp->gcgrp_rwlock, RW_READER);
11042 11044 }
11043 11045 mutex_exit(&attrp->igsa_lock);
11044 11046 }
11045 11047 /*
11046 11048 * Return all IRE types for route table... let caller pick and choose
11047 11049 */
11048 11050 re->ipv6RouteDest = ire->ire_addr_v6;
11049 11051 re->ipv6RoutePfxLength = ip_mask_to_plen_v6(&ire->ire_mask_v6);
11050 11052 re->ipv6RouteIndex = 0; /* Unique when multiple with same dest/plen */
11051 11053 re->ipv6RouteIfIndex.o_length = 0;
11052 11054 ill = ire->ire_ill;
11053 11055 if (ill != NULL) {
11054 11056 ill_get_name(ill, re->ipv6RouteIfIndex.o_bytes, OCTET_LENGTH);
11055 11057 re->ipv6RouteIfIndex.o_length =
11056 11058 mi_strlen(re->ipv6RouteIfIndex.o_bytes);
11057 11059 }
11058 11060
11059 11061 ASSERT(!(ire->ire_type & IRE_BROADCAST));
11060 11062
11061 11063 mutex_enter(&ire->ire_lock);
11062 11064 re->ipv6RouteNextHop = ire->ire_gateway_addr_v6;
11063 11065 mutex_exit(&ire->ire_lock);
11064 11066
11065 11067 /* remote(4), local(3), or discard(2) */
11066 11068 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))
11067 11069 re->ipv6RouteType = 2;
11068 11070 else if (ire->ire_type & IRE_ONLINK)
11069 11071 re->ipv6RouteType = 3;
11070 11072 else
11071 11073 re->ipv6RouteType = 4;
11072 11074
11073 11075 re->ipv6RouteProtocol = -1;
11074 11076 re->ipv6RoutePolicy = 0;
11075 11077 re->ipv6RouteAge = gethrestime_sec() - ire->ire_create_time;
11076 11078 re->ipv6RouteNextHopRDI = 0;
11077 11079 re->ipv6RouteWeight = 0;
11078 11080 re->ipv6RouteMetric = 0;
11079 11081 re->ipv6RouteInfo.re_max_frag = ire->ire_metrics.iulp_mtu;
11080 11082 if (ire->ire_ill != NULL && re->ipv6RouteInfo.re_max_frag == 0)
11081 11083 re->ipv6RouteInfo.re_max_frag = ire->ire_ill->ill_mtu;
11082 11084
11083 11085 re->ipv6RouteInfo.re_frag_flag = 0;
11084 11086 re->ipv6RouteInfo.re_rtt = 0;
11085 11087 re->ipv6RouteInfo.re_src_addr = ipv6_all_zeros;
11086 11088 re->ipv6RouteInfo.re_obpkt = ire->ire_ob_pkt_count;
11087 11089 re->ipv6RouteInfo.re_ibpkt = ire->ire_ib_pkt_count;
11088 11090 re->ipv6RouteInfo.re_ref = ire->ire_refcnt;
11089 11091 re->ipv6RouteInfo.re_flags = ire->ire_flags;
11090 11092
11091 11093 /* Add the IRE_IF_CLONE's counters to their parent IRE_INTERFACE */
11092 11094 if (ire->ire_type & IRE_INTERFACE) {
11093 11095 ire_t *child;
11094 11096
11095 11097 rw_enter(&ipst->ips_ire_dep_lock, RW_READER);
11096 11098 child = ire->ire_dep_children;
11097 11099 while (child != NULL) {
11098 11100 re->ipv6RouteInfo.re_obpkt += child->ire_ob_pkt_count;
11099 11101 re->ipv6RouteInfo.re_ibpkt += child->ire_ib_pkt_count;
11100 11102 child = child->ire_dep_sib_next;
11101 11103 }
11102 11104 rw_exit(&ipst->ips_ire_dep_lock);
11103 11105 }
11104 11106 if (ire->ire_flags & RTF_DYNAMIC) {
11105 11107 re->ipv6RouteInfo.re_ire_type = IRE_HOST_REDIRECT;
11106 11108 } else {
11107 11109 re->ipv6RouteInfo.re_ire_type = ire->ire_type;
11108 11110 }
11109 11111
11110 11112 if (!snmp_append_data2(ird->ird_route.lp_head, &ird->ird_route.lp_tail,
11111 11113 (char *)re, (int)sizeof (*re))) {
11112 11114 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u bytes\n",
11113 11115 (uint_t)sizeof (*re)));
11114 11116 }
11115 11117
11116 11118 if (gc != NULL) {
11117 11119 iaes.iae_routeidx = ird->ird_idx;
11118 11120 iaes.iae_doi = gc->gc_db->gcdb_doi;
11119 11121 iaes.iae_slrange = gc->gc_db->gcdb_slrange;
11120 11122
11121 11123 if (!snmp_append_data2(ird->ird_attrs.lp_head,
11122 11124 &ird->ird_attrs.lp_tail, (char *)&iaes, sizeof (iaes))) {
11123 11125 ip1dbg(("ip_snmp_get2_v6: failed to allocate %u "
11124 11126 "bytes\n", (uint_t)sizeof (iaes)));
11125 11127 }
11126 11128 }
11127 11129
11128 11130 /* bump route index for next pass */
11129 11131 ird->ird_idx++;
11130 11132
11131 11133 kmem_free(re, sizeof (*re));
11132 11134 if (gcgrp != NULL)
11133 11135 rw_exit(&gcgrp->gcgrp_rwlock);
11134 11136 }
11135 11137
11136 11138 /*
11137 11139 * ncec_walk routine to create ipv6NetToMediaEntryTable
11138 11140 */
11139 11141 static int
11140 11142 ip_snmp_get2_v6_media(ncec_t *ncec, iproutedata_t *ird)
11141 11143 {
11142 11144 ill_t *ill;
11143 11145 mib2_ipv6NetToMediaEntry_t ntme;
11144 11146
11145 11147 ill = ncec->ncec_ill;
11146 11148 /* skip arpce entries, and loopback ncec entries */
11147 11149 if (ill->ill_isv6 == B_FALSE || ill->ill_net_type == IRE_LOOPBACK)
11148 11150 return (0);
11149 11151 /*
11150 11152 * Neighbor cache entry attached to IRE with on-link
11151 11153 * destination.
11152 11154 * We report all IPMP groups on ncec_ill which is normally the upper.
11153 11155 */
11154 11156 ntme.ipv6NetToMediaIfIndex = ill->ill_phyint->phyint_ifindex;
11155 11157 ntme.ipv6NetToMediaNetAddress = ncec->ncec_addr;
11156 11158 ntme.ipv6NetToMediaPhysAddress.o_length = ill->ill_phys_addr_length;
11157 11159 if (ncec->ncec_lladdr != NULL) {
11158 11160 bcopy(ncec->ncec_lladdr, ntme.ipv6NetToMediaPhysAddress.o_bytes,
11159 11161 ntme.ipv6NetToMediaPhysAddress.o_length);
11160 11162 }
11161 11163 /*
11162 11164 * Note: Returns ND_* states. Should be:
11163 11165 * reachable(1), stale(2), delay(3), probe(4),
11164 11166 * invalid(5), unknown(6)
11165 11167 */
11166 11168 ntme.ipv6NetToMediaState = ncec->ncec_state;
11167 11169 ntme.ipv6NetToMediaLastUpdated = 0;
11168 11170
11169 11171 /* other(1), dynamic(2), static(3), local(4) */
11170 11172 if (NCE_MYADDR(ncec)) {
11171 11173 ntme.ipv6NetToMediaType = 4;
11172 11174 } else if (ncec->ncec_flags & NCE_F_PUBLISH) {
11173 11175 ntme.ipv6NetToMediaType = 1; /* proxy */
11174 11176 } else if (ncec->ncec_flags & NCE_F_STATIC) {
11175 11177 ntme.ipv6NetToMediaType = 3;
11176 11178 } else if (ncec->ncec_flags & (NCE_F_MCAST|NCE_F_BCAST)) {
11177 11179 ntme.ipv6NetToMediaType = 1;
11178 11180 } else {
11179 11181 ntme.ipv6NetToMediaType = 2;
11180 11182 }
11181 11183
11182 11184 if (!snmp_append_data2(ird->ird_netmedia.lp_head,
11183 11185 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) {
11184 11186 ip1dbg(("ip_snmp_get2_v6_media: failed to allocate %u bytes\n",
11185 11187 (uint_t)sizeof (ntme)));
11186 11188 }
11187 11189 return (0);
11188 11190 }
11189 11191
11190 11192 int
11191 11193 nce2ace(ncec_t *ncec)
11192 11194 {
11193 11195 int flags = 0;
11194 11196
11195 11197 if (NCE_ISREACHABLE(ncec))
11196 11198 flags |= ACE_F_RESOLVED;
11197 11199 if (ncec->ncec_flags & NCE_F_AUTHORITY)
11198 11200 flags |= ACE_F_AUTHORITY;
11199 11201 if (ncec->ncec_flags & NCE_F_PUBLISH)
11200 11202 flags |= ACE_F_PUBLISH;
11201 11203 if ((ncec->ncec_flags & NCE_F_NONUD) != 0)
11202 11204 flags |= ACE_F_PERMANENT;
11203 11205 if (NCE_MYADDR(ncec))
11204 11206 flags |= (ACE_F_MYADDR | ACE_F_AUTHORITY);
11205 11207 if (ncec->ncec_flags & NCE_F_UNVERIFIED)
11206 11208 flags |= ACE_F_UNVERIFIED;
11207 11209 if (ncec->ncec_flags & NCE_F_AUTHORITY)
11208 11210 flags |= ACE_F_AUTHORITY;
11209 11211 if (ncec->ncec_flags & NCE_F_DELAYED)
11210 11212 flags |= ACE_F_DELAYED;
11211 11213 return (flags);
11212 11214 }
11213 11215
11214 11216 /*
11215 11217 * ncec_walk routine to create ipNetToMediaEntryTable
11216 11218 */
11217 11219 static int
11218 11220 ip_snmp_get2_v4_media(ncec_t *ncec, iproutedata_t *ird)
11219 11221 {
11220 11222 ill_t *ill;
11221 11223 mib2_ipNetToMediaEntry_t ntme;
11222 11224 const char *name = "unknown";
11223 11225 ipaddr_t ncec_addr;
11224 11226
11225 11227 ill = ncec->ncec_ill;
11226 11228 if (ill->ill_isv6 || (ncec->ncec_flags & NCE_F_BCAST) ||
11227 11229 ill->ill_net_type == IRE_LOOPBACK)
11228 11230 return (0);
11229 11231
11230 11232 /* We report all IPMP groups on ncec_ill which is normally the upper. */
11231 11233 name = ill->ill_name;
11232 11234 /* Based on RFC 4293: other(1), inval(2), dyn(3), stat(4) */
11233 11235 if (NCE_MYADDR(ncec)) {
11234 11236 ntme.ipNetToMediaType = 4;
11235 11237 } else if (ncec->ncec_flags & (NCE_F_MCAST|NCE_F_BCAST|NCE_F_PUBLISH)) {
11236 11238 ntme.ipNetToMediaType = 1;
11237 11239 } else {
11238 11240 ntme.ipNetToMediaType = 3;
11239 11241 }
11240 11242 ntme.ipNetToMediaIfIndex.o_length = MIN(OCTET_LENGTH, strlen(name));
11241 11243 bcopy(name, ntme.ipNetToMediaIfIndex.o_bytes,
11242 11244 ntme.ipNetToMediaIfIndex.o_length);
11243 11245
11244 11246 IN6_V4MAPPED_TO_IPADDR(&ncec->ncec_addr, ncec_addr);
11245 11247 bcopy(&ncec_addr, &ntme.ipNetToMediaNetAddress, sizeof (ncec_addr));
11246 11248
11247 11249 ntme.ipNetToMediaInfo.ntm_mask.o_length = sizeof (ipaddr_t);
11248 11250 ncec_addr = INADDR_BROADCAST;
11249 11251 bcopy(&ncec_addr, ntme.ipNetToMediaInfo.ntm_mask.o_bytes,
11250 11252 sizeof (ncec_addr));
11251 11253 /*
11252 11254 * map all the flags to the ACE counterpart.
11253 11255 */
11254 11256 ntme.ipNetToMediaInfo.ntm_flags = nce2ace(ncec);
11255 11257
11256 11258 ntme.ipNetToMediaPhysAddress.o_length =
11257 11259 MIN(OCTET_LENGTH, ill->ill_phys_addr_length);
11258 11260
11259 11261 if (!NCE_ISREACHABLE(ncec))
11260 11262 ntme.ipNetToMediaPhysAddress.o_length = 0;
11261 11263 else {
11262 11264 if (ncec->ncec_lladdr != NULL) {
11263 11265 bcopy(ncec->ncec_lladdr,
11264 11266 ntme.ipNetToMediaPhysAddress.o_bytes,
11265 11267 ntme.ipNetToMediaPhysAddress.o_length);
11266 11268 }
11267 11269 }
11268 11270
11269 11271 if (!snmp_append_data2(ird->ird_netmedia.lp_head,
11270 11272 &ird->ird_netmedia.lp_tail, (char *)&ntme, sizeof (ntme))) {
11271 11273 ip1dbg(("ip_snmp_get2_v4_media: failed to allocate %u bytes\n",
11272 11274 (uint_t)sizeof (ntme)));
11273 11275 }
11274 11276 return (0);
11275 11277 }
11276 11278
11277 11279 /*
11278 11280 * return (0) if invalid set request, 1 otherwise, including non-tcp requests
11279 11281 */
11280 11282 /* ARGSUSED */
11281 11283 int
11282 11284 ip_snmp_set(queue_t *q, int level, int name, uchar_t *ptr, int len)
11283 11285 {
11284 11286 switch (level) {
11285 11287 case MIB2_IP:
11286 11288 case MIB2_ICMP:
11287 11289 switch (name) {
11288 11290 default:
11289 11291 break;
11290 11292 }
11291 11293 return (1);
11292 11294 default:
11293 11295 return (1);
11294 11296 }
11295 11297 }
11296 11298
11297 11299 /*
11298 11300 * When there exists both a 64- and 32-bit counter of a particular type
11299 11301 * (i.e., InReceives), only the 64-bit counters are added.
11300 11302 */
11301 11303 void
11302 11304 ip_mib2_add_ip_stats(mib2_ipIfStatsEntry_t *o1, mib2_ipIfStatsEntry_t *o2)
11303 11305 {
11304 11306 UPDATE_MIB(o1, ipIfStatsInHdrErrors, o2->ipIfStatsInHdrErrors);
11305 11307 UPDATE_MIB(o1, ipIfStatsInTooBigErrors, o2->ipIfStatsInTooBigErrors);
11306 11308 UPDATE_MIB(o1, ipIfStatsInNoRoutes, o2->ipIfStatsInNoRoutes);
11307 11309 UPDATE_MIB(o1, ipIfStatsInAddrErrors, o2->ipIfStatsInAddrErrors);
11308 11310 UPDATE_MIB(o1, ipIfStatsInUnknownProtos, o2->ipIfStatsInUnknownProtos);
11309 11311 UPDATE_MIB(o1, ipIfStatsInTruncatedPkts, o2->ipIfStatsInTruncatedPkts);
11310 11312 UPDATE_MIB(o1, ipIfStatsInDiscards, o2->ipIfStatsInDiscards);
11311 11313 UPDATE_MIB(o1, ipIfStatsOutDiscards, o2->ipIfStatsOutDiscards);
11312 11314 UPDATE_MIB(o1, ipIfStatsOutFragOKs, o2->ipIfStatsOutFragOKs);
11313 11315 UPDATE_MIB(o1, ipIfStatsOutFragFails, o2->ipIfStatsOutFragFails);
11314 11316 UPDATE_MIB(o1, ipIfStatsOutFragCreates, o2->ipIfStatsOutFragCreates);
11315 11317 UPDATE_MIB(o1, ipIfStatsReasmReqds, o2->ipIfStatsReasmReqds);
11316 11318 UPDATE_MIB(o1, ipIfStatsReasmOKs, o2->ipIfStatsReasmOKs);
11317 11319 UPDATE_MIB(o1, ipIfStatsReasmFails, o2->ipIfStatsReasmFails);
11318 11320 UPDATE_MIB(o1, ipIfStatsOutNoRoutes, o2->ipIfStatsOutNoRoutes);
11319 11321 UPDATE_MIB(o1, ipIfStatsReasmDuplicates, o2->ipIfStatsReasmDuplicates);
11320 11322 UPDATE_MIB(o1, ipIfStatsReasmPartDups, o2->ipIfStatsReasmPartDups);
11321 11323 UPDATE_MIB(o1, ipIfStatsForwProhibits, o2->ipIfStatsForwProhibits);
11322 11324 UPDATE_MIB(o1, udpInCksumErrs, o2->udpInCksumErrs);
11323 11325 UPDATE_MIB(o1, udpInOverflows, o2->udpInOverflows);
11324 11326 UPDATE_MIB(o1, rawipInOverflows, o2->rawipInOverflows);
11325 11327 UPDATE_MIB(o1, ipIfStatsInWrongIPVersion,
11326 11328 o2->ipIfStatsInWrongIPVersion);
11327 11329 UPDATE_MIB(o1, ipIfStatsOutWrongIPVersion,
11328 11330 o2->ipIfStatsInWrongIPVersion);
11329 11331 UPDATE_MIB(o1, ipIfStatsOutSwitchIPVersion,
11330 11332 o2->ipIfStatsOutSwitchIPVersion);
11331 11333 UPDATE_MIB(o1, ipIfStatsHCInReceives, o2->ipIfStatsHCInReceives);
11332 11334 UPDATE_MIB(o1, ipIfStatsHCInOctets, o2->ipIfStatsHCInOctets);
11333 11335 UPDATE_MIB(o1, ipIfStatsHCInForwDatagrams,
11334 11336 o2->ipIfStatsHCInForwDatagrams);
11335 11337 UPDATE_MIB(o1, ipIfStatsHCInDelivers, o2->ipIfStatsHCInDelivers);
11336 11338 UPDATE_MIB(o1, ipIfStatsHCOutRequests, o2->ipIfStatsHCOutRequests);
11337 11339 UPDATE_MIB(o1, ipIfStatsHCOutForwDatagrams,
11338 11340 o2->ipIfStatsHCOutForwDatagrams);
11339 11341 UPDATE_MIB(o1, ipIfStatsOutFragReqds, o2->ipIfStatsOutFragReqds);
11340 11342 UPDATE_MIB(o1, ipIfStatsHCOutTransmits, o2->ipIfStatsHCOutTransmits);
11341 11343 UPDATE_MIB(o1, ipIfStatsHCOutOctets, o2->ipIfStatsHCOutOctets);
11342 11344 UPDATE_MIB(o1, ipIfStatsHCInMcastPkts, o2->ipIfStatsHCInMcastPkts);
11343 11345 UPDATE_MIB(o1, ipIfStatsHCInMcastOctets, o2->ipIfStatsHCInMcastOctets);
11344 11346 UPDATE_MIB(o1, ipIfStatsHCOutMcastPkts, o2->ipIfStatsHCOutMcastPkts);
11345 11347 UPDATE_MIB(o1, ipIfStatsHCOutMcastOctets,
11346 11348 o2->ipIfStatsHCOutMcastOctets);
11347 11349 UPDATE_MIB(o1, ipIfStatsHCInBcastPkts, o2->ipIfStatsHCInBcastPkts);
11348 11350 UPDATE_MIB(o1, ipIfStatsHCOutBcastPkts, o2->ipIfStatsHCOutBcastPkts);
11349 11351 UPDATE_MIB(o1, ipsecInSucceeded, o2->ipsecInSucceeded);
11350 11352 UPDATE_MIB(o1, ipsecInFailed, o2->ipsecInFailed);
11351 11353 UPDATE_MIB(o1, ipInCksumErrs, o2->ipInCksumErrs);
11352 11354 UPDATE_MIB(o1, tcpInErrs, o2->tcpInErrs);
11353 11355 UPDATE_MIB(o1, udpNoPorts, o2->udpNoPorts);
11354 11356 }
11355 11357
11356 11358 void
11357 11359 ip_mib2_add_icmp6_stats(mib2_ipv6IfIcmpEntry_t *o1, mib2_ipv6IfIcmpEntry_t *o2)
11358 11360 {
11359 11361 UPDATE_MIB(o1, ipv6IfIcmpInMsgs, o2->ipv6IfIcmpInMsgs);
11360 11362 UPDATE_MIB(o1, ipv6IfIcmpInErrors, o2->ipv6IfIcmpInErrors);
11361 11363 UPDATE_MIB(o1, ipv6IfIcmpInDestUnreachs, o2->ipv6IfIcmpInDestUnreachs);
11362 11364 UPDATE_MIB(o1, ipv6IfIcmpInAdminProhibs, o2->ipv6IfIcmpInAdminProhibs);
11363 11365 UPDATE_MIB(o1, ipv6IfIcmpInTimeExcds, o2->ipv6IfIcmpInTimeExcds);
11364 11366 UPDATE_MIB(o1, ipv6IfIcmpInParmProblems, o2->ipv6IfIcmpInParmProblems);
11365 11367 UPDATE_MIB(o1, ipv6IfIcmpInPktTooBigs, o2->ipv6IfIcmpInPktTooBigs);
11366 11368 UPDATE_MIB(o1, ipv6IfIcmpInEchos, o2->ipv6IfIcmpInEchos);
11367 11369 UPDATE_MIB(o1, ipv6IfIcmpInEchoReplies, o2->ipv6IfIcmpInEchoReplies);
11368 11370 UPDATE_MIB(o1, ipv6IfIcmpInRouterSolicits,
11369 11371 o2->ipv6IfIcmpInRouterSolicits);
11370 11372 UPDATE_MIB(o1, ipv6IfIcmpInRouterAdvertisements,
11371 11373 o2->ipv6IfIcmpInRouterAdvertisements);
11372 11374 UPDATE_MIB(o1, ipv6IfIcmpInNeighborSolicits,
11373 11375 o2->ipv6IfIcmpInNeighborSolicits);
11374 11376 UPDATE_MIB(o1, ipv6IfIcmpInNeighborAdvertisements,
11375 11377 o2->ipv6IfIcmpInNeighborAdvertisements);
11376 11378 UPDATE_MIB(o1, ipv6IfIcmpInRedirects, o2->ipv6IfIcmpInRedirects);
11377 11379 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembQueries,
11378 11380 o2->ipv6IfIcmpInGroupMembQueries);
11379 11381 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembResponses,
11380 11382 o2->ipv6IfIcmpInGroupMembResponses);
11381 11383 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembReductions,
11382 11384 o2->ipv6IfIcmpInGroupMembReductions);
11383 11385 UPDATE_MIB(o1, ipv6IfIcmpOutMsgs, o2->ipv6IfIcmpOutMsgs);
11384 11386 UPDATE_MIB(o1, ipv6IfIcmpOutErrors, o2->ipv6IfIcmpOutErrors);
11385 11387 UPDATE_MIB(o1, ipv6IfIcmpOutDestUnreachs,
11386 11388 o2->ipv6IfIcmpOutDestUnreachs);
11387 11389 UPDATE_MIB(o1, ipv6IfIcmpOutAdminProhibs,
11388 11390 o2->ipv6IfIcmpOutAdminProhibs);
11389 11391 UPDATE_MIB(o1, ipv6IfIcmpOutTimeExcds, o2->ipv6IfIcmpOutTimeExcds);
11390 11392 UPDATE_MIB(o1, ipv6IfIcmpOutParmProblems,
11391 11393 o2->ipv6IfIcmpOutParmProblems);
11392 11394 UPDATE_MIB(o1, ipv6IfIcmpOutPktTooBigs, o2->ipv6IfIcmpOutPktTooBigs);
11393 11395 UPDATE_MIB(o1, ipv6IfIcmpOutEchos, o2->ipv6IfIcmpOutEchos);
11394 11396 UPDATE_MIB(o1, ipv6IfIcmpOutEchoReplies, o2->ipv6IfIcmpOutEchoReplies);
11395 11397 UPDATE_MIB(o1, ipv6IfIcmpOutRouterSolicits,
11396 11398 o2->ipv6IfIcmpOutRouterSolicits);
11397 11399 UPDATE_MIB(o1, ipv6IfIcmpOutRouterAdvertisements,
11398 11400 o2->ipv6IfIcmpOutRouterAdvertisements);
11399 11401 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborSolicits,
11400 11402 o2->ipv6IfIcmpOutNeighborSolicits);
11401 11403 UPDATE_MIB(o1, ipv6IfIcmpOutNeighborAdvertisements,
11402 11404 o2->ipv6IfIcmpOutNeighborAdvertisements);
11403 11405 UPDATE_MIB(o1, ipv6IfIcmpOutRedirects, o2->ipv6IfIcmpOutRedirects);
11404 11406 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembQueries,
11405 11407 o2->ipv6IfIcmpOutGroupMembQueries);
11406 11408 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembResponses,
11407 11409 o2->ipv6IfIcmpOutGroupMembResponses);
11408 11410 UPDATE_MIB(o1, ipv6IfIcmpOutGroupMembReductions,
11409 11411 o2->ipv6IfIcmpOutGroupMembReductions);
11410 11412 UPDATE_MIB(o1, ipv6IfIcmpInOverflows, o2->ipv6IfIcmpInOverflows);
11411 11413 UPDATE_MIB(o1, ipv6IfIcmpBadHoplimit, o2->ipv6IfIcmpBadHoplimit);
11412 11414 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborAdvertisements,
11413 11415 o2->ipv6IfIcmpInBadNeighborAdvertisements);
11414 11416 UPDATE_MIB(o1, ipv6IfIcmpInBadNeighborSolicitations,
11415 11417 o2->ipv6IfIcmpInBadNeighborSolicitations);
11416 11418 UPDATE_MIB(o1, ipv6IfIcmpInBadRedirects, o2->ipv6IfIcmpInBadRedirects);
11417 11419 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembTotal,
11418 11420 o2->ipv6IfIcmpInGroupMembTotal);
11419 11421 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadQueries,
11420 11422 o2->ipv6IfIcmpInGroupMembBadQueries);
11421 11423 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembBadReports,
11422 11424 o2->ipv6IfIcmpInGroupMembBadReports);
11423 11425 UPDATE_MIB(o1, ipv6IfIcmpInGroupMembOurReports,
11424 11426 o2->ipv6IfIcmpInGroupMembOurReports);
11425 11427 }
11426 11428
11427 11429 /*
11428 11430 * Called before the options are updated to check if this packet will
11429 11431 * be source routed from here.
11430 11432 * This routine assumes that the options are well formed i.e. that they
11431 11433 * have already been checked.
11432 11434 */
11433 11435 boolean_t
11434 11436 ip_source_routed(ipha_t *ipha, ip_stack_t *ipst)
11435 11437 {
11436 11438 ipoptp_t opts;
11437 11439 uchar_t *opt;
11438 11440 uint8_t optval;
11439 11441 uint8_t optlen;
11440 11442 ipaddr_t dst;
11441 11443
11442 11444 if (IS_SIMPLE_IPH(ipha)) {
11443 11445 ip2dbg(("not source routed\n"));
11444 11446 return (B_FALSE);
11445 11447 }
11446 11448 dst = ipha->ipha_dst;
11447 11449 for (optval = ipoptp_first(&opts, ipha);
11448 11450 optval != IPOPT_EOL;
11449 11451 optval = ipoptp_next(&opts)) {
11450 11452 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11451 11453 opt = opts.ipoptp_cur;
11452 11454 optlen = opts.ipoptp_len;
11453 11455 ip2dbg(("ip_source_routed: opt %d, len %d\n",
11454 11456 optval, optlen));
11455 11457 switch (optval) {
11456 11458 uint32_t off;
11457 11459 case IPOPT_SSRR:
11458 11460 case IPOPT_LSRR:
11459 11461 /*
11460 11462 * If dst is one of our addresses and there are some
11461 11463 * entries left in the source route return (true).
11462 11464 */
11463 11465 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
11464 11466 ip2dbg(("ip_source_routed: not next"
11465 11467 " source route 0x%x\n",
11466 11468 ntohl(dst)));
11467 11469 return (B_FALSE);
11468 11470 }
11469 11471 off = opt[IPOPT_OFFSET];
11470 11472 off--;
11471 11473 if (optlen < IP_ADDR_LEN ||
11472 11474 off > optlen - IP_ADDR_LEN) {
11473 11475 /* End of source route */
11474 11476 ip1dbg(("ip_source_routed: end of SR\n"));
11475 11477 return (B_FALSE);
11476 11478 }
11477 11479 return (B_TRUE);
11478 11480 }
11479 11481 }
11480 11482 ip2dbg(("not source routed\n"));
11481 11483 return (B_FALSE);
11482 11484 }
11483 11485
11484 11486 /*
11485 11487 * ip_unbind is called by the transports to remove a conn from
11486 11488 * the fanout table.
11487 11489 */
11488 11490 void
11489 11491 ip_unbind(conn_t *connp)
11490 11492 {
11491 11493
11492 11494 ASSERT(!MUTEX_HELD(&connp->conn_lock));
11493 11495
11494 11496 if (is_system_labeled() && connp->conn_anon_port) {
11495 11497 (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
11496 11498 connp->conn_mlp_type, connp->conn_proto,
11497 11499 ntohs(connp->conn_lport), B_FALSE);
11498 11500 connp->conn_anon_port = 0;
11499 11501 }
11500 11502 connp->conn_mlp_type = mlptSingle;
11501 11503
11502 11504 ipcl_hash_remove(connp);
11503 11505 }
11504 11506
11505 11507 /*
11506 11508 * Used for deciding the MSS size for the upper layer. Thus
11507 11509 * we need to check the outbound policy values in the conn.
11508 11510 */
11509 11511 int
11510 11512 conn_ipsec_length(conn_t *connp)
11511 11513 {
11512 11514 ipsec_latch_t *ipl;
11513 11515
11514 11516 ipl = connp->conn_latch;
11515 11517 if (ipl == NULL)
11516 11518 return (0);
11517 11519
11518 11520 if (connp->conn_ixa->ixa_ipsec_policy == NULL)
11519 11521 return (0);
11520 11522
11521 11523 return (connp->conn_ixa->ixa_ipsec_policy->ipsp_act->ipa_ovhd);
11522 11524 }
11523 11525
11524 11526 /*
11525 11527 * Returns an estimate of the IPsec headers size. This is used if
11526 11528 * we don't want to call into IPsec to get the exact size.
11527 11529 */
11528 11530 int
11529 11531 ipsec_out_extra_length(ip_xmit_attr_t *ixa)
11530 11532 {
11531 11533 ipsec_action_t *a;
11532 11534
11533 11535 if (!(ixa->ixa_flags & IXAF_IPSEC_SECURE))
11534 11536 return (0);
11535 11537
11536 11538 a = ixa->ixa_ipsec_action;
11537 11539 if (a == NULL) {
11538 11540 ASSERT(ixa->ixa_ipsec_policy != NULL);
11539 11541 a = ixa->ixa_ipsec_policy->ipsp_act;
11540 11542 }
11541 11543 ASSERT(a != NULL);
11542 11544
11543 11545 return (a->ipa_ovhd);
11544 11546 }
11545 11547
11546 11548 /*
11547 11549 * If there are any source route options, return the true final
11548 11550 * destination. Otherwise, return the destination.
11549 11551 */
11550 11552 ipaddr_t
11551 11553 ip_get_dst(ipha_t *ipha)
11552 11554 {
11553 11555 ipoptp_t opts;
11554 11556 uchar_t *opt;
11555 11557 uint8_t optval;
11556 11558 uint8_t optlen;
11557 11559 ipaddr_t dst;
11558 11560 uint32_t off;
11559 11561
11560 11562 dst = ipha->ipha_dst;
11561 11563
11562 11564 if (IS_SIMPLE_IPH(ipha))
11563 11565 return (dst);
11564 11566
11565 11567 for (optval = ipoptp_first(&opts, ipha);
11566 11568 optval != IPOPT_EOL;
11567 11569 optval = ipoptp_next(&opts)) {
11568 11570 opt = opts.ipoptp_cur;
11569 11571 optlen = opts.ipoptp_len;
11570 11572 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11571 11573 switch (optval) {
11572 11574 case IPOPT_SSRR:
11573 11575 case IPOPT_LSRR:
11574 11576 off = opt[IPOPT_OFFSET];
11575 11577 /*
11576 11578 * If one of the conditions is true, it means
11577 11579 * end of options and dst already has the right
11578 11580 * value.
11579 11581 */
11580 11582 if (!(optlen < IP_ADDR_LEN || off > optlen - 3)) {
11581 11583 off = optlen - IP_ADDR_LEN;
11582 11584 bcopy(&opt[off], &dst, IP_ADDR_LEN);
11583 11585 }
11584 11586 return (dst);
11585 11587 default:
11586 11588 break;
11587 11589 }
11588 11590 }
11589 11591
11590 11592 return (dst);
11591 11593 }
11592 11594
11593 11595 /*
11594 11596 * Outbound IP fragmentation routine.
11595 11597 * Assumes the caller has checked whether or not fragmentation should
11596 11598 * be allowed. Here we copy the DF bit from the header to all the generated
11597 11599 * fragments.
11598 11600 */
11599 11601 int
11600 11602 ip_fragment_v4(mblk_t *mp_orig, nce_t *nce, iaflags_t ixaflags,
11601 11603 uint_t pkt_len, uint32_t max_frag, uint32_t xmit_hint, zoneid_t szone,
11602 11604 zoneid_t nolzid, pfirepostfrag_t postfragfn, uintptr_t *ixa_cookie)
11603 11605 {
11604 11606 int i1;
11605 11607 int hdr_len;
11606 11608 mblk_t *hdr_mp;
11607 11609 ipha_t *ipha;
11608 11610 int ip_data_end;
11609 11611 int len;
11610 11612 mblk_t *mp = mp_orig;
11611 11613 int offset;
11612 11614 ill_t *ill = nce->nce_ill;
11613 11615 ip_stack_t *ipst = ill->ill_ipst;
11614 11616 mblk_t *carve_mp;
11615 11617 uint32_t frag_flag;
11616 11618 uint_t priority = mp->b_band;
11617 11619 int error = 0;
11618 11620
11619 11621 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragReqds);
11620 11622
11621 11623 if (pkt_len != msgdsize(mp)) {
11622 11624 ip0dbg(("Packet length mismatch: %d, %ld\n",
11623 11625 pkt_len, msgdsize(mp)));
11624 11626 freemsg(mp);
11625 11627 return (EINVAL);
11626 11628 }
11627 11629
11628 11630 if (max_frag == 0) {
11629 11631 ip1dbg(("ip_fragment_v4: max_frag is zero. Dropping packet\n"));
11630 11632 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11631 11633 ip_drop_output("FragFails: zero max_frag", mp, ill);
11632 11634 freemsg(mp);
11633 11635 return (EINVAL);
11634 11636 }
11635 11637
11636 11638 ASSERT(MBLKL(mp) >= sizeof (ipha_t));
11637 11639 ipha = (ipha_t *)mp->b_rptr;
11638 11640 ASSERT(ntohs(ipha->ipha_length) == pkt_len);
11639 11641 frag_flag = ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_DF;
11640 11642
11641 11643 /*
11642 11644 * Establish the starting offset. May not be zero if we are fragging
11643 11645 * a fragment that is being forwarded.
11644 11646 */
11645 11647 offset = ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET;
11646 11648
11647 11649 /* TODO why is this test needed? */
11648 11650 if (((max_frag - ntohs(ipha->ipha_length)) & ~7) < 8) {
11649 11651 /* TODO: notify ulp somehow */
11650 11652 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11651 11653 ip_drop_output("FragFails: bad starting offset", mp, ill);
11652 11654 freemsg(mp);
11653 11655 return (EINVAL);
11654 11656 }
11655 11657
11656 11658 hdr_len = IPH_HDR_LENGTH(ipha);
11657 11659 ipha->ipha_hdr_checksum = 0;
11658 11660
11659 11661 /*
11660 11662 * Establish the number of bytes maximum per frag, after putting
11661 11663 * in the header.
11662 11664 */
11663 11665 len = (max_frag - hdr_len) & ~7;
11664 11666
11665 11667 /* Get a copy of the header for the trailing frags */
11666 11668 hdr_mp = ip_fragment_copyhdr((uchar_t *)ipha, hdr_len, offset, ipst,
11667 11669 mp);
11668 11670 if (hdr_mp == NULL) {
11669 11671 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11670 11672 ip_drop_output("FragFails: no hdr_mp", mp, ill);
11671 11673 freemsg(mp);
11672 11674 return (ENOBUFS);
11673 11675 }
11674 11676
11675 11677 /* Store the starting offset, with the MoreFrags flag. */
11676 11678 i1 = offset | IPH_MF | frag_flag;
11677 11679 ipha->ipha_fragment_offset_and_flags = htons((uint16_t)i1);
11678 11680
11679 11681 /* Establish the ending byte offset, based on the starting offset. */
11680 11682 offset <<= 3;
11681 11683 ip_data_end = offset + ntohs(ipha->ipha_length) - hdr_len;
11682 11684
11683 11685 /* Store the length of the first fragment in the IP header. */
11684 11686 i1 = len + hdr_len;
11685 11687 ASSERT(i1 <= IP_MAXPACKET);
11686 11688 ipha->ipha_length = htons((uint16_t)i1);
11687 11689
11688 11690 /*
11689 11691 * Compute the IP header checksum for the first frag. We have to
11690 11692 * watch out that we stop at the end of the header.
11691 11693 */
11692 11694 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
11693 11695
11694 11696 /*
11695 11697 * Now carve off the first frag. Note that this will include the
11696 11698 * original IP header.
11697 11699 */
11698 11700 if (!(mp = ip_carve_mp(&mp_orig, i1))) {
11699 11701 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11700 11702 ip_drop_output("FragFails: could not carve mp", mp_orig, ill);
11701 11703 freeb(hdr_mp);
11702 11704 freemsg(mp_orig);
11703 11705 return (ENOBUFS);
11704 11706 }
11705 11707
11706 11708 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates);
11707 11709
11708 11710 error = postfragfn(mp, nce, ixaflags, i1, xmit_hint, szone, nolzid,
11709 11711 ixa_cookie);
11710 11712 if (error != 0 && error != EWOULDBLOCK) {
11711 11713 /* No point in sending the other fragments */
11712 11714 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11713 11715 ip_drop_output("FragFails: postfragfn failed", mp_orig, ill);
11714 11716 freeb(hdr_mp);
11715 11717 freemsg(mp_orig);
11716 11718 return (error);
11717 11719 }
11718 11720
11719 11721 /* No need to redo state machine in loop */
11720 11722 ixaflags &= ~IXAF_REACH_CONF;
11721 11723
11722 11724 /* Advance the offset to the second frag starting point. */
11723 11725 offset += len;
11724 11726 /*
11725 11727 * Update hdr_len from the copied header - there might be less options
11726 11728 * in the later fragments.
11727 11729 */
11728 11730 hdr_len = IPH_HDR_LENGTH(hdr_mp->b_rptr);
11729 11731 /* Loop until done. */
11730 11732 for (;;) {
11731 11733 uint16_t offset_and_flags;
11732 11734 uint16_t ip_len;
11733 11735
11734 11736 if (ip_data_end - offset > len) {
11735 11737 /*
11736 11738 * Carve off the appropriate amount from the original
11737 11739 * datagram.
11738 11740 */
11739 11741 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) {
11740 11742 mp = NULL;
11741 11743 break;
11742 11744 }
11743 11745 /*
11744 11746 * More frags after this one. Get another copy
11745 11747 * of the header.
11746 11748 */
11747 11749 if (carve_mp->b_datap->db_ref == 1 &&
11748 11750 hdr_mp->b_wptr - hdr_mp->b_rptr <
11749 11751 carve_mp->b_rptr - carve_mp->b_datap->db_base) {
11750 11752 /* Inline IP header */
11751 11753 carve_mp->b_rptr -= hdr_mp->b_wptr -
11752 11754 hdr_mp->b_rptr;
11753 11755 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr,
11754 11756 hdr_mp->b_wptr - hdr_mp->b_rptr);
11755 11757 mp = carve_mp;
11756 11758 } else {
11757 11759 if (!(mp = copyb(hdr_mp))) {
11758 11760 freemsg(carve_mp);
11759 11761 break;
11760 11762 }
11761 11763 /* Get priority marking, if any. */
11762 11764 mp->b_band = priority;
11763 11765 mp->b_cont = carve_mp;
11764 11766 }
11765 11767 ipha = (ipha_t *)mp->b_rptr;
11766 11768 offset_and_flags = IPH_MF;
11767 11769 } else {
11768 11770 /*
11769 11771 * Last frag. Consume the header. Set len to
11770 11772 * the length of this last piece.
11771 11773 */
11772 11774 len = ip_data_end - offset;
11773 11775
11774 11776 /*
11775 11777 * Carve off the appropriate amount from the original
11776 11778 * datagram.
11777 11779 */
11778 11780 if (!(carve_mp = ip_carve_mp(&mp_orig, len))) {
11779 11781 mp = NULL;
11780 11782 break;
11781 11783 }
11782 11784 if (carve_mp->b_datap->db_ref == 1 &&
11783 11785 hdr_mp->b_wptr - hdr_mp->b_rptr <
11784 11786 carve_mp->b_rptr - carve_mp->b_datap->db_base) {
11785 11787 /* Inline IP header */
11786 11788 carve_mp->b_rptr -= hdr_mp->b_wptr -
11787 11789 hdr_mp->b_rptr;
11788 11790 bcopy(hdr_mp->b_rptr, carve_mp->b_rptr,
11789 11791 hdr_mp->b_wptr - hdr_mp->b_rptr);
11790 11792 mp = carve_mp;
11791 11793 freeb(hdr_mp);
11792 11794 hdr_mp = mp;
11793 11795 } else {
11794 11796 mp = hdr_mp;
11795 11797 /* Get priority marking, if any. */
11796 11798 mp->b_band = priority;
11797 11799 mp->b_cont = carve_mp;
11798 11800 }
11799 11801 ipha = (ipha_t *)mp->b_rptr;
11800 11802 /* A frag of a frag might have IPH_MF non-zero */
11801 11803 offset_and_flags =
11802 11804 ntohs(ipha->ipha_fragment_offset_and_flags) &
11803 11805 IPH_MF;
11804 11806 }
11805 11807 offset_and_flags |= (uint16_t)(offset >> 3);
11806 11808 offset_and_flags |= (uint16_t)frag_flag;
11807 11809 /* Store the offset and flags in the IP header. */
11808 11810 ipha->ipha_fragment_offset_and_flags = htons(offset_and_flags);
11809 11811
11810 11812 /* Store the length in the IP header. */
11811 11813 ip_len = (uint16_t)(len + hdr_len);
11812 11814 ipha->ipha_length = htons(ip_len);
11813 11815
11814 11816 /*
11815 11817 * Set the IP header checksum. Note that mp is just
11816 11818 * the header, so this is easy to pass to ip_csum.
11817 11819 */
11818 11820 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
11819 11821
11820 11822 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragCreates);
11821 11823
11822 11824 error = postfragfn(mp, nce, ixaflags, ip_len, xmit_hint, szone,
11823 11825 nolzid, ixa_cookie);
11824 11826 /* All done if we just consumed the hdr_mp. */
11825 11827 if (mp == hdr_mp) {
11826 11828 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragOKs);
11827 11829 return (error);
11828 11830 }
11829 11831 if (error != 0 && error != EWOULDBLOCK) {
11830 11832 DTRACE_PROBE2(ip__xmit__frag__fail, ill_t *, ill,
11831 11833 mblk_t *, hdr_mp);
11832 11834 /* No point in sending the other fragments */
11833 11835 break;
11834 11836 }
11835 11837
11836 11838 /* Otherwise, advance and loop. */
11837 11839 offset += len;
11838 11840 }
11839 11841 /* Clean up following allocation failure. */
11840 11842 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutFragFails);
11841 11843 ip_drop_output("FragFails: loop ended", NULL, ill);
11842 11844 if (mp != hdr_mp)
11843 11845 freeb(hdr_mp);
11844 11846 if (mp != mp_orig)
11845 11847 freemsg(mp_orig);
11846 11848 return (error);
11847 11849 }
11848 11850
11849 11851 /*
11850 11852 * Copy the header plus those options which have the copy bit set
11851 11853 */
11852 11854 static mblk_t *
11853 11855 ip_fragment_copyhdr(uchar_t *rptr, int hdr_len, int offset, ip_stack_t *ipst,
11854 11856 mblk_t *src)
11855 11857 {
11856 11858 mblk_t *mp;
11857 11859 uchar_t *up;
11858 11860
11859 11861 /*
11860 11862 * Quick check if we need to look for options without the copy bit
11861 11863 * set
11862 11864 */
11863 11865 mp = allocb_tmpl(ipst->ips_ip_wroff_extra + hdr_len, src);
11864 11866 if (!mp)
11865 11867 return (mp);
11866 11868 mp->b_rptr += ipst->ips_ip_wroff_extra;
11867 11869 if (hdr_len == IP_SIMPLE_HDR_LENGTH || offset != 0) {
11868 11870 bcopy(rptr, mp->b_rptr, hdr_len);
11869 11871 mp->b_wptr += hdr_len + ipst->ips_ip_wroff_extra;
11870 11872 return (mp);
11871 11873 }
11872 11874 up = mp->b_rptr;
11873 11875 bcopy(rptr, up, IP_SIMPLE_HDR_LENGTH);
11874 11876 up += IP_SIMPLE_HDR_LENGTH;
11875 11877 rptr += IP_SIMPLE_HDR_LENGTH;
11876 11878 hdr_len -= IP_SIMPLE_HDR_LENGTH;
11877 11879 while (hdr_len > 0) {
11878 11880 uint32_t optval;
11879 11881 uint32_t optlen;
11880 11882
11881 11883 optval = *rptr;
11882 11884 if (optval == IPOPT_EOL)
11883 11885 break;
11884 11886 if (optval == IPOPT_NOP)
11885 11887 optlen = 1;
11886 11888 else
11887 11889 optlen = rptr[1];
11888 11890 if (optval & IPOPT_COPY) {
11889 11891 bcopy(rptr, up, optlen);
11890 11892 up += optlen;
11891 11893 }
11892 11894 rptr += optlen;
11893 11895 hdr_len -= optlen;
11894 11896 }
11895 11897 /*
11896 11898 * Make sure that we drop an even number of words by filling
11897 11899 * with EOL to the next word boundary.
11898 11900 */
11899 11901 for (hdr_len = up - (mp->b_rptr + IP_SIMPLE_HDR_LENGTH);
11900 11902 hdr_len & 0x3; hdr_len++)
11901 11903 *up++ = IPOPT_EOL;
11902 11904 mp->b_wptr = up;
11903 11905 /* Update header length */
11904 11906 mp->b_rptr[0] = (uint8_t)((IP_VERSION << 4) | ((up - mp->b_rptr) >> 2));
11905 11907 return (mp);
11906 11908 }
11907 11909
11908 11910 /*
11909 11911 * Update any source route, record route, or timestamp options when
11910 11912 * sending a packet back to ourselves.
11911 11913 * Check that we are at end of strict source route.
11912 11914 * The options have been sanity checked by ip_output_options().
11913 11915 */
11914 11916 void
11915 11917 ip_output_local_options(ipha_t *ipha, ip_stack_t *ipst)
11916 11918 {
11917 11919 ipoptp_t opts;
11918 11920 uchar_t *opt;
11919 11921 uint8_t optval;
11920 11922 uint8_t optlen;
11921 11923 ipaddr_t dst;
11922 11924 uint32_t ts;
11923 11925 timestruc_t now;
11924 11926
11925 11927 for (optval = ipoptp_first(&opts, ipha);
11926 11928 optval != IPOPT_EOL;
11927 11929 optval = ipoptp_next(&opts)) {
11928 11930 opt = opts.ipoptp_cur;
11929 11931 optlen = opts.ipoptp_len;
11930 11932 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
11931 11933 switch (optval) {
11932 11934 uint32_t off;
11933 11935 case IPOPT_SSRR:
11934 11936 case IPOPT_LSRR:
11935 11937 off = opt[IPOPT_OFFSET];
11936 11938 off--;
11937 11939 if (optlen < IP_ADDR_LEN ||
11938 11940 off > optlen - IP_ADDR_LEN) {
11939 11941 /* End of source route */
11940 11942 break;
11941 11943 }
11942 11944 /*
11943 11945 * This will only happen if two consecutive entries
11944 11946 * in the source route contains our address or if
11945 11947 * it is a packet with a loose source route which
11946 11948 * reaches us before consuming the whole source route
11947 11949 */
11948 11950
11949 11951 if (optval == IPOPT_SSRR) {
11950 11952 return;
11951 11953 }
11952 11954 /*
11953 11955 * Hack: instead of dropping the packet truncate the
11954 11956 * source route to what has been used by filling the
11955 11957 * rest with IPOPT_NOP.
11956 11958 */
11957 11959 opt[IPOPT_OLEN] = (uint8_t)off;
11958 11960 while (off < optlen) {
11959 11961 opt[off++] = IPOPT_NOP;
11960 11962 }
11961 11963 break;
11962 11964 case IPOPT_RR:
11963 11965 off = opt[IPOPT_OFFSET];
11964 11966 off--;
11965 11967 if (optlen < IP_ADDR_LEN ||
11966 11968 off > optlen - IP_ADDR_LEN) {
11967 11969 /* No more room - ignore */
11968 11970 ip1dbg((
11969 11971 "ip_output_local_options: end of RR\n"));
11970 11972 break;
11971 11973 }
11972 11974 dst = htonl(INADDR_LOOPBACK);
11973 11975 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN);
11974 11976 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
11975 11977 break;
11976 11978 case IPOPT_TS:
11977 11979 /* Insert timestamp if there is romm */
11978 11980 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
11979 11981 case IPOPT_TS_TSONLY:
11980 11982 off = IPOPT_TS_TIMELEN;
11981 11983 break;
11982 11984 case IPOPT_TS_PRESPEC:
11983 11985 case IPOPT_TS_PRESPEC_RFC791:
11984 11986 /* Verify that the address matched */
11985 11987 off = opt[IPOPT_OFFSET] - 1;
11986 11988 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
11987 11989 if (ip_type_v4(dst, ipst) != IRE_LOCAL) {
11988 11990 /* Not for us */
11989 11991 break;
11990 11992 }
11991 11993 /* FALLTHRU */
11992 11994 case IPOPT_TS_TSANDADDR:
11993 11995 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
11994 11996 break;
11995 11997 default:
11996 11998 /*
11997 11999 * ip_*put_options should have already
11998 12000 * dropped this packet.
11999 12001 */
12000 12002 cmn_err(CE_PANIC, "ip_output_local_options: "
12001 12003 "unknown IT - bug in ip_output_options?\n");
12002 12004 return; /* Keep "lint" happy */
12003 12005 }
12004 12006 if (opt[IPOPT_OFFSET] - 1 + off > optlen) {
12005 12007 /* Increase overflow counter */
12006 12008 off = (opt[IPOPT_POS_OV_FLG] >> 4) + 1;
12007 12009 opt[IPOPT_POS_OV_FLG] = (uint8_t)
12008 12010 (opt[IPOPT_POS_OV_FLG] & 0x0F) |
12009 12011 (off << 4);
12010 12012 break;
12011 12013 }
12012 12014 off = opt[IPOPT_OFFSET] - 1;
12013 12015 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
12014 12016 case IPOPT_TS_PRESPEC:
12015 12017 case IPOPT_TS_PRESPEC_RFC791:
12016 12018 case IPOPT_TS_TSANDADDR:
12017 12019 dst = htonl(INADDR_LOOPBACK);
12018 12020 bcopy(&dst, (char *)opt + off, IP_ADDR_LEN);
12019 12021 opt[IPOPT_OFFSET] += IP_ADDR_LEN;
12020 12022 /* FALLTHRU */
12021 12023 case IPOPT_TS_TSONLY:
12022 12024 off = opt[IPOPT_OFFSET] - 1;
12023 12025 /* Compute # of milliseconds since midnight */
12024 12026 gethrestime(&now);
12025 12027 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
12026 12028 now.tv_nsec / (NANOSEC / MILLISEC);
12027 12029 bcopy(&ts, (char *)opt + off, IPOPT_TS_TIMELEN);
12028 12030 opt[IPOPT_OFFSET] += IPOPT_TS_TIMELEN;
12029 12031 break;
12030 12032 }
12031 12033 break;
12032 12034 }
12033 12035 }
12034 12036 }
12035 12037
12036 12038 /*
12037 12039 * Prepend an M_DATA fastpath header, and if none present prepend a
12038 12040 * DL_UNITDATA_REQ. Frees the mblk on failure.
12039 12041 *
12040 12042 * nce_dlur_mp and nce_fp_mp can not disappear once they have been set.
12041 12043 * If there is a change to them, the nce will be deleted (condemned) and
12042 12044 * a new nce_t will be created when packets are sent. Thus we need no locks
12043 12045 * to access those fields.
12044 12046 *
12045 12047 * We preserve b_band to support IPQoS. If a DL_UNITDATA_REQ is prepended
12046 12048 * we place b_band in dl_priority.dl_max.
12047 12049 */
12048 12050 static mblk_t *
12049 12051 ip_xmit_attach_llhdr(mblk_t *mp, nce_t *nce)
12050 12052 {
12051 12053 uint_t hlen;
12052 12054 mblk_t *mp1;
12053 12055 uint_t priority;
12054 12056 uchar_t *rptr;
12055 12057
12056 12058 rptr = mp->b_rptr;
12057 12059
12058 12060 ASSERT(DB_TYPE(mp) == M_DATA);
12059 12061 priority = mp->b_band;
12060 12062
12061 12063 ASSERT(nce != NULL);
12062 12064 if ((mp1 = nce->nce_fp_mp) != NULL) {
12063 12065 hlen = MBLKL(mp1);
12064 12066 /*
12065 12067 * Check if we have enough room to prepend fastpath
12066 12068 * header
12067 12069 */
12068 12070 if (hlen != 0 && (rptr - mp->b_datap->db_base) >= hlen) {
12069 12071 rptr -= hlen;
12070 12072 bcopy(mp1->b_rptr, rptr, hlen);
12071 12073 /*
12072 12074 * Set the b_rptr to the start of the link layer
12073 12075 * header
12074 12076 */
12075 12077 mp->b_rptr = rptr;
12076 12078 return (mp);
12077 12079 }
12078 12080 mp1 = copyb(mp1);
12079 12081 if (mp1 == NULL) {
12080 12082 ill_t *ill = nce->nce_ill;
12081 12083
12082 12084 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12083 12085 ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12084 12086 freemsg(mp);
12085 12087 return (NULL);
12086 12088 }
12087 12089 mp1->b_band = priority;
12088 12090 mp1->b_cont = mp;
12089 12091 DB_CKSUMSTART(mp1) = DB_CKSUMSTART(mp);
12090 12092 DB_CKSUMSTUFF(mp1) = DB_CKSUMSTUFF(mp);
12091 12093 DB_CKSUMEND(mp1) = DB_CKSUMEND(mp);
12092 12094 DB_CKSUMFLAGS(mp1) = DB_CKSUMFLAGS(mp);
12093 12095 DB_LSOMSS(mp1) = DB_LSOMSS(mp);
12094 12096 DTRACE_PROBE1(ip__xmit__copyb, (mblk_t *), mp1);
12095 12097 /*
12096 12098 * XXX disable ICK_VALID and compute checksum
12097 12099 * here; can happen if nce_fp_mp changes and
12098 12100 * it can't be copied now due to insufficient
12099 12101 * space. (unlikely, fp mp can change, but it
12100 12102 * does not increase in length)
12101 12103 */
12102 12104 return (mp1);
12103 12105 }
12104 12106 mp1 = copyb(nce->nce_dlur_mp);
12105 12107
12106 12108 if (mp1 == NULL) {
12107 12109 ill_t *ill = nce->nce_ill;
12108 12110
12109 12111 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12110 12112 ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12111 12113 freemsg(mp);
12112 12114 return (NULL);
12113 12115 }
12114 12116 mp1->b_cont = mp;
12115 12117 if (priority != 0) {
12116 12118 mp1->b_band = priority;
12117 12119 ((dl_unitdata_req_t *)(mp1->b_rptr))->dl_priority.dl_max =
12118 12120 priority;
12119 12121 }
12120 12122 return (mp1);
12121 12123 #undef rptr
12122 12124 }
12123 12125
12124 12126 /*
12125 12127 * Finish the outbound IPsec processing. This function is called from
12126 12128 * ipsec_out_process() if the IPsec packet was processed
12127 12129 * synchronously, or from {ah,esp}_kcf_callback_outbound() if it was processed
12128 12130 * asynchronously.
12129 12131 *
12130 12132 * This is common to IPv4 and IPv6.
12131 12133 */
12132 12134 int
12133 12135 ip_output_post_ipsec(mblk_t *mp, ip_xmit_attr_t *ixa)
12134 12136 {
12135 12137 iaflags_t ixaflags = ixa->ixa_flags;
12136 12138 uint_t pktlen;
12137 12139
12138 12140
12139 12141 /* AH/ESP don't update ixa_pktlen when they modify the packet */
12140 12142 if (ixaflags & IXAF_IS_IPV4) {
12141 12143 ipha_t *ipha = (ipha_t *)mp->b_rptr;
12142 12144
12143 12145 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
12144 12146 pktlen = ntohs(ipha->ipha_length);
12145 12147 } else {
12146 12148 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
12147 12149
12148 12150 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION);
12149 12151 pktlen = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN;
12150 12152 }
12151 12153
12152 12154 /*
12153 12155 * We release any hard reference on the SAs here to make
12154 12156 * sure the SAs can be garbage collected. ipsr_sa has a soft reference
12155 12157 * on the SAs.
12156 12158 * If in the future we want the hard latching of the SAs in the
12157 12159 * ip_xmit_attr_t then we should remove this.
12158 12160 */
12159 12161 if (ixa->ixa_ipsec_esp_sa != NULL) {
12160 12162 IPSA_REFRELE(ixa->ixa_ipsec_esp_sa);
12161 12163 ixa->ixa_ipsec_esp_sa = NULL;
12162 12164 }
12163 12165 if (ixa->ixa_ipsec_ah_sa != NULL) {
12164 12166 IPSA_REFRELE(ixa->ixa_ipsec_ah_sa);
12165 12167 ixa->ixa_ipsec_ah_sa = NULL;
12166 12168 }
12167 12169
12168 12170 /* Do we need to fragment? */
12169 12171 if ((ixa->ixa_flags & IXAF_IPV6_ADD_FRAGHDR) ||
12170 12172 pktlen > ixa->ixa_fragsize) {
12171 12173 if (ixaflags & IXAF_IS_IPV4) {
12172 12174 ASSERT(!(ixa->ixa_flags & IXAF_IPV6_ADD_FRAGHDR));
12173 12175 /*
12174 12176 * We check for the DF case in ipsec_out_process
12175 12177 * hence this only handles the non-DF case.
12176 12178 */
12177 12179 return (ip_fragment_v4(mp, ixa->ixa_nce, ixa->ixa_flags,
12178 12180 pktlen, ixa->ixa_fragsize,
12179 12181 ixa->ixa_xmit_hint, ixa->ixa_zoneid,
12180 12182 ixa->ixa_no_loop_zoneid, ixa->ixa_postfragfn,
12181 12183 &ixa->ixa_cookie));
12182 12184 } else {
12183 12185 mp = ip_fraghdr_add_v6(mp, ixa->ixa_ident, ixa);
12184 12186 if (mp == NULL) {
12185 12187 /* MIB and ip_drop_output already done */
12186 12188 return (ENOMEM);
12187 12189 }
12188 12190 pktlen += sizeof (ip6_frag_t);
12189 12191 if (pktlen > ixa->ixa_fragsize) {
12190 12192 return (ip_fragment_v6(mp, ixa->ixa_nce,
12191 12193 ixa->ixa_flags, pktlen,
12192 12194 ixa->ixa_fragsize, ixa->ixa_xmit_hint,
12193 12195 ixa->ixa_zoneid, ixa->ixa_no_loop_zoneid,
12194 12196 ixa->ixa_postfragfn, &ixa->ixa_cookie));
12195 12197 }
12196 12198 }
12197 12199 }
12198 12200 return ((ixa->ixa_postfragfn)(mp, ixa->ixa_nce, ixa->ixa_flags,
12199 12201 pktlen, ixa->ixa_xmit_hint, ixa->ixa_zoneid,
12200 12202 ixa->ixa_no_loop_zoneid, NULL));
12201 12203 }
12202 12204
12203 12205 /*
12204 12206 * Finish the inbound IPsec processing. This function is called from
12205 12207 * ipsec_out_process() if the IPsec packet was processed
12206 12208 * synchronously, or from {ah,esp}_kcf_callback_outbound() if it was processed
12207 12209 * asynchronously.
12208 12210 *
12209 12211 * This is common to IPv4 and IPv6.
12210 12212 */
12211 12213 void
12212 12214 ip_input_post_ipsec(mblk_t *mp, ip_recv_attr_t *ira)
12213 12215 {
12214 12216 iaflags_t iraflags = ira->ira_flags;
12215 12217
12216 12218 /* Length might have changed */
12217 12219 if (iraflags & IRAF_IS_IPV4) {
12218 12220 ipha_t *ipha = (ipha_t *)mp->b_rptr;
12219 12221
12220 12222 ASSERT(IPH_HDR_VERSION(ipha) == IPV4_VERSION);
12221 12223 ira->ira_pktlen = ntohs(ipha->ipha_length);
12222 12224 ira->ira_ip_hdr_length = IPH_HDR_LENGTH(ipha);
12223 12225 ira->ira_protocol = ipha->ipha_protocol;
12224 12226
12225 12227 ip_fanout_v4(mp, ipha, ira);
12226 12228 } else {
12227 12229 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
12228 12230 uint8_t *nexthdrp;
12229 12231
12230 12232 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV6_VERSION);
12231 12233 ira->ira_pktlen = ntohs(ip6h->ip6_plen) + IPV6_HDR_LEN;
12232 12234 if (!ip_hdr_length_nexthdr_v6(mp, ip6h, &ira->ira_ip_hdr_length,
12233 12235 &nexthdrp)) {
12234 12236 /* Malformed packet */
12235 12237 BUMP_MIB(ira->ira_ill->ill_ip_mib, ipIfStatsInDiscards);
12236 12238 ip_drop_input("ipIfStatsInDiscards", mp, ira->ira_ill);
12237 12239 freemsg(mp);
12238 12240 return;
12239 12241 }
12240 12242 ira->ira_protocol = *nexthdrp;
12241 12243 ip_fanout_v6(mp, ip6h, ira);
12242 12244 }
12243 12245 }
12244 12246
12245 12247 /*
12246 12248 * Select which AH & ESP SA's to use (if any) for the outbound packet.
12247 12249 *
12248 12250 * If this function returns B_TRUE, the requested SA's have been filled
12249 12251 * into the ixa_ipsec_*_sa pointers.
12250 12252 *
12251 12253 * If the function returns B_FALSE, the packet has been "consumed", most
12252 12254 * likely by an ACQUIRE sent up via PF_KEY to a key management daemon.
12253 12255 *
12254 12256 * The SA references created by the protocol-specific "select"
12255 12257 * function will be released in ip_output_post_ipsec.
12256 12258 */
12257 12259 static boolean_t
12258 12260 ipsec_out_select_sa(mblk_t *mp, ip_xmit_attr_t *ixa)
12259 12261 {
12260 12262 boolean_t need_ah_acquire = B_FALSE, need_esp_acquire = B_FALSE;
12261 12263 ipsec_policy_t *pp;
12262 12264 ipsec_action_t *ap;
12263 12265
12264 12266 ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE);
12265 12267 ASSERT((ixa->ixa_ipsec_policy != NULL) ||
12266 12268 (ixa->ixa_ipsec_action != NULL));
12267 12269
12268 12270 ap = ixa->ixa_ipsec_action;
12269 12271 if (ap == NULL) {
12270 12272 pp = ixa->ixa_ipsec_policy;
12271 12273 ASSERT(pp != NULL);
12272 12274 ap = pp->ipsp_act;
12273 12275 ASSERT(ap != NULL);
12274 12276 }
12275 12277
12276 12278 /*
12277 12279 * We have an action. now, let's select SA's.
12278 12280 * A side effect of setting ixa_ipsec_*_sa is that it will
12279 12281 * be cached in the conn_t.
12280 12282 */
12281 12283 if (ap->ipa_want_esp) {
12282 12284 if (ixa->ixa_ipsec_esp_sa == NULL) {
12283 12285 need_esp_acquire = !ipsec_outbound_sa(mp, ixa,
12284 12286 IPPROTO_ESP);
12285 12287 }
12286 12288 ASSERT(need_esp_acquire || ixa->ixa_ipsec_esp_sa != NULL);
12287 12289 }
12288 12290
12289 12291 if (ap->ipa_want_ah) {
12290 12292 if (ixa->ixa_ipsec_ah_sa == NULL) {
12291 12293 need_ah_acquire = !ipsec_outbound_sa(mp, ixa,
12292 12294 IPPROTO_AH);
12293 12295 }
12294 12296 ASSERT(need_ah_acquire || ixa->ixa_ipsec_ah_sa != NULL);
12295 12297 /*
12296 12298 * The ESP and AH processing order needs to be preserved
12297 12299 * when both protocols are required (ESP should be applied
12298 12300 * before AH for an outbound packet). Force an ESP ACQUIRE
12299 12301 * when both ESP and AH are required, and an AH ACQUIRE
12300 12302 * is needed.
12301 12303 */
12302 12304 if (ap->ipa_want_esp && need_ah_acquire)
12303 12305 need_esp_acquire = B_TRUE;
12304 12306 }
12305 12307
12306 12308 /*
12307 12309 * Send an ACQUIRE (extended, regular, or both) if we need one.
12308 12310 * Release SAs that got referenced, but will not be used until we
12309 12311 * acquire _all_ of the SAs we need.
12310 12312 */
12311 12313 if (need_ah_acquire || need_esp_acquire) {
12312 12314 if (ixa->ixa_ipsec_ah_sa != NULL) {
12313 12315 IPSA_REFRELE(ixa->ixa_ipsec_ah_sa);
12314 12316 ixa->ixa_ipsec_ah_sa = NULL;
12315 12317 }
12316 12318 if (ixa->ixa_ipsec_esp_sa != NULL) {
12317 12319 IPSA_REFRELE(ixa->ixa_ipsec_esp_sa);
12318 12320 ixa->ixa_ipsec_esp_sa = NULL;
12319 12321 }
12320 12322
12321 12323 sadb_acquire(mp, ixa, need_ah_acquire, need_esp_acquire);
12322 12324 return (B_FALSE);
12323 12325 }
12324 12326
12325 12327 return (B_TRUE);
12326 12328 }
12327 12329
12328 12330 /*
12329 12331 * Handle IPsec output processing.
12330 12332 * This function is only entered once for a given packet.
12331 12333 * We try to do things synchronously, but if we need to have user-level
12332 12334 * set up SAs, or ESP or AH uses asynchronous kEF, then the operation
12333 12335 * will be completed
12334 12336 * - when the SAs are added in esp_add_sa_finish/ah_add_sa_finish
12335 12337 * - when asynchronous ESP is done it will do AH
12336 12338 *
12337 12339 * In all cases we come back in ip_output_post_ipsec() to fragment and
12338 12340 * send out the packet.
12339 12341 */
12340 12342 int
12341 12343 ipsec_out_process(mblk_t *mp, ip_xmit_attr_t *ixa)
12342 12344 {
12343 12345 ill_t *ill = ixa->ixa_nce->nce_ill;
12344 12346 ip_stack_t *ipst = ixa->ixa_ipst;
12345 12347 ipsec_stack_t *ipss;
12346 12348 ipsec_policy_t *pp;
12347 12349 ipsec_action_t *ap;
12348 12350
12349 12351 ASSERT(ixa->ixa_flags & IXAF_IPSEC_SECURE);
12350 12352
12351 12353 ASSERT((ixa->ixa_ipsec_policy != NULL) ||
12352 12354 (ixa->ixa_ipsec_action != NULL));
12353 12355
12354 12356 ipss = ipst->ips_netstack->netstack_ipsec;
12355 12357 if (!ipsec_loaded(ipss)) {
12356 12358 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12357 12359 ip_drop_packet(mp, B_TRUE, ill,
12358 12360 DROPPER(ipss, ipds_ip_ipsec_not_loaded),
12359 12361 &ipss->ipsec_dropper);
12360 12362 return (ENOTSUP);
12361 12363 }
12362 12364
12363 12365 ap = ixa->ixa_ipsec_action;
12364 12366 if (ap == NULL) {
12365 12367 pp = ixa->ixa_ipsec_policy;
12366 12368 ASSERT(pp != NULL);
12367 12369 ap = pp->ipsp_act;
12368 12370 ASSERT(ap != NULL);
12369 12371 }
12370 12372
12371 12373 /* Handle explicit drop action and bypass. */
12372 12374 switch (ap->ipa_act.ipa_type) {
12373 12375 case IPSEC_ACT_DISCARD:
12374 12376 case IPSEC_ACT_REJECT:
12375 12377 ip_drop_packet(mp, B_FALSE, ill,
12376 12378 DROPPER(ipss, ipds_spd_explicit), &ipss->ipsec_spd_dropper);
12377 12379 return (EHOSTUNREACH); /* IPsec policy failure */
12378 12380 case IPSEC_ACT_BYPASS:
12379 12381 return (ip_output_post_ipsec(mp, ixa));
12380 12382 }
12381 12383
12382 12384 /*
12383 12385 * The order of processing is first insert a IP header if needed.
12384 12386 * Then insert the ESP header and then the AH header.
12385 12387 */
12386 12388 if ((ixa->ixa_flags & IXAF_IS_IPV4) && ap->ipa_want_se) {
12387 12389 /*
12388 12390 * First get the outer IP header before sending
12389 12391 * it to ESP.
12390 12392 */
12391 12393 ipha_t *oipha, *iipha;
12392 12394 mblk_t *outer_mp, *inner_mp;
12393 12395
12394 12396 if ((outer_mp = allocb(sizeof (ipha_t), BPRI_HI)) == NULL) {
12395 12397 (void) mi_strlog(ill->ill_rq, 0,
12396 12398 SL_ERROR|SL_TRACE|SL_CONSOLE,
12397 12399 "ipsec_out_process: "
12398 12400 "Self-Encapsulation failed: Out of memory\n");
12399 12401 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
12400 12402 ip_drop_output("ipIfStatsOutDiscards", mp, ill);
12401 12403 freemsg(mp);
12402 12404 return (ENOBUFS);
12403 12405 }
12404 12406 inner_mp = mp;
12405 12407 ASSERT(inner_mp->b_datap->db_type == M_DATA);
12406 12408 oipha = (ipha_t *)outer_mp->b_rptr;
12407 12409 iipha = (ipha_t *)inner_mp->b_rptr;
12408 12410 *oipha = *iipha;
12409 12411 outer_mp->b_wptr += sizeof (ipha_t);
12410 12412 oipha->ipha_length = htons(ntohs(iipha->ipha_length) +
12411 12413 sizeof (ipha_t));
12412 12414 oipha->ipha_protocol = IPPROTO_ENCAP;
12413 12415 oipha->ipha_version_and_hdr_length =
12414 12416 IP_SIMPLE_HDR_VERSION;
12415 12417 oipha->ipha_hdr_checksum = 0;
12416 12418 oipha->ipha_hdr_checksum = ip_csum_hdr(oipha);
12417 12419 outer_mp->b_cont = inner_mp;
12418 12420 mp = outer_mp;
12419 12421
12420 12422 ixa->ixa_flags |= IXAF_IPSEC_TUNNEL;
12421 12423 }
12422 12424
12423 12425 /* If we need to wait for a SA then we can't return any errno */
12424 12426 if (((ap->ipa_want_ah && (ixa->ixa_ipsec_ah_sa == NULL)) ||
12425 12427 (ap->ipa_want_esp && (ixa->ixa_ipsec_esp_sa == NULL))) &&
12426 12428 !ipsec_out_select_sa(mp, ixa))
12427 12429 return (0);
12428 12430
12429 12431 /*
12430 12432 * By now, we know what SA's to use. Toss over to ESP & AH
12431 12433 * to do the heavy lifting.
12432 12434 */
12433 12435 if (ap->ipa_want_esp) {
12434 12436 ASSERT(ixa->ixa_ipsec_esp_sa != NULL);
12435 12437
12436 12438 mp = ixa->ixa_ipsec_esp_sa->ipsa_output_func(mp, ixa);
12437 12439 if (mp == NULL) {
12438 12440 /*
12439 12441 * Either it failed or is pending. In the former case
12440 12442 * ipIfStatsInDiscards was increased.
12441 12443 */
12442 12444 return (0);
12443 12445 }
12444 12446 }
12445 12447
12446 12448 if (ap->ipa_want_ah) {
12447 12449 ASSERT(ixa->ixa_ipsec_ah_sa != NULL);
12448 12450
12449 12451 mp = ixa->ixa_ipsec_ah_sa->ipsa_output_func(mp, ixa);
12450 12452 if (mp == NULL) {
12451 12453 /*
12452 12454 * Either it failed or is pending. In the former case
12453 12455 * ipIfStatsInDiscards was increased.
12454 12456 */
12455 12457 return (0);
12456 12458 }
12457 12459 }
12458 12460 /*
12459 12461 * We are done with IPsec processing. Send it over
12460 12462 * the wire.
12461 12463 */
12462 12464 return (ip_output_post_ipsec(mp, ixa));
12463 12465 }
12464 12466
12465 12467 /*
12466 12468 * ioctls that go through a down/up sequence may need to wait for the down
12467 12469 * to complete. This involves waiting for the ire and ipif refcnts to go down
12468 12470 * to zero. Subsequently the ioctl is restarted from ipif_ill_refrele_tail.
12469 12471 */
12470 12472 /* ARGSUSED */
12471 12473 void
12472 12474 ip_reprocess_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *dummy_arg)
12473 12475 {
12474 12476 struct iocblk *iocp;
12475 12477 mblk_t *mp1;
12476 12478 ip_ioctl_cmd_t *ipip;
12477 12479 int err;
12478 12480 sin_t *sin;
12479 12481 struct lifreq *lifr;
12480 12482 struct ifreq *ifr;
12481 12483
12482 12484 iocp = (struct iocblk *)mp->b_rptr;
12483 12485 ASSERT(ipsq != NULL);
12484 12486 /* Existence of mp1 verified in ip_wput_nondata */
12485 12487 mp1 = mp->b_cont->b_cont;
12486 12488 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12487 12489 if (ipip->ipi_cmd == SIOCSLIFNAME || ipip->ipi_cmd == IF_UNITSEL) {
12488 12490 /*
12489 12491 * Special case where ipx_current_ipif is not set:
12490 12492 * ill_phyint_reinit merged the v4 and v6 into a single ipsq.
12491 12493 * We are here as were not able to complete the operation in
12492 12494 * ipif_set_values because we could not become exclusive on
12493 12495 * the new ipsq.
12494 12496 */
12495 12497 ill_t *ill = q->q_ptr;
12496 12498 ipsq_current_start(ipsq, ill->ill_ipif, ipip->ipi_cmd);
12497 12499 }
12498 12500 ASSERT(ipsq->ipsq_xop->ipx_current_ipif != NULL);
12499 12501
12500 12502 if (ipip->ipi_cmd_type == IF_CMD) {
12501 12503 /* This a old style SIOC[GS]IF* command */
12502 12504 ifr = (struct ifreq *)mp1->b_rptr;
12503 12505 sin = (sin_t *)&ifr->ifr_addr;
12504 12506 } else if (ipip->ipi_cmd_type == LIF_CMD) {
12505 12507 /* This a new style SIOC[GS]LIF* command */
12506 12508 lifr = (struct lifreq *)mp1->b_rptr;
12507 12509 sin = (sin_t *)&lifr->lifr_addr;
12508 12510 } else {
12509 12511 sin = NULL;
12510 12512 }
12511 12513
12512 12514 err = (*ipip->ipi_func_restart)(ipsq->ipsq_xop->ipx_current_ipif, sin,
12513 12515 q, mp, ipip, mp1->b_rptr);
12514 12516
12515 12517 DTRACE_PROBE4(ipif__ioctl, char *, "ip_reprocess_ioctl finish",
12516 12518 int, ipip->ipi_cmd,
12517 12519 ill_t *, ipsq->ipsq_xop->ipx_current_ipif->ipif_ill,
12518 12520 ipif_t *, ipsq->ipsq_xop->ipx_current_ipif);
12519 12521
12520 12522 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq);
12521 12523 }
12522 12524
12523 12525 /*
12524 12526 * ioctl processing
12525 12527 *
12526 12528 * ioctl processing starts with ip_sioctl_copyin_setup(), which looks up
12527 12529 * the ioctl command in the ioctl tables, determines the copyin data size
12528 12530 * from the ipi_copyin_size field, and does an mi_copyin() of that size.
12529 12531 *
12530 12532 * ioctl processing then continues when the M_IOCDATA makes its way down to
12531 12533 * ip_wput_nondata(). The ioctl is looked up again in the ioctl table, its
12532 12534 * associated 'conn' is refheld till the end of the ioctl and the general
12533 12535 * ioctl processing function ip_process_ioctl() is called to extract the
12534 12536 * arguments and process the ioctl. To simplify extraction, ioctl commands
12535 12537 * are "typed" based on the arguments they take (e.g., LIF_CMD which takes a
12536 12538 * `struct lifreq'), and a common extract function (e.g., ip_extract_lifreq())
12537 12539 * is used to extract the ioctl's arguments.
12538 12540 *
12539 12541 * ip_process_ioctl determines if the ioctl needs to be serialized, and if
12540 12542 * so goes thru the serialization primitive ipsq_try_enter. Then the
12541 12543 * appropriate function to handle the ioctl is called based on the entry in
12542 12544 * the ioctl table. ioctl completion is encapsulated in ip_ioctl_finish
12543 12545 * which also refreleases the 'conn' that was refheld at the start of the
12544 12546 * ioctl. Finally ipsq_exit is called if needed to exit the ipsq.
12545 12547 *
12546 12548 * Many exclusive ioctls go thru an internal down up sequence as part of
12547 12549 * the operation. For example an attempt to change the IP address of an
12548 12550 * ipif entails ipif_down, set address, ipif_up. Bringing down the interface
12549 12551 * does all the cleanup such as deleting all ires that use this address.
12550 12552 * Then we need to wait till all references to the interface go away.
12551 12553 */
12552 12554 void
12553 12555 ip_process_ioctl(ipsq_t *ipsq, queue_t *q, mblk_t *mp, void *arg)
12554 12556 {
12555 12557 struct iocblk *iocp = (struct iocblk *)mp->b_rptr;
12556 12558 ip_ioctl_cmd_t *ipip = arg;
12557 12559 ip_extract_func_t *extract_funcp;
12558 12560 cmd_info_t ci;
12559 12561 int err;
12560 12562 boolean_t entered_ipsq = B_FALSE;
12561 12563
12562 12564 ip3dbg(("ip_process_ioctl: ioctl %X\n", iocp->ioc_cmd));
12563 12565
12564 12566 if (ipip == NULL)
12565 12567 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12566 12568
12567 12569 /*
12568 12570 * SIOCLIFADDIF needs to go thru a special path since the
12569 12571 * ill may not exist yet. This happens in the case of lo0
12570 12572 * which is created using this ioctl.
12571 12573 */
12572 12574 if (ipip->ipi_cmd == SIOCLIFADDIF) {
12573 12575 err = ip_sioctl_addif(NULL, NULL, q, mp, NULL, NULL);
12574 12576 DTRACE_PROBE4(ipif__ioctl, char *, "ip_process_ioctl finish",
12575 12577 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12576 12578 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12577 12579 return;
12578 12580 }
12579 12581
12580 12582 ci.ci_ipif = NULL;
12581 12583 switch (ipip->ipi_cmd_type) {
12582 12584 case MISC_CMD:
12583 12585 case MSFILT_CMD:
12584 12586 /*
12585 12587 * All MISC_CMD ioctls come in here -- e.g. SIOCGLIFCONF.
12586 12588 */
12587 12589 if (ipip->ipi_cmd == IF_UNITSEL) {
12588 12590 /* ioctl comes down the ill */
12589 12591 ci.ci_ipif = ((ill_t *)q->q_ptr)->ill_ipif;
12590 12592 ipif_refhold(ci.ci_ipif);
12591 12593 }
12592 12594 err = 0;
12593 12595 ci.ci_sin = NULL;
12594 12596 ci.ci_sin6 = NULL;
12595 12597 ci.ci_lifr = NULL;
12596 12598 extract_funcp = NULL;
12597 12599 break;
12598 12600
12599 12601 case IF_CMD:
12600 12602 case LIF_CMD:
12601 12603 extract_funcp = ip_extract_lifreq;
12602 12604 break;
12603 12605
12604 12606 case ARP_CMD:
12605 12607 case XARP_CMD:
12606 12608 extract_funcp = ip_extract_arpreq;
12607 12609 break;
12608 12610
12609 12611 default:
12610 12612 ASSERT(0);
12611 12613 }
12612 12614
12613 12615 if (extract_funcp != NULL) {
12614 12616 err = (*extract_funcp)(q, mp, ipip, &ci);
12615 12617 if (err != 0) {
12616 12618 DTRACE_PROBE4(ipif__ioctl,
12617 12619 char *, "ip_process_ioctl finish err",
12618 12620 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12619 12621 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12620 12622 return;
12621 12623 }
12622 12624
12623 12625 /*
12624 12626 * All of the extraction functions return a refheld ipif.
12625 12627 */
12626 12628 ASSERT(ci.ci_ipif != NULL);
12627 12629 }
12628 12630
12629 12631 if (!(ipip->ipi_flags & IPI_WR)) {
12630 12632 /*
12631 12633 * A return value of EINPROGRESS means the ioctl is
12632 12634 * either queued and waiting for some reason or has
12633 12635 * already completed.
12634 12636 */
12635 12637 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip,
12636 12638 ci.ci_lifr);
12637 12639 if (ci.ci_ipif != NULL) {
12638 12640 DTRACE_PROBE4(ipif__ioctl,
12639 12641 char *, "ip_process_ioctl finish RD",
12640 12642 int, ipip->ipi_cmd, ill_t *, ci.ci_ipif->ipif_ill,
12641 12643 ipif_t *, ci.ci_ipif);
12642 12644 ipif_refrele(ci.ci_ipif);
12643 12645 } else {
12644 12646 DTRACE_PROBE4(ipif__ioctl,
12645 12647 char *, "ip_process_ioctl finish RD",
12646 12648 int, ipip->ipi_cmd, ill_t *, NULL, ipif_t *, NULL);
12647 12649 }
12648 12650 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), NULL);
12649 12651 return;
12650 12652 }
12651 12653
12652 12654 ASSERT(ci.ci_ipif != NULL);
12653 12655
12654 12656 /*
12655 12657 * If ipsq is non-NULL, we are already being called exclusively
12656 12658 */
12657 12659 ASSERT(ipsq == NULL || IAM_WRITER_IPSQ(ipsq));
12658 12660 if (ipsq == NULL) {
12659 12661 ipsq = ipsq_try_enter(ci.ci_ipif, NULL, q, mp, ip_process_ioctl,
12660 12662 NEW_OP, B_TRUE);
12661 12663 if (ipsq == NULL) {
12662 12664 ipif_refrele(ci.ci_ipif);
12663 12665 return;
12664 12666 }
12665 12667 entered_ipsq = B_TRUE;
12666 12668 }
12667 12669 /*
12668 12670 * Release the ipif so that ipif_down and friends that wait for
12669 12671 * references to go away are not misled about the current ipif_refcnt
12670 12672 * values. We are writer so we can access the ipif even after releasing
12671 12673 * the ipif.
12672 12674 */
12673 12675 ipif_refrele(ci.ci_ipif);
12674 12676
12675 12677 ipsq_current_start(ipsq, ci.ci_ipif, ipip->ipi_cmd);
12676 12678
12677 12679 /*
12678 12680 * A return value of EINPROGRESS means the ioctl is
12679 12681 * either queued and waiting for some reason or has
12680 12682 * already completed.
12681 12683 */
12682 12684 err = (*ipip->ipi_func)(ci.ci_ipif, ci.ci_sin, q, mp, ipip, ci.ci_lifr);
12683 12685
12684 12686 DTRACE_PROBE4(ipif__ioctl, char *, "ip_process_ioctl finish WR",
12685 12687 int, ipip->ipi_cmd,
12686 12688 ill_t *, ci.ci_ipif == NULL ? NULL : ci.ci_ipif->ipif_ill,
12687 12689 ipif_t *, ci.ci_ipif);
12688 12690 ip_ioctl_finish(q, mp, err, IPI2MODE(ipip), ipsq);
12689 12691
12690 12692 if (entered_ipsq)
12691 12693 ipsq_exit(ipsq);
12692 12694 }
12693 12695
12694 12696 /*
12695 12697 * Complete the ioctl. Typically ioctls use the mi package and need to
12696 12698 * do mi_copyout/mi_copy_done.
12697 12699 */
12698 12700 void
12699 12701 ip_ioctl_finish(queue_t *q, mblk_t *mp, int err, int mode, ipsq_t *ipsq)
12700 12702 {
12701 12703 conn_t *connp = NULL;
12702 12704
12703 12705 if (err == EINPROGRESS)
12704 12706 return;
12705 12707
12706 12708 if (CONN_Q(q)) {
12707 12709 connp = Q_TO_CONN(q);
12708 12710 ASSERT(connp->conn_ref >= 2);
12709 12711 }
12710 12712
12711 12713 switch (mode) {
12712 12714 case COPYOUT:
12713 12715 if (err == 0)
12714 12716 mi_copyout(q, mp);
12715 12717 else
12716 12718 mi_copy_done(q, mp, err);
12717 12719 break;
12718 12720
12719 12721 case NO_COPYOUT:
12720 12722 mi_copy_done(q, mp, err);
12721 12723 break;
12722 12724
12723 12725 default:
12724 12726 ASSERT(mode == CONN_CLOSE); /* aborted through CONN_CLOSE */
12725 12727 break;
12726 12728 }
12727 12729
12728 12730 /*
12729 12731 * The conn refhold and ioctlref placed on the conn at the start of the
12730 12732 * ioctl are released here.
12731 12733 */
12732 12734 if (connp != NULL) {
12733 12735 CONN_DEC_IOCTLREF(connp);
12734 12736 CONN_OPER_PENDING_DONE(connp);
12735 12737 }
12736 12738
12737 12739 if (ipsq != NULL)
12738 12740 ipsq_current_finish(ipsq);
12739 12741 }
12740 12742
12741 12743 /* Handles all non data messages */
12742 12744 void
12743 12745 ip_wput_nondata(queue_t *q, mblk_t *mp)
12744 12746 {
12745 12747 mblk_t *mp1;
12746 12748 struct iocblk *iocp;
12747 12749 ip_ioctl_cmd_t *ipip;
12748 12750 conn_t *connp;
12749 12751 cred_t *cr;
12750 12752 char *proto_str;
12751 12753
12752 12754 if (CONN_Q(q))
12753 12755 connp = Q_TO_CONN(q);
12754 12756 else
12755 12757 connp = NULL;
12756 12758
12757 12759 switch (DB_TYPE(mp)) {
12758 12760 case M_IOCTL:
12759 12761 /*
12760 12762 * IOCTL processing begins in ip_sioctl_copyin_setup which
12761 12763 * will arrange to copy in associated control structures.
12762 12764 */
12763 12765 ip_sioctl_copyin_setup(q, mp);
12764 12766 return;
12765 12767 case M_IOCDATA:
12766 12768 /*
12767 12769 * Ensure that this is associated with one of our trans-
12768 12770 * parent ioctls. If it's not ours, discard it if we're
12769 12771 * running as a driver, or pass it on if we're a module.
12770 12772 */
12771 12773 iocp = (struct iocblk *)mp->b_rptr;
12772 12774 ipip = ip_sioctl_lookup(iocp->ioc_cmd);
12773 12775 if (ipip == NULL) {
12774 12776 if (q->q_next == NULL) {
12775 12777 goto nak;
12776 12778 } else {
12777 12779 putnext(q, mp);
12778 12780 }
12779 12781 return;
12780 12782 }
12781 12783 if ((q->q_next != NULL) && !(ipip->ipi_flags & IPI_MODOK)) {
12782 12784 /*
12783 12785 * The ioctl is one we recognise, but is not consumed
12784 12786 * by IP as a module and we are a module, so we drop
12785 12787 */
12786 12788 goto nak;
12787 12789 }
12788 12790
12789 12791 /* IOCTL continuation following copyin or copyout. */
12790 12792 if (mi_copy_state(q, mp, NULL) == -1) {
12791 12793 /*
12792 12794 * The copy operation failed. mi_copy_state already
12793 12795 * cleaned up, so we're out of here.
12794 12796 */
12795 12797 return;
12796 12798 }
12797 12799 /*
12798 12800 * If we just completed a copy in, we become writer and
12799 12801 * continue processing in ip_sioctl_copyin_done. If it
12800 12802 * was a copy out, we call mi_copyout again. If there is
12801 12803 * nothing more to copy out, it will complete the IOCTL.
12802 12804 */
12803 12805 if (MI_COPY_DIRECTION(mp) == MI_COPY_IN) {
12804 12806 if (!(mp1 = mp->b_cont) || !(mp1 = mp1->b_cont)) {
12805 12807 mi_copy_done(q, mp, EPROTO);
12806 12808 return;
12807 12809 }
12808 12810 /*
12809 12811 * Check for cases that need more copying. A return
12810 12812 * value of 0 means a second copyin has been started,
12811 12813 * so we return; a return value of 1 means no more
12812 12814 * copying is needed, so we continue.
12813 12815 */
12814 12816 if (ipip->ipi_cmd_type == MSFILT_CMD &&
12815 12817 MI_COPY_COUNT(mp) == 1) {
12816 12818 if (ip_copyin_msfilter(q, mp) == 0)
12817 12819 return;
12818 12820 }
12819 12821 /*
12820 12822 * Refhold the conn, till the ioctl completes. This is
12821 12823 * needed in case the ioctl ends up in the pending mp
12822 12824 * list. Every mp in the ipx_pending_mp list must have
12823 12825 * a refhold on the conn to resume processing. The
12824 12826 * refhold is released when the ioctl completes
12825 12827 * (whether normally or abnormally). An ioctlref is also
12826 12828 * placed on the conn to prevent TCP from removing the
12827 12829 * queue needed to send the ioctl reply back.
12828 12830 * In all cases ip_ioctl_finish is called to finish
12829 12831 * the ioctl and release the refholds.
12830 12832 */
12831 12833 if (connp != NULL) {
12832 12834 /* This is not a reentry */
12833 12835 CONN_INC_REF(connp);
12834 12836 CONN_INC_IOCTLREF(connp);
12835 12837 } else {
12836 12838 if (!(ipip->ipi_flags & IPI_MODOK)) {
12837 12839 mi_copy_done(q, mp, EINVAL);
12838 12840 return;
12839 12841 }
12840 12842 }
12841 12843
12842 12844 ip_process_ioctl(NULL, q, mp, ipip);
12843 12845
12844 12846 } else {
12845 12847 mi_copyout(q, mp);
12846 12848 }
12847 12849 return;
12848 12850
12849 12851 case M_IOCNAK:
12850 12852 /*
12851 12853 * The only way we could get here is if a resolver didn't like
12852 12854 * an IOCTL we sent it. This shouldn't happen.
12853 12855 */
12854 12856 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
12855 12857 "ip_wput_nondata: unexpected M_IOCNAK, ioc_cmd 0x%x",
12856 12858 ((struct iocblk *)mp->b_rptr)->ioc_cmd);
12857 12859 freemsg(mp);
12858 12860 return;
12859 12861 case M_IOCACK:
12860 12862 /* /dev/ip shouldn't see this */
12861 12863 goto nak;
12862 12864 case M_FLUSH:
12863 12865 if (*mp->b_rptr & FLUSHW)
12864 12866 flushq(q, FLUSHALL);
12865 12867 if (q->q_next) {
12866 12868 putnext(q, mp);
12867 12869 return;
12868 12870 }
12869 12871 if (*mp->b_rptr & FLUSHR) {
12870 12872 *mp->b_rptr &= ~FLUSHW;
12871 12873 qreply(q, mp);
12872 12874 return;
12873 12875 }
12874 12876 freemsg(mp);
12875 12877 return;
12876 12878 case M_CTL:
12877 12879 break;
12878 12880 case M_PROTO:
12879 12881 case M_PCPROTO:
12880 12882 /*
12881 12883 * The only PROTO messages we expect are SNMP-related.
12882 12884 */
12883 12885 switch (((union T_primitives *)mp->b_rptr)->type) {
12884 12886 case T_SVR4_OPTMGMT_REQ:
12885 12887 ip2dbg(("ip_wput_nondata: T_SVR4_OPTMGMT_REQ "
12886 12888 "flags %x\n",
12887 12889 ((struct T_optmgmt_req *)mp->b_rptr)->MGMT_flags));
12888 12890
12889 12891 if (connp == NULL) {
12890 12892 proto_str = "T_SVR4_OPTMGMT_REQ";
12891 12893 goto protonak;
12892 12894 }
12893 12895
12894 12896 /*
12895 12897 * All Solaris components should pass a db_credp
12896 12898 * for this TPI message, hence we ASSERT.
12897 12899 * But in case there is some other M_PROTO that looks
12898 12900 * like a TPI message sent by some other kernel
12899 12901 * component, we check and return an error.
12900 12902 */
12901 12903 cr = msg_getcred(mp, NULL);
12902 12904 ASSERT(cr != NULL);
12903 12905 if (cr == NULL) {
12904 12906 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, EINVAL);
12905 12907 if (mp != NULL)
12906 12908 qreply(q, mp);
12907 12909 return;
12908 12910 }
12909 12911
12910 12912 if (!snmpcom_req(q, mp, ip_snmp_set, ip_snmp_get, cr)) {
12911 12913 proto_str = "Bad SNMPCOM request?";
12912 12914 goto protonak;
12913 12915 }
12914 12916 return;
12915 12917 default:
12916 12918 ip1dbg(("ip_wput_nondata: dropping M_PROTO prim %u\n",
12917 12919 (int)*(uint_t *)mp->b_rptr));
12918 12920 freemsg(mp);
12919 12921 return;
12920 12922 }
12921 12923 default:
12922 12924 break;
12923 12925 }
12924 12926 if (q->q_next) {
12925 12927 putnext(q, mp);
12926 12928 } else
12927 12929 freemsg(mp);
12928 12930 return;
12929 12931
12930 12932 nak:
12931 12933 iocp->ioc_error = EINVAL;
12932 12934 mp->b_datap->db_type = M_IOCNAK;
12933 12935 iocp->ioc_count = 0;
12934 12936 qreply(q, mp);
12935 12937 return;
12936 12938
12937 12939 protonak:
12938 12940 cmn_err(CE_NOTE, "IP doesn't process %s as a module", proto_str);
12939 12941 if ((mp = mi_tpi_err_ack_alloc(mp, TPROTO, EINVAL)) != NULL)
12940 12942 qreply(q, mp);
12941 12943 }
12942 12944
12943 12945 /*
12944 12946 * Process IP options in an outbound packet. Verify that the nexthop in a
12945 12947 * strict source route is onlink.
12946 12948 * Returns non-zero if something fails in which case an ICMP error has been
12947 12949 * sent and mp freed.
12948 12950 *
12949 12951 * Assumes the ULP has called ip_massage_options to move nexthop into ipha_dst.
12950 12952 */
12951 12953 int
12952 12954 ip_output_options(mblk_t *mp, ipha_t *ipha, ip_xmit_attr_t *ixa, ill_t *ill)
12953 12955 {
12954 12956 ipoptp_t opts;
12955 12957 uchar_t *opt;
12956 12958 uint8_t optval;
12957 12959 uint8_t optlen;
12958 12960 ipaddr_t dst;
12959 12961 intptr_t code = 0;
12960 12962 ire_t *ire;
12961 12963 ip_stack_t *ipst = ixa->ixa_ipst;
12962 12964 ip_recv_attr_t iras;
12963 12965
12964 12966 ip2dbg(("ip_output_options\n"));
12965 12967
12966 12968 dst = ipha->ipha_dst;
12967 12969 for (optval = ipoptp_first(&opts, ipha);
12968 12970 optval != IPOPT_EOL;
12969 12971 optval = ipoptp_next(&opts)) {
12970 12972 opt = opts.ipoptp_cur;
12971 12973 optlen = opts.ipoptp_len;
12972 12974 ip2dbg(("ip_output_options: opt %d, len %d\n",
12973 12975 optval, optlen));
12974 12976 switch (optval) {
12975 12977 uint32_t off;
12976 12978 case IPOPT_SSRR:
12977 12979 case IPOPT_LSRR:
12978 12980 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
12979 12981 ip1dbg((
12980 12982 "ip_output_options: bad option offset\n"));
12981 12983 code = (char *)&opt[IPOPT_OLEN] -
12982 12984 (char *)ipha;
12983 12985 goto param_prob;
12984 12986 }
12985 12987 off = opt[IPOPT_OFFSET];
12986 12988 ip1dbg(("ip_output_options: next hop 0x%x\n",
12987 12989 ntohl(dst)));
12988 12990 /*
12989 12991 * For strict: verify that dst is directly
12990 12992 * reachable.
12991 12993 */
12992 12994 if (optval == IPOPT_SSRR) {
12993 12995 ire = ire_ftable_lookup_v4(dst, 0, 0,
12994 12996 IRE_INTERFACE, NULL, ALL_ZONES,
12995 12997 ixa->ixa_tsl,
12996 12998 MATCH_IRE_TYPE | MATCH_IRE_SECATTR, 0, ipst,
12997 12999 NULL);
12998 13000 if (ire == NULL) {
12999 13001 ip1dbg(("ip_output_options: SSRR not"
13000 13002 " directly reachable: 0x%x\n",
13001 13003 ntohl(dst)));
13002 13004 goto bad_src_route;
13003 13005 }
13004 13006 ire_refrele(ire);
13005 13007 }
13006 13008 break;
13007 13009 case IPOPT_RR:
13008 13010 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
13009 13011 ip1dbg((
13010 13012 "ip_output_options: bad option offset\n"));
13011 13013 code = (char *)&opt[IPOPT_OLEN] -
13012 13014 (char *)ipha;
13013 13015 goto param_prob;
13014 13016 }
13015 13017 break;
13016 13018 case IPOPT_TS:
13017 13019 /*
13018 13020 * Verify that length >=5 and that there is either
13019 13021 * room for another timestamp or that the overflow
13020 13022 * counter is not maxed out.
13021 13023 */
13022 13024 code = (char *)&opt[IPOPT_OLEN] - (char *)ipha;
13023 13025 if (optlen < IPOPT_MINLEN_IT) {
13024 13026 goto param_prob;
13025 13027 }
13026 13028 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
13027 13029 ip1dbg((
13028 13030 "ip_output_options: bad option offset\n"));
13029 13031 code = (char *)&opt[IPOPT_OFFSET] -
13030 13032 (char *)ipha;
13031 13033 goto param_prob;
13032 13034 }
13033 13035 switch (opt[IPOPT_POS_OV_FLG] & 0x0F) {
13034 13036 case IPOPT_TS_TSONLY:
13035 13037 off = IPOPT_TS_TIMELEN;
13036 13038 break;
13037 13039 case IPOPT_TS_TSANDADDR:
13038 13040 case IPOPT_TS_PRESPEC:
13039 13041 case IPOPT_TS_PRESPEC_RFC791:
13040 13042 off = IP_ADDR_LEN + IPOPT_TS_TIMELEN;
13041 13043 break;
13042 13044 default:
13043 13045 code = (char *)&opt[IPOPT_POS_OV_FLG] -
13044 13046 (char *)ipha;
13045 13047 goto param_prob;
13046 13048 }
13047 13049 if (opt[IPOPT_OFFSET] - 1 + off > optlen &&
13048 13050 (opt[IPOPT_POS_OV_FLG] & 0xF0) == 0xF0) {
13049 13051 /*
13050 13052 * No room and the overflow counter is 15
13051 13053 * already.
13052 13054 */
13053 13055 goto param_prob;
13054 13056 }
13055 13057 break;
13056 13058 }
13057 13059 }
13058 13060
13059 13061 if ((opts.ipoptp_flags & IPOPTP_ERROR) == 0)
13060 13062 return (0);
13061 13063
13062 13064 ip1dbg(("ip_output_options: error processing IP options."));
13063 13065 code = (char *)&opt[IPOPT_OFFSET] - (char *)ipha;
13064 13066
13065 13067 param_prob:
13066 13068 bzero(&iras, sizeof (iras));
13067 13069 iras.ira_ill = iras.ira_rill = ill;
13068 13070 iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
13069 13071 iras.ira_rifindex = iras.ira_ruifindex;
13070 13072 iras.ira_flags = IRAF_IS_IPV4;
13071 13073
13072 13074 ip_drop_output("ip_output_options", mp, ill);
13073 13075 icmp_param_problem(mp, (uint8_t)code, &iras);
13074 13076 ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
13075 13077 return (-1);
13076 13078
13077 13079 bad_src_route:
13078 13080 bzero(&iras, sizeof (iras));
13079 13081 iras.ira_ill = iras.ira_rill = ill;
13080 13082 iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
13081 13083 iras.ira_rifindex = iras.ira_ruifindex;
13082 13084 iras.ira_flags = IRAF_IS_IPV4;
13083 13085
13084 13086 ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ill);
13085 13087 icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, &iras);
13086 13088 ASSERT(!(iras.ira_flags & IRAF_IPSEC_SECURE));
13087 13089 return (-1);
13088 13090 }
13089 13091
13090 13092 /*
13091 13093 * The maximum value of conn_drain_list_cnt is CONN_MAXDRAINCNT.
13092 13094 * conn_drain_list_cnt can be changed by setting conn_drain_nthreads
13093 13095 * thru /etc/system.
13094 13096 */
13095 13097 #define CONN_MAXDRAINCNT 64
13096 13098
13097 13099 static void
13098 13100 conn_drain_init(ip_stack_t *ipst)
13099 13101 {
13100 13102 int i, j;
13101 13103 idl_tx_list_t *itl_tx;
13102 13104
13103 13105 ipst->ips_conn_drain_list_cnt = conn_drain_nthreads;
13104 13106
13105 13107 if ((ipst->ips_conn_drain_list_cnt == 0) ||
13106 13108 (ipst->ips_conn_drain_list_cnt > CONN_MAXDRAINCNT)) {
13107 13109 /*
13108 13110 * Default value of the number of drainers is the
13109 13111 * number of cpus, subject to maximum of 8 drainers.
13110 13112 */
13111 13113 if (boot_max_ncpus != -1)
13112 13114 ipst->ips_conn_drain_list_cnt = MIN(boot_max_ncpus, 8);
13113 13115 else
13114 13116 ipst->ips_conn_drain_list_cnt = MIN(max_ncpus, 8);
13115 13117 }
13116 13118
13117 13119 ipst->ips_idl_tx_list =
13118 13120 kmem_zalloc(TX_FANOUT_SIZE * sizeof (idl_tx_list_t), KM_SLEEP);
13119 13121 for (i = 0; i < TX_FANOUT_SIZE; i++) {
13120 13122 itl_tx = &ipst->ips_idl_tx_list[i];
13121 13123 itl_tx->txl_drain_list =
13122 13124 kmem_zalloc(ipst->ips_conn_drain_list_cnt *
13123 13125 sizeof (idl_t), KM_SLEEP);
13124 13126 mutex_init(&itl_tx->txl_lock, NULL, MUTEX_DEFAULT, NULL);
13125 13127 for (j = 0; j < ipst->ips_conn_drain_list_cnt; j++) {
13126 13128 mutex_init(&itl_tx->txl_drain_list[j].idl_lock, NULL,
13127 13129 MUTEX_DEFAULT, NULL);
13128 13130 itl_tx->txl_drain_list[j].idl_itl = itl_tx;
13129 13131 }
13130 13132 }
13131 13133 }
13132 13134
13133 13135 static void
13134 13136 conn_drain_fini(ip_stack_t *ipst)
13135 13137 {
13136 13138 int i;
13137 13139 idl_tx_list_t *itl_tx;
13138 13140
13139 13141 for (i = 0; i < TX_FANOUT_SIZE; i++) {
13140 13142 itl_tx = &ipst->ips_idl_tx_list[i];
13141 13143 kmem_free(itl_tx->txl_drain_list,
13142 13144 ipst->ips_conn_drain_list_cnt * sizeof (idl_t));
13143 13145 }
13144 13146 kmem_free(ipst->ips_idl_tx_list,
13145 13147 TX_FANOUT_SIZE * sizeof (idl_tx_list_t));
13146 13148 ipst->ips_idl_tx_list = NULL;
13147 13149 }
13148 13150
13149 13151 /*
13150 13152 * Flow control has blocked us from proceeding. Insert the given conn in one
13151 13153 * of the conn drain lists. When flow control is unblocked, either ip_wsrv()
13152 13154 * (STREAMS) or ill_flow_enable() (direct) will be called back, which in turn
13153 13155 * will call conn_walk_drain(). See the flow control notes at the top of this
13154 13156 * file for more details.
13155 13157 */
13156 13158 void
13157 13159 conn_drain_insert(conn_t *connp, idl_tx_list_t *tx_list)
13158 13160 {
13159 13161 idl_t *idl = tx_list->txl_drain_list;
13160 13162 uint_t index;
13161 13163 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
13162 13164
13163 13165 mutex_enter(&connp->conn_lock);
13164 13166 if (connp->conn_state_flags & CONN_CLOSING) {
13165 13167 /*
13166 13168 * The conn is closing as a result of which CONN_CLOSING
13167 13169 * is set. Return.
13168 13170 */
13169 13171 mutex_exit(&connp->conn_lock);
13170 13172 return;
13171 13173 } else if (connp->conn_idl == NULL) {
13172 13174 /*
13173 13175 * Assign the next drain list round robin. We dont' use
13174 13176 * a lock, and thus it may not be strictly round robin.
13175 13177 * Atomicity of load/stores is enough to make sure that
13176 13178 * conn_drain_list_index is always within bounds.
13177 13179 */
13178 13180 index = tx_list->txl_drain_index;
13179 13181 ASSERT(index < ipst->ips_conn_drain_list_cnt);
13180 13182 connp->conn_idl = &tx_list->txl_drain_list[index];
13181 13183 index++;
13182 13184 if (index == ipst->ips_conn_drain_list_cnt)
13183 13185 index = 0;
13184 13186 tx_list->txl_drain_index = index;
13185 13187 } else {
13186 13188 ASSERT(connp->conn_idl->idl_itl == tx_list);
13187 13189 }
13188 13190 mutex_exit(&connp->conn_lock);
13189 13191
13190 13192 idl = connp->conn_idl;
13191 13193 mutex_enter(&idl->idl_lock);
13192 13194 if ((connp->conn_drain_prev != NULL) ||
13193 13195 (connp->conn_state_flags & CONN_CLOSING)) {
13194 13196 /*
13195 13197 * The conn is either already in the drain list or closing.
13196 13198 * (We needed to check for CONN_CLOSING again since close can
13197 13199 * sneak in between dropping conn_lock and acquiring idl_lock.)
13198 13200 */
13199 13201 mutex_exit(&idl->idl_lock);
13200 13202 return;
13201 13203 }
13202 13204
13203 13205 /*
13204 13206 * The conn is not in the drain list. Insert it at the
13205 13207 * tail of the drain list. The drain list is circular
13206 13208 * and doubly linked. idl_conn points to the 1st element
13207 13209 * in the list.
13208 13210 */
13209 13211 if (idl->idl_conn == NULL) {
13210 13212 idl->idl_conn = connp;
13211 13213 connp->conn_drain_next = connp;
13212 13214 connp->conn_drain_prev = connp;
13213 13215 } else {
13214 13216 conn_t *head = idl->idl_conn;
13215 13217
13216 13218 connp->conn_drain_next = head;
13217 13219 connp->conn_drain_prev = head->conn_drain_prev;
13218 13220 head->conn_drain_prev->conn_drain_next = connp;
13219 13221 head->conn_drain_prev = connp;
13220 13222 }
13221 13223 /*
13222 13224 * For non streams based sockets assert flow control.
13223 13225 */
13224 13226 conn_setqfull(connp, NULL);
13225 13227 mutex_exit(&idl->idl_lock);
13226 13228 }
13227 13229
13228 13230 static void
13229 13231 conn_drain_remove(conn_t *connp)
13230 13232 {
13231 13233 idl_t *idl = connp->conn_idl;
13232 13234
13233 13235 if (idl != NULL) {
13234 13236 /*
13235 13237 * Remove ourself from the drain list.
13236 13238 */
13237 13239 if (connp->conn_drain_next == connp) {
13238 13240 /* Singleton in the list */
13239 13241 ASSERT(connp->conn_drain_prev == connp);
13240 13242 idl->idl_conn = NULL;
13241 13243 } else {
13242 13244 connp->conn_drain_prev->conn_drain_next =
13243 13245 connp->conn_drain_next;
13244 13246 connp->conn_drain_next->conn_drain_prev =
13245 13247 connp->conn_drain_prev;
13246 13248 if (idl->idl_conn == connp)
13247 13249 idl->idl_conn = connp->conn_drain_next;
13248 13250 }
13249 13251
13250 13252 /*
13251 13253 * NOTE: because conn_idl is associated with a specific drain
13252 13254 * list which in turn is tied to the index the TX ring
13253 13255 * (txl_cookie) hashes to, and because the TX ring can change
13254 13256 * over the lifetime of the conn_t, we must clear conn_idl so
13255 13257 * a subsequent conn_drain_insert() will set conn_idl again
13256 13258 * based on the latest txl_cookie.
13257 13259 */
13258 13260 connp->conn_idl = NULL;
13259 13261 }
13260 13262 connp->conn_drain_next = NULL;
13261 13263 connp->conn_drain_prev = NULL;
13262 13264
13263 13265 conn_clrqfull(connp, NULL);
13264 13266 /*
13265 13267 * For streams based sockets open up flow control.
13266 13268 */
13267 13269 if (!IPCL_IS_NONSTR(connp))
13268 13270 enableok(connp->conn_wq);
13269 13271 }
13270 13272
13271 13273 /*
13272 13274 * This conn is closing, and we are called from ip_close. OR
13273 13275 * this conn is draining because flow-control on the ill has been relieved.
13274 13276 *
13275 13277 * We must also need to remove conn's on this idl from the list, and also
13276 13278 * inform the sockfs upcalls about the change in flow-control.
13277 13279 */
13278 13280 static void
13279 13281 conn_drain(conn_t *connp, boolean_t closing)
13280 13282 {
13281 13283 idl_t *idl;
13282 13284 conn_t *next_connp;
13283 13285
13284 13286 /*
13285 13287 * connp->conn_idl is stable at this point, and no lock is needed
13286 13288 * to check it. If we are called from ip_close, close has already
13287 13289 * set CONN_CLOSING, thus freezing the value of conn_idl, and
13288 13290 * called us only because conn_idl is non-null. If we are called thru
13289 13291 * service, conn_idl could be null, but it cannot change because
13290 13292 * service is single-threaded per queue, and there cannot be another
13291 13293 * instance of service trying to call conn_drain_insert on this conn
13292 13294 * now.
13293 13295 */
13294 13296 ASSERT(!closing || connp == NULL || connp->conn_idl != NULL);
13295 13297
13296 13298 /*
13297 13299 * If the conn doesn't exist or is not on a drain list, bail.
13298 13300 */
13299 13301 if (connp == NULL || connp->conn_idl == NULL ||
13300 13302 connp->conn_drain_prev == NULL) {
13301 13303 return;
13302 13304 }
13303 13305
13304 13306 idl = connp->conn_idl;
13305 13307 ASSERT(MUTEX_HELD(&idl->idl_lock));
13306 13308
13307 13309 if (!closing) {
13308 13310 next_connp = connp->conn_drain_next;
13309 13311 while (next_connp != connp) {
13310 13312 conn_t *delconnp = next_connp;
13311 13313
13312 13314 next_connp = next_connp->conn_drain_next;
13313 13315 conn_drain_remove(delconnp);
13314 13316 }
13315 13317 ASSERT(connp->conn_drain_next == idl->idl_conn);
13316 13318 }
13317 13319 conn_drain_remove(connp);
13318 13320 }
13319 13321
13320 13322 /*
13321 13323 * Write service routine. Shared perimeter entry point.
13322 13324 * The device queue's messages has fallen below the low water mark and STREAMS
13323 13325 * has backenabled the ill_wq. Send sockfs notification about flow-control on
13324 13326 * each waiting conn.
13325 13327 */
13326 13328 void
13327 13329 ip_wsrv(queue_t *q)
13328 13330 {
13329 13331 ill_t *ill;
13330 13332
13331 13333 ill = (ill_t *)q->q_ptr;
13332 13334 if (ill->ill_state_flags == 0) {
13333 13335 ip_stack_t *ipst = ill->ill_ipst;
13334 13336
13335 13337 /*
13336 13338 * The device flow control has opened up.
13337 13339 * Walk through conn drain lists and qenable the
13338 13340 * first conn in each list. This makes sense only
13339 13341 * if the stream is fully plumbed and setup.
13340 13342 * Hence the ill_state_flags check above.
13341 13343 */
13342 13344 ip1dbg(("ip_wsrv: walking\n"));
13343 13345 conn_walk_drain(ipst, &ipst->ips_idl_tx_list[0]);
13344 13346 enableok(ill->ill_wq);
13345 13347 }
13346 13348 }
13347 13349
13348 13350 /*
13349 13351 * Callback to disable flow control in IP.
13350 13352 *
13351 13353 * This is a mac client callback added when the DLD_CAPAB_DIRECT capability
13352 13354 * is enabled.
13353 13355 *
13354 13356 * When MAC_TX() is not able to send any more packets, dld sets its queue
13355 13357 * to QFULL and enable the STREAMS flow control. Later, when the underlying
13356 13358 * driver is able to continue to send packets, it calls mac_tx_(ring_)update()
13357 13359 * function and wakes up corresponding mac worker threads, which in turn
13358 13360 * calls this callback function, and disables flow control.
13359 13361 */
13360 13362 void
13361 13363 ill_flow_enable(void *arg, ip_mac_tx_cookie_t cookie)
13362 13364 {
13363 13365 ill_t *ill = (ill_t *)arg;
13364 13366 ip_stack_t *ipst = ill->ill_ipst;
13365 13367 idl_tx_list_t *idl_txl;
13366 13368
13367 13369 idl_txl = &ipst->ips_idl_tx_list[IDLHASHINDEX(cookie)];
13368 13370 mutex_enter(&idl_txl->txl_lock);
13369 13371 /* add code to to set a flag to indicate idl_txl is enabled */
13370 13372 conn_walk_drain(ipst, idl_txl);
13371 13373 mutex_exit(&idl_txl->txl_lock);
13372 13374 }
13373 13375
13374 13376 /*
13375 13377 * Flow control has been relieved and STREAMS has backenabled us; drain
13376 13378 * all the conn lists on `tx_list'.
13377 13379 */
13378 13380 static void
13379 13381 conn_walk_drain(ip_stack_t *ipst, idl_tx_list_t *tx_list)
13380 13382 {
13381 13383 int i;
13382 13384 idl_t *idl;
13383 13385
13384 13386 IP_STAT(ipst, ip_conn_walk_drain);
13385 13387
13386 13388 for (i = 0; i < ipst->ips_conn_drain_list_cnt; i++) {
13387 13389 idl = &tx_list->txl_drain_list[i];
13388 13390 mutex_enter(&idl->idl_lock);
13389 13391 conn_drain(idl->idl_conn, B_FALSE);
13390 13392 mutex_exit(&idl->idl_lock);
13391 13393 }
13392 13394 }
13393 13395
13394 13396 /*
13395 13397 * Determine if the ill and multicast aspects of that packets
13396 13398 * "matches" the conn.
13397 13399 */
13398 13400 boolean_t
13399 13401 conn_wantpacket(conn_t *connp, ip_recv_attr_t *ira, ipha_t *ipha)
13400 13402 {
13401 13403 ill_t *ill = ira->ira_rill;
13402 13404 zoneid_t zoneid = ira->ira_zoneid;
13403 13405 uint_t in_ifindex;
13404 13406 ipaddr_t dst, src;
13405 13407
13406 13408 dst = ipha->ipha_dst;
13407 13409 src = ipha->ipha_src;
13408 13410
13409 13411 /*
13410 13412 * conn_incoming_ifindex is set by IP_BOUND_IF which limits
13411 13413 * unicast, broadcast and multicast reception to
13412 13414 * conn_incoming_ifindex.
13413 13415 * conn_wantpacket is called for unicast, broadcast and
13414 13416 * multicast packets.
13415 13417 */
13416 13418 in_ifindex = connp->conn_incoming_ifindex;
13417 13419
13418 13420 /* mpathd can bind to the under IPMP interface, which we allow */
13419 13421 if (in_ifindex != 0 && in_ifindex != ill->ill_phyint->phyint_ifindex) {
13420 13422 if (!IS_UNDER_IPMP(ill))
13421 13423 return (B_FALSE);
13422 13424
13423 13425 if (in_ifindex != ipmp_ill_get_ipmp_ifindex(ill))
13424 13426 return (B_FALSE);
13425 13427 }
13426 13428
13427 13429 if (!IPCL_ZONE_MATCH(connp, zoneid))
13428 13430 return (B_FALSE);
13429 13431
13430 13432 if (!(ira->ira_flags & IRAF_MULTICAST))
13431 13433 return (B_TRUE);
13432 13434
13433 13435 if (connp->conn_multi_router) {
13434 13436 /* multicast packet and multicast router socket: send up */
13435 13437 return (B_TRUE);
13436 13438 }
13437 13439
13438 13440 if (ipha->ipha_protocol == IPPROTO_PIM ||
13439 13441 ipha->ipha_protocol == IPPROTO_RSVP)
13440 13442 return (B_TRUE);
13441 13443
13442 13444 return (conn_hasmembers_ill_withsrc_v4(connp, dst, src, ira->ira_ill));
13443 13445 }
13444 13446
13445 13447 void
13446 13448 conn_setqfull(conn_t *connp, boolean_t *flow_stopped)
13447 13449 {
13448 13450 if (IPCL_IS_NONSTR(connp)) {
13449 13451 (*connp->conn_upcalls->su_txq_full)
13450 13452 (connp->conn_upper_handle, B_TRUE);
13451 13453 if (flow_stopped != NULL)
13452 13454 *flow_stopped = B_TRUE;
13453 13455 } else {
13454 13456 queue_t *q = connp->conn_wq;
13455 13457
13456 13458 ASSERT(q != NULL);
13457 13459 if (!(q->q_flag & QFULL)) {
13458 13460 mutex_enter(QLOCK(q));
13459 13461 if (!(q->q_flag & QFULL)) {
13460 13462 /* still need to set QFULL */
13461 13463 q->q_flag |= QFULL;
13462 13464 /* set flow_stopped to true under QLOCK */
13463 13465 if (flow_stopped != NULL)
13464 13466 *flow_stopped = B_TRUE;
13465 13467 mutex_exit(QLOCK(q));
13466 13468 } else {
13467 13469 /* flow_stopped is left unchanged */
13468 13470 mutex_exit(QLOCK(q));
13469 13471 }
13470 13472 }
13471 13473 }
13472 13474 }
13473 13475
13474 13476 void
13475 13477 conn_clrqfull(conn_t *connp, boolean_t *flow_stopped)
13476 13478 {
13477 13479 if (IPCL_IS_NONSTR(connp)) {
13478 13480 (*connp->conn_upcalls->su_txq_full)
13479 13481 (connp->conn_upper_handle, B_FALSE);
13480 13482 if (flow_stopped != NULL)
13481 13483 *flow_stopped = B_FALSE;
13482 13484 } else {
13483 13485 queue_t *q = connp->conn_wq;
13484 13486
13485 13487 ASSERT(q != NULL);
13486 13488 if (q->q_flag & QFULL) {
13487 13489 mutex_enter(QLOCK(q));
13488 13490 if (q->q_flag & QFULL) {
13489 13491 q->q_flag &= ~QFULL;
13490 13492 /* set flow_stopped to false under QLOCK */
13491 13493 if (flow_stopped != NULL)
13492 13494 *flow_stopped = B_FALSE;
13493 13495 mutex_exit(QLOCK(q));
13494 13496 if (q->q_flag & QWANTW)
13495 13497 qbackenable(q, 0);
13496 13498 } else {
13497 13499 /* flow_stopped is left unchanged */
13498 13500 mutex_exit(QLOCK(q));
13499 13501 }
13500 13502 }
13501 13503 }
13502 13504
13503 13505 mutex_enter(&connp->conn_lock);
13504 13506 connp->conn_blocked = B_FALSE;
13505 13507 mutex_exit(&connp->conn_lock);
13506 13508 }
13507 13509
13508 13510 /*
13509 13511 * Return the length in bytes of the IPv4 headers (base header, label, and
13510 13512 * other IP options) that will be needed based on the
13511 13513 * ip_pkt_t structure passed by the caller.
13512 13514 *
13513 13515 * The returned length does not include the length of the upper level
13514 13516 * protocol (ULP) header.
13515 13517 * The caller needs to check that the length doesn't exceed the max for IPv4.
13516 13518 */
13517 13519 int
13518 13520 ip_total_hdrs_len_v4(const ip_pkt_t *ipp)
13519 13521 {
13520 13522 int len;
13521 13523
13522 13524 len = IP_SIMPLE_HDR_LENGTH;
13523 13525 if (ipp->ipp_fields & IPPF_LABEL_V4) {
13524 13526 ASSERT(ipp->ipp_label_len_v4 != 0);
13525 13527 /* We need to round up here */
13526 13528 len += (ipp->ipp_label_len_v4 + 3) & ~3;
13527 13529 }
13528 13530
13529 13531 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
13530 13532 ASSERT(ipp->ipp_ipv4_options_len != 0);
13531 13533 ASSERT((ipp->ipp_ipv4_options_len & 3) == 0);
13532 13534 len += ipp->ipp_ipv4_options_len;
13533 13535 }
13534 13536 return (len);
13535 13537 }
13536 13538
13537 13539 /*
13538 13540 * All-purpose routine to build an IPv4 header with options based
13539 13541 * on the abstract ip_pkt_t.
13540 13542 *
13541 13543 * The caller has to set the source and destination address as well as
13542 13544 * ipha_length. The caller has to massage any source route and compensate
13543 13545 * for the ULP pseudo-header checksum due to the source route.
13544 13546 */
13545 13547 void
13546 13548 ip_build_hdrs_v4(uchar_t *buf, uint_t buf_len, const ip_pkt_t *ipp,
13547 13549 uint8_t protocol)
13548 13550 {
13549 13551 ipha_t *ipha = (ipha_t *)buf;
13550 13552 uint8_t *cp;
13551 13553
13552 13554 /* Initialize IPv4 header */
13553 13555 ipha->ipha_type_of_service = ipp->ipp_type_of_service;
13554 13556 ipha->ipha_length = 0; /* Caller will set later */
13555 13557 ipha->ipha_ident = 0;
13556 13558 ipha->ipha_fragment_offset_and_flags = 0;
13557 13559 ipha->ipha_ttl = ipp->ipp_unicast_hops;
13558 13560 ipha->ipha_protocol = protocol;
13559 13561 ipha->ipha_hdr_checksum = 0;
13560 13562
13561 13563 if ((ipp->ipp_fields & IPPF_ADDR) &&
13562 13564 IN6_IS_ADDR_V4MAPPED(&ipp->ipp_addr))
13563 13565 ipha->ipha_src = ipp->ipp_addr_v4;
13564 13566
13565 13567 cp = (uint8_t *)&ipha[1];
13566 13568 if (ipp->ipp_fields & IPPF_LABEL_V4) {
13567 13569 ASSERT(ipp->ipp_label_len_v4 != 0);
13568 13570 bcopy(ipp->ipp_label_v4, cp, ipp->ipp_label_len_v4);
13569 13571 cp += ipp->ipp_label_len_v4;
13570 13572 /* We need to round up here */
13571 13573 while ((uintptr_t)cp & 0x3) {
13572 13574 *cp++ = IPOPT_NOP;
13573 13575 }
13574 13576 }
13575 13577
13576 13578 if (ipp->ipp_fields & IPPF_IPV4_OPTIONS) {
13577 13579 ASSERT(ipp->ipp_ipv4_options_len != 0);
13578 13580 ASSERT((ipp->ipp_ipv4_options_len & 3) == 0);
13579 13581 bcopy(ipp->ipp_ipv4_options, cp, ipp->ipp_ipv4_options_len);
13580 13582 cp += ipp->ipp_ipv4_options_len;
13581 13583 }
13582 13584 ipha->ipha_version_and_hdr_length =
13583 13585 (uint8_t)((IP_VERSION << 4) + buf_len / 4);
13584 13586
13585 13587 ASSERT((int)(cp - buf) == buf_len);
13586 13588 }
13587 13589
13588 13590 /* Allocate the private structure */
13589 13591 static int
13590 13592 ip_priv_alloc(void **bufp)
13591 13593 {
13592 13594 void *buf;
13593 13595
13594 13596 if ((buf = kmem_alloc(sizeof (ip_priv_t), KM_NOSLEEP)) == NULL)
13595 13597 return (ENOMEM);
13596 13598
13597 13599 *bufp = buf;
13598 13600 return (0);
13599 13601 }
13600 13602
13601 13603 /* Function to delete the private structure */
13602 13604 void
13603 13605 ip_priv_free(void *buf)
13604 13606 {
13605 13607 ASSERT(buf != NULL);
13606 13608 kmem_free(buf, sizeof (ip_priv_t));
13607 13609 }
13608 13610
13609 13611 /*
13610 13612 * The entry point for IPPF processing.
13611 13613 * If the classifier (IPGPC_CLASSIFY) is not loaded and configured, the
13612 13614 * routine just returns.
13613 13615 *
13614 13616 * When called, ip_process generates an ipp_packet_t structure
13615 13617 * which holds the state information for this packet and invokes the
13616 13618 * the classifier (via ipp_packet_process). The classification, depending on
13617 13619 * configured filters, results in a list of actions for this packet. Invoking
13618 13620 * an action may cause the packet to be dropped, in which case we return NULL.
13619 13621 * proc indicates the callout position for
13620 13622 * this packet and ill is the interface this packet arrived on or will leave
13621 13623 * on (inbound and outbound resp.).
13622 13624 *
13623 13625 * We do the processing on the rill (mapped to the upper if ipmp), but MIB
13624 13626 * on the ill corrsponding to the destination IP address.
13625 13627 */
13626 13628 mblk_t *
13627 13629 ip_process(ip_proc_t proc, mblk_t *mp, ill_t *rill, ill_t *ill)
13628 13630 {
13629 13631 ip_priv_t *priv;
13630 13632 ipp_action_id_t aid;
13631 13633 int rc = 0;
13632 13634 ipp_packet_t *pp;
13633 13635
13634 13636 /* If the classifier is not loaded, return */
13635 13637 if ((aid = ipp_action_lookup(IPGPC_CLASSIFY)) == IPP_ACTION_INVAL) {
13636 13638 return (mp);
13637 13639 }
13638 13640
13639 13641 ASSERT(mp != NULL);
13640 13642
13641 13643 /* Allocate the packet structure */
13642 13644 rc = ipp_packet_alloc(&pp, "ip", aid);
13643 13645 if (rc != 0)
13644 13646 goto drop;
13645 13647
13646 13648 /* Allocate the private structure */
13647 13649 rc = ip_priv_alloc((void **)&priv);
13648 13650 if (rc != 0) {
13649 13651 ipp_packet_free(pp);
13650 13652 goto drop;
13651 13653 }
13652 13654 priv->proc = proc;
13653 13655 priv->ill_index = ill_get_upper_ifindex(rill);
13654 13656
13655 13657 ipp_packet_set_private(pp, priv, ip_priv_free);
13656 13658 ipp_packet_set_data(pp, mp);
13657 13659
13658 13660 /* Invoke the classifier */
13659 13661 rc = ipp_packet_process(&pp);
13660 13662 if (pp != NULL) {
13661 13663 mp = ipp_packet_get_data(pp);
13662 13664 ipp_packet_free(pp);
13663 13665 if (rc != 0)
13664 13666 goto drop;
13665 13667 return (mp);
13666 13668 } else {
13667 13669 /* No mp to trace in ip_drop_input/ip_drop_output */
13668 13670 mp = NULL;
13669 13671 }
13670 13672 drop:
13671 13673 if (proc == IPP_LOCAL_IN || proc == IPP_FWD_IN) {
13672 13674 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
13673 13675 ip_drop_input("ip_process", mp, ill);
13674 13676 } else {
13675 13677 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
13676 13678 ip_drop_output("ip_process", mp, ill);
13677 13679 }
13678 13680 freemsg(mp);
13679 13681 return (NULL);
13680 13682 }
13681 13683
13682 13684 /*
13683 13685 * Propagate a multicast group membership operation (add/drop) on
13684 13686 * all the interfaces crossed by the related multirt routes.
13685 13687 * The call is considered successful if the operation succeeds
13686 13688 * on at least one interface.
13687 13689 *
13688 13690 * This assumes that a set of IRE_HOST/RTF_MULTIRT has been created for the
13689 13691 * multicast addresses with the ire argument being the first one.
13690 13692 * We walk the bucket to find all the of those.
13691 13693 *
13692 13694 * Common to IPv4 and IPv6.
13693 13695 */
13694 13696 static int
13695 13697 ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t,
13696 13698 const in6_addr_t *, ipaddr_t, uint_t, mcast_record_t, const in6_addr_t *),
13697 13699 ire_t *ire, conn_t *connp, boolean_t checkonly, const in6_addr_t *v6group,
13698 13700 mcast_record_t fmode, const in6_addr_t *v6src)
13699 13701 {
13700 13702 ire_t *ire_gw;
13701 13703 irb_t *irb;
13702 13704 int ifindex;
13703 13705 int error = 0;
13704 13706 int result;
13705 13707 ip_stack_t *ipst = ire->ire_ipst;
13706 13708 ipaddr_t group;
13707 13709 boolean_t isv6;
13708 13710 int match_flags;
13709 13711
13710 13712 if (IN6_IS_ADDR_V4MAPPED(v6group)) {
13711 13713 IN6_V4MAPPED_TO_IPADDR(v6group, group);
13712 13714 isv6 = B_FALSE;
13713 13715 } else {
13714 13716 isv6 = B_TRUE;
13715 13717 }
13716 13718
13717 13719 irb = ire->ire_bucket;
13718 13720 ASSERT(irb != NULL);
13719 13721
13720 13722 result = 0;
13721 13723 irb_refhold(irb);
13722 13724 for (; ire != NULL; ire = ire->ire_next) {
13723 13725 if ((ire->ire_flags & RTF_MULTIRT) == 0)
13724 13726 continue;
13725 13727
13726 13728 /* We handle -ifp routes by matching on the ill if set */
13727 13729 match_flags = MATCH_IRE_TYPE;
13728 13730 if (ire->ire_ill != NULL)
13729 13731 match_flags |= MATCH_IRE_ILL;
13730 13732
13731 13733 if (isv6) {
13732 13734 if (!IN6_ARE_ADDR_EQUAL(&ire->ire_addr_v6, v6group))
13733 13735 continue;
13734 13736
13735 13737 ire_gw = ire_ftable_lookup_v6(&ire->ire_gateway_addr_v6,
13736 13738 0, 0, IRE_INTERFACE, ire->ire_ill, ALL_ZONES, NULL,
13737 13739 match_flags, 0, ipst, NULL);
13738 13740 } else {
13739 13741 if (ire->ire_addr != group)
13740 13742 continue;
13741 13743
13742 13744 ire_gw = ire_ftable_lookup_v4(ire->ire_gateway_addr,
13743 13745 0, 0, IRE_INTERFACE, ire->ire_ill, ALL_ZONES, NULL,
13744 13746 match_flags, 0, ipst, NULL);
13745 13747 }
13746 13748 /* No interface route exists for the gateway; skip this ire. */
13747 13749 if (ire_gw == NULL)
13748 13750 continue;
13749 13751 if (ire_gw->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
13750 13752 ire_refrele(ire_gw);
13751 13753 continue;
13752 13754 }
13753 13755 ASSERT(ire_gw->ire_ill != NULL); /* IRE_INTERFACE */
13754 13756 ifindex = ire_gw->ire_ill->ill_phyint->phyint_ifindex;
13755 13757
13756 13758 /*
13757 13759 * The operation is considered a success if
13758 13760 * it succeeds at least once on any one interface.
13759 13761 */
13760 13762 error = fn(connp, checkonly, v6group, INADDR_ANY, ifindex,
13761 13763 fmode, v6src);
13762 13764 if (error == 0)
13763 13765 result = CGTP_MCAST_SUCCESS;
13764 13766
13765 13767 ire_refrele(ire_gw);
13766 13768 }
13767 13769 irb_refrele(irb);
13768 13770 /*
13769 13771 * Consider the call as successful if we succeeded on at least
13770 13772 * one interface. Otherwise, return the last encountered error.
13771 13773 */
13772 13774 return (result == CGTP_MCAST_SUCCESS ? 0 : error);
13773 13775 }
13774 13776
13775 13777 /*
13776 13778 * Return the expected CGTP hooks version number.
13777 13779 */
13778 13780 int
13779 13781 ip_cgtp_filter_supported(void)
13780 13782 {
13781 13783 return (ip_cgtp_filter_rev);
13782 13784 }
13783 13785
13784 13786 /*
13785 13787 * CGTP hooks can be registered by invoking this function.
13786 13788 * Checks that the version number matches.
13787 13789 */
13788 13790 int
13789 13791 ip_cgtp_filter_register(netstackid_t stackid, cgtp_filter_ops_t *ops)
13790 13792 {
13791 13793 netstack_t *ns;
13792 13794 ip_stack_t *ipst;
13793 13795
13794 13796 if (ops->cfo_filter_rev != CGTP_FILTER_REV)
13795 13797 return (ENOTSUP);
13796 13798
13797 13799 ns = netstack_find_by_stackid(stackid);
13798 13800 if (ns == NULL)
13799 13801 return (EINVAL);
13800 13802 ipst = ns->netstack_ip;
13801 13803 ASSERT(ipst != NULL);
13802 13804
13803 13805 if (ipst->ips_ip_cgtp_filter_ops != NULL) {
13804 13806 netstack_rele(ns);
13805 13807 return (EALREADY);
13806 13808 }
13807 13809
13808 13810 ipst->ips_ip_cgtp_filter_ops = ops;
13809 13811
13810 13812 ill_set_inputfn_all(ipst);
13811 13813
13812 13814 netstack_rele(ns);
13813 13815 return (0);
13814 13816 }
13815 13817
13816 13818 /*
13817 13819 * CGTP hooks can be unregistered by invoking this function.
13818 13820 * Returns ENXIO if there was no registration.
13819 13821 * Returns EBUSY if the ndd variable has not been turned off.
13820 13822 */
13821 13823 int
13822 13824 ip_cgtp_filter_unregister(netstackid_t stackid)
13823 13825 {
13824 13826 netstack_t *ns;
13825 13827 ip_stack_t *ipst;
13826 13828
13827 13829 ns = netstack_find_by_stackid(stackid);
13828 13830 if (ns == NULL)
13829 13831 return (EINVAL);
13830 13832 ipst = ns->netstack_ip;
13831 13833 ASSERT(ipst != NULL);
13832 13834
13833 13835 if (ipst->ips_ip_cgtp_filter) {
13834 13836 netstack_rele(ns);
13835 13837 return (EBUSY);
13836 13838 }
13837 13839
13838 13840 if (ipst->ips_ip_cgtp_filter_ops == NULL) {
13839 13841 netstack_rele(ns);
13840 13842 return (ENXIO);
13841 13843 }
13842 13844 ipst->ips_ip_cgtp_filter_ops = NULL;
13843 13845
13844 13846 ill_set_inputfn_all(ipst);
13845 13847
13846 13848 netstack_rele(ns);
13847 13849 return (0);
13848 13850 }
13849 13851
13850 13852 /*
13851 13853 * Check whether there is a CGTP filter registration.
13852 13854 * Returns non-zero if there is a registration, otherwise returns zero.
13853 13855 * Note: returns zero if bad stackid.
13854 13856 */
13855 13857 int
13856 13858 ip_cgtp_filter_is_registered(netstackid_t stackid)
13857 13859 {
13858 13860 netstack_t *ns;
13859 13861 ip_stack_t *ipst;
13860 13862 int ret;
13861 13863
13862 13864 ns = netstack_find_by_stackid(stackid);
13863 13865 if (ns == NULL)
13864 13866 return (0);
13865 13867 ipst = ns->netstack_ip;
13866 13868 ASSERT(ipst != NULL);
13867 13869
13868 13870 if (ipst->ips_ip_cgtp_filter_ops != NULL)
13869 13871 ret = 1;
13870 13872 else
13871 13873 ret = 0;
13872 13874
13873 13875 netstack_rele(ns);
13874 13876 return (ret);
13875 13877 }
13876 13878
13877 13879 static int
13878 13880 ip_squeue_switch(int val)
13879 13881 {
13880 13882 int rval;
13881 13883
13882 13884 switch (val) {
13883 13885 case IP_SQUEUE_ENTER_NODRAIN:
13884 13886 rval = SQ_NODRAIN;
13885 13887 break;
13886 13888 case IP_SQUEUE_ENTER:
13887 13889 rval = SQ_PROCESS;
13888 13890 break;
13889 13891 case IP_SQUEUE_FILL:
13890 13892 default:
13891 13893 rval = SQ_FILL;
13892 13894 break;
13893 13895 }
13894 13896 return (rval);
13895 13897 }
13896 13898
13897 13899 static void *
13898 13900 ip_kstat2_init(netstackid_t stackid, ip_stat_t *ip_statisticsp)
13899 13901 {
13900 13902 kstat_t *ksp;
13901 13903
13902 13904 ip_stat_t template = {
13903 13905 { "ip_udp_fannorm", KSTAT_DATA_UINT64 },
13904 13906 { "ip_udp_fanmb", KSTAT_DATA_UINT64 },
13905 13907 { "ip_recv_pullup", KSTAT_DATA_UINT64 },
13906 13908 { "ip_db_ref", KSTAT_DATA_UINT64 },
13907 13909 { "ip_notaligned", KSTAT_DATA_UINT64 },
13908 13910 { "ip_multimblk", KSTAT_DATA_UINT64 },
13909 13911 { "ip_opt", KSTAT_DATA_UINT64 },
13910 13912 { "ipsec_proto_ahesp", KSTAT_DATA_UINT64 },
13911 13913 { "ip_conn_flputbq", KSTAT_DATA_UINT64 },
13912 13914 { "ip_conn_walk_drain", KSTAT_DATA_UINT64 },
13913 13915 { "ip_out_sw_cksum", KSTAT_DATA_UINT64 },
13914 13916 { "ip_out_sw_cksum_bytes", KSTAT_DATA_UINT64 },
13915 13917 { "ip_in_sw_cksum", KSTAT_DATA_UINT64 },
13916 13918 { "ip_ire_reclaim_calls", KSTAT_DATA_UINT64 },
13917 13919 { "ip_ire_reclaim_deleted", KSTAT_DATA_UINT64 },
13918 13920 { "ip_nce_reclaim_calls", KSTAT_DATA_UINT64 },
13919 13921 { "ip_nce_reclaim_deleted", KSTAT_DATA_UINT64 },
13920 13922 { "ip_dce_reclaim_calls", KSTAT_DATA_UINT64 },
13921 13923 { "ip_dce_reclaim_deleted", KSTAT_DATA_UINT64 },
13922 13924 { "ip_tcp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 },
13923 13925 { "ip_tcp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 },
13924 13926 { "ip_tcp_in_sw_cksum_err", KSTAT_DATA_UINT64 },
13925 13927 { "ip_udp_in_full_hw_cksum_err", KSTAT_DATA_UINT64 },
13926 13928 { "ip_udp_in_part_hw_cksum_err", KSTAT_DATA_UINT64 },
13927 13929 { "ip_udp_in_sw_cksum_err", KSTAT_DATA_UINT64 },
13928 13930 { "conn_in_recvdstaddr", KSTAT_DATA_UINT64 },
13929 13931 { "conn_in_recvopts", KSTAT_DATA_UINT64 },
13930 13932 { "conn_in_recvif", KSTAT_DATA_UINT64 },
13931 13933 { "conn_in_recvslla", KSTAT_DATA_UINT64 },
13932 13934 { "conn_in_recvucred", KSTAT_DATA_UINT64 },
13933 13935 { "conn_in_recvttl", KSTAT_DATA_UINT64 },
13934 13936 { "conn_in_recvhopopts", KSTAT_DATA_UINT64 },
13935 13937 { "conn_in_recvhoplimit", KSTAT_DATA_UINT64 },
13936 13938 { "conn_in_recvdstopts", KSTAT_DATA_UINT64 },
13937 13939 { "conn_in_recvrthdrdstopts", KSTAT_DATA_UINT64 },
13938 13940 { "conn_in_recvrthdr", KSTAT_DATA_UINT64 },
13939 13941 { "conn_in_recvpktinfo", KSTAT_DATA_UINT64 },
13940 13942 { "conn_in_recvtclass", KSTAT_DATA_UINT64 },
13941 13943 { "conn_in_timestamp", KSTAT_DATA_UINT64 },
13942 13944 };
13943 13945
13944 13946 ksp = kstat_create_netstack("ip", 0, "ipstat", "net",
13945 13947 KSTAT_TYPE_NAMED, sizeof (template) / sizeof (kstat_named_t),
13946 13948 KSTAT_FLAG_VIRTUAL, stackid);
13947 13949
13948 13950 if (ksp == NULL)
13949 13951 return (NULL);
13950 13952
13951 13953 bcopy(&template, ip_statisticsp, sizeof (template));
13952 13954 ksp->ks_data = (void *)ip_statisticsp;
13953 13955 ksp->ks_private = (void *)(uintptr_t)stackid;
13954 13956
13955 13957 kstat_install(ksp);
13956 13958 return (ksp);
13957 13959 }
13958 13960
13959 13961 static void
13960 13962 ip_kstat2_fini(netstackid_t stackid, kstat_t *ksp)
13961 13963 {
13962 13964 if (ksp != NULL) {
13963 13965 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
13964 13966 kstat_delete_netstack(ksp, stackid);
13965 13967 }
13966 13968 }
13967 13969
13968 13970 static void *
13969 13971 ip_kstat_init(netstackid_t stackid, ip_stack_t *ipst)
13970 13972 {
13971 13973 kstat_t *ksp;
13972 13974
13973 13975 ip_named_kstat_t template = {
13974 13976 { "forwarding", KSTAT_DATA_UINT32, 0 },
13975 13977 { "defaultTTL", KSTAT_DATA_UINT32, 0 },
13976 13978 { "inReceives", KSTAT_DATA_UINT64, 0 },
13977 13979 { "inHdrErrors", KSTAT_DATA_UINT32, 0 },
13978 13980 { "inAddrErrors", KSTAT_DATA_UINT32, 0 },
13979 13981 { "forwDatagrams", KSTAT_DATA_UINT64, 0 },
13980 13982 { "inUnknownProtos", KSTAT_DATA_UINT32, 0 },
13981 13983 { "inDiscards", KSTAT_DATA_UINT32, 0 },
13982 13984 { "inDelivers", KSTAT_DATA_UINT64, 0 },
13983 13985 { "outRequests", KSTAT_DATA_UINT64, 0 },
13984 13986 { "outDiscards", KSTAT_DATA_UINT32, 0 },
13985 13987 { "outNoRoutes", KSTAT_DATA_UINT32, 0 },
13986 13988 { "reasmTimeout", KSTAT_DATA_UINT32, 0 },
13987 13989 { "reasmReqds", KSTAT_DATA_UINT32, 0 },
13988 13990 { "reasmOKs", KSTAT_DATA_UINT32, 0 },
13989 13991 { "reasmFails", KSTAT_DATA_UINT32, 0 },
13990 13992 { "fragOKs", KSTAT_DATA_UINT32, 0 },
13991 13993 { "fragFails", KSTAT_DATA_UINT32, 0 },
13992 13994 { "fragCreates", KSTAT_DATA_UINT32, 0 },
13993 13995 { "addrEntrySize", KSTAT_DATA_INT32, 0 },
13994 13996 { "routeEntrySize", KSTAT_DATA_INT32, 0 },
13995 13997 { "netToMediaEntrySize", KSTAT_DATA_INT32, 0 },
13996 13998 { "routingDiscards", KSTAT_DATA_UINT32, 0 },
13997 13999 { "inErrs", KSTAT_DATA_UINT32, 0 },
13998 14000 { "noPorts", KSTAT_DATA_UINT32, 0 },
13999 14001 { "inCksumErrs", KSTAT_DATA_UINT32, 0 },
14000 14002 { "reasmDuplicates", KSTAT_DATA_UINT32, 0 },
14001 14003 { "reasmPartDups", KSTAT_DATA_UINT32, 0 },
14002 14004 { "forwProhibits", KSTAT_DATA_UINT32, 0 },
14003 14005 { "udpInCksumErrs", KSTAT_DATA_UINT32, 0 },
14004 14006 { "udpInOverflows", KSTAT_DATA_UINT32, 0 },
14005 14007 { "rawipInOverflows", KSTAT_DATA_UINT32, 0 },
14006 14008 { "ipsecInSucceeded", KSTAT_DATA_UINT32, 0 },
14007 14009 { "ipsecInFailed", KSTAT_DATA_INT32, 0 },
14008 14010 { "memberEntrySize", KSTAT_DATA_INT32, 0 },
14009 14011 { "inIPv6", KSTAT_DATA_UINT32, 0 },
14010 14012 { "outIPv6", KSTAT_DATA_UINT32, 0 },
14011 14013 { "outSwitchIPv6", KSTAT_DATA_UINT32, 0 },
14012 14014 };
14013 14015
14014 14016 ksp = kstat_create_netstack("ip", 0, "ip", "mib2", KSTAT_TYPE_NAMED,
14015 14017 NUM_OF_FIELDS(ip_named_kstat_t), 0, stackid);
14016 14018 if (ksp == NULL || ksp->ks_data == NULL)
14017 14019 return (NULL);
14018 14020
14019 14021 template.forwarding.value.ui32 = WE_ARE_FORWARDING(ipst) ? 1:2;
14020 14022 template.defaultTTL.value.ui32 = (uint32_t)ipst->ips_ip_def_ttl;
14021 14023 template.reasmTimeout.value.ui32 = ipst->ips_ip_reassembly_timeout;
14022 14024 template.addrEntrySize.value.i32 = sizeof (mib2_ipAddrEntry_t);
14023 14025 template.routeEntrySize.value.i32 = sizeof (mib2_ipRouteEntry_t);
14024 14026
14025 14027 template.netToMediaEntrySize.value.i32 =
14026 14028 sizeof (mib2_ipNetToMediaEntry_t);
14027 14029
14028 14030 template.memberEntrySize.value.i32 = sizeof (ipv6_member_t);
14029 14031
14030 14032 bcopy(&template, ksp->ks_data, sizeof (template));
14031 14033 ksp->ks_update = ip_kstat_update;
14032 14034 ksp->ks_private = (void *)(uintptr_t)stackid;
14033 14035
14034 14036 kstat_install(ksp);
14035 14037 return (ksp);
14036 14038 }
14037 14039
14038 14040 static void
14039 14041 ip_kstat_fini(netstackid_t stackid, kstat_t *ksp)
14040 14042 {
14041 14043 if (ksp != NULL) {
14042 14044 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
14043 14045 kstat_delete_netstack(ksp, stackid);
14044 14046 }
14045 14047 }
14046 14048
14047 14049 static int
14048 14050 ip_kstat_update(kstat_t *kp, int rw)
14049 14051 {
14050 14052 ip_named_kstat_t *ipkp;
14051 14053 mib2_ipIfStatsEntry_t ipmib;
14052 14054 ill_walk_context_t ctx;
14053 14055 ill_t *ill;
14054 14056 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private;
14055 14057 netstack_t *ns;
14056 14058 ip_stack_t *ipst;
14057 14059
14058 14060 if (kp == NULL || kp->ks_data == NULL)
14059 14061 return (EIO);
14060 14062
14061 14063 if (rw == KSTAT_WRITE)
14062 14064 return (EACCES);
14063 14065
14064 14066 ns = netstack_find_by_stackid(stackid);
14065 14067 if (ns == NULL)
14066 14068 return (-1);
14067 14069 ipst = ns->netstack_ip;
14068 14070 if (ipst == NULL) {
14069 14071 netstack_rele(ns);
14070 14072 return (-1);
14071 14073 }
14072 14074 ipkp = (ip_named_kstat_t *)kp->ks_data;
14073 14075
14074 14076 bcopy(&ipst->ips_ip_mib, &ipmib, sizeof (ipmib));
14075 14077 rw_enter(&ipst->ips_ill_g_lock, RW_READER);
14076 14078 ill = ILL_START_WALK_V4(&ctx, ipst);
14077 14079 for (; ill != NULL; ill = ill_next(&ctx, ill))
14078 14080 ip_mib2_add_ip_stats(&ipmib, ill->ill_ip_mib);
14079 14081 rw_exit(&ipst->ips_ill_g_lock);
14080 14082
14081 14083 ipkp->forwarding.value.ui32 = ipmib.ipIfStatsForwarding;
14082 14084 ipkp->defaultTTL.value.ui32 = ipmib.ipIfStatsDefaultTTL;
14083 14085 ipkp->inReceives.value.ui64 = ipmib.ipIfStatsHCInReceives;
14084 14086 ipkp->inHdrErrors.value.ui32 = ipmib.ipIfStatsInHdrErrors;
14085 14087 ipkp->inAddrErrors.value.ui32 = ipmib.ipIfStatsInAddrErrors;
14086 14088 ipkp->forwDatagrams.value.ui64 = ipmib.ipIfStatsHCOutForwDatagrams;
14087 14089 ipkp->inUnknownProtos.value.ui32 = ipmib.ipIfStatsInUnknownProtos;
14088 14090 ipkp->inDiscards.value.ui32 = ipmib.ipIfStatsInDiscards;
14089 14091 ipkp->inDelivers.value.ui64 = ipmib.ipIfStatsHCInDelivers;
14090 14092 ipkp->outRequests.value.ui64 = ipmib.ipIfStatsHCOutRequests;
14091 14093 ipkp->outDiscards.value.ui32 = ipmib.ipIfStatsOutDiscards;
14092 14094 ipkp->outNoRoutes.value.ui32 = ipmib.ipIfStatsOutNoRoutes;
14093 14095 ipkp->reasmTimeout.value.ui32 = ipst->ips_ip_reassembly_timeout;
14094 14096 ipkp->reasmReqds.value.ui32 = ipmib.ipIfStatsReasmReqds;
14095 14097 ipkp->reasmOKs.value.ui32 = ipmib.ipIfStatsReasmOKs;
14096 14098 ipkp->reasmFails.value.ui32 = ipmib.ipIfStatsReasmFails;
14097 14099 ipkp->fragOKs.value.ui32 = ipmib.ipIfStatsOutFragOKs;
14098 14100 ipkp->fragFails.value.ui32 = ipmib.ipIfStatsOutFragFails;
14099 14101 ipkp->fragCreates.value.ui32 = ipmib.ipIfStatsOutFragCreates;
14100 14102
14101 14103 ipkp->routingDiscards.value.ui32 = 0;
14102 14104 ipkp->inErrs.value.ui32 = ipmib.tcpIfStatsInErrs;
14103 14105 ipkp->noPorts.value.ui32 = ipmib.udpIfStatsNoPorts;
14104 14106 ipkp->inCksumErrs.value.ui32 = ipmib.ipIfStatsInCksumErrs;
14105 14107 ipkp->reasmDuplicates.value.ui32 = ipmib.ipIfStatsReasmDuplicates;
14106 14108 ipkp->reasmPartDups.value.ui32 = ipmib.ipIfStatsReasmPartDups;
14107 14109 ipkp->forwProhibits.value.ui32 = ipmib.ipIfStatsForwProhibits;
14108 14110 ipkp->udpInCksumErrs.value.ui32 = ipmib.udpIfStatsInCksumErrs;
14109 14111 ipkp->udpInOverflows.value.ui32 = ipmib.udpIfStatsInOverflows;
14110 14112 ipkp->rawipInOverflows.value.ui32 = ipmib.rawipIfStatsInOverflows;
14111 14113 ipkp->ipsecInSucceeded.value.ui32 = ipmib.ipsecIfStatsInSucceeded;
14112 14114 ipkp->ipsecInFailed.value.i32 = ipmib.ipsecIfStatsInFailed;
14113 14115
14114 14116 ipkp->inIPv6.value.ui32 = ipmib.ipIfStatsInWrongIPVersion;
14115 14117 ipkp->outIPv6.value.ui32 = ipmib.ipIfStatsOutWrongIPVersion;
14116 14118 ipkp->outSwitchIPv6.value.ui32 = ipmib.ipIfStatsOutSwitchIPVersion;
14117 14119
14118 14120 netstack_rele(ns);
14119 14121
14120 14122 return (0);
14121 14123 }
14122 14124
14123 14125 static void *
14124 14126 icmp_kstat_init(netstackid_t stackid)
14125 14127 {
14126 14128 kstat_t *ksp;
14127 14129
14128 14130 icmp_named_kstat_t template = {
14129 14131 { "inMsgs", KSTAT_DATA_UINT32 },
14130 14132 { "inErrors", KSTAT_DATA_UINT32 },
14131 14133 { "inDestUnreachs", KSTAT_DATA_UINT32 },
14132 14134 { "inTimeExcds", KSTAT_DATA_UINT32 },
14133 14135 { "inParmProbs", KSTAT_DATA_UINT32 },
14134 14136 { "inSrcQuenchs", KSTAT_DATA_UINT32 },
14135 14137 { "inRedirects", KSTAT_DATA_UINT32 },
14136 14138 { "inEchos", KSTAT_DATA_UINT32 },
14137 14139 { "inEchoReps", KSTAT_DATA_UINT32 },
14138 14140 { "inTimestamps", KSTAT_DATA_UINT32 },
14139 14141 { "inTimestampReps", KSTAT_DATA_UINT32 },
14140 14142 { "inAddrMasks", KSTAT_DATA_UINT32 },
14141 14143 { "inAddrMaskReps", KSTAT_DATA_UINT32 },
14142 14144 { "outMsgs", KSTAT_DATA_UINT32 },
14143 14145 { "outErrors", KSTAT_DATA_UINT32 },
14144 14146 { "outDestUnreachs", KSTAT_DATA_UINT32 },
14145 14147 { "outTimeExcds", KSTAT_DATA_UINT32 },
14146 14148 { "outParmProbs", KSTAT_DATA_UINT32 },
14147 14149 { "outSrcQuenchs", KSTAT_DATA_UINT32 },
14148 14150 { "outRedirects", KSTAT_DATA_UINT32 },
14149 14151 { "outEchos", KSTAT_DATA_UINT32 },
14150 14152 { "outEchoReps", KSTAT_DATA_UINT32 },
14151 14153 { "outTimestamps", KSTAT_DATA_UINT32 },
14152 14154 { "outTimestampReps", KSTAT_DATA_UINT32 },
14153 14155 { "outAddrMasks", KSTAT_DATA_UINT32 },
14154 14156 { "outAddrMaskReps", KSTAT_DATA_UINT32 },
14155 14157 { "inChksumErrs", KSTAT_DATA_UINT32 },
14156 14158 { "inUnknowns", KSTAT_DATA_UINT32 },
14157 14159 { "inFragNeeded", KSTAT_DATA_UINT32 },
14158 14160 { "outFragNeeded", KSTAT_DATA_UINT32 },
14159 14161 { "outDrops", KSTAT_DATA_UINT32 },
14160 14162 { "inOverFlows", KSTAT_DATA_UINT32 },
14161 14163 { "inBadRedirects", KSTAT_DATA_UINT32 },
14162 14164 };
14163 14165
14164 14166 ksp = kstat_create_netstack("ip", 0, "icmp", "mib2", KSTAT_TYPE_NAMED,
14165 14167 NUM_OF_FIELDS(icmp_named_kstat_t), 0, stackid);
14166 14168 if (ksp == NULL || ksp->ks_data == NULL)
14167 14169 return (NULL);
14168 14170
14169 14171 bcopy(&template, ksp->ks_data, sizeof (template));
14170 14172
14171 14173 ksp->ks_update = icmp_kstat_update;
14172 14174 ksp->ks_private = (void *)(uintptr_t)stackid;
14173 14175
14174 14176 kstat_install(ksp);
14175 14177 return (ksp);
14176 14178 }
14177 14179
14178 14180 static void
14179 14181 icmp_kstat_fini(netstackid_t stackid, kstat_t *ksp)
14180 14182 {
14181 14183 if (ksp != NULL) {
14182 14184 ASSERT(stackid == (netstackid_t)(uintptr_t)ksp->ks_private);
14183 14185 kstat_delete_netstack(ksp, stackid);
14184 14186 }
14185 14187 }
14186 14188
14187 14189 static int
14188 14190 icmp_kstat_update(kstat_t *kp, int rw)
14189 14191 {
14190 14192 icmp_named_kstat_t *icmpkp;
14191 14193 netstackid_t stackid = (zoneid_t)(uintptr_t)kp->ks_private;
14192 14194 netstack_t *ns;
14193 14195 ip_stack_t *ipst;
14194 14196
14195 14197 if ((kp == NULL) || (kp->ks_data == NULL))
14196 14198 return (EIO);
14197 14199
14198 14200 if (rw == KSTAT_WRITE)
14199 14201 return (EACCES);
14200 14202
14201 14203 ns = netstack_find_by_stackid(stackid);
14202 14204 if (ns == NULL)
14203 14205 return (-1);
14204 14206 ipst = ns->netstack_ip;
14205 14207 if (ipst == NULL) {
14206 14208 netstack_rele(ns);
14207 14209 return (-1);
14208 14210 }
14209 14211 icmpkp = (icmp_named_kstat_t *)kp->ks_data;
14210 14212
14211 14213 icmpkp->inMsgs.value.ui32 = ipst->ips_icmp_mib.icmpInMsgs;
14212 14214 icmpkp->inErrors.value.ui32 = ipst->ips_icmp_mib.icmpInErrors;
14213 14215 icmpkp->inDestUnreachs.value.ui32 =
14214 14216 ipst->ips_icmp_mib.icmpInDestUnreachs;
14215 14217 icmpkp->inTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpInTimeExcds;
14216 14218 icmpkp->inParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpInParmProbs;
14217 14219 icmpkp->inSrcQuenchs.value.ui32 = ipst->ips_icmp_mib.icmpInSrcQuenchs;
14218 14220 icmpkp->inRedirects.value.ui32 = ipst->ips_icmp_mib.icmpInRedirects;
14219 14221 icmpkp->inEchos.value.ui32 = ipst->ips_icmp_mib.icmpInEchos;
14220 14222 icmpkp->inEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpInEchoReps;
14221 14223 icmpkp->inTimestamps.value.ui32 = ipst->ips_icmp_mib.icmpInTimestamps;
14222 14224 icmpkp->inTimestampReps.value.ui32 =
14223 14225 ipst->ips_icmp_mib.icmpInTimestampReps;
14224 14226 icmpkp->inAddrMasks.value.ui32 = ipst->ips_icmp_mib.icmpInAddrMasks;
14225 14227 icmpkp->inAddrMaskReps.value.ui32 =
14226 14228 ipst->ips_icmp_mib.icmpInAddrMaskReps;
14227 14229 icmpkp->outMsgs.value.ui32 = ipst->ips_icmp_mib.icmpOutMsgs;
14228 14230 icmpkp->outErrors.value.ui32 = ipst->ips_icmp_mib.icmpOutErrors;
14229 14231 icmpkp->outDestUnreachs.value.ui32 =
14230 14232 ipst->ips_icmp_mib.icmpOutDestUnreachs;
14231 14233 icmpkp->outTimeExcds.value.ui32 = ipst->ips_icmp_mib.icmpOutTimeExcds;
14232 14234 icmpkp->outParmProbs.value.ui32 = ipst->ips_icmp_mib.icmpOutParmProbs;
14233 14235 icmpkp->outSrcQuenchs.value.ui32 =
14234 14236 ipst->ips_icmp_mib.icmpOutSrcQuenchs;
14235 14237 icmpkp->outRedirects.value.ui32 = ipst->ips_icmp_mib.icmpOutRedirects;
14236 14238 icmpkp->outEchos.value.ui32 = ipst->ips_icmp_mib.icmpOutEchos;
14237 14239 icmpkp->outEchoReps.value.ui32 = ipst->ips_icmp_mib.icmpOutEchoReps;
14238 14240 icmpkp->outTimestamps.value.ui32 =
14239 14241 ipst->ips_icmp_mib.icmpOutTimestamps;
14240 14242 icmpkp->outTimestampReps.value.ui32 =
14241 14243 ipst->ips_icmp_mib.icmpOutTimestampReps;
14242 14244 icmpkp->outAddrMasks.value.ui32 =
14243 14245 ipst->ips_icmp_mib.icmpOutAddrMasks;
14244 14246 icmpkp->outAddrMaskReps.value.ui32 =
14245 14247 ipst->ips_icmp_mib.icmpOutAddrMaskReps;
14246 14248 icmpkp->inCksumErrs.value.ui32 = ipst->ips_icmp_mib.icmpInCksumErrs;
14247 14249 icmpkp->inUnknowns.value.ui32 = ipst->ips_icmp_mib.icmpInUnknowns;
14248 14250 icmpkp->inFragNeeded.value.ui32 = ipst->ips_icmp_mib.icmpInFragNeeded;
14249 14251 icmpkp->outFragNeeded.value.ui32 =
14250 14252 ipst->ips_icmp_mib.icmpOutFragNeeded;
14251 14253 icmpkp->outDrops.value.ui32 = ipst->ips_icmp_mib.icmpOutDrops;
14252 14254 icmpkp->inOverflows.value.ui32 = ipst->ips_icmp_mib.icmpInOverflows;
14253 14255 icmpkp->inBadRedirects.value.ui32 =
14254 14256 ipst->ips_icmp_mib.icmpInBadRedirects;
14255 14257
14256 14258 netstack_rele(ns);
14257 14259 return (0);
14258 14260 }
14259 14261
14260 14262 /*
14261 14263 * This is the fanout function for raw socket opened for SCTP. Note
14262 14264 * that it is called after SCTP checks that there is no socket which
14263 14265 * wants a packet. Then before SCTP handles this out of the blue packet,
14264 14266 * this function is called to see if there is any raw socket for SCTP.
14265 14267 * If there is and it is bound to the correct address, the packet will
14266 14268 * be sent to that socket. Note that only one raw socket can be bound to
14267 14269 * a port. This is assured in ipcl_sctp_hash_insert();
14268 14270 */
14269 14271 void
14270 14272 ip_fanout_sctp_raw(mblk_t *mp, ipha_t *ipha, ip6_t *ip6h, uint32_t ports,
14271 14273 ip_recv_attr_t *ira)
14272 14274 {
14273 14275 conn_t *connp;
14274 14276 queue_t *rq;
14275 14277 boolean_t secure;
14276 14278 ill_t *ill = ira->ira_ill;
14277 14279 ip_stack_t *ipst = ill->ill_ipst;
14278 14280 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
14279 14281 sctp_stack_t *sctps = ipst->ips_netstack->netstack_sctp;
14280 14282 iaflags_t iraflags = ira->ira_flags;
14281 14283 ill_t *rill = ira->ira_rill;
14282 14284
14283 14285 secure = iraflags & IRAF_IPSEC_SECURE;
14284 14286
14285 14287 connp = ipcl_classify_raw(mp, IPPROTO_SCTP, ports, ipha, ip6h,
14286 14288 ira, ipst);
14287 14289 if (connp == NULL) {
14288 14290 /*
14289 14291 * Although raw sctp is not summed, OOB chunks must be.
14290 14292 * Drop the packet here if the sctp checksum failed.
14291 14293 */
14292 14294 if (iraflags & IRAF_SCTP_CSUM_ERR) {
14293 14295 SCTPS_BUMP_MIB(sctps, sctpChecksumError);
14294 14296 freemsg(mp);
14295 14297 return;
14296 14298 }
14297 14299 ira->ira_ill = ira->ira_rill = NULL;
14298 14300 sctp_ootb_input(mp, ira, ipst);
14299 14301 ira->ira_ill = ill;
14300 14302 ira->ira_rill = rill;
14301 14303 return;
14302 14304 }
14303 14305 rq = connp->conn_rq;
14304 14306 if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld : !canputnext(rq)) {
14305 14307 CONN_DEC_REF(connp);
14306 14308 BUMP_MIB(ill->ill_ip_mib, rawipIfStatsInOverflows);
14307 14309 freemsg(mp);
14308 14310 return;
14309 14311 }
14310 14312 if (((iraflags & IRAF_IS_IPV4) ?
14311 14313 CONN_INBOUND_POLICY_PRESENT(connp, ipss) :
14312 14314 CONN_INBOUND_POLICY_PRESENT_V6(connp, ipss)) ||
14313 14315 secure) {
14314 14316 mp = ipsec_check_inbound_policy(mp, connp, ipha,
14315 14317 ip6h, ira);
14316 14318 if (mp == NULL) {
14317 14319 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
14318 14320 /* Note that mp is NULL */
14319 14321 ip_drop_input("ipIfStatsInDiscards", mp, ill);
14320 14322 CONN_DEC_REF(connp);
14321 14323 return;
14322 14324 }
14323 14325 }
14324 14326
14325 14327 if (iraflags & IRAF_ICMP_ERROR) {
14326 14328 (connp->conn_recvicmp)(connp, mp, NULL, ira);
14327 14329 } else {
14328 14330 ill_t *rill = ira->ira_rill;
14329 14331
14330 14332 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
14331 14333 /* This is the SOCK_RAW, IPPROTO_SCTP case. */
14332 14334 ira->ira_ill = ira->ira_rill = NULL;
14333 14335 (connp->conn_recv)(connp, mp, NULL, ira);
14334 14336 ira->ira_ill = ill;
14335 14337 ira->ira_rill = rill;
14336 14338 }
14337 14339 CONN_DEC_REF(connp);
14338 14340 }
14339 14341
14340 14342 /*
14341 14343 * Free a packet that has the link-layer dl_unitdata_req_t or fast-path
14342 14344 * header before the ip payload.
14343 14345 */
14344 14346 static void
14345 14347 ip_xmit_flowctl_drop(ill_t *ill, mblk_t *mp, boolean_t is_fp_mp, int fp_mp_len)
14346 14348 {
14347 14349 int len = (mp->b_wptr - mp->b_rptr);
14348 14350 mblk_t *ip_mp;
14349 14351
14350 14352 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14351 14353 if (is_fp_mp || len != fp_mp_len) {
14352 14354 if (len > fp_mp_len) {
14353 14355 /*
14354 14356 * fastpath header and ip header in the first mblk
14355 14357 */
14356 14358 mp->b_rptr += fp_mp_len;
14357 14359 } else {
14358 14360 /*
14359 14361 * ip_xmit_attach_llhdr had to prepend an mblk to
14360 14362 * attach the fastpath header before ip header.
14361 14363 */
14362 14364 ip_mp = mp->b_cont;
14363 14365 freeb(mp);
14364 14366 mp = ip_mp;
14365 14367 mp->b_rptr += (fp_mp_len - len);
14366 14368 }
14367 14369 } else {
14368 14370 ip_mp = mp->b_cont;
14369 14371 freeb(mp);
14370 14372 mp = ip_mp;
14371 14373 }
14372 14374 ip_drop_output("ipIfStatsOutDiscards - flow ctl", mp, ill);
14373 14375 freemsg(mp);
14374 14376 }
14375 14377
14376 14378 /*
14377 14379 * Normal post fragmentation function.
14378 14380 *
14379 14381 * Send a packet using the passed in nce. This handles both IPv4 and IPv6
14380 14382 * using the same state machine.
14381 14383 *
14382 14384 * We return an error on failure. In particular we return EWOULDBLOCK
14383 14385 * when the driver flow controls. In that case this ensures that ip_wsrv runs
14384 14386 * (currently by canputnext failure resulting in backenabling from GLD.)
14385 14387 * This allows the callers of conn_ip_output() to use EWOULDBLOCK as an
14386 14388 * indication that they can flow control until ip_wsrv() tells then to restart.
14387 14389 *
14388 14390 * If the nce passed by caller is incomplete, this function
14389 14391 * queues the packet and if necessary, sends ARP request and bails.
14390 14392 * If the Neighbor Cache passed is fully resolved, we simply prepend
14391 14393 * the link-layer header to the packet, do ipsec hw acceleration
14392 14394 * work if necessary, and send the packet out on the wire.
14393 14395 */
14394 14396 /* ARGSUSED6 */
14395 14397 int
14396 14398 ip_xmit(mblk_t *mp, nce_t *nce, iaflags_t ixaflags, uint_t pkt_len,
14397 14399 uint32_t xmit_hint, zoneid_t szone, zoneid_t nolzid, uintptr_t *ixacookie)
14398 14400 {
14399 14401 queue_t *wq;
14400 14402 ill_t *ill = nce->nce_ill;
14401 14403 ip_stack_t *ipst = ill->ill_ipst;
14402 14404 uint64_t delta;
14403 14405 boolean_t isv6 = ill->ill_isv6;
14404 14406 boolean_t fp_mp;
14405 14407 ncec_t *ncec = nce->nce_common;
14406 14408 int64_t now = LBOLT_FASTPATH64;
14407 14409 boolean_t is_probe;
14408 14410
14409 14411 DTRACE_PROBE1(ip__xmit, nce_t *, nce);
14410 14412
14411 14413 ASSERT(mp != NULL);
14412 14414 ASSERT(mp->b_datap->db_type == M_DATA);
14413 14415 ASSERT(pkt_len == msgdsize(mp));
14414 14416
14415 14417 /*
14416 14418 * If we have already been here and are coming back after ARP/ND.
14417 14419 * the IXAF_NO_TRACE flag is set. We skip FW_HOOKS, DTRACE and ipobs
14418 14420 * in that case since they have seen the packet when it came here
14419 14421 * the first time.
14420 14422 */
14421 14423 if (ixaflags & IXAF_NO_TRACE)
14422 14424 goto sendit;
14423 14425
14424 14426 if (ixaflags & IXAF_IS_IPV4) {
14425 14427 ipha_t *ipha = (ipha_t *)mp->b_rptr;
14426 14428
14427 14429 ASSERT(!isv6);
14428 14430 ASSERT(pkt_len == ntohs(((ipha_t *)mp->b_rptr)->ipha_length));
14429 14431 if (HOOKS4_INTERESTED_PHYSICAL_OUT(ipst) &&
14430 14432 !(ixaflags & IXAF_NO_PFHOOK)) {
14431 14433 int error;
14432 14434
14433 14435 FW_HOOKS(ipst->ips_ip4_physical_out_event,
14434 14436 ipst->ips_ipv4firewall_physical_out,
14435 14437 NULL, ill, ipha, mp, mp, 0, ipst, error);
14436 14438 DTRACE_PROBE1(ip4__physical__out__end,
14437 14439 mblk_t *, mp);
14438 14440 if (mp == NULL)
14439 14441 return (error);
14440 14442
14441 14443 /* The length could have changed */
14442 14444 pkt_len = msgdsize(mp);
14443 14445 }
14444 14446 if (ipst->ips_ip4_observe.he_interested) {
14445 14447 /*
14446 14448 * Note that for TX the zoneid is the sending
14447 14449 * zone, whether or not MLP is in play.
14448 14450 * Since the szone argument is the IP zoneid (i.e.,
14449 14451 * zero for exclusive-IP zones) and ipobs wants
14450 14452 * the system zoneid, we map it here.
14451 14453 */
14452 14454 szone = IP_REAL_ZONEID(szone, ipst);
14453 14455
14454 14456 /*
14455 14457 * On the outbound path the destination zone will be
14456 14458 * unknown as we're sending this packet out on the
14457 14459 * wire.
14458 14460 */
14459 14461 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, ALL_ZONES,
14460 14462 ill, ipst);
14461 14463 }
14462 14464 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL,
14463 14465 void_ip_t *, ipha, __dtrace_ipsr_ill_t *, ill,
14464 14466 ipha_t *, ipha, ip6_t *, NULL, int, 0);
14465 14467 } else {
14466 14468 ip6_t *ip6h = (ip6_t *)mp->b_rptr;
14467 14469
14468 14470 ASSERT(isv6);
14469 14471 ASSERT(pkt_len ==
14470 14472 ntohs(((ip6_t *)mp->b_rptr)->ip6_plen) + IPV6_HDR_LEN);
14471 14473 if (HOOKS6_INTERESTED_PHYSICAL_OUT(ipst) &&
14472 14474 !(ixaflags & IXAF_NO_PFHOOK)) {
14473 14475 int error;
14474 14476
14475 14477 FW_HOOKS6(ipst->ips_ip6_physical_out_event,
14476 14478 ipst->ips_ipv6firewall_physical_out,
14477 14479 NULL, ill, ip6h, mp, mp, 0, ipst, error);
14478 14480 DTRACE_PROBE1(ip6__physical__out__end,
14479 14481 mblk_t *, mp);
14480 14482 if (mp == NULL)
14481 14483 return (error);
14482 14484
14483 14485 /* The length could have changed */
14484 14486 pkt_len = msgdsize(mp);
14485 14487 }
14486 14488 if (ipst->ips_ip6_observe.he_interested) {
14487 14489 /* See above */
14488 14490 szone = IP_REAL_ZONEID(szone, ipst);
14489 14491
14490 14492 ipobs_hook(mp, IPOBS_HOOK_OUTBOUND, szone, ALL_ZONES,
14491 14493 ill, ipst);
14492 14494 }
14493 14495 DTRACE_IP7(send, mblk_t *, mp, conn_t *, NULL,
14494 14496 void_ip_t *, ip6h, __dtrace_ipsr_ill_t *, ill,
14495 14497 ipha_t *, NULL, ip6_t *, ip6h, int, 0);
14496 14498 }
14497 14499
14498 14500 sendit:
14499 14501 /*
14500 14502 * We check the state without a lock because the state can never
14501 14503 * move "backwards" to initial or incomplete.
14502 14504 */
14503 14505 switch (ncec->ncec_state) {
14504 14506 case ND_REACHABLE:
14505 14507 case ND_STALE:
14506 14508 case ND_DELAY:
14507 14509 case ND_PROBE:
14508 14510 mp = ip_xmit_attach_llhdr(mp, nce);
14509 14511 if (mp == NULL) {
14510 14512 /*
14511 14513 * ip_xmit_attach_llhdr has increased
14512 14514 * ipIfStatsOutDiscards and called ip_drop_output()
14513 14515 */
14514 14516 return (ENOBUFS);
14515 14517 }
14516 14518 /*
14517 14519 * check if nce_fastpath completed and we tagged on a
14518 14520 * copy of nce_fp_mp in ip_xmit_attach_llhdr().
14519 14521 */
14520 14522 fp_mp = (mp->b_datap->db_type == M_DATA);
14521 14523
14522 14524 if (fp_mp &&
14523 14525 (ill->ill_capabilities & ILL_CAPAB_DLD_DIRECT)) {
14524 14526 ill_dld_direct_t *idd;
14525 14527
14526 14528 idd = &ill->ill_dld_capab->idc_direct;
14527 14529 /*
14528 14530 * Send the packet directly to DLD, where it
14529 14531 * may be queued depending on the availability
14530 14532 * of transmit resources at the media layer.
14531 14533 * Return value should be taken into
14532 14534 * account and flow control the TCP.
14533 14535 */
14534 14536 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits);
14535 14537 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets,
14536 14538 pkt_len);
14537 14539
14538 14540 if (ixaflags & IXAF_NO_DEV_FLOW_CTL) {
14539 14541 (void) idd->idd_tx_df(idd->idd_tx_dh, mp,
14540 14542 (uintptr_t)xmit_hint, IP_DROP_ON_NO_DESC);
14541 14543 } else {
14542 14544 uintptr_t cookie;
14543 14545
14544 14546 if ((cookie = idd->idd_tx_df(idd->idd_tx_dh,
14545 14547 mp, (uintptr_t)xmit_hint, 0)) != 0) {
14546 14548 if (ixacookie != NULL)
14547 14549 *ixacookie = cookie;
14548 14550 return (EWOULDBLOCK);
14549 14551 }
14550 14552 }
14551 14553 } else {
14552 14554 wq = ill->ill_wq;
14553 14555
14554 14556 if (!(ixaflags & IXAF_NO_DEV_FLOW_CTL) &&
14555 14557 !canputnext(wq)) {
14556 14558 if (ixacookie != NULL)
14557 14559 *ixacookie = 0;
14558 14560 ip_xmit_flowctl_drop(ill, mp, fp_mp,
14559 14561 nce->nce_fp_mp != NULL ?
14560 14562 MBLKL(nce->nce_fp_mp) : 0);
14561 14563 return (EWOULDBLOCK);
14562 14564 }
14563 14565 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCOutTransmits);
14564 14566 UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCOutOctets,
14565 14567 pkt_len);
14566 14568 putnext(wq, mp);
14567 14569 }
14568 14570
14569 14571 /*
14570 14572 * The rest of this function implements Neighbor Unreachability
14571 14573 * detection. Determine if the ncec is eligible for NUD.
14572 14574 */
14573 14575 if (ncec->ncec_flags & NCE_F_NONUD)
14574 14576 return (0);
14575 14577
14576 14578 ASSERT(ncec->ncec_state != ND_INCOMPLETE);
14577 14579
14578 14580 /*
14579 14581 * Check for upper layer advice
14580 14582 */
14581 14583 if (ixaflags & IXAF_REACH_CONF) {
14582 14584 timeout_id_t tid;
14583 14585
14584 14586 /*
14585 14587 * It should be o.k. to check the state without
14586 14588 * a lock here, at most we lose an advice.
14587 14589 */
14588 14590 ncec->ncec_last = TICK_TO_MSEC(now);
14589 14591 if (ncec->ncec_state != ND_REACHABLE) {
14590 14592 mutex_enter(&ncec->ncec_lock);
14591 14593 ncec->ncec_state = ND_REACHABLE;
14592 14594 tid = ncec->ncec_timeout_id;
14593 14595 ncec->ncec_timeout_id = 0;
14594 14596 mutex_exit(&ncec->ncec_lock);
14595 14597 (void) untimeout(tid);
14596 14598 if (ip_debug > 2) {
14597 14599 /* ip1dbg */
14598 14600 pr_addr_dbg("ip_xmit: state"
14599 14601 " for %s changed to"
14600 14602 " REACHABLE\n", AF_INET6,
14601 14603 &ncec->ncec_addr);
14602 14604 }
14603 14605 }
14604 14606 return (0);
14605 14607 }
14606 14608
14607 14609 delta = TICK_TO_MSEC(now) - ncec->ncec_last;
14608 14610 ip1dbg(("ip_xmit: delta = %" PRId64
14609 14611 " ill_reachable_time = %d \n", delta,
14610 14612 ill->ill_reachable_time));
14611 14613 if (delta > (uint64_t)ill->ill_reachable_time) {
14612 14614 mutex_enter(&ncec->ncec_lock);
14613 14615 switch (ncec->ncec_state) {
14614 14616 case ND_REACHABLE:
14615 14617 ASSERT((ncec->ncec_flags & NCE_F_NONUD) == 0);
14616 14618 /* FALLTHROUGH */
14617 14619 case ND_STALE:
14618 14620 /*
14619 14621 * ND_REACHABLE is identical to
14620 14622 * ND_STALE in this specific case. If
14621 14623 * reachable time has expired for this
14622 14624 * neighbor (delta is greater than
14623 14625 * reachable time), conceptually, the
14624 14626 * neighbor cache is no longer in
14625 14627 * REACHABLE state, but already in
14626 14628 * STALE state. So the correct
14627 14629 * transition here is to ND_DELAY.
14628 14630 */
14629 14631 ncec->ncec_state = ND_DELAY;
14630 14632 mutex_exit(&ncec->ncec_lock);
14631 14633 nce_restart_timer(ncec,
14632 14634 ipst->ips_delay_first_probe_time);
14633 14635 if (ip_debug > 3) {
14634 14636 /* ip2dbg */
14635 14637 pr_addr_dbg("ip_xmit: state"
14636 14638 " for %s changed to"
14637 14639 " DELAY\n", AF_INET6,
14638 14640 &ncec->ncec_addr);
14639 14641 }
14640 14642 break;
14641 14643 case ND_DELAY:
14642 14644 case ND_PROBE:
14643 14645 mutex_exit(&ncec->ncec_lock);
14644 14646 /* Timers have already started */
14645 14647 break;
14646 14648 case ND_UNREACHABLE:
14647 14649 /*
14648 14650 * nce_timer has detected that this ncec
14649 14651 * is unreachable and initiated deleting
14650 14652 * this ncec.
14651 14653 * This is a harmless race where we found the
14652 14654 * ncec before it was deleted and have
14653 14655 * just sent out a packet using this
14654 14656 * unreachable ncec.
14655 14657 */
14656 14658 mutex_exit(&ncec->ncec_lock);
14657 14659 break;
14658 14660 default:
14659 14661 ASSERT(0);
14660 14662 mutex_exit(&ncec->ncec_lock);
14661 14663 }
14662 14664 }
14663 14665 return (0);
14664 14666
14665 14667 case ND_INCOMPLETE:
14666 14668 /*
14667 14669 * the state could have changed since we didn't hold the lock.
14668 14670 * Re-verify state under lock.
14669 14671 */
14670 14672 is_probe = ipmp_packet_is_probe(mp, nce->nce_ill);
14671 14673 mutex_enter(&ncec->ncec_lock);
14672 14674 if (NCE_ISREACHABLE(ncec)) {
14673 14675 mutex_exit(&ncec->ncec_lock);
14674 14676 goto sendit;
14675 14677 }
14676 14678 /* queue the packet */
14677 14679 nce_queue_mp(ncec, mp, is_probe);
14678 14680 mutex_exit(&ncec->ncec_lock);
14679 14681 DTRACE_PROBE2(ip__xmit__incomplete,
14680 14682 (ncec_t *), ncec, (mblk_t *), mp);
14681 14683 return (0);
14682 14684
14683 14685 case ND_INITIAL:
14684 14686 /*
14685 14687 * State could have changed since we didn't hold the lock, so
14686 14688 * re-verify state.
14687 14689 */
14688 14690 is_probe = ipmp_packet_is_probe(mp, nce->nce_ill);
14689 14691 mutex_enter(&ncec->ncec_lock);
14690 14692 if (NCE_ISREACHABLE(ncec)) {
14691 14693 mutex_exit(&ncec->ncec_lock);
14692 14694 goto sendit;
14693 14695 }
14694 14696 nce_queue_mp(ncec, mp, is_probe);
14695 14697 if (ncec->ncec_state == ND_INITIAL) {
14696 14698 ncec->ncec_state = ND_INCOMPLETE;
14697 14699 mutex_exit(&ncec->ncec_lock);
14698 14700 /*
14699 14701 * figure out the source we want to use
14700 14702 * and resolve it.
14701 14703 */
14702 14704 ip_ndp_resolve(ncec);
14703 14705 } else {
14704 14706 mutex_exit(&ncec->ncec_lock);
14705 14707 }
14706 14708 return (0);
14707 14709
14708 14710 case ND_UNREACHABLE:
14709 14711 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14710 14712 ip_drop_output("ipIfStatsOutDiscards - ND_UNREACHABLE",
14711 14713 mp, ill);
14712 14714 freemsg(mp);
14713 14715 return (0);
14714 14716
14715 14717 default:
14716 14718 ASSERT(0);
14717 14719 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
14718 14720 ip_drop_output("ipIfStatsOutDiscards - ND_other",
14719 14721 mp, ill);
14720 14722 freemsg(mp);
14721 14723 return (ENETUNREACH);
14722 14724 }
14723 14725 }
14724 14726
14725 14727 /*
14726 14728 * Return B_TRUE if the buffers differ in length or content.
14727 14729 * This is used for comparing extension header buffers.
14728 14730 * Note that an extension header would be declared different
14729 14731 * even if all that changed was the next header value in that header i.e.
14730 14732 * what really changed is the next extension header.
14731 14733 */
14732 14734 boolean_t
14733 14735 ip_cmpbuf(const void *abuf, uint_t alen, boolean_t b_valid, const void *bbuf,
14734 14736 uint_t blen)
14735 14737 {
14736 14738 if (!b_valid)
14737 14739 blen = 0;
14738 14740
14739 14741 if (alen != blen)
14740 14742 return (B_TRUE);
14741 14743 if (alen == 0)
14742 14744 return (B_FALSE); /* Both zero length */
14743 14745 return (bcmp(abuf, bbuf, alen));
14744 14746 }
14745 14747
14746 14748 /*
14747 14749 * Preallocate memory for ip_savebuf(). Returns B_TRUE if ok.
14748 14750 * Return B_FALSE if memory allocation fails - don't change any state!
14749 14751 */
14750 14752 boolean_t
14751 14753 ip_allocbuf(void **dstp, uint_t *dstlenp, boolean_t src_valid,
14752 14754 const void *src, uint_t srclen)
14753 14755 {
14754 14756 void *dst;
14755 14757
14756 14758 if (!src_valid)
14757 14759 srclen = 0;
14758 14760
14759 14761 ASSERT(*dstlenp == 0);
14760 14762 if (src != NULL && srclen != 0) {
14761 14763 dst = mi_alloc(srclen, BPRI_MED);
14762 14764 if (dst == NULL)
14763 14765 return (B_FALSE);
14764 14766 } else {
14765 14767 dst = NULL;
14766 14768 }
14767 14769 if (*dstp != NULL)
14768 14770 mi_free(*dstp);
14769 14771 *dstp = dst;
14770 14772 *dstlenp = dst == NULL ? 0 : srclen;
14771 14773 return (B_TRUE);
14772 14774 }
14773 14775
14774 14776 /*
14775 14777 * Replace what is in *dst, *dstlen with the source.
14776 14778 * Assumes ip_allocbuf has already been called.
14777 14779 */
14778 14780 void
14779 14781 ip_savebuf(void **dstp, uint_t *dstlenp, boolean_t src_valid,
14780 14782 const void *src, uint_t srclen)
14781 14783 {
14782 14784 if (!src_valid)
14783 14785 srclen = 0;
14784 14786
14785 14787 ASSERT(*dstlenp == srclen);
14786 14788 if (src != NULL && srclen != 0)
14787 14789 bcopy(src, *dstp, srclen);
14788 14790 }
14789 14791
14790 14792 /*
14791 14793 * Free the storage pointed to by the members of an ip_pkt_t.
14792 14794 */
14793 14795 void
14794 14796 ip_pkt_free(ip_pkt_t *ipp)
14795 14797 {
14796 14798 uint_t fields = ipp->ipp_fields;
14797 14799
14798 14800 if (fields & IPPF_HOPOPTS) {
14799 14801 kmem_free(ipp->ipp_hopopts, ipp->ipp_hopoptslen);
14800 14802 ipp->ipp_hopopts = NULL;
14801 14803 ipp->ipp_hopoptslen = 0;
14802 14804 }
14803 14805 if (fields & IPPF_RTHDRDSTOPTS) {
14804 14806 kmem_free(ipp->ipp_rthdrdstopts, ipp->ipp_rthdrdstoptslen);
14805 14807 ipp->ipp_rthdrdstopts = NULL;
14806 14808 ipp->ipp_rthdrdstoptslen = 0;
14807 14809 }
14808 14810 if (fields & IPPF_DSTOPTS) {
14809 14811 kmem_free(ipp->ipp_dstopts, ipp->ipp_dstoptslen);
14810 14812 ipp->ipp_dstopts = NULL;
14811 14813 ipp->ipp_dstoptslen = 0;
14812 14814 }
14813 14815 if (fields & IPPF_RTHDR) {
14814 14816 kmem_free(ipp->ipp_rthdr, ipp->ipp_rthdrlen);
14815 14817 ipp->ipp_rthdr = NULL;
14816 14818 ipp->ipp_rthdrlen = 0;
14817 14819 }
14818 14820 if (fields & IPPF_IPV4_OPTIONS) {
14819 14821 kmem_free(ipp->ipp_ipv4_options, ipp->ipp_ipv4_options_len);
14820 14822 ipp->ipp_ipv4_options = NULL;
14821 14823 ipp->ipp_ipv4_options_len = 0;
14822 14824 }
14823 14825 if (fields & IPPF_LABEL_V4) {
14824 14826 kmem_free(ipp->ipp_label_v4, ipp->ipp_label_len_v4);
14825 14827 ipp->ipp_label_v4 = NULL;
14826 14828 ipp->ipp_label_len_v4 = 0;
14827 14829 }
14828 14830 if (fields & IPPF_LABEL_V6) {
14829 14831 kmem_free(ipp->ipp_label_v6, ipp->ipp_label_len_v6);
14830 14832 ipp->ipp_label_v6 = NULL;
14831 14833 ipp->ipp_label_len_v6 = 0;
14832 14834 }
14833 14835 ipp->ipp_fields &= ~(IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14834 14836 IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6);
14835 14837 }
14836 14838
14837 14839 /*
14838 14840 * Copy from src to dst and allocate as needed.
14839 14841 * Returns zero or ENOMEM.
14840 14842 *
14841 14843 * The caller must initialize dst to zero.
14842 14844 */
14843 14845 int
14844 14846 ip_pkt_copy(ip_pkt_t *src, ip_pkt_t *dst, int kmflag)
14845 14847 {
14846 14848 uint_t fields = src->ipp_fields;
14847 14849
14848 14850 /* Start with fields that don't require memory allocation */
14849 14851 dst->ipp_fields = fields &
14850 14852 ~(IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14851 14853 IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6);
14852 14854
14853 14855 dst->ipp_addr = src->ipp_addr;
14854 14856 dst->ipp_unicast_hops = src->ipp_unicast_hops;
14855 14857 dst->ipp_hoplimit = src->ipp_hoplimit;
14856 14858 dst->ipp_tclass = src->ipp_tclass;
14857 14859 dst->ipp_type_of_service = src->ipp_type_of_service;
14858 14860
14859 14861 if (!(fields & (IPPF_HOPOPTS | IPPF_RTHDRDSTOPTS | IPPF_DSTOPTS |
14860 14862 IPPF_RTHDR | IPPF_IPV4_OPTIONS | IPPF_LABEL_V4 | IPPF_LABEL_V6)))
14861 14863 return (0);
14862 14864
14863 14865 if (fields & IPPF_HOPOPTS) {
14864 14866 dst->ipp_hopopts = kmem_alloc(src->ipp_hopoptslen, kmflag);
14865 14867 if (dst->ipp_hopopts == NULL) {
14866 14868 ip_pkt_free(dst);
14867 14869 return (ENOMEM);
14868 14870 }
14869 14871 dst->ipp_fields |= IPPF_HOPOPTS;
14870 14872 bcopy(src->ipp_hopopts, dst->ipp_hopopts,
14871 14873 src->ipp_hopoptslen);
14872 14874 dst->ipp_hopoptslen = src->ipp_hopoptslen;
14873 14875 }
14874 14876 if (fields & IPPF_RTHDRDSTOPTS) {
14875 14877 dst->ipp_rthdrdstopts = kmem_alloc(src->ipp_rthdrdstoptslen,
14876 14878 kmflag);
14877 14879 if (dst->ipp_rthdrdstopts == NULL) {
14878 14880 ip_pkt_free(dst);
14879 14881 return (ENOMEM);
14880 14882 }
14881 14883 dst->ipp_fields |= IPPF_RTHDRDSTOPTS;
14882 14884 bcopy(src->ipp_rthdrdstopts, dst->ipp_rthdrdstopts,
14883 14885 src->ipp_rthdrdstoptslen);
14884 14886 dst->ipp_rthdrdstoptslen = src->ipp_rthdrdstoptslen;
14885 14887 }
14886 14888 if (fields & IPPF_DSTOPTS) {
14887 14889 dst->ipp_dstopts = kmem_alloc(src->ipp_dstoptslen, kmflag);
14888 14890 if (dst->ipp_dstopts == NULL) {
14889 14891 ip_pkt_free(dst);
14890 14892 return (ENOMEM);
14891 14893 }
14892 14894 dst->ipp_fields |= IPPF_DSTOPTS;
14893 14895 bcopy(src->ipp_dstopts, dst->ipp_dstopts,
14894 14896 src->ipp_dstoptslen);
14895 14897 dst->ipp_dstoptslen = src->ipp_dstoptslen;
14896 14898 }
14897 14899 if (fields & IPPF_RTHDR) {
14898 14900 dst->ipp_rthdr = kmem_alloc(src->ipp_rthdrlen, kmflag);
14899 14901 if (dst->ipp_rthdr == NULL) {
14900 14902 ip_pkt_free(dst);
14901 14903 return (ENOMEM);
14902 14904 }
14903 14905 dst->ipp_fields |= IPPF_RTHDR;
14904 14906 bcopy(src->ipp_rthdr, dst->ipp_rthdr,
14905 14907 src->ipp_rthdrlen);
14906 14908 dst->ipp_rthdrlen = src->ipp_rthdrlen;
14907 14909 }
14908 14910 if (fields & IPPF_IPV4_OPTIONS) {
14909 14911 dst->ipp_ipv4_options = kmem_alloc(src->ipp_ipv4_options_len,
14910 14912 kmflag);
14911 14913 if (dst->ipp_ipv4_options == NULL) {
14912 14914 ip_pkt_free(dst);
14913 14915 return (ENOMEM);
14914 14916 }
14915 14917 dst->ipp_fields |= IPPF_IPV4_OPTIONS;
14916 14918 bcopy(src->ipp_ipv4_options, dst->ipp_ipv4_options,
14917 14919 src->ipp_ipv4_options_len);
14918 14920 dst->ipp_ipv4_options_len = src->ipp_ipv4_options_len;
14919 14921 }
14920 14922 if (fields & IPPF_LABEL_V4) {
14921 14923 dst->ipp_label_v4 = kmem_alloc(src->ipp_label_len_v4, kmflag);
14922 14924 if (dst->ipp_label_v4 == NULL) {
14923 14925 ip_pkt_free(dst);
14924 14926 return (ENOMEM);
14925 14927 }
14926 14928 dst->ipp_fields |= IPPF_LABEL_V4;
14927 14929 bcopy(src->ipp_label_v4, dst->ipp_label_v4,
14928 14930 src->ipp_label_len_v4);
14929 14931 dst->ipp_label_len_v4 = src->ipp_label_len_v4;
14930 14932 }
14931 14933 if (fields & IPPF_LABEL_V6) {
14932 14934 dst->ipp_label_v6 = kmem_alloc(src->ipp_label_len_v6, kmflag);
14933 14935 if (dst->ipp_label_v6 == NULL) {
14934 14936 ip_pkt_free(dst);
14935 14937 return (ENOMEM);
14936 14938 }
14937 14939 dst->ipp_fields |= IPPF_LABEL_V6;
14938 14940 bcopy(src->ipp_label_v6, dst->ipp_label_v6,
14939 14941 src->ipp_label_len_v6);
14940 14942 dst->ipp_label_len_v6 = src->ipp_label_len_v6;
14941 14943 }
14942 14944 if (fields & IPPF_FRAGHDR) {
14943 14945 dst->ipp_fraghdr = kmem_alloc(src->ipp_fraghdrlen, kmflag);
14944 14946 if (dst->ipp_fraghdr == NULL) {
14945 14947 ip_pkt_free(dst);
14946 14948 return (ENOMEM);
14947 14949 }
14948 14950 dst->ipp_fields |= IPPF_FRAGHDR;
14949 14951 bcopy(src->ipp_fraghdr, dst->ipp_fraghdr,
14950 14952 src->ipp_fraghdrlen);
14951 14953 dst->ipp_fraghdrlen = src->ipp_fraghdrlen;
14952 14954 }
14953 14955 return (0);
14954 14956 }
14955 14957
14956 14958 /*
14957 14959 * Returns INADDR_ANY if no source route
14958 14960 */
14959 14961 ipaddr_t
14960 14962 ip_pkt_source_route_v4(const ip_pkt_t *ipp)
14961 14963 {
14962 14964 ipaddr_t nexthop = INADDR_ANY;
14963 14965 ipoptp_t opts;
14964 14966 uchar_t *opt;
14965 14967 uint8_t optval;
14966 14968 uint8_t optlen;
14967 14969 uint32_t totallen;
14968 14970
14969 14971 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
14970 14972 return (INADDR_ANY);
14971 14973
14972 14974 totallen = ipp->ipp_ipv4_options_len;
14973 14975 if (totallen & 0x3)
14974 14976 return (INADDR_ANY);
14975 14977
14976 14978 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
14977 14979 optval != IPOPT_EOL;
14978 14980 optval = ipoptp_next(&opts)) {
14979 14981 opt = opts.ipoptp_cur;
14980 14982 switch (optval) {
14981 14983 uint8_t off;
14982 14984 case IPOPT_SSRR:
14983 14985 case IPOPT_LSRR:
14984 14986 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
14985 14987 break;
14986 14988 }
14987 14989 optlen = opts.ipoptp_len;
14988 14990 off = opt[IPOPT_OFFSET];
14989 14991 off--;
14990 14992 if (optlen < IP_ADDR_LEN ||
14991 14993 off > optlen - IP_ADDR_LEN) {
14992 14994 /* End of source route */
14993 14995 break;
14994 14996 }
14995 14997 bcopy((char *)opt + off, &nexthop, IP_ADDR_LEN);
14996 14998 if (nexthop == htonl(INADDR_LOOPBACK)) {
14997 14999 /* Ignore */
14998 15000 nexthop = INADDR_ANY;
14999 15001 break;
15000 15002 }
15001 15003 break;
15002 15004 }
15003 15005 }
15004 15006 return (nexthop);
15005 15007 }
15006 15008
15007 15009 /*
15008 15010 * Reverse a source route.
15009 15011 */
15010 15012 void
15011 15013 ip_pkt_source_route_reverse_v4(ip_pkt_t *ipp)
15012 15014 {
15013 15015 ipaddr_t tmp;
15014 15016 ipoptp_t opts;
15015 15017 uchar_t *opt;
15016 15018 uint8_t optval;
15017 15019 uint32_t totallen;
15018 15020
15019 15021 if (!(ipp->ipp_fields & IPPF_IPV4_OPTIONS))
15020 15022 return;
15021 15023
15022 15024 totallen = ipp->ipp_ipv4_options_len;
15023 15025 if (totallen & 0x3)
15024 15026 return;
15025 15027
15026 15028 for (optval = ipoptp_first2(&opts, totallen, ipp->ipp_ipv4_options);
15027 15029 optval != IPOPT_EOL;
15028 15030 optval = ipoptp_next(&opts)) {
15029 15031 uint8_t off1, off2;
15030 15032
15031 15033 opt = opts.ipoptp_cur;
15032 15034 switch (optval) {
15033 15035 case IPOPT_SSRR:
15034 15036 case IPOPT_LSRR:
15035 15037 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
15036 15038 break;
15037 15039 }
15038 15040 off1 = IPOPT_MINOFF_SR - 1;
15039 15041 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1;
15040 15042 while (off2 > off1) {
15041 15043 bcopy(opt + off2, &tmp, IP_ADDR_LEN);
15042 15044 bcopy(opt + off1, opt + off2, IP_ADDR_LEN);
15043 15045 bcopy(&tmp, opt + off2, IP_ADDR_LEN);
15044 15046 off2 -= IP_ADDR_LEN;
15045 15047 off1 += IP_ADDR_LEN;
15046 15048 }
15047 15049 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR;
15048 15050 break;
15049 15051 }
15050 15052 }
15051 15053 }
15052 15054
15053 15055 /*
15054 15056 * Returns NULL if no routing header
15055 15057 */
15056 15058 in6_addr_t *
15057 15059 ip_pkt_source_route_v6(const ip_pkt_t *ipp)
15058 15060 {
15059 15061 in6_addr_t *nexthop = NULL;
15060 15062 ip6_rthdr0_t *rthdr;
15061 15063
15062 15064 if (!(ipp->ipp_fields & IPPF_RTHDR))
15063 15065 return (NULL);
15064 15066
15065 15067 rthdr = (ip6_rthdr0_t *)ipp->ipp_rthdr;
15066 15068 if (rthdr->ip6r0_segleft == 0)
15067 15069 return (NULL);
15068 15070
15069 15071 nexthop = (in6_addr_t *)((char *)rthdr + sizeof (*rthdr));
15070 15072 return (nexthop);
15071 15073 }
15072 15074
15073 15075 zoneid_t
15074 15076 ip_get_zoneid_v4(ipaddr_t addr, mblk_t *mp, ip_recv_attr_t *ira,
15075 15077 zoneid_t lookup_zoneid)
15076 15078 {
15077 15079 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
15078 15080 ire_t *ire;
15079 15081 int ire_flags = MATCH_IRE_TYPE;
15080 15082 zoneid_t zoneid = ALL_ZONES;
15081 15083
15082 15084 if (is_system_labeled() && !tsol_can_accept_raw(mp, ira, B_FALSE))
15083 15085 return (ALL_ZONES);
15084 15086
15085 15087 if (lookup_zoneid != ALL_ZONES)
15086 15088 ire_flags |= MATCH_IRE_ZONEONLY;
15087 15089 ire = ire_ftable_lookup_v4(addr, NULL, NULL, IRE_LOCAL | IRE_LOOPBACK,
15088 15090 NULL, lookup_zoneid, NULL, ire_flags, 0, ipst, NULL);
15089 15091 if (ire != NULL) {
15090 15092 zoneid = IP_REAL_ZONEID(ire->ire_zoneid, ipst);
15091 15093 ire_refrele(ire);
15092 15094 }
15093 15095 return (zoneid);
15094 15096 }
15095 15097
15096 15098 zoneid_t
15097 15099 ip_get_zoneid_v6(in6_addr_t *addr, mblk_t *mp, const ill_t *ill,
15098 15100 ip_recv_attr_t *ira, zoneid_t lookup_zoneid)
15099 15101 {
15100 15102 ip_stack_t *ipst = ira->ira_ill->ill_ipst;
15101 15103 ire_t *ire;
15102 15104 int ire_flags = MATCH_IRE_TYPE;
15103 15105 zoneid_t zoneid = ALL_ZONES;
15104 15106
15105 15107 if (is_system_labeled() && !tsol_can_accept_raw(mp, ira, B_FALSE))
15106 15108 return (ALL_ZONES);
15107 15109
15108 15110 if (IN6_IS_ADDR_LINKLOCAL(addr))
15109 15111 ire_flags |= MATCH_IRE_ILL;
15110 15112
15111 15113 if (lookup_zoneid != ALL_ZONES)
15112 15114 ire_flags |= MATCH_IRE_ZONEONLY;
15113 15115 ire = ire_ftable_lookup_v6(addr, NULL, NULL, IRE_LOCAL | IRE_LOOPBACK,
15114 15116 ill, lookup_zoneid, NULL, ire_flags, 0, ipst, NULL);
15115 15117 if (ire != NULL) {
15116 15118 zoneid = IP_REAL_ZONEID(ire->ire_zoneid, ipst);
15117 15119 ire_refrele(ire);
15118 15120 }
15119 15121 return (zoneid);
15120 15122 }
15121 15123
15122 15124 /*
15123 15125 * IP obserability hook support functions.
15124 15126 */
15125 15127 static void
15126 15128 ipobs_init(ip_stack_t *ipst)
15127 15129 {
15128 15130 netid_t id;
15129 15131
15130 15132 id = net_getnetidbynetstackid(ipst->ips_netstack->netstack_stackid);
15131 15133
15132 15134 ipst->ips_ip4_observe_pr = net_protocol_lookup(id, NHF_INET);
15133 15135 VERIFY(ipst->ips_ip4_observe_pr != NULL);
15134 15136
15135 15137 ipst->ips_ip6_observe_pr = net_protocol_lookup(id, NHF_INET6);
15136 15138 VERIFY(ipst->ips_ip6_observe_pr != NULL);
15137 15139 }
15138 15140
15139 15141 static void
15140 15142 ipobs_fini(ip_stack_t *ipst)
15141 15143 {
15142 15144
15143 15145 VERIFY(net_protocol_release(ipst->ips_ip4_observe_pr) == 0);
15144 15146 VERIFY(net_protocol_release(ipst->ips_ip6_observe_pr) == 0);
15145 15147 }
15146 15148
15147 15149 /*
15148 15150 * hook_pkt_observe_t is composed in network byte order so that the
15149 15151 * entire mblk_t chain handed into hook_run can be used as-is.
15150 15152 * The caveat is that use of the fields, such as the zone fields,
15151 15153 * requires conversion into host byte order first.
15152 15154 */
15153 15155 void
15154 15156 ipobs_hook(mblk_t *mp, int htype, zoneid_t zsrc, zoneid_t zdst,
15155 15157 const ill_t *ill, ip_stack_t *ipst)
15156 15158 {
15157 15159 hook_pkt_observe_t *hdr;
15158 15160 uint64_t grifindex;
15159 15161 mblk_t *imp;
15160 15162
15161 15163 imp = allocb(sizeof (*hdr), BPRI_HI);
15162 15164 if (imp == NULL)
15163 15165 return;
15164 15166
15165 15167 hdr = (hook_pkt_observe_t *)imp->b_rptr;
15166 15168 /*
15167 15169 * b_wptr is set to make the apparent size of the data in the mblk_t
15168 15170 * to exclude the pointers at the end of hook_pkt_observer_t.
15169 15171 */
15170 15172 imp->b_wptr = imp->b_rptr + sizeof (dl_ipnetinfo_t);
15171 15173 imp->b_cont = mp;
15172 15174
15173 15175 ASSERT(DB_TYPE(mp) == M_DATA);
15174 15176
15175 15177 if (IS_UNDER_IPMP(ill))
15176 15178 grifindex = ipmp_ill_get_ipmp_ifindex(ill);
15177 15179 else
15178 15180 grifindex = 0;
15179 15181
15180 15182 hdr->hpo_version = 1;
15181 15183 hdr->hpo_htype = htons(htype);
15182 15184 hdr->hpo_pktlen = htonl((ulong_t)msgdsize(mp));
15183 15185 hdr->hpo_ifindex = htonl(ill->ill_phyint->phyint_ifindex);
15184 15186 hdr->hpo_grifindex = htonl(grifindex);
15185 15187 hdr->hpo_zsrc = htonl(zsrc);
15186 15188 hdr->hpo_zdst = htonl(zdst);
15187 15189 hdr->hpo_pkt = imp;
15188 15190 hdr->hpo_ctx = ipst->ips_netstack;
15189 15191
15190 15192 if (ill->ill_isv6) {
15191 15193 hdr->hpo_family = AF_INET6;
15192 15194 (void) hook_run(ipst->ips_ipv6_net_data->netd_hooks,
15193 15195 ipst->ips_ipv6observing, (hook_data_t)hdr);
15194 15196 } else {
15195 15197 hdr->hpo_family = AF_INET;
15196 15198 (void) hook_run(ipst->ips_ipv4_net_data->netd_hooks,
15197 15199 ipst->ips_ipv4observing, (hook_data_t)hdr);
15198 15200 }
15199 15201
15200 15202 imp->b_cont = NULL;
15201 15203 freemsg(imp);
15202 15204 }
15203 15205
15204 15206 /*
15205 15207 * Utility routine that checks if `v4srcp' is a valid address on underlying
15206 15208 * interface `ill'. If `ipifp' is non-NULL, it's set to a held ipif
15207 15209 * associated with `v4srcp' on success. NOTE: if this is not called from
15208 15210 * inside the IPSQ (ill_g_lock is not held), `ill' may be removed from the
15209 15211 * group during or after this lookup.
15210 15212 */
15211 15213 boolean_t
15212 15214 ipif_lookup_testaddr_v4(ill_t *ill, const in_addr_t *v4srcp, ipif_t **ipifp)
15213 15215 {
15214 15216 ipif_t *ipif;
15215 15217
15216 15218 ipif = ipif_lookup_addr_exact(*v4srcp, ill, ill->ill_ipst);
15217 15219 if (ipif != NULL) {
15218 15220 if (ipifp != NULL)
15219 15221 *ipifp = ipif;
15220 15222 else
15221 15223 ipif_refrele(ipif);
15222 15224 return (B_TRUE);
15223 15225 }
15224 15226
15225 15227 ip1dbg(("ipif_lookup_testaddr_v4: cannot find ipif for src %x\n",
15226 15228 *v4srcp));
15227 15229 return (B_FALSE);
15228 15230 }
15229 15231
15230 15232 /*
15231 15233 * Transport protocol call back function for CPU state change.
15232 15234 */
15233 15235 /* ARGSUSED */
15234 15236 static int
15235 15237 ip_tp_cpu_update(cpu_setup_t what, int id, void *arg)
15236 15238 {
15237 15239 processorid_t cpu_seqid;
15238 15240 netstack_handle_t nh;
15239 15241 netstack_t *ns;
15240 15242
15241 15243 ASSERT(MUTEX_HELD(&cpu_lock));
15242 15244
15243 15245 switch (what) {
15244 15246 case CPU_CONFIG:
15245 15247 case CPU_ON:
15246 15248 case CPU_INIT:
15247 15249 case CPU_CPUPART_IN:
15248 15250 cpu_seqid = cpu[id]->cpu_seqid;
15249 15251 netstack_next_init(&nh);
15250 15252 while ((ns = netstack_next(&nh)) != NULL) {
15251 15253 tcp_stack_cpu_add(ns->netstack_tcp, cpu_seqid);
15252 15254 sctp_stack_cpu_add(ns->netstack_sctp, cpu_seqid);
15253 15255 udp_stack_cpu_add(ns->netstack_udp, cpu_seqid);
15254 15256 netstack_rele(ns);
15255 15257 }
15256 15258 netstack_next_fini(&nh);
15257 15259 break;
15258 15260 case CPU_UNCONFIG:
15259 15261 case CPU_OFF:
15260 15262 case CPU_CPUPART_OUT:
15261 15263 /*
15262 15264 * Nothing to do. We don't remove the per CPU stats from
15263 15265 * the IP stack even when the CPU goes offline.
15264 15266 */
15265 15267 break;
15266 15268 default:
15267 15269 break;
15268 15270 }
15269 15271 return (0);
15270 15272 }
|
↓ open down ↓ |
14225 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX