Print this page
*** NO COMMENTS ***
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/uts/common/inet/ip/ip.c
+++ new/usr/src/uts/common/inet/ip/ip.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License (the "License").
6 6 * You may not use this file except in compliance with the License.
7 7 *
8 8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 9 * or http://www.opensolaris.org/os/licensing.
10 10 * See the License for the specific language governing permissions
11 11 * and limitations under the License.
12 12 *
13 13 * When distributing Covered Code, include this CDDL HEADER in each
14 14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 15 * If applicable, add the following below this CDDL HEADER, with the
16 16 * fields enclosed by brackets "[]" replaced with your own identifying
17 17 * information: Portions Copyright [yyyy] [name of copyright owner]
18 18 *
19 19 * CDDL HEADER END
20 20 */
21 21
22 22 /*
23 23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 24 * Use is subject to license terms.
25 25 */
26 26 /* Copyright (c) 1990 Mentat Inc. */
27 27
28 28 #include <sys/types.h>
29 29 #include <sys/stream.h>
30 30 #include <sys/dlpi.h>
31 31 #include <sys/stropts.h>
32 32 #include <sys/sysmacros.h>
33 33 #include <sys/strsubr.h>
34 34 #include <sys/strlog.h>
35 35 #include <sys/strsun.h>
36 36 #include <sys/zone.h>
37 37 #define _SUN_TPI_VERSION 2
38 38 #include <sys/tihdr.h>
39 39 #include <sys/xti_inet.h>
40 40 #include <sys/ddi.h>
41 41 #include <sys/sunddi.h>
42 42 #include <sys/cmn_err.h>
43 43 #include <sys/debug.h>
44 44 #include <sys/kobj.h>
45 45 #include <sys/modctl.h>
46 46 #include <sys/atomic.h>
47 47 #include <sys/policy.h>
48 48 #include <sys/priv.h>
49 49
50 50 #include <sys/systm.h>
51 51 #include <sys/param.h>
52 52 #include <sys/kmem.h>
53 53 #include <sys/sdt.h>
54 54 #include <sys/socket.h>
55 55 #include <sys/vtrace.h>
56 56 #include <sys/isa_defs.h>
57 57 #include <sys/mac.h>
58 58 #include <net/if.h>
59 59 #include <net/if_arp.h>
60 60 #include <net/route.h>
61 61 #include <sys/sockio.h>
62 62 #include <netinet/in.h>
63 63 #include <net/if_dl.h>
64 64
65 65 #include <inet/common.h>
66 66 #include <inet/mi.h>
67 67 #include <inet/mib2.h>
68 68 #include <inet/nd.h>
69 69 #include <inet/arp.h>
70 70 #include <inet/snmpcom.h>
71 71 #include <inet/optcom.h>
72 72 #include <inet/kstatcom.h>
73 73
74 74 #include <netinet/igmp_var.h>
75 75 #include <netinet/ip6.h>
76 76 #include <netinet/icmp6.h>
77 77 #include <netinet/sctp.h>
78 78
79 79 #include <inet/ip.h>
80 80 #include <inet/ip_impl.h>
81 81 #include <inet/ip6.h>
82 82 #include <inet/ip6_asp.h>
83 83 #include <inet/tcp.h>
84 84 #include <inet/tcp_impl.h>
85 85 #include <inet/ip_multi.h>
86 86 #include <inet/ip_if.h>
87 87 #include <inet/ip_ire.h>
88 88 #include <inet/ip_ftable.h>
89 89 #include <inet/ip_rts.h>
90 90 #include <inet/ip_ndp.h>
91 91 #include <inet/ip_listutils.h>
92 92 #include <netinet/igmp.h>
93 93 #include <netinet/ip_mroute.h>
94 94 #include <inet/ipp_common.h>
95 95
96 96 #include <net/pfkeyv2.h>
97 97 #include <inet/ipsec_info.h>
98 98 #include <inet/sadb.h>
99 99 #include <inet/ipsec_impl.h>
100 100 #include <sys/iphada.h>
101 101 #include <inet/tun.h>
102 102 #include <inet/ipdrop.h>
103 103 #include <inet/ip_netinfo.h>
104 104
105 105 #include <sys/ethernet.h>
106 106 #include <net/if_types.h>
107 107 #include <sys/cpuvar.h>
108 108
109 109 #include <ipp/ipp.h>
110 110 #include <ipp/ipp_impl.h>
111 111 #include <ipp/ipgpc/ipgpc.h>
112 112
113 113 #include <sys/multidata.h>
114 114 #include <sys/pattr.h>
115 115
116 116 #include <inet/ipclassifier.h>
117 117 #include <inet/sctp_ip.h>
118 118 #include <inet/sctp/sctp_impl.h>
119 119 #include <inet/udp_impl.h>
120 120 #include <inet/rawip_impl.h>
121 121 #include <inet/rts_impl.h>
122 122 #include <sys/sunddi.h>
123 123
124 124 #include <sys/tsol/label.h>
125 125 #include <sys/tsol/tnet.h>
126 126
127 127 #include <rpc/pmap_prot.h>
128 128
129 129 /*
130 130 * Values for squeue switch:
131 131 * IP_SQUEUE_ENTER_NODRAIN: squeue_enter_nodrain
132 132 * IP_SQUEUE_ENTER: squeue_enter
133 133 * IP_SQUEUE_FILL: squeue_fill
134 134 */
135 135 int ip_squeue_enter = 2; /* Setable in /etc/system */
136 136
137 137 squeue_func_t ip_input_proc;
138 138 #define SET_BPREV_FLAG(x) ((mblk_t *)(uintptr_t)(x))
139 139
140 140 /*
141 141 * Setable in /etc/system
142 142 */
143 143 int ip_poll_normal_ms = 100;
144 144 int ip_poll_normal_ticks = 0;
145 145 int ip_modclose_ackwait_ms = 3000;
146 146
147 147 /*
148 148 * It would be nice to have these present only in DEBUG systems, but the
149 149 * current design of the global symbol checking logic requires them to be
150 150 * unconditionally present.
151 151 */
152 152 uint_t ip_thread_data; /* TSD key for debug support */
153 153 krwlock_t ip_thread_rwlock;
154 154 list_t ip_thread_list;
155 155
156 156 /*
157 157 * Structure to represent a linked list of msgblks. Used by ip_snmp_ functions.
158 158 */
159 159
160 160 struct listptr_s {
161 161 mblk_t *lp_head; /* pointer to the head of the list */
162 162 mblk_t *lp_tail; /* pointer to the tail of the list */
163 163 };
164 164
165 165 typedef struct listptr_s listptr_t;
166 166
167 167 /*
168 168 * This is used by ip_snmp_get_mib2_ip_route_media and
169 169 * ip_snmp_get_mib2_ip6_route_media to carry the lists of return data.
170 170 */
171 171 typedef struct iproutedata_s {
172 172 uint_t ird_idx;
173 173 listptr_t ird_route; /* ipRouteEntryTable */
174 174 listptr_t ird_netmedia; /* ipNetToMediaEntryTable */
175 175 listptr_t ird_attrs; /* ipRouteAttributeTable */
176 176 } iproutedata_t;
177 177
178 178 /*
179 179 * Cluster specific hooks. These should be NULL when booted as a non-cluster
180 180 */
181 181
182 182 /*
183 183 * Hook functions to enable cluster networking
184 184 * On non-clustered systems these vectors must always be NULL.
185 185 *
186 186 * Hook function to Check ip specified ip address is a shared ip address
187 187 * in the cluster
188 188 *
189 189 */
190 190 int (*cl_inet_isclusterwide)(uint8_t protocol,
191 191 sa_family_t addr_family, uint8_t *laddrp) = NULL;
192 192
193 193 /*
194 194 * Hook function to generate cluster wide ip fragment identifier
195 195 */
196 196 uint32_t (*cl_inet_ipident)(uint8_t protocol, sa_family_t addr_family,
197 197 uint8_t *laddrp, uint8_t *faddrp) = NULL;
198 198
199 199 /*
200 200 * Synchronization notes:
201 201 *
202 202 * IP is a fully D_MP STREAMS module/driver. Thus it does not depend on any
203 203 * MT level protection given by STREAMS. IP uses a combination of its own
204 204 * internal serialization mechanism and standard Solaris locking techniques.
205 205 * The internal serialization is per phyint (no IPMP) or per IPMP group.
206 206 * This is used to serialize plumbing operations, IPMP operations, certain
207 207 * multicast operations, most set ioctls, igmp/mld timers etc.
208 208 *
209 209 * Plumbing is a long sequence of operations involving message
210 210 * exchanges between IP, ARP and device drivers. Many set ioctls are typically
211 211 * involved in plumbing operations. A natural model is to serialize these
212 212 * ioctls one per ill. For example plumbing of hme0 and qfe0 can go on in
213 213 * parallel without any interference. But various set ioctls on hme0 are best
214 214 * serialized. However if the system uses IPMP, the operations are easier if
215 215 * they are serialized on a per IPMP group basis since IPMP operations
216 216 * happen across ill's of a group. Thus the lowest common denominator is to
217 217 * serialize most set ioctls, multicast join/leave operations, IPMP operations
218 218 * igmp/mld timer operations, and processing of DLPI control messages received
219 219 * from drivers on a per IPMP group basis. If the system does not employ
220 220 * IPMP the serialization is on a per phyint basis. This serialization is
221 221 * provided by the ipsq_t and primitives operating on this. Details can
222 222 * be found in ip_if.c above the core primitives operating on ipsq_t.
223 223 *
224 224 * Lookups of an ipif or ill by a thread return a refheld ipif / ill.
225 225 * Simiarly lookup of an ire by a thread also returns a refheld ire.
226 226 * In addition ipif's and ill's referenced by the ire are also indirectly
227 227 * refheld. Thus no ipif or ill can vanish nor can critical parameters like
228 228 * the ipif's address or netmask change as long as an ipif is refheld
229 229 * directly or indirectly. For example an SIOCLIFADDR ioctl that changes the
230 230 * address of an ipif has to go through the ipsq_t. This ensures that only
231 231 * 1 such exclusive operation proceeds at any time on the ipif. It then
232 232 * deletes all ires associated with this ipif, and waits for all refcnts
233 233 * associated with this ipif to come down to zero. The address is changed
234 234 * only after the ipif has been quiesced. Then the ipif is brought up again.
235 235 * More details are described above the comment in ip_sioctl_flags.
236 236 *
237 237 * Packet processing is based mostly on IREs and are fully multi-threaded
238 238 * using standard Solaris MT techniques.
239 239 *
240 240 * There are explicit locks in IP to handle:
241 241 * - The ip_g_head list maintained by mi_open_link() and friends.
242 242 *
243 243 * - The reassembly data structures (one lock per hash bucket)
244 244 *
245 245 * - conn_lock is meant to protect conn_t fields. The fields actually
246 246 * protected by conn_lock are documented in the conn_t definition.
247 247 *
248 248 * - ire_lock to protect some of the fields of the ire, IRE tables
249 249 * (one lock per hash bucket). Refer to ip_ire.c for details.
250 250 *
251 251 * - ndp_g_lock and nce_lock for protecting NCEs.
252 252 *
253 253 * - ill_lock protects fields of the ill and ipif. Details in ip.h
254 254 *
255 255 * - ill_g_lock: This is a global reader/writer lock. Protects the following
256 256 * * The AVL tree based global multi list of all ills.
257 257 * * The linked list of all ipifs of an ill
258 258 * * The <ill-ipsq> mapping
259 259 * * The ipsq->ipsq_phyint_list threaded by phyint_ipsq_next
260 260 * * The illgroup list threaded by ill_group_next.
261 261 * * <ill-phyint> association
262 262 * Insertion/deletion of an ill in the system, insertion/deletion of an ipif
263 263 * into an ill, changing the <ill-ipsq> mapping of an ill, insertion/deletion
264 264 * of an ill into the illgrp list, changing the <ill-phyint> assoc of an ill
265 265 * will all have to hold the ill_g_lock as writer for the actual duration
266 266 * of the insertion/deletion/change. More details about the <ill-ipsq> mapping
267 267 * may be found in the IPMP section.
268 268 *
269 269 * - ill_lock: This is a per ill mutex.
270 270 * It protects some members of the ill and is documented below.
271 271 * It also protects the <ill-ipsq> mapping
272 272 * It also protects the illgroup list threaded by ill_group_next.
273 273 * It also protects the <ill-phyint> assoc.
274 274 * It also protects the list of ipifs hanging off the ill.
275 275 *
276 276 * - ipsq_lock: This is a per ipsq_t mutex lock.
277 277 * This protects all the other members of the ipsq struct except
278 278 * ipsq_refs and ipsq_phyint_list which are protected by ill_g_lock
279 279 *
280 280 * - illgrp_lock: This is a per ill_group mutex lock.
281 281 * The only thing it protects is the illgrp_ill_schednext member of ill_group
282 282 * which dictates which is the next ill in an ill_group that is to be chosen
283 283 * for sending outgoing packets, through creation of an IRE_CACHE that
284 284 * references this ill.
285 285 *
286 286 * - phyint_lock: This is a per phyint mutex lock. Protects just the
287 287 * phyint_flags
288 288 *
289 289 * - ip_g_nd_lock: This is a global reader/writer lock.
290 290 * Any call to nd_load to load a new parameter to the ND table must hold the
291 291 * lock as writer. ND_GET/ND_SET routines that read the ND table hold the lock
292 292 * as reader.
293 293 *
294 294 * - ip_addr_avail_lock: This is used to ensure the uniqueness of IP addresses.
295 295 * This lock is held in ipif_up_done and the ipif is marked IPIF_UP and the
296 296 * uniqueness check also done atomically.
297 297 *
298 298 * - ipsec_capab_ills_lock: This readers/writer lock protects the global
299 299 * lists of IPsec capable ills (ipsec_capab_ills_{ah,esp}). It is taken
300 300 * as a writer when adding or deleting elements from these lists, and
301 301 * as a reader when walking these lists to send a SADB update to the
302 302 * IPsec capable ills.
303 303 *
304 304 * - ill_g_usesrc_lock: This readers/writer lock protects the usesrc
305 305 * group list linked by ill_usesrc_grp_next. It also protects the
306 306 * ill_usesrc_ifindex field. It is taken as a writer when a member of the
307 307 * group is being added or deleted. This lock is taken as a reader when
308 308 * walking the list/group(eg: to get the number of members in a usesrc group).
309 309 * Note, it is only necessary to take this lock if the ill_usesrc_grp_next
310 310 * field is changing state i.e from NULL to non-NULL or vice-versa. For
311 311 * example, it is not necessary to take this lock in the initial portion
312 312 * of ip_sioctl_slifusesrc or at all in ip_sioctl_groupname and
313 313 * ip_sioctl_flags since the these operations are executed exclusively and
314 314 * that ensures that the "usesrc group state" cannot change. The "usesrc
315 315 * group state" change can happen only in the latter part of
316 316 * ip_sioctl_slifusesrc and in ill_delete.
317 317 *
318 318 * Changing <ill-phyint>, <ill-ipsq>, <ill-illgroup> assocications.
319 319 *
320 320 * To change the <ill-phyint> association, the ill_g_lock must be held
321 321 * as writer, and the ill_locks of both the v4 and v6 instance of the ill
322 322 * must be held.
323 323 *
324 324 * To change the <ill-ipsq> association the ill_g_lock must be held as writer
325 325 * and the ill_lock of the ill in question must be held.
326 326 *
327 327 * To change the <ill-illgroup> association the ill_g_lock must be held as
328 328 * writer and the ill_lock of the ill in question must be held.
329 329 *
330 330 * To add or delete an ipif from the list of ipifs hanging off the ill,
331 331 * ill_g_lock (writer) and ill_lock must be held and the thread must be
332 332 * a writer on the associated ipsq,.
333 333 *
334 334 * To add or delete an ill to the system, the ill_g_lock must be held as
335 335 * writer and the thread must be a writer on the associated ipsq.
336 336 *
337 337 * To add or delete an ilm to an ill, the ill_lock must be held and the thread
338 338 * must be a writer on the associated ipsq.
339 339 *
340 340 * Lock hierarchy
341 341 *
342 342 * Some lock hierarchy scenarios are listed below.
343 343 *
344 344 * ill_g_lock -> conn_lock -> ill_lock -> ipsq_lock
345 345 * ill_g_lock -> illgrp_lock -> ill_lock
346 346 * ill_g_lock -> ill_lock(s) -> phyint_lock
347 347 * ill_g_lock -> ndp_g_lock -> ill_lock -> nce_lock
348 348 * ill_g_lock -> ip_addr_avail_lock
349 349 * conn_lock -> irb_lock -> ill_lock -> ire_lock
350 350 * ill_g_lock -> ip_g_nd_lock
351 351 *
352 352 * When more than 1 ill lock is needed to be held, all ill lock addresses
353 353 * are sorted on address and locked starting from highest addressed lock
354 354 * downward.
355 355 *
356 356 * IPsec scenarios
357 357 *
358 358 * ipsa_lock -> ill_g_lock -> ill_lock
359 359 * ipsec_capab_ills_lock -> ill_g_lock -> ill_lock
360 360 * ipsec_capab_ills_lock -> ipsa_lock
361 361 * ill_g_usesrc_lock -> ill_g_lock -> ill_lock
362 362 *
363 363 * Trusted Solaris scenarios
364 364 *
365 365 * igsa_lock -> gcgrp_rwlock -> gcgrp_lock
366 366 * igsa_lock -> gcdb_lock
367 367 * gcgrp_rwlock -> ire_lock
368 368 * gcgrp_rwlock -> gcdb_lock
369 369 *
370 370 *
371 371 * Routing/forwarding table locking notes:
372 372 *
373 373 * Lock acquisition order: Radix tree lock, irb_lock.
374 374 * Requirements:
375 375 * i. Walker must not hold any locks during the walker callback.
376 376 * ii Walker must not see a truncated tree during the walk because of any node
377 377 * deletion.
378 378 * iii Existing code assumes ire_bucket is valid if it is non-null and is used
379 379 * in many places in the code to walk the irb list. Thus even if all the
380 380 * ires in a bucket have been deleted, we still can't free the radix node
381 381 * until the ires have actually been inactive'd (freed).
382 382 *
383 383 * Tree traversal - Need to hold the global tree lock in read mode.
384 384 * Before dropping the global tree lock, need to either increment the ire_refcnt
385 385 * to ensure that the radix node can't be deleted.
386 386 *
387 387 * Tree add - Need to hold the global tree lock in write mode to add a
388 388 * radix node. To prevent the node from being deleted, increment the
389 389 * irb_refcnt, after the node is added to the tree. The ire itself is
390 390 * added later while holding the irb_lock, but not the tree lock.
391 391 *
392 392 * Tree delete - Need to hold the global tree lock and irb_lock in write mode.
393 393 * All associated ires must be inactive (i.e. freed), and irb_refcnt
394 394 * must be zero.
395 395 *
396 396 * Walker - Increment irb_refcnt before calling the walker callback. Hold the
397 397 * global tree lock (read mode) for traversal.
398 398 *
399 399 * IPsec notes :
400 400 *
401 401 * IP interacts with the IPsec code (AH/ESP) by tagging a M_CTL message
402 402 * in front of the actual packet. For outbound datagrams, the M_CTL
403 403 * contains a ipsec_out_t (defined in ipsec_info.h), which has the
404 404 * information used by the IPsec code for applying the right level of
405 405 * protection. The information initialized by IP in the ipsec_out_t
406 406 * is determined by the per-socket policy or global policy in the system.
407 407 * For inbound datagrams, the M_CTL contains a ipsec_in_t (defined in
408 408 * ipsec_info.h) which starts out with nothing in it. It gets filled
409 409 * with the right information if it goes through the AH/ESP code, which
410 410 * happens if the incoming packet is secure. The information initialized
411 411 * by AH/ESP, is later used by IP(during fanouts to ULP) to see whether
412 412 * the policy requirements needed by per-socket policy or global policy
413 413 * is met or not.
414 414 *
415 415 * If there is both per-socket policy (set using setsockopt) and there
416 416 * is also global policy match for the 5 tuples of the socket,
417 417 * ipsec_override_policy() makes the decision of which one to use.
418 418 *
419 419 * For fully connected sockets i.e dst, src [addr, port] is known,
420 420 * conn_policy_cached is set indicating that policy has been cached.
421 421 * conn_in_enforce_policy may or may not be set depending on whether
422 422 * there is a global policy match or per-socket policy match.
423 423 * Policy inheriting happpens in ip_bind during the ipa_conn_t bind.
424 424 * Once the right policy is set on the conn_t, policy cannot change for
425 425 * this socket. This makes life simpler for TCP (UDP ?) where
426 426 * re-transmissions go out with the same policy. For symmetry, policy
427 427 * is cached for fully connected UDP sockets also. Thus if policy is cached,
428 428 * it also implies that policy is latched i.e policy cannot change
429 429 * on these sockets. As we have the right policy on the conn, we don't
430 430 * have to lookup global policy for every outbound and inbound datagram
431 431 * and thus serving as an optimization. Note that a global policy change
432 432 * does not affect fully connected sockets if they have policy. If fully
433 433 * connected sockets did not have any policy associated with it, global
434 434 * policy change may affect them.
435 435 *
436 436 * IP Flow control notes:
437 437 *
438 438 * Non-TCP streams are flow controlled by IP. On the send side, if the packet
439 439 * cannot be sent down to the driver by IP, because of a canput failure, IP
440 440 * does a putq on the conn_wq. This will cause ip_wsrv to run on the conn_wq.
441 441 * ip_wsrv in turn, inserts the conn in a list of conn's that need to be drained
442 442 * when the flowcontrol condition subsides. Ultimately STREAMS backenables the
443 443 * ip_wsrv on the IP module, which in turn does a qenable of the conn_wq of the
444 444 * first conn in the list of conn's to be drained. ip_wsrv on this conn drains
445 445 * the queued messages, and removes the conn from the drain list, if all
446 446 * messages were drained. It also qenables the next conn in the drain list to
447 447 * continue the drain process.
448 448 *
449 449 * In reality the drain list is not a single list, but a configurable number
450 450 * of lists. The ip_wsrv on the IP module, qenables the first conn in each
451 451 * list. If the ip_wsrv of the next qenabled conn does not run, because the
452 452 * stream closes, ip_close takes responsibility to qenable the next conn in
453 453 * the drain list. The directly called ip_wput path always does a putq, if
454 454 * it cannot putnext. Thus synchronization problems are handled between
455 455 * ip_wsrv and ip_close. conn_drain_insert and conn_drain_tail are the only
456 456 * functions that manipulate this drain list. Furthermore conn_drain_insert
457 457 * is called only from ip_wsrv, and there can be only 1 instance of ip_wsrv
458 458 * running on a queue at any time. conn_drain_tail can be simultaneously called
459 459 * from both ip_wsrv and ip_close.
460 460 *
461 461 * IPQOS notes:
462 462 *
463 463 * IPQoS Policies are applied to packets using IPPF (IP Policy framework)
464 464 * and IPQoS modules. IPPF includes hooks in IP at different control points
465 465 * (callout positions) which direct packets to IPQoS modules for policy
466 466 * processing. Policies, if present, are global.
467 467 *
468 468 * The callout positions are located in the following paths:
469 469 * o local_in (packets destined for this host)
470 470 * o local_out (packets orginating from this host )
471 471 * o fwd_in (packets forwarded by this m/c - inbound)
472 472 * o fwd_out (packets forwarded by this m/c - outbound)
473 473 * Hooks at these callout points can be enabled/disabled using the ndd variable
474 474 * ip_policy_mask (a bit mask with the 4 LSB indicating the callout positions).
475 475 * By default all the callout positions are enabled.
476 476 *
477 477 * Outbound (local_out)
478 478 * Hooks are placed in ip_wput_ire and ipsec_out_process.
479 479 *
480 480 * Inbound (local_in)
481 481 * Hooks are placed in ip_proto_input, icmp_inbound, ip_fanout_proto and
482 482 * TCP and UDP fanout routines.
483 483 *
484 484 * Forwarding (in and out)
485 485 * Hooks are placed in ip_rput_forward.
486 486 *
487 487 * IP Policy Framework processing (IPPF processing)
488 488 * Policy processing for a packet is initiated by ip_process, which ascertains
489 489 * that the classifier (ipgpc) is loaded and configured, failing which the
490 490 * packet resumes normal processing in IP. If the clasifier is present, the
491 491 * packet is acted upon by one or more IPQoS modules (action instances), per
492 492 * filters configured in ipgpc and resumes normal IP processing thereafter.
493 493 * An action instance can drop a packet in course of its processing.
494 494 *
495 495 * A boolean variable, ip_policy, is used in all the fanout routines that can
496 496 * invoke ip_process for a packet. This variable indicates if the packet should
497 497 * to be sent for policy processing. The variable is set to B_TRUE by default,
498 498 * i.e. when the routines are invoked in the normal ip procesing path for a
499 499 * packet. The two exceptions being ip_wput_local and icmp_inbound_error_fanout;
500 500 * ip_policy is set to B_FALSE for all the routines called in these two
501 501 * functions because, in the former case, we don't process loopback traffic
502 502 * currently while in the latter, the packets have already been processed in
503 503 * icmp_inbound.
504 504 *
505 505 * Zones notes:
506 506 *
507 507 * The partitioning rules for networking are as follows:
508 508 * 1) Packets coming from a zone must have a source address belonging to that
509 509 * zone.
510 510 * 2) Packets coming from a zone can only be sent on a physical interface on
511 511 * which the zone has an IP address.
512 512 * 3) Between two zones on the same machine, packet delivery is only allowed if
513 513 * there's a matching route for the destination and zone in the forwarding
514 514 * table.
515 515 * 4) The TCP and UDP port spaces are per-zone; that is, two processes in
516 516 * different zones can bind to the same port with the wildcard address
517 517 * (INADDR_ANY).
518 518 *
519 519 * The granularity of interface partitioning is at the logical interface level.
520 520 * Therefore, every zone has its own IP addresses, and incoming packets can be
521 521 * attributed to a zone unambiguously. A logical interface is placed into a zone
522 522 * using the SIOCSLIFZONE ioctl; this sets the ipif_zoneid field in the ipif_t
523 523 * structure. Rule (1) is implemented by modifying the source address selection
524 524 * algorithm so that the list of eligible addresses is filtered based on the
525 525 * sending process zone.
526 526 *
527 527 * The Internet Routing Entries (IREs) are either exclusive to a zone or shared
528 528 * across all zones, depending on their type. Here is the break-up:
529 529 *
530 530 * IRE type Shared/exclusive
531 531 * -------- ----------------
532 532 * IRE_BROADCAST Exclusive
533 533 * IRE_DEFAULT (default routes) Shared (*)
534 534 * IRE_LOCAL Exclusive (x)
535 535 * IRE_LOOPBACK Exclusive
536 536 * IRE_PREFIX (net routes) Shared (*)
537 537 * IRE_CACHE Exclusive
538 538 * IRE_IF_NORESOLVER (interface routes) Exclusive
539 539 * IRE_IF_RESOLVER (interface routes) Exclusive
540 540 * IRE_HOST (host routes) Shared (*)
541 541 *
542 542 * (*) A zone can only use a default or off-subnet route if the gateway is
543 543 * directly reachable from the zone, that is, if the gateway's address matches
544 544 * one of the zone's logical interfaces.
545 545 *
546 546 * (x) IRE_LOCAL are handled a bit differently, since for all other entries
547 547 * in ire_ctable and IRE_INTERFACE, ire_src_addr is what can be used as source
548 548 * when sending packets using the IRE. For IRE_LOCAL ire_src_addr is the IP
549 549 * address of the zone itself (the destination). Since IRE_LOCAL is used
550 550 * for communication between zones, ip_wput_ire has special logic to set
551 551 * the right source address when sending using an IRE_LOCAL.
552 552 *
553 553 * Furthermore, when ip_restrict_interzone_loopback is set (the default),
554 554 * ire_cache_lookup restricts loopback using an IRE_LOCAL
555 555 * between zone to the case when L2 would have conceptually looped the packet
556 556 * back, i.e. the loopback which is required since neither Ethernet drivers
557 557 * nor Ethernet hardware loops them back. This is the case when the normal
558 558 * routes (ignoring IREs with different zoneids) would send out the packet on
559 559 * the same ill (or ill group) as the ill with which is IRE_LOCAL is
560 560 * associated.
561 561 *
562 562 * Multiple zones can share a common broadcast address; typically all zones
563 563 * share the 255.255.255.255 address. Incoming as well as locally originated
564 564 * broadcast packets must be dispatched to all the zones on the broadcast
565 565 * network. For directed broadcasts (e.g. 10.16.72.255) this is not trivial
566 566 * since some zones may not be on the 10.16.72/24 network. To handle this, each
567 567 * zone has its own set of IRE_BROADCAST entries; then, broadcast packets are
568 568 * sent to every zone that has an IRE_BROADCAST entry for the destination
569 569 * address on the input ill, see conn_wantpacket().
570 570 *
571 571 * Applications in different zones can join the same multicast group address.
572 572 * For IPv4, group memberships are per-logical interface, so they're already
573 573 * inherently part of a zone. For IPv6, group memberships are per-physical
574 574 * interface, so we distinguish IPv6 group memberships based on group address,
575 575 * interface and zoneid. In both cases, received multicast packets are sent to
576 576 * every zone for which a group membership entry exists. On IPv6 we need to
577 577 * check that the target zone still has an address on the receiving physical
578 578 * interface; it could have been removed since the application issued the
579 579 * IPV6_JOIN_GROUP.
580 580 */
581 581
582 582 /*
583 583 * Squeue Fanout flags:
584 584 * 0: No fanout.
585 585 * 1: Fanout across all squeues
586 586 */
587 587 boolean_t ip_squeue_fanout = 0;
588 588
589 589 /*
590 590 * Maximum dups allowed per packet.
591 591 */
592 592 uint_t ip_max_frag_dups = 10;
593 593
594 594 #define IS_SIMPLE_IPH(ipha) \
595 595 ((ipha)->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION)
596 596
597 597 /* RFC1122 Conformance */
598 598 #define IP_FORWARD_DEFAULT IP_FORWARD_NEVER
599 599
600 600 #define ILL_MAX_NAMELEN LIFNAMSIZ
601 601
602 602 static int conn_set_held_ipif(conn_t *, ipif_t **, ipif_t *);
603 603
604 604 static int ip_open(queue_t *q, dev_t *devp, int flag, int sflag,
605 605 cred_t *credp, boolean_t isv6);
606 606 static mblk_t *ip_wput_attach_llhdr(mblk_t *, ire_t *, ip_proc_t, uint32_t,
607 607 ipha_t **);
608 608
609 609 static void icmp_frag_needed(queue_t *, mblk_t *, int, zoneid_t,
610 610 ip_stack_t *);
611 611 static void icmp_inbound(queue_t *, mblk_t *, boolean_t, ill_t *, int,
612 612 uint32_t, boolean_t, boolean_t, ill_t *, zoneid_t);
613 613 static ipaddr_t icmp_get_nexthop_addr(ipha_t *, ill_t *, zoneid_t, mblk_t *mp);
614 614 static boolean_t icmp_inbound_too_big(icmph_t *, ipha_t *, ill_t *, zoneid_t,
615 615 mblk_t *, int, ip_stack_t *);
616 616 static void icmp_inbound_error_fanout(queue_t *, ill_t *, mblk_t *,
617 617 icmph_t *, ipha_t *, int, int, boolean_t, boolean_t,
618 618 ill_t *, zoneid_t);
619 619 static void icmp_options_update(ipha_t *);
620 620 static void icmp_param_problem(queue_t *, mblk_t *, uint8_t, zoneid_t,
621 621 ip_stack_t *);
622 622 static void icmp_pkt(queue_t *, mblk_t *, void *, size_t, boolean_t,
623 623 zoneid_t zoneid, ip_stack_t *);
624 624 static mblk_t *icmp_pkt_err_ok(mblk_t *, ip_stack_t *);
625 625 static void icmp_redirect(ill_t *, mblk_t *);
626 626 static void icmp_send_redirect(queue_t *, mblk_t *, ipaddr_t,
627 627 ip_stack_t *);
628 628
629 629 static void ip_arp_news(queue_t *, mblk_t *);
630 630 static boolean_t ip_bind_insert_ire(mblk_t *, ire_t *, iulp_t *,
631 631 ip_stack_t *);
632 632 mblk_t *ip_dlpi_alloc(size_t, t_uscalar_t);
633 633 char *ip_dot_addr(ipaddr_t, char *);
634 634 mblk_t *ip_carve_mp(mblk_t **, ssize_t);
635 635 int ip_close(queue_t *, int);
636 636 static char *ip_dot_saddr(uchar_t *, char *);
637 637 static void ip_fanout_proto(queue_t *, mblk_t *, ill_t *, ipha_t *, uint_t,
638 638 boolean_t, boolean_t, ill_t *, zoneid_t);
639 639 static void ip_fanout_tcp(queue_t *, mblk_t *, ill_t *, ipha_t *, uint_t,
640 640 boolean_t, boolean_t, zoneid_t);
641 641 static void ip_fanout_udp(queue_t *, mblk_t *, ill_t *, ipha_t *, uint32_t,
642 642 boolean_t, uint_t, boolean_t, boolean_t, ill_t *, zoneid_t);
643 643 static void ip_lrput(queue_t *, mblk_t *);
644 644 ipaddr_t ip_net_mask(ipaddr_t);
645 645 void ip_newroute(queue_t *, mblk_t *, ipaddr_t, conn_t *, zoneid_t,
646 646 ip_stack_t *);
647 647 static void ip_newroute_ipif(queue_t *, mblk_t *, ipif_t *, ipaddr_t,
648 648 conn_t *, uint32_t, zoneid_t, ip_opt_info_t *);
649 649 char *ip_nv_lookup(nv_t *, int);
650 650 static boolean_t ip_check_for_ipsec_opt(queue_t *, mblk_t *);
651 651 static int ip_param_get(queue_t *, mblk_t *, caddr_t, cred_t *);
652 652 static int ip_param_generic_get(queue_t *, mblk_t *, caddr_t, cred_t *);
653 653 static boolean_t ip_param_register(IDP *ndp, ipparam_t *, size_t,
654 654 ipndp_t *, size_t);
655 655 static int ip_param_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *);
656 656 void ip_rput(queue_t *, mblk_t *);
657 657 static void ip_rput_dlpi_writer(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp,
658 658 void *dummy_arg);
659 659 void ip_rput_forward(ire_t *, ipha_t *, mblk_t *, ill_t *);
660 660 static int ip_rput_forward_options(mblk_t *, ipha_t *, ire_t *,
661 661 ip_stack_t *);
662 662 static boolean_t ip_rput_local_options(queue_t *, mblk_t *, ipha_t *,
663 663 ire_t *, ip_stack_t *);
664 664 static boolean_t ip_rput_multimblk_ipoptions(queue_t *, ill_t *,
665 665 mblk_t *, ipha_t **, ipaddr_t *, ip_stack_t *);
666 666 static int ip_rput_options(queue_t *, mblk_t *, ipha_t *, ipaddr_t *,
667 667 ip_stack_t *);
668 668 static boolean_t ip_rput_fragment(queue_t *, mblk_t **, ipha_t *, uint32_t *,
669 669 uint16_t *);
670 670 int ip_snmp_get(queue_t *, mblk_t *, int);
671 671 static mblk_t *ip_snmp_get_mib2_ip(queue_t *, mblk_t *,
672 672 mib2_ipIfStatsEntry_t *, ip_stack_t *);
673 673 static mblk_t *ip_snmp_get_mib2_ip_traffic_stats(queue_t *, mblk_t *,
674 674 ip_stack_t *);
675 675 static mblk_t *ip_snmp_get_mib2_ip6(queue_t *, mblk_t *, ip_stack_t *);
676 676 static mblk_t *ip_snmp_get_mib2_icmp(queue_t *, mblk_t *, ip_stack_t *ipst);
677 677 static mblk_t *ip_snmp_get_mib2_icmp6(queue_t *, mblk_t *, ip_stack_t *ipst);
678 678 static mblk_t *ip_snmp_get_mib2_igmp(queue_t *, mblk_t *, ip_stack_t *ipst);
679 679 static mblk_t *ip_snmp_get_mib2_multi(queue_t *, mblk_t *, ip_stack_t *ipst);
680 680 static mblk_t *ip_snmp_get_mib2_ip_addr(queue_t *, mblk_t *,
681 681 ip_stack_t *ipst);
682 682 static mblk_t *ip_snmp_get_mib2_ip6_addr(queue_t *, mblk_t *,
683 683 ip_stack_t *ipst);
684 684 static mblk_t *ip_snmp_get_mib2_ip_group_src(queue_t *, mblk_t *,
685 685 ip_stack_t *ipst);
686 686 static mblk_t *ip_snmp_get_mib2_ip6_group_src(queue_t *, mblk_t *,
687 687 ip_stack_t *ipst);
688 688 static mblk_t *ip_snmp_get_mib2_ip_group_mem(queue_t *, mblk_t *,
689 689 ip_stack_t *ipst);
690 690 static mblk_t *ip_snmp_get_mib2_ip6_group_mem(queue_t *, mblk_t *,
691 691 ip_stack_t *ipst);
692 692 static mblk_t *ip_snmp_get_mib2_virt_multi(queue_t *, mblk_t *,
693 693 ip_stack_t *ipst);
694 694 static mblk_t *ip_snmp_get_mib2_multi_rtable(queue_t *, mblk_t *,
695 695 ip_stack_t *ipst);
696 696 static mblk_t *ip_snmp_get_mib2_ip_route_media(queue_t *, mblk_t *,
697 697 ip_stack_t *ipst);
698 698 static mblk_t *ip_snmp_get_mib2_ip6_route_media(queue_t *, mblk_t *,
699 699 ip_stack_t *ipst);
700 700 static void ip_snmp_get2_v4(ire_t *, iproutedata_t *);
701 701 static void ip_snmp_get2_v6_route(ire_t *, iproutedata_t *);
702 702 static int ip_snmp_get2_v6_media(nce_t *, iproutedata_t *);
703 703 int ip_snmp_set(queue_t *, int, int, uchar_t *, int);
704 704 static boolean_t ip_source_routed(ipha_t *, ip_stack_t *);
705 705 static boolean_t ip_source_route_included(ipha_t *);
706 706 static void ip_trash_ire_reclaim_stack(ip_stack_t *);
707 707
708 708 static void ip_wput_frag(ire_t *, mblk_t *, ip_pkt_t, uint32_t, uint32_t,
709 709 zoneid_t, ip_stack_t *);
710 710 static mblk_t *ip_wput_frag_copyhdr(uchar_t *, int, int, ip_stack_t *);
711 711 static void ip_wput_local_options(ipha_t *, ip_stack_t *);
712 712 static int ip_wput_options(queue_t *, mblk_t *, ipha_t *, boolean_t,
713 713 zoneid_t, ip_stack_t *);
714 714
715 715 static void conn_drain_init(ip_stack_t *);
716 716 static void conn_drain_fini(ip_stack_t *);
717 717 static void conn_drain_tail(conn_t *connp, boolean_t closing);
718 718
719 719 static void conn_walk_drain(ip_stack_t *);
720 720 static void conn_walk_fanout_table(connf_t *, uint_t, pfv_t, void *,
721 721 zoneid_t);
722 722
723 723 static void *ip_stack_init(netstackid_t stackid, netstack_t *ns);
724 724 static void ip_stack_shutdown(netstackid_t stackid, void *arg);
725 725 static void ip_stack_fini(netstackid_t stackid, void *arg);
726 726
727 727 static boolean_t conn_wantpacket(conn_t *, ill_t *, ipha_t *, int,
728 728 zoneid_t);
729 729 static void ip_arp_done(ipsq_t *dummy_sq, queue_t *q, mblk_t *mp,
730 730 void *dummy_arg);
731 731
732 732 static int ip_forward_set(queue_t *, mblk_t *, char *, caddr_t, cred_t *);
733 733
734 734 static int ip_multirt_apply_membership(int (*fn)(conn_t *, boolean_t,
735 735 ipaddr_t, ipaddr_t, uint_t *, mcast_record_t, ipaddr_t, mblk_t *), ire_t *,
736 736 conn_t *, boolean_t, ipaddr_t, mcast_record_t, ipaddr_t, mblk_t *);
737 737 static void ip_multirt_bad_mtu(ire_t *, uint32_t);
738 738
739 739 static int ip_cgtp_filter_get(queue_t *, mblk_t *, caddr_t, cred_t *);
740 740 static int ip_cgtp_filter_set(queue_t *, mblk_t *, char *,
741 741 caddr_t, cred_t *);
742 742 extern int ip_squeue_bind_set(queue_t *q, mblk_t *mp, char *value,
743 743 caddr_t cp, cred_t *cr);
744 744 extern int ip_squeue_profile_set(queue_t *, mblk_t *, char *, caddr_t,
745 745 cred_t *);
746 746 static int ip_input_proc_set(queue_t *q, mblk_t *mp, char *value,
747 747 caddr_t cp, cred_t *cr);
748 748 static int ip_int_set(queue_t *, mblk_t *, char *, caddr_t,
749 749 cred_t *);
750 750 static int ipmp_hook_emulation_set(queue_t *, mblk_t *, char *, caddr_t,
751 751 cred_t *);
752 752 static squeue_func_t ip_squeue_switch(int);
753 753
754 754 static void *ip_kstat_init(netstackid_t, ip_stack_t *);
755 755 static void ip_kstat_fini(netstackid_t, kstat_t *);
756 756 static int ip_kstat_update(kstat_t *kp, int rw);
757 757 static void *icmp_kstat_init(netstackid_t);
758 758 static void icmp_kstat_fini(netstackid_t, kstat_t *);
759 759 static int icmp_kstat_update(kstat_t *kp, int rw);
760 760 static void *ip_kstat2_init(netstackid_t, ip_stat_t *);
761 761 static void ip_kstat2_fini(netstackid_t, kstat_t *);
762 762
763 763 static int ip_conn_report(queue_t *, mblk_t *, caddr_t, cred_t *);
764 764
765 765 static mblk_t *ip_tcp_input(mblk_t *, ipha_t *, ill_t *, boolean_t,
766 766 ire_t *, mblk_t *, uint_t, queue_t *, ill_rx_ring_t *);
767 767
768 768 static void ip_rput_process_forward(queue_t *, mblk_t *, ire_t *,
769 769 ipha_t *, ill_t *, boolean_t);
770 770 ipaddr_t ip_g_all_ones = IP_HOST_MASK;
771 771
772 772 /* How long, in seconds, we allow frags to hang around. */
773 773 #define IP_FRAG_TIMEOUT 60
774 774
775 775 /*
776 776 * Threshold which determines whether MDT should be used when
777 777 * generating IP fragments; payload size must be greater than
778 778 * this threshold for MDT to take place.
779 779 */
780 780 #define IP_WPUT_FRAG_MDT_MIN 32768
781 781
782 782 /* Setable in /etc/system only */
783 783 int ip_wput_frag_mdt_min = IP_WPUT_FRAG_MDT_MIN;
784 784
785 785 static long ip_rput_pullups;
786 786 int dohwcksum = 1; /* use h/w cksum if supported by the hardware */
787 787
788 788 vmem_t *ip_minor_arena_sa; /* for minor nos. from INET_MIN_DEV+2 thru 2^^18-1 */
789 789 vmem_t *ip_minor_arena_la; /* for minor nos. from 2^^18 thru 2^^32-1 */
790 790
791 791 int ip_debug;
792 792
793 793 #ifdef DEBUG
794 794 uint32_t ipsechw_debug = 0;
795 795 #endif
796 796
797 797 /*
798 798 * Multirouting/CGTP stuff
799 799 */
800 800 int ip_cgtp_filter_rev = CGTP_FILTER_REV; /* CGTP hooks version */
801 801
802 802 /*
803 803 * XXX following really should only be in a header. Would need more
804 804 * header and .c clean up first.
805 805 */
806 806 extern optdb_obj_t ip_opt_obj;
807 807
808 808 ulong_t ip_squeue_enter_unbound = 0;
809 809
810 810 /*
811 811 * Named Dispatch Parameter Table.
812 812 * All of these are alterable, within the min/max values given, at run time.
813 813 */
814 814 static ipparam_t lcl_param_arr[] = {
815 815 /* min max value name */
816 816 { 0, 1, 0, "ip_respond_to_address_mask_broadcast"},
817 817 { 0, 1, 1, "ip_respond_to_echo_broadcast"},
818 818 { 0, 1, 1, "ip_respond_to_echo_multicast"},
819 819 { 0, 1, 0, "ip_respond_to_timestamp"},
820 820 { 0, 1, 0, "ip_respond_to_timestamp_broadcast"},
821 821 { 0, 1, 1, "ip_send_redirects"},
822 822 { 0, 1, 0, "ip_forward_directed_broadcasts"},
823 823 { 0, 10, 0, "ip_mrtdebug"},
824 824 { 5000, 999999999, 60000, "ip_ire_timer_interval" },
825 825 { 60000, 999999999, 1200000, "ip_ire_arp_interval" },
826 826 { 60000, 999999999, 60000, "ip_ire_redirect_interval" },
827 827 { 1, 255, 255, "ip_def_ttl" },
828 828 { 0, 1, 0, "ip_forward_src_routed"},
829 829 { 0, 256, 32, "ip_wroff_extra" },
830 830 { 5000, 999999999, 600000, "ip_ire_pathmtu_interval" },
831 831 { 8, 65536, 64, "ip_icmp_return_data_bytes" },
832 832 { 0, 1, 1, "ip_path_mtu_discovery" },
833 833 { 0, 240, 30, "ip_ignore_delete_time" },
834 834 { 0, 1, 0, "ip_ignore_redirect" },
835 835 { 0, 1, 1, "ip_output_queue" },
836 836 { 1, 254, 1, "ip_broadcast_ttl" },
837 837 { 0, 99999, 100, "ip_icmp_err_interval" },
838 838 { 1, 99999, 10, "ip_icmp_err_burst" },
839 839 { 0, 999999999, 1000000, "ip_reass_queue_bytes" },
840 840 { 0, 1, 0, "ip_strict_dst_multihoming" },
841 841 { 1, MAX_ADDRS_PER_IF, 256, "ip_addrs_per_if"},
842 842 { 0, 1, 0, "ipsec_override_persocket_policy" },
843 843 { 0, 1, 1, "icmp_accept_clear_messages" },
844 844 { 0, 1, 1, "igmp_accept_clear_messages" },
845 845 { 2, 999999999, ND_DELAY_FIRST_PROBE_TIME,
846 846 "ip_ndp_delay_first_probe_time"},
847 847 { 1, 999999999, ND_MAX_UNICAST_SOLICIT,
848 848 "ip_ndp_max_unicast_solicit"},
849 849 { 1, 255, IPV6_MAX_HOPS, "ip6_def_hops" },
850 850 { 8, IPV6_MIN_MTU, IPV6_MIN_MTU, "ip6_icmp_return_data_bytes" },
851 851 { 0, 1, 0, "ip6_forward_src_routed"},
852 852 { 0, 1, 1, "ip6_respond_to_echo_multicast"},
853 853 { 0, 1, 1, "ip6_send_redirects"},
854 854 { 0, 1, 0, "ip6_ignore_redirect" },
855 855 { 0, 1, 0, "ip6_strict_dst_multihoming" },
856 856
857 857 { 1, 8, 3, "ip_ire_reclaim_fraction" },
858 858
859 859 { 0, 999999, 1000, "ipsec_policy_log_interval" },
860 860
861 861 { 0, 1, 1, "pim_accept_clear_messages" },
862 862 { 1000, 20000, 2000, "ip_ndp_unsolicit_interval" },
863 863 { 1, 20, 3, "ip_ndp_unsolicit_count" },
864 864 { 0, 1, 1, "ip6_ignore_home_address_opt" },
865 865 { 0, 15, 0, "ip_policy_mask" },
866 866 { 1000, 60000, 1000, "ip_multirt_resolution_interval" },
867 867 { 0, 255, 1, "ip_multirt_ttl" },
868 868 { 0, 1, 1, "ip_multidata_outbound" },
869 869 { 0, 3600000, 300000, "ip_ndp_defense_interval" },
870 870 { 0, 999999, 60*60*24, "ip_max_temp_idle" },
871 871 { 0, 1000, 1, "ip_max_temp_defend" },
872 872 { 0, 1000, 3, "ip_max_defend" },
873 873 { 0, 999999, 30, "ip_defend_interval" },
874 874 { 0, 3600000, 300000, "ip_dup_recovery" },
875 875 { 0, 1, 1, "ip_restrict_interzone_loopback" },
876 876 { 0, 1, 1, "ip_lso_outbound" },
877 877 { IGMP_V1_ROUTER, IGMP_V3_ROUTER, IGMP_V3_ROUTER, "igmp_max_version" },
878 878 { MLD_V1_ROUTER, MLD_V2_ROUTER, MLD_V2_ROUTER, "mld_max_version" },
879 879 #ifdef DEBUG
880 880 { 0, 1, 0, "ip6_drop_inbound_icmpv6" },
881 881 #else
882 882 { 0, 0, 0, "" },
883 883 #endif
884 884 };
885 885
886 886 /*
887 887 * Extended NDP table
888 888 * The addresses for the first two are filled in to be ips_ip_g_forward
889 889 * and ips_ipv6_forward at init time.
890 890 */
891 891 static ipndp_t lcl_ndp_arr[] = {
892 892 /* getf setf data name */
893 893 #define IPNDP_IP_FORWARDING_OFFSET 0
894 894 { ip_param_generic_get, ip_forward_set, NULL,
895 895 "ip_forwarding" },
896 896 #define IPNDP_IP6_FORWARDING_OFFSET 1
897 897 { ip_param_generic_get, ip_forward_set, NULL,
898 898 "ip6_forwarding" },
899 899 { ip_ill_report, NULL, NULL,
900 900 "ip_ill_status" },
901 901 { ip_ipif_report, NULL, NULL,
902 902 "ip_ipif_status" },
903 903 { ip_conn_report, NULL, NULL,
904 904 "ip_conn_status" },
905 905 { nd_get_long, nd_set_long, (caddr_t)&ip_rput_pullups,
906 906 "ip_rput_pullups" },
907 907 { ip_srcid_report, NULL, NULL,
908 908 "ip_srcid_status" },
909 909 { ip_param_generic_get, ip_squeue_profile_set,
910 910 (caddr_t)&ip_squeue_profile, "ip_squeue_profile" },
911 911 { ip_param_generic_get, ip_squeue_bind_set,
912 912 (caddr_t)&ip_squeue_bind, "ip_squeue_bind" },
913 913 { ip_param_generic_get, ip_input_proc_set,
914 914 (caddr_t)&ip_squeue_enter, "ip_squeue_enter" },
915 915 { ip_param_generic_get, ip_int_set,
916 916 (caddr_t)&ip_squeue_fanout, "ip_squeue_fanout" },
917 917 #define IPNDP_CGTP_FILTER_OFFSET 11
918 918 { ip_cgtp_filter_get, ip_cgtp_filter_set, NULL,
919 919 "ip_cgtp_filter" },
920 920 { ip_param_generic_get, ip_int_set,
921 921 (caddr_t)&ip_soft_rings_cnt, "ip_soft_rings_cnt" },
922 922 #define IPNDP_IPMP_HOOK_OFFSET 13
923 923 { ip_param_generic_get, ipmp_hook_emulation_set, NULL,
924 924 "ipmp_hook_emulation" },
925 925 { ip_param_generic_get, ip_int_set, (caddr_t)&ip_debug,
926 926 "ip_debug" },
927 927 };
928 928
929 929 /*
930 930 * Table of IP ioctls encoding the various properties of the ioctl and
931 931 * indexed based on the last byte of the ioctl command. Occasionally there
932 932 * is a clash, and there is more than 1 ioctl with the same last byte.
933 933 * In such a case 1 ioctl is encoded in the ndx table and the remaining
934 934 * ioctls are encoded in the misc table. An entry in the ndx table is
935 935 * retrieved by indexing on the last byte of the ioctl command and comparing
936 936 * the ioctl command with the value in the ndx table. In the event of a
937 937 * mismatch the misc table is then searched sequentially for the desired
938 938 * ioctl command.
939 939 *
940 940 * Entry: <command> <copyin_size> <flags> <cmd_type> <function> <restart_func>
941 941 */
942 942 ip_ioctl_cmd_t ip_ndx_ioctl_table[] = {
943 943 /* 000 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
944 944 /* 001 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
945 945 /* 002 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
946 946 /* 003 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
947 947 /* 004 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
948 948 /* 005 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
949 949 /* 006 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
950 950 /* 007 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
951 951 /* 008 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
952 952 /* 009 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
953 953
954 954 /* 010 */ { SIOCADDRT, sizeof (struct rtentry), IPI_PRIV,
955 955 MISC_CMD, ip_siocaddrt, NULL },
956 956 /* 011 */ { SIOCDELRT, sizeof (struct rtentry), IPI_PRIV,
957 957 MISC_CMD, ip_siocdelrt, NULL },
958 958
959 959 /* 012 */ { SIOCSIFADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
960 960 IF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart },
961 961 /* 013 */ { SIOCGIFADDR, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL,
962 962 IF_CMD, ip_sioctl_get_addr, NULL },
963 963
964 964 /* 014 */ { SIOCSIFDSTADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
965 965 IF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart },
966 966 /* 015 */ { SIOCGIFDSTADDR, sizeof (struct ifreq),
967 967 IPI_GET_CMD | IPI_REPL,
968 968 IF_CMD, ip_sioctl_get_dstaddr, NULL },
969 969
970 970 /* 016 */ { SIOCSIFFLAGS, sizeof (struct ifreq),
971 971 IPI_PRIV | IPI_WR | IPI_REPL,
972 972 IF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart },
973 973 /* 017 */ { SIOCGIFFLAGS, sizeof (struct ifreq),
974 974 IPI_MODOK | IPI_GET_CMD | IPI_REPL,
975 975 IF_CMD, ip_sioctl_get_flags, NULL },
976 976
977 977 /* 018 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
978 978 /* 019 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
979 979
980 980 /* copyin size cannot be coded for SIOCGIFCONF */
981 981 /* 020 */ { O_SIOCGIFCONF, 0, IPI_GET_CMD,
982 982 MISC_CMD, ip_sioctl_get_ifconf, NULL },
983 983
984 984 /* 021 */ { SIOCSIFMTU, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
985 985 IF_CMD, ip_sioctl_mtu, NULL },
986 986 /* 022 */ { SIOCGIFMTU, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL,
987 987 IF_CMD, ip_sioctl_get_mtu, NULL },
988 988 /* 023 */ { SIOCGIFBRDADDR, sizeof (struct ifreq),
989 989 IPI_GET_CMD | IPI_REPL,
990 990 IF_CMD, ip_sioctl_get_brdaddr, NULL },
991 991 /* 024 */ { SIOCSIFBRDADDR, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
992 992 IF_CMD, ip_sioctl_brdaddr, NULL },
993 993 /* 025 */ { SIOCGIFNETMASK, sizeof (struct ifreq),
994 994 IPI_GET_CMD | IPI_REPL,
995 995 IF_CMD, ip_sioctl_get_netmask, NULL },
996 996 /* 026 */ { SIOCSIFNETMASK, sizeof (struct ifreq), IPI_PRIV | IPI_WR,
997 997 IF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart },
998 998 /* 027 */ { SIOCGIFMETRIC, sizeof (struct ifreq),
999 999 IPI_GET_CMD | IPI_REPL,
1000 1000 IF_CMD, ip_sioctl_get_metric, NULL },
1001 1001 /* 028 */ { SIOCSIFMETRIC, sizeof (struct ifreq), IPI_PRIV,
1002 1002 IF_CMD, ip_sioctl_metric, NULL },
1003 1003 /* 029 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1004 1004
1005 1005 /* See 166-168 below for extended SIOC*XARP ioctls */
1006 1006 /* 030 */ { SIOCSARP, sizeof (struct arpreq), IPI_PRIV,
1007 1007 ARP_CMD, ip_sioctl_arp, NULL },
1008 1008 /* 031 */ { SIOCGARP, sizeof (struct arpreq), IPI_GET_CMD | IPI_REPL,
1009 1009 ARP_CMD, ip_sioctl_arp, NULL },
1010 1010 /* 032 */ { SIOCDARP, sizeof (struct arpreq), IPI_PRIV,
1011 1011 ARP_CMD, ip_sioctl_arp, NULL },
1012 1012
1013 1013 /* 033 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1014 1014 /* 034 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1015 1015 /* 035 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1016 1016 /* 036 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1017 1017 /* 037 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1018 1018 /* 038 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1019 1019 /* 039 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1020 1020 /* 040 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1021 1021 /* 041 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1022 1022 /* 042 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1023 1023 /* 043 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1024 1024 /* 044 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1025 1025 /* 045 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1026 1026 /* 046 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1027 1027 /* 047 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1028 1028 /* 048 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1029 1029 /* 049 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1030 1030 /* 050 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1031 1031 /* 051 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1032 1032 /* 052 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1033 1033 /* 053 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1034 1034
1035 1035 /* 054 */ { IF_UNITSEL, sizeof (int), IPI_PRIV | IPI_WR | IPI_MODOK,
1036 1036 MISC_CMD, if_unitsel, if_unitsel_restart },
1037 1037
1038 1038 /* 055 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1039 1039 /* 056 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1040 1040 /* 057 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1041 1041 /* 058 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1042 1042 /* 059 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1043 1043 /* 060 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1044 1044 /* 061 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1045 1045 /* 062 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1046 1046 /* 063 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1047 1047 /* 064 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1048 1048 /* 065 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1049 1049 /* 066 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1050 1050 /* 067 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1051 1051 /* 068 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1052 1052 /* 069 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1053 1053 /* 070 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1054 1054 /* 071 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1055 1055 /* 072 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1056 1056
1057 1057 /* 073 */ { SIOCSIFNAME, sizeof (struct ifreq),
1058 1058 IPI_PRIV | IPI_WR | IPI_MODOK,
1059 1059 IF_CMD, ip_sioctl_sifname, NULL },
1060 1060
1061 1061 /* 074 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1062 1062 /* 075 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1063 1063 /* 076 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1064 1064 /* 077 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1065 1065 /* 078 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1066 1066 /* 079 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1067 1067 /* 080 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1068 1068 /* 081 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1069 1069 /* 082 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1070 1070 /* 083 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1071 1071 /* 084 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1072 1072 /* 085 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1073 1073 /* 086 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1074 1074
1075 1075 /* 087 */ { SIOCGIFNUM, sizeof (int), IPI_GET_CMD | IPI_REPL,
1076 1076 MISC_CMD, ip_sioctl_get_ifnum, NULL },
1077 1077 /* 088 */ { SIOCGIFMUXID, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL,
1078 1078 IF_CMD, ip_sioctl_get_muxid, NULL },
1079 1079 /* 089 */ { SIOCSIFMUXID, sizeof (struct ifreq),
1080 1080 IPI_PRIV | IPI_WR | IPI_REPL,
1081 1081 IF_CMD, ip_sioctl_muxid, NULL },
1082 1082
1083 1083 /* Both if and lif variants share same func */
1084 1084 /* 090 */ { SIOCGIFINDEX, sizeof (struct ifreq), IPI_GET_CMD | IPI_REPL,
1085 1085 IF_CMD, ip_sioctl_get_lifindex, NULL },
1086 1086 /* Both if and lif variants share same func */
1087 1087 /* 091 */ { SIOCSIFINDEX, sizeof (struct ifreq),
1088 1088 IPI_PRIV | IPI_WR | IPI_REPL,
1089 1089 IF_CMD, ip_sioctl_slifindex, NULL },
1090 1090
1091 1091 /* copyin size cannot be coded for SIOCGIFCONF */
1092 1092 /* 092 */ { SIOCGIFCONF, 0, IPI_GET_CMD,
1093 1093 MISC_CMD, ip_sioctl_get_ifconf, NULL },
1094 1094 /* 093 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1095 1095 /* 094 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1096 1096 /* 095 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1097 1097 /* 096 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1098 1098 /* 097 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1099 1099 /* 098 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1100 1100 /* 099 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1101 1101 /* 100 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1102 1102 /* 101 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1103 1103 /* 102 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1104 1104 /* 103 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1105 1105 /* 104 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1106 1106 /* 105 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1107 1107 /* 106 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1108 1108 /* 107 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1109 1109 /* 108 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1110 1110 /* 109 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1111 1111
1112 1112 /* 110 */ { SIOCLIFREMOVEIF, sizeof (struct lifreq),
1113 1113 IPI_PRIV | IPI_WR | IPI_REPL,
1114 1114 LIF_CMD, ip_sioctl_removeif,
1115 1115 ip_sioctl_removeif_restart },
1116 1116 /* 111 */ { SIOCLIFADDIF, sizeof (struct lifreq),
1117 1117 IPI_GET_CMD | IPI_PRIV | IPI_WR | IPI_REPL,
1118 1118 LIF_CMD, ip_sioctl_addif, NULL },
1119 1119 #define SIOCLIFADDR_NDX 112
1120 1120 /* 112 */ { SIOCSLIFADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1121 1121 LIF_CMD, ip_sioctl_addr, ip_sioctl_addr_restart },
1122 1122 /* 113 */ { SIOCGLIFADDR, sizeof (struct lifreq),
1123 1123 IPI_GET_CMD | IPI_REPL,
1124 1124 LIF_CMD, ip_sioctl_get_addr, NULL },
1125 1125 /* 114 */ { SIOCSLIFDSTADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1126 1126 LIF_CMD, ip_sioctl_dstaddr, ip_sioctl_dstaddr_restart },
1127 1127 /* 115 */ { SIOCGLIFDSTADDR, sizeof (struct lifreq),
1128 1128 IPI_GET_CMD | IPI_REPL,
1129 1129 LIF_CMD, ip_sioctl_get_dstaddr, NULL },
1130 1130 /* 116 */ { SIOCSLIFFLAGS, sizeof (struct lifreq),
1131 1131 IPI_PRIV | IPI_WR | IPI_REPL,
1132 1132 LIF_CMD, ip_sioctl_flags, ip_sioctl_flags_restart },
1133 1133 /* 117 */ { SIOCGLIFFLAGS, sizeof (struct lifreq),
1134 1134 IPI_GET_CMD | IPI_MODOK | IPI_REPL,
1135 1135 LIF_CMD, ip_sioctl_get_flags, NULL },
1136 1136
1137 1137 /* 118 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1138 1138 /* 119 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1139 1139
1140 1140 /* 120 */ { O_SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD,
1141 1141 ip_sioctl_get_lifconf, NULL },
1142 1142 /* 121 */ { SIOCSLIFMTU, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1143 1143 LIF_CMD, ip_sioctl_mtu, NULL },
1144 1144 /* 122 */ { SIOCGLIFMTU, sizeof (struct lifreq), IPI_GET_CMD | IPI_REPL,
1145 1145 LIF_CMD, ip_sioctl_get_mtu, NULL },
1146 1146 /* 123 */ { SIOCGLIFBRDADDR, sizeof (struct lifreq),
1147 1147 IPI_GET_CMD | IPI_REPL,
1148 1148 LIF_CMD, ip_sioctl_get_brdaddr, NULL },
1149 1149 /* 124 */ { SIOCSLIFBRDADDR, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1150 1150 LIF_CMD, ip_sioctl_brdaddr, NULL },
1151 1151 /* 125 */ { SIOCGLIFNETMASK, sizeof (struct lifreq),
1152 1152 IPI_GET_CMD | IPI_REPL,
1153 1153 LIF_CMD, ip_sioctl_get_netmask, NULL },
1154 1154 /* 126 */ { SIOCSLIFNETMASK, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1155 1155 LIF_CMD, ip_sioctl_netmask, ip_sioctl_netmask_restart },
1156 1156 /* 127 */ { SIOCGLIFMETRIC, sizeof (struct lifreq),
1157 1157 IPI_GET_CMD | IPI_REPL,
1158 1158 LIF_CMD, ip_sioctl_get_metric, NULL },
1159 1159 /* 128 */ { SIOCSLIFMETRIC, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1160 1160 LIF_CMD, ip_sioctl_metric, NULL },
1161 1161 /* 129 */ { SIOCSLIFNAME, sizeof (struct lifreq),
1162 1162 IPI_PRIV | IPI_WR | IPI_MODOK | IPI_REPL,
1163 1163 LIF_CMD, ip_sioctl_slifname,
1164 1164 ip_sioctl_slifname_restart },
1165 1165
1166 1166 /* 130 */ { SIOCGLIFNUM, sizeof (struct lifnum), IPI_GET_CMD | IPI_REPL,
1167 1167 MISC_CMD, ip_sioctl_get_lifnum, NULL },
1168 1168 /* 131 */ { SIOCGLIFMUXID, sizeof (struct lifreq),
1169 1169 IPI_GET_CMD | IPI_REPL,
1170 1170 LIF_CMD, ip_sioctl_get_muxid, NULL },
1171 1171 /* 132 */ { SIOCSLIFMUXID, sizeof (struct lifreq),
1172 1172 IPI_PRIV | IPI_WR | IPI_REPL,
1173 1173 LIF_CMD, ip_sioctl_muxid, NULL },
1174 1174 /* 133 */ { SIOCGLIFINDEX, sizeof (struct lifreq),
1175 1175 IPI_GET_CMD | IPI_REPL,
1176 1176 LIF_CMD, ip_sioctl_get_lifindex, 0 },
1177 1177 /* 134 */ { SIOCSLIFINDEX, sizeof (struct lifreq),
1178 1178 IPI_PRIV | IPI_WR | IPI_REPL,
1179 1179 LIF_CMD, ip_sioctl_slifindex, 0 },
1180 1180 /* 135 */ { SIOCSLIFTOKEN, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1181 1181 LIF_CMD, ip_sioctl_token, NULL },
1182 1182 /* 136 */ { SIOCGLIFTOKEN, sizeof (struct lifreq),
1183 1183 IPI_GET_CMD | IPI_REPL,
1184 1184 LIF_CMD, ip_sioctl_get_token, NULL },
1185 1185 /* 137 */ { SIOCSLIFSUBNET, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1186 1186 LIF_CMD, ip_sioctl_subnet, ip_sioctl_subnet_restart },
1187 1187 /* 138 */ { SIOCGLIFSUBNET, sizeof (struct lifreq),
1188 1188 IPI_GET_CMD | IPI_REPL,
1189 1189 LIF_CMD, ip_sioctl_get_subnet, NULL },
1190 1190 /* 139 */ { SIOCSLIFLNKINFO, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1191 1191 LIF_CMD, ip_sioctl_lnkinfo, NULL },
1192 1192
1193 1193 /* 140 */ { SIOCGLIFLNKINFO, sizeof (struct lifreq),
1194 1194 IPI_GET_CMD | IPI_REPL,
1195 1195 LIF_CMD, ip_sioctl_get_lnkinfo, NULL },
1196 1196 /* 141 */ { SIOCLIFDELND, sizeof (struct lifreq), IPI_PRIV,
1197 1197 LIF_CMD, ip_siocdelndp_v6, NULL },
1198 1198 /* 142 */ { SIOCLIFGETND, sizeof (struct lifreq), IPI_GET_CMD,
1199 1199 LIF_CMD, ip_siocqueryndp_v6, NULL },
1200 1200 /* 143 */ { SIOCLIFSETND, sizeof (struct lifreq), IPI_PRIV,
1201 1201 LIF_CMD, ip_siocsetndp_v6, NULL },
1202 1202 /* 144 */ { SIOCTMYADDR, sizeof (struct sioc_addrreq), IPI_GET_CMD,
1203 1203 MISC_CMD, ip_sioctl_tmyaddr, NULL },
1204 1204 /* 145 */ { SIOCTONLINK, sizeof (struct sioc_addrreq), IPI_GET_CMD,
1205 1205 MISC_CMD, ip_sioctl_tonlink, NULL },
1206 1206 /* 146 */ { SIOCTMYSITE, sizeof (struct sioc_addrreq), 0,
1207 1207 MISC_CMD, ip_sioctl_tmysite, NULL },
1208 1208 /* 147 */ { SIOCGTUNPARAM, sizeof (struct iftun_req), IPI_REPL,
1209 1209 TUN_CMD, ip_sioctl_tunparam, NULL },
1210 1210 /* 148 */ { SIOCSTUNPARAM, sizeof (struct iftun_req),
1211 1211 IPI_PRIV | IPI_WR,
1212 1212 TUN_CMD, ip_sioctl_tunparam, NULL },
1213 1213
1214 1214 /* IPSECioctls handled in ip_sioctl_copyin_setup itself */
1215 1215 /* 149 */ { SIOCFIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL },
1216 1216 /* 150 */ { SIOCSIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL },
1217 1217 /* 151 */ { SIOCDIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL },
1218 1218 /* 152 */ { SIOCLIPSECONFIG, 0, IPI_PRIV, MISC_CMD, NULL, NULL },
1219 1219
1220 1220 /* 153 */ { SIOCLIFFAILOVER, sizeof (struct lifreq),
1221 1221 IPI_PRIV | IPI_WR | IPI_REPL,
1222 1222 LIF_CMD, ip_sioctl_move, ip_sioctl_move },
1223 1223 /* 154 */ { SIOCLIFFAILBACK, sizeof (struct lifreq),
1224 1224 IPI_PRIV | IPI_WR | IPI_REPL,
1225 1225 LIF_CMD, ip_sioctl_move, ip_sioctl_move },
1226 1226 /* 155 */ { SIOCSLIFGROUPNAME, sizeof (struct lifreq),
1227 1227 IPI_PRIV | IPI_WR | IPI_REPL,
1228 1228 LIF_CMD, ip_sioctl_groupname, ip_sioctl_groupname },
1229 1229 /* 156 */ { SIOCGLIFGROUPNAME, sizeof (struct lifreq),
1230 1230 IPI_GET_CMD | IPI_REPL,
1231 1231 LIF_CMD, ip_sioctl_get_groupname, NULL },
1232 1232 /* 157 */ { SIOCGLIFOINDEX, sizeof (struct lifreq),
1233 1233 IPI_GET_CMD | IPI_REPL,
1234 1234 LIF_CMD, ip_sioctl_get_oindex, NULL },
1235 1235
1236 1236 /* Leave 158-160 unused; used to be SIOC*IFARP ioctls */
1237 1237 /* 158 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1238 1238 /* 159 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1239 1239 /* 160 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1240 1240
1241 1241 /* 161 */ { SIOCSLIFOINDEX, sizeof (struct lifreq), IPI_PRIV | IPI_WR,
1242 1242 LIF_CMD, ip_sioctl_slifoindex, NULL },
1243 1243
1244 1244 /* These are handled in ip_sioctl_copyin_setup itself */
1245 1245 /* 162 */ { SIOCGIP6ADDRPOLICY, 0, IPI_NULL_BCONT,
1246 1246 MISC_CMD, NULL, NULL },
1247 1247 /* 163 */ { SIOCSIP6ADDRPOLICY, 0, IPI_PRIV | IPI_NULL_BCONT,
1248 1248 MISC_CMD, NULL, NULL },
1249 1249 /* 164 */ { SIOCGDSTINFO, 0, IPI_GET_CMD, MISC_CMD, NULL, NULL },
1250 1250
1251 1251 /* 165 */ { SIOCGLIFCONF, 0, IPI_GET_CMD, MISC_CMD,
1252 1252 ip_sioctl_get_lifconf, NULL },
1253 1253
1254 1254 /* 166 */ { SIOCSXARP, sizeof (struct xarpreq), IPI_PRIV,
1255 1255 XARP_CMD, ip_sioctl_arp, NULL },
1256 1256 /* 167 */ { SIOCGXARP, sizeof (struct xarpreq), IPI_GET_CMD | IPI_REPL,
1257 1257 XARP_CMD, ip_sioctl_arp, NULL },
1258 1258 /* 168 */ { SIOCDXARP, sizeof (struct xarpreq), IPI_PRIV,
1259 1259 XARP_CMD, ip_sioctl_arp, NULL },
1260 1260
1261 1261 /* SIOCPOPSOCKFS is not handled by IP */
1262 1262 /* 169 */ { IPI_DONTCARE /* SIOCPOPSOCKFS */, 0, 0, 0, NULL, NULL },
1263 1263
1264 1264 /* 170 */ { SIOCGLIFZONE, sizeof (struct lifreq),
1265 1265 IPI_GET_CMD | IPI_REPL,
1266 1266 LIF_CMD, ip_sioctl_get_lifzone, NULL },
1267 1267 /* 171 */ { SIOCSLIFZONE, sizeof (struct lifreq),
1268 1268 IPI_PRIV | IPI_WR | IPI_REPL,
1269 1269 LIF_CMD, ip_sioctl_slifzone,
1270 1270 ip_sioctl_slifzone_restart },
1271 1271 /* 172-174 are SCTP ioctls and not handled by IP */
1272 1272 /* 172 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1273 1273 /* 173 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1274 1274 /* 174 */ { IPI_DONTCARE, 0, 0, 0, NULL, NULL },
1275 1275 /* 175 */ { SIOCGLIFUSESRC, sizeof (struct lifreq),
1276 1276 IPI_GET_CMD, LIF_CMD,
1277 1277 ip_sioctl_get_lifusesrc, 0 },
1278 1278 /* 176 */ { SIOCSLIFUSESRC, sizeof (struct lifreq),
1279 1279 IPI_PRIV | IPI_WR,
1280 1280 LIF_CMD, ip_sioctl_slifusesrc,
1281 1281 NULL },
1282 1282 /* 177 */ { SIOCGLIFSRCOF, 0, IPI_GET_CMD, MISC_CMD,
1283 1283 ip_sioctl_get_lifsrcof, NULL },
1284 1284 /* 178 */ { SIOCGMSFILTER, sizeof (struct group_filter), IPI_GET_CMD,
1285 1285 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1286 1286 /* 179 */ { SIOCSMSFILTER, sizeof (struct group_filter), IPI_WR,
1287 1287 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1288 1288 /* 180 */ { SIOCGIPMSFILTER, sizeof (struct ip_msfilter), IPI_GET_CMD,
1289 1289 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1290 1290 /* 181 */ { SIOCSIPMSFILTER, sizeof (struct ip_msfilter), IPI_WR,
1291 1291 MSFILT_CMD, ip_sioctl_msfilter, NULL },
1292 1292 /* 182 */ { SIOCSIPMPFAILBACK, sizeof (int), IPI_PRIV, MISC_CMD,
1293 1293 ip_sioctl_set_ipmpfailback, NULL },
1294 1294 /* SIOCSENABLESDP is handled by SDP */
1295 1295 /* 183 */ { IPI_DONTCARE /* SIOCSENABLESDP */, 0, 0, 0, NULL, NULL },
1296 1296 };
1297 1297
1298 1298 int ip_ndx_ioctl_count = sizeof (ip_ndx_ioctl_table) / sizeof (ip_ioctl_cmd_t);
1299 1299
1300 1300 ip_ioctl_cmd_t ip_misc_ioctl_table[] = {
1301 1301 { OSIOCGTUNPARAM, sizeof (struct old_iftun_req),
1302 1302 IPI_GET_CMD | IPI_REPL, TUN_CMD, ip_sioctl_tunparam, NULL },
1303 1303 { OSIOCSTUNPARAM, sizeof (struct old_iftun_req), IPI_PRIV | IPI_WR,
1304 1304 TUN_CMD, ip_sioctl_tunparam, NULL },
1305 1305 { I_LINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL },
1306 1306 { I_UNLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL },
1307 1307 { I_PLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL },
1308 1308 { I_PUNLINK, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL },
1309 1309 { ND_GET, 0, IPI_PASS_DOWN, 0, NULL, NULL },
1310 1310 { ND_SET, 0, IPI_PRIV | IPI_WR | IPI_PASS_DOWN, 0, NULL, NULL },
1311 1311 { IP_IOCTL, 0, 0, 0, NULL, NULL },
1312 1312 { SIOCGETVIFCNT, sizeof (struct sioc_vif_req), IPI_REPL | IPI_GET_CMD,
1313 1313 MISC_CMD, mrt_ioctl},
1314 1314 { SIOCGETSGCNT, sizeof (struct sioc_sg_req), IPI_REPL | IPI_GET_CMD,
1315 1315 MISC_CMD, mrt_ioctl},
1316 1316 { SIOCGETLSGCNT, sizeof (struct sioc_lsg_req), IPI_REPL | IPI_GET_CMD,
1317 1317 MISC_CMD, mrt_ioctl}
1318 1318 };
1319 1319
1320 1320 int ip_misc_ioctl_count =
1321 1321 sizeof (ip_misc_ioctl_table) / sizeof (ip_ioctl_cmd_t);
1322 1322
1323 1323 int conn_drain_nthreads; /* Number of drainers reqd. */
1324 1324 /* Settable in /etc/system */
1325 1325 /* Defined in ip_ire.c */
1326 1326 extern uint32_t ip_ire_max_bucket_cnt, ip6_ire_max_bucket_cnt;
1327 1327 extern uint32_t ip_ire_min_bucket_cnt, ip6_ire_min_bucket_cnt;
1328 1328 extern uint32_t ip_ire_mem_ratio, ip_ire_cpu_ratio;
1329 1329
1330 1330 static nv_t ire_nv_arr[] = {
1331 1331 { IRE_BROADCAST, "BROADCAST" },
1332 1332 { IRE_LOCAL, "LOCAL" },
1333 1333 { IRE_LOOPBACK, "LOOPBACK" },
1334 1334 { IRE_CACHE, "CACHE" },
1335 1335 { IRE_DEFAULT, "DEFAULT" },
1336 1336 { IRE_PREFIX, "PREFIX" },
1337 1337 { IRE_IF_NORESOLVER, "IF_NORESOL" },
1338 1338 { IRE_IF_RESOLVER, "IF_RESOLV" },
1339 1339 { IRE_HOST, "HOST" },
1340 1340 { 0 }
1341 1341 };
1342 1342
1343 1343 nv_t *ire_nv_tbl = ire_nv_arr;
1344 1344
1345 1345 /* Simple ICMP IP Header Template */
1346 1346 static ipha_t icmp_ipha = {
1347 1347 IP_SIMPLE_HDR_VERSION, 0, 0, 0, 0, 0, IPPROTO_ICMP
1348 1348 };
1349 1349
1350 1350 struct module_info ip_mod_info = {
1351 1351 IP_MOD_ID, IP_MOD_NAME, 1, INFPSZ, 65536, 1024
1352 1352 };
1353 1353
1354 1354 /*
1355 1355 * Duplicate static symbols within a module confuses mdb; so we avoid the
1356 1356 * problem by making the symbols here distinct from those in udp.c.
1357 1357 */
1358 1358
1359 1359 /*
1360 1360 * Entry points for IP as a device and as a module.
1361 1361 * FIXME: down the road we might want a separate module and driver qinit.
1362 1362 * We have separate open functions for the /dev/ip and /dev/ip6 devices.
1363 1363 */
1364 1364 static struct qinit iprinitv4 = {
1365 1365 (pfi_t)ip_rput, NULL, ip_openv4, ip_close, NULL,
1366 1366 &ip_mod_info
1367 1367 };
1368 1368
1369 1369 struct qinit iprinitv6 = {
1370 1370 (pfi_t)ip_rput_v6, NULL, ip_openv6, ip_close, NULL,
1371 1371 &ip_mod_info
1372 1372 };
1373 1373
1374 1374 static struct qinit ipwinitv4 = {
1375 1375 (pfi_t)ip_wput, (pfi_t)ip_wsrv, NULL, NULL, NULL,
1376 1376 &ip_mod_info
1377 1377 };
1378 1378
1379 1379 struct qinit ipwinitv6 = {
1380 1380 (pfi_t)ip_wput_v6, (pfi_t)ip_wsrv, NULL, NULL, NULL,
1381 1381 &ip_mod_info
1382 1382 };
1383 1383
1384 1384 static struct qinit iplrinit = {
1385 1385 (pfi_t)ip_lrput, NULL, ip_openv4, ip_close, NULL,
1386 1386 &ip_mod_info
1387 1387 };
1388 1388
1389 1389 static struct qinit iplwinit = {
1390 1390 (pfi_t)ip_lwput, NULL, NULL, NULL, NULL,
1391 1391 &ip_mod_info
1392 1392 };
1393 1393
1394 1394 /* For AF_INET aka /dev/ip */
1395 1395 struct streamtab ipinfov4 = {
1396 1396 &iprinitv4, &ipwinitv4, &iplrinit, &iplwinit
1397 1397 };
1398 1398
1399 1399 /* For AF_INET6 aka /dev/ip6 */
1400 1400 struct streamtab ipinfov6 = {
1401 1401 &iprinitv6, &ipwinitv6, &iplrinit, &iplwinit
1402 1402 };
1403 1403
1404 1404 #ifdef DEBUG
1405 1405 static boolean_t skip_sctp_cksum = B_FALSE;
1406 1406 #endif
1407 1407
1408 1408 /*
1409 1409 * Prepend the zoneid using an ipsec_out_t for later use by functions like
1410 1410 * ip_rput_v6(), ip_output(), etc. If the message
1411 1411 * block already has a M_CTL at the front of it, then simply set the zoneid
1412 1412 * appropriately.
1413 1413 */
1414 1414 mblk_t *
1415 1415 ip_prepend_zoneid(mblk_t *mp, zoneid_t zoneid, ip_stack_t *ipst)
1416 1416 {
1417 1417 mblk_t *first_mp;
1418 1418 ipsec_out_t *io;
1419 1419
1420 1420 ASSERT(zoneid != ALL_ZONES);
1421 1421 if (mp->b_datap->db_type == M_CTL) {
1422 1422 io = (ipsec_out_t *)mp->b_rptr;
1423 1423 ASSERT(io->ipsec_out_type == IPSEC_OUT);
1424 1424 io->ipsec_out_zoneid = zoneid;
1425 1425 return (mp);
1426 1426 }
1427 1427
1428 1428 first_mp = ipsec_alloc_ipsec_out(ipst->ips_netstack);
1429 1429 if (first_mp == NULL)
1430 1430 return (NULL);
1431 1431 io = (ipsec_out_t *)first_mp->b_rptr;
1432 1432 /* This is not a secure packet */
1433 1433 io->ipsec_out_secure = B_FALSE;
1434 1434 io->ipsec_out_zoneid = zoneid;
1435 1435 first_mp->b_cont = mp;
1436 1436 return (first_mp);
1437 1437 }
1438 1438
1439 1439 /*
1440 1440 * Copy an M_CTL-tagged message, preserving reference counts appropriately.
1441 1441 */
1442 1442 mblk_t *
1443 1443 ip_copymsg(mblk_t *mp)
1444 1444 {
1445 1445 mblk_t *nmp;
1446 1446 ipsec_info_t *in;
1447 1447
1448 1448 if (mp->b_datap->db_type != M_CTL)
1449 1449 return (copymsg(mp));
1450 1450
1451 1451 in = (ipsec_info_t *)mp->b_rptr;
1452 1452
1453 1453 /*
1454 1454 * Note that M_CTL is also used for delivering ICMP error messages
1455 1455 * upstream to transport layers.
1456 1456 */
1457 1457 if (in->ipsec_info_type != IPSEC_OUT &&
1458 1458 in->ipsec_info_type != IPSEC_IN)
1459 1459 return (copymsg(mp));
1460 1460
1461 1461 nmp = copymsg(mp->b_cont);
1462 1462
1463 1463 if (in->ipsec_info_type == IPSEC_OUT) {
1464 1464 return (ipsec_out_tag(mp, nmp,
1465 1465 ((ipsec_out_t *)in)->ipsec_out_ns));
1466 1466 } else {
1467 1467 return (ipsec_in_tag(mp, nmp,
1468 1468 ((ipsec_in_t *)in)->ipsec_in_ns));
1469 1469 }
1470 1470 }
1471 1471
1472 1472 /* Generate an ICMP fragmentation needed message. */
1473 1473 static void
1474 1474 icmp_frag_needed(queue_t *q, mblk_t *mp, int mtu, zoneid_t zoneid,
1475 1475 ip_stack_t *ipst)
1476 1476 {
1477 1477 icmph_t icmph;
1478 1478 mblk_t *first_mp;
1479 1479 boolean_t mctl_present;
1480 1480
1481 1481 EXTRACT_PKT_MP(mp, first_mp, mctl_present);
1482 1482
1483 1483 if (!(mp = icmp_pkt_err_ok(mp, ipst))) {
1484 1484 if (mctl_present)
1485 1485 freeb(first_mp);
1486 1486 return;
1487 1487 }
1488 1488
1489 1489 bzero(&icmph, sizeof (icmph_t));
1490 1490 icmph.icmph_type = ICMP_DEST_UNREACHABLE;
1491 1491 icmph.icmph_code = ICMP_FRAGMENTATION_NEEDED;
1492 1492 icmph.icmph_du_mtu = htons((uint16_t)mtu);
1493 1493 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutFragNeeded);
1494 1494 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs);
1495 1495 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid,
1496 1496 ipst);
1497 1497 }
1498 1498
1499 1499 /*
1500 1500 * icmp_inbound deals with ICMP messages in the following ways.
1501 1501 *
1502 1502 * 1) It needs to send a reply back and possibly delivering it
1503 1503 * to the "interested" upper clients.
1504 1504 * 2) It needs to send it to the upper clients only.
1505 1505 * 3) It needs to change some values in IP only.
1506 1506 * 4) It needs to change some values in IP and upper layers e.g TCP.
1507 1507 *
1508 1508 * We need to accomodate icmp messages coming in clear until we get
1509 1509 * everything secure from the wire. If icmp_accept_clear_messages
1510 1510 * is zero we check with the global policy and act accordingly. If
1511 1511 * it is non-zero, we accept the message without any checks. But
1512 1512 * *this does not mean* that this will be delivered to the upper
1513 1513 * clients. By accepting we might send replies back, change our MTU
1514 1514 * value etc. but delivery to the ULP/clients depends on their policy
1515 1515 * dispositions.
1516 1516 *
1517 1517 * We handle the above 4 cases in the context of IPsec in the
1518 1518 * following way :
1519 1519 *
1520 1520 * 1) Send the reply back in the same way as the request came in.
1521 1521 * If it came in encrypted, it goes out encrypted. If it came in
1522 1522 * clear, it goes out in clear. Thus, this will prevent chosen
1523 1523 * plain text attack.
1524 1524 * 2) The client may or may not expect things to come in secure.
1525 1525 * If it comes in secure, the policy constraints are checked
1526 1526 * before delivering it to the upper layers. If it comes in
1527 1527 * clear, ipsec_inbound_accept_clear will decide whether to
1528 1528 * accept this in clear or not. In both the cases, if the returned
1529 1529 * message (IP header + 8 bytes) that caused the icmp message has
1530 1530 * AH/ESP headers, it is sent up to AH/ESP for validation before
1531 1531 * sending up. If there are only 8 bytes of returned message, then
1532 1532 * upper client will not be notified.
1533 1533 * 3) Check with global policy to see whether it matches the constaints.
1534 1534 * But this will be done only if icmp_accept_messages_in_clear is
1535 1535 * zero.
1536 1536 * 4) If we need to change both in IP and ULP, then the decision taken
1537 1537 * while affecting the values in IP and while delivering up to TCP
1538 1538 * should be the same.
1539 1539 *
1540 1540 * There are two cases.
1541 1541 *
1542 1542 * a) If we reject data at the IP layer (ipsec_check_global_policy()
1543 1543 * failed), we will not deliver it to the ULP, even though they
1544 1544 * are *willing* to accept in *clear*. This is fine as our global
1545 1545 * disposition to icmp messages asks us reject the datagram.
1546 1546 *
1547 1547 * b) If we accept data at the IP layer (ipsec_check_global_policy()
1548 1548 * succeeded or icmp_accept_messages_in_clear is 1), and not able
1549 1549 * to deliver it to ULP (policy failed), it can lead to
1550 1550 * consistency problems. The cases known at this time are
1551 1551 * ICMP_DESTINATION_UNREACHABLE messages with following code
1552 1552 * values :
1553 1553 *
1554 1554 * - ICMP_FRAGMENTATION_NEEDED : IP adapts to the new value
1555 1555 * and Upper layer rejects. Then the communication will
1556 1556 * come to a stop. This is solved by making similar decisions
1557 1557 * at both levels. Currently, when we are unable to deliver
1558 1558 * to the Upper Layer (due to policy failures) while IP has
1559 1559 * adjusted ire_max_frag, the next outbound datagram would
1560 1560 * generate a local ICMP_FRAGMENTATION_NEEDED message - which
1561 1561 * will be with the right level of protection. Thus the right
1562 1562 * value will be communicated even if we are not able to
1563 1563 * communicate when we get from the wire initially. But this
1564 1564 * assumes there would be at least one outbound datagram after
1565 1565 * IP has adjusted its ire_max_frag value. To make things
1566 1566 * simpler, we accept in clear after the validation of
1567 1567 * AH/ESP headers.
1568 1568 *
1569 1569 * - Other ICMP ERRORS : We may not be able to deliver it to the
1570 1570 * upper layer depending on the level of protection the upper
1571 1571 * layer expects and the disposition in ipsec_inbound_accept_clear().
1572 1572 * ipsec_inbound_accept_clear() decides whether a given ICMP error
1573 1573 * should be accepted in clear when the Upper layer expects secure.
1574 1574 * Thus the communication may get aborted by some bad ICMP
1575 1575 * packets.
1576 1576 *
1577 1577 * IPQoS Notes:
1578 1578 * The only instance when a packet is sent for processing is when there
1579 1579 * isn't an ICMP client and if we are interested in it.
1580 1580 * If there is a client, IPPF processing will take place in the
1581 1581 * ip_fanout_proto routine.
1582 1582 *
1583 1583 * Zones notes:
1584 1584 * The packet is only processed in the context of the specified zone: typically
1585 1585 * only this zone will reply to an echo request, and only interested clients in
1586 1586 * this zone will receive a copy of the packet. This means that the caller must
1587 1587 * call icmp_inbound() for each relevant zone.
1588 1588 */
1589 1589 static void
1590 1590 icmp_inbound(queue_t *q, mblk_t *mp, boolean_t broadcast, ill_t *ill,
1591 1591 int sum_valid, uint32_t sum, boolean_t mctl_present, boolean_t ip_policy,
1592 1592 ill_t *recv_ill, zoneid_t zoneid)
1593 1593 {
1594 1594 icmph_t *icmph;
1595 1595 ipha_t *ipha;
1596 1596 int iph_hdr_length;
1597 1597 int hdr_length;
1598 1598 boolean_t interested;
1599 1599 uint32_t ts;
1600 1600 uchar_t *wptr;
1601 1601 ipif_t *ipif;
1602 1602 mblk_t *first_mp;
1603 1603 ipsec_in_t *ii;
1604 1604 ire_t *src_ire;
1605 1605 boolean_t onlink;
1606 1606 timestruc_t now;
1607 1607 uint32_t ill_index;
1608 1608 ip_stack_t *ipst;
1609 1609
1610 1610 ASSERT(ill != NULL);
1611 1611 ipst = ill->ill_ipst;
1612 1612
1613 1613 first_mp = mp;
1614 1614 if (mctl_present) {
1615 1615 mp = first_mp->b_cont;
1616 1616 ASSERT(mp != NULL);
1617 1617 }
1618 1618
1619 1619 ipha = (ipha_t *)mp->b_rptr;
1620 1620 if (ipst->ips_icmp_accept_clear_messages == 0) {
1621 1621 first_mp = ipsec_check_global_policy(first_mp, NULL,
1622 1622 ipha, NULL, mctl_present, ipst->ips_netstack);
1623 1623 if (first_mp == NULL)
1624 1624 return;
1625 1625 }
1626 1626
1627 1627 /*
1628 1628 * On a labeled system, we have to check whether the zone itself is
1629 1629 * permitted to receive raw traffic.
1630 1630 */
1631 1631 if (is_system_labeled()) {
1632 1632 if (zoneid == ALL_ZONES)
1633 1633 zoneid = tsol_packet_to_zoneid(mp);
1634 1634 if (!tsol_can_accept_raw(mp, B_FALSE)) {
1635 1635 ip1dbg(("icmp_inbound: zone %d can't receive raw",
1636 1636 zoneid));
1637 1637 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
1638 1638 freemsg(first_mp);
1639 1639 return;
1640 1640 }
1641 1641 }
1642 1642
1643 1643 /*
1644 1644 * We have accepted the ICMP message. It means that we will
1645 1645 * respond to the packet if needed. It may not be delivered
1646 1646 * to the upper client depending on the policy constraints
1647 1647 * and the disposition in ipsec_inbound_accept_clear.
1648 1648 */
1649 1649
1650 1650 ASSERT(ill != NULL);
1651 1651
1652 1652 BUMP_MIB(&ipst->ips_icmp_mib, icmpInMsgs);
1653 1653 iph_hdr_length = IPH_HDR_LENGTH(ipha);
1654 1654 if ((mp->b_wptr - mp->b_rptr) < (iph_hdr_length + ICMPH_SIZE)) {
1655 1655 /* Last chance to get real. */
1656 1656 if (!pullupmsg(mp, iph_hdr_length + ICMPH_SIZE)) {
1657 1657 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
1658 1658 freemsg(first_mp);
1659 1659 return;
1660 1660 }
1661 1661 /* Refresh iph following the pullup. */
1662 1662 ipha = (ipha_t *)mp->b_rptr;
1663 1663 }
1664 1664 /* ICMP header checksum, including checksum field, should be zero. */
1665 1665 if (sum_valid ? (sum != 0 && sum != 0xFFFF) :
1666 1666 IP_CSUM(mp, iph_hdr_length, 0)) {
1667 1667 BUMP_MIB(&ipst->ips_icmp_mib, icmpInCksumErrs);
1668 1668 freemsg(first_mp);
1669 1669 return;
1670 1670 }
1671 1671 /* The IP header will always be a multiple of four bytes */
1672 1672 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
1673 1673 ip2dbg(("icmp_inbound: type %d code %d\n", icmph->icmph_type,
1674 1674 icmph->icmph_code));
1675 1675 wptr = (uchar_t *)icmph + ICMPH_SIZE;
1676 1676 /* We will set "interested" to "true" if we want a copy */
1677 1677 interested = B_FALSE;
1678 1678 switch (icmph->icmph_type) {
1679 1679 case ICMP_ECHO_REPLY:
1680 1680 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchoReps);
1681 1681 break;
1682 1682 case ICMP_DEST_UNREACHABLE:
1683 1683 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED)
1684 1684 BUMP_MIB(&ipst->ips_icmp_mib, icmpInFragNeeded);
1685 1685 interested = B_TRUE; /* Pass up to transport */
1686 1686 BUMP_MIB(&ipst->ips_icmp_mib, icmpInDestUnreachs);
1687 1687 break;
1688 1688 case ICMP_SOURCE_QUENCH:
1689 1689 interested = B_TRUE; /* Pass up to transport */
1690 1690 BUMP_MIB(&ipst->ips_icmp_mib, icmpInSrcQuenchs);
1691 1691 break;
1692 1692 case ICMP_REDIRECT:
1693 1693 if (!ipst->ips_ip_ignore_redirect)
1694 1694 interested = B_TRUE;
1695 1695 BUMP_MIB(&ipst->ips_icmp_mib, icmpInRedirects);
1696 1696 break;
1697 1697 case ICMP_ECHO_REQUEST:
1698 1698 /*
1699 1699 * Whether to respond to echo requests that come in as IP
1700 1700 * broadcasts or as IP multicast is subject to debate
1701 1701 * (what isn't?). We aim to please, you pick it.
1702 1702 * Default is do it.
1703 1703 */
1704 1704 if (!broadcast && !CLASSD(ipha->ipha_dst)) {
1705 1705 /* unicast: always respond */
1706 1706 interested = B_TRUE;
1707 1707 } else if (CLASSD(ipha->ipha_dst)) {
1708 1708 /* multicast: respond based on tunable */
1709 1709 interested = ipst->ips_ip_g_resp_to_echo_mcast;
1710 1710 } else if (broadcast) {
1711 1711 /* broadcast: respond based on tunable */
1712 1712 interested = ipst->ips_ip_g_resp_to_echo_bcast;
1713 1713 }
1714 1714 BUMP_MIB(&ipst->ips_icmp_mib, icmpInEchos);
1715 1715 break;
1716 1716 case ICMP_ROUTER_ADVERTISEMENT:
1717 1717 case ICMP_ROUTER_SOLICITATION:
1718 1718 break;
1719 1719 case ICMP_TIME_EXCEEDED:
1720 1720 interested = B_TRUE; /* Pass up to transport */
1721 1721 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimeExcds);
1722 1722 break;
1723 1723 case ICMP_PARAM_PROBLEM:
1724 1724 interested = B_TRUE; /* Pass up to transport */
1725 1725 BUMP_MIB(&ipst->ips_icmp_mib, icmpInParmProbs);
1726 1726 break;
1727 1727 case ICMP_TIME_STAMP_REQUEST:
1728 1728 /* Response to Time Stamp Requests is local policy. */
1729 1729 if (ipst->ips_ip_g_resp_to_timestamp &&
1730 1730 /* So is whether to respond if it was an IP broadcast. */
1731 1731 (!broadcast || ipst->ips_ip_g_resp_to_timestamp_bcast)) {
1732 1732 int tstamp_len = 3 * sizeof (uint32_t);
1733 1733
1734 1734 if (wptr + tstamp_len > mp->b_wptr) {
1735 1735 if (!pullupmsg(mp, wptr + tstamp_len -
1736 1736 mp->b_rptr)) {
1737 1737 BUMP_MIB(ill->ill_ip_mib,
1738 1738 ipIfStatsInDiscards);
1739 1739 freemsg(first_mp);
1740 1740 return;
1741 1741 }
1742 1742 /* Refresh ipha following the pullup. */
1743 1743 ipha = (ipha_t *)mp->b_rptr;
1744 1744 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
1745 1745 wptr = (uchar_t *)icmph + ICMPH_SIZE;
1746 1746 }
1747 1747 interested = B_TRUE;
1748 1748 }
1749 1749 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestamps);
1750 1750 break;
1751 1751 case ICMP_TIME_STAMP_REPLY:
1752 1752 BUMP_MIB(&ipst->ips_icmp_mib, icmpInTimestampReps);
1753 1753 break;
1754 1754 case ICMP_INFO_REQUEST:
1755 1755 /* Per RFC 1122 3.2.2.7, ignore this. */
1756 1756 case ICMP_INFO_REPLY:
1757 1757 break;
1758 1758 case ICMP_ADDRESS_MASK_REQUEST:
1759 1759 if ((ipst->ips_ip_respond_to_address_mask_broadcast ||
1760 1760 !broadcast) &&
1761 1761 /* TODO m_pullup of complete header? */
1762 1762 (mp->b_datap->db_lim - wptr) >= IP_ADDR_LEN) {
1763 1763 interested = B_TRUE;
1764 1764 }
1765 1765 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMasks);
1766 1766 break;
1767 1767 case ICMP_ADDRESS_MASK_REPLY:
1768 1768 BUMP_MIB(&ipst->ips_icmp_mib, icmpInAddrMaskReps);
1769 1769 break;
1770 1770 default:
1771 1771 interested = B_TRUE; /* Pass up to transport */
1772 1772 BUMP_MIB(&ipst->ips_icmp_mib, icmpInUnknowns);
1773 1773 break;
1774 1774 }
1775 1775 /* See if there is an ICMP client. */
1776 1776 if (ipst->ips_ipcl_proto_fanout[IPPROTO_ICMP].connf_head != NULL) {
1777 1777 /* If there is an ICMP client and we want one too, copy it. */
1778 1778 mblk_t *first_mp1;
1779 1779
1780 1780 if (!interested) {
1781 1781 ip_fanout_proto(q, first_mp, ill, ipha, 0, mctl_present,
1782 1782 ip_policy, recv_ill, zoneid);
1783 1783 return;
1784 1784 }
1785 1785 first_mp1 = ip_copymsg(first_mp);
1786 1786 if (first_mp1 != NULL) {
1787 1787 ip_fanout_proto(q, first_mp1, ill, ipha,
1788 1788 0, mctl_present, ip_policy, recv_ill, zoneid);
1789 1789 }
1790 1790 } else if (!interested) {
1791 1791 freemsg(first_mp);
1792 1792 return;
1793 1793 } else {
1794 1794 /*
1795 1795 * Initiate policy processing for this packet if ip_policy
1796 1796 * is true.
1797 1797 */
1798 1798 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) {
1799 1799 ill_index = ill->ill_phyint->phyint_ifindex;
1800 1800 ip_process(IPP_LOCAL_IN, &mp, ill_index);
1801 1801 if (mp == NULL) {
1802 1802 if (mctl_present) {
1803 1803 freeb(first_mp);
1804 1804 }
1805 1805 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
1806 1806 return;
1807 1807 }
1808 1808 }
1809 1809 }
1810 1810 /* We want to do something with it. */
1811 1811 /* Check db_ref to make sure we can modify the packet. */
1812 1812 if (mp->b_datap->db_ref > 1) {
1813 1813 mblk_t *first_mp1;
1814 1814
1815 1815 first_mp1 = ip_copymsg(first_mp);
1816 1816 freemsg(first_mp);
1817 1817 if (!first_mp1) {
1818 1818 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
1819 1819 return;
1820 1820 }
1821 1821 first_mp = first_mp1;
1822 1822 if (mctl_present) {
1823 1823 mp = first_mp->b_cont;
1824 1824 ASSERT(mp != NULL);
1825 1825 } else {
1826 1826 mp = first_mp;
1827 1827 }
1828 1828 ipha = (ipha_t *)mp->b_rptr;
1829 1829 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
1830 1830 wptr = (uchar_t *)icmph + ICMPH_SIZE;
1831 1831 }
1832 1832 switch (icmph->icmph_type) {
1833 1833 case ICMP_ADDRESS_MASK_REQUEST:
1834 1834 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid);
1835 1835 if (ipif == NULL) {
1836 1836 freemsg(first_mp);
1837 1837 return;
1838 1838 }
1839 1839 /*
1840 1840 * outging interface must be IPv4
1841 1841 */
1842 1842 ASSERT(ipif != NULL && !ipif->ipif_isv6);
1843 1843 icmph->icmph_type = ICMP_ADDRESS_MASK_REPLY;
1844 1844 bcopy(&ipif->ipif_net_mask, wptr, IP_ADDR_LEN);
1845 1845 ipif_refrele(ipif);
1846 1846 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutAddrMaskReps);
1847 1847 break;
1848 1848 case ICMP_ECHO_REQUEST:
1849 1849 icmph->icmph_type = ICMP_ECHO_REPLY;
1850 1850 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutEchoReps);
1851 1851 break;
1852 1852 case ICMP_TIME_STAMP_REQUEST: {
1853 1853 uint32_t *tsp;
1854 1854
1855 1855 icmph->icmph_type = ICMP_TIME_STAMP_REPLY;
1856 1856 tsp = (uint32_t *)wptr;
1857 1857 tsp++; /* Skip past 'originate time' */
1858 1858 /* Compute # of milliseconds since midnight */
1859 1859 gethrestime(&now);
1860 1860 ts = (now.tv_sec % (24 * 60 * 60)) * 1000 +
1861 1861 now.tv_nsec / (NANOSEC / MILLISEC);
1862 1862 *tsp++ = htonl(ts); /* Lay in 'receive time' */
1863 1863 *tsp++ = htonl(ts); /* Lay in 'send time' */
1864 1864 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimestampReps);
1865 1865 break;
1866 1866 }
1867 1867 default:
1868 1868 ipha = (ipha_t *)&icmph[1];
1869 1869 if ((uchar_t *)&ipha[1] > mp->b_wptr) {
1870 1870 if (!pullupmsg(mp, (uchar_t *)&ipha[1] - mp->b_rptr)) {
1871 1871 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1872 1872 freemsg(first_mp);
1873 1873 return;
1874 1874 }
1875 1875 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
1876 1876 ipha = (ipha_t *)&icmph[1];
1877 1877 }
1878 1878 if ((IPH_HDR_VERSION(ipha) != IPV4_VERSION)) {
1879 1879 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1880 1880 freemsg(first_mp);
1881 1881 return;
1882 1882 }
1883 1883 hdr_length = IPH_HDR_LENGTH(ipha);
1884 1884 if (hdr_length < sizeof (ipha_t)) {
1885 1885 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1886 1886 freemsg(first_mp);
1887 1887 return;
1888 1888 }
1889 1889 if ((uchar_t *)ipha + hdr_length > mp->b_wptr) {
1890 1890 if (!pullupmsg(mp,
1891 1891 (uchar_t *)ipha + hdr_length - mp->b_rptr)) {
1892 1892 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1893 1893 freemsg(first_mp);
1894 1894 return;
1895 1895 }
1896 1896 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
1897 1897 ipha = (ipha_t *)&icmph[1];
1898 1898 }
1899 1899 switch (icmph->icmph_type) {
1900 1900 case ICMP_REDIRECT:
1901 1901 /*
1902 1902 * As there is no upper client to deliver, we don't
1903 1903 * need the first_mp any more.
1904 1904 */
1905 1905 if (mctl_present) {
1906 1906 freeb(first_mp);
1907 1907 }
1908 1908 icmp_redirect(ill, mp);
1909 1909 return;
1910 1910 case ICMP_DEST_UNREACHABLE:
1911 1911 if (icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED) {
1912 1912 if (!icmp_inbound_too_big(icmph, ipha, ill,
1913 1913 zoneid, mp, iph_hdr_length, ipst)) {
1914 1914 freemsg(first_mp);
1915 1915 return;
1916 1916 }
1917 1917 /*
1918 1918 * icmp_inbound_too_big() may alter mp.
1919 1919 * Resynch ipha and icmph accordingly.
1920 1920 */
1921 1921 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
1922 1922 ipha = (ipha_t *)&icmph[1];
1923 1923 }
1924 1924 /* FALLTHRU */
1925 1925 default :
1926 1926 /*
1927 1927 * IPQoS notes: Since we have already done IPQoS
1928 1928 * processing we don't want to do it again in
1929 1929 * the fanout routines called by
1930 1930 * icmp_inbound_error_fanout, hence the last
1931 1931 * argument, ip_policy, is B_FALSE.
1932 1932 */
1933 1933 icmp_inbound_error_fanout(q, ill, first_mp, icmph,
1934 1934 ipha, iph_hdr_length, hdr_length, mctl_present,
1935 1935 B_FALSE, recv_ill, zoneid);
1936 1936 }
1937 1937 return;
1938 1938 }
1939 1939 /* Send out an ICMP packet */
1940 1940 icmph->icmph_checksum = 0;
1941 1941 icmph->icmph_checksum = IP_CSUM(mp, iph_hdr_length, 0);
1942 1942 if (broadcast || CLASSD(ipha->ipha_dst)) {
1943 1943 ipif_t *ipif_chosen;
1944 1944 /*
1945 1945 * Make it look like it was directed to us, so we don't look
1946 1946 * like a fool with a broadcast or multicast source address.
1947 1947 */
1948 1948 ipif = ipif_lookup_remote(ill, ipha->ipha_src, zoneid);
1949 1949 /*
1950 1950 * Make sure that we haven't grabbed an interface that's DOWN.
1951 1951 */
1952 1952 if (ipif != NULL) {
1953 1953 ipif_chosen = ipif_select_source(ipif->ipif_ill,
1954 1954 ipha->ipha_src, zoneid);
1955 1955 if (ipif_chosen != NULL) {
1956 1956 ipif_refrele(ipif);
1957 1957 ipif = ipif_chosen;
1958 1958 }
1959 1959 }
1960 1960 if (ipif == NULL) {
1961 1961 ip0dbg(("icmp_inbound: "
1962 1962 "No source for broadcast/multicast:\n"
1963 1963 "\tsrc 0x%x dst 0x%x ill %p "
1964 1964 "ipif_lcl_addr 0x%x\n",
1965 1965 ntohl(ipha->ipha_src), ntohl(ipha->ipha_dst),
1966 1966 (void *)ill,
1967 1967 ill->ill_ipif->ipif_lcl_addr));
1968 1968 freemsg(first_mp);
1969 1969 return;
1970 1970 }
1971 1971 ASSERT(ipif != NULL && !ipif->ipif_isv6);
1972 1972 ipha->ipha_dst = ipif->ipif_src_addr;
1973 1973 ipif_refrele(ipif);
1974 1974 }
1975 1975 /* Reset time to live. */
1976 1976 ipha->ipha_ttl = ipst->ips_ip_def_ttl;
1977 1977 {
1978 1978 /* Swap source and destination addresses */
1979 1979 ipaddr_t tmp;
1980 1980
1981 1981 tmp = ipha->ipha_src;
1982 1982 ipha->ipha_src = ipha->ipha_dst;
1983 1983 ipha->ipha_dst = tmp;
1984 1984 }
1985 1985 ipha->ipha_ident = 0;
1986 1986 if (!IS_SIMPLE_IPH(ipha))
1987 1987 icmp_options_update(ipha);
1988 1988
1989 1989 /*
1990 1990 * ICMP echo replies should go out on the same interface
1991 1991 * the request came on as probes used by in.mpathd for detecting
1992 1992 * NIC failures are ECHO packets. We turn-off load spreading
1993 1993 * by setting ipsec_in_attach_if to B_TRUE, which is copied
1994 1994 * to ipsec_out_attach_if by ipsec_in_to_out called later in this
1995 1995 * function. This is in turn handled by ip_wput and ip_newroute
1996 1996 * to make sure that the packet goes out on the interface it came
1997 1997 * in on. If we don't turnoff load spreading, the packets might get
1998 1998 * dropped if there are no non-FAILED/INACTIVE interfaces for it
1999 1999 * to go out and in.mpathd would wrongly detect a failure or
2000 2000 * mis-detect a NIC failure for link failure. As load spreading
2001 2001 * can happen only if ill_group is not NULL, we do only for
2002 2002 * that case and this does not affect the normal case.
2003 2003 *
2004 2004 * We turn off load spreading only on echo packets that came from
2005 2005 * on-link hosts. If the interface route has been deleted, this will
2006 2006 * not be enforced as we can't do much. For off-link hosts, as the
2007 2007 * default routes in IPv4 does not typically have an ire_ipif
2008 2008 * pointer, we can't force MATCH_IRE_ILL in ip_wput/ip_newroute.
2009 2009 * Moreover, expecting a default route through this interface may
2010 2010 * not be correct. We use ipha_dst because of the swap above.
2011 2011 */
2012 2012 onlink = B_FALSE;
2013 2013 if (icmph->icmph_type == ICMP_ECHO_REPLY && ill->ill_group != NULL) {
2014 2014 /*
2015 2015 * First, we need to make sure that it is not one of our
2016 2016 * local addresses. If we set onlink when it is one of
2017 2017 * our local addresses, we will end up creating IRE_CACHES
2018 2018 * for one of our local addresses. Then, we will never
2019 2019 * accept packets for them afterwards.
2020 2020 */
2021 2021 src_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_LOCAL,
2022 2022 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
2023 2023 if (src_ire == NULL) {
2024 2024 ipif = ipif_get_next_ipif(NULL, ill);
2025 2025 if (ipif == NULL) {
2026 2026 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2027 2027 freemsg(mp);
2028 2028 return;
2029 2029 }
2030 2030 src_ire = ire_ftable_lookup(ipha->ipha_dst, 0, 0,
2031 2031 IRE_INTERFACE, ipif, NULL, ALL_ZONES, 0,
2032 2032 NULL, MATCH_IRE_ILL | MATCH_IRE_TYPE, ipst);
2033 2033 ipif_refrele(ipif);
2034 2034 if (src_ire != NULL) {
2035 2035 onlink = B_TRUE;
2036 2036 ire_refrele(src_ire);
2037 2037 }
2038 2038 } else {
2039 2039 ire_refrele(src_ire);
2040 2040 }
2041 2041 }
2042 2042 if (!mctl_present) {
2043 2043 /*
2044 2044 * This packet should go out the same way as it
2045 2045 * came in i.e in clear. To make sure that global
2046 2046 * policy will not be applied to this in ip_wput_ire,
2047 2047 * we attach a IPSEC_IN mp and clear ipsec_in_secure.
2048 2048 */
2049 2049 ASSERT(first_mp == mp);
2050 2050 first_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack);
2051 2051 if (first_mp == NULL) {
2052 2052 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2053 2053 freemsg(mp);
2054 2054 return;
2055 2055 }
2056 2056 ii = (ipsec_in_t *)first_mp->b_rptr;
2057 2057
2058 2058 /* This is not a secure packet */
2059 2059 ii->ipsec_in_secure = B_FALSE;
2060 2060 if (onlink) {
2061 2061 ii->ipsec_in_attach_if = B_TRUE;
2062 2062 ii->ipsec_in_ill_index =
2063 2063 ill->ill_phyint->phyint_ifindex;
2064 2064 ii->ipsec_in_rill_index =
2065 2065 recv_ill->ill_phyint->phyint_ifindex;
2066 2066 }
2067 2067 first_mp->b_cont = mp;
2068 2068 } else if (onlink) {
2069 2069 ii = (ipsec_in_t *)first_mp->b_rptr;
2070 2070 ii->ipsec_in_attach_if = B_TRUE;
2071 2071 ii->ipsec_in_ill_index = ill->ill_phyint->phyint_ifindex;
2072 2072 ii->ipsec_in_rill_index = recv_ill->ill_phyint->phyint_ifindex;
2073 2073 ii->ipsec_in_ns = ipst->ips_netstack; /* No netstack_hold */
2074 2074 } else {
2075 2075 ii = (ipsec_in_t *)first_mp->b_rptr;
2076 2076 ii->ipsec_in_ns = ipst->ips_netstack; /* No netstack_hold */
2077 2077 }
2078 2078 ii->ipsec_in_zoneid = zoneid;
2079 2079 ASSERT(zoneid != ALL_ZONES);
2080 2080 if (!ipsec_in_to_out(first_mp, ipha, NULL)) {
2081 2081 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2082 2082 return;
2083 2083 }
2084 2084 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs);
2085 2085 put(WR(q), first_mp);
2086 2086 }
2087 2087
2088 2088 static ipaddr_t
2089 2089 icmp_get_nexthop_addr(ipha_t *ipha, ill_t *ill, zoneid_t zoneid, mblk_t *mp)
2090 2090 {
2091 2091 conn_t *connp;
2092 2092 connf_t *connfp;
2093 2093 ipaddr_t nexthop_addr = INADDR_ANY;
2094 2094 int hdr_length = IPH_HDR_LENGTH(ipha);
2095 2095 uint16_t *up;
2096 2096 uint32_t ports;
2097 2097 ip_stack_t *ipst = ill->ill_ipst;
2098 2098
2099 2099 up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2100 2100 switch (ipha->ipha_protocol) {
2101 2101 case IPPROTO_TCP:
2102 2102 {
2103 2103 tcph_t *tcph;
2104 2104
2105 2105 /* do a reverse lookup */
2106 2106 tcph = (tcph_t *)((uchar_t *)ipha + hdr_length);
2107 2107 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcph,
2108 2108 TCPS_LISTEN, ipst);
2109 2109 break;
2110 2110 }
2111 2111 case IPPROTO_UDP:
2112 2112 {
2113 2113 uint32_t dstport, srcport;
2114 2114
2115 2115 ((uint16_t *)&ports)[0] = up[1];
2116 2116 ((uint16_t *)&ports)[1] = up[0];
2117 2117
2118 2118 /* Extract ports in net byte order */
2119 2119 dstport = htons(ntohl(ports) & 0xFFFF);
2120 2120 srcport = htons(ntohl(ports) >> 16);
2121 2121
2122 2122 connfp = &ipst->ips_ipcl_udp_fanout[
2123 2123 IPCL_UDP_HASH(dstport, ipst)];
2124 2124 mutex_enter(&connfp->connf_lock);
2125 2125 connp = connfp->connf_head;
2126 2126
2127 2127 /* do a reverse lookup */
2128 2128 while ((connp != NULL) &&
2129 2129 (!IPCL_UDP_MATCH(connp, dstport,
2130 2130 ipha->ipha_src, srcport, ipha->ipha_dst) ||
2131 2131 !IPCL_ZONE_MATCH(connp, zoneid))) {
2132 2132 connp = connp->conn_next;
2133 2133 }
2134 2134 if (connp != NULL)
2135 2135 CONN_INC_REF(connp);
2136 2136 mutex_exit(&connfp->connf_lock);
2137 2137 break;
2138 2138 }
2139 2139 case IPPROTO_SCTP:
2140 2140 {
2141 2141 in6_addr_t map_src, map_dst;
2142 2142
2143 2143 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &map_src);
2144 2144 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &map_dst);
2145 2145 ((uint16_t *)&ports)[0] = up[1];
2146 2146 ((uint16_t *)&ports)[1] = up[0];
2147 2147
2148 2148 connp = sctp_find_conn(&map_src, &map_dst, ports,
2149 2149 zoneid, ipst->ips_netstack->netstack_sctp);
2150 2150 if (connp == NULL) {
2151 2151 connp = ipcl_classify_raw(mp, IPPROTO_SCTP,
2152 2152 zoneid, ports, ipha, ipst);
2153 2153 } else {
2154 2154 CONN_INC_REF(connp);
2155 2155 SCTP_REFRELE(CONN2SCTP(connp));
2156 2156 }
2157 2157 break;
2158 2158 }
2159 2159 default:
2160 2160 {
2161 2161 ipha_t ripha;
2162 2162
2163 2163 ripha.ipha_src = ipha->ipha_dst;
2164 2164 ripha.ipha_dst = ipha->ipha_src;
2165 2165 ripha.ipha_protocol = ipha->ipha_protocol;
2166 2166
2167 2167 connfp = &ipst->ips_ipcl_proto_fanout[
2168 2168 ipha->ipha_protocol];
2169 2169 mutex_enter(&connfp->connf_lock);
2170 2170 connp = connfp->connf_head;
2171 2171 for (connp = connfp->connf_head; connp != NULL;
2172 2172 connp = connp->conn_next) {
2173 2173 if (IPCL_PROTO_MATCH(connp,
2174 2174 ipha->ipha_protocol, &ripha, ill,
2175 2175 0, zoneid)) {
2176 2176 CONN_INC_REF(connp);
2177 2177 break;
2178 2178 }
2179 2179 }
2180 2180 mutex_exit(&connfp->connf_lock);
2181 2181 }
2182 2182 }
2183 2183 if (connp != NULL) {
2184 2184 if (connp->conn_nexthop_set)
2185 2185 nexthop_addr = connp->conn_nexthop_v4;
2186 2186 CONN_DEC_REF(connp);
2187 2187 }
2188 2188 return (nexthop_addr);
2189 2189 }
2190 2190
2191 2191 /* Table from RFC 1191 */
2192 2192 static int icmp_frag_size_table[] =
2193 2193 { 32000, 17914, 8166, 4352, 2002, 1496, 1006, 508, 296, 68 };
2194 2194
2195 2195 /*
2196 2196 * Process received ICMP Packet too big.
2197 2197 * After updating any IRE it does the fanout to any matching transport streams.
2198 2198 * Assumes the message has been pulled up till the IP header that caused
2199 2199 * the error.
2200 2200 *
2201 2201 * Returns B_FALSE on failure and B_TRUE on success.
2202 2202 */
2203 2203 static boolean_t
2204 2204 icmp_inbound_too_big(icmph_t *icmph, ipha_t *ipha, ill_t *ill,
2205 2205 zoneid_t zoneid, mblk_t *mp, int iph_hdr_length,
2206 2206 ip_stack_t *ipst)
2207 2207 {
2208 2208 ire_t *ire, *first_ire;
2209 2209 int mtu;
2210 2210 int hdr_length;
2211 2211 ipaddr_t nexthop_addr;
2212 2212
2213 2213 ASSERT(icmph->icmph_type == ICMP_DEST_UNREACHABLE &&
2214 2214 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED);
2215 2215 ASSERT(ill != NULL);
2216 2216
2217 2217 hdr_length = IPH_HDR_LENGTH(ipha);
2218 2218
2219 2219 /* Drop if the original packet contained a source route */
2220 2220 if (ip_source_route_included(ipha)) {
2221 2221 return (B_FALSE);
2222 2222 }
2223 2223 /*
2224 2224 * Verify we have atleast ICMP_MIN_TP_HDR_LENGTH bytes of transport
2225 2225 * header.
2226 2226 */
2227 2227 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
2228 2228 mp->b_wptr) {
2229 2229 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length +
2230 2230 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) {
2231 2231 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2232 2232 ip1dbg(("icmp_inbound_too_big: insufficient hdr\n"));
2233 2233 return (B_FALSE);
2234 2234 }
2235 2235 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2236 2236 ipha = (ipha_t *)&icmph[1];
2237 2237 }
2238 2238 nexthop_addr = icmp_get_nexthop_addr(ipha, ill, zoneid, mp);
2239 2239 if (nexthop_addr != INADDR_ANY) {
2240 2240 /* nexthop set */
2241 2241 first_ire = ire_ctable_lookup(ipha->ipha_dst,
2242 2242 nexthop_addr, 0, NULL, ALL_ZONES, MBLK_GETLABEL(mp),
2243 2243 MATCH_IRE_MARK_PRIVATE_ADDR | MATCH_IRE_GW, ipst);
2244 2244 } else {
2245 2245 /* nexthop not set */
2246 2246 first_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_CACHE,
2247 2247 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
2248 2248 }
2249 2249
2250 2250 if (!first_ire) {
2251 2251 ip1dbg(("icmp_inbound_too_big: no route for 0x%x\n",
2252 2252 ntohl(ipha->ipha_dst)));
2253 2253 return (B_FALSE);
2254 2254 }
2255 2255 /* Check for MTU discovery advice as described in RFC 1191 */
2256 2256 mtu = ntohs(icmph->icmph_du_mtu);
2257 2257 rw_enter(&first_ire->ire_bucket->irb_lock, RW_READER);
2258 2258 for (ire = first_ire; ire != NULL && ire->ire_addr == ipha->ipha_dst;
2259 2259 ire = ire->ire_next) {
2260 2260 /*
2261 2261 * Look for the connection to which this ICMP message is
2262 2262 * directed. If it has the IP_NEXTHOP option set, then the
2263 2263 * search is limited to IREs with the MATCH_IRE_PRIVATE
2264 2264 * option. Else the search is limited to regular IREs.
2265 2265 */
2266 2266 if (((ire->ire_marks & IRE_MARK_PRIVATE_ADDR) &&
2267 2267 (nexthop_addr != ire->ire_gateway_addr)) ||
2268 2268 (!(ire->ire_marks & IRE_MARK_PRIVATE_ADDR) &&
2269 2269 (nexthop_addr != INADDR_ANY)))
2270 2270 continue;
2271 2271
2272 2272 mutex_enter(&ire->ire_lock);
2273 2273 if (icmph->icmph_du_zero == 0 && mtu > 68) {
2274 2274 /* Reduce the IRE max frag value as advised. */
2275 2275 ip1dbg(("Received mtu from router: %d (was %d)\n",
2276 2276 mtu, ire->ire_max_frag));
2277 2277 ire->ire_max_frag = MIN(ire->ire_max_frag, mtu);
2278 2278 } else {
2279 2279 uint32_t length;
2280 2280 int i;
2281 2281
2282 2282 /*
2283 2283 * Use the table from RFC 1191 to figure out
2284 2284 * the next "plateau" based on the length in
2285 2285 * the original IP packet.
2286 2286 */
2287 2287 length = ntohs(ipha->ipha_length);
2288 2288 if (ire->ire_max_frag <= length &&
2289 2289 ire->ire_max_frag >= length - hdr_length) {
2290 2290 /*
2291 2291 * Handle broken BSD 4.2 systems that
2292 2292 * return the wrong iph_length in ICMP
2293 2293 * errors.
2294 2294 */
2295 2295 ip1dbg(("Wrong mtu: sent %d, ire %d\n",
2296 2296 length, ire->ire_max_frag));
2297 2297 length -= hdr_length;
2298 2298 }
2299 2299 for (i = 0; i < A_CNT(icmp_frag_size_table); i++) {
2300 2300 if (length > icmp_frag_size_table[i])
2301 2301 break;
2302 2302 }
2303 2303 if (i == A_CNT(icmp_frag_size_table)) {
2304 2304 /* Smaller than 68! */
2305 2305 ip1dbg(("Too big for packet size %d\n",
2306 2306 length));
2307 2307 ire->ire_max_frag = MIN(ire->ire_max_frag, 576);
2308 2308 ire->ire_frag_flag = 0;
2309 2309 } else {
2310 2310 mtu = icmp_frag_size_table[i];
2311 2311 ip1dbg(("Calculated mtu %d, packet size %d, "
2312 2312 "before %d", mtu, length,
2313 2313 ire->ire_max_frag));
2314 2314 ire->ire_max_frag = MIN(ire->ire_max_frag, mtu);
2315 2315 ip1dbg((", after %d\n", ire->ire_max_frag));
2316 2316 }
2317 2317 /* Record the new max frag size for the ULP. */
2318 2318 icmph->icmph_du_zero = 0;
2319 2319 icmph->icmph_du_mtu =
2320 2320 htons((uint16_t)ire->ire_max_frag);
2321 2321 }
2322 2322 mutex_exit(&ire->ire_lock);
2323 2323 }
2324 2324 rw_exit(&first_ire->ire_bucket->irb_lock);
2325 2325 ire_refrele(first_ire);
2326 2326 return (B_TRUE);
2327 2327 }
2328 2328
2329 2329 /*
2330 2330 * If the packet in error is Self-Encapsulated, icmp_inbound_error_fanout
2331 2331 * calls this function.
2332 2332 */
2333 2333 static mblk_t *
2334 2334 icmp_inbound_self_encap_error(mblk_t *mp, int iph_hdr_length, int hdr_length)
2335 2335 {
2336 2336 ipha_t *ipha;
2337 2337 icmph_t *icmph;
2338 2338 ipha_t *in_ipha;
2339 2339 int length;
2340 2340
2341 2341 ASSERT(mp->b_datap->db_type == M_DATA);
2342 2342
2343 2343 /*
2344 2344 * For Self-encapsulated packets, we added an extra IP header
2345 2345 * without the options. Inner IP header is the one from which
2346 2346 * the outer IP header was formed. Thus, we need to remove the
2347 2347 * outer IP header. To do this, we pullup the whole message
2348 2348 * and overlay whatever follows the outer IP header over the
2349 2349 * outer IP header.
2350 2350 */
2351 2351
2352 2352 if (!pullupmsg(mp, -1))
2353 2353 return (NULL);
2354 2354
2355 2355 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2356 2356 ipha = (ipha_t *)&icmph[1];
2357 2357 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length);
2358 2358
2359 2359 /*
2360 2360 * The length that we want to overlay is following the inner
2361 2361 * IP header. Subtracting the IP header + icmp header + outer
2362 2362 * IP header's length should give us the length that we want to
2363 2363 * overlay.
2364 2364 */
2365 2365 length = msgdsize(mp) - iph_hdr_length - sizeof (icmph_t) -
2366 2366 hdr_length;
2367 2367 /*
2368 2368 * Overlay whatever follows the inner header over the
2369 2369 * outer header.
2370 2370 */
2371 2371 bcopy((uchar_t *)in_ipha, (uchar_t *)ipha, length);
2372 2372
2373 2373 /* Set the wptr to account for the outer header */
2374 2374 mp->b_wptr -= hdr_length;
2375 2375 return (mp);
2376 2376 }
2377 2377
2378 2378 /*
2379 2379 * Try to pass the ICMP message upstream in case the ULP cares.
2380 2380 *
2381 2381 * If the packet that caused the ICMP error is secure, we send
2382 2382 * it to AH/ESP to make sure that the attached packet has a
2383 2383 * valid association. ipha in the code below points to the
2384 2384 * IP header of the packet that caused the error.
2385 2385 *
2386 2386 * We handle ICMP_FRAGMENTATION_NEEDED(IFN) message differently
2387 2387 * in the context of IPsec. Normally we tell the upper layer
2388 2388 * whenever we send the ire (including ip_bind), the IPsec header
2389 2389 * length in ire_ipsec_overhead. TCP can deduce the MSS as it
2390 2390 * has both the MTU (ire_max_frag) and the ire_ipsec_overhead.
2391 2391 * Similarly, we pass the new MTU icmph_du_mtu and TCP does the
2392 2392 * same thing. As TCP has the IPsec options size that needs to be
2393 2393 * adjusted, we just pass the MTU unchanged.
2394 2394 *
2395 2395 * IFN could have been generated locally or by some router.
2396 2396 *
2397 2397 * LOCAL : *ip_wput_ire -> icmp_frag_needed could have generated this.
2398 2398 * This happens because IP adjusted its value of MTU on an
2399 2399 * earlier IFN message and could not tell the upper layer,
2400 2400 * the new adjusted value of MTU e.g. Packet was encrypted
2401 2401 * or there was not enough information to fanout to upper
2402 2402 * layers. Thus on the next outbound datagram, ip_wput_ire
2403 2403 * generates the IFN, where IPsec processing has *not* been
2404 2404 * done.
2405 2405 *
2406 2406 * *ip_wput_ire_fragmentit -> ip_wput_frag -> icmp_frag_needed
2407 2407 * could have generated this. This happens because ire_max_frag
2408 2408 * value in IP was set to a new value, while the IPsec processing
2409 2409 * was being done and after we made the fragmentation check in
2410 2410 * ip_wput_ire. Thus on return from IPsec processing,
2411 2411 * ip_wput_ipsec_out finds that the new length is > ire_max_frag
2412 2412 * and generates the IFN. As IPsec processing is over, we fanout
2413 2413 * to AH/ESP to remove the header.
2414 2414 *
2415 2415 * In both these cases, ipsec_in_loopback will be set indicating
2416 2416 * that IFN was generated locally.
2417 2417 *
2418 2418 * ROUTER : IFN could be secure or non-secure.
2419 2419 *
2420 2420 * * SECURE : We use the IPSEC_IN to fanout to AH/ESP if the
2421 2421 * packet in error has AH/ESP headers to validate the AH/ESP
2422 2422 * headers. AH/ESP will verify whether there is a valid SA or
2423 2423 * not and send it back. We will fanout again if we have more
2424 2424 * data in the packet.
2425 2425 *
2426 2426 * If the packet in error does not have AH/ESP, we handle it
2427 2427 * like any other case.
2428 2428 *
2429 2429 * * NON_SECURE : If the packet in error has AH/ESP headers,
2430 2430 * we attach a dummy ipsec_in and send it up to AH/ESP
2431 2431 * for validation. AH/ESP will verify whether there is a
2432 2432 * valid SA or not and send it back. We will fanout again if
2433 2433 * we have more data in the packet.
2434 2434 *
2435 2435 * If the packet in error does not have AH/ESP, we handle it
2436 2436 * like any other case.
2437 2437 */
2438 2438 static void
2439 2439 icmp_inbound_error_fanout(queue_t *q, ill_t *ill, mblk_t *mp,
2440 2440 icmph_t *icmph, ipha_t *ipha, int iph_hdr_length, int hdr_length,
2441 2441 boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill,
2442 2442 zoneid_t zoneid)
2443 2443 {
2444 2444 uint16_t *up; /* Pointer to ports in ULP header */
2445 2445 uint32_t ports; /* reversed ports for fanout */
2446 2446 ipha_t ripha; /* With reversed addresses */
2447 2447 mblk_t *first_mp;
2448 2448 ipsec_in_t *ii;
2449 2449 tcph_t *tcph;
2450 2450 conn_t *connp;
2451 2451 ip_stack_t *ipst;
2452 2452
2453 2453 ASSERT(ill != NULL);
2454 2454
2455 2455 ASSERT(recv_ill != NULL);
2456 2456 ipst = recv_ill->ill_ipst;
2457 2457
2458 2458 first_mp = mp;
2459 2459 if (mctl_present) {
2460 2460 mp = first_mp->b_cont;
2461 2461 ASSERT(mp != NULL);
2462 2462
2463 2463 ii = (ipsec_in_t *)first_mp->b_rptr;
2464 2464 ASSERT(ii->ipsec_in_type == IPSEC_IN);
2465 2465 } else {
2466 2466 ii = NULL;
2467 2467 }
2468 2468
2469 2469 switch (ipha->ipha_protocol) {
2470 2470 case IPPROTO_UDP:
2471 2471 /*
2472 2472 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
2473 2473 * transport header.
2474 2474 */
2475 2475 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
2476 2476 mp->b_wptr) {
2477 2477 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length +
2478 2478 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) {
2479 2479 goto discard_pkt;
2480 2480 }
2481 2481 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2482 2482 ipha = (ipha_t *)&icmph[1];
2483 2483 }
2484 2484 up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2485 2485
2486 2486 /*
2487 2487 * Attempt to find a client stream based on port.
2488 2488 * Note that we do a reverse lookup since the header is
2489 2489 * in the form we sent it out.
2490 2490 * The ripha header is only used for the IP_UDP_MATCH and we
2491 2491 * only set the src and dst addresses and protocol.
2492 2492 */
2493 2493 ripha.ipha_src = ipha->ipha_dst;
2494 2494 ripha.ipha_dst = ipha->ipha_src;
2495 2495 ripha.ipha_protocol = ipha->ipha_protocol;
2496 2496 ((uint16_t *)&ports)[0] = up[1];
2497 2497 ((uint16_t *)&ports)[1] = up[0];
2498 2498 ip2dbg(("icmp_inbound_error: UDP %x:%d to %x:%d: %d/%d\n",
2499 2499 ntohl(ipha->ipha_src), ntohs(up[0]),
2500 2500 ntohl(ipha->ipha_dst), ntohs(up[1]),
2501 2501 icmph->icmph_type, icmph->icmph_code));
2502 2502
2503 2503 /* Have to change db_type after any pullupmsg */
2504 2504 DB_TYPE(mp) = M_CTL;
2505 2505
2506 2506 ip_fanout_udp(q, first_mp, ill, &ripha, ports, B_FALSE, 0,
2507 2507 mctl_present, ip_policy, recv_ill, zoneid);
2508 2508 return;
2509 2509
2510 2510 case IPPROTO_TCP:
2511 2511 /*
2512 2512 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
2513 2513 * transport header.
2514 2514 */
2515 2515 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
2516 2516 mp->b_wptr) {
2517 2517 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length +
2518 2518 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) {
2519 2519 goto discard_pkt;
2520 2520 }
2521 2521 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2522 2522 ipha = (ipha_t *)&icmph[1];
2523 2523 }
2524 2524 /*
2525 2525 * Find a TCP client stream for this packet.
2526 2526 * Note that we do a reverse lookup since the header is
2527 2527 * in the form we sent it out.
2528 2528 */
2529 2529 tcph = (tcph_t *)((uchar_t *)ipha + hdr_length);
2530 2530 connp = ipcl_tcp_lookup_reversed_ipv4(ipha, tcph, TCPS_LISTEN,
2531 2531 ipst);
2532 2532 if (connp == NULL)
2533 2533 goto discard_pkt;
2534 2534
2535 2535 /* Have to change db_type after any pullupmsg */
2536 2536 DB_TYPE(mp) = M_CTL;
2537 2537 squeue_fill(connp->conn_sqp, first_mp, tcp_input,
2538 2538 connp, SQTAG_TCP_INPUT_ICMP_ERR);
2539 2539 return;
2540 2540
2541 2541 case IPPROTO_SCTP:
2542 2542 /*
2543 2543 * Verify we have at least ICMP_MIN_TP_HDR_LEN bytes of
2544 2544 * transport header.
2545 2545 */
2546 2546 if ((uchar_t *)ipha + hdr_length + ICMP_MIN_TP_HDR_LEN >
2547 2547 mp->b_wptr) {
2548 2548 if (!pullupmsg(mp, (uchar_t *)ipha + hdr_length +
2549 2549 ICMP_MIN_TP_HDR_LEN - mp->b_rptr)) {
2550 2550 goto discard_pkt;
2551 2551 }
2552 2552 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2553 2553 ipha = (ipha_t *)&icmph[1];
2554 2554 }
2555 2555 up = (uint16_t *)((uchar_t *)ipha + hdr_length);
2556 2556 /*
2557 2557 * Find a SCTP client stream for this packet.
2558 2558 * Note that we do a reverse lookup since the header is
2559 2559 * in the form we sent it out.
2560 2560 * The ripha header is only used for the matching and we
2561 2561 * only set the src and dst addresses, protocol, and version.
2562 2562 */
2563 2563 ripha.ipha_src = ipha->ipha_dst;
2564 2564 ripha.ipha_dst = ipha->ipha_src;
2565 2565 ripha.ipha_protocol = ipha->ipha_protocol;
2566 2566 ripha.ipha_version_and_hdr_length =
2567 2567 ipha->ipha_version_and_hdr_length;
2568 2568 ((uint16_t *)&ports)[0] = up[1];
2569 2569 ((uint16_t *)&ports)[1] = up[0];
2570 2570
2571 2571 /* Have to change db_type after any pullupmsg */
2572 2572 DB_TYPE(mp) = M_CTL;
2573 2573 ip_fanout_sctp(first_mp, recv_ill, &ripha, ports, 0,
2574 2574 mctl_present, ip_policy, zoneid);
2575 2575 return;
2576 2576
2577 2577 case IPPROTO_ESP:
2578 2578 case IPPROTO_AH: {
2579 2579 int ipsec_rc;
2580 2580 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
2581 2581
2582 2582 /*
2583 2583 * We need a IPSEC_IN in the front to fanout to AH/ESP.
2584 2584 * We will re-use the IPSEC_IN if it is already present as
2585 2585 * AH/ESP will not affect any fields in the IPSEC_IN for
2586 2586 * ICMP errors. If there is no IPSEC_IN, allocate a new
2587 2587 * one and attach it in the front.
2588 2588 */
2589 2589 if (ii != NULL) {
2590 2590 /*
2591 2591 * ip_fanout_proto_again converts the ICMP errors
2592 2592 * that come back from AH/ESP to M_DATA so that
2593 2593 * if it is non-AH/ESP and we do a pullupmsg in
2594 2594 * this function, it would work. Convert it back
2595 2595 * to M_CTL before we send up as this is a ICMP
2596 2596 * error. This could have been generated locally or
2597 2597 * by some router. Validate the inner IPsec
2598 2598 * headers.
2599 2599 *
2600 2600 * NOTE : ill_index is used by ip_fanout_proto_again
2601 2601 * to locate the ill.
2602 2602 */
2603 2603 ASSERT(ill != NULL);
2604 2604 ii->ipsec_in_ill_index =
2605 2605 ill->ill_phyint->phyint_ifindex;
2606 2606 ii->ipsec_in_rill_index =
2607 2607 recv_ill->ill_phyint->phyint_ifindex;
2608 2608 DB_TYPE(first_mp->b_cont) = M_CTL;
2609 2609 } else {
2610 2610 /*
2611 2611 * IPSEC_IN is not present. We attach a ipsec_in
2612 2612 * message and send up to IPsec for validating
2613 2613 * and removing the IPsec headers. Clear
2614 2614 * ipsec_in_secure so that when we return
2615 2615 * from IPsec, we don't mistakenly think that this
2616 2616 * is a secure packet came from the network.
2617 2617 *
2618 2618 * NOTE : ill_index is used by ip_fanout_proto_again
2619 2619 * to locate the ill.
2620 2620 */
2621 2621 ASSERT(first_mp == mp);
2622 2622 first_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack);
2623 2623 if (first_mp == NULL) {
2624 2624 freemsg(mp);
2625 2625 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2626 2626 return;
2627 2627 }
2628 2628 ii = (ipsec_in_t *)first_mp->b_rptr;
2629 2629
2630 2630 /* This is not a secure packet */
2631 2631 ii->ipsec_in_secure = B_FALSE;
2632 2632 first_mp->b_cont = mp;
2633 2633 DB_TYPE(mp) = M_CTL;
2634 2634 ASSERT(ill != NULL);
2635 2635 ii->ipsec_in_ill_index =
2636 2636 ill->ill_phyint->phyint_ifindex;
2637 2637 ii->ipsec_in_rill_index =
2638 2638 recv_ill->ill_phyint->phyint_ifindex;
2639 2639 }
2640 2640 ip2dbg(("icmp_inbound_error: ipsec\n"));
2641 2641
2642 2642 if (!ipsec_loaded(ipss)) {
2643 2643 ip_proto_not_sup(q, first_mp, 0, zoneid, ipst);
2644 2644 return;
2645 2645 }
2646 2646
2647 2647 if (ipha->ipha_protocol == IPPROTO_ESP)
2648 2648 ipsec_rc = ipsecesp_icmp_error(first_mp);
2649 2649 else
2650 2650 ipsec_rc = ipsecah_icmp_error(first_mp);
2651 2651 if (ipsec_rc == IPSEC_STATUS_FAILED)
2652 2652 return;
2653 2653
2654 2654 ip_fanout_proto_again(first_mp, ill, recv_ill, NULL);
2655 2655 return;
2656 2656 }
2657 2657 default:
2658 2658 /*
2659 2659 * The ripha header is only used for the lookup and we
2660 2660 * only set the src and dst addresses and protocol.
2661 2661 */
2662 2662 ripha.ipha_src = ipha->ipha_dst;
2663 2663 ripha.ipha_dst = ipha->ipha_src;
2664 2664 ripha.ipha_protocol = ipha->ipha_protocol;
2665 2665 ip2dbg(("icmp_inbound_error: proto %d %x to %x: %d/%d\n",
2666 2666 ripha.ipha_protocol, ntohl(ipha->ipha_src),
2667 2667 ntohl(ipha->ipha_dst),
2668 2668 icmph->icmph_type, icmph->icmph_code));
2669 2669 if (ipha->ipha_protocol == IPPROTO_ENCAP) {
2670 2670 ipha_t *in_ipha;
2671 2671
2672 2672 if ((uchar_t *)ipha + hdr_length + sizeof (ipha_t) >
2673 2673 mp->b_wptr) {
2674 2674 if (!pullupmsg(mp, (uchar_t *)ipha +
2675 2675 hdr_length + sizeof (ipha_t) -
2676 2676 mp->b_rptr)) {
2677 2677 goto discard_pkt;
2678 2678 }
2679 2679 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2680 2680 ipha = (ipha_t *)&icmph[1];
2681 2681 }
2682 2682 /*
2683 2683 * Caller has verified that length has to be
2684 2684 * at least the size of IP header.
2685 2685 */
2686 2686 ASSERT(hdr_length >= sizeof (ipha_t));
2687 2687 /*
2688 2688 * Check the sanity of the inner IP header like
2689 2689 * we did for the outer header.
2690 2690 */
2691 2691 in_ipha = (ipha_t *)((uchar_t *)ipha + hdr_length);
2692 2692 if ((IPH_HDR_VERSION(in_ipha) != IPV4_VERSION)) {
2693 2693 goto discard_pkt;
2694 2694 }
2695 2695 if (IPH_HDR_LENGTH(in_ipha) < sizeof (ipha_t)) {
2696 2696 goto discard_pkt;
2697 2697 }
2698 2698 /* Check for Self-encapsulated tunnels */
2699 2699 if (in_ipha->ipha_src == ipha->ipha_src &&
2700 2700 in_ipha->ipha_dst == ipha->ipha_dst) {
2701 2701
2702 2702 mp = icmp_inbound_self_encap_error(mp,
2703 2703 iph_hdr_length, hdr_length);
2704 2704 if (mp == NULL)
2705 2705 goto discard_pkt;
2706 2706 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
2707 2707 ipha = (ipha_t *)&icmph[1];
2708 2708 hdr_length = IPH_HDR_LENGTH(ipha);
2709 2709 /*
2710 2710 * The packet in error is self-encapsualted.
2711 2711 * And we are finding it further encapsulated
2712 2712 * which we could not have possibly generated.
2713 2713 */
2714 2714 if (ipha->ipha_protocol == IPPROTO_ENCAP) {
2715 2715 goto discard_pkt;
2716 2716 }
2717 2717 icmp_inbound_error_fanout(q, ill, first_mp,
2718 2718 icmph, ipha, iph_hdr_length, hdr_length,
2719 2719 mctl_present, ip_policy, recv_ill, zoneid);
2720 2720 return;
2721 2721 }
2722 2722 }
2723 2723 if ((ipha->ipha_protocol == IPPROTO_ENCAP ||
2724 2724 ipha->ipha_protocol == IPPROTO_IPV6) &&
2725 2725 icmph->icmph_code == ICMP_FRAGMENTATION_NEEDED &&
2726 2726 ii != NULL &&
2727 2727 ii->ipsec_in_loopback &&
2728 2728 ii->ipsec_in_secure) {
2729 2729 /*
2730 2730 * For IP tunnels that get a looped-back
2731 2731 * ICMP_FRAGMENTATION_NEEDED message, adjust the
2732 2732 * reported new MTU to take into account the IPsec
2733 2733 * headers protecting this configured tunnel.
2734 2734 *
2735 2735 * This allows the tunnel module (tun.c) to blindly
2736 2736 * accept the MTU reported in an ICMP "too big"
2737 2737 * message.
2738 2738 *
2739 2739 * Non-looped back ICMP messages will just be
2740 2740 * handled by the security protocols (if needed),
2741 2741 * and the first subsequent packet will hit this
2742 2742 * path.
2743 2743 */
2744 2744 icmph->icmph_du_mtu = htons(ntohs(icmph->icmph_du_mtu) -
2745 2745 ipsec_in_extra_length(first_mp));
2746 2746 }
2747 2747 /* Have to change db_type after any pullupmsg */
2748 2748 DB_TYPE(mp) = M_CTL;
2749 2749
2750 2750 ip_fanout_proto(q, first_mp, ill, &ripha, 0, mctl_present,
2751 2751 ip_policy, recv_ill, zoneid);
2752 2752 return;
2753 2753 }
2754 2754 /* NOTREACHED */
2755 2755 discard_pkt:
2756 2756 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2757 2757 drop_pkt:;
2758 2758 ip1dbg(("icmp_inbound_error_fanout: drop pkt\n"));
2759 2759 freemsg(first_mp);
2760 2760 }
2761 2761
2762 2762 /*
2763 2763 * Common IP options parser.
2764 2764 *
2765 2765 * Setup routine: fill in *optp with options-parsing state, then
2766 2766 * tail-call ipoptp_next to return the first option.
2767 2767 */
2768 2768 uint8_t
2769 2769 ipoptp_first(ipoptp_t *optp, ipha_t *ipha)
2770 2770 {
2771 2771 uint32_t totallen; /* total length of all options */
2772 2772
2773 2773 totallen = ipha->ipha_version_and_hdr_length -
2774 2774 (uint8_t)((IP_VERSION << 4) + IP_SIMPLE_HDR_LENGTH_IN_WORDS);
2775 2775 totallen <<= 2;
2776 2776 optp->ipoptp_next = (uint8_t *)(&ipha[1]);
2777 2777 optp->ipoptp_end = optp->ipoptp_next + totallen;
2778 2778 optp->ipoptp_flags = 0;
2779 2779 return (ipoptp_next(optp));
2780 2780 }
2781 2781
2782 2782 /*
2783 2783 * Common IP options parser: extract next option.
2784 2784 */
2785 2785 uint8_t
2786 2786 ipoptp_next(ipoptp_t *optp)
2787 2787 {
2788 2788 uint8_t *end = optp->ipoptp_end;
2789 2789 uint8_t *cur = optp->ipoptp_next;
2790 2790 uint8_t opt, len, pointer;
2791 2791
2792 2792 /*
2793 2793 * If cur > end already, then the ipoptp_end or ipoptp_next pointer
2794 2794 * has been corrupted.
2795 2795 */
2796 2796 ASSERT(cur <= end);
2797 2797
2798 2798 if (cur == end)
2799 2799 return (IPOPT_EOL);
2800 2800
2801 2801 opt = cur[IPOPT_OPTVAL];
2802 2802
2803 2803 /*
2804 2804 * Skip any NOP options.
2805 2805 */
2806 2806 while (opt == IPOPT_NOP) {
2807 2807 cur++;
2808 2808 if (cur == end)
2809 2809 return (IPOPT_EOL);
2810 2810 opt = cur[IPOPT_OPTVAL];
2811 2811 }
2812 2812
2813 2813 if (opt == IPOPT_EOL)
2814 2814 return (IPOPT_EOL);
2815 2815
2816 2816 /*
2817 2817 * Option requiring a length.
2818 2818 */
2819 2819 if ((cur + 1) >= end) {
2820 2820 optp->ipoptp_flags |= IPOPTP_ERROR;
2821 2821 return (IPOPT_EOL);
2822 2822 }
2823 2823 len = cur[IPOPT_OLEN];
2824 2824 if (len < 2) {
2825 2825 optp->ipoptp_flags |= IPOPTP_ERROR;
2826 2826 return (IPOPT_EOL);
2827 2827 }
2828 2828 optp->ipoptp_cur = cur;
2829 2829 optp->ipoptp_len = len;
2830 2830 optp->ipoptp_next = cur + len;
2831 2831 if (cur + len > end) {
2832 2832 optp->ipoptp_flags |= IPOPTP_ERROR;
2833 2833 return (IPOPT_EOL);
2834 2834 }
2835 2835
2836 2836 /*
2837 2837 * For the options which require a pointer field, make sure
2838 2838 * its there, and make sure it points to either something
2839 2839 * inside this option, or the end of the option.
2840 2840 */
2841 2841 switch (opt) {
2842 2842 case IPOPT_RR:
2843 2843 case IPOPT_TS:
2844 2844 case IPOPT_LSRR:
2845 2845 case IPOPT_SSRR:
2846 2846 if (len <= IPOPT_OFFSET) {
2847 2847 optp->ipoptp_flags |= IPOPTP_ERROR;
2848 2848 return (opt);
2849 2849 }
2850 2850 pointer = cur[IPOPT_OFFSET];
2851 2851 if (pointer - 1 > len) {
2852 2852 optp->ipoptp_flags |= IPOPTP_ERROR;
2853 2853 return (opt);
2854 2854 }
2855 2855 break;
2856 2856 }
2857 2857
2858 2858 /*
2859 2859 * Sanity check the pointer field based on the type of the
2860 2860 * option.
2861 2861 */
2862 2862 switch (opt) {
2863 2863 case IPOPT_RR:
2864 2864 case IPOPT_SSRR:
2865 2865 case IPOPT_LSRR:
2866 2866 if (pointer < IPOPT_MINOFF_SR)
2867 2867 optp->ipoptp_flags |= IPOPTP_ERROR;
2868 2868 break;
2869 2869 case IPOPT_TS:
2870 2870 if (pointer < IPOPT_MINOFF_IT)
2871 2871 optp->ipoptp_flags |= IPOPTP_ERROR;
2872 2872 /*
2873 2873 * Note that the Internet Timestamp option also
2874 2874 * contains two four bit fields (the Overflow field,
2875 2875 * and the Flag field), which follow the pointer
2876 2876 * field. We don't need to check that these fields
2877 2877 * fall within the length of the option because this
2878 2878 * was implicitely done above. We've checked that the
2879 2879 * pointer value is at least IPOPT_MINOFF_IT, and that
2880 2880 * it falls within the option. Since IPOPT_MINOFF_IT >
2881 2881 * IPOPT_POS_OV_FLG, we don't need the explicit check.
2882 2882 */
2883 2883 ASSERT(len > IPOPT_POS_OV_FLG);
2884 2884 break;
2885 2885 }
2886 2886
2887 2887 return (opt);
2888 2888 }
2889 2889
2890 2890 /*
2891 2891 * Use the outgoing IP header to create an IP_OPTIONS option the way
2892 2892 * it was passed down from the application.
2893 2893 */
2894 2894 int
2895 2895 ip_opt_get_user(const ipha_t *ipha, uchar_t *buf)
2896 2896 {
2897 2897 ipoptp_t opts;
2898 2898 const uchar_t *opt;
2899 2899 uint8_t optval;
2900 2900 uint8_t optlen;
2901 2901 uint32_t len = 0;
2902 2902 uchar_t *buf1 = buf;
2903 2903
2904 2904 buf += IP_ADDR_LEN; /* Leave room for final destination */
2905 2905 len += IP_ADDR_LEN;
2906 2906 bzero(buf1, IP_ADDR_LEN);
2907 2907
2908 2908 /*
2909 2909 * OK to cast away const here, as we don't store through the returned
2910 2910 * opts.ipoptp_cur pointer.
2911 2911 */
2912 2912 for (optval = ipoptp_first(&opts, (ipha_t *)ipha);
2913 2913 optval != IPOPT_EOL;
2914 2914 optval = ipoptp_next(&opts)) {
2915 2915 int off;
2916 2916
2917 2917 opt = opts.ipoptp_cur;
2918 2918 optlen = opts.ipoptp_len;
2919 2919 switch (optval) {
2920 2920 case IPOPT_SSRR:
2921 2921 case IPOPT_LSRR:
2922 2922
2923 2923 /*
2924 2924 * Insert ipha_dst as the first entry in the source
2925 2925 * route and move down the entries on step.
2926 2926 * The last entry gets placed at buf1.
2927 2927 */
2928 2928 buf[IPOPT_OPTVAL] = optval;
2929 2929 buf[IPOPT_OLEN] = optlen;
2930 2930 buf[IPOPT_OFFSET] = optlen;
2931 2931
2932 2932 off = optlen - IP_ADDR_LEN;
2933 2933 if (off < 0) {
2934 2934 /* No entries in source route */
2935 2935 break;
2936 2936 }
2937 2937 /* Last entry in source route */
2938 2938 bcopy(opt + off, buf1, IP_ADDR_LEN);
2939 2939 off -= IP_ADDR_LEN;
2940 2940
2941 2941 while (off > 0) {
2942 2942 bcopy(opt + off,
2943 2943 buf + off + IP_ADDR_LEN,
2944 2944 IP_ADDR_LEN);
2945 2945 off -= IP_ADDR_LEN;
2946 2946 }
2947 2947 /* ipha_dst into first slot */
2948 2948 bcopy(&ipha->ipha_dst,
2949 2949 buf + off + IP_ADDR_LEN,
2950 2950 IP_ADDR_LEN);
2951 2951 buf += optlen;
2952 2952 len += optlen;
2953 2953 break;
2954 2954
2955 2955 case IPOPT_COMSEC:
2956 2956 case IPOPT_SECURITY:
2957 2957 /* if passing up a label is not ok, then remove */
2958 2958 if (is_system_labeled())
2959 2959 break;
2960 2960 /* FALLTHROUGH */
2961 2961 default:
2962 2962 bcopy(opt, buf, optlen);
2963 2963 buf += optlen;
2964 2964 len += optlen;
2965 2965 break;
2966 2966 }
2967 2967 }
2968 2968 done:
2969 2969 /* Pad the resulting options */
2970 2970 while (len & 0x3) {
2971 2971 *buf++ = IPOPT_EOL;
2972 2972 len++;
2973 2973 }
2974 2974 return (len);
2975 2975 }
2976 2976
2977 2977 /*
2978 2978 * Update any record route or timestamp options to include this host.
2979 2979 * Reverse any source route option.
2980 2980 * This routine assumes that the options are well formed i.e. that they
2981 2981 * have already been checked.
2982 2982 */
2983 2983 static void
2984 2984 icmp_options_update(ipha_t *ipha)
2985 2985 {
2986 2986 ipoptp_t opts;
2987 2987 uchar_t *opt;
2988 2988 uint8_t optval;
2989 2989 ipaddr_t src; /* Our local address */
2990 2990 ipaddr_t dst;
2991 2991
2992 2992 ip2dbg(("icmp_options_update\n"));
2993 2993 src = ipha->ipha_src;
2994 2994 dst = ipha->ipha_dst;
2995 2995
2996 2996 for (optval = ipoptp_first(&opts, ipha);
2997 2997 optval != IPOPT_EOL;
2998 2998 optval = ipoptp_next(&opts)) {
2999 2999 ASSERT((opts.ipoptp_flags & IPOPTP_ERROR) == 0);
3000 3000 opt = opts.ipoptp_cur;
3001 3001 ip2dbg(("icmp_options_update: opt %d, len %d\n",
3002 3002 optval, opts.ipoptp_len));
3003 3003 switch (optval) {
3004 3004 int off1, off2;
3005 3005 case IPOPT_SSRR:
3006 3006 case IPOPT_LSRR:
3007 3007 /*
3008 3008 * Reverse the source route. The first entry
3009 3009 * should be the next to last one in the current
3010 3010 * source route (the last entry is our address).
3011 3011 * The last entry should be the final destination.
3012 3012 */
3013 3013 off1 = IPOPT_MINOFF_SR - 1;
3014 3014 off2 = opt[IPOPT_OFFSET] - IP_ADDR_LEN - 1;
3015 3015 if (off2 < 0) {
3016 3016 /* No entries in source route */
3017 3017 ip1dbg((
3018 3018 "icmp_options_update: bad src route\n"));
3019 3019 break;
3020 3020 }
3021 3021 bcopy((char *)opt + off2, &dst, IP_ADDR_LEN);
3022 3022 bcopy(&ipha->ipha_dst, (char *)opt + off2, IP_ADDR_LEN);
3023 3023 bcopy(&dst, &ipha->ipha_dst, IP_ADDR_LEN);
3024 3024 off2 -= IP_ADDR_LEN;
3025 3025
3026 3026 while (off1 < off2) {
3027 3027 bcopy((char *)opt + off1, &src, IP_ADDR_LEN);
3028 3028 bcopy((char *)opt + off2, (char *)opt + off1,
3029 3029 IP_ADDR_LEN);
3030 3030 bcopy(&src, (char *)opt + off2, IP_ADDR_LEN);
3031 3031 off1 += IP_ADDR_LEN;
3032 3032 off2 -= IP_ADDR_LEN;
3033 3033 }
3034 3034 opt[IPOPT_OFFSET] = IPOPT_MINOFF_SR;
3035 3035 break;
3036 3036 }
3037 3037 }
3038 3038 }
3039 3039
3040 3040 /*
3041 3041 * Process received ICMP Redirect messages.
3042 3042 */
3043 3043 static void
3044 3044 icmp_redirect(ill_t *ill, mblk_t *mp)
3045 3045 {
3046 3046 ipha_t *ipha;
3047 3047 int iph_hdr_length;
3048 3048 icmph_t *icmph;
3049 3049 ipha_t *ipha_err;
3050 3050 ire_t *ire;
3051 3051 ire_t *prev_ire;
3052 3052 ire_t *save_ire;
3053 3053 ipaddr_t src, dst, gateway;
3054 3054 iulp_t ulp_info = { 0 };
3055 3055 int error;
3056 3056 ip_stack_t *ipst;
3057 3057
3058 3058 ASSERT(ill != NULL);
3059 3059 ipst = ill->ill_ipst;
3060 3060
3061 3061 ipha = (ipha_t *)mp->b_rptr;
3062 3062 iph_hdr_length = IPH_HDR_LENGTH(ipha);
3063 3063 if (((mp->b_wptr - mp->b_rptr) - iph_hdr_length) <
3064 3064 sizeof (icmph_t) + IP_SIMPLE_HDR_LENGTH) {
3065 3065 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
3066 3066 freemsg(mp);
3067 3067 return;
3068 3068 }
3069 3069 icmph = (icmph_t *)&mp->b_rptr[iph_hdr_length];
3070 3070 ipha_err = (ipha_t *)&icmph[1];
3071 3071 src = ipha->ipha_src;
3072 3072 dst = ipha_err->ipha_dst;
3073 3073 gateway = icmph->icmph_rd_gateway;
3074 3074 /* Make sure the new gateway is reachable somehow. */
3075 3075 ire = ire_route_lookup(gateway, 0, 0, IRE_INTERFACE, NULL, NULL,
3076 3076 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
3077 3077 /*
3078 3078 * Make sure we had a route for the dest in question and that
3079 3079 * that route was pointing to the old gateway (the source of the
3080 3080 * redirect packet.)
3081 3081 */
3082 3082 prev_ire = ire_route_lookup(dst, 0, src, 0, NULL, NULL, ALL_ZONES,
3083 3083 NULL, MATCH_IRE_GW, ipst);
3084 3084 /*
3085 3085 * Check that
3086 3086 * the redirect was not from ourselves
3087 3087 * the new gateway and the old gateway are directly reachable
3088 3088 */
3089 3089 if (!prev_ire ||
3090 3090 !ire ||
3091 3091 ire->ire_type == IRE_LOCAL) {
3092 3092 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects);
3093 3093 freemsg(mp);
3094 3094 if (ire != NULL)
3095 3095 ire_refrele(ire);
3096 3096 if (prev_ire != NULL)
3097 3097 ire_refrele(prev_ire);
3098 3098 return;
3099 3099 }
3100 3100
3101 3101 /*
3102 3102 * Should we use the old ULP info to create the new gateway? From
3103 3103 * a user's perspective, we should inherit the info so that it
3104 3104 * is a "smooth" transition. If we do not do that, then new
3105 3105 * connections going thru the new gateway will have no route metrics,
3106 3106 * which is counter-intuitive to user. From a network point of
3107 3107 * view, this may or may not make sense even though the new gateway
3108 3108 * is still directly connected to us so the route metrics should not
3109 3109 * change much.
3110 3110 *
3111 3111 * But if the old ire_uinfo is not initialized, we do another
3112 3112 * recursive lookup on the dest using the new gateway. There may
3113 3113 * be a route to that. If so, use it to initialize the redirect
3114 3114 * route.
3115 3115 */
3116 3116 if (prev_ire->ire_uinfo.iulp_set) {
3117 3117 bcopy(&prev_ire->ire_uinfo, &ulp_info, sizeof (iulp_t));
3118 3118 } else {
3119 3119 ire_t *tmp_ire;
3120 3120 ire_t *sire;
3121 3121
3122 3122 tmp_ire = ire_ftable_lookup(dst, 0, gateway, 0, NULL, &sire,
3123 3123 ALL_ZONES, 0, NULL,
3124 3124 (MATCH_IRE_RECURSIVE | MATCH_IRE_GW | MATCH_IRE_DEFAULT),
3125 3125 ipst);
3126 3126 if (sire != NULL) {
3127 3127 bcopy(&sire->ire_uinfo, &ulp_info, sizeof (iulp_t));
3128 3128 /*
3129 3129 * If sire != NULL, ire_ftable_lookup() should not
3130 3130 * return a NULL value.
3131 3131 */
3132 3132 ASSERT(tmp_ire != NULL);
3133 3133 ire_refrele(tmp_ire);
3134 3134 ire_refrele(sire);
3135 3135 } else if (tmp_ire != NULL) {
3136 3136 bcopy(&tmp_ire->ire_uinfo, &ulp_info,
3137 3137 sizeof (iulp_t));
3138 3138 ire_refrele(tmp_ire);
3139 3139 }
3140 3140 }
3141 3141 if (prev_ire->ire_type == IRE_CACHE)
3142 3142 ire_delete(prev_ire);
3143 3143 ire_refrele(prev_ire);
3144 3144 /*
3145 3145 * TODO: more precise handling for cases 0, 2, 3, the latter two
3146 3146 * require TOS routing
3147 3147 */
3148 3148 switch (icmph->icmph_code) {
3149 3149 case 0:
3150 3150 case 1:
3151 3151 /* TODO: TOS specificity for cases 2 and 3 */
3152 3152 case 2:
3153 3153 case 3:
3154 3154 break;
3155 3155 default:
3156 3156 freemsg(mp);
3157 3157 BUMP_MIB(&ipst->ips_icmp_mib, icmpInBadRedirects);
3158 3158 ire_refrele(ire);
3159 3159 return;
3160 3160 }
3161 3161 /*
3162 3162 * Create a Route Association. This will allow us to remember that
3163 3163 * someone we believe told us to use the particular gateway.
3164 3164 */
3165 3165 save_ire = ire;
3166 3166 ire = ire_create(
3167 3167 (uchar_t *)&dst, /* dest addr */
3168 3168 (uchar_t *)&ip_g_all_ones, /* mask */
3169 3169 (uchar_t *)&save_ire->ire_src_addr, /* source addr */
3170 3170 (uchar_t *)&gateway, /* gateway addr */
3171 3171 &save_ire->ire_max_frag, /* max frag */
3172 3172 NULL, /* no src nce */
3173 3173 NULL, /* no rfq */
3174 3174 NULL, /* no stq */
3175 3175 IRE_HOST,
3176 3176 NULL, /* ipif */
3177 3177 0, /* cmask */
3178 3178 0, /* phandle */
3179 3179 0, /* ihandle */
3180 3180 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST),
3181 3181 &ulp_info,
3182 3182 NULL, /* tsol_gc_t */
3183 3183 NULL, /* gcgrp */
3184 3184 ipst);
3185 3185
3186 3186 if (ire == NULL) {
3187 3187 freemsg(mp);
3188 3188 ire_refrele(save_ire);
3189 3189 return;
3190 3190 }
3191 3191 error = ire_add(&ire, NULL, NULL, NULL, B_FALSE);
3192 3192 ire_refrele(save_ire);
3193 3193 atomic_inc_32(&ipst->ips_ip_redirect_cnt);
3194 3194
3195 3195 if (error == 0) {
3196 3196 ire_refrele(ire); /* Held in ire_add_v4 */
3197 3197 /* tell routing sockets that we received a redirect */
3198 3198 ip_rts_change(RTM_REDIRECT, dst, gateway, IP_HOST_MASK, 0, src,
3199 3199 (RTF_DYNAMIC | RTF_GATEWAY | RTF_HOST), 0,
3200 3200 (RTA_DST | RTA_GATEWAY | RTA_NETMASK | RTA_AUTHOR), ipst);
3201 3201 }
3202 3202
3203 3203 /*
3204 3204 * Delete any existing IRE_HOST type redirect ires for this destination.
3205 3205 * This together with the added IRE has the effect of
3206 3206 * modifying an existing redirect.
3207 3207 */
3208 3208 prev_ire = ire_ftable_lookup(dst, 0, src, IRE_HOST, NULL, NULL,
3209 3209 ALL_ZONES, 0, NULL, (MATCH_IRE_GW | MATCH_IRE_TYPE), ipst);
3210 3210 if (prev_ire != NULL) {
3211 3211 if (prev_ire ->ire_flags & RTF_DYNAMIC)
3212 3212 ire_delete(prev_ire);
3213 3213 ire_refrele(prev_ire);
3214 3214 }
3215 3215
3216 3216 freemsg(mp);
3217 3217 }
3218 3218
3219 3219 /*
3220 3220 * Generate an ICMP parameter problem message.
3221 3221 */
3222 3222 static void
3223 3223 icmp_param_problem(queue_t *q, mblk_t *mp, uint8_t ptr, zoneid_t zoneid,
3224 3224 ip_stack_t *ipst)
3225 3225 {
3226 3226 icmph_t icmph;
3227 3227 boolean_t mctl_present;
3228 3228 mblk_t *first_mp;
3229 3229
3230 3230 EXTRACT_PKT_MP(mp, first_mp, mctl_present);
3231 3231
3232 3232 if (!(mp = icmp_pkt_err_ok(mp, ipst))) {
3233 3233 if (mctl_present)
3234 3234 freeb(first_mp);
3235 3235 return;
3236 3236 }
3237 3237
3238 3238 bzero(&icmph, sizeof (icmph_t));
3239 3239 icmph.icmph_type = ICMP_PARAM_PROBLEM;
3240 3240 icmph.icmph_pp_ptr = ptr;
3241 3241 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutParmProbs);
3242 3242 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid,
3243 3243 ipst);
3244 3244 }
3245 3245
3246 3246 /*
3247 3247 * Build and ship an IPv4 ICMP message using the packet data in mp, and
3248 3248 * the ICMP header pointed to by "stuff". (May be called as writer.)
3249 3249 * Note: assumes that icmp_pkt_err_ok has been called to verify that
3250 3250 * an icmp error packet can be sent.
3251 3251 * Assigns an appropriate source address to the packet. If ipha_dst is
3252 3252 * one of our addresses use it for source. Otherwise pick a source based
3253 3253 * on a route lookup back to ipha_src.
3254 3254 * Note that ipha_src must be set here since the
3255 3255 * packet is likely to arrive on an ill queue in ip_wput() which will
3256 3256 * not set a source address.
3257 3257 */
3258 3258 static void
3259 3259 icmp_pkt(queue_t *q, mblk_t *mp, void *stuff, size_t len,
3260 3260 boolean_t mctl_present, zoneid_t zoneid, ip_stack_t *ipst)
3261 3261 {
3262 3262 ipaddr_t dst;
3263 3263 icmph_t *icmph;
3264 3264 ipha_t *ipha;
3265 3265 uint_t len_needed;
3266 3266 size_t msg_len;
3267 3267 mblk_t *mp1;
3268 3268 ipaddr_t src;
3269 3269 ire_t *ire;
3270 3270 mblk_t *ipsec_mp;
3271 3271 ipsec_out_t *io = NULL;
3272 3272
3273 3273 if (mctl_present) {
3274 3274 /*
3275 3275 * If it is :
3276 3276 *
3277 3277 * 1) a IPSEC_OUT, then this is caused by outbound
3278 3278 * datagram originating on this host. IPsec processing
3279 3279 * may or may not have been done. Refer to comments above
3280 3280 * icmp_inbound_error_fanout for details.
3281 3281 *
3282 3282 * 2) a IPSEC_IN if we are generating a icmp_message
3283 3283 * for an incoming datagram destined for us i.e called
3284 3284 * from ip_fanout_send_icmp.
3285 3285 */
3286 3286 ipsec_info_t *in;
3287 3287 ipsec_mp = mp;
3288 3288 mp = ipsec_mp->b_cont;
3289 3289
3290 3290 in = (ipsec_info_t *)ipsec_mp->b_rptr;
3291 3291 ipha = (ipha_t *)mp->b_rptr;
3292 3292
3293 3293 ASSERT(in->ipsec_info_type == IPSEC_OUT ||
3294 3294 in->ipsec_info_type == IPSEC_IN);
3295 3295
3296 3296 if (in->ipsec_info_type == IPSEC_IN) {
3297 3297 /*
3298 3298 * Convert the IPSEC_IN to IPSEC_OUT.
3299 3299 */
3300 3300 if (!ipsec_in_to_out(ipsec_mp, ipha, NULL)) {
3301 3301 BUMP_MIB(&ipst->ips_ip_mib,
3302 3302 ipIfStatsOutDiscards);
3303 3303 return;
3304 3304 }
3305 3305 io = (ipsec_out_t *)ipsec_mp->b_rptr;
3306 3306 } else {
3307 3307 ASSERT(in->ipsec_info_type == IPSEC_OUT);
3308 3308 io = (ipsec_out_t *)in;
3309 3309 /*
3310 3310 * Clear out ipsec_out_proc_begin, so we do a fresh
3311 3311 * ire lookup.
3312 3312 */
3313 3313 io->ipsec_out_proc_begin = B_FALSE;
3314 3314 }
3315 3315 ASSERT(zoneid == io->ipsec_out_zoneid);
3316 3316 ASSERT(zoneid != ALL_ZONES);
3317 3317 } else {
3318 3318 /*
3319 3319 * This is in clear. The icmp message we are building
3320 3320 * here should go out in clear.
3321 3321 *
3322 3322 * Pardon the convolution of it all, but it's easier to
3323 3323 * allocate a "use cleartext" IPSEC_IN message and convert
3324 3324 * it than it is to allocate a new one.
3325 3325 */
3326 3326 ipsec_in_t *ii;
3327 3327 ASSERT(DB_TYPE(mp) == M_DATA);
3328 3328 ipsec_mp = ipsec_in_alloc(B_TRUE, ipst->ips_netstack);
3329 3329 if (ipsec_mp == NULL) {
3330 3330 freemsg(mp);
3331 3331 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
3332 3332 return;
3333 3333 }
3334 3334 ii = (ipsec_in_t *)ipsec_mp->b_rptr;
3335 3335
3336 3336 /* This is not a secure packet */
3337 3337 ii->ipsec_in_secure = B_FALSE;
3338 3338 /*
3339 3339 * For trusted extensions using a shared IP address we can
3340 3340 * send using any zoneid.
3341 3341 */
3342 3342 if (zoneid == ALL_ZONES)
3343 3343 ii->ipsec_in_zoneid = GLOBAL_ZONEID;
3344 3344 else
3345 3345 ii->ipsec_in_zoneid = zoneid;
3346 3346 ipsec_mp->b_cont = mp;
3347 3347 ipha = (ipha_t *)mp->b_rptr;
3348 3348 /*
3349 3349 * Convert the IPSEC_IN to IPSEC_OUT.
3350 3350 */
3351 3351 if (!ipsec_in_to_out(ipsec_mp, ipha, NULL)) {
3352 3352 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
3353 3353 return;
3354 3354 }
3355 3355 io = (ipsec_out_t *)ipsec_mp->b_rptr;
3356 3356 }
3357 3357
3358 3358 /* Remember our eventual destination */
3359 3359 dst = ipha->ipha_src;
3360 3360
3361 3361 ire = ire_route_lookup(ipha->ipha_dst, 0, 0, (IRE_LOCAL|IRE_LOOPBACK),
3362 3362 NULL, NULL, zoneid, NULL, MATCH_IRE_TYPE, ipst);
3363 3363 if (ire != NULL &&
3364 3364 (ire->ire_zoneid == zoneid || ire->ire_zoneid == ALL_ZONES)) {
3365 3365 src = ipha->ipha_dst;
3366 3366 } else {
3367 3367 if (ire != NULL)
3368 3368 ire_refrele(ire);
3369 3369 ire = ire_route_lookup(dst, 0, 0, 0, NULL, NULL, zoneid, NULL,
3370 3370 (MATCH_IRE_DEFAULT|MATCH_IRE_RECURSIVE|MATCH_IRE_ZONEONLY),
3371 3371 ipst);
3372 3372 if (ire == NULL) {
3373 3373 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes);
3374 3374 freemsg(ipsec_mp);
3375 3375 return;
3376 3376 }
3377 3377 src = ire->ire_src_addr;
3378 3378 }
3379 3379
3380 3380 if (ire != NULL)
3381 3381 ire_refrele(ire);
3382 3382
3383 3383 /*
3384 3384 * Check if we can send back more then 8 bytes in addition to
3385 3385 * the IP header. We try to send 64 bytes of data and the internal
3386 3386 * header in the special cases of ipv4 encapsulated ipv4 or ipv6.
3387 3387 */
3388 3388 len_needed = IPH_HDR_LENGTH(ipha);
3389 3389 if (ipha->ipha_protocol == IPPROTO_ENCAP ||
3390 3390 ipha->ipha_protocol == IPPROTO_IPV6) {
3391 3391
3392 3392 if (!pullupmsg(mp, -1)) {
3393 3393 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
3394 3394 freemsg(ipsec_mp);
3395 3395 return;
3396 3396 }
3397 3397 ipha = (ipha_t *)mp->b_rptr;
3398 3398
3399 3399 if (ipha->ipha_protocol == IPPROTO_ENCAP) {
3400 3400 len_needed += IPH_HDR_LENGTH(((uchar_t *)ipha +
3401 3401 len_needed));
3402 3402 } else {
3403 3403 ip6_t *ip6h = (ip6_t *)((uchar_t *)ipha + len_needed);
3404 3404
3405 3405 ASSERT(ipha->ipha_protocol == IPPROTO_IPV6);
3406 3406 len_needed += ip_hdr_length_v6(mp, ip6h);
3407 3407 }
3408 3408 }
3409 3409 len_needed += ipst->ips_ip_icmp_return;
3410 3410 msg_len = msgdsize(mp);
3411 3411 if (msg_len > len_needed) {
3412 3412 (void) adjmsg(mp, len_needed - msg_len);
3413 3413 msg_len = len_needed;
3414 3414 }
3415 3415 mp1 = allocb_tmpl(sizeof (icmp_ipha) + len, mp);
3416 3416 if (mp1 == NULL) {
3417 3417 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutErrors);
3418 3418 freemsg(ipsec_mp);
3419 3419 return;
3420 3420 }
3421 3421 mp1->b_cont = mp;
3422 3422 mp = mp1;
3423 3423 ASSERT(ipsec_mp->b_datap->db_type == M_CTL &&
3424 3424 ipsec_mp->b_rptr == (uint8_t *)io &&
3425 3425 io->ipsec_out_type == IPSEC_OUT);
3426 3426 ipsec_mp->b_cont = mp;
3427 3427
3428 3428 /*
3429 3429 * Set ipsec_out_icmp_loopback so we can let the ICMP messages this
3430 3430 * node generates be accepted in peace by all on-host destinations.
3431 3431 * If we do NOT assume that all on-host destinations trust
3432 3432 * self-generated ICMP messages, then rework here, ip6.c, and spd.c.
3433 3433 * (Look for ipsec_out_icmp_loopback).
3434 3434 */
3435 3435 io->ipsec_out_icmp_loopback = B_TRUE;
3436 3436
3437 3437 ipha = (ipha_t *)mp->b_rptr;
3438 3438 mp1->b_wptr = (uchar_t *)ipha + (sizeof (icmp_ipha) + len);
3439 3439 *ipha = icmp_ipha;
3440 3440 ipha->ipha_src = src;
3441 3441 ipha->ipha_dst = dst;
3442 3442 ipha->ipha_ttl = ipst->ips_ip_def_ttl;
3443 3443 msg_len += sizeof (icmp_ipha) + len;
3444 3444 if (msg_len > IP_MAXPACKET) {
3445 3445 (void) adjmsg(mp, IP_MAXPACKET - msg_len);
3446 3446 msg_len = IP_MAXPACKET;
3447 3447 }
3448 3448 ipha->ipha_length = htons((uint16_t)msg_len);
3449 3449 icmph = (icmph_t *)&ipha[1];
3450 3450 bcopy(stuff, icmph, len);
3451 3451 icmph->icmph_checksum = 0;
3452 3452 icmph->icmph_checksum = IP_CSUM(mp, (int32_t)sizeof (ipha_t), 0);
3453 3453 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutMsgs);
3454 3454 put(q, ipsec_mp);
3455 3455 }
3456 3456
3457 3457 /*
3458 3458 * Determine if an ICMP error packet can be sent given the rate limit.
3459 3459 * The limit consists of an average frequency (icmp_pkt_err_interval measured
3460 3460 * in milliseconds) and a burst size. Burst size number of packets can
3461 3461 * be sent arbitrarely closely spaced.
3462 3462 * The state is tracked using two variables to implement an approximate
3463 3463 * token bucket filter:
3464 3464 * icmp_pkt_err_last - lbolt value when the last burst started
3465 3465 * icmp_pkt_err_sent - number of packets sent in current burst
3466 3466 */
3467 3467 boolean_t
3468 3468 icmp_err_rate_limit(ip_stack_t *ipst)
3469 3469 {
3470 3470 clock_t now = TICK_TO_MSEC(lbolt);
3471 3471 uint_t refilled; /* Number of packets refilled in tbf since last */
3472 3472 /* Guard against changes by loading into local variable */
3473 3473 uint_t err_interval = ipst->ips_ip_icmp_err_interval;
3474 3474
3475 3475 if (err_interval == 0)
3476 3476 return (B_FALSE);
3477 3477
3478 3478 if (ipst->ips_icmp_pkt_err_last > now) {
3479 3479 /* 100HZ lbolt in ms for 32bit arch wraps every 49.7 days */
3480 3480 ipst->ips_icmp_pkt_err_last = 0;
3481 3481 ipst->ips_icmp_pkt_err_sent = 0;
3482 3482 }
3483 3483 /*
3484 3484 * If we are in a burst update the token bucket filter.
3485 3485 * Update the "last" time to be close to "now" but make sure
3486 3486 * we don't loose precision.
3487 3487 */
3488 3488 if (ipst->ips_icmp_pkt_err_sent != 0) {
3489 3489 refilled = (now - ipst->ips_icmp_pkt_err_last)/err_interval;
3490 3490 if (refilled > ipst->ips_icmp_pkt_err_sent) {
3491 3491 ipst->ips_icmp_pkt_err_sent = 0;
3492 3492 } else {
3493 3493 ipst->ips_icmp_pkt_err_sent -= refilled;
3494 3494 ipst->ips_icmp_pkt_err_last += refilled * err_interval;
3495 3495 }
3496 3496 }
3497 3497 if (ipst->ips_icmp_pkt_err_sent == 0) {
3498 3498 /* Start of new burst */
3499 3499 ipst->ips_icmp_pkt_err_last = now;
3500 3500 }
3501 3501 if (ipst->ips_icmp_pkt_err_sent < ipst->ips_ip_icmp_err_burst) {
3502 3502 ipst->ips_icmp_pkt_err_sent++;
3503 3503 ip1dbg(("icmp_err_rate_limit: %d sent in burst\n",
3504 3504 ipst->ips_icmp_pkt_err_sent));
3505 3505 return (B_FALSE);
3506 3506 }
3507 3507 ip1dbg(("icmp_err_rate_limit: dropped\n"));
3508 3508 return (B_TRUE);
3509 3509 }
3510 3510
3511 3511 /*
3512 3512 * Check if it is ok to send an IPv4 ICMP error packet in
3513 3513 * response to the IPv4 packet in mp.
3514 3514 * Free the message and return null if no
3515 3515 * ICMP error packet should be sent.
3516 3516 */
3517 3517 static mblk_t *
3518 3518 icmp_pkt_err_ok(mblk_t *mp, ip_stack_t *ipst)
3519 3519 {
3520 3520 icmph_t *icmph;
3521 3521 ipha_t *ipha;
3522 3522 uint_t len_needed;
3523 3523 ire_t *src_ire;
3524 3524 ire_t *dst_ire;
3525 3525
3526 3526 if (!mp)
3527 3527 return (NULL);
3528 3528 ipha = (ipha_t *)mp->b_rptr;
3529 3529 if (ip_csum_hdr(ipha)) {
3530 3530 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInCksumErrs);
3531 3531 freemsg(mp);
3532 3532 return (NULL);
3533 3533 }
3534 3534 src_ire = ire_ctable_lookup(ipha->ipha_dst, 0, IRE_BROADCAST,
3535 3535 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
3536 3536 dst_ire = ire_ctable_lookup(ipha->ipha_src, 0, IRE_BROADCAST,
3537 3537 NULL, ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
3538 3538 if (src_ire != NULL || dst_ire != NULL ||
3539 3539 CLASSD(ipha->ipha_dst) ||
3540 3540 CLASSD(ipha->ipha_src) ||
3541 3541 (ntohs(ipha->ipha_fragment_offset_and_flags) & IPH_OFFSET)) {
3542 3542 /* Note: only errors to the fragment with offset 0 */
3543 3543 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
3544 3544 freemsg(mp);
3545 3545 if (src_ire != NULL)
3546 3546 ire_refrele(src_ire);
3547 3547 if (dst_ire != NULL)
3548 3548 ire_refrele(dst_ire);
3549 3549 return (NULL);
3550 3550 }
3551 3551 if (ipha->ipha_protocol == IPPROTO_ICMP) {
3552 3552 /*
3553 3553 * Check the ICMP type. RFC 1122 sez: don't send ICMP
3554 3554 * errors in response to any ICMP errors.
3555 3555 */
3556 3556 len_needed = IPH_HDR_LENGTH(ipha) + ICMPH_SIZE;
3557 3557 if (mp->b_wptr - mp->b_rptr < len_needed) {
3558 3558 if (!pullupmsg(mp, len_needed)) {
3559 3559 BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
3560 3560 freemsg(mp);
3561 3561 return (NULL);
3562 3562 }
3563 3563 ipha = (ipha_t *)mp->b_rptr;
3564 3564 }
3565 3565 icmph = (icmph_t *)
3566 3566 (&((char *)ipha)[IPH_HDR_LENGTH(ipha)]);
3567 3567 switch (icmph->icmph_type) {
3568 3568 case ICMP_DEST_UNREACHABLE:
3569 3569 case ICMP_SOURCE_QUENCH:
3570 3570 case ICMP_TIME_EXCEEDED:
3571 3571 case ICMP_PARAM_PROBLEM:
3572 3572 case ICMP_REDIRECT:
3573 3573 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
3574 3574 freemsg(mp);
3575 3575 return (NULL);
3576 3576 default:
3577 3577 break;
3578 3578 }
3579 3579 }
3580 3580 /*
3581 3581 * If this is a labeled system, then check to see if we're allowed to
3582 3582 * send a response to this particular sender. If not, then just drop.
3583 3583 */
3584 3584 if (is_system_labeled() && !tsol_can_reply_error(mp)) {
3585 3585 ip2dbg(("icmp_pkt_err_ok: can't respond to packet\n"));
3586 3586 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDrops);
3587 3587 freemsg(mp);
3588 3588 return (NULL);
3589 3589 }
3590 3590 if (icmp_err_rate_limit(ipst)) {
3591 3591 /*
3592 3592 * Only send ICMP error packets every so often.
3593 3593 * This should be done on a per port/source basis,
3594 3594 * but for now this will suffice.
3595 3595 */
3596 3596 freemsg(mp);
3597 3597 return (NULL);
3598 3598 }
3599 3599 return (mp);
3600 3600 }
3601 3601
3602 3602 /*
3603 3603 * Generate an ICMP redirect message.
3604 3604 */
3605 3605 static void
3606 3606 icmp_send_redirect(queue_t *q, mblk_t *mp, ipaddr_t gateway, ip_stack_t *ipst)
3607 3607 {
3608 3608 icmph_t icmph;
3609 3609
3610 3610 /*
3611 3611 * We are called from ip_rput where we could
3612 3612 * not have attached an IPSEC_IN.
3613 3613 */
3614 3614 ASSERT(mp->b_datap->db_type == M_DATA);
3615 3615
3616 3616 if (!(mp = icmp_pkt_err_ok(mp, ipst))) {
3617 3617 return;
3618 3618 }
3619 3619
3620 3620 bzero(&icmph, sizeof (icmph_t));
3621 3621 icmph.icmph_type = ICMP_REDIRECT;
3622 3622 icmph.icmph_code = 1;
3623 3623 icmph.icmph_rd_gateway = gateway;
3624 3624 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutRedirects);
3625 3625 /* Redirects sent by router, and router is global zone */
3626 3626 icmp_pkt(q, mp, &icmph, sizeof (icmph_t), B_FALSE, GLOBAL_ZONEID, ipst);
3627 3627 }
3628 3628
3629 3629 /*
3630 3630 * Generate an ICMP time exceeded message.
3631 3631 */
3632 3632 void
3633 3633 icmp_time_exceeded(queue_t *q, mblk_t *mp, uint8_t code, zoneid_t zoneid,
3634 3634 ip_stack_t *ipst)
3635 3635 {
3636 3636 icmph_t icmph;
3637 3637 boolean_t mctl_present;
3638 3638 mblk_t *first_mp;
3639 3639
3640 3640 EXTRACT_PKT_MP(mp, first_mp, mctl_present);
3641 3641
3642 3642 if (!(mp = icmp_pkt_err_ok(mp, ipst))) {
3643 3643 if (mctl_present)
3644 3644 freeb(first_mp);
3645 3645 return;
3646 3646 }
3647 3647
3648 3648 bzero(&icmph, sizeof (icmph_t));
3649 3649 icmph.icmph_type = ICMP_TIME_EXCEEDED;
3650 3650 icmph.icmph_code = code;
3651 3651 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutTimeExcds);
3652 3652 icmp_pkt(q, first_mp, &icmph, sizeof (icmph_t), mctl_present, zoneid,
3653 3653 ipst);
3654 3654 }
3655 3655
3656 3656 /*
3657 3657 * Generate an ICMP unreachable message.
3658 3658 */
3659 3659 void
3660 3660 icmp_unreachable(queue_t *q, mblk_t *mp, uint8_t code, zoneid_t zoneid,
3661 3661 ip_stack_t *ipst)
3662 3662 {
3663 3663 icmph_t icmph;
3664 3664 mblk_t *first_mp;
3665 3665 boolean_t mctl_present;
3666 3666
3667 3667 EXTRACT_PKT_MP(mp, first_mp, mctl_present);
3668 3668
3669 3669 if (!(mp = icmp_pkt_err_ok(mp, ipst))) {
3670 3670 if (mctl_present)
3671 3671 freeb(first_mp);
3672 3672 return;
3673 3673 }
3674 3674
3675 3675 bzero(&icmph, sizeof (icmph_t));
3676 3676 icmph.icmph_type = ICMP_DEST_UNREACHABLE;
3677 3677 icmph.icmph_code = code;
3678 3678 BUMP_MIB(&ipst->ips_icmp_mib, icmpOutDestUnreachs);
3679 3679 ip2dbg(("send icmp destination unreachable code %d\n", code));
3680 3680 icmp_pkt(q, first_mp, (char *)&icmph, sizeof (icmph_t), mctl_present,
3681 3681 zoneid, ipst);
3682 3682 }
3683 3683
3684 3684 /*
3685 3685 * Attempt to start recovery of an IPv4 interface that's been shut down as a
3686 3686 * duplicate. As long as someone else holds the address, the interface will
3687 3687 * stay down. When that conflict goes away, the interface is brought back up.
3688 3688 * This is done so that accidental shutdowns of addresses aren't made
3689 3689 * permanent. Your server will recover from a failure.
3690 3690 *
3691 3691 * For DHCP, recovery is not done in the kernel. Instead, it's handled by a
3692 3692 * user space process (dhcpagent).
3693 3693 *
3694 3694 * Recovery completes if ARP reports that the address is now ours (via
3695 3695 * AR_CN_READY). In that case, we go to ip_arp_excl to finish the operation.
3696 3696 *
3697 3697 * This function is entered on a timer expiry; the ID is in ipif_recovery_id.
3698 3698 */
3699 3699 static void
3700 3700 ipif_dup_recovery(void *arg)
3701 3701 {
3702 3702 ipif_t *ipif = arg;
3703 3703 ill_t *ill = ipif->ipif_ill;
3704 3704 mblk_t *arp_add_mp;
3705 3705 mblk_t *arp_del_mp;
3706 3706 area_t *area;
3707 3707 ip_stack_t *ipst = ill->ill_ipst;
3708 3708
3709 3709 ipif->ipif_recovery_id = 0;
3710 3710
3711 3711 /*
3712 3712 * No lock needed for moving or condemned check, as this is just an
3713 3713 * optimization.
3714 3714 */
3715 3715 if (ill->ill_arp_closing || !(ipif->ipif_flags & IPIF_DUPLICATE) ||
3716 3716 (ipif->ipif_flags & IPIF_POINTOPOINT) ||
3717 3717 (ipif->ipif_state_flags & (IPIF_MOVING | IPIF_CONDEMNED))) {
3718 3718 /* No reason to try to bring this address back. */
3719 3719 return;
3720 3720 }
3721 3721
3722 3722 if ((arp_add_mp = ipif_area_alloc(ipif)) == NULL)
3723 3723 goto alloc_fail;
3724 3724
3725 3725 if (ipif->ipif_arp_del_mp == NULL) {
3726 3726 if ((arp_del_mp = ipif_ared_alloc(ipif)) == NULL)
3727 3727 goto alloc_fail;
3728 3728 ipif->ipif_arp_del_mp = arp_del_mp;
3729 3729 }
3730 3730
3731 3731 /* Setting the 'unverified' flag restarts DAD */
3732 3732 area = (area_t *)arp_add_mp->b_rptr;
3733 3733 area->area_flags = ACE_F_PERMANENT | ACE_F_PUBLISH | ACE_F_MYADDR |
3734 3734 ACE_F_UNVERIFIED;
3735 3735 putnext(ill->ill_rq, arp_add_mp);
3736 3736 return;
3737 3737
3738 3738 alloc_fail:
3739 3739 /*
3740 3740 * On allocation failure, just restart the timer. Note that the ipif
3741 3741 * is down here, so no other thread could be trying to start a recovery
3742 3742 * timer. The ill_lock protects the condemned flag and the recovery
3743 3743 * timer ID.
3744 3744 */
3745 3745 freemsg(arp_add_mp);
3746 3746 mutex_enter(&ill->ill_lock);
3747 3747 if (ipst->ips_ip_dup_recovery > 0 && ipif->ipif_recovery_id == 0 &&
3748 3748 !(ipif->ipif_state_flags & IPIF_CONDEMNED)) {
3749 3749 ipif->ipif_recovery_id = timeout(ipif_dup_recovery, ipif,
3750 3750 MSEC_TO_TICK(ipst->ips_ip_dup_recovery));
3751 3751 }
3752 3752 mutex_exit(&ill->ill_lock);
3753 3753 }
3754 3754
3755 3755 /*
3756 3756 * This is for exclusive changes due to ARP. Either tear down an interface due
3757 3757 * to AR_CN_FAILED and AR_CN_BOGON, or bring one up for successful recovery.
3758 3758 */
3759 3759 /* ARGSUSED */
3760 3760 static void
3761 3761 ip_arp_excl(ipsq_t *ipsq, queue_t *rq, mblk_t *mp, void *dummy_arg)
3762 3762 {
3763 3763 ill_t *ill = rq->q_ptr;
3764 3764 arh_t *arh;
3765 3765 ipaddr_t src;
3766 3766 ipif_t *ipif;
3767 3767 char ibuf[LIFNAMSIZ + 10]; /* 10 digits for logical i/f number */
3768 3768 char hbuf[MAC_STR_LEN];
3769 3769 char sbuf[INET_ADDRSTRLEN];
3770 3770 const char *failtype;
3771 3771 boolean_t bring_up;
3772 3772 ip_stack_t *ipst = ill->ill_ipst;
3773 3773
3774 3774 switch (((arcn_t *)mp->b_rptr)->arcn_code) {
3775 3775 case AR_CN_READY:
3776 3776 failtype = NULL;
3777 3777 bring_up = B_TRUE;
3778 3778 break;
3779 3779 case AR_CN_FAILED:
3780 3780 failtype = "in use";
3781 3781 bring_up = B_FALSE;
3782 3782 break;
3783 3783 default:
3784 3784 failtype = "claimed";
3785 3785 bring_up = B_FALSE;
3786 3786 break;
3787 3787 }
3788 3788
3789 3789 arh = (arh_t *)mp->b_cont->b_rptr;
3790 3790 bcopy((char *)&arh[1] + arh->arh_hlen, &src, IP_ADDR_LEN);
3791 3791
3792 3792 (void) mac_colon_addr((uint8_t *)(arh + 1), arh->arh_hlen, hbuf,
3793 3793 sizeof (hbuf));
3794 3794 (void) ip_dot_addr(src, sbuf);
3795 3795 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3796 3796
3797 3797 if ((ipif->ipif_flags & IPIF_POINTOPOINT) ||
3798 3798 ipif->ipif_lcl_addr != src) {
3799 3799 continue;
3800 3800 }
3801 3801
3802 3802 /*
3803 3803 * If we failed on a recovery probe, then restart the timer to
3804 3804 * try again later.
3805 3805 */
3806 3806 if (!bring_up && (ipif->ipif_flags & IPIF_DUPLICATE) &&
3807 3807 !(ipif->ipif_flags & (IPIF_DHCPRUNNING|IPIF_TEMPORARY)) &&
3808 3808 ill->ill_net_type == IRE_IF_RESOLVER &&
3809 3809 !(ipif->ipif_state_flags & IPIF_CONDEMNED) &&
3810 3810 ipst->ips_ip_dup_recovery > 0 &&
3811 3811 ipif->ipif_recovery_id == 0) {
3812 3812 ipif->ipif_recovery_id = timeout(ipif_dup_recovery,
3813 3813 ipif, MSEC_TO_TICK(ipst->ips_ip_dup_recovery));
3814 3814 continue;
3815 3815 }
3816 3816
3817 3817 /*
3818 3818 * If what we're trying to do has already been done, then do
3819 3819 * nothing.
3820 3820 */
3821 3821 if (bring_up == ((ipif->ipif_flags & IPIF_UP) != 0))
3822 3822 continue;
3823 3823
3824 3824 ipif_get_name(ipif, ibuf, sizeof (ibuf));
3825 3825
3826 3826 if (failtype == NULL) {
3827 3827 cmn_err(CE_NOTE, "recovered address %s on %s", sbuf,
3828 3828 ibuf);
3829 3829 } else {
3830 3830 cmn_err(CE_WARN, "%s has duplicate address %s (%s "
3831 3831 "by %s); disabled", ibuf, sbuf, failtype, hbuf);
3832 3832 }
3833 3833
3834 3834 if (bring_up) {
3835 3835 ASSERT(ill->ill_dl_up);
3836 3836 /*
3837 3837 * Free up the ARP delete message so we can allocate
3838 3838 * a fresh one through the normal path.
3839 3839 */
3840 3840 freemsg(ipif->ipif_arp_del_mp);
3841 3841 ipif->ipif_arp_del_mp = NULL;
3842 3842 if (ipif_resolver_up(ipif, Res_act_initial) !=
3843 3843 EINPROGRESS) {
3844 3844 ipif->ipif_addr_ready = 1;
3845 3845 (void) ipif_up_done(ipif);
3846 3846 }
3847 3847 continue;
3848 3848 }
3849 3849
3850 3850 mutex_enter(&ill->ill_lock);
3851 3851 ASSERT(!(ipif->ipif_flags & IPIF_DUPLICATE));
3852 3852 ipif->ipif_flags |= IPIF_DUPLICATE;
3853 3853 ill->ill_ipif_dup_count++;
3854 3854 mutex_exit(&ill->ill_lock);
3855 3855 /*
3856 3856 * Already exclusive on the ill; no need to handle deferred
3857 3857 * processing here.
3858 3858 */
3859 3859 (void) ipif_down(ipif, NULL, NULL);
3860 3860 ipif_down_tail(ipif);
3861 3861 mutex_enter(&ill->ill_lock);
3862 3862 if (!(ipif->ipif_flags & (IPIF_DHCPRUNNING|IPIF_TEMPORARY)) &&
3863 3863 ill->ill_net_type == IRE_IF_RESOLVER &&
3864 3864 !(ipif->ipif_state_flags & IPIF_CONDEMNED) &&
3865 3865 ipst->ips_ip_dup_recovery > 0) {
3866 3866 ipif->ipif_recovery_id = timeout(ipif_dup_recovery,
3867 3867 ipif, MSEC_TO_TICK(ipst->ips_ip_dup_recovery));
3868 3868 }
3869 3869 mutex_exit(&ill->ill_lock);
3870 3870 }
3871 3871 freemsg(mp);
3872 3872 }
3873 3873
3874 3874 /* ARGSUSED */
3875 3875 static void
3876 3876 ip_arp_defend(ipsq_t *ipsq, queue_t *rq, mblk_t *mp, void *dummy_arg)
3877 3877 {
3878 3878 ill_t *ill = rq->q_ptr;
3879 3879 arh_t *arh;
3880 3880 ipaddr_t src;
3881 3881 ipif_t *ipif;
3882 3882
3883 3883 arh = (arh_t *)mp->b_cont->b_rptr;
3884 3884 bcopy((char *)&arh[1] + arh->arh_hlen, &src, IP_ADDR_LEN);
3885 3885 for (ipif = ill->ill_ipif; ipif != NULL; ipif = ipif->ipif_next) {
3886 3886 if ((ipif->ipif_flags & IPIF_UP) && ipif->ipif_lcl_addr == src)
3887 3887 (void) ipif_resolver_up(ipif, Res_act_defend);
3888 3888 }
3889 3889 freemsg(mp);
3890 3890 }
3891 3891
3892 3892 /*
3893 3893 * News from ARP. ARP sends notification of interesting events down
3894 3894 * to its clients using M_CTL messages with the interesting ARP packet
3895 3895 * attached via b_cont.
3896 3896 * The interesting event from a device comes up the corresponding ARP-IP-DEV
3897 3897 * queue as opposed to ARP sending the message to all the clients, i.e. all
3898 3898 * its ARP-IP-DEV instances. Thus, for AR_CN_ANNOUNCE, we must walk the cache
3899 3899 * table if a cache IRE is found to delete all the entries for the address in
3900 3900 * the packet.
3901 3901 */
3902 3902 static void
3903 3903 ip_arp_news(queue_t *q, mblk_t *mp)
3904 3904 {
3905 3905 arcn_t *arcn;
3906 3906 arh_t *arh;
3907 3907 ire_t *ire = NULL;
3908 3908 char hbuf[MAC_STR_LEN];
3909 3909 char sbuf[INET_ADDRSTRLEN];
3910 3910 ipaddr_t src;
3911 3911 in6_addr_t v6src;
3912 3912 boolean_t isv6 = B_FALSE;
3913 3913 ipif_t *ipif;
3914 3914 ill_t *ill;
3915 3915 ip_stack_t *ipst;
3916 3916
3917 3917 if (CONN_Q(q)) {
3918 3918 conn_t *connp = Q_TO_CONN(q);
3919 3919
3920 3920 ipst = connp->conn_netstack->netstack_ip;
3921 3921 } else {
3922 3922 ill_t *ill = (ill_t *)q->q_ptr;
3923 3923
3924 3924 ipst = ill->ill_ipst;
3925 3925 }
3926 3926
3927 3927 if ((mp->b_wptr - mp->b_rptr) < sizeof (arcn_t) || !mp->b_cont) {
3928 3928 if (q->q_next) {
3929 3929 putnext(q, mp);
3930 3930 } else
3931 3931 freemsg(mp);
3932 3932 return;
3933 3933 }
3934 3934 arh = (arh_t *)mp->b_cont->b_rptr;
3935 3935 /* Is it one we are interested in? */
3936 3936 if (BE16_TO_U16(arh->arh_proto) == IP6_DL_SAP) {
3937 3937 isv6 = B_TRUE;
3938 3938 bcopy((char *)&arh[1] + (arh->arh_hlen & 0xFF), &v6src,
3939 3939 IPV6_ADDR_LEN);
3940 3940 } else if (BE16_TO_U16(arh->arh_proto) == IP_ARP_PROTO_TYPE) {
3941 3941 bcopy((char *)&arh[1] + (arh->arh_hlen & 0xFF), &src,
3942 3942 IP_ADDR_LEN);
3943 3943 } else {
3944 3944 freemsg(mp);
3945 3945 return;
3946 3946 }
3947 3947
3948 3948 ill = q->q_ptr;
3949 3949
3950 3950 arcn = (arcn_t *)mp->b_rptr;
3951 3951 switch (arcn->arcn_code) {
3952 3952 case AR_CN_BOGON:
3953 3953 /*
3954 3954 * Someone is sending ARP packets with a source protocol
3955 3955 * address that we have published and for which we believe our
3956 3956 * entry is authoritative and (when ill_arp_extend is set)
3957 3957 * verified to be unique on the network.
3958 3958 *
3959 3959 * The ARP module internally handles the cases where the sender
3960 3960 * is just probing (for DAD) and where the hardware address of
3961 3961 * a non-authoritative entry has changed. Thus, these are the
3962 3962 * real conflicts, and we have to do resolution.
3963 3963 *
3964 3964 * We back away quickly from the address if it's from DHCP or
3965 3965 * otherwise temporary and hasn't been used recently (or at
3966 3966 * all). We'd like to include "deprecated" addresses here as
3967 3967 * well (as there's no real reason to defend something we're
3968 3968 * discarding), but IPMP "reuses" this flag to mean something
3969 3969 * other than the standard meaning.
3970 3970 *
3971 3971 * If the ARP module above is not extended (meaning that it
3972 3972 * doesn't know how to defend the address), then we just log
3973 3973 * the problem as we always did and continue on. It's not
3974 3974 * right, but there's little else we can do, and those old ATM
3975 3975 * users are going away anyway.
3976 3976 */
3977 3977 (void) mac_colon_addr((uint8_t *)(arh + 1), arh->arh_hlen,
3978 3978 hbuf, sizeof (hbuf));
3979 3979 (void) ip_dot_addr(src, sbuf);
3980 3980 if (isv6) {
3981 3981 ire = ire_cache_lookup_v6(&v6src, ALL_ZONES, NULL,
3982 3982 ipst);
3983 3983 } else {
3984 3984 ire = ire_cache_lookup(src, ALL_ZONES, NULL, ipst);
3985 3985 }
3986 3986 if (ire != NULL && IRE_IS_LOCAL(ire)) {
3987 3987 uint32_t now;
3988 3988 uint32_t maxage;
3989 3989 clock_t lused;
3990 3990 uint_t maxdefense;
3991 3991 uint_t defs;
3992 3992
3993 3993 /*
3994 3994 * First, figure out if this address hasn't been used
3995 3995 * in a while. If it hasn't, then it's a better
3996 3996 * candidate for abandoning.
3997 3997 */
3998 3998 ipif = ire->ire_ipif;
3999 3999 ASSERT(ipif != NULL);
4000 4000 now = gethrestime_sec();
4001 4001 maxage = now - ire->ire_create_time;
4002 4002 if (maxage > ipst->ips_ip_max_temp_idle)
4003 4003 maxage = ipst->ips_ip_max_temp_idle;
4004 4004 lused = drv_hztousec(ddi_get_lbolt() -
4005 4005 ire->ire_last_used_time) / MICROSEC + 1;
4006 4006 if (lused >= maxage && (ipif->ipif_flags &
4007 4007 (IPIF_DHCPRUNNING | IPIF_TEMPORARY)))
4008 4008 maxdefense = ipst->ips_ip_max_temp_defend;
4009 4009 else
4010 4010 maxdefense = ipst->ips_ip_max_defend;
4011 4011
4012 4012 /*
4013 4013 * Now figure out how many times we've defended
4014 4014 * ourselves. Ignore defenses that happened long in
4015 4015 * the past.
4016 4016 */
4017 4017 mutex_enter(&ire->ire_lock);
4018 4018 if ((defs = ire->ire_defense_count) > 0 &&
4019 4019 now - ire->ire_defense_time >
4020 4020 ipst->ips_ip_defend_interval) {
4021 4021 ire->ire_defense_count = defs = 0;
4022 4022 }
4023 4023 ire->ire_defense_count++;
4024 4024 ire->ire_defense_time = now;
4025 4025 mutex_exit(&ire->ire_lock);
4026 4026 ill_refhold(ill);
4027 4027 ire_refrele(ire);
4028 4028
4029 4029 /*
4030 4030 * If we've defended ourselves too many times already,
4031 4031 * then give up and tear down the interface(s) using
4032 4032 * this address. Otherwise, defend by sending out a
4033 4033 * gratuitous ARP.
4034 4034 */
4035 4035 if (defs >= maxdefense && ill->ill_arp_extend) {
4036 4036 qwriter_ip(ill, q, mp, ip_arp_excl, NEW_OP,
4037 4037 B_FALSE);
4038 4038 } else {
4039 4039 cmn_err(CE_WARN,
4040 4040 "node %s is using our IP address %s on %s",
4041 4041 hbuf, sbuf, ill->ill_name);
4042 4042 /*
4043 4043 * If this is an old (ATM) ARP module, then
4044 4044 * don't try to defend the address. Remain
4045 4045 * compatible with the old behavior. Defend
4046 4046 * only with new ARP.
4047 4047 */
4048 4048 if (ill->ill_arp_extend) {
4049 4049 qwriter_ip(ill, q, mp, ip_arp_defend,
4050 4050 NEW_OP, B_FALSE);
4051 4051 } else {
4052 4052 ill_refrele(ill);
4053 4053 }
4054 4054 }
4055 4055 return;
4056 4056 }
4057 4057 cmn_err(CE_WARN,
4058 4058 "proxy ARP problem? Node '%s' is using %s on %s",
4059 4059 hbuf, sbuf, ill->ill_name);
4060 4060 if (ire != NULL)
4061 4061 ire_refrele(ire);
4062 4062 break;
4063 4063 case AR_CN_ANNOUNCE:
4064 4064 if (isv6) {
4065 4065 /*
4066 4066 * For XRESOLV interfaces.
4067 4067 * Delete the IRE cache entry and NCE for this
4068 4068 * v6 address
4069 4069 */
4070 4070 ip_ire_clookup_and_delete_v6(&v6src, ipst);
4071 4071 /*
4072 4072 * If v6src is a non-zero, it's a router address
4073 4073 * as below. Do the same sort of thing to clean
4074 4074 * out off-net IRE_CACHE entries that go through
4075 4075 * the router.
4076 4076 */
4077 4077 if (!IN6_IS_ADDR_UNSPECIFIED(&v6src)) {
4078 4078 ire_walk_v6(ire_delete_cache_gw_v6,
4079 4079 (char *)&v6src, ALL_ZONES, ipst);
4080 4080 }
4081 4081 } else {
4082 4082 nce_hw_map_t hwm;
4083 4083
4084 4084 /*
4085 4085 * ARP gives us a copy of any packet where it thinks
4086 4086 * the address has changed, so that we can update our
4087 4087 * caches. We're responsible for caching known answers
4088 4088 * in the current design. We check whether the
4089 4089 * hardware address really has changed in all of our
4090 4090 * entries that have cached this mapping, and if so, we
4091 4091 * blow them away. This way we will immediately pick
4092 4092 * up the rare case of a host changing hardware
4093 4093 * address.
4094 4094 */
4095 4095 if (src == 0)
4096 4096 break;
4097 4097 hwm.hwm_addr = src;
4098 4098 hwm.hwm_hwlen = arh->arh_hlen;
4099 4099 hwm.hwm_hwaddr = (uchar_t *)(arh + 1);
4100 4100 NDP_HW_CHANGE_INCR(ipst->ips_ndp4);
4101 4101 ndp_walk_common(ipst->ips_ndp4, NULL,
4102 4102 (pfi_t)nce_delete_hw_changed, &hwm, ALL_ZONES);
4103 4103 NDP_HW_CHANGE_DECR(ipst->ips_ndp4);
4104 4104 }
4105 4105 break;
4106 4106 case AR_CN_READY:
4107 4107 /* No external v6 resolver has a contract to use this */
4108 4108 if (isv6)
4109 4109 break;
4110 4110 /* If the link is down, we'll retry this later */
4111 4111 if (!(ill->ill_phyint->phyint_flags & PHYI_RUNNING))
4112 4112 break;
4113 4113 ipif = ipif_lookup_addr(src, ill, ALL_ZONES, NULL, NULL,
4114 4114 NULL, NULL, ipst);
4115 4115 if (ipif != NULL) {
4116 4116 /*
4117 4117 * If this is a duplicate recovery, then we now need to
4118 4118 * go exclusive to bring this thing back up.
4119 4119 */
4120 4120 if ((ipif->ipif_flags & (IPIF_UP|IPIF_DUPLICATE)) ==
4121 4121 IPIF_DUPLICATE) {
4122 4122 ipif_refrele(ipif);
4123 4123 ill_refhold(ill);
4124 4124 qwriter_ip(ill, q, mp, ip_arp_excl, NEW_OP,
4125 4125 B_FALSE);
4126 4126 return;
4127 4127 }
4128 4128 /*
4129 4129 * If this is the first notice that this address is
4130 4130 * ready, then let the user know now.
4131 4131 */
4132 4132 if ((ipif->ipif_flags & IPIF_UP) &&
4133 4133 !ipif->ipif_addr_ready) {
4134 4134 ipif_mask_reply(ipif);
4135 4135 ip_rts_ifmsg(ipif);
4136 4136 ip_rts_newaddrmsg(RTM_ADD, 0, ipif);
4137 4137 sctp_update_ipif(ipif, SCTP_IPIF_UP);
4138 4138 }
4139 4139 ipif->ipif_addr_ready = 1;
4140 4140 ipif_refrele(ipif);
4141 4141 }
4142 4142 ire = ire_cache_lookup(src, ALL_ZONES, MBLK_GETLABEL(mp), ipst);
4143 4143 if (ire != NULL) {
4144 4144 ire->ire_defense_count = 0;
4145 4145 ire_refrele(ire);
4146 4146 }
4147 4147 break;
4148 4148 case AR_CN_FAILED:
4149 4149 /* No external v6 resolver has a contract to use this */
4150 4150 if (isv6)
4151 4151 break;
4152 4152 ill_refhold(ill);
4153 4153 qwriter_ip(ill, q, mp, ip_arp_excl, NEW_OP, B_FALSE);
4154 4154 return;
4155 4155 }
4156 4156 freemsg(mp);
4157 4157 }
4158 4158
4159 4159 /*
4160 4160 * Create a mblk suitable for carrying the interface index and/or source link
4161 4161 * address. This mblk is tagged as an M_CTL and is sent to ULP. This is used
4162 4162 * when the IP_RECVIF and/or IP_RECVSLLA socket option is set by the user
4163 4163 * application.
4164 4164 */
4165 4165 mblk_t *
4166 4166 ip_add_info(mblk_t *data_mp, ill_t *ill, uint_t flags, zoneid_t zoneid,
4167 4167 ip_stack_t *ipst)
4168 4168 {
4169 4169 mblk_t *mp;
4170 4170 ip_pktinfo_t *pinfo;
4171 4171 ipha_t *ipha;
4172 4172 struct ether_header *pether;
4173 4173
4174 4174 mp = allocb(sizeof (ip_pktinfo_t), BPRI_MED);
4175 4175 if (mp == NULL) {
4176 4176 ip1dbg(("ip_add_info: allocation failure.\n"));
4177 4177 return (data_mp);
4178 4178 }
4179 4179
4180 4180 ipha = (ipha_t *)data_mp->b_rptr;
4181 4181 pinfo = (ip_pktinfo_t *)mp->b_rptr;
4182 4182 bzero(pinfo, sizeof (ip_pktinfo_t));
4183 4183 pinfo->ip_pkt_flags = (uchar_t)flags;
4184 4184 pinfo->ip_pkt_ulp_type = IN_PKTINFO; /* Tell ULP what type of info */
4185 4185
4186 4186 if (flags & (IPF_RECVIF | IPF_RECVADDR))
4187 4187 pinfo->ip_pkt_ifindex = ill->ill_phyint->phyint_ifindex;
4188 4188 if (flags & IPF_RECVADDR) {
4189 4189 ipif_t *ipif;
4190 4190 ire_t *ire;
4191 4191
4192 4192 /*
4193 4193 * Only valid for V4
4194 4194 */
4195 4195 ASSERT((ipha->ipha_version_and_hdr_length & 0xf0) ==
4196 4196 (IPV4_VERSION << 4));
4197 4197
4198 4198 ipif = ipif_get_next_ipif(NULL, ill);
4199 4199 if (ipif != NULL) {
4200 4200 /*
4201 4201 * Since a decision has already been made to deliver the
4202 4202 * packet, there is no need to test for SECATTR and
4203 4203 * ZONEONLY.
4204 4204 * When a multicast packet is transmitted
4205 4205 * a cache entry is created for the multicast address.
4206 4206 * When delivering a copy of the packet or when new
4207 4207 * packets are received we do not want to match on the
4208 4208 * cached entry so explicitly match on
4209 4209 * IRE_LOCAL and IRE_LOOPBACK
4210 4210 */
4211 4211 ire = ire_ctable_lookup(ipha->ipha_dst, 0,
4212 4212 IRE_LOCAL | IRE_LOOPBACK,
4213 4213 ipif, zoneid, NULL,
4214 4214 MATCH_IRE_TYPE | MATCH_IRE_ILL_GROUP, ipst);
4215 4215 if (ire == NULL) {
4216 4216 /*
4217 4217 * packet must have come on a different
4218 4218 * interface.
4219 4219 * Since a decision has already been made to
4220 4220 * deliver the packet, there is no need to test
4221 4221 * for SECATTR and ZONEONLY.
4222 4222 * Only match on local and broadcast ire's.
4223 4223 * See detailed comment above.
4224 4224 */
4225 4225 ire = ire_ctable_lookup(ipha->ipha_dst, 0,
4226 4226 IRE_LOCAL | IRE_LOOPBACK, ipif, zoneid,
4227 4227 NULL, MATCH_IRE_TYPE, ipst);
4228 4228 }
4229 4229
4230 4230 if (ire == NULL) {
4231 4231 /*
4232 4232 * This is either a multicast packet or
4233 4233 * the address has been removed since
4234 4234 * the packet was received.
4235 4235 * Return INADDR_ANY so that normal source
4236 4236 * selection occurs for the response.
4237 4237 */
4238 4238
4239 4239 pinfo->ip_pkt_match_addr.s_addr = INADDR_ANY;
4240 4240 } else {
4241 4241 pinfo->ip_pkt_match_addr.s_addr =
4242 4242 ire->ire_src_addr;
4243 4243 ire_refrele(ire);
4244 4244 }
4245 4245 ipif_refrele(ipif);
4246 4246 } else {
4247 4247 pinfo->ip_pkt_match_addr.s_addr = INADDR_ANY;
4248 4248 }
4249 4249 }
4250 4250
4251 4251 pether = (struct ether_header *)((char *)ipha
4252 4252 - sizeof (struct ether_header));
4253 4253 /*
4254 4254 * Make sure the interface is an ethernet type, since this option
4255 4255 * is currently supported only on this type of interface. Also make
4256 4256 * sure we are pointing correctly above db_base.
4257 4257 */
4258 4258
4259 4259 if ((flags & IPF_RECVSLLA) &&
4260 4260 ((uchar_t *)pether >= data_mp->b_datap->db_base) &&
4261 4261 (ill->ill_type == IFT_ETHER) &&
4262 4262 (ill->ill_net_type == IRE_IF_RESOLVER)) {
4263 4263
4264 4264 pinfo->ip_pkt_slla.sdl_type = IFT_ETHER;
4265 4265 bcopy((uchar_t *)pether->ether_shost.ether_addr_octet,
4266 4266 (uchar_t *)pinfo->ip_pkt_slla.sdl_data, ETHERADDRL);
4267 4267 } else {
4268 4268 /*
4269 4269 * Clear the bit. Indicate to upper layer that IP is not
4270 4270 * sending this ancillary info.
4271 4271 */
4272 4272 pinfo->ip_pkt_flags = pinfo->ip_pkt_flags & ~IPF_RECVSLLA;
4273 4273 }
4274 4274
4275 4275 mp->b_datap->db_type = M_CTL;
4276 4276 mp->b_wptr += sizeof (ip_pktinfo_t);
4277 4277 mp->b_cont = data_mp;
4278 4278
4279 4279 return (mp);
4280 4280 }
4281 4281
4282 4282 /*
4283 4283 * Latch in the IPsec state for a stream based on the ipsec_in_t passed in as
4284 4284 * part of the bind request.
4285 4285 */
4286 4286
4287 4287 boolean_t
4288 4288 ip_bind_ipsec_policy_set(conn_t *connp, mblk_t *policy_mp)
4289 4289 {
4290 4290 ipsec_in_t *ii;
4291 4291
4292 4292 ASSERT(policy_mp != NULL);
4293 4293 ASSERT(policy_mp->b_datap->db_type == IPSEC_POLICY_SET);
4294 4294
4295 4295 ii = (ipsec_in_t *)policy_mp->b_rptr;
4296 4296 ASSERT(ii->ipsec_in_type == IPSEC_IN);
4297 4297
4298 4298 connp->conn_policy = ii->ipsec_in_policy;
4299 4299 ii->ipsec_in_policy = NULL;
4300 4300
4301 4301 if (ii->ipsec_in_action != NULL) {
4302 4302 if (connp->conn_latch == NULL) {
4303 4303 connp->conn_latch = iplatch_create();
4304 4304 if (connp->conn_latch == NULL)
4305 4305 return (B_FALSE);
4306 4306 }
4307 4307 ipsec_latch_inbound(connp->conn_latch, ii);
4308 4308 }
4309 4309 return (B_TRUE);
4310 4310 }
4311 4311
4312 4312 /*
4313 4313 * Upper level protocols (ULP) pass through bind requests to IP for inspection
4314 4314 * and to arrange for power-fanout assist. The ULP is identified by
4315 4315 * adding a single byte at the end of the original bind message.
4316 4316 * A ULP other than UDP or TCP that wishes to be recognized passes
4317 4317 * down a bind with a zero length address.
4318 4318 *
4319 4319 * The binding works as follows:
4320 4320 * - A zero byte address means just bind to the protocol.
4321 4321 * - A four byte address is treated as a request to validate
4322 4322 * that the address is a valid local address, appropriate for
4323 4323 * an application to bind to. This does not affect any fanout
4324 4324 * information in IP.
4325 4325 * - A sizeof sin_t byte address is used to bind to only the local address
4326 4326 * and port.
4327 4327 * - A sizeof ipa_conn_t byte address contains complete fanout information
4328 4328 * consisting of local and remote addresses and ports. In
4329 4329 * this case, the addresses are both validated as appropriate
4330 4330 * for this operation, and, if so, the information is retained
4331 4331 * for use in the inbound fanout.
4332 4332 *
4333 4333 * The ULP (except in the zero-length bind) can append an
4334 4334 * additional mblk of db_type IRE_DB_REQ_TYPE or IPSEC_POLICY_SET to the
4335 4335 * T_BIND_REQ/O_T_BIND_REQ. IRE_DB_REQ_TYPE indicates that the ULP wants
4336 4336 * a copy of the source or destination IRE (source for local bind;
4337 4337 * destination for complete bind). IPSEC_POLICY_SET indicates that the
4338 4338 * policy information contained should be copied on to the conn.
4339 4339 *
4340 4340 * NOTE : Only one of IRE_DB_REQ_TYPE or IPSEC_POLICY_SET can be present.
4341 4341 */
4342 4342 mblk_t *
4343 4343 ip_bind_v4(queue_t *q, mblk_t *mp, conn_t *connp)
4344 4344 {
4345 4345 ssize_t len;
4346 4346 struct T_bind_req *tbr;
4347 4347 sin_t *sin;
4348 4348 ipa_conn_t *ac;
4349 4349 uchar_t *ucp;
4350 4350 mblk_t *mp1;
4351 4351 boolean_t ire_requested;
4352 4352 boolean_t ipsec_policy_set = B_FALSE;
4353 4353 int error = 0;
4354 4354 int protocol;
4355 4355 ipa_conn_x_t *acx;
4356 4356
4357 4357 ASSERT(!connp->conn_af_isv6);
4358 4358 connp->conn_pkt_isv6 = B_FALSE;
4359 4359
4360 4360 len = MBLKL(mp);
4361 4361 if (len < (sizeof (*tbr) + 1)) {
4362 4362 (void) mi_strlog(q, 1, SL_ERROR|SL_TRACE,
4363 4363 "ip_bind: bogus msg, len %ld", len);
4364 4364 /* XXX: Need to return something better */
4365 4365 goto bad_addr;
4366 4366 }
4367 4367 /* Back up and extract the protocol identifier. */
4368 4368 mp->b_wptr--;
4369 4369 protocol = *mp->b_wptr & 0xFF;
4370 4370 tbr = (struct T_bind_req *)mp->b_rptr;
4371 4371 /* Reset the message type in preparation for shipping it back. */
4372 4372 DB_TYPE(mp) = M_PCPROTO;
4373 4373
4374 4374 connp->conn_ulp = (uint8_t)protocol;
4375 4375
4376 4376 /*
4377 4377 * Check for a zero length address. This is from a protocol that
4378 4378 * wants to register to receive all packets of its type.
4379 4379 */
4380 4380 if (tbr->ADDR_length == 0) {
4381 4381 /*
4382 4382 * These protocols are now intercepted in ip_bind_v6().
4383 4383 * Reject protocol-level binds here for now.
4384 4384 *
4385 4385 * For SCTP raw socket, ICMP sends down a bind with sin_t
4386 4386 * so that the protocol type cannot be SCTP.
4387 4387 */
4388 4388 if (protocol == IPPROTO_TCP || protocol == IPPROTO_AH ||
4389 4389 protocol == IPPROTO_ESP || protocol == IPPROTO_SCTP) {
4390 4390 goto bad_addr;
4391 4391 }
4392 4392
4393 4393 /*
4394 4394 *
4395 4395 * The udp module never sends down a zero-length address,
4396 4396 * and allowing this on a labeled system will break MLP
4397 4397 * functionality.
4398 4398 */
4399 4399 if (is_system_labeled() && protocol == IPPROTO_UDP)
4400 4400 goto bad_addr;
4401 4401
4402 4402 if (connp->conn_mac_exempt)
4403 4403 goto bad_addr;
4404 4404
4405 4405 /* No hash here really. The table is big enough. */
4406 4406 connp->conn_srcv6 = ipv6_all_zeros;
4407 4407
4408 4408 ipcl_proto_insert(connp, protocol);
4409 4409
4410 4410 tbr->PRIM_type = T_BIND_ACK;
4411 4411 return (mp);
4412 4412 }
4413 4413
4414 4414 /* Extract the address pointer from the message. */
4415 4415 ucp = (uchar_t *)mi_offset_param(mp, tbr->ADDR_offset,
4416 4416 tbr->ADDR_length);
4417 4417 if (ucp == NULL) {
4418 4418 ip1dbg(("ip_bind: no address\n"));
4419 4419 goto bad_addr;
4420 4420 }
4421 4421 if (!OK_32PTR(ucp)) {
4422 4422 ip1dbg(("ip_bind: unaligned address\n"));
4423 4423 goto bad_addr;
4424 4424 }
4425 4425 /*
4426 4426 * Check for trailing mps.
4427 4427 */
4428 4428
4429 4429 mp1 = mp->b_cont;
4430 4430 ire_requested = (mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE);
4431 4431 ipsec_policy_set = (mp1 != NULL && DB_TYPE(mp1) == IPSEC_POLICY_SET);
4432 4432
4433 4433 switch (tbr->ADDR_length) {
4434 4434 default:
4435 4435 ip1dbg(("ip_bind: bad address length %d\n",
4436 4436 (int)tbr->ADDR_length));
4437 4437 goto bad_addr;
4438 4438
4439 4439 case IP_ADDR_LEN:
4440 4440 /* Verification of local address only */
4441 4441 error = ip_bind_laddr(connp, mp, *(ipaddr_t *)ucp, 0,
4442 4442 ire_requested, ipsec_policy_set, B_FALSE);
4443 4443 break;
4444 4444
4445 4445 case sizeof (sin_t):
4446 4446 sin = (sin_t *)ucp;
4447 4447 error = ip_bind_laddr(connp, mp, sin->sin_addr.s_addr,
4448 4448 sin->sin_port, ire_requested, ipsec_policy_set, B_TRUE);
4449 4449 break;
4450 4450
4451 4451 case sizeof (ipa_conn_t):
4452 4452 ac = (ipa_conn_t *)ucp;
4453 4453 /* For raw socket, the local port is not set. */
4454 4454 if (ac->ac_lport == 0)
4455 4455 ac->ac_lport = connp->conn_lport;
4456 4456 /* Always verify destination reachability. */
4457 4457 error = ip_bind_connected(connp, mp, &ac->ac_laddr,
4458 4458 ac->ac_lport, ac->ac_faddr, ac->ac_fport, ire_requested,
4459 4459 ipsec_policy_set, B_TRUE, B_TRUE);
4460 4460 break;
4461 4461
4462 4462 case sizeof (ipa_conn_x_t):
4463 4463 acx = (ipa_conn_x_t *)ucp;
4464 4464 /*
4465 4465 * Whether or not to verify destination reachability depends
4466 4466 * on the setting of the ACX_VERIFY_DST flag in acx->acx_flags.
4467 4467 */
4468 4468 error = ip_bind_connected(connp, mp, &acx->acx_conn.ac_laddr,
4469 4469 acx->acx_conn.ac_lport, acx->acx_conn.ac_faddr,
4470 4470 acx->acx_conn.ac_fport, ire_requested, ipsec_policy_set,
4471 4471 B_TRUE, (acx->acx_flags & ACX_VERIFY_DST) != 0);
4472 4472 break;
4473 4473 }
4474 4474 if (error == EINPROGRESS)
4475 4475 return (NULL);
4476 4476 else if (error != 0)
4477 4477 goto bad_addr;
4478 4478 /*
4479 4479 * Pass the IPsec headers size in ire_ipsec_overhead.
4480 4480 * We can't do this in ip_bind_insert_ire because the policy
4481 4481 * may not have been inherited at that point in time and hence
4482 4482 * conn_out_enforce_policy may not be set.
4483 4483 */
4484 4484 mp1 = mp->b_cont;
4485 4485 if (ire_requested && connp->conn_out_enforce_policy &&
4486 4486 mp1 != NULL && DB_TYPE(mp1) == IRE_DB_REQ_TYPE) {
4487 4487 ire_t *ire = (ire_t *)mp1->b_rptr;
4488 4488 ASSERT(MBLKL(mp1) >= sizeof (ire_t));
4489 4489 ire->ire_ipsec_overhead = conn_ipsec_length(connp);
4490 4490 }
4491 4491
4492 4492 /* Send it home. */
4493 4493 mp->b_datap->db_type = M_PCPROTO;
4494 4494 tbr->PRIM_type = T_BIND_ACK;
4495 4495 return (mp);
4496 4496
4497 4497 bad_addr:
4498 4498 /*
4499 4499 * If error = -1 then we generate a TBADADDR - otherwise error is
4500 4500 * a unix errno.
4501 4501 */
4502 4502 if (error > 0)
4503 4503 mp = mi_tpi_err_ack_alloc(mp, TSYSERR, error);
4504 4504 else
4505 4505 mp = mi_tpi_err_ack_alloc(mp, TBADADDR, 0);
4506 4506 return (mp);
4507 4507 }
4508 4508
4509 4509 /*
4510 4510 * Here address is verified to be a valid local address.
4511 4511 * If the IRE_DB_REQ_TYPE mp is present, a broadcast/multicast
4512 4512 * address is also considered a valid local address.
4513 4513 * In the case of a broadcast/multicast address, however, the
4514 4514 * upper protocol is expected to reset the src address
4515 4515 * to 0 if it sees a IRE_BROADCAST type returned so that
4516 4516 * no packets are emitted with broadcast/multicast address as
4517 4517 * source address (that violates hosts requirements RFC1122)
4518 4518 * The addresses valid for bind are:
4519 4519 * (1) - INADDR_ANY (0)
4520 4520 * (2) - IP address of an UP interface
4521 4521 * (3) - IP address of a DOWN interface
4522 4522 * (4) - valid local IP broadcast addresses. In this case
4523 4523 * the conn will only receive packets destined to
4524 4524 * the specified broadcast address.
4525 4525 * (5) - a multicast address. In this case
4526 4526 * the conn will only receive packets destined to
4527 4527 * the specified multicast address. Note: the
4528 4528 * application still has to issue an
4529 4529 * IP_ADD_MEMBERSHIP socket option.
4530 4530 *
4531 4531 * On error, return -1 for TBADADDR otherwise pass the
4532 4532 * errno with TSYSERR reply.
4533 4533 *
4534 4534 * In all the above cases, the bound address must be valid in the current zone.
4535 4535 * When the address is loopback, multicast or broadcast, there might be many
4536 4536 * matching IREs so bind has to look up based on the zone.
4537 4537 *
4538 4538 * Note: lport is in network byte order.
4539 4539 */
4540 4540 int
4541 4541 ip_bind_laddr(conn_t *connp, mblk_t *mp, ipaddr_t src_addr, uint16_t lport,
4542 4542 boolean_t ire_requested, boolean_t ipsec_policy_set,
4543 4543 boolean_t fanout_insert)
4544 4544 {
4545 4545 int error = 0;
4546 4546 ire_t *src_ire;
4547 4547 mblk_t *policy_mp;
4548 4548 ipif_t *ipif;
4549 4549 zoneid_t zoneid;
4550 4550 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
4551 4551
4552 4552 if (ipsec_policy_set) {
4553 4553 policy_mp = mp->b_cont;
4554 4554 }
4555 4555
4556 4556 /*
4557 4557 * If it was previously connected, conn_fully_bound would have
4558 4558 * been set.
4559 4559 */
4560 4560 connp->conn_fully_bound = B_FALSE;
4561 4561
4562 4562 src_ire = NULL;
4563 4563 ipif = NULL;
4564 4564
4565 4565 zoneid = IPCL_ZONEID(connp);
4566 4566
4567 4567 if (src_addr) {
4568 4568 src_ire = ire_route_lookup(src_addr, 0, 0, 0,
4569 4569 NULL, NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, ipst);
4570 4570 /*
4571 4571 * If an address other than 0.0.0.0 is requested,
4572 4572 * we verify that it is a valid address for bind
4573 4573 * Note: Following code is in if-else-if form for
4574 4574 * readability compared to a condition check.
4575 4575 */
4576 4576 /* LINTED - statement has no consequent */
4577 4577 if (IRE_IS_LOCAL(src_ire)) {
4578 4578 /*
4579 4579 * (2) Bind to address of local UP interface
4580 4580 */
4581 4581 } else if (src_ire && src_ire->ire_type == IRE_BROADCAST) {
4582 4582 /*
4583 4583 * (4) Bind to broadcast address
4584 4584 * Note: permitted only from transports that
4585 4585 * request IRE
4586 4586 */
4587 4587 if (!ire_requested)
4588 4588 error = EADDRNOTAVAIL;
4589 4589 } else {
4590 4590 /*
4591 4591 * (3) Bind to address of local DOWN interface
4592 4592 * (ipif_lookup_addr() looks up all interfaces
4593 4593 * but we do not get here for UP interfaces
4594 4594 * - case (2) above)
4595 4595 * We put the protocol byte back into the mblk
4596 4596 * since we may come back via ip_wput_nondata()
4597 4597 * later with this mblk if ipif_lookup_addr chooses
4598 4598 * to defer processing.
4599 4599 */
4600 4600 *mp->b_wptr++ = (char)connp->conn_ulp;
4601 4601 if ((ipif = ipif_lookup_addr(src_addr, NULL, zoneid,
4602 4602 CONNP_TO_WQ(connp), mp, ip_wput_nondata,
4603 4603 &error, ipst)) != NULL) {
4604 4604 ipif_refrele(ipif);
4605 4605 } else if (error == EINPROGRESS) {
4606 4606 if (src_ire != NULL)
4607 4607 ire_refrele(src_ire);
4608 4608 return (EINPROGRESS);
4609 4609 } else if (CLASSD(src_addr)) {
4610 4610 error = 0;
4611 4611 if (src_ire != NULL)
4612 4612 ire_refrele(src_ire);
4613 4613 /*
4614 4614 * (5) bind to multicast address.
4615 4615 * Fake out the IRE returned to upper
4616 4616 * layer to be a broadcast IRE.
4617 4617 */
4618 4618 src_ire = ire_ctable_lookup(
4619 4619 INADDR_BROADCAST, INADDR_ANY,
4620 4620 IRE_BROADCAST, NULL, zoneid, NULL,
4621 4621 (MATCH_IRE_TYPE | MATCH_IRE_ZONEONLY),
4622 4622 ipst);
4623 4623 if (src_ire == NULL || !ire_requested)
4624 4624 error = EADDRNOTAVAIL;
4625 4625 } else {
4626 4626 /*
4627 4627 * Not a valid address for bind
4628 4628 */
4629 4629 error = EADDRNOTAVAIL;
4630 4630 }
4631 4631 /*
4632 4632 * Just to keep it consistent with the processing in
4633 4633 * ip_bind_v4()
4634 4634 */
4635 4635 mp->b_wptr--;
4636 4636 }
4637 4637 if (error) {
4638 4638 /* Red Alert! Attempting to be a bogon! */
4639 4639 ip1dbg(("ip_bind: bad src address 0x%x\n",
4640 4640 ntohl(src_addr)));
4641 4641 goto bad_addr;
4642 4642 }
4643 4643 }
4644 4644
4645 4645 /*
4646 4646 * Allow setting new policies. For example, disconnects come
4647 4647 * down as ipa_t bind. As we would have set conn_policy_cached
4648 4648 * to B_TRUE before, we should set it to B_FALSE, so that policy
4649 4649 * can change after the disconnect.
4650 4650 */
4651 4651 connp->conn_policy_cached = B_FALSE;
4652 4652
4653 4653 /*
4654 4654 * If not fanout_insert this was just an address verification
4655 4655 */
4656 4656 if (fanout_insert) {
4657 4657 /*
4658 4658 * The addresses have been verified. Time to insert in
4659 4659 * the correct fanout list.
4660 4660 */
4661 4661 IN6_IPADDR_TO_V4MAPPED(src_addr, &connp->conn_srcv6);
4662 4662 IN6_IPADDR_TO_V4MAPPED(INADDR_ANY, &connp->conn_remv6);
4663 4663 connp->conn_lport = lport;
4664 4664 connp->conn_fport = 0;
4665 4665 /*
4666 4666 * Do we need to add a check to reject Multicast packets
4667 4667 */
4668 4668 error = ipcl_bind_insert(connp, *mp->b_wptr, src_addr, lport);
4669 4669 }
4670 4670
4671 4671 if (error == 0) {
4672 4672 if (ire_requested) {
4673 4673 if (!ip_bind_insert_ire(mp, src_ire, NULL, ipst)) {
4674 4674 error = -1;
4675 4675 /* Falls through to bad_addr */
4676 4676 }
4677 4677 } else if (ipsec_policy_set) {
4678 4678 if (!ip_bind_ipsec_policy_set(connp, policy_mp)) {
4679 4679 error = -1;
4680 4680 /* Falls through to bad_addr */
4681 4681 }
4682 4682 }
4683 4683 }
4684 4684 bad_addr:
4685 4685 if (error != 0) {
4686 4686 if (connp->conn_anon_port) {
4687 4687 (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
4688 4688 connp->conn_mlp_type, connp->conn_ulp, ntohs(lport),
4689 4689 B_FALSE);
4690 4690 }
4691 4691 connp->conn_mlp_type = mlptSingle;
4692 4692 }
4693 4693 if (src_ire != NULL)
4694 4694 IRE_REFRELE(src_ire);
4695 4695 if (ipsec_policy_set) {
4696 4696 ASSERT(policy_mp == mp->b_cont);
4697 4697 ASSERT(policy_mp != NULL);
4698 4698 freeb(policy_mp);
4699 4699 /*
4700 4700 * As of now assume that nothing else accompanies
4701 4701 * IPSEC_POLICY_SET.
4702 4702 */
4703 4703 mp->b_cont = NULL;
4704 4704 }
4705 4705 return (error);
4706 4706 }
4707 4707
4708 4708 /*
4709 4709 * Verify that both the source and destination addresses
4710 4710 * are valid. If verify_dst is false, then the destination address may be
4711 4711 * unreachable, i.e. have no route to it. Protocols like TCP want to verify
4712 4712 * destination reachability, while tunnels do not.
4713 4713 * Note that we allow connect to broadcast and multicast
4714 4714 * addresses when ire_requested is set. Thus the ULP
4715 4715 * has to check for IRE_BROADCAST and multicast.
4716 4716 *
4717 4717 * Returns zero if ok.
4718 4718 * On error: returns -1 to mean TBADADDR otherwise returns an errno
4719 4719 * (for use with TSYSERR reply).
4720 4720 *
4721 4721 * Note: lport and fport are in network byte order.
4722 4722 */
4723 4723 int
4724 4724 ip_bind_connected(conn_t *connp, mblk_t *mp, ipaddr_t *src_addrp,
4725 4725 uint16_t lport, ipaddr_t dst_addr, uint16_t fport,
4726 4726 boolean_t ire_requested, boolean_t ipsec_policy_set,
4727 4727 boolean_t fanout_insert, boolean_t verify_dst)
4728 4728 {
4729 4729 ire_t *src_ire;
4730 4730 ire_t *dst_ire;
4731 4731 int error = 0;
4732 4732 int protocol;
4733 4733 mblk_t *policy_mp;
4734 4734 ire_t *sire = NULL;
4735 4735 ire_t *md_dst_ire = NULL;
4736 4736 ire_t *lso_dst_ire = NULL;
4737 4737 ill_t *ill = NULL;
4738 4738 zoneid_t zoneid;
4739 4739 ipaddr_t src_addr = *src_addrp;
4740 4740 ip_stack_t *ipst = connp->conn_netstack->netstack_ip;
4741 4741
4742 4742 src_ire = dst_ire = NULL;
4743 4743 protocol = *mp->b_wptr & 0xFF;
4744 4744
4745 4745 /*
4746 4746 * If we never got a disconnect before, clear it now.
4747 4747 */
4748 4748 connp->conn_fully_bound = B_FALSE;
4749 4749
4750 4750 if (ipsec_policy_set) {
4751 4751 policy_mp = mp->b_cont;
4752 4752 }
4753 4753
4754 4754 zoneid = IPCL_ZONEID(connp);
4755 4755
4756 4756 if (CLASSD(dst_addr)) {
4757 4757 /* Pick up an IRE_BROADCAST */
4758 4758 dst_ire = ire_route_lookup(ip_g_all_ones, 0, 0, 0, NULL,
4759 4759 NULL, zoneid, MBLK_GETLABEL(mp),
4760 4760 (MATCH_IRE_RECURSIVE |
4761 4761 MATCH_IRE_DEFAULT | MATCH_IRE_RJ_BHOLE |
4762 4762 MATCH_IRE_SECATTR), ipst);
4763 4763 } else {
4764 4764 /*
4765 4765 * If conn_dontroute is set or if conn_nexthop_set is set,
4766 4766 * and onlink ipif is not found set ENETUNREACH error.
4767 4767 */
4768 4768 if (connp->conn_dontroute || connp->conn_nexthop_set) {
4769 4769 ipif_t *ipif;
4770 4770
4771 4771 ipif = ipif_lookup_onlink_addr(connp->conn_dontroute ?
4772 4772 dst_addr : connp->conn_nexthop_v4, zoneid, ipst);
4773 4773 if (ipif == NULL) {
4774 4774 error = ENETUNREACH;
4775 4775 goto bad_addr;
4776 4776 }
4777 4777 ipif_refrele(ipif);
4778 4778 }
4779 4779
4780 4780 if (connp->conn_nexthop_set) {
4781 4781 dst_ire = ire_route_lookup(connp->conn_nexthop_v4, 0,
4782 4782 0, 0, NULL, NULL, zoneid, MBLK_GETLABEL(mp),
4783 4783 MATCH_IRE_SECATTR, ipst);
4784 4784 } else {
4785 4785 dst_ire = ire_route_lookup(dst_addr, 0, 0, 0, NULL,
4786 4786 &sire, zoneid, MBLK_GETLABEL(mp),
4787 4787 (MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
4788 4788 MATCH_IRE_PARENT | MATCH_IRE_RJ_BHOLE |
4789 4789 MATCH_IRE_SECATTR), ipst);
4790 4790 }
4791 4791 }
4792 4792 /*
4793 4793 * dst_ire can't be a broadcast when not ire_requested.
4794 4794 * We also prevent ire's with src address INADDR_ANY to
4795 4795 * be used, which are created temporarily for
4796 4796 * sending out packets from endpoints that have
4797 4797 * conn_unspec_src set. If verify_dst is true, the destination must be
4798 4798 * reachable. If verify_dst is false, the destination needn't be
4799 4799 * reachable.
4800 4800 *
4801 4801 * If we match on a reject or black hole, then we've got a
4802 4802 * local failure. May as well fail out the connect() attempt,
4803 4803 * since it's never going to succeed.
4804 4804 */
4805 4805 if (dst_ire == NULL || dst_ire->ire_src_addr == INADDR_ANY ||
4806 4806 (dst_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) ||
4807 4807 ((dst_ire->ire_type & IRE_BROADCAST) && !ire_requested)) {
4808 4808 /*
4809 4809 * If we're verifying destination reachability, we always want
4810 4810 * to complain here.
4811 4811 *
4812 4812 * If we're not verifying destination reachability but the
4813 4813 * destination has a route, we still want to fail on the
4814 4814 * temporary address and broadcast address tests.
4815 4815 */
4816 4816 if (verify_dst || (dst_ire != NULL)) {
4817 4817 if (ip_debug > 2) {
4818 4818 pr_addr_dbg("ip_bind_connected: bad connected "
4819 4819 "dst %s\n", AF_INET, &dst_addr);
4820 4820 }
4821 4821 if (dst_ire == NULL || !(dst_ire->ire_type & IRE_HOST))
4822 4822 error = ENETUNREACH;
4823 4823 else
4824 4824 error = EHOSTUNREACH;
4825 4825 goto bad_addr;
4826 4826 }
4827 4827 }
4828 4828
4829 4829 /*
4830 4830 * We now know that routing will allow us to reach the destination.
4831 4831 * Check whether Trusted Solaris policy allows communication with this
4832 4832 * host, and pretend that the destination is unreachable if not.
4833 4833 *
4834 4834 * This is never a problem for TCP, since that transport is known to
4835 4835 * compute the label properly as part of the tcp_rput_other T_BIND_ACK
4836 4836 * handling. If the remote is unreachable, it will be detected at that
4837 4837 * point, so there's no reason to check it here.
4838 4838 *
4839 4839 * Note that for sendto (and other datagram-oriented friends), this
4840 4840 * check is done as part of the data path label computation instead.
4841 4841 * The check here is just to make non-TCP connect() report the right
4842 4842 * error.
4843 4843 */
4844 4844 if (dst_ire != NULL && is_system_labeled() &&
4845 4845 !IPCL_IS_TCP(connp) &&
4846 4846 tsol_compute_label(DB_CREDDEF(mp, connp->conn_cred), dst_addr, NULL,
4847 4847 connp->conn_mac_exempt, ipst) != 0) {
4848 4848 error = EHOSTUNREACH;
4849 4849 if (ip_debug > 2) {
4850 4850 pr_addr_dbg("ip_bind_connected: no label for dst %s\n",
4851 4851 AF_INET, &dst_addr);
4852 4852 }
4853 4853 goto bad_addr;
4854 4854 }
4855 4855
4856 4856 /*
4857 4857 * If the app does a connect(), it means that it will most likely
4858 4858 * send more than 1 packet to the destination. It makes sense
4859 4859 * to clear the temporary flag.
4860 4860 */
4861 4861 if (dst_ire != NULL && dst_ire->ire_type == IRE_CACHE &&
4862 4862 (dst_ire->ire_marks & IRE_MARK_TEMPORARY)) {
4863 4863 irb_t *irb = dst_ire->ire_bucket;
4864 4864
4865 4865 rw_enter(&irb->irb_lock, RW_WRITER);
4866 4866 /*
4867 4867 * We need to recheck for IRE_MARK_TEMPORARY after acquiring
4868 4868 * the lock to guarantee irb_tmp_ire_cnt.
4869 4869 */
4870 4870 if (dst_ire->ire_marks & IRE_MARK_TEMPORARY) {
4871 4871 dst_ire->ire_marks &= ~IRE_MARK_TEMPORARY;
4872 4872 irb->irb_tmp_ire_cnt--;
4873 4873 }
4874 4874 rw_exit(&irb->irb_lock);
4875 4875 }
4876 4876
4877 4877 /*
4878 4878 * See if we should notify ULP about LSO/MDT; we do this whether or not
4879 4879 * ire_requested is TRUE, in order to handle active connects; LSO/MDT
4880 4880 * eligibility tests for passive connects are handled separately
4881 4881 * through tcp_adapt_ire(). We do this before the source address
4882 4882 * selection, because dst_ire may change after a call to
4883 4883 * ipif_select_source(). This is a best-effort check, as the
4884 4884 * packet for this connection may not actually go through
4885 4885 * dst_ire->ire_stq, and the exact IRE can only be known after
4886 4886 * calling ip_newroute(). This is why we further check on the
4887 4887 * IRE during LSO/Multidata packet transmission in
4888 4888 * tcp_lsosend()/tcp_multisend().
4889 4889 */
4890 4890 if (!ipsec_policy_set && dst_ire != NULL &&
4891 4891 !(dst_ire->ire_type & (IRE_LOCAL | IRE_LOOPBACK | IRE_BROADCAST)) &&
4892 4892 (ill = ire_to_ill(dst_ire), ill != NULL)) {
4893 4893 if (ipst->ips_ip_lso_outbound && ILL_LSO_CAPABLE(ill)) {
4894 4894 lso_dst_ire = dst_ire;
4895 4895 IRE_REFHOLD(lso_dst_ire);
4896 4896 } else if (ipst->ips_ip_multidata_outbound &&
4897 4897 ILL_MDT_CAPABLE(ill)) {
4898 4898 md_dst_ire = dst_ire;
4899 4899 IRE_REFHOLD(md_dst_ire);
4900 4900 }
4901 4901 }
4902 4902
4903 4903 if (dst_ire != NULL &&
4904 4904 dst_ire->ire_type == IRE_LOCAL &&
4905 4905 dst_ire->ire_zoneid != zoneid && dst_ire->ire_zoneid != ALL_ZONES) {
4906 4906 /*
4907 4907 * If the IRE belongs to a different zone, look for a matching
4908 4908 * route in the forwarding table and use the source address from
4909 4909 * that route.
4910 4910 */
4911 4911 src_ire = ire_ftable_lookup(dst_addr, 0, 0, 0, NULL, NULL,
4912 4912 zoneid, 0, NULL,
4913 4913 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
4914 4914 MATCH_IRE_RJ_BHOLE, ipst);
4915 4915 if (src_ire == NULL) {
4916 4916 error = EHOSTUNREACH;
4917 4917 goto bad_addr;
4918 4918 } else if (src_ire->ire_flags & (RTF_REJECT|RTF_BLACKHOLE)) {
4919 4919 if (!(src_ire->ire_type & IRE_HOST))
4920 4920 error = ENETUNREACH;
4921 4921 else
4922 4922 error = EHOSTUNREACH;
4923 4923 goto bad_addr;
4924 4924 }
4925 4925 if (src_addr == INADDR_ANY)
4926 4926 src_addr = src_ire->ire_src_addr;
4927 4927 ire_refrele(src_ire);
4928 4928 src_ire = NULL;
4929 4929 } else if ((src_addr == INADDR_ANY) && (dst_ire != NULL)) {
4930 4930 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) {
4931 4931 src_addr = sire->ire_src_addr;
4932 4932 ire_refrele(dst_ire);
4933 4933 dst_ire = sire;
4934 4934 sire = NULL;
4935 4935 } else {
4936 4936 /*
4937 4937 * Pick a source address so that a proper inbound
4938 4938 * load spreading would happen.
4939 4939 */
4940 4940 ill_t *dst_ill = dst_ire->ire_ipif->ipif_ill;
4941 4941 ipif_t *src_ipif = NULL;
4942 4942 ire_t *ipif_ire;
4943 4943
4944 4944 /*
4945 4945 * Supply a local source address such that inbound
4946 4946 * load spreading happens.
4947 4947 *
4948 4948 * Determine the best source address on this ill for
4949 4949 * the destination.
4950 4950 *
4951 4951 * 1) For broadcast, we should return a broadcast ire
4952 4952 * found above so that upper layers know that the
4953 4953 * destination address is a broadcast address.
4954 4954 *
4955 4955 * 2) If this is part of a group, select a better
4956 4956 * source address so that better inbound load
4957 4957 * balancing happens. Do the same if the ipif
4958 4958 * is DEPRECATED.
4959 4959 *
4960 4960 * 3) If the outgoing interface is part of a usesrc
4961 4961 * group, then try selecting a source address from
4962 4962 * the usesrc ILL.
4963 4963 */
4964 4964 if ((dst_ire->ire_zoneid != zoneid &&
4965 4965 dst_ire->ire_zoneid != ALL_ZONES) ||
4966 4966 (!(dst_ire->ire_flags & RTF_SETSRC)) &&
4967 4967 (!(dst_ire->ire_type & IRE_BROADCAST) &&
4968 4968 ((dst_ill->ill_group != NULL) ||
4969 4969 (dst_ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) ||
4970 4970 (dst_ill->ill_usesrc_ifindex != 0)))) {
4971 4971 /*
4972 4972 * If the destination is reachable via a
4973 4973 * given gateway, the selected source address
4974 4974 * should be in the same subnet as the gateway.
4975 4975 * Otherwise, the destination is not reachable.
4976 4976 *
4977 4977 * If there are no interfaces on the same subnet
4978 4978 * as the destination, ipif_select_source gives
4979 4979 * first non-deprecated interface which might be
4980 4980 * on a different subnet than the gateway.
4981 4981 * This is not desirable. Hence pass the dst_ire
4982 4982 * source address to ipif_select_source.
4983 4983 * It is sure that the destination is reachable
4984 4984 * with the dst_ire source address subnet.
4985 4985 * So passing dst_ire source address to
4986 4986 * ipif_select_source will make sure that the
4987 4987 * selected source will be on the same subnet
4988 4988 * as dst_ire source address.
4989 4989 */
4990 4990 ipaddr_t saddr =
4991 4991 dst_ire->ire_ipif->ipif_src_addr;
4992 4992 src_ipif = ipif_select_source(dst_ill,
4993 4993 saddr, zoneid);
4994 4994 if (src_ipif != NULL) {
4995 4995 if (IS_VNI(src_ipif->ipif_ill)) {
4996 4996 /*
4997 4997 * For VNI there is no
4998 4998 * interface route
4999 4999 */
5000 5000 src_addr =
5001 5001 src_ipif->ipif_src_addr;
5002 5002 } else {
5003 5003 ipif_ire =
5004 5004 ipif_to_ire(src_ipif);
5005 5005 if (ipif_ire != NULL) {
5006 5006 IRE_REFRELE(dst_ire);
5007 5007 dst_ire = ipif_ire;
5008 5008 }
5009 5009 src_addr =
5010 5010 dst_ire->ire_src_addr;
5011 5011 }
5012 5012 ipif_refrele(src_ipif);
5013 5013 } else {
5014 5014 src_addr = dst_ire->ire_src_addr;
5015 5015 }
5016 5016 } else {
5017 5017 src_addr = dst_ire->ire_src_addr;
5018 5018 }
5019 5019 }
5020 5020 }
5021 5021
5022 5022 /*
5023 5023 * We do ire_route_lookup() here (and not
5024 5024 * interface lookup as we assert that
5025 5025 * src_addr should only come from an
5026 5026 * UP interface for hard binding.
5027 5027 */
5028 5028 ASSERT(src_ire == NULL);
5029 5029 src_ire = ire_route_lookup(src_addr, 0, 0, 0, NULL,
5030 5030 NULL, zoneid, NULL, MATCH_IRE_ZONEONLY, ipst);
5031 5031 /* src_ire must be a local|loopback */
5032 5032 if (!IRE_IS_LOCAL(src_ire)) {
5033 5033 if (ip_debug > 2) {
5034 5034 pr_addr_dbg("ip_bind_connected: bad connected "
5035 5035 "src %s\n", AF_INET, &src_addr);
5036 5036 }
5037 5037 error = EADDRNOTAVAIL;
5038 5038 goto bad_addr;
5039 5039 }
5040 5040
5041 5041 /*
5042 5042 * If the source address is a loopback address, the
5043 5043 * destination had best be local or multicast.
5044 5044 * The transports that can't handle multicast will reject
5045 5045 * those addresses.
5046 5046 */
5047 5047 if (src_ire->ire_type == IRE_LOOPBACK &&
5048 5048 !(IRE_IS_LOCAL(dst_ire) || CLASSD(dst_addr))) {
5049 5049 ip1dbg(("ip_bind_connected: bad connected loopback\n"));
5050 5050 error = -1;
5051 5051 goto bad_addr;
5052 5052 }
5053 5053
5054 5054 /*
5055 5055 * Allow setting new policies. For example, disconnects come
5056 5056 * down as ipa_t bind. As we would have set conn_policy_cached
5057 5057 * to B_TRUE before, we should set it to B_FALSE, so that policy
5058 5058 * can change after the disconnect.
5059 5059 */
5060 5060 connp->conn_policy_cached = B_FALSE;
5061 5061
5062 5062 /*
5063 5063 * Set the conn addresses/ports immediately, so the IPsec policy calls
5064 5064 * can handle their passed-in conn's.
5065 5065 */
5066 5066
5067 5067 IN6_IPADDR_TO_V4MAPPED(src_addr, &connp->conn_srcv6);
5068 5068 IN6_IPADDR_TO_V4MAPPED(dst_addr, &connp->conn_remv6);
5069 5069 connp->conn_lport = lport;
5070 5070 connp->conn_fport = fport;
5071 5071 *src_addrp = src_addr;
5072 5072
5073 5073 ASSERT(!(ipsec_policy_set && ire_requested));
5074 5074 if (ire_requested) {
5075 5075 iulp_t *ulp_info = NULL;
5076 5076
5077 5077 /*
5078 5078 * Note that sire will not be NULL if this is an off-link
5079 5079 * connection and there is not cache for that dest yet.
5080 5080 *
5081 5081 * XXX Because of an existing bug, if there are multiple
5082 5082 * default routes, the IRE returned now may not be the actual
5083 5083 * default route used (default routes are chosen in a
5084 5084 * round robin fashion). So if the metrics for different
5085 5085 * default routes are different, we may return the wrong
5086 5086 * metrics. This will not be a problem if the existing
5087 5087 * bug is fixed.
5088 5088 */
5089 5089 if (sire != NULL) {
5090 5090 ulp_info = &(sire->ire_uinfo);
5091 5091 }
5092 5092 if (!ip_bind_insert_ire(mp, dst_ire, ulp_info, ipst)) {
5093 5093 error = -1;
5094 5094 goto bad_addr;
5095 5095 }
5096 5096 } else if (ipsec_policy_set) {
5097 5097 if (!ip_bind_ipsec_policy_set(connp, policy_mp)) {
5098 5098 error = -1;
5099 5099 goto bad_addr;
5100 5100 }
5101 5101 }
5102 5102
5103 5103 /*
5104 5104 * Cache IPsec policy in this conn. If we have per-socket policy,
5105 5105 * we'll cache that. If we don't, we'll inherit global policy.
5106 5106 *
5107 5107 * We can't insert until the conn reflects the policy. Note that
5108 5108 * conn_policy_cached is set by ipsec_conn_cache_policy() even for
5109 5109 * connections where we don't have a policy. This is to prevent
5110 5110 * global policy lookups in the inbound path.
5111 5111 *
5112 5112 * If we insert before we set conn_policy_cached,
5113 5113 * CONN_INBOUND_POLICY_PRESENT() check can still evaluate true
5114 5114 * because global policy cound be non-empty. We normally call
5115 5115 * ipsec_check_policy() for conn_policy_cached connections only if
5116 5116 * ipc_in_enforce_policy is set. But in this case,
5117 5117 * conn_policy_cached can get set anytime since we made the
5118 5118 * CONN_INBOUND_POLICY_PRESENT() check and ipsec_check_policy() is
5119 5119 * called, which will make the above assumption false. Thus, we
5120 5120 * need to insert after we set conn_policy_cached.
5121 5121 */
5122 5122 if ((error = ipsec_conn_cache_policy(connp, B_TRUE)) != 0)
5123 5123 goto bad_addr;
5124 5124
5125 5125 if (fanout_insert) {
5126 5126 /*
5127 5127 * The addresses have been verified. Time to insert in
5128 5128 * the correct fanout list.
5129 5129 */
5130 5130 error = ipcl_conn_insert(connp, protocol, src_addr,
5131 5131 dst_addr, connp->conn_ports);
5132 5132 }
5133 5133
5134 5134 if (error == 0) {
5135 5135 connp->conn_fully_bound = B_TRUE;
5136 5136 /*
5137 5137 * Our initial checks for LSO/MDT have passed; the IRE is not
5138 5138 * LOCAL/LOOPBACK/BROADCAST, and the link layer seems to
5139 5139 * be supporting LSO/MDT. Pass the IRE, IPC and ILL into
5140 5140 * ip_xxinfo_return(), which performs further checks
5141 5141 * against them and upon success, returns the LSO/MDT info
5142 5142 * mblk which we will attach to the bind acknowledgment.
5143 5143 */
5144 5144 if (lso_dst_ire != NULL) {
5145 5145 mblk_t *lsoinfo_mp;
5146 5146
5147 5147 ASSERT(ill->ill_lso_capab != NULL);
5148 5148 if ((lsoinfo_mp = ip_lsoinfo_return(lso_dst_ire, connp,
5149 5149 ill->ill_name, ill->ill_lso_capab)) != NULL)
5150 5150 linkb(mp, lsoinfo_mp);
5151 5151 } else if (md_dst_ire != NULL) {
5152 5152 mblk_t *mdinfo_mp;
5153 5153
5154 5154 ASSERT(ill->ill_mdt_capab != NULL);
5155 5155 if ((mdinfo_mp = ip_mdinfo_return(md_dst_ire, connp,
5156 5156 ill->ill_name, ill->ill_mdt_capab)) != NULL)
5157 5157 linkb(mp, mdinfo_mp);
5158 5158 }
5159 5159 }
5160 5160 bad_addr:
5161 5161 if (ipsec_policy_set) {
5162 5162 ASSERT(policy_mp == mp->b_cont);
5163 5163 ASSERT(policy_mp != NULL);
5164 5164 freeb(policy_mp);
5165 5165 /*
5166 5166 * As of now assume that nothing else accompanies
5167 5167 * IPSEC_POLICY_SET.
5168 5168 */
5169 5169 mp->b_cont = NULL;
5170 5170 }
5171 5171 if (src_ire != NULL)
5172 5172 IRE_REFRELE(src_ire);
5173 5173 if (dst_ire != NULL)
5174 5174 IRE_REFRELE(dst_ire);
5175 5175 if (sire != NULL)
5176 5176 IRE_REFRELE(sire);
5177 5177 if (md_dst_ire != NULL)
5178 5178 IRE_REFRELE(md_dst_ire);
5179 5179 if (lso_dst_ire != NULL)
5180 5180 IRE_REFRELE(lso_dst_ire);
5181 5181 return (error);
5182 5182 }
5183 5183
5184 5184 /*
5185 5185 * Insert the ire in b_cont. Returns false if it fails (due to lack of space).
5186 5186 * Prefers dst_ire over src_ire.
5187 5187 */
5188 5188 static boolean_t
5189 5189 ip_bind_insert_ire(mblk_t *mp, ire_t *ire, iulp_t *ulp_info, ip_stack_t *ipst)
5190 5190 {
5191 5191 mblk_t *mp1;
5192 5192 ire_t *ret_ire = NULL;
5193 5193
5194 5194 mp1 = mp->b_cont;
5195 5195 ASSERT(mp1 != NULL);
5196 5196
5197 5197 if (ire != NULL) {
5198 5198 /*
5199 5199 * mp1 initialized above to IRE_DB_REQ_TYPE
5200 5200 * appended mblk. Its <upper protocol>'s
5201 5201 * job to make sure there is room.
5202 5202 */
5203 5203 if ((mp1->b_datap->db_lim - mp1->b_rptr) < sizeof (ire_t))
5204 5204 return (0);
5205 5205
5206 5206 mp1->b_datap->db_type = IRE_DB_TYPE;
5207 5207 mp1->b_wptr = mp1->b_rptr + sizeof (ire_t);
5208 5208 bcopy(ire, mp1->b_rptr, sizeof (ire_t));
5209 5209 ret_ire = (ire_t *)mp1->b_rptr;
5210 5210 /*
5211 5211 * Pass the latest setting of the ip_path_mtu_discovery and
5212 5212 * copy the ulp info if any.
5213 5213 */
5214 5214 ret_ire->ire_frag_flag |= (ipst->ips_ip_path_mtu_discovery) ?
5215 5215 IPH_DF : 0;
5216 5216 if (ulp_info != NULL) {
5217 5217 bcopy(ulp_info, &(ret_ire->ire_uinfo),
5218 5218 sizeof (iulp_t));
5219 5219 }
5220 5220 ret_ire->ire_mp = mp1;
5221 5221 } else {
5222 5222 /*
5223 5223 * No IRE was found. Remove IRE mblk.
5224 5224 */
5225 5225 mp->b_cont = mp1->b_cont;
5226 5226 freeb(mp1);
5227 5227 }
5228 5228
5229 5229 return (1);
5230 5230 }
5231 5231
5232 5232 /*
5233 5233 * Carve "len" bytes out of an mblk chain, consuming any we empty, and duping
5234 5234 * the final piece where we don't. Return a pointer to the first mblk in the
5235 5235 * result, and update the pointer to the next mblk to chew on. If anything
5236 5236 * goes wrong (i.e., dupb fails), we waste everything in sight and return a
5237 5237 * NULL pointer.
5238 5238 */
5239 5239 mblk_t *
5240 5240 ip_carve_mp(mblk_t **mpp, ssize_t len)
5241 5241 {
5242 5242 mblk_t *mp0;
5243 5243 mblk_t *mp1;
5244 5244 mblk_t *mp2;
5245 5245
5246 5246 if (!len || !mpp || !(mp0 = *mpp))
5247 5247 return (NULL);
5248 5248 /* If we aren't going to consume the first mblk, we need a dup. */
5249 5249 if (mp0->b_wptr - mp0->b_rptr > len) {
5250 5250 mp1 = dupb(mp0);
5251 5251 if (mp1) {
5252 5252 /* Partition the data between the two mblks. */
5253 5253 mp1->b_wptr = mp1->b_rptr + len;
5254 5254 mp0->b_rptr = mp1->b_wptr;
5255 5255 /*
5256 5256 * after adjustments if mblk not consumed is now
5257 5257 * unaligned, try to align it. If this fails free
5258 5258 * all messages and let upper layer recover.
5259 5259 */
5260 5260 if (!OK_32PTR(mp0->b_rptr)) {
5261 5261 if (!pullupmsg(mp0, -1)) {
5262 5262 freemsg(mp0);
5263 5263 freemsg(mp1);
5264 5264 *mpp = NULL;
5265 5265 return (NULL);
5266 5266 }
5267 5267 }
5268 5268 }
5269 5269 return (mp1);
5270 5270 }
5271 5271 /* Eat through as many mblks as we need to get len bytes. */
5272 5272 len -= mp0->b_wptr - mp0->b_rptr;
5273 5273 for (mp2 = mp1 = mp0; (mp2 = mp2->b_cont) != 0 && len; mp1 = mp2) {
5274 5274 if (mp2->b_wptr - mp2->b_rptr > len) {
5275 5275 /*
5276 5276 * We won't consume the entire last mblk. Like
5277 5277 * above, dup and partition it.
5278 5278 */
5279 5279 mp1->b_cont = dupb(mp2);
5280 5280 mp1 = mp1->b_cont;
5281 5281 if (!mp1) {
5282 5282 /*
5283 5283 * Trouble. Rather than go to a lot of
5284 5284 * trouble to clean up, we free the messages.
5285 5285 * This won't be any worse than losing it on
5286 5286 * the wire.
5287 5287 */
5288 5288 freemsg(mp0);
5289 5289 freemsg(mp2);
5290 5290 *mpp = NULL;
5291 5291 return (NULL);
5292 5292 }
5293 5293 mp1->b_wptr = mp1->b_rptr + len;
5294 5294 mp2->b_rptr = mp1->b_wptr;
5295 5295 /*
5296 5296 * after adjustments if mblk not consumed is now
5297 5297 * unaligned, try to align it. If this fails free
5298 5298 * all messages and let upper layer recover.
5299 5299 */
5300 5300 if (!OK_32PTR(mp2->b_rptr)) {
5301 5301 if (!pullupmsg(mp2, -1)) {
5302 5302 freemsg(mp0);
5303 5303 freemsg(mp2);
5304 5304 *mpp = NULL;
5305 5305 return (NULL);
5306 5306 }
5307 5307 }
5308 5308 *mpp = mp2;
5309 5309 return (mp0);
5310 5310 }
5311 5311 /* Decrement len by the amount we just got. */
5312 5312 len -= mp2->b_wptr - mp2->b_rptr;
5313 5313 }
5314 5314 /*
5315 5315 * len should be reduced to zero now. If not our caller has
5316 5316 * screwed up.
5317 5317 */
5318 5318 if (len) {
5319 5319 /* Shouldn't happen! */
5320 5320 freemsg(mp0);
5321 5321 *mpp = NULL;
5322 5322 return (NULL);
5323 5323 }
5324 5324 /*
5325 5325 * We consumed up to exactly the end of an mblk. Detach the part
5326 5326 * we are returning from the rest of the chain.
5327 5327 */
5328 5328 mp1->b_cont = NULL;
5329 5329 *mpp = mp2;
5330 5330 return (mp0);
5331 5331 }
5332 5332
5333 5333 /* The ill stream is being unplumbed. Called from ip_close */
5334 5334 int
5335 5335 ip_modclose(ill_t *ill)
5336 5336 {
5337 5337 boolean_t success;
5338 5338 ipsq_t *ipsq;
5339 5339 ipif_t *ipif;
5340 5340 queue_t *q = ill->ill_rq;
5341 5341 ip_stack_t *ipst = ill->ill_ipst;
5342 5342 clock_t timeout;
5343 5343
5344 5344 /*
5345 5345 * Wait for the ACKs of all deferred control messages to be processed.
5346 5346 * In particular, we wait for a potential capability reset initiated
5347 5347 * in ip_sioctl_plink() to complete before proceeding.
5348 5348 *
5349 5349 * Note: we wait for at most ip_modclose_ackwait_ms (by default 3000 ms)
5350 5350 * in case the driver never replies.
5351 5351 */
5352 5352 timeout = lbolt + MSEC_TO_TICK(ip_modclose_ackwait_ms);
5353 5353 mutex_enter(&ill->ill_lock);
5354 5354 while (ill->ill_dlpi_pending != DL_PRIM_INVAL) {
5355 5355 if (cv_timedwait(&ill->ill_cv, &ill->ill_lock, timeout) < 0) {
5356 5356 /* Timeout */
5357 5357 break;
5358 5358 }
5359 5359 }
5360 5360 mutex_exit(&ill->ill_lock);
5361 5361
5362 5362 /*
5363 5363 * Forcibly enter the ipsq after some delay. This is to take
5364 5364 * care of the case when some ioctl does not complete because
5365 5365 * we sent a control message to the driver and it did not
5366 5366 * send us a reply. We want to be able to at least unplumb
5367 5367 * and replumb rather than force the user to reboot the system.
5368 5368 */
5369 5369 success = ipsq_enter(ill, B_FALSE);
5370 5370
5371 5371 /*
5372 5372 * Open/close/push/pop is guaranteed to be single threaded
5373 5373 * per stream by STREAMS. FS guarantees that all references
5374 5374 * from top are gone before close is called. So there can't
5375 5375 * be another close thread that has set CONDEMNED on this ill.
5376 5376 * and cause ipsq_enter to return failure.
5377 5377 */
5378 5378 ASSERT(success);
5379 5379 ipsq = ill->ill_phyint->phyint_ipsq;
5380 5380
5381 5381 /*
5382 5382 * Mark it condemned. No new reference will be made to this ill.
5383 5383 * Lookup functions will return an error. Threads that try to
5384 5384 * increment the refcnt must check for ILL_CAN_LOOKUP. This ensures
5385 5385 * that the refcnt will drop down to zero.
5386 5386 */
5387 5387 mutex_enter(&ill->ill_lock);
5388 5388 ill->ill_state_flags |= ILL_CONDEMNED;
5389 5389 for (ipif = ill->ill_ipif; ipif != NULL;
5390 5390 ipif = ipif->ipif_next) {
5391 5391 ipif->ipif_state_flags |= IPIF_CONDEMNED;
5392 5392 }
5393 5393 /*
5394 5394 * Wake up anybody waiting to enter the ipsq. ipsq_enter
5395 5395 * returns error if ILL_CONDEMNED is set
5396 5396 */
5397 5397 cv_broadcast(&ill->ill_cv);
5398 5398 mutex_exit(&ill->ill_lock);
5399 5399
5400 5400 /*
5401 5401 * Send all the deferred DLPI messages downstream which came in
5402 5402 * during the small window right before ipsq_enter(). We do this
5403 5403 * without waiting for the ACKs because all the ACKs for M_PROTO
5404 5404 * messages are ignored in ip_rput() when ILL_CONDEMNED is set.
5405 5405 */
5406 5406 ill_dlpi_send_deferred(ill);
5407 5407
5408 5408 /*
5409 5409 * Shut down fragmentation reassembly.
5410 5410 * ill_frag_timer won't start a timer again.
5411 5411 * Now cancel any existing timer
5412 5412 */
5413 5413 (void) untimeout(ill->ill_frag_timer_id);
5414 5414 (void) ill_frag_timeout(ill, 0);
5415 5415
5416 5416 /*
5417 5417 * If MOVE was in progress, clear the
5418 5418 * move_in_progress fields also.
5419 5419 */
5420 5420 if (ill->ill_move_in_progress) {
5421 5421 ILL_CLEAR_MOVE(ill);
5422 5422 }
5423 5423
5424 5424 /*
5425 5425 * Call ill_delete to bring down the ipifs, ilms and ill on
5426 5426 * this ill. Then wait for the refcnts to drop to zero.
5427 5427 * ill_is_freeable checks whether the ill is really quiescent.
5428 5428 * Then make sure that threads that are waiting to enter the
5429 5429 * ipsq have seen the error returned by ipsq_enter and have
5430 5430 * gone away. Then we call ill_delete_tail which does the
5431 5431 * DL_UNBIND_REQ with the driver and then qprocsoff.
5432 5432 */
5433 5433 ill_delete(ill);
5434 5434 mutex_enter(&ill->ill_lock);
5435 5435 while (!ill_is_freeable(ill))
5436 5436 cv_wait(&ill->ill_cv, &ill->ill_lock);
5437 5437 while (ill->ill_waiters)
5438 5438 cv_wait(&ill->ill_cv, &ill->ill_lock);
5439 5439
5440 5440 mutex_exit(&ill->ill_lock);
5441 5441
5442 5442 /*
5443 5443 * ill_delete_tail drops reference on ill_ipst, but we need to keep
5444 5444 * it held until the end of the function since the cleanup
5445 5445 * below needs to be able to use the ip_stack_t.
5446 5446 */
5447 5447 netstack_hold(ipst->ips_netstack);
5448 5448
5449 5449 /* qprocsoff is called in ill_delete_tail */
5450 5450 ill_delete_tail(ill);
5451 5451 ASSERT(ill->ill_ipst == NULL);
5452 5452
5453 5453 /*
5454 5454 * Walk through all upper (conn) streams and qenable
5455 5455 * those that have queued data.
5456 5456 * close synchronization needs this to
5457 5457 * be done to ensure that all upper layers blocked
5458 5458 * due to flow control to the closing device
5459 5459 * get unblocked.
5460 5460 */
5461 5461 ip1dbg(("ip_wsrv: walking\n"));
5462 5462 conn_walk_drain(ipst);
5463 5463
5464 5464 mutex_enter(&ipst->ips_ip_mi_lock);
5465 5465 mi_close_unlink(&ipst->ips_ip_g_head, (IDP)ill);
5466 5466 mutex_exit(&ipst->ips_ip_mi_lock);
5467 5467
5468 5468 /*
5469 5469 * credp could be null if the open didn't succeed and ip_modopen
5470 5470 * itself calls ip_close.
5471 5471 */
5472 5472 if (ill->ill_credp != NULL)
5473 5473 crfree(ill->ill_credp);
5474 5474
5475 5475 mutex_enter(&ill->ill_lock);
5476 5476 ill_nic_info_dispatch(ill);
5477 5477 mutex_exit(&ill->ill_lock);
5478 5478
5479 5479 /*
5480 5480 * Now we are done with the module close pieces that
5481 5481 * need the netstack_t.
5482 5482 */
5483 5483 netstack_rele(ipst->ips_netstack);
5484 5484
5485 5485 mi_close_free((IDP)ill);
5486 5486 q->q_ptr = WR(q)->q_ptr = NULL;
5487 5487
5488 5488 ipsq_exit(ipsq);
5489 5489
5490 5490 return (0);
5491 5491 }
5492 5492
5493 5493 /*
5494 5494 * This is called as part of close() for IP, UDP, ICMP, and RTS
5495 5495 * in order to quiesce the conn.
5496 5496 */
5497 5497 void
5498 5498 ip_quiesce_conn(conn_t *connp)
5499 5499 {
5500 5500 boolean_t drain_cleanup_reqd = B_FALSE;
5501 5501 boolean_t conn_ioctl_cleanup_reqd = B_FALSE;
5502 5502 boolean_t ilg_cleanup_reqd = B_FALSE;
5503 5503 ip_stack_t *ipst;
5504 5504
5505 5505 ASSERT(!IPCL_IS_TCP(connp));
5506 5506 ipst = connp->conn_netstack->netstack_ip;
5507 5507
5508 5508 /*
5509 5509 * Mark the conn as closing, and this conn must not be
5510 5510 * inserted in future into any list. Eg. conn_drain_insert(),
5511 5511 * won't insert this conn into the conn_drain_list.
5512 5512 * Similarly ill_pending_mp_add() will not add any mp to
5513 5513 * the pending mp list, after this conn has started closing.
5514 5514 *
5515 5515 * conn_idl, conn_pending_ill, conn_down_pending_ill, conn_ilg
5516 5516 * cannot get set henceforth.
5517 5517 */
5518 5518 mutex_enter(&connp->conn_lock);
5519 5519 ASSERT(!(connp->conn_state_flags & CONN_QUIESCED));
5520 5520 connp->conn_state_flags |= CONN_CLOSING;
5521 5521 if (connp->conn_idl != NULL)
5522 5522 drain_cleanup_reqd = B_TRUE;
5523 5523 if (connp->conn_oper_pending_ill != NULL)
5524 5524 conn_ioctl_cleanup_reqd = B_TRUE;
5525 5525 if (connp->conn_dhcpinit_ill != NULL) {
5526 5526 ASSERT(connp->conn_dhcpinit_ill->ill_dhcpinit != 0);
5527 5527 atomic_dec_32(&connp->conn_dhcpinit_ill->ill_dhcpinit);
5528 5528 connp->conn_dhcpinit_ill = NULL;
5529 5529 }
5530 5530 if (connp->conn_ilg_inuse != 0)
5531 5531 ilg_cleanup_reqd = B_TRUE;
5532 5532 mutex_exit(&connp->conn_lock);
5533 5533
5534 5534 if (conn_ioctl_cleanup_reqd)
5535 5535 conn_ioctl_cleanup(connp);
5536 5536
5537 5537 if (is_system_labeled() && connp->conn_anon_port) {
5538 5538 (void) tsol_mlp_anon(crgetzone(connp->conn_cred),
5539 5539 connp->conn_mlp_type, connp->conn_ulp,
5540 5540 ntohs(connp->conn_lport), B_FALSE);
5541 5541 connp->conn_anon_port = 0;
5542 5542 }
5543 5543 connp->conn_mlp_type = mlptSingle;
5544 5544
5545 5545 /*
5546 5546 * Remove this conn from any fanout list it is on.
5547 5547 * and then wait for any threads currently operating
5548 5548 * on this endpoint to finish
5549 5549 */
5550 5550 ipcl_hash_remove(connp);
5551 5551
5552 5552 /*
5553 5553 * Remove this conn from the drain list, and do
5554 5554 * any other cleanup that may be required.
5555 5555 * (Only non-tcp streams may have a non-null conn_idl.
5556 5556 * TCP streams are never flow controlled, and
5557 5557 * conn_idl will be null)
5558 5558 */
5559 5559 if (drain_cleanup_reqd)
5560 5560 conn_drain_tail(connp, B_TRUE);
5561 5561
5562 5562 if (connp == ipst->ips_ip_g_mrouter)
5563 5563 (void) ip_mrouter_done(NULL, ipst);
5564 5564
5565 5565 if (ilg_cleanup_reqd)
5566 5566 ilg_delete_all(connp);
5567 5567
5568 5568 conn_delete_ire(connp, NULL);
5569 5569
5570 5570 /*
5571 5571 * Now conn refcnt can increase only thru CONN_INC_REF_LOCKED.
5572 5572 * callers from write side can't be there now because close
5573 5573 * is in progress. The only other caller is ipcl_walk
5574 5574 * which checks for the condemned flag.
5575 5575 */
5576 5576 mutex_enter(&connp->conn_lock);
5577 5577 connp->conn_state_flags |= CONN_CONDEMNED;
5578 5578 while (connp->conn_ref != 1)
5579 5579 cv_wait(&connp->conn_cv, &connp->conn_lock);
5580 5580 connp->conn_state_flags |= CONN_QUIESCED;
5581 5581 mutex_exit(&connp->conn_lock);
5582 5582 }
5583 5583
5584 5584 /* ARGSUSED */
5585 5585 int
5586 5586 ip_close(queue_t *q, int flags)
5587 5587 {
5588 5588 conn_t *connp;
5589 5589
5590 5590 TRACE_1(TR_FAC_IP, TR_IP_CLOSE, "ip_close: q %p", q);
5591 5591
5592 5592 /*
5593 5593 * Call the appropriate delete routine depending on whether this is
5594 5594 * a module or device.
5595 5595 */
5596 5596 if (WR(q)->q_next != NULL) {
5597 5597 /* This is a module close */
5598 5598 return (ip_modclose((ill_t *)q->q_ptr));
5599 5599 }
5600 5600
5601 5601 connp = q->q_ptr;
5602 5602 ip_quiesce_conn(connp);
5603 5603
5604 5604 qprocsoff(q);
5605 5605
5606 5606 /*
5607 5607 * Now we are truly single threaded on this stream, and can
5608 5608 * delete the things hanging off the connp, and finally the connp.
5609 5609 * We removed this connp from the fanout list, it cannot be
5610 5610 * accessed thru the fanouts, and we already waited for the
5611 5611 * conn_ref to drop to 0. We are already in close, so
5612 5612 * there cannot be any other thread from the top. qprocsoff
5613 5613 * has completed, and service has completed or won't run in
5614 5614 * future.
5615 5615 */
5616 5616 ASSERT(connp->conn_ref == 1);
5617 5617
5618 5618 inet_minor_free(connp->conn_minor_arena, connp->conn_dev);
5619 5619
5620 5620 connp->conn_ref--;
5621 5621 ipcl_conn_destroy(connp);
5622 5622
5623 5623 q->q_ptr = WR(q)->q_ptr = NULL;
5624 5624 return (0);
5625 5625 }
5626 5626
5627 5627 /*
5628 5628 * Wapper around putnext() so that ip_rts_request can merely use
5629 5629 * conn_recv.
5630 5630 */
5631 5631 /*ARGSUSED2*/
5632 5632 static void
5633 5633 ip_conn_input(void *arg1, mblk_t *mp, void *arg2)
5634 5634 {
5635 5635 conn_t *connp = (conn_t *)arg1;
5636 5636
5637 5637 putnext(connp->conn_rq, mp);
5638 5638 }
5639 5639
5640 5640 /* Return the IP checksum for the IP header at "iph". */
5641 5641 uint16_t
5642 5642 ip_csum_hdr(ipha_t *ipha)
5643 5643 {
5644 5644 uint16_t *uph;
5645 5645 uint32_t sum;
5646 5646 int opt_len;
5647 5647
5648 5648 opt_len = (ipha->ipha_version_and_hdr_length & 0xF) -
5649 5649 IP_SIMPLE_HDR_LENGTH_IN_WORDS;
5650 5650 uph = (uint16_t *)ipha;
5651 5651 sum = uph[0] + uph[1] + uph[2] + uph[3] + uph[4] +
5652 5652 uph[5] + uph[6] + uph[7] + uph[8] + uph[9];
5653 5653 if (opt_len > 0) {
5654 5654 do {
5655 5655 sum += uph[10];
5656 5656 sum += uph[11];
5657 5657 uph += 2;
5658 5658 } while (--opt_len);
5659 5659 }
5660 5660 sum = (sum & 0xFFFF) + (sum >> 16);
5661 5661 sum = ~(sum + (sum >> 16)) & 0xFFFF;
5662 5662 if (sum == 0xffff)
5663 5663 sum = 0;
5664 5664 return ((uint16_t)sum);
5665 5665 }
5666 5666
5667 5667 /*
5668 5668 * Called when the module is about to be unloaded
5669 5669 */
5670 5670 void
5671 5671 ip_ddi_destroy(void)
5672 5672 {
5673 5673 tnet_fini();
5674 5674
5675 5675 icmp_ddi_destroy();
5676 5676 rts_ddi_destroy();
5677 5677 udp_ddi_destroy();
5678 5678 sctp_ddi_g_destroy();
5679 5679 tcp_ddi_g_destroy();
5680 5680 ipsec_policy_g_destroy();
5681 5681 ipcl_g_destroy();
5682 5682 ip_net_g_destroy();
5683 5683 ip_ire_g_fini();
5684 5684 inet_minor_destroy(ip_minor_arena_sa);
5685 5685 #if defined(_LP64)
5686 5686 inet_minor_destroy(ip_minor_arena_la);
5687 5687 #endif
5688 5688
5689 5689 #ifdef DEBUG
5690 5690 list_destroy(&ip_thread_list);
5691 5691 rw_destroy(&ip_thread_rwlock);
5692 5692 tsd_destroy(&ip_thread_data);
5693 5693 #endif
5694 5694
5695 5695 netstack_unregister(NS_IP);
5696 5696 }
5697 5697
5698 5698 /*
5699 5699 * First step in cleanup.
5700 5700 */
5701 5701 /* ARGSUSED */
5702 5702 static void
5703 5703 ip_stack_shutdown(netstackid_t stackid, void *arg)
5704 5704 {
5705 5705 ip_stack_t *ipst = (ip_stack_t *)arg;
5706 5706
5707 5707 #ifdef NS_DEBUG
5708 5708 printf("ip_stack_shutdown(%p, stack %d)\n", (void *)ipst, stackid);
5709 5709 #endif
5710 5710
5711 5711 /* Get rid of loopback interfaces and their IREs */
5712 5712 ip_loopback_cleanup(ipst);
5713 5713 }
5714 5714
5715 5715 /*
5716 5716 * Free the IP stack instance.
5717 5717 */
5718 5718 static void
5719 5719 ip_stack_fini(netstackid_t stackid, void *arg)
5720 5720 {
5721 5721 ip_stack_t *ipst = (ip_stack_t *)arg;
5722 5722 int ret;
5723 5723
5724 5724 #ifdef NS_DEBUG
5725 5725 printf("ip_stack_fini(%p, stack %d)\n", (void *)ipst, stackid);
5726 5726 #endif
5727 5727 ipv4_hook_destroy(ipst);
5728 5728 ipv6_hook_destroy(ipst);
5729 5729 ip_net_destroy(ipst);
5730 5730
5731 5731 rw_destroy(&ipst->ips_srcid_lock);
5732 5732
5733 5733 ip_kstat_fini(stackid, ipst->ips_ip_mibkp);
5734 5734 ipst->ips_ip_mibkp = NULL;
5735 5735 icmp_kstat_fini(stackid, ipst->ips_icmp_mibkp);
5736 5736 ipst->ips_icmp_mibkp = NULL;
5737 5737 ip_kstat2_fini(stackid, ipst->ips_ip_kstat);
5738 5738 ipst->ips_ip_kstat = NULL;
5739 5739 bzero(&ipst->ips_ip_statistics, sizeof (ipst->ips_ip_statistics));
5740 5740 ip6_kstat_fini(stackid, ipst->ips_ip6_kstat);
5741 5741 ipst->ips_ip6_kstat = NULL;
5742 5742 bzero(&ipst->ips_ip6_statistics, sizeof (ipst->ips_ip6_statistics));
5743 5743
5744 5744 nd_free(&ipst->ips_ip_g_nd);
5745 5745 kmem_free(ipst->ips_param_arr, sizeof (lcl_param_arr));
5746 5746 ipst->ips_param_arr = NULL;
5747 5747 kmem_free(ipst->ips_ndp_arr, sizeof (lcl_ndp_arr));
5748 5748 ipst->ips_ndp_arr = NULL;
5749 5749
5750 5750 ip_mrouter_stack_destroy(ipst);
5751 5751
5752 5752 mutex_destroy(&ipst->ips_ip_mi_lock);
5753 5753 rw_destroy(&ipst->ips_ipsec_capab_ills_lock);
5754 5754 rw_destroy(&ipst->ips_ill_g_usesrc_lock);
5755 5755 rw_destroy(&ipst->ips_ip_g_nd_lock);
5756 5756
5757 5757 ret = untimeout(ipst->ips_igmp_timeout_id);
5758 5758 if (ret == -1) {
5759 5759 ASSERT(ipst->ips_igmp_timeout_id == 0);
5760 5760 } else {
5761 5761 ASSERT(ipst->ips_igmp_timeout_id != 0);
5762 5762 ipst->ips_igmp_timeout_id = 0;
5763 5763 }
5764 5764 ret = untimeout(ipst->ips_igmp_slowtimeout_id);
5765 5765 if (ret == -1) {
5766 5766 ASSERT(ipst->ips_igmp_slowtimeout_id == 0);
5767 5767 } else {
5768 5768 ASSERT(ipst->ips_igmp_slowtimeout_id != 0);
5769 5769 ipst->ips_igmp_slowtimeout_id = 0;
5770 5770 }
5771 5771 ret = untimeout(ipst->ips_mld_timeout_id);
5772 5772 if (ret == -1) {
5773 5773 ASSERT(ipst->ips_mld_timeout_id == 0);
5774 5774 } else {
5775 5775 ASSERT(ipst->ips_mld_timeout_id != 0);
5776 5776 ipst->ips_mld_timeout_id = 0;
5777 5777 }
5778 5778 ret = untimeout(ipst->ips_mld_slowtimeout_id);
5779 5779 if (ret == -1) {
5780 5780 ASSERT(ipst->ips_mld_slowtimeout_id == 0);
5781 5781 } else {
5782 5782 ASSERT(ipst->ips_mld_slowtimeout_id != 0);
5783 5783 ipst->ips_mld_slowtimeout_id = 0;
5784 5784 }
5785 5785 ret = untimeout(ipst->ips_ip_ire_expire_id);
5786 5786 if (ret == -1) {
5787 5787 ASSERT(ipst->ips_ip_ire_expire_id == 0);
5788 5788 } else {
5789 5789 ASSERT(ipst->ips_ip_ire_expire_id != 0);
5790 5790 ipst->ips_ip_ire_expire_id = 0;
5791 5791 }
5792 5792
5793 5793 mutex_destroy(&ipst->ips_igmp_timer_lock);
5794 5794 mutex_destroy(&ipst->ips_mld_timer_lock);
5795 5795 mutex_destroy(&ipst->ips_igmp_slowtimeout_lock);
5796 5796 mutex_destroy(&ipst->ips_mld_slowtimeout_lock);
5797 5797 mutex_destroy(&ipst->ips_ip_addr_avail_lock);
5798 5798 rw_destroy(&ipst->ips_ill_g_lock);
5799 5799
5800 5800 ip_ire_fini(ipst);
5801 5801 ip6_asp_free(ipst);
5802 5802 conn_drain_fini(ipst);
5803 5803 ipcl_destroy(ipst);
5804 5804
5805 5805 mutex_destroy(&ipst->ips_ndp4->ndp_g_lock);
5806 5806 mutex_destroy(&ipst->ips_ndp6->ndp_g_lock);
5807 5807 kmem_free(ipst->ips_ndp4, sizeof (ndp_g_t));
5808 5808 ipst->ips_ndp4 = NULL;
5809 5809 kmem_free(ipst->ips_ndp6, sizeof (ndp_g_t));
5810 5810 ipst->ips_ndp6 = NULL;
5811 5811
5812 5812 if (ipst->ips_loopback_ksp != NULL) {
5813 5813 kstat_delete_netstack(ipst->ips_loopback_ksp, stackid);
5814 5814 ipst->ips_loopback_ksp = NULL;
5815 5815 }
5816 5816
5817 5817 kmem_free(ipst->ips_phyint_g_list, sizeof (phyint_list_t));
5818 5818 ipst->ips_phyint_g_list = NULL;
5819 5819 kmem_free(ipst->ips_ill_g_heads, sizeof (ill_g_head_t) * MAX_G_HEADS);
5820 5820 ipst->ips_ill_g_heads = NULL;
5821 5821
5822 5822 kmem_free(ipst, sizeof (*ipst));
5823 5823 }
5824 5824
5825 5825 /*
5826 5826 * This function is called from the TSD destructor, and is used to debug
5827 5827 * reference count issues in IP. See block comment in <inet/ip_if.h> for
5828 5828 * details.
5829 5829 */
5830 5830 static void
5831 5831 ip_thread_exit(void *phash)
5832 5832 {
5833 5833 th_hash_t *thh = phash;
5834 5834
5835 5835 rw_enter(&ip_thread_rwlock, RW_WRITER);
5836 5836 list_remove(&ip_thread_list, thh);
5837 5837 rw_exit(&ip_thread_rwlock);
5838 5838 mod_hash_destroy_hash(thh->thh_hash);
5839 5839 kmem_free(thh, sizeof (*thh));
5840 5840 }
5841 5841
5842 5842 /*
5843 5843 * Called when the IP kernel module is loaded into the kernel
5844 5844 */
5845 5845 void
5846 5846 ip_ddi_init(void)
5847 5847 {
5848 5848 ip_input_proc = ip_squeue_switch(ip_squeue_enter);
5849 5849
5850 5850 /*
5851 5851 * For IP and TCP the minor numbers should start from 2 since we have 4
5852 5852 * initial devices: ip, ip6, tcp, tcp6.
5853 5853 */
5854 5854 /*
5855 5855 * If this is a 64-bit kernel, then create two separate arenas -
5856 5856 * one for TLIs in the range of INET_MIN_DEV+2 through 2^^18-1, and the
5857 5857 * other for socket apps in the range 2^^18 through 2^^32-1.
5858 5858 */
5859 5859 ip_minor_arena_la = NULL;
5860 5860 ip_minor_arena_sa = NULL;
5861 5861 #if defined(_LP64)
5862 5862 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa",
5863 5863 INET_MIN_DEV + 2, MAXMIN32, KM_SLEEP)) == NULL) {
5864 5864 cmn_err(CE_PANIC,
5865 5865 "ip_ddi_init: ip_minor_arena_sa creation failed\n");
5866 5866 }
5867 5867 if ((ip_minor_arena_la = inet_minor_create("ip_minor_arena_la",
5868 5868 MAXMIN32 + 1, MAXMIN64, KM_SLEEP)) == NULL) {
5869 5869 cmn_err(CE_PANIC,
5870 5870 "ip_ddi_init: ip_minor_arena_la creation failed\n");
5871 5871 }
5872 5872 #else
5873 5873 if ((ip_minor_arena_sa = inet_minor_create("ip_minor_arena_sa",
5874 5874 INET_MIN_DEV + 2, MAXMIN, KM_SLEEP)) == NULL) {
5875 5875 cmn_err(CE_PANIC,
5876 5876 "ip_ddi_init: ip_minor_arena_sa creation failed\n");
5877 5877 }
5878 5878 #endif
5879 5879 ip_poll_normal_ticks = MSEC_TO_TICK_ROUNDUP(ip_poll_normal_ms);
5880 5880
5881 5881 ipcl_g_init();
5882 5882 ip_ire_g_init();
5883 5883 ip_net_g_init();
5884 5884
5885 5885 #ifdef DEBUG
5886 5886 tsd_create(&ip_thread_data, ip_thread_exit);
5887 5887 rw_init(&ip_thread_rwlock, NULL, RW_DEFAULT, NULL);
5888 5888 list_create(&ip_thread_list, sizeof (th_hash_t),
5889 5889 offsetof(th_hash_t, thh_link));
5890 5890 #endif
5891 5891
5892 5892 /*
5893 5893 * We want to be informed each time a stack is created or
5894 5894 * destroyed in the kernel, so we can maintain the
5895 5895 * set of udp_stack_t's.
5896 5896 */
5897 5897 netstack_register(NS_IP, ip_stack_init, ip_stack_shutdown,
5898 5898 ip_stack_fini);
5899 5899
5900 5900 ipsec_policy_g_init();
5901 5901 tcp_ddi_g_init();
5902 5902 sctp_ddi_g_init();
5903 5903
5904 5904 tnet_init();
5905 5905
5906 5906 udp_ddi_init();
5907 5907 rts_ddi_init();
5908 5908 icmp_ddi_init();
5909 5909 }
5910 5910
5911 5911 /*
5912 5912 * Initialize the IP stack instance.
5913 5913 */
5914 5914 static void *
5915 5915 ip_stack_init(netstackid_t stackid, netstack_t *ns)
5916 5916 {
5917 5917 ip_stack_t *ipst;
5918 5918 ipparam_t *pa;
5919 5919 ipndp_t *na;
5920 5920
5921 5921 #ifdef NS_DEBUG
5922 5922 printf("ip_stack_init(stack %d)\n", stackid);
5923 5923 #endif
5924 5924
5925 5925 ipst = (ip_stack_t *)kmem_zalloc(sizeof (*ipst), KM_SLEEP);
5926 5926 ipst->ips_netstack = ns;
5927 5927
5928 5928 ipst->ips_ill_g_heads = kmem_zalloc(sizeof (ill_g_head_t) * MAX_G_HEADS,
5929 5929 KM_SLEEP);
5930 5930 ipst->ips_phyint_g_list = kmem_zalloc(sizeof (phyint_list_t),
5931 5931 KM_SLEEP);
5932 5932 ipst->ips_ndp4 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP);
5933 5933 ipst->ips_ndp6 = kmem_zalloc(sizeof (ndp_g_t), KM_SLEEP);
5934 5934 mutex_init(&ipst->ips_ndp4->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL);
5935 5935 mutex_init(&ipst->ips_ndp6->ndp_g_lock, NULL, MUTEX_DEFAULT, NULL);
5936 5936
5937 5937 rw_init(&ipst->ips_ip_g_nd_lock, NULL, RW_DEFAULT, NULL);
5938 5938 mutex_init(&ipst->ips_igmp_timer_lock, NULL, MUTEX_DEFAULT, NULL);
5939 5939 ipst->ips_igmp_deferred_next = INFINITY;
5940 5940 mutex_init(&ipst->ips_mld_timer_lock, NULL, MUTEX_DEFAULT, NULL);
5941 5941 ipst->ips_mld_deferred_next = INFINITY;
5942 5942 mutex_init(&ipst->ips_igmp_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL);
5943 5943 mutex_init(&ipst->ips_mld_slowtimeout_lock, NULL, MUTEX_DEFAULT, NULL);
5944 5944 mutex_init(&ipst->ips_ip_mi_lock, NULL, MUTEX_DEFAULT, NULL);
5945 5945 mutex_init(&ipst->ips_ip_addr_avail_lock, NULL, MUTEX_DEFAULT, NULL);
5946 5946 rw_init(&ipst->ips_ill_g_lock, NULL, RW_DEFAULT, NULL);
5947 5947 rw_init(&ipst->ips_ipsec_capab_ills_lock, NULL, RW_DEFAULT, NULL);
5948 5948 rw_init(&ipst->ips_ill_g_usesrc_lock, NULL, RW_DEFAULT, NULL);
5949 5949
5950 5950 ipcl_init(ipst);
5951 5951 ip_ire_init(ipst);
5952 5952 ip6_asp_init(ipst);
5953 5953 ipif_init(ipst);
5954 5954 conn_drain_init(ipst);
5955 5955 ip_mrouter_stack_init(ipst);
5956 5956
5957 5957 ipst->ips_ip_g_frag_timeout = IP_FRAG_TIMEOUT;
5958 5958 ipst->ips_ip_g_frag_timo_ms = IP_FRAG_TIMEOUT * 1000;
5959 5959
5960 5960 ipst->ips_ip_multirt_log_interval = 1000;
5961 5961
5962 5962 ipst->ips_ip_g_forward = IP_FORWARD_DEFAULT;
5963 5963 ipst->ips_ipv6_forward = IP_FORWARD_DEFAULT;
5964 5964 ipst->ips_ill_index = 1;
5965 5965
5966 5966 ipst->ips_saved_ip_g_forward = -1;
5967 5967 ipst->ips_reg_vif_num = ALL_VIFS; /* Index to Register vif */
5968 5968
5969 5969 pa = (ipparam_t *)kmem_alloc(sizeof (lcl_param_arr), KM_SLEEP);
5970 5970 ipst->ips_param_arr = pa;
5971 5971 bcopy(lcl_param_arr, ipst->ips_param_arr, sizeof (lcl_param_arr));
5972 5972
5973 5973 na = (ipndp_t *)kmem_alloc(sizeof (lcl_ndp_arr), KM_SLEEP);
5974 5974 ipst->ips_ndp_arr = na;
5975 5975 bcopy(lcl_ndp_arr, ipst->ips_ndp_arr, sizeof (lcl_ndp_arr));
5976 5976 ipst->ips_ndp_arr[IPNDP_IP_FORWARDING_OFFSET].ip_ndp_data =
5977 5977 (caddr_t)&ipst->ips_ip_g_forward;
5978 5978 ipst->ips_ndp_arr[IPNDP_IP6_FORWARDING_OFFSET].ip_ndp_data =
5979 5979 (caddr_t)&ipst->ips_ipv6_forward;
5980 5980 ASSERT(strcmp(ipst->ips_ndp_arr[IPNDP_CGTP_FILTER_OFFSET].ip_ndp_name,
5981 5981 "ip_cgtp_filter") == 0);
5982 5982 ipst->ips_ndp_arr[IPNDP_CGTP_FILTER_OFFSET].ip_ndp_data =
5983 5983 (caddr_t)&ipst->ips_ip_cgtp_filter;
5984 5984 ASSERT(strcmp(ipst->ips_ndp_arr[IPNDP_IPMP_HOOK_OFFSET].ip_ndp_name,
5985 5985 "ipmp_hook_emulation") == 0);
5986 5986 ipst->ips_ndp_arr[IPNDP_IPMP_HOOK_OFFSET].ip_ndp_data =
5987 5987 (caddr_t)&ipst->ips_ipmp_hook_emulation;
5988 5988
5989 5989 (void) ip_param_register(&ipst->ips_ip_g_nd,
5990 5990 ipst->ips_param_arr, A_CNT(lcl_param_arr),
5991 5991 ipst->ips_ndp_arr, A_CNT(lcl_ndp_arr));
5992 5992
5993 5993 ipst->ips_ip_mibkp = ip_kstat_init(stackid, ipst);
5994 5994 ipst->ips_icmp_mibkp = icmp_kstat_init(stackid);
5995 5995 ipst->ips_ip_kstat = ip_kstat2_init(stackid, &ipst->ips_ip_statistics);
5996 5996 ipst->ips_ip6_kstat =
5997 5997 ip6_kstat_init(stackid, &ipst->ips_ip6_statistics);
5998 5998
5999 5999 ipst->ips_ipmp_enable_failback = B_TRUE;
6000 6000
6001 6001 ipst->ips_ip_src_id = 1;
6002 6002 rw_init(&ipst->ips_srcid_lock, NULL, RW_DEFAULT, NULL);
6003 6003
6004 6004 ip_net_init(ipst, ns);
6005 6005 ipv4_hook_init(ipst);
6006 6006 ipv6_hook_init(ipst);
6007 6007
6008 6008 return (ipst);
6009 6009 }
6010 6010
6011 6011 /*
6012 6012 * Allocate and initialize a DLPI template of the specified length. (May be
6013 6013 * called as writer.)
6014 6014 */
6015 6015 mblk_t *
6016 6016 ip_dlpi_alloc(size_t len, t_uscalar_t prim)
6017 6017 {
6018 6018 mblk_t *mp;
6019 6019
6020 6020 mp = allocb(len, BPRI_MED);
6021 6021 if (!mp)
6022 6022 return (NULL);
6023 6023
6024 6024 /*
6025 6025 * DLPIv2 says that DL_INFO_REQ and DL_TOKEN_REQ (the latter
6026 6026 * of which we don't seem to use) are sent with M_PCPROTO, and
6027 6027 * that other DLPI are M_PROTO.
6028 6028 */
6029 6029 if (prim == DL_INFO_REQ) {
6030 6030 mp->b_datap->db_type = M_PCPROTO;
6031 6031 } else {
6032 6032 mp->b_datap->db_type = M_PROTO;
6033 6033 }
6034 6034
6035 6035 mp->b_wptr = mp->b_rptr + len;
6036 6036 bzero(mp->b_rptr, len);
6037 6037 ((dl_unitdata_req_t *)mp->b_rptr)->dl_primitive = prim;
6038 6038 return (mp);
6039 6039 }
6040 6040
6041 6041 /*
6042 6042 * Debug formatting routine. Returns a character string representation of the
6043 6043 * addr in buf, of the form xxx.xxx.xxx.xxx. This routine takes the address
6044 6044 * in the form of a ipaddr_t and calls ip_dot_saddr with a pointer.
6045 6045 *
6046 6046 * Once the ndd table-printing interfaces are removed, this can be changed to
6047 6047 * standard dotted-decimal form.
6048 6048 */
6049 6049 char *
6050 6050 ip_dot_addr(ipaddr_t addr, char *buf)
6051 6051 {
6052 6052 uint8_t *ap = (uint8_t *)&addr;
6053 6053
6054 6054 (void) mi_sprintf(buf, "%03d.%03d.%03d.%03d",
6055 6055 ap[0] & 0xFF, ap[1] & 0xFF, ap[2] & 0xFF, ap[3] & 0xFF);
6056 6056 return (buf);
6057 6057 }
6058 6058
6059 6059 /*
6060 6060 * Write the given MAC address as a printable string in the usual colon-
6061 6061 * separated format.
6062 6062 */
6063 6063 const char *
6064 6064 mac_colon_addr(const uint8_t *addr, size_t alen, char *buf, size_t buflen)
6065 6065 {
6066 6066 char *bp;
6067 6067
6068 6068 if (alen == 0 || buflen < 4)
6069 6069 return ("?");
6070 6070 bp = buf;
6071 6071 for (;;) {
6072 6072 /*
6073 6073 * If there are more MAC address bytes available, but we won't
6074 6074 * have any room to print them, then add "..." to the string
6075 6075 * instead. See below for the 'magic number' explanation.
6076 6076 */
6077 6077 if ((alen == 2 && buflen < 6) || (alen > 2 && buflen < 7)) {
6078 6078 (void) strcpy(bp, "...");
6079 6079 break;
6080 6080 }
6081 6081 (void) sprintf(bp, "%02x", *addr++);
6082 6082 bp += 2;
6083 6083 if (--alen == 0)
6084 6084 break;
6085 6085 *bp++ = ':';
6086 6086 buflen -= 3;
6087 6087 /*
6088 6088 * At this point, based on the first 'if' statement above,
6089 6089 * either alen == 1 and buflen >= 3, or alen > 1 and
6090 6090 * buflen >= 4. The first case leaves room for the final "xx"
6091 6091 * number and trailing NUL byte. The second leaves room for at
6092 6092 * least "...". Thus the apparently 'magic' numbers chosen for
6093 6093 * that statement.
6094 6094 */
6095 6095 }
6096 6096 return (buf);
6097 6097 }
6098 6098
6099 6099 /*
6100 6100 * Send an ICMP error after patching up the packet appropriately. Returns
6101 6101 * non-zero if the appropriate MIB should be bumped; zero otherwise.
6102 6102 */
6103 6103 static boolean_t
6104 6104 ip_fanout_send_icmp(queue_t *q, mblk_t *mp, uint_t flags,
6105 6105 uint_t icmp_type, uint_t icmp_code, boolean_t mctl_present,
6106 6106 zoneid_t zoneid, ip_stack_t *ipst)
6107 6107 {
6108 6108 ipha_t *ipha;
6109 6109 mblk_t *first_mp;
6110 6110 boolean_t secure;
6111 6111 unsigned char db_type;
6112 6112 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
6113 6113
6114 6114 first_mp = mp;
6115 6115 if (mctl_present) {
6116 6116 mp = mp->b_cont;
6117 6117 secure = ipsec_in_is_secure(first_mp);
6118 6118 ASSERT(mp != NULL);
6119 6119 } else {
6120 6120 /*
6121 6121 * If this is an ICMP error being reported - which goes
6122 6122 * up as M_CTLs, we need to convert them to M_DATA till
6123 6123 * we finish checking with global policy because
6124 6124 * ipsec_check_global_policy() assumes M_DATA as clear
6125 6125 * and M_CTL as secure.
6126 6126 */
6127 6127 db_type = DB_TYPE(mp);
6128 6128 DB_TYPE(mp) = M_DATA;
6129 6129 secure = B_FALSE;
6130 6130 }
6131 6131 /*
6132 6132 * We are generating an icmp error for some inbound packet.
6133 6133 * Called from all ip_fanout_(udp, tcp, proto) functions.
6134 6134 * Before we generate an error, check with global policy
6135 6135 * to see whether this is allowed to enter the system. As
6136 6136 * there is no "conn", we are checking with global policy.
6137 6137 */
6138 6138 ipha = (ipha_t *)mp->b_rptr;
6139 6139 if (secure || ipss->ipsec_inbound_v4_policy_present) {
6140 6140 first_mp = ipsec_check_global_policy(first_mp, NULL,
6141 6141 ipha, NULL, mctl_present, ipst->ips_netstack);
6142 6142 if (first_mp == NULL)
6143 6143 return (B_FALSE);
6144 6144 }
6145 6145
6146 6146 if (!mctl_present)
6147 6147 DB_TYPE(mp) = db_type;
6148 6148
6149 6149 if (flags & IP_FF_SEND_ICMP) {
6150 6150 if (flags & IP_FF_HDR_COMPLETE) {
6151 6151 if (ip_hdr_complete(ipha, zoneid, ipst)) {
6152 6152 freemsg(first_mp);
6153 6153 return (B_TRUE);
6154 6154 }
6155 6155 }
6156 6156 if (flags & IP_FF_CKSUM) {
6157 6157 /*
6158 6158 * Have to correct checksum since
6159 6159 * the packet might have been
6160 6160 * fragmented and the reassembly code in ip_rput
6161 6161 * does not restore the IP checksum.
6162 6162 */
6163 6163 ipha->ipha_hdr_checksum = 0;
6164 6164 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
6165 6165 }
6166 6166 switch (icmp_type) {
6167 6167 case ICMP_DEST_UNREACHABLE:
6168 6168 icmp_unreachable(WR(q), first_mp, icmp_code, zoneid,
6169 6169 ipst);
6170 6170 break;
6171 6171 default:
6172 6172 freemsg(first_mp);
6173 6173 break;
6174 6174 }
6175 6175 } else {
6176 6176 freemsg(first_mp);
6177 6177 return (B_FALSE);
6178 6178 }
6179 6179
6180 6180 return (B_TRUE);
6181 6181 }
6182 6182
6183 6183 /*
6184 6184 * Used to send an ICMP error message when a packet is received for
6185 6185 * a protocol that is not supported. The mblk passed as argument
6186 6186 * is consumed by this function.
6187 6187 */
6188 6188 void
6189 6189 ip_proto_not_sup(queue_t *q, mblk_t *ipsec_mp, uint_t flags, zoneid_t zoneid,
6190 6190 ip_stack_t *ipst)
6191 6191 {
6192 6192 mblk_t *mp;
6193 6193 ipha_t *ipha;
6194 6194 ill_t *ill;
6195 6195 ipsec_in_t *ii;
6196 6196
6197 6197 ii = (ipsec_in_t *)ipsec_mp->b_rptr;
6198 6198 ASSERT(ii->ipsec_in_type == IPSEC_IN);
6199 6199
6200 6200 mp = ipsec_mp->b_cont;
6201 6201 ipsec_mp->b_cont = NULL;
6202 6202 ipha = (ipha_t *)mp->b_rptr;
6203 6203 /* Get ill from index in ipsec_in_t. */
6204 6204 ill = ill_lookup_on_ifindex(ii->ipsec_in_ill_index,
6205 6205 (IPH_HDR_VERSION(ipha) == IPV6_VERSION), NULL, NULL, NULL, NULL,
6206 6206 ipst);
6207 6207 if (ill != NULL) {
6208 6208 if (IPH_HDR_VERSION(ipha) == IP_VERSION) {
6209 6209 if (ip_fanout_send_icmp(q, mp, flags,
6210 6210 ICMP_DEST_UNREACHABLE,
6211 6211 ICMP_PROTOCOL_UNREACHABLE, B_FALSE, zoneid, ipst)) {
6212 6212 BUMP_MIB(ill->ill_ip_mib,
6213 6213 ipIfStatsInUnknownProtos);
6214 6214 }
6215 6215 } else {
6216 6216 if (ip_fanout_send_icmp_v6(q, mp, flags,
6217 6217 ICMP6_PARAM_PROB, ICMP6_PARAMPROB_NEXTHEADER,
6218 6218 0, B_FALSE, zoneid, ipst)) {
6219 6219 BUMP_MIB(ill->ill_ip_mib,
6220 6220 ipIfStatsInUnknownProtos);
6221 6221 }
6222 6222 }
6223 6223 ill_refrele(ill);
6224 6224 } else { /* re-link for the freemsg() below. */
6225 6225 ipsec_mp->b_cont = mp;
6226 6226 }
6227 6227
6228 6228 /* If ICMP delivered, ipsec_mp will be a singleton (b_cont == NULL). */
6229 6229 freemsg(ipsec_mp);
6230 6230 }
6231 6231
6232 6232 /*
6233 6233 * See if the inbound datagram has had IPsec processing applied to it.
6234 6234 */
6235 6235 boolean_t
6236 6236 ipsec_in_is_secure(mblk_t *ipsec_mp)
6237 6237 {
6238 6238 ipsec_in_t *ii;
6239 6239
6240 6240 ii = (ipsec_in_t *)ipsec_mp->b_rptr;
6241 6241 ASSERT(ii->ipsec_in_type == IPSEC_IN);
6242 6242
6243 6243 if (ii->ipsec_in_loopback) {
6244 6244 return (ii->ipsec_in_secure);
6245 6245 } else {
6246 6246 return (ii->ipsec_in_ah_sa != NULL ||
6247 6247 ii->ipsec_in_esp_sa != NULL ||
6248 6248 ii->ipsec_in_decaps);
6249 6249 }
6250 6250 }
6251 6251
6252 6252 /*
6253 6253 * Handle protocols with which IP is less intimate. There
6254 6254 * can be more than one stream bound to a particular
6255 6255 * protocol. When this is the case, normally each one gets a copy
6256 6256 * of any incoming packets.
6257 6257 *
6258 6258 * IPsec NOTE :
6259 6259 *
6260 6260 * Don't allow a secure packet going up a non-secure connection.
6261 6261 * We don't allow this because
6262 6262 *
6263 6263 * 1) Reply might go out in clear which will be dropped at
6264 6264 * the sending side.
6265 6265 * 2) If the reply goes out in clear it will give the
6266 6266 * adversary enough information for getting the key in
6267 6267 * most of the cases.
6268 6268 *
6269 6269 * Moreover getting a secure packet when we expect clear
6270 6270 * implies that SA's were added without checking for
6271 6271 * policy on both ends. This should not happen once ISAKMP
6272 6272 * is used to negotiate SAs as SAs will be added only after
6273 6273 * verifying the policy.
6274 6274 *
6275 6275 * NOTE : If the packet was tunneled and not multicast we only send
6276 6276 * to it the first match. Unlike TCP and UDP fanouts this doesn't fall
6277 6277 * back to delivering packets to AF_INET6 raw sockets.
6278 6278 *
6279 6279 * IPQoS Notes:
6280 6280 * Once we have determined the client, invoke IPPF processing.
6281 6281 * Policy processing takes place only if the callout_position, IPP_LOCAL_IN,
6282 6282 * is enabled. If we get here from icmp_inbound_error_fanout or ip_wput_local
6283 6283 * ip_policy will be false.
6284 6284 *
6285 6285 * Zones notes:
6286 6286 * Currently only applications in the global zone can create raw sockets for
6287 6287 * protocols other than ICMP. So unlike the broadcast / multicast case of
6288 6288 * ip_fanout_udp(), we only send a copy of the packet to streams in the
6289 6289 * specified zone. For ICMP, this is handled by the callers of icmp_inbound().
6290 6290 */
6291 6291 static void
6292 6292 ip_fanout_proto(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha, uint_t flags,
6293 6293 boolean_t mctl_present, boolean_t ip_policy, ill_t *recv_ill,
6294 6294 zoneid_t zoneid)
6295 6295 {
6296 6296 queue_t *rq;
6297 6297 mblk_t *mp1, *first_mp1;
6298 6298 uint_t protocol = ipha->ipha_protocol;
6299 6299 ipaddr_t dst;
6300 6300 boolean_t one_only;
6301 6301 mblk_t *first_mp = mp;
6302 6302 boolean_t secure;
6303 6303 uint32_t ill_index;
6304 6304 conn_t *connp, *first_connp, *next_connp;
6305 6305 connf_t *connfp;
6306 6306 boolean_t shared_addr;
6307 6307 mib2_ipIfStatsEntry_t *mibptr;
6308 6308 ip_stack_t *ipst = recv_ill->ill_ipst;
6309 6309 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
6310 6310
6311 6311 mibptr = (ill != NULL) ? ill->ill_ip_mib : &ipst->ips_ip_mib;
6312 6312 if (mctl_present) {
6313 6313 mp = first_mp->b_cont;
6314 6314 secure = ipsec_in_is_secure(first_mp);
6315 6315 ASSERT(mp != NULL);
6316 6316 } else {
6317 6317 secure = B_FALSE;
6318 6318 }
6319 6319 dst = ipha->ipha_dst;
6320 6320 /*
6321 6321 * If the packet was tunneled and not multicast we only send to it
6322 6322 * the first match.
6323 6323 */
6324 6324 one_only = ((protocol == IPPROTO_ENCAP || protocol == IPPROTO_IPV6) &&
6325 6325 !CLASSD(dst));
6326 6326
6327 6327 shared_addr = (zoneid == ALL_ZONES);
6328 6328 if (shared_addr) {
6329 6329 /*
6330 6330 * We don't allow multilevel ports for raw IP, so no need to
6331 6331 * check for that here.
6332 6332 */
6333 6333 zoneid = tsol_packet_to_zoneid(mp);
6334 6334 }
6335 6335
6336 6336 connfp = &ipst->ips_ipcl_proto_fanout[protocol];
6337 6337 mutex_enter(&connfp->connf_lock);
6338 6338 connp = connfp->connf_head;
6339 6339 for (connp = connfp->connf_head; connp != NULL;
6340 6340 connp = connp->conn_next) {
6341 6341 if (IPCL_PROTO_MATCH(connp, protocol, ipha, ill, flags,
6342 6342 zoneid) &&
6343 6343 (!is_system_labeled() ||
6344 6344 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr,
6345 6345 connp))) {
6346 6346 break;
6347 6347 }
6348 6348 }
6349 6349
6350 6350 if (connp == NULL || connp->conn_upq == NULL) {
6351 6351 /*
6352 6352 * No one bound to these addresses. Is
6353 6353 * there a client that wants all
6354 6354 * unclaimed datagrams?
6355 6355 */
6356 6356 mutex_exit(&connfp->connf_lock);
6357 6357 /*
6358 6358 * Check for IPPROTO_ENCAP...
6359 6359 */
6360 6360 if (protocol == IPPROTO_ENCAP && ipst->ips_ip_g_mrouter) {
6361 6361 /*
6362 6362 * If an IPsec mblk is here on a multicast
6363 6363 * tunnel (using ip_mroute stuff), check policy here,
6364 6364 * THEN ship off to ip_mroute_decap().
6365 6365 *
6366 6366 * BTW, If I match a configured IP-in-IP
6367 6367 * tunnel, this path will not be reached, and
6368 6368 * ip_mroute_decap will never be called.
6369 6369 */
6370 6370 first_mp = ipsec_check_global_policy(first_mp, connp,
6371 6371 ipha, NULL, mctl_present, ipst->ips_netstack);
6372 6372 if (first_mp != NULL) {
6373 6373 if (mctl_present)
6374 6374 freeb(first_mp);
6375 6375 ip_mroute_decap(q, mp, ill);
6376 6376 } /* Else we already freed everything! */
6377 6377 } else {
6378 6378 /*
6379 6379 * Otherwise send an ICMP protocol unreachable.
6380 6380 */
6381 6381 if (ip_fanout_send_icmp(q, first_mp, flags,
6382 6382 ICMP_DEST_UNREACHABLE, ICMP_PROTOCOL_UNREACHABLE,
6383 6383 mctl_present, zoneid, ipst)) {
6384 6384 BUMP_MIB(mibptr, ipIfStatsInUnknownProtos);
6385 6385 }
6386 6386 }
6387 6387 return;
6388 6388 }
6389 6389 CONN_INC_REF(connp);
6390 6390 first_connp = connp;
6391 6391
6392 6392 /*
6393 6393 * Only send message to one tunnel driver by immediately
6394 6394 * terminating the loop.
6395 6395 */
6396 6396 connp = one_only ? NULL : connp->conn_next;
6397 6397
6398 6398 for (;;) {
6399 6399 while (connp != NULL) {
6400 6400 if (IPCL_PROTO_MATCH(connp, protocol, ipha, ill,
6401 6401 flags, zoneid) &&
6402 6402 (!is_system_labeled() ||
6403 6403 tsol_receive_local(mp, &dst, IPV4_VERSION,
6404 6404 shared_addr, connp)))
6405 6405 break;
6406 6406 connp = connp->conn_next;
6407 6407 }
6408 6408
6409 6409 /*
6410 6410 * Copy the packet.
6411 6411 */
6412 6412 if (connp == NULL || connp->conn_upq == NULL ||
6413 6413 (((first_mp1 = dupmsg(first_mp)) == NULL) &&
6414 6414 ((first_mp1 = ip_copymsg(first_mp)) == NULL))) {
6415 6415 /*
6416 6416 * No more interested clients or memory
6417 6417 * allocation failed
6418 6418 */
6419 6419 connp = first_connp;
6420 6420 break;
6421 6421 }
6422 6422 mp1 = mctl_present ? first_mp1->b_cont : first_mp1;
6423 6423 CONN_INC_REF(connp);
6424 6424 mutex_exit(&connfp->connf_lock);
6425 6425 rq = connp->conn_rq;
6426 6426 if (!canputnext(rq)) {
6427 6427 if (flags & IP_FF_RAWIP) {
6428 6428 BUMP_MIB(mibptr, rawipIfStatsInOverflows);
6429 6429 } else {
6430 6430 BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows);
6431 6431 }
6432 6432
6433 6433 freemsg(first_mp1);
6434 6434 } else {
6435 6435 /*
6436 6436 * Don't enforce here if we're an actual tunnel -
6437 6437 * let "tun" do it instead.
6438 6438 */
6439 6439 if (!IPCL_IS_IPTUN(connp) &&
6440 6440 (CONN_INBOUND_POLICY_PRESENT(connp, ipss) ||
6441 6441 secure)) {
6442 6442 first_mp1 = ipsec_check_inbound_policy
6443 6443 (first_mp1, connp, ipha, NULL,
6444 6444 mctl_present);
6445 6445 }
6446 6446 if (first_mp1 != NULL) {
6447 6447 int in_flags = 0;
6448 6448 /*
6449 6449 * ip_fanout_proto also gets called from
6450 6450 * icmp_inbound_error_fanout, in which case
6451 6451 * the msg type is M_CTL. Don't add info
6452 6452 * in this case for the time being. In future
6453 6453 * when there is a need for knowing the
6454 6454 * inbound iface index for ICMP error msgs,
6455 6455 * then this can be changed.
6456 6456 */
6457 6457 if (connp->conn_recvif)
6458 6458 in_flags = IPF_RECVIF;
6459 6459 /*
6460 6460 * The ULP may support IP_RECVPKTINFO for both
6461 6461 * IP v4 and v6 so pass the appropriate argument
6462 6462 * based on conn IP version.
6463 6463 */
6464 6464 if (connp->conn_ip_recvpktinfo) {
6465 6465 if (connp->conn_af_isv6) {
6466 6466 /*
6467 6467 * V6 only needs index
6468 6468 */
6469 6469 in_flags |= IPF_RECVIF;
6470 6470 } else {
6471 6471 /*
6472 6472 * V4 needs index +
6473 6473 * matching address.
6474 6474 */
6475 6475 in_flags |= IPF_RECVADDR;
6476 6476 }
6477 6477 }
6478 6478 if ((in_flags != 0) &&
6479 6479 (mp->b_datap->db_type != M_CTL)) {
6480 6480 /*
6481 6481 * the actual data will be
6482 6482 * contained in b_cont upon
6483 6483 * successful return of the
6484 6484 * following call else
6485 6485 * original mblk is returned
6486 6486 */
6487 6487 ASSERT(recv_ill != NULL);
6488 6488 mp1 = ip_add_info(mp1, recv_ill,
6489 6489 in_flags, IPCL_ZONEID(connp), ipst);
6490 6490 }
6491 6491 BUMP_MIB(mibptr, ipIfStatsHCInDelivers);
6492 6492 if (mctl_present)
6493 6493 freeb(first_mp1);
6494 6494 (connp->conn_recv)(connp, mp1, NULL);
6495 6495 }
6496 6496 }
6497 6497 mutex_enter(&connfp->connf_lock);
6498 6498 /* Follow the next pointer before releasing the conn. */
6499 6499 next_connp = connp->conn_next;
6500 6500 CONN_DEC_REF(connp);
6501 6501 connp = next_connp;
6502 6502 }
6503 6503
6504 6504 /* Last one. Send it upstream. */
6505 6505 mutex_exit(&connfp->connf_lock);
6506 6506
6507 6507 /*
6508 6508 * If this packet is coming from icmp_inbound_error_fanout ip_policy
6509 6509 * will be set to false.
6510 6510 */
6511 6511 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) {
6512 6512 ill_index = ill->ill_phyint->phyint_ifindex;
6513 6513 ip_process(IPP_LOCAL_IN, &mp, ill_index);
6514 6514 if (mp == NULL) {
6515 6515 CONN_DEC_REF(connp);
6516 6516 if (mctl_present) {
6517 6517 freeb(first_mp);
6518 6518 }
6519 6519 return;
6520 6520 }
6521 6521 }
6522 6522
6523 6523 rq = connp->conn_rq;
6524 6524 if (!canputnext(rq)) {
6525 6525 if (flags & IP_FF_RAWIP) {
6526 6526 BUMP_MIB(mibptr, rawipIfStatsInOverflows);
6527 6527 } else {
6528 6528 BUMP_MIB(&ipst->ips_icmp_mib, icmpInOverflows);
6529 6529 }
6530 6530
6531 6531 freemsg(first_mp);
6532 6532 } else {
6533 6533 if (IPCL_IS_IPTUN(connp)) {
6534 6534 /*
6535 6535 * Tunneled packet. We enforce policy in the tunnel
6536 6536 * module itself.
6537 6537 *
6538 6538 * Send the WHOLE packet up (incl. IPSEC_IN) without
6539 6539 * a policy check.
6540 6540 * FIXME to use conn_recv for tun later.
6541 6541 */
6542 6542 putnext(rq, first_mp);
6543 6543 CONN_DEC_REF(connp);
6544 6544 return;
6545 6545 }
6546 6546
6547 6547 if ((CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure)) {
6548 6548 first_mp = ipsec_check_inbound_policy(first_mp, connp,
6549 6549 ipha, NULL, mctl_present);
6550 6550 }
6551 6551
6552 6552 if (first_mp != NULL) {
6553 6553 int in_flags = 0;
6554 6554
6555 6555 /*
6556 6556 * ip_fanout_proto also gets called
6557 6557 * from icmp_inbound_error_fanout, in
6558 6558 * which case the msg type is M_CTL.
6559 6559 * Don't add info in this case for time
6560 6560 * being. In future when there is a
6561 6561 * need for knowing the inbound iface
6562 6562 * index for ICMP error msgs, then this
6563 6563 * can be changed
6564 6564 */
6565 6565 if (connp->conn_recvif)
6566 6566 in_flags = IPF_RECVIF;
6567 6567 if (connp->conn_ip_recvpktinfo) {
6568 6568 if (connp->conn_af_isv6) {
6569 6569 /*
6570 6570 * V6 only needs index
6571 6571 */
6572 6572 in_flags |= IPF_RECVIF;
6573 6573 } else {
6574 6574 /*
6575 6575 * V4 needs index +
6576 6576 * matching address.
6577 6577 */
6578 6578 in_flags |= IPF_RECVADDR;
6579 6579 }
6580 6580 }
6581 6581 if ((in_flags != 0) &&
6582 6582 (mp->b_datap->db_type != M_CTL)) {
6583 6583
6584 6584 /*
6585 6585 * the actual data will be contained in
6586 6586 * b_cont upon successful return
6587 6587 * of the following call else original
6588 6588 * mblk is returned
6589 6589 */
6590 6590 ASSERT(recv_ill != NULL);
6591 6591 mp = ip_add_info(mp, recv_ill,
6592 6592 in_flags, IPCL_ZONEID(connp), ipst);
6593 6593 }
6594 6594 BUMP_MIB(mibptr, ipIfStatsHCInDelivers);
6595 6595 (connp->conn_recv)(connp, mp, NULL);
6596 6596 if (mctl_present)
6597 6597 freeb(first_mp);
6598 6598 }
6599 6599 }
6600 6600 CONN_DEC_REF(connp);
6601 6601 }
6602 6602
6603 6603 /*
6604 6604 * Fanout for TCP packets
6605 6605 * The caller puts <fport, lport> in the ports parameter.
6606 6606 *
6607 6607 * IPQoS Notes
6608 6608 * Before sending it to the client, invoke IPPF processing.
6609 6609 * Policy processing takes place only if the callout_position, IPP_LOCAL_IN,
6610 6610 * is enabled. If we get here from icmp_inbound_error_fanout or ip_wput_local
6611 6611 * ip_policy is false.
6612 6612 */
6613 6613 static void
6614 6614 ip_fanout_tcp(queue_t *q, mblk_t *mp, ill_t *recv_ill, ipha_t *ipha,
6615 6615 uint_t flags, boolean_t mctl_present, boolean_t ip_policy, zoneid_t zoneid)
6616 6616 {
6617 6617 mblk_t *first_mp;
6618 6618 boolean_t secure;
6619 6619 uint32_t ill_index;
6620 6620 int ip_hdr_len;
6621 6621 tcph_t *tcph;
6622 6622 boolean_t syn_present = B_FALSE;
6623 6623 conn_t *connp;
6624 6624 ip_stack_t *ipst = recv_ill->ill_ipst;
6625 6625 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
6626 6626
6627 6627 ASSERT(recv_ill != NULL);
6628 6628
6629 6629 first_mp = mp;
6630 6630 if (mctl_present) {
6631 6631 ASSERT(first_mp->b_datap->db_type == M_CTL);
6632 6632 mp = first_mp->b_cont;
6633 6633 secure = ipsec_in_is_secure(first_mp);
6634 6634 ASSERT(mp != NULL);
6635 6635 } else {
6636 6636 secure = B_FALSE;
6637 6637 }
6638 6638
6639 6639 ip_hdr_len = IPH_HDR_LENGTH(mp->b_rptr);
6640 6640
6641 6641 if ((connp = ipcl_classify_v4(mp, IPPROTO_TCP, ip_hdr_len,
6642 6642 zoneid, ipst)) == NULL) {
6643 6643 /*
6644 6644 * No connected connection or listener. Send a
6645 6645 * TH_RST via tcp_xmit_listeners_reset.
6646 6646 */
6647 6647
6648 6648 /* Initiate IPPf processing, if needed. */
6649 6649 if (IPP_ENABLED(IPP_LOCAL_IN, ipst)) {
6650 6650 uint32_t ill_index;
6651 6651 ill_index = recv_ill->ill_phyint->phyint_ifindex;
6652 6652 ip_process(IPP_LOCAL_IN, &first_mp, ill_index);
6653 6653 if (first_mp == NULL)
6654 6654 return;
6655 6655 }
6656 6656 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers);
6657 6657 ip2dbg(("ip_fanout_tcp: no listener; send reset to zone %d\n",
6658 6658 zoneid));
6659 6659 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid,
6660 6660 ipst->ips_netstack->netstack_tcp, NULL);
6661 6661 return;
6662 6662 }
6663 6663
6664 6664 /*
6665 6665 * Allocate the SYN for the TCP connection here itself
6666 6666 */
6667 6667 tcph = (tcph_t *)&mp->b_rptr[ip_hdr_len];
6668 6668 if ((tcph->th_flags[0] & (TH_SYN|TH_ACK|TH_RST|TH_URG)) == TH_SYN) {
6669 6669 if (IPCL_IS_TCP(connp)) {
6670 6670 squeue_t *sqp;
6671 6671
6672 6672 /*
6673 6673 * For fused tcp loopback, assign the eager's
6674 6674 * squeue to be that of the active connect's.
6675 6675 * Note that we don't check for IP_FF_LOOPBACK
6676 6676 * here since this routine gets called only
6677 6677 * for loopback (unlike the IPv6 counterpart).
6678 6678 */
6679 6679 ASSERT(Q_TO_CONN(q) != NULL);
6680 6680 if (do_tcp_fusion &&
6681 6681 !CONN_INBOUND_POLICY_PRESENT(connp, ipss) &&
6682 6682 !secure &&
6683 6683 !IPP_ENABLED(IPP_LOCAL_IN, ipst) && !ip_policy &&
6684 6684 IPCL_IS_TCP(Q_TO_CONN(q))) {
6685 6685 ASSERT(Q_TO_CONN(q)->conn_sqp != NULL);
6686 6686 sqp = Q_TO_CONN(q)->conn_sqp;
6687 6687 } else {
6688 6688 sqp = IP_SQUEUE_GET(lbolt);
6689 6689 }
6690 6690
|
↓ open down ↓ |
6690 lines elided |
↑ open up ↑ |
6691 6691 mp->b_datap->db_struioflag |= STRUIO_EAGER;
6692 6692 DB_CKSUMSTART(mp) = (intptr_t)sqp;
6693 6693 syn_present = B_TRUE;
6694 6694 }
6695 6695 }
6696 6696
6697 6697 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp) && !syn_present) {
6698 6698 uint_t flags = (unsigned int)tcph->th_flags[0] & 0xFF;
6699 6699 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers);
6700 6700 if ((flags & TH_RST) || (flags & TH_URG)) {
6701 + DTRACE_TCP5(receive, mblk_t *, NULL, conn_t *, NULL,
6702 + void_ip_t *, ipha, tcp_t *, NULL, tcph_t *, tcph);
6701 6703 CONN_DEC_REF(connp);
6702 6704 freemsg(first_mp);
6703 6705 return;
6704 6706 }
6705 6707 if (flags & TH_ACK) {
6706 6708 tcp_xmit_listeners_reset(first_mp, ip_hdr_len, zoneid,
6707 6709 ipst->ips_netstack->netstack_tcp, connp);
6708 6710 CONN_DEC_REF(connp);
6709 6711 return;
6710 6712 }
6711 6713
6714 + DTRACE_TCP5(receive, mblk_t *, NULL, conn_t *, NULL,
6715 + void_ip_t *, ipha, tcp_t *, NULL, tcph_t *, tcph);
6712 6716 CONN_DEC_REF(connp);
6713 6717 freemsg(first_mp);
6714 6718 return;
6715 6719 }
6716 6720
6717 6721 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure) {
6718 6722 first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha,
6719 6723 NULL, mctl_present);
6720 6724 if (first_mp == NULL) {
6721 6725 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards);
6722 6726 CONN_DEC_REF(connp);
6723 6727 return;
6724 6728 }
6725 6729 if (IPCL_IS_TCP(connp) && IPCL_IS_BOUND(connp)) {
6726 6730 ASSERT(syn_present);
6727 6731 if (mctl_present) {
6728 6732 ASSERT(first_mp != mp);
6729 6733 first_mp->b_datap->db_struioflag |=
6730 6734 STRUIO_POLICY;
6731 6735 } else {
6732 6736 ASSERT(first_mp == mp);
6733 6737 mp->b_datap->db_struioflag &=
6734 6738 ~STRUIO_EAGER;
6735 6739 mp->b_datap->db_struioflag |=
6736 6740 STRUIO_POLICY;
6737 6741 }
6738 6742 } else {
6739 6743 /*
6740 6744 * Discard first_mp early since we're dealing with a
6741 6745 * fully-connected conn_t and tcp doesn't do policy in
6742 6746 * this case.
6743 6747 */
6744 6748 if (mctl_present) {
6745 6749 freeb(first_mp);
6746 6750 mctl_present = B_FALSE;
6747 6751 }
6748 6752 first_mp = mp;
6749 6753 }
6750 6754 }
6751 6755
6752 6756 /*
6753 6757 * Initiate policy processing here if needed. If we get here from
6754 6758 * icmp_inbound_error_fanout, ip_policy is false.
6755 6759 */
6756 6760 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) {
6757 6761 ill_index = recv_ill->ill_phyint->phyint_ifindex;
6758 6762 ip_process(IPP_LOCAL_IN, &mp, ill_index);
6759 6763 if (mp == NULL) {
6760 6764 CONN_DEC_REF(connp);
6761 6765 if (mctl_present)
6762 6766 freeb(first_mp);
6763 6767 return;
6764 6768 } else if (mctl_present) {
6765 6769 ASSERT(first_mp != mp);
6766 6770 first_mp->b_cont = mp;
6767 6771 } else {
6768 6772 first_mp = mp;
6769 6773 }
6770 6774 }
6771 6775
6772 6776
6773 6777
6774 6778 /* Handle socket options. */
6775 6779 if (!syn_present &&
6776 6780 connp->conn_ip_recvpktinfo && (flags & IP_FF_IPINFO)) {
6777 6781 /* Add header */
6778 6782 ASSERT(recv_ill != NULL);
6779 6783 /*
6780 6784 * Since tcp does not support IP_RECVPKTINFO for V4, only pass
6781 6785 * IPF_RECVIF.
6782 6786 */
6783 6787 mp = ip_add_info(mp, recv_ill, IPF_RECVIF, IPCL_ZONEID(connp),
6784 6788 ipst);
6785 6789 if (mp == NULL) {
6786 6790 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsInDiscards);
6787 6791 CONN_DEC_REF(connp);
6788 6792 if (mctl_present)
6789 6793 freeb(first_mp);
6790 6794 return;
6791 6795 } else if (mctl_present) {
6792 6796 /*
6793 6797 * ip_add_info might return a new mp.
6794 6798 */
6795 6799 ASSERT(first_mp != mp);
6796 6800 first_mp->b_cont = mp;
6797 6801 } else {
6798 6802 first_mp = mp;
6799 6803 }
6800 6804 }
6801 6805 BUMP_MIB(recv_ill->ill_ip_mib, ipIfStatsHCInDelivers);
6802 6806 if (IPCL_IS_TCP(connp)) {
6803 6807 /* do not drain, certain use cases can blow the stack */
6804 6808 squeue_enter_nodrain(connp->conn_sqp, first_mp,
6805 6809 connp->conn_recv, connp, SQTAG_IP_FANOUT_TCP);
6806 6810 } else {
6807 6811 /* Not TCP; must be SOCK_RAW, IPPROTO_TCP */
6808 6812 (connp->conn_recv)(connp, first_mp, NULL);
6809 6813 CONN_DEC_REF(connp);
6810 6814 }
6811 6815 }
6812 6816
6813 6817 /*
6814 6818 * If we have a IPsec NAT-Traversal packet, strip the zero-SPI or
6815 6819 * pass it along to ESP if the SPI is non-zero. Returns TRUE if the mblk
6816 6820 * is not consumed.
6817 6821 *
6818 6822 * One of four things can happen, all of which affect the passed-in mblk:
6819 6823 *
6820 6824 * 1.) ICMP messages that go through here just get returned TRUE.
6821 6825 *
6822 6826 * 2.) The packet is stock UDP and gets its zero-SPI stripped. Return TRUE.
6823 6827 *
6824 6828 * 3.) The packet is ESP-in-UDP, gets transformed into an equivalent
6825 6829 * ESP packet, and is passed along to ESP for consumption. Return FALSE.
6826 6830 *
6827 6831 * 4.) The packet is an ESP-in-UDP Keepalive. Drop it and return FALSE.
6828 6832 */
6829 6833 static boolean_t
6830 6834 zero_spi_check(queue_t *q, mblk_t *mp, ire_t *ire, ill_t *recv_ill,
6831 6835 ipsec_stack_t *ipss)
6832 6836 {
6833 6837 int shift, plen, iph_len;
6834 6838 ipha_t *ipha;
6835 6839 udpha_t *udpha;
6836 6840 uint32_t *spi;
6837 6841 uint32_t esp_ports;
6838 6842 uint8_t *orptr;
6839 6843 boolean_t free_ire;
6840 6844
6841 6845 if (DB_TYPE(mp) == M_CTL) {
6842 6846 /*
6843 6847 * ICMP message with UDP inside. Don't bother stripping, just
6844 6848 * send it up.
6845 6849 *
6846 6850 * NOTE: Any app with UDP_NAT_T_ENDPOINT set is probably going
6847 6851 * to ignore errors set by ICMP anyway ('cause they might be
6848 6852 * forged), but that's the app's decision, not ours.
6849 6853 */
6850 6854
6851 6855 /* Bunch of reality checks for DEBUG kernels... */
6852 6856 ASSERT(IPH_HDR_VERSION(mp->b_rptr) == IPV4_VERSION);
6853 6857 ASSERT(((ipha_t *)mp->b_rptr)->ipha_protocol == IPPROTO_ICMP);
6854 6858
6855 6859 return (B_TRUE);
6856 6860 }
6857 6861
6858 6862 ipha = (ipha_t *)mp->b_rptr;
6859 6863 iph_len = IPH_HDR_LENGTH(ipha);
6860 6864 plen = ntohs(ipha->ipha_length);
6861 6865
6862 6866 if (plen - iph_len - sizeof (udpha_t) < sizeof (uint32_t)) {
6863 6867 /*
6864 6868 * Most likely a keepalive for the benefit of an intervening
6865 6869 * NAT. These aren't for us, per se, so drop it.
6866 6870 *
6867 6871 * RFC 3947/8 doesn't say for sure what to do for 2-3
6868 6872 * byte packets (keepalives are 1-byte), but we'll drop them
6869 6873 * also.
6870 6874 */
6871 6875 ip_drop_packet(mp, B_TRUE, recv_ill, NULL,
6872 6876 DROPPER(ipss, ipds_esp_nat_t_ka), &ipss->ipsec_dropper);
6873 6877 return (B_FALSE);
6874 6878 }
6875 6879
6876 6880 if (MBLKL(mp) < iph_len + sizeof (udpha_t) + sizeof (*spi)) {
6877 6881 /* might as well pull it all up - it might be ESP. */
6878 6882 if (!pullupmsg(mp, -1)) {
6879 6883 ip_drop_packet(mp, B_TRUE, recv_ill, NULL,
6880 6884 DROPPER(ipss, ipds_esp_nomem),
6881 6885 &ipss->ipsec_dropper);
6882 6886 return (B_FALSE);
6883 6887 }
6884 6888
6885 6889 ipha = (ipha_t *)mp->b_rptr;
6886 6890 }
6887 6891 spi = (uint32_t *)(mp->b_rptr + iph_len + sizeof (udpha_t));
6888 6892 if (*spi == 0) {
6889 6893 /* UDP packet - remove 0-spi. */
6890 6894 shift = sizeof (uint32_t);
6891 6895 } else {
6892 6896 /* ESP-in-UDP packet - reduce to ESP. */
6893 6897 ipha->ipha_protocol = IPPROTO_ESP;
6894 6898 shift = sizeof (udpha_t);
6895 6899 }
6896 6900
6897 6901 /* Fix IP header */
6898 6902 ipha->ipha_length = htons(plen - shift);
6899 6903 ipha->ipha_hdr_checksum = 0;
6900 6904
6901 6905 orptr = mp->b_rptr;
6902 6906 mp->b_rptr += shift;
6903 6907
6904 6908 udpha = (udpha_t *)(orptr + iph_len);
6905 6909 if (*spi == 0) {
6906 6910 ASSERT((uint8_t *)ipha == orptr);
6907 6911 udpha->uha_length = htons(plen - shift - iph_len);
6908 6912 iph_len += sizeof (udpha_t); /* For the call to ovbcopy(). */
6909 6913 esp_ports = 0;
6910 6914 } else {
6911 6915 esp_ports = *((uint32_t *)udpha);
6912 6916 ASSERT(esp_ports != 0);
6913 6917 }
6914 6918 ovbcopy(orptr, orptr + shift, iph_len);
6915 6919 if (esp_ports != 0) /* Punt up for ESP processing. */ {
6916 6920 ipha = (ipha_t *)(orptr + shift);
6917 6921
6918 6922 free_ire = (ire == NULL);
6919 6923 if (free_ire) {
6920 6924 /* Re-acquire ire. */
6921 6925 ire = ire_cache_lookup(ipha->ipha_dst, ALL_ZONES, NULL,
6922 6926 ipss->ipsec_netstack->netstack_ip);
6923 6927 if (ire == NULL || !(ire->ire_type & IRE_LOCAL)) {
6924 6928 if (ire != NULL)
6925 6929 ire_refrele(ire);
6926 6930 /*
6927 6931 * Do a regular freemsg(), as this is an IP
6928 6932 * error (no local route) not an IPsec one.
6929 6933 */
6930 6934 freemsg(mp);
6931 6935 }
6932 6936 }
6933 6937
6934 6938 ip_proto_input(q, mp, ipha, ire, recv_ill, esp_ports);
6935 6939 if (free_ire)
6936 6940 ire_refrele(ire);
6937 6941 }
6938 6942
6939 6943 return (esp_ports == 0);
6940 6944 }
6941 6945
6942 6946 /*
6943 6947 * Deliver a udp packet to the given conn, possibly applying ipsec policy.
6944 6948 * We are responsible for disposing of mp, such as by freemsg() or putnext()
6945 6949 * Caller is responsible for dropping references to the conn, and freeing
6946 6950 * first_mp.
6947 6951 *
6948 6952 * IPQoS Notes
6949 6953 * Before sending it to the client, invoke IPPF processing. Policy processing
6950 6954 * takes place only if the callout_position, IPP_LOCAL_IN, is enabled and
6951 6955 * ip_policy is true. If we get here from icmp_inbound_error_fanout or
6952 6956 * ip_wput_local, ip_policy is false.
6953 6957 */
6954 6958 static void
6955 6959 ip_fanout_udp_conn(conn_t *connp, mblk_t *first_mp, mblk_t *mp,
6956 6960 boolean_t secure, ill_t *ill, ipha_t *ipha, uint_t flags, ill_t *recv_ill,
6957 6961 boolean_t ip_policy)
6958 6962 {
6959 6963 boolean_t mctl_present = (first_mp != NULL);
6960 6964 uint32_t in_flags = 0; /* set to IP_RECVSLLA and/or IP_RECVIF */
6961 6965 uint32_t ill_index;
6962 6966 ip_stack_t *ipst = recv_ill->ill_ipst;
6963 6967 ipsec_stack_t *ipss = ipst->ips_netstack->netstack_ipsec;
6964 6968
6965 6969 ASSERT(ill != NULL);
6966 6970
6967 6971 if (mctl_present)
6968 6972 first_mp->b_cont = mp;
6969 6973 else
6970 6974 first_mp = mp;
6971 6975
6972 6976 if (CONN_UDP_FLOWCTLD(connp)) {
6973 6977 BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows);
6974 6978 freemsg(first_mp);
6975 6979 return;
6976 6980 }
6977 6981
6978 6982 if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) || secure) {
6979 6983 first_mp = ipsec_check_inbound_policy(first_mp, connp, ipha,
6980 6984 NULL, mctl_present);
6981 6985 if (first_mp == NULL) {
6982 6986 BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
6983 6987 return; /* Freed by ipsec_check_inbound_policy(). */
6984 6988 }
6985 6989 }
6986 6990 if (mctl_present)
6987 6991 freeb(first_mp);
6988 6992
6989 6993 /* Let's hope the compilers utter "branch, predict-not-taken..." ;) */
6990 6994 if (connp->conn_udp->udp_nat_t_endpoint) {
6991 6995 if (mctl_present) {
6992 6996 /* mctl_present *shouldn't* happen. */
6993 6997 ip_drop_packet(mp, B_TRUE, NULL, NULL,
6994 6998 DROPPER(ipss, ipds_esp_nat_t_ipsec),
6995 6999 &ipss->ipsec_dropper);
6996 7000 return;
6997 7001 }
6998 7002
6999 7003 if (!zero_spi_check(ill->ill_rq, mp, NULL, recv_ill, ipss))
7000 7004 return;
7001 7005 }
7002 7006
7003 7007 /* Handle options. */
7004 7008 if (connp->conn_recvif)
7005 7009 in_flags = IPF_RECVIF;
7006 7010 /*
7007 7011 * UDP supports IP_RECVPKTINFO option for both v4 and v6 so the flag
7008 7012 * passed to ip_add_info is based on IP version of connp.
7009 7013 */
7010 7014 if (connp->conn_ip_recvpktinfo && (flags & IP_FF_IPINFO)) {
7011 7015 if (connp->conn_af_isv6) {
7012 7016 /*
7013 7017 * V6 only needs index
7014 7018 */
7015 7019 in_flags |= IPF_RECVIF;
7016 7020 } else {
7017 7021 /*
7018 7022 * V4 needs index + matching address.
7019 7023 */
7020 7024 in_flags |= IPF_RECVADDR;
7021 7025 }
7022 7026 }
7023 7027
7024 7028 if (connp->conn_recvslla && !(flags & IP_FF_SEND_SLLA))
7025 7029 in_flags |= IPF_RECVSLLA;
7026 7030
7027 7031 /*
7028 7032 * Initiate IPPF processing here, if needed. Note first_mp won't be
7029 7033 * freed if the packet is dropped. The caller will do so.
7030 7034 */
7031 7035 if (IPP_ENABLED(IPP_LOCAL_IN, ipst) && ip_policy) {
7032 7036 ill_index = recv_ill->ill_phyint->phyint_ifindex;
7033 7037 ip_process(IPP_LOCAL_IN, &mp, ill_index);
7034 7038 if (mp == NULL) {
7035 7039 return;
7036 7040 }
7037 7041 }
7038 7042 if ((in_flags != 0) &&
7039 7043 (mp->b_datap->db_type != M_CTL)) {
7040 7044 /*
7041 7045 * The actual data will be contained in b_cont
7042 7046 * upon successful return of the following call
7043 7047 * else original mblk is returned
7044 7048 */
7045 7049 ASSERT(recv_ill != NULL);
7046 7050 mp = ip_add_info(mp, recv_ill, in_flags, IPCL_ZONEID(connp),
7047 7051 ipst);
7048 7052 }
7049 7053 BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
7050 7054 /* Send it upstream */
7051 7055 (connp->conn_recv)(connp, mp, NULL);
7052 7056 }
7053 7057
7054 7058 /*
7055 7059 * Fanout for UDP packets.
7056 7060 * The caller puts <fport, lport> in the ports parameter.
7057 7061 *
7058 7062 * If SO_REUSEADDR is set all multicast and broadcast packets
7059 7063 * will be delivered to all streams bound to the same port.
7060 7064 *
7061 7065 * Zones notes:
7062 7066 * Multicast and broadcast packets will be distributed to streams in all zones.
7063 7067 * In the special case where an AF_INET socket binds to 0.0.0.0/<port> and an
7064 7068 * AF_INET6 socket binds to ::/<port>, only the AF_INET socket receives the IPv4
7065 7069 * packets. To maintain this behavior with multiple zones, the conns are grouped
7066 7070 * by zone and the SO_REUSEADDR flag is checked for the first matching conn in
7067 7071 * each zone. If unset, all the following conns in the same zone are skipped.
7068 7072 */
7069 7073 static void
7070 7074 ip_fanout_udp(queue_t *q, mblk_t *mp, ill_t *ill, ipha_t *ipha,
7071 7075 uint32_t ports, boolean_t broadcast, uint_t flags, boolean_t mctl_present,
7072 7076 boolean_t ip_policy, ill_t *recv_ill, zoneid_t zoneid)
7073 7077 {
7074 7078 uint32_t dstport, srcport;
7075 7079 ipaddr_t dst;
7076 7080 mblk_t *first_mp;
7077 7081 boolean_t secure;
7078 7082 in6_addr_t v6src;
7079 7083 conn_t *connp;
7080 7084 connf_t *connfp;
7081 7085 conn_t *first_connp;
7082 7086 conn_t *next_connp;
7083 7087 mblk_t *mp1, *first_mp1;
7084 7088 ipaddr_t src;
7085 7089 zoneid_t last_zoneid;
7086 7090 boolean_t reuseaddr;
7087 7091 boolean_t shared_addr;
7088 7092 boolean_t unlabeled;
7089 7093 ip_stack_t *ipst;
7090 7094
7091 7095 ASSERT(recv_ill != NULL);
7092 7096 ipst = recv_ill->ill_ipst;
7093 7097
7094 7098 first_mp = mp;
7095 7099 if (mctl_present) {
7096 7100 mp = first_mp->b_cont;
7097 7101 first_mp->b_cont = NULL;
7098 7102 secure = ipsec_in_is_secure(first_mp);
7099 7103 ASSERT(mp != NULL);
7100 7104 } else {
7101 7105 first_mp = NULL;
7102 7106 secure = B_FALSE;
7103 7107 }
7104 7108
7105 7109 /* Extract ports in net byte order */
7106 7110 dstport = htons(ntohl(ports) & 0xFFFF);
7107 7111 srcport = htons(ntohl(ports) >> 16);
7108 7112 dst = ipha->ipha_dst;
7109 7113 src = ipha->ipha_src;
7110 7114
7111 7115 unlabeled = B_FALSE;
7112 7116 if (is_system_labeled())
7113 7117 /* Cred cannot be null on IPv4 */
7114 7118 unlabeled = (crgetlabel(DB_CRED(mp))->tsl_flags &
7115 7119 TSLF_UNLABELED) != 0;
7116 7120 shared_addr = (zoneid == ALL_ZONES);
7117 7121 if (shared_addr) {
7118 7122 /*
7119 7123 * No need to handle exclusive-stack zones since ALL_ZONES
7120 7124 * only applies to the shared stack.
7121 7125 */
7122 7126 zoneid = tsol_mlp_findzone(IPPROTO_UDP, dstport);
7123 7127 /*
7124 7128 * If no shared MLP is found, tsol_mlp_findzone returns
7125 7129 * ALL_ZONES. In that case, we assume it's SLP, and
7126 7130 * search for the zone based on the packet label.
7127 7131 *
7128 7132 * If there is such a zone, we prefer to find a
7129 7133 * connection in it. Otherwise, we look for a
7130 7134 * MAC-exempt connection in any zone whose label
7131 7135 * dominates the default label on the packet.
7132 7136 */
7133 7137 if (zoneid == ALL_ZONES)
7134 7138 zoneid = tsol_packet_to_zoneid(mp);
7135 7139 else
7136 7140 unlabeled = B_FALSE;
7137 7141 }
7138 7142
7139 7143 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(dstport, ipst)];
7140 7144 mutex_enter(&connfp->connf_lock);
7141 7145 connp = connfp->connf_head;
7142 7146 if (!broadcast && !CLASSD(dst)) {
7143 7147 /*
7144 7148 * Not broadcast or multicast. Send to the one (first)
7145 7149 * client we find. No need to check conn_wantpacket()
7146 7150 * since IP_BOUND_IF/conn_incoming_ill does not apply to
7147 7151 * IPv4 unicast packets.
7148 7152 */
7149 7153 while ((connp != NULL) &&
7150 7154 (!IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) ||
7151 7155 (!IPCL_ZONE_MATCH(connp, zoneid) &&
7152 7156 !(unlabeled && connp->conn_mac_exempt)))) {
7153 7157 /*
7154 7158 * We keep searching since the conn did not match,
7155 7159 * or its zone did not match and it is not either
7156 7160 * an allzones conn or a mac exempt conn (if the
7157 7161 * sender is unlabeled.)
7158 7162 */
7159 7163 connp = connp->conn_next;
7160 7164 }
7161 7165
7162 7166 if (connp == NULL || connp->conn_upq == NULL)
7163 7167 goto notfound;
7164 7168
7165 7169 if (is_system_labeled() &&
7166 7170 !tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr,
7167 7171 connp))
7168 7172 goto notfound;
7169 7173
7170 7174 CONN_INC_REF(connp);
7171 7175 mutex_exit(&connfp->connf_lock);
7172 7176 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha,
7173 7177 flags, recv_ill, ip_policy);
7174 7178 IP_STAT(ipst, ip_udp_fannorm);
7175 7179 CONN_DEC_REF(connp);
7176 7180 return;
7177 7181 }
7178 7182
7179 7183 /*
7180 7184 * Broadcast and multicast case
7181 7185 *
7182 7186 * Need to check conn_wantpacket().
7183 7187 * If SO_REUSEADDR has been set on the first we send the
7184 7188 * packet to all clients that have joined the group and
7185 7189 * match the port.
7186 7190 */
7187 7191
7188 7192 while (connp != NULL) {
7189 7193 if ((IPCL_UDP_MATCH(connp, dstport, dst, srcport, src)) &&
7190 7194 conn_wantpacket(connp, ill, ipha, flags, zoneid) &&
7191 7195 (!is_system_labeled() ||
7192 7196 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr,
7193 7197 connp)))
7194 7198 break;
7195 7199 connp = connp->conn_next;
7196 7200 }
7197 7201
7198 7202 if (connp == NULL || connp->conn_upq == NULL)
7199 7203 goto notfound;
7200 7204
7201 7205 first_connp = connp;
7202 7206 /*
7203 7207 * When SO_REUSEADDR is not set, send the packet only to the first
7204 7208 * matching connection in its zone by keeping track of the zoneid.
7205 7209 */
7206 7210 reuseaddr = first_connp->conn_reuseaddr;
7207 7211 last_zoneid = first_connp->conn_zoneid;
7208 7212
7209 7213 CONN_INC_REF(connp);
7210 7214 connp = connp->conn_next;
7211 7215 for (;;) {
7212 7216 while (connp != NULL) {
7213 7217 if (IPCL_UDP_MATCH(connp, dstport, dst, srcport, src) &&
7214 7218 (reuseaddr || connp->conn_zoneid != last_zoneid) &&
7215 7219 conn_wantpacket(connp, ill, ipha, flags, zoneid) &&
7216 7220 (!is_system_labeled() ||
7217 7221 tsol_receive_local(mp, &dst, IPV4_VERSION,
7218 7222 shared_addr, connp)))
7219 7223 break;
7220 7224 connp = connp->conn_next;
7221 7225 }
7222 7226 /*
7223 7227 * Just copy the data part alone. The mctl part is
7224 7228 * needed just for verifying policy and it is never
7225 7229 * sent up.
7226 7230 */
7227 7231 if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) &&
7228 7232 ((mp1 = copymsg(mp)) == NULL))) {
7229 7233 /*
7230 7234 * No more interested clients or memory
7231 7235 * allocation failed
7232 7236 */
7233 7237 connp = first_connp;
7234 7238 break;
7235 7239 }
7236 7240 if (connp->conn_zoneid != last_zoneid) {
7237 7241 /*
7238 7242 * Update the zoneid so that the packet isn't sent to
7239 7243 * any more conns in the same zone unless SO_REUSEADDR
7240 7244 * is set.
7241 7245 */
7242 7246 reuseaddr = connp->conn_reuseaddr;
7243 7247 last_zoneid = connp->conn_zoneid;
7244 7248 }
7245 7249 if (first_mp != NULL) {
7246 7250 ASSERT(((ipsec_info_t *)first_mp->b_rptr)->
7247 7251 ipsec_info_type == IPSEC_IN);
7248 7252 first_mp1 = ipsec_in_tag(first_mp, NULL,
7249 7253 ipst->ips_netstack);
7250 7254 if (first_mp1 == NULL) {
7251 7255 freemsg(mp1);
7252 7256 connp = first_connp;
7253 7257 break;
7254 7258 }
7255 7259 } else {
7256 7260 first_mp1 = NULL;
7257 7261 }
7258 7262 CONN_INC_REF(connp);
7259 7263 mutex_exit(&connfp->connf_lock);
7260 7264 /*
7261 7265 * IPQoS notes: We don't send the packet for policy
7262 7266 * processing here, will do it for the last one (below).
7263 7267 * i.e. we do it per-packet now, but if we do policy
7264 7268 * processing per-conn, then we would need to do it
7265 7269 * here too.
7266 7270 */
7267 7271 ip_fanout_udp_conn(connp, first_mp1, mp1, secure, ill,
7268 7272 ipha, flags, recv_ill, B_FALSE);
7269 7273 mutex_enter(&connfp->connf_lock);
7270 7274 /* Follow the next pointer before releasing the conn. */
7271 7275 next_connp = connp->conn_next;
7272 7276 IP_STAT(ipst, ip_udp_fanmb);
7273 7277 CONN_DEC_REF(connp);
7274 7278 connp = next_connp;
7275 7279 }
7276 7280
7277 7281 /* Last one. Send it upstream. */
7278 7282 mutex_exit(&connfp->connf_lock);
7279 7283 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, flags,
7280 7284 recv_ill, ip_policy);
7281 7285 IP_STAT(ipst, ip_udp_fanmb);
7282 7286 CONN_DEC_REF(connp);
7283 7287 return;
7284 7288
7285 7289 notfound:
7286 7290
7287 7291 mutex_exit(&connfp->connf_lock);
7288 7292 IP_STAT(ipst, ip_udp_fanothers);
7289 7293 /*
7290 7294 * IPv6 endpoints bound to unicast or multicast IPv4-mapped addresses
7291 7295 * have already been matched above, since they live in the IPv4
7292 7296 * fanout tables. This implies we only need to
7293 7297 * check for IPv6 in6addr_any endpoints here.
7294 7298 * Thus we compare using ipv6_all_zeros instead of the destination
7295 7299 * address, except for the multicast group membership lookup which
7296 7300 * uses the IPv4 destination.
7297 7301 */
7298 7302 IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &v6src);
7299 7303 connfp = &ipst->ips_ipcl_udp_fanout[IPCL_UDP_HASH(dstport, ipst)];
7300 7304 mutex_enter(&connfp->connf_lock);
7301 7305 connp = connfp->connf_head;
7302 7306 if (!broadcast && !CLASSD(dst)) {
7303 7307 while (connp != NULL) {
7304 7308 if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros,
7305 7309 srcport, v6src) && IPCL_ZONE_MATCH(connp, zoneid) &&
7306 7310 conn_wantpacket(connp, ill, ipha, flags, zoneid) &&
7307 7311 !connp->conn_ipv6_v6only)
7308 7312 break;
7309 7313 connp = connp->conn_next;
7310 7314 }
7311 7315
7312 7316 if (connp != NULL && is_system_labeled() &&
7313 7317 !tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr,
7314 7318 connp))
7315 7319 connp = NULL;
7316 7320
7317 7321 if (connp == NULL || connp->conn_upq == NULL) {
7318 7322 /*
7319 7323 * No one bound to this port. Is
7320 7324 * there a client that wants all
7321 7325 * unclaimed datagrams?
7322 7326 */
7323 7327 mutex_exit(&connfp->connf_lock);
7324 7328
7325 7329 if (mctl_present)
7326 7330 first_mp->b_cont = mp;
7327 7331 else
7328 7332 first_mp = mp;
7329 7333 if (ipst->ips_ipcl_proto_fanout[IPPROTO_UDP].
7330 7334 connf_head != NULL) {
7331 7335 ip_fanout_proto(q, first_mp, ill, ipha,
7332 7336 flags | IP_FF_RAWIP, mctl_present,
7333 7337 ip_policy, recv_ill, zoneid);
7334 7338 } else {
7335 7339 if (ip_fanout_send_icmp(q, first_mp, flags,
7336 7340 ICMP_DEST_UNREACHABLE,
7337 7341 ICMP_PORT_UNREACHABLE,
7338 7342 mctl_present, zoneid, ipst)) {
7339 7343 BUMP_MIB(ill->ill_ip_mib,
7340 7344 udpIfStatsNoPorts);
7341 7345 }
7342 7346 }
7343 7347 return;
7344 7348 }
7345 7349
7346 7350 CONN_INC_REF(connp);
7347 7351 mutex_exit(&connfp->connf_lock);
7348 7352 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha,
7349 7353 flags, recv_ill, ip_policy);
7350 7354 CONN_DEC_REF(connp);
7351 7355 return;
7352 7356 }
7353 7357 /*
7354 7358 * IPv4 multicast packet being delivered to an AF_INET6
7355 7359 * in6addr_any endpoint.
7356 7360 * Need to check conn_wantpacket(). Note that we use conn_wantpacket()
7357 7361 * and not conn_wantpacket_v6() since any multicast membership is
7358 7362 * for an IPv4-mapped multicast address.
7359 7363 * The packet is sent to all clients in all zones that have joined the
7360 7364 * group and match the port.
7361 7365 */
7362 7366 while (connp != NULL) {
7363 7367 if (IPCL_UDP_MATCH_V6(connp, dstport, ipv6_all_zeros,
7364 7368 srcport, v6src) &&
7365 7369 conn_wantpacket(connp, ill, ipha, flags, zoneid) &&
7366 7370 (!is_system_labeled() ||
7367 7371 tsol_receive_local(mp, &dst, IPV4_VERSION, shared_addr,
7368 7372 connp)))
7369 7373 break;
7370 7374 connp = connp->conn_next;
7371 7375 }
7372 7376
7373 7377 if (connp == NULL || connp->conn_upq == NULL) {
7374 7378 /*
7375 7379 * No one bound to this port. Is
7376 7380 * there a client that wants all
7377 7381 * unclaimed datagrams?
7378 7382 */
7379 7383 mutex_exit(&connfp->connf_lock);
7380 7384
7381 7385 if (mctl_present)
7382 7386 first_mp->b_cont = mp;
7383 7387 else
7384 7388 first_mp = mp;
7385 7389 if (ipst->ips_ipcl_proto_fanout[IPPROTO_UDP].connf_head !=
7386 7390 NULL) {
7387 7391 ip_fanout_proto(q, first_mp, ill, ipha,
7388 7392 flags | IP_FF_RAWIP, mctl_present, ip_policy,
7389 7393 recv_ill, zoneid);
7390 7394 } else {
7391 7395 /*
7392 7396 * We used to attempt to send an icmp error here, but
7393 7397 * since this is known to be a multicast packet
7394 7398 * and we don't send icmp errors in response to
7395 7399 * multicast, just drop the packet and give up sooner.
7396 7400 */
7397 7401 BUMP_MIB(ill->ill_ip_mib, udpIfStatsNoPorts);
7398 7402 freemsg(first_mp);
7399 7403 }
7400 7404 return;
7401 7405 }
7402 7406
7403 7407 first_connp = connp;
7404 7408
7405 7409 CONN_INC_REF(connp);
7406 7410 connp = connp->conn_next;
7407 7411 for (;;) {
7408 7412 while (connp != NULL) {
7409 7413 if (IPCL_UDP_MATCH_V6(connp, dstport,
7410 7414 ipv6_all_zeros, srcport, v6src) &&
7411 7415 conn_wantpacket(connp, ill, ipha, flags, zoneid) &&
7412 7416 (!is_system_labeled() ||
7413 7417 tsol_receive_local(mp, &dst, IPV4_VERSION,
7414 7418 shared_addr, connp)))
7415 7419 break;
7416 7420 connp = connp->conn_next;
7417 7421 }
7418 7422 /*
7419 7423 * Just copy the data part alone. The mctl part is
7420 7424 * needed just for verifying policy and it is never
7421 7425 * sent up.
7422 7426 */
7423 7427 if (connp == NULL || (((mp1 = dupmsg(mp)) == NULL) &&
7424 7428 ((mp1 = copymsg(mp)) == NULL))) {
7425 7429 /*
7426 7430 * No more intested clients or memory
7427 7431 * allocation failed
7428 7432 */
7429 7433 connp = first_connp;
7430 7434 break;
7431 7435 }
7432 7436 if (first_mp != NULL) {
7433 7437 ASSERT(((ipsec_info_t *)first_mp->b_rptr)->
7434 7438 ipsec_info_type == IPSEC_IN);
7435 7439 first_mp1 = ipsec_in_tag(first_mp, NULL,
7436 7440 ipst->ips_netstack);
7437 7441 if (first_mp1 == NULL) {
7438 7442 freemsg(mp1);
7439 7443 connp = first_connp;
7440 7444 break;
7441 7445 }
7442 7446 } else {
7443 7447 first_mp1 = NULL;
7444 7448 }
7445 7449 CONN_INC_REF(connp);
7446 7450 mutex_exit(&connfp->connf_lock);
7447 7451 /*
7448 7452 * IPQoS notes: We don't send the packet for policy
7449 7453 * processing here, will do it for the last one (below).
7450 7454 * i.e. we do it per-packet now, but if we do policy
7451 7455 * processing per-conn, then we would need to do it
7452 7456 * here too.
7453 7457 */
7454 7458 ip_fanout_udp_conn(connp, first_mp1, mp1, secure, ill,
7455 7459 ipha, flags, recv_ill, B_FALSE);
7456 7460 mutex_enter(&connfp->connf_lock);
7457 7461 /* Follow the next pointer before releasing the conn. */
7458 7462 next_connp = connp->conn_next;
7459 7463 CONN_DEC_REF(connp);
7460 7464 connp = next_connp;
7461 7465 }
7462 7466
7463 7467 /* Last one. Send it upstream. */
7464 7468 mutex_exit(&connfp->connf_lock);
7465 7469 ip_fanout_udp_conn(connp, first_mp, mp, secure, ill, ipha, flags,
7466 7470 recv_ill, ip_policy);
7467 7471 CONN_DEC_REF(connp);
7468 7472 }
7469 7473
7470 7474 /*
7471 7475 * Complete the ip_wput header so that it
7472 7476 * is possible to generate ICMP
7473 7477 * errors.
7474 7478 */
7475 7479 int
7476 7480 ip_hdr_complete(ipha_t *ipha, zoneid_t zoneid, ip_stack_t *ipst)
7477 7481 {
7478 7482 ire_t *ire;
7479 7483
7480 7484 if (ipha->ipha_src == INADDR_ANY) {
7481 7485 ire = ire_lookup_local(zoneid, ipst);
7482 7486 if (ire == NULL) {
7483 7487 ip1dbg(("ip_hdr_complete: no source IRE\n"));
7484 7488 return (1);
7485 7489 }
7486 7490 ipha->ipha_src = ire->ire_addr;
7487 7491 ire_refrele(ire);
7488 7492 }
7489 7493 ipha->ipha_ttl = ipst->ips_ip_def_ttl;
7490 7494 ipha->ipha_hdr_checksum = 0;
7491 7495 ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
7492 7496 return (0);
7493 7497 }
7494 7498
7495 7499 /*
7496 7500 * Nobody should be sending
7497 7501 * packets up this stream
7498 7502 */
7499 7503 static void
7500 7504 ip_lrput(queue_t *q, mblk_t *mp)
7501 7505 {
7502 7506 mblk_t *mp1;
7503 7507
7504 7508 switch (mp->b_datap->db_type) {
7505 7509 case M_FLUSH:
7506 7510 /* Turn around */
7507 7511 if (*mp->b_rptr & FLUSHW) {
7508 7512 *mp->b_rptr &= ~FLUSHR;
7509 7513 qreply(q, mp);
7510 7514 return;
7511 7515 }
7512 7516 break;
7513 7517 }
7514 7518 /* Could receive messages that passed through ar_rput */
7515 7519 for (mp1 = mp; mp1; mp1 = mp1->b_cont)
7516 7520 mp1->b_prev = mp1->b_next = NULL;
7517 7521 freemsg(mp);
7518 7522 }
7519 7523
7520 7524 /* Nobody should be sending packets down this stream */
7521 7525 /* ARGSUSED */
7522 7526 void
7523 7527 ip_lwput(queue_t *q, mblk_t *mp)
7524 7528 {
7525 7529 freemsg(mp);
7526 7530 }
7527 7531
7528 7532 /*
7529 7533 * Move the first hop in any source route to ipha_dst and remove that part of
7530 7534 * the source route. Called by other protocols. Errors in option formatting
7531 7535 * are ignored - will be handled by ip_wput_options Return the final
7532 7536 * destination (either ipha_dst or the last entry in a source route.)
7533 7537 */
7534 7538 ipaddr_t
7535 7539 ip_massage_options(ipha_t *ipha, netstack_t *ns)
7536 7540 {
7537 7541 ipoptp_t opts;
7538 7542 uchar_t *opt;
7539 7543 uint8_t optval;
7540 7544 uint8_t optlen;
7541 7545 ipaddr_t dst;
7542 7546 int i;
7543 7547 ire_t *ire;
7544 7548 ip_stack_t *ipst = ns->netstack_ip;
7545 7549
7546 7550 ip2dbg(("ip_massage_options\n"));
7547 7551 dst = ipha->ipha_dst;
7548 7552 for (optval = ipoptp_first(&opts, ipha);
7549 7553 optval != IPOPT_EOL;
7550 7554 optval = ipoptp_next(&opts)) {
7551 7555 opt = opts.ipoptp_cur;
7552 7556 switch (optval) {
7553 7557 uint8_t off;
7554 7558 case IPOPT_SSRR:
7555 7559 case IPOPT_LSRR:
7556 7560 if ((opts.ipoptp_flags & IPOPTP_ERROR) != 0) {
7557 7561 ip1dbg(("ip_massage_options: bad src route\n"));
7558 7562 break;
7559 7563 }
7560 7564 optlen = opts.ipoptp_len;
7561 7565 off = opt[IPOPT_OFFSET];
7562 7566 off--;
7563 7567 redo_srr:
7564 7568 if (optlen < IP_ADDR_LEN ||
7565 7569 off > optlen - IP_ADDR_LEN) {
7566 7570 /* End of source route */
7567 7571 ip1dbg(("ip_massage_options: end of SR\n"));
7568 7572 break;
7569 7573 }
7570 7574 bcopy((char *)opt + off, &dst, IP_ADDR_LEN);
7571 7575 ip1dbg(("ip_massage_options: next hop 0x%x\n",
7572 7576 ntohl(dst)));
7573 7577 /*
7574 7578 * Check if our address is present more than
7575 7579 * once as consecutive hops in source route.
7576 7580 * XXX verify per-interface ip_forwarding
7577 7581 * for source route?
7578 7582 */
7579 7583 ire = ire_ctable_lookup(dst, 0, IRE_LOCAL, NULL,
7580 7584 ALL_ZONES, NULL, MATCH_IRE_TYPE, ipst);
7581 7585 if (ire != NULL) {
7582 7586 ire_refrele(ire);
7583 7587 off += IP_ADDR_LEN;
7584 7588 goto redo_srr;
7585 7589 }
7586 7590 if (dst == htonl(INADDR_LOOPBACK)) {
7587 7591 ip1dbg(("ip_massage_options: loopback addr in "
7588 7592 "source route!\n"));
7589 7593 break;
7590 7594 }
7591 7595 /*
7592 7596 * Update ipha_dst to be the first hop and remove the
7593 7597 * first hop from the source route (by overwriting
7594 7598 * part of the option with NOP options).
7595 7599 */
7596 7600 ipha->ipha_dst = dst;
7597 7601 /* Put the last entry in dst */
7598 7602 off = ((optlen - IP_ADDR_LEN - 3) & ~(IP_ADDR_LEN-1)) +
7599 7603 3;
7600 7604 bcopy(&opt[off], &dst, IP_ADDR_LEN);
7601 7605
7602 7606 ip1dbg(("ip_massage_options: last hop 0x%x\n",
7603 7607 ntohl(dst)));
7604 7608 /* Move down and overwrite */
7605 7609 opt[IP_ADDR_LEN] = opt[0];
7606 7610 opt[IP_ADDR_LEN+1] = opt[IPOPT_OLEN] - IP_ADDR_LEN;
7607 7611 opt[IP_ADDR_LEN+2] = opt[IPOPT_OFFSET];
7608 7612 for (i = 0; i < IP_ADDR_LEN; i++)
7609 7613 opt[i] = IPOPT_NOP;
7610 7614 break;
7611 7615 }
7612 7616 }
7613 7617 return (dst);
7614 7618 }
7615 7619
7616 7620 /*
7617 7621 * Return the network mask
7618 7622 * associated with the specified address.
7619 7623 */
7620 7624 ipaddr_t
7621 7625 ip_net_mask(ipaddr_t addr)
7622 7626 {
7623 7627 uchar_t *up = (uchar_t *)&addr;
7624 7628 ipaddr_t mask = 0;
7625 7629 uchar_t *maskp = (uchar_t *)&mask;
7626 7630
7627 7631 #if defined(__i386) || defined(__amd64)
7628 7632 #define TOTALLY_BRAIN_DAMAGED_C_COMPILER
7629 7633 #endif
7630 7634 #ifdef TOTALLY_BRAIN_DAMAGED_C_COMPILER
7631 7635 maskp[0] = maskp[1] = maskp[2] = maskp[3] = 0;
7632 7636 #endif
7633 7637 if (CLASSD(addr)) {
7634 7638 maskp[0] = 0xF0;
7635 7639 return (mask);
7636 7640 }
7637 7641
7638 7642 /* We assume Class E default netmask to be 32 */
7639 7643 if (CLASSE(addr))
7640 7644 return (0xffffffffU);
7641 7645
7642 7646 if (addr == 0)
7643 7647 return (0);
7644 7648 maskp[0] = 0xFF;
7645 7649 if ((up[0] & 0x80) == 0)
7646 7650 return (mask);
7647 7651
7648 7652 maskp[1] = 0xFF;
7649 7653 if ((up[0] & 0xC0) == 0x80)
7650 7654 return (mask);
7651 7655
7652 7656 maskp[2] = 0xFF;
7653 7657 if ((up[0] & 0xE0) == 0xC0)
7654 7658 return (mask);
7655 7659
7656 7660 /* Otherwise return no mask */
7657 7661 return ((ipaddr_t)0);
7658 7662 }
7659 7663
7660 7664 /*
7661 7665 * Select an ill for the packet by considering load spreading across
7662 7666 * a different ill in the group if dst_ill is part of some group.
7663 7667 */
7664 7668 ill_t *
7665 7669 ip_newroute_get_dst_ill(ill_t *dst_ill)
7666 7670 {
7667 7671 ill_t *ill;
7668 7672
7669 7673 /*
7670 7674 * We schedule irrespective of whether the source address is
7671 7675 * INADDR_ANY or not. illgrp_scheduler returns a held ill.
7672 7676 */
7673 7677 ill = illgrp_scheduler(dst_ill);
7674 7678 if (ill == NULL)
7675 7679 return (NULL);
7676 7680
7677 7681 /*
7678 7682 * For groups with names ip_sioctl_groupname ensures that all
7679 7683 * ills are of same type. For groups without names, ifgrp_insert
7680 7684 * ensures this.
7681 7685 */
7682 7686 ASSERT(dst_ill->ill_type == ill->ill_type);
7683 7687
7684 7688 return (ill);
7685 7689 }
7686 7690
7687 7691 /*
7688 7692 * Helper function for the IPIF_NOFAILOVER/ATTACH_IF interface attachment case.
7689 7693 */
7690 7694 ill_t *
7691 7695 ip_grab_attach_ill(ill_t *ill, mblk_t *first_mp, int ifindex, boolean_t isv6,
7692 7696 ip_stack_t *ipst)
7693 7697 {
7694 7698 ill_t *ret_ill;
7695 7699
7696 7700 ASSERT(ifindex != 0);
7697 7701 ret_ill = ill_lookup_on_ifindex(ifindex, isv6, NULL, NULL, NULL, NULL,
7698 7702 ipst);
7699 7703 if (ret_ill == NULL ||
7700 7704 (ret_ill->ill_phyint->phyint_flags & PHYI_OFFLINE)) {
7701 7705 if (isv6) {
7702 7706 if (ill != NULL) {
7703 7707 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
7704 7708 } else {
7705 7709 BUMP_MIB(&ipst->ips_ip6_mib,
7706 7710 ipIfStatsOutDiscards);
7707 7711 }
7708 7712 ip1dbg(("ip_grab_attach_ill (IPv6): "
7709 7713 "bad ifindex %d.\n", ifindex));
7710 7714 } else {
7711 7715 if (ill != NULL) {
7712 7716 BUMP_MIB(ill->ill_ip_mib, ipIfStatsOutDiscards);
7713 7717 } else {
7714 7718 BUMP_MIB(&ipst->ips_ip_mib,
7715 7719 ipIfStatsOutDiscards);
7716 7720 }
7717 7721 ip1dbg(("ip_grab_attach_ill (IPv4): "
7718 7722 "bad ifindex %d.\n", ifindex));
7719 7723 }
7720 7724 if (ret_ill != NULL)
7721 7725 ill_refrele(ret_ill);
7722 7726 freemsg(first_mp);
7723 7727 return (NULL);
7724 7728 }
7725 7729
7726 7730 return (ret_ill);
7727 7731 }
7728 7732
7729 7733 /*
7730 7734 * IPv4 -
7731 7735 * ip_newroute is called by ip_rput or ip_wput whenever we need to send
7732 7736 * out a packet to a destination address for which we do not have specific
7733 7737 * (or sufficient) routing information.
7734 7738 *
7735 7739 * NOTE : These are the scopes of some of the variables that point at IRE,
7736 7740 * which needs to be followed while making any future modifications
7737 7741 * to avoid memory leaks.
7738 7742 *
7739 7743 * - ire and sire are the entries looked up initially by
7740 7744 * ire_ftable_lookup.
7741 7745 * - ipif_ire is used to hold the interface ire associated with
7742 7746 * the new cache ire. But it's scope is limited, so we always REFRELE
7743 7747 * it before branching out to error paths.
7744 7748 * - save_ire is initialized before ire_create, so that ire returned
7745 7749 * by ire_create will not over-write the ire. We REFRELE save_ire
7746 7750 * before breaking out of the switch.
7747 7751 *
7748 7752 * Thus on failures, we have to REFRELE only ire and sire, if they
7749 7753 * are not NULL.
7750 7754 */
7751 7755 void
7752 7756 ip_newroute(queue_t *q, mblk_t *mp, ipaddr_t dst, conn_t *connp,
7753 7757 zoneid_t zoneid, ip_stack_t *ipst)
7754 7758 {
7755 7759 areq_t *areq;
7756 7760 ipaddr_t gw = 0;
7757 7761 ire_t *ire = NULL;
7758 7762 mblk_t *res_mp;
7759 7763 ipaddr_t *addrp;
7760 7764 ipaddr_t nexthop_addr;
7761 7765 ipif_t *src_ipif = NULL;
7762 7766 ill_t *dst_ill = NULL;
7763 7767 ipha_t *ipha;
7764 7768 ire_t *sire = NULL;
7765 7769 mblk_t *first_mp;
7766 7770 ire_t *save_ire;
7767 7771 ill_t *attach_ill = NULL; /* Bind to IPIF_NOFAILOVER address */
7768 7772 ushort_t ire_marks = 0;
7769 7773 boolean_t mctl_present;
7770 7774 ipsec_out_t *io;
7771 7775 mblk_t *saved_mp;
7772 7776 ire_t *first_sire = NULL;
7773 7777 mblk_t *copy_mp = NULL;
7774 7778 mblk_t *xmit_mp = NULL;
7775 7779 ipaddr_t save_dst;
7776 7780 uint32_t multirt_flags =
7777 7781 MULTIRT_CACHEGW | MULTIRT_USESTAMP | MULTIRT_SETSTAMP;
7778 7782 boolean_t multirt_is_resolvable;
7779 7783 boolean_t multirt_resolve_next;
7780 7784 boolean_t unspec_src;
7781 7785 boolean_t do_attach_ill = B_FALSE;
7782 7786 boolean_t ip_nexthop = B_FALSE;
7783 7787 tsol_ire_gw_secattr_t *attrp = NULL;
7784 7788 tsol_gcgrp_t *gcgrp = NULL;
7785 7789 tsol_gcgrp_addr_t ga;
7786 7790
7787 7791 if (ip_debug > 2) {
7788 7792 /* ip1dbg */
7789 7793 pr_addr_dbg("ip_newroute: dst %s\n", AF_INET, &dst);
7790 7794 }
7791 7795
7792 7796 EXTRACT_PKT_MP(mp, first_mp, mctl_present);
7793 7797 if (mctl_present) {
7794 7798 io = (ipsec_out_t *)first_mp->b_rptr;
7795 7799 ASSERT(io->ipsec_out_type == IPSEC_OUT);
7796 7800 ASSERT(zoneid == io->ipsec_out_zoneid);
7797 7801 ASSERT(zoneid != ALL_ZONES);
7798 7802 }
7799 7803
7800 7804 ipha = (ipha_t *)mp->b_rptr;
7801 7805
7802 7806 /* All multicast lookups come through ip_newroute_ipif() */
7803 7807 if (CLASSD(dst)) {
7804 7808 ip0dbg(("ip_newroute: CLASSD 0x%x (b_prev %p, b_next %p)\n",
7805 7809 ntohl(dst), (void *)mp->b_prev, (void *)mp->b_next));
7806 7810 freemsg(first_mp);
7807 7811 return;
7808 7812 }
7809 7813
7810 7814 if (mctl_present && io->ipsec_out_attach_if) {
7811 7815 /* ip_grab_attach_ill returns a held ill */
7812 7816 attach_ill = ip_grab_attach_ill(NULL, first_mp,
7813 7817 io->ipsec_out_ill_index, B_FALSE, ipst);
7814 7818
7815 7819 /* Failure case frees things for us. */
7816 7820 if (attach_ill == NULL)
7817 7821 return;
7818 7822
7819 7823 /*
7820 7824 * Check if we need an ire that will not be
7821 7825 * looked up by anybody else i.e. HIDDEN.
7822 7826 */
7823 7827 if (ill_is_probeonly(attach_ill))
7824 7828 ire_marks = IRE_MARK_HIDDEN;
7825 7829 }
7826 7830 if (mctl_present && io->ipsec_out_ip_nexthop) {
7827 7831 ip_nexthop = B_TRUE;
7828 7832 nexthop_addr = io->ipsec_out_nexthop_addr;
7829 7833 }
7830 7834 /*
7831 7835 * If this IRE is created for forwarding or it is not for
7832 7836 * traffic for congestion controlled protocols, mark it as temporary.
7833 7837 */
7834 7838 if (mp->b_prev != NULL || !IP_FLOW_CONTROLLED_ULP(ipha->ipha_protocol))
7835 7839 ire_marks |= IRE_MARK_TEMPORARY;
7836 7840
7837 7841 /*
7838 7842 * Get what we can from ire_ftable_lookup which will follow an IRE
7839 7843 * chain until it gets the most specific information available.
7840 7844 * For example, we know that there is no IRE_CACHE for this dest,
7841 7845 * but there may be an IRE_OFFSUBNET which specifies a gateway.
7842 7846 * ire_ftable_lookup will look up the gateway, etc.
7843 7847 * Otherwise, given ire_ftable_lookup algorithm, only one among routes
7844 7848 * to the destination, of equal netmask length in the forward table,
7845 7849 * will be recursively explored. If no information is available
7846 7850 * for the final gateway of that route, we force the returned ire
7847 7851 * to be equal to sire using MATCH_IRE_PARENT.
7848 7852 * At least, in this case we have a starting point (in the buckets)
7849 7853 * to look for other routes to the destination in the forward table.
7850 7854 * This is actually used only for multirouting, where a list
7851 7855 * of routes has to be processed in sequence.
7852 7856 *
7853 7857 * In the process of coming up with the most specific information,
7854 7858 * ire_ftable_lookup may end up with an incomplete IRE_CACHE entry
7855 7859 * for the gateway (i.e., one for which the ire_nce->nce_state is
7856 7860 * not yet ND_REACHABLE, and is in the middle of arp resolution).
7857 7861 * Two caveats when handling incomplete ire's in ip_newroute:
7858 7862 * - we should be careful when accessing its ire_nce (specifically
7859 7863 * the nce_res_mp) ast it might change underneath our feet, and,
7860 7864 * - not all legacy code path callers are prepared to handle
7861 7865 * incomplete ire's, so we should not create/add incomplete
7862 7866 * ire_cache entries here. (See discussion about temporary solution
7863 7867 * further below).
7864 7868 *
7865 7869 * In order to minimize packet dropping, and to preserve existing
7866 7870 * behavior, we treat this case as if there were no IRE_CACHE for the
7867 7871 * gateway, and instead use the IF_RESOLVER ire to send out
7868 7872 * another request to ARP (this is achieved by passing the
7869 7873 * MATCH_IRE_COMPLETE flag to ire_ftable_lookup). When the
7870 7874 * arp response comes back in ip_wput_nondata, we will create
7871 7875 * a per-dst ire_cache that has an ND_COMPLETE ire.
7872 7876 *
7873 7877 * Note that this is a temporary solution; the correct solution is
7874 7878 * to create an incomplete per-dst ire_cache entry, and send the
7875 7879 * packet out when the gw's nce is resolved. In order to achieve this,
7876 7880 * all packet processing must have been completed prior to calling
7877 7881 * ire_add_then_send. Some legacy code paths (e.g. cgtp) would need
7878 7882 * to be modified to accomodate this solution.
7879 7883 */
7880 7884 if (ip_nexthop) {
7881 7885 /*
7882 7886 * The first time we come here, we look for an IRE_INTERFACE
7883 7887 * entry for the specified nexthop, set the dst to be the
7884 7888 * nexthop address and create an IRE_CACHE entry for the
7885 7889 * nexthop. The next time around, we are able to find an
7886 7890 * IRE_CACHE entry for the nexthop, set the gateway to be the
7887 7891 * nexthop address and create an IRE_CACHE entry for the
7888 7892 * destination address via the specified nexthop.
7889 7893 */
7890 7894 ire = ire_cache_lookup(nexthop_addr, zoneid,
7891 7895 MBLK_GETLABEL(mp), ipst);
7892 7896 if (ire != NULL) {
7893 7897 gw = nexthop_addr;
7894 7898 ire_marks |= IRE_MARK_PRIVATE_ADDR;
7895 7899 } else {
7896 7900 ire = ire_ftable_lookup(nexthop_addr, 0, 0,
7897 7901 IRE_INTERFACE, NULL, NULL, zoneid, 0,
7898 7902 MBLK_GETLABEL(mp),
7899 7903 MATCH_IRE_TYPE | MATCH_IRE_SECATTR,
7900 7904 ipst);
7901 7905 if (ire != NULL) {
7902 7906 dst = nexthop_addr;
7903 7907 }
7904 7908 }
7905 7909 } else if (attach_ill == NULL) {
7906 7910 ire = ire_ftable_lookup(dst, 0, 0, 0,
7907 7911 NULL, &sire, zoneid, 0, MBLK_GETLABEL(mp),
7908 7912 MATCH_IRE_RECURSIVE | MATCH_IRE_DEFAULT |
7909 7913 MATCH_IRE_RJ_BHOLE | MATCH_IRE_PARENT |
7910 7914 MATCH_IRE_SECATTR | MATCH_IRE_COMPLETE,
7911 7915 ipst);
7912 7916 } else {
7913 7917 /*
7914 7918 * attach_ill is set only for communicating with
7915 7919 * on-link hosts. So, don't look for DEFAULT.
7916 7920 */
7917 7921 ipif_t *attach_ipif;
7918 7922
7919 7923 attach_ipif = ipif_get_next_ipif(NULL, attach_ill);
7920 7924 if (attach_ipif == NULL) {
7921 7925 ill_refrele(attach_ill);
7922 7926 goto icmp_err_ret;
7923 7927 }
7924 7928 ire = ire_ftable_lookup(dst, 0, 0, 0, attach_ipif,
7925 7929 &sire, zoneid, 0, MBLK_GETLABEL(mp),
7926 7930 MATCH_IRE_RJ_BHOLE | MATCH_IRE_ILL |
7927 7931 MATCH_IRE_SECATTR, ipst);
7928 7932 ipif_refrele(attach_ipif);
7929 7933 }
7930 7934 ip3dbg(("ip_newroute: ire_ftable_lookup() "
7931 7935 "returned ire %p, sire %p\n", (void *)ire, (void *)sire));
7932 7936
7933 7937 /*
7934 7938 * This loop is run only once in most cases.
7935 7939 * We loop to resolve further routes only when the destination
7936 7940 * can be reached through multiple RTF_MULTIRT-flagged ires.
7937 7941 */
7938 7942 do {
7939 7943 /* Clear the previous iteration's values */
7940 7944 if (src_ipif != NULL) {
7941 7945 ipif_refrele(src_ipif);
7942 7946 src_ipif = NULL;
7943 7947 }
7944 7948 if (dst_ill != NULL) {
7945 7949 ill_refrele(dst_ill);
7946 7950 dst_ill = NULL;
7947 7951 }
7948 7952
7949 7953 multirt_resolve_next = B_FALSE;
7950 7954 /*
7951 7955 * We check if packets have to be multirouted.
7952 7956 * In this case, given the current <ire, sire> couple,
7953 7957 * we look for the next suitable <ire, sire>.
7954 7958 * This check is done in ire_multirt_lookup(),
7955 7959 * which applies various criteria to find the next route
7956 7960 * to resolve. ire_multirt_lookup() leaves <ire, sire>
7957 7961 * unchanged if it detects it has not been tried yet.
7958 7962 */
7959 7963 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) {
7960 7964 ip3dbg(("ip_newroute: starting next_resolution "
7961 7965 "with first_mp %p, tag %d\n",
7962 7966 (void *)first_mp,
7963 7967 MULTIRT_DEBUG_TAGGED(first_mp)));
7964 7968
7965 7969 ASSERT(sire != NULL);
7966 7970 multirt_is_resolvable =
7967 7971 ire_multirt_lookup(&ire, &sire, multirt_flags,
7968 7972 MBLK_GETLABEL(mp), ipst);
7969 7973
7970 7974 ip3dbg(("ip_newroute: multirt_is_resolvable %d, "
7971 7975 "ire %p, sire %p\n",
7972 7976 multirt_is_resolvable,
7973 7977 (void *)ire, (void *)sire));
7974 7978
7975 7979 if (!multirt_is_resolvable) {
7976 7980 /*
7977 7981 * No more multirt route to resolve; give up
7978 7982 * (all routes resolved or no more
7979 7983 * resolvable routes).
7980 7984 */
7981 7985 if (ire != NULL) {
7982 7986 ire_refrele(ire);
7983 7987 ire = NULL;
7984 7988 }
7985 7989 } else {
7986 7990 ASSERT(sire != NULL);
7987 7991 ASSERT(ire != NULL);
7988 7992 /*
7989 7993 * We simply use first_sire as a flag that
7990 7994 * indicates if a resolvable multirt route
7991 7995 * has already been found.
7992 7996 * If it is not the case, we may have to send
7993 7997 * an ICMP error to report that the
7994 7998 * destination is unreachable.
7995 7999 * We do not IRE_REFHOLD first_sire.
7996 8000 */
7997 8001 if (first_sire == NULL) {
7998 8002 first_sire = sire;
7999 8003 }
8000 8004 }
8001 8005 }
8002 8006 if (ire == NULL) {
8003 8007 if (ip_debug > 3) {
8004 8008 /* ip2dbg */
8005 8009 pr_addr_dbg("ip_newroute: "
8006 8010 "can't resolve %s\n", AF_INET, &dst);
8007 8011 }
8008 8012 ip3dbg(("ip_newroute: "
8009 8013 "ire %p, sire %p, first_sire %p\n",
8010 8014 (void *)ire, (void *)sire, (void *)first_sire));
8011 8015
8012 8016 if (sire != NULL) {
8013 8017 ire_refrele(sire);
8014 8018 sire = NULL;
8015 8019 }
8016 8020
8017 8021 if (first_sire != NULL) {
8018 8022 /*
8019 8023 * At least one multirt route has been found
8020 8024 * in the same call to ip_newroute();
8021 8025 * there is no need to report an ICMP error.
8022 8026 * first_sire was not IRE_REFHOLDed.
8023 8027 */
8024 8028 MULTIRT_DEBUG_UNTAG(first_mp);
8025 8029 freemsg(first_mp);
8026 8030 return;
8027 8031 }
8028 8032 ip_rts_change(RTM_MISS, dst, 0, 0, 0, 0, 0, 0,
8029 8033 RTA_DST, ipst);
8030 8034 if (attach_ill != NULL)
8031 8035 ill_refrele(attach_ill);
8032 8036 goto icmp_err_ret;
8033 8037 }
8034 8038
8035 8039 /*
8036 8040 * Verify that the returned IRE does not have either
8037 8041 * the RTF_REJECT or RTF_BLACKHOLE flags set and that the IRE is
8038 8042 * either an IRE_CACHE, IRE_IF_NORESOLVER or IRE_IF_RESOLVER.
8039 8043 */
8040 8044 if ((ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE)) ||
8041 8045 (ire->ire_type & (IRE_CACHE | IRE_INTERFACE)) == 0) {
8042 8046 if (attach_ill != NULL)
8043 8047 ill_refrele(attach_ill);
8044 8048 goto icmp_err_ret;
8045 8049 }
8046 8050 /*
8047 8051 * Increment the ire_ob_pkt_count field for ire if it is an
8048 8052 * INTERFACE (IF_RESOLVER or IF_NORESOLVER) IRE type, and
8049 8053 * increment the same for the parent IRE, sire, if it is some
8050 8054 * sort of prefix IRE (which includes DEFAULT, PREFIX, and HOST)
8051 8055 */
8052 8056 if ((ire->ire_type & IRE_INTERFACE) != 0) {
8053 8057 UPDATE_OB_PKT_COUNT(ire);
8054 8058 ire->ire_last_used_time = lbolt;
8055 8059 }
8056 8060
8057 8061 if (sire != NULL) {
8058 8062 gw = sire->ire_gateway_addr;
8059 8063 ASSERT((sire->ire_type & (IRE_CACHETABLE |
8060 8064 IRE_INTERFACE)) == 0);
8061 8065 UPDATE_OB_PKT_COUNT(sire);
8062 8066 sire->ire_last_used_time = lbolt;
8063 8067 }
8064 8068 /*
8065 8069 * We have a route to reach the destination.
8066 8070 *
8067 8071 * 1) If the interface is part of ill group, try to get a new
8068 8072 * ill taking load spreading into account.
8069 8073 *
8070 8074 * 2) After selecting the ill, get a source address that
8071 8075 * might create good inbound load spreading.
8072 8076 * ipif_select_source does this for us.
8073 8077 *
8074 8078 * If the application specified the ill (ifindex), we still
8075 8079 * load spread. Only if the packets needs to go out
8076 8080 * specifically on a given ill e.g. binding to
8077 8081 * IPIF_NOFAILOVER address, then we don't try to use a
8078 8082 * different ill for load spreading.
8079 8083 */
8080 8084 if (attach_ill == NULL) {
8081 8085 /*
8082 8086 * Don't perform outbound load spreading in the
8083 8087 * case of an RTF_MULTIRT route, as we actually
8084 8088 * typically want to replicate outgoing packets
8085 8089 * through particular interfaces.
8086 8090 */
8087 8091 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) {
8088 8092 dst_ill = ire->ire_ipif->ipif_ill;
8089 8093 /* for uniformity */
8090 8094 ill_refhold(dst_ill);
8091 8095 } else {
8092 8096 /*
8093 8097 * If we are here trying to create an IRE_CACHE
8094 8098 * for an offlink destination and have the
8095 8099 * IRE_CACHE for the next hop and the latter is
8096 8100 * using virtual IP source address selection i.e
8097 8101 * it's ire->ire_ipif is pointing to a virtual
8098 8102 * network interface (vni) then
8099 8103 * ip_newroute_get_dst_ll() will return the vni
8100 8104 * interface as the dst_ill. Since the vni is
8101 8105 * virtual i.e not associated with any physical
8102 8106 * interface, it cannot be the dst_ill, hence
8103 8107 * in such a case call ip_newroute_get_dst_ll()
8104 8108 * with the stq_ill instead of the ire_ipif ILL.
8105 8109 * The function returns a refheld ill.
8106 8110 */
8107 8111 if ((ire->ire_type == IRE_CACHE) &&
8108 8112 IS_VNI(ire->ire_ipif->ipif_ill))
8109 8113 dst_ill = ip_newroute_get_dst_ill(
8110 8114 ire->ire_stq->q_ptr);
8111 8115 else
8112 8116 dst_ill = ip_newroute_get_dst_ill(
8113 8117 ire->ire_ipif->ipif_ill);
8114 8118 }
8115 8119 if (dst_ill == NULL) {
8116 8120 if (ip_debug > 2) {
8117 8121 pr_addr_dbg("ip_newroute: "
8118 8122 "no dst ill for dst"
8119 8123 " %s\n", AF_INET, &dst);
8120 8124 }
8121 8125 goto icmp_err_ret;
8122 8126 }
8123 8127 } else {
8124 8128 dst_ill = ire->ire_ipif->ipif_ill;
8125 8129 /* for uniformity */
8126 8130 ill_refhold(dst_ill);
8127 8131 /*
8128 8132 * We should have found a route matching ill as we
8129 8133 * called ire_ftable_lookup with MATCH_IRE_ILL.
8130 8134 * Rather than asserting, when there is a mismatch,
8131 8135 * we just drop the packet.
8132 8136 */
8133 8137 if (dst_ill != attach_ill) {
8134 8138 ip0dbg(("ip_newroute: Packet dropped as "
8135 8139 "IPIF_NOFAILOVER ill is %s, "
8136 8140 "ire->ire_ipif->ipif_ill is %s\n",
8137 8141 attach_ill->ill_name,
8138 8142 dst_ill->ill_name));
8139 8143 ill_refrele(attach_ill);
8140 8144 goto icmp_err_ret;
8141 8145 }
8142 8146 }
8143 8147 /* attach_ill can't go in loop. IPMP and CGTP are disjoint */
8144 8148 if (attach_ill != NULL) {
8145 8149 ill_refrele(attach_ill);
8146 8150 attach_ill = NULL;
8147 8151 do_attach_ill = B_TRUE;
8148 8152 }
8149 8153 ASSERT(dst_ill != NULL);
8150 8154 ip2dbg(("ip_newroute: dst_ill %s\n", dst_ill->ill_name));
8151 8155
8152 8156 /*
8153 8157 * Pick the best source address from dst_ill.
8154 8158 *
8155 8159 * 1) If it is part of a multipathing group, we would
8156 8160 * like to spread the inbound packets across different
8157 8161 * interfaces. ipif_select_source picks a random source
8158 8162 * across the different ills in the group.
8159 8163 *
8160 8164 * 2) If it is not part of a multipathing group, we try
8161 8165 * to pick the source address from the destination
8162 8166 * route. Clustering assumes that when we have multiple
8163 8167 * prefixes hosted on an interface, the prefix of the
8164 8168 * source address matches the prefix of the destination
8165 8169 * route. We do this only if the address is not
8166 8170 * DEPRECATED.
8167 8171 *
8168 8172 * 3) If the conn is in a different zone than the ire, we
8169 8173 * need to pick a source address from the right zone.
8170 8174 *
8171 8175 * NOTE : If we hit case (1) above, the prefix of the source
8172 8176 * address picked may not match the prefix of the
8173 8177 * destination routes prefix as ipif_select_source
8174 8178 * does not look at "dst" while picking a source
8175 8179 * address.
8176 8180 * If we want the same behavior as (2), we will need
8177 8181 * to change the behavior of ipif_select_source.
8178 8182 */
8179 8183 ASSERT(src_ipif == NULL);
8180 8184 if ((sire != NULL) && (sire->ire_flags & RTF_SETSRC)) {
8181 8185 /*
8182 8186 * The RTF_SETSRC flag is set in the parent ire (sire).
8183 8187 * Check that the ipif matching the requested source
8184 8188 * address still exists.
8185 8189 */
8186 8190 src_ipif = ipif_lookup_addr(sire->ire_src_addr, NULL,
8187 8191 zoneid, NULL, NULL, NULL, NULL, ipst);
8188 8192 }
8189 8193
8190 8194 unspec_src = (connp != NULL && connp->conn_unspec_src);
8191 8195
8192 8196 if (src_ipif == NULL &&
8193 8197 (!unspec_src || ipha->ipha_src != INADDR_ANY)) {
8194 8198 ire_marks |= IRE_MARK_USESRC_CHECK;
8195 8199 if ((dst_ill->ill_group != NULL) ||
8196 8200 (ire->ire_ipif->ipif_flags & IPIF_DEPRECATED) ||
8197 8201 (connp != NULL && ire->ire_zoneid != zoneid &&
8198 8202 ire->ire_zoneid != ALL_ZONES) ||
8199 8203 (dst_ill->ill_usesrc_ifindex != 0)) {
8200 8204 /*
8201 8205 * If the destination is reachable via a
8202 8206 * given gateway, the selected source address
8203 8207 * should be in the same subnet as the gateway.
8204 8208 * Otherwise, the destination is not reachable.
8205 8209 *
8206 8210 * If there are no interfaces on the same subnet
8207 8211 * as the destination, ipif_select_source gives
8208 8212 * first non-deprecated interface which might be
8209 8213 * on a different subnet than the gateway.
8210 8214 * This is not desirable. Hence pass the dst_ire
8211 8215 * source address to ipif_select_source.
8212 8216 * It is sure that the destination is reachable
8213 8217 * with the dst_ire source address subnet.
8214 8218 * So passing dst_ire source address to
8215 8219 * ipif_select_source will make sure that the
8216 8220 * selected source will be on the same subnet
8217 8221 * as dst_ire source address.
8218 8222 */
8219 8223 ipaddr_t saddr = ire->ire_ipif->ipif_src_addr;
8220 8224 src_ipif = ipif_select_source(dst_ill, saddr,
8221 8225 zoneid);
8222 8226 if (src_ipif == NULL) {
8223 8227 if (ip_debug > 2) {
8224 8228 pr_addr_dbg("ip_newroute: "
8225 8229 "no src for dst %s ",
8226 8230 AF_INET, &dst);
8227 8231 printf("through interface %s\n",
8228 8232 dst_ill->ill_name);
8229 8233 }
8230 8234 goto icmp_err_ret;
8231 8235 }
8232 8236 } else {
8233 8237 src_ipif = ire->ire_ipif;
8234 8238 ASSERT(src_ipif != NULL);
8235 8239 /* hold src_ipif for uniformity */
8236 8240 ipif_refhold(src_ipif);
8237 8241 }
8238 8242 }
8239 8243
8240 8244 /*
8241 8245 * Assign a source address while we have the conn.
8242 8246 * We can't have ip_wput_ire pick a source address when the
8243 8247 * packet returns from arp since we need to look at
8244 8248 * conn_unspec_src and conn_zoneid, and we lose the conn when
8245 8249 * going through arp.
8246 8250 *
8247 8251 * NOTE : ip_newroute_v6 does not have this piece of code as
8248 8252 * it uses ip6i to store this information.
8249 8253 */
8250 8254 if (ipha->ipha_src == INADDR_ANY && !unspec_src)
8251 8255 ipha->ipha_src = src_ipif->ipif_src_addr;
8252 8256
8253 8257 if (ip_debug > 3) {
8254 8258 /* ip2dbg */
8255 8259 pr_addr_dbg("ip_newroute: first hop %s\n",
8256 8260 AF_INET, &gw);
8257 8261 }
8258 8262 ip2dbg(("\tire type %s (%d)\n",
8259 8263 ip_nv_lookup(ire_nv_tbl, ire->ire_type), ire->ire_type));
8260 8264
8261 8265 /*
8262 8266 * The TTL of multirouted packets is bounded by the
8263 8267 * ip_multirt_ttl ndd variable.
8264 8268 */
8265 8269 if ((sire != NULL) && (sire->ire_flags & RTF_MULTIRT)) {
8266 8270 /* Force TTL of multirouted packets */
8267 8271 if ((ipst->ips_ip_multirt_ttl > 0) &&
8268 8272 (ipha->ipha_ttl > ipst->ips_ip_multirt_ttl)) {
8269 8273 ip2dbg(("ip_newroute: forcing multirt TTL "
8270 8274 "to %d (was %d), dst 0x%08x\n",
8271 8275 ipst->ips_ip_multirt_ttl, ipha->ipha_ttl,
8272 8276 ntohl(sire->ire_addr)));
8273 8277 ipha->ipha_ttl = ipst->ips_ip_multirt_ttl;
8274 8278 }
8275 8279 }
8276 8280 /*
8277 8281 * At this point in ip_newroute(), ire is either the
8278 8282 * IRE_CACHE of the next-hop gateway for an off-subnet
8279 8283 * destination or an IRE_INTERFACE type that should be used
8280 8284 * to resolve an on-subnet destination or an on-subnet
8281 8285 * next-hop gateway.
8282 8286 *
8283 8287 * In the IRE_CACHE case, we have the following :
8284 8288 *
8285 8289 * 1) src_ipif - used for getting a source address.
8286 8290 *
8287 8291 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This
8288 8292 * means packets using this IRE_CACHE will go out on
8289 8293 * dst_ill.
8290 8294 *
8291 8295 * 3) The IRE sire will point to the prefix that is the
8292 8296 * longest matching route for the destination. These
8293 8297 * prefix types include IRE_DEFAULT, IRE_PREFIX, IRE_HOST.
8294 8298 *
8295 8299 * The newly created IRE_CACHE entry for the off-subnet
8296 8300 * destination is tied to both the prefix route and the
8297 8301 * interface route used to resolve the next-hop gateway
8298 8302 * via the ire_phandle and ire_ihandle fields,
8299 8303 * respectively.
8300 8304 *
8301 8305 * In the IRE_INTERFACE case, we have the following :
8302 8306 *
8303 8307 * 1) src_ipif - used for getting a source address.
8304 8308 *
8305 8309 * 2) dst_ill - from which we derive ire_stq/ire_rfq. This
8306 8310 * means packets using the IRE_CACHE that we will build
8307 8311 * here will go out on dst_ill.
8308 8312 *
8309 8313 * 3) sire may or may not be NULL. But, the IRE_CACHE that is
8310 8314 * to be created will only be tied to the IRE_INTERFACE
8311 8315 * that was derived from the ire_ihandle field.
8312 8316 *
8313 8317 * If sire is non-NULL, it means the destination is
8314 8318 * off-link and we will first create the IRE_CACHE for the
8315 8319 * gateway. Next time through ip_newroute, we will create
8316 8320 * the IRE_CACHE for the final destination as described
8317 8321 * above.
8318 8322 *
8319 8323 * In both cases, after the current resolution has been
8320 8324 * completed (or possibly initialised, in the IRE_INTERFACE
8321 8325 * case), the loop may be re-entered to attempt the resolution
8322 8326 * of another RTF_MULTIRT route.
8323 8327 *
8324 8328 * When an IRE_CACHE entry for the off-subnet destination is
8325 8329 * created, RTF_SETSRC and RTF_MULTIRT are inherited from sire,
8326 8330 * for further processing in emission loops.
8327 8331 */
8328 8332 save_ire = ire;
8329 8333 switch (ire->ire_type) {
8330 8334 case IRE_CACHE: {
8331 8335 ire_t *ipif_ire;
8332 8336
8333 8337 ASSERT(save_ire->ire_nce->nce_state == ND_REACHABLE);
8334 8338 if (gw == 0)
8335 8339 gw = ire->ire_gateway_addr;
8336 8340 /*
8337 8341 * We need 3 ire's to create a new cache ire for an
8338 8342 * off-link destination from the cache ire of the
8339 8343 * gateway.
8340 8344 *
8341 8345 * 1. The prefix ire 'sire' (Note that this does
8342 8346 * not apply to the conn_nexthop_set case)
8343 8347 * 2. The cache ire of the gateway 'ire'
8344 8348 * 3. The interface ire 'ipif_ire'
8345 8349 *
8346 8350 * We have (1) and (2). We lookup (3) below.
8347 8351 *
8348 8352 * If there is no interface route to the gateway,
8349 8353 * it is a race condition, where we found the cache
8350 8354 * but the interface route has been deleted.
8351 8355 */
8352 8356 if (ip_nexthop) {
8353 8357 ipif_ire = ire_ihandle_lookup_onlink(ire);
8354 8358 } else {
8355 8359 ipif_ire =
8356 8360 ire_ihandle_lookup_offlink(ire, sire);
8357 8361 }
8358 8362 if (ipif_ire == NULL) {
8359 8363 ip1dbg(("ip_newroute: "
8360 8364 "ire_ihandle_lookup_offlink failed\n"));
8361 8365 goto icmp_err_ret;
8362 8366 }
8363 8367
8364 8368 /*
8365 8369 * Check cached gateway IRE for any security
8366 8370 * attributes; if found, associate the gateway
8367 8371 * credentials group to the destination IRE.
8368 8372 */
8369 8373 if ((attrp = save_ire->ire_gw_secattr) != NULL) {
8370 8374 mutex_enter(&attrp->igsa_lock);
8371 8375 if ((gcgrp = attrp->igsa_gcgrp) != NULL)
8372 8376 GCGRP_REFHOLD(gcgrp);
8373 8377 mutex_exit(&attrp->igsa_lock);
8374 8378 }
8375 8379
8376 8380 /*
8377 8381 * XXX For the source of the resolver mp,
8378 8382 * we are using the same DL_UNITDATA_REQ
8379 8383 * (from save_ire->ire_nce->nce_res_mp)
8380 8384 * though the save_ire is not pointing at the same ill.
8381 8385 * This is incorrect. We need to send it up to the
8382 8386 * resolver to get the right res_mp. For ethernets
8383 8387 * this may be okay (ill_type == DL_ETHER).
8384 8388 */
8385 8389
8386 8390 ire = ire_create(
8387 8391 (uchar_t *)&dst, /* dest address */
8388 8392 (uchar_t *)&ip_g_all_ones, /* mask */
8389 8393 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
8390 8394 (uchar_t *)&gw, /* gateway address */
8391 8395 &save_ire->ire_max_frag,
8392 8396 save_ire->ire_nce, /* src nce */
8393 8397 dst_ill->ill_rq, /* recv-from queue */
8394 8398 dst_ill->ill_wq, /* send-to queue */
8395 8399 IRE_CACHE, /* IRE type */
8396 8400 src_ipif,
8397 8401 (sire != NULL) ?
8398 8402 sire->ire_mask : 0, /* Parent mask */
8399 8403 (sire != NULL) ?
8400 8404 sire->ire_phandle : 0, /* Parent handle */
8401 8405 ipif_ire->ire_ihandle, /* Interface handle */
8402 8406 (sire != NULL) ? (sire->ire_flags &
8403 8407 (RTF_SETSRC | RTF_MULTIRT)) : 0, /* flags */
8404 8408 (sire != NULL) ?
8405 8409 &(sire->ire_uinfo) : &(save_ire->ire_uinfo),
8406 8410 NULL,
8407 8411 gcgrp,
8408 8412 ipst);
8409 8413
8410 8414 if (ire == NULL) {
8411 8415 if (gcgrp != NULL) {
8412 8416 GCGRP_REFRELE(gcgrp);
8413 8417 gcgrp = NULL;
8414 8418 }
8415 8419 ire_refrele(ipif_ire);
8416 8420 ire_refrele(save_ire);
8417 8421 break;
8418 8422 }
8419 8423
8420 8424 /* reference now held by IRE */
8421 8425 gcgrp = NULL;
8422 8426
8423 8427 ire->ire_marks |= ire_marks;
8424 8428
8425 8429 /*
8426 8430 * Prevent sire and ipif_ire from getting deleted.
8427 8431 * The newly created ire is tied to both of them via
8428 8432 * the phandle and ihandle respectively.
8429 8433 */
8430 8434 if (sire != NULL) {
8431 8435 IRB_REFHOLD(sire->ire_bucket);
8432 8436 /* Has it been removed already ? */
8433 8437 if (sire->ire_marks & IRE_MARK_CONDEMNED) {
8434 8438 IRB_REFRELE(sire->ire_bucket);
8435 8439 ire_refrele(ipif_ire);
8436 8440 ire_refrele(save_ire);
8437 8441 break;
8438 8442 }
8439 8443 }
8440 8444
8441 8445 IRB_REFHOLD(ipif_ire->ire_bucket);
8442 8446 /* Has it been removed already ? */
8443 8447 if (ipif_ire->ire_marks & IRE_MARK_CONDEMNED) {
8444 8448 IRB_REFRELE(ipif_ire->ire_bucket);
8445 8449 if (sire != NULL)
8446 8450 IRB_REFRELE(sire->ire_bucket);
8447 8451 ire_refrele(ipif_ire);
8448 8452 ire_refrele(save_ire);
8449 8453 break;
8450 8454 }
8451 8455
8452 8456 xmit_mp = first_mp;
8453 8457 /*
8454 8458 * In the case of multirouting, a copy
8455 8459 * of the packet is done before its sending.
8456 8460 * The copy is used to attempt another
8457 8461 * route resolution, in a next loop.
8458 8462 */
8459 8463 if (ire->ire_flags & RTF_MULTIRT) {
8460 8464 copy_mp = copymsg(first_mp);
8461 8465 if (copy_mp != NULL) {
8462 8466 xmit_mp = copy_mp;
8463 8467 MULTIRT_DEBUG_TAG(first_mp);
8464 8468 }
8465 8469 }
8466 8470 ire_add_then_send(q, ire, xmit_mp);
8467 8471 ire_refrele(save_ire);
8468 8472
8469 8473 /* Assert that sire is not deleted yet. */
8470 8474 if (sire != NULL) {
8471 8475 ASSERT(sire->ire_ptpn != NULL);
8472 8476 IRB_REFRELE(sire->ire_bucket);
8473 8477 }
8474 8478
8475 8479 /* Assert that ipif_ire is not deleted yet. */
8476 8480 ASSERT(ipif_ire->ire_ptpn != NULL);
8477 8481 IRB_REFRELE(ipif_ire->ire_bucket);
8478 8482 ire_refrele(ipif_ire);
8479 8483
8480 8484 /*
8481 8485 * If copy_mp is not NULL, multirouting was
8482 8486 * requested. We loop to initiate a next
8483 8487 * route resolution attempt, starting from sire.
8484 8488 */
8485 8489 if (copy_mp != NULL) {
8486 8490 /*
8487 8491 * Search for the next unresolved
8488 8492 * multirt route.
8489 8493 */
8490 8494 copy_mp = NULL;
8491 8495 ipif_ire = NULL;
8492 8496 ire = NULL;
8493 8497 multirt_resolve_next = B_TRUE;
8494 8498 continue;
8495 8499 }
8496 8500 if (sire != NULL)
8497 8501 ire_refrele(sire);
8498 8502 ipif_refrele(src_ipif);
8499 8503 ill_refrele(dst_ill);
8500 8504 return;
8501 8505 }
8502 8506 case IRE_IF_NORESOLVER: {
8503 8507
8504 8508 if (dst_ill->ill_phys_addr_length != IP_ADDR_LEN &&
8505 8509 dst_ill->ill_resolver_mp == NULL) {
8506 8510 ip1dbg(("ip_newroute: dst_ill %p "
8507 8511 "for IRE_IF_NORESOLVER ire %p has "
8508 8512 "no ill_resolver_mp\n",
8509 8513 (void *)dst_ill, (void *)ire));
8510 8514 break;
8511 8515 }
8512 8516
8513 8517 /*
8514 8518 * TSol note: We are creating the ire cache for the
8515 8519 * destination 'dst'. If 'dst' is offlink, going
8516 8520 * through the first hop 'gw', the security attributes
8517 8521 * of 'dst' must be set to point to the gateway
8518 8522 * credentials of gateway 'gw'. If 'dst' is onlink, it
8519 8523 * is possible that 'dst' is a potential gateway that is
8520 8524 * referenced by some route that has some security
8521 8525 * attributes. Thus in the former case, we need to do a
8522 8526 * gcgrp_lookup of 'gw' while in the latter case we
8523 8527 * need to do gcgrp_lookup of 'dst' itself.
8524 8528 */
8525 8529 ga.ga_af = AF_INET;
8526 8530 IN6_IPADDR_TO_V4MAPPED(gw != INADDR_ANY ? gw : dst,
8527 8531 &ga.ga_addr);
8528 8532 gcgrp = gcgrp_lookup(&ga, B_FALSE);
8529 8533
8530 8534 ire = ire_create(
8531 8535 (uchar_t *)&dst, /* dest address */
8532 8536 (uchar_t *)&ip_g_all_ones, /* mask */
8533 8537 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
8534 8538 (uchar_t *)&gw, /* gateway address */
8535 8539 &save_ire->ire_max_frag,
8536 8540 NULL, /* no src nce */
8537 8541 dst_ill->ill_rq, /* recv-from queue */
8538 8542 dst_ill->ill_wq, /* send-to queue */
8539 8543 IRE_CACHE,
8540 8544 src_ipif,
8541 8545 save_ire->ire_mask, /* Parent mask */
8542 8546 (sire != NULL) ? /* Parent handle */
8543 8547 sire->ire_phandle : 0,
8544 8548 save_ire->ire_ihandle, /* Interface handle */
8545 8549 (sire != NULL) ? sire->ire_flags &
8546 8550 (RTF_SETSRC | RTF_MULTIRT) : 0, /* flags */
8547 8551 &(save_ire->ire_uinfo),
8548 8552 NULL,
8549 8553 gcgrp,
8550 8554 ipst);
8551 8555
8552 8556 if (ire == NULL) {
8553 8557 if (gcgrp != NULL) {
8554 8558 GCGRP_REFRELE(gcgrp);
8555 8559 gcgrp = NULL;
8556 8560 }
8557 8561 ire_refrele(save_ire);
8558 8562 break;
8559 8563 }
8560 8564
8561 8565 /* reference now held by IRE */
8562 8566 gcgrp = NULL;
8563 8567
8564 8568 ire->ire_marks |= ire_marks;
8565 8569
8566 8570 /* Prevent save_ire from getting deleted */
8567 8571 IRB_REFHOLD(save_ire->ire_bucket);
8568 8572 /* Has it been removed already ? */
8569 8573 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) {
8570 8574 IRB_REFRELE(save_ire->ire_bucket);
8571 8575 ire_refrele(save_ire);
8572 8576 break;
8573 8577 }
8574 8578
8575 8579 /*
8576 8580 * In the case of multirouting, a copy
8577 8581 * of the packet is made before it is sent.
8578 8582 * The copy is used in the next
8579 8583 * loop to attempt another resolution.
8580 8584 */
8581 8585 xmit_mp = first_mp;
8582 8586 if ((sire != NULL) &&
8583 8587 (sire->ire_flags & RTF_MULTIRT)) {
8584 8588 copy_mp = copymsg(first_mp);
8585 8589 if (copy_mp != NULL) {
8586 8590 xmit_mp = copy_mp;
8587 8591 MULTIRT_DEBUG_TAG(first_mp);
8588 8592 }
8589 8593 }
8590 8594 ire_add_then_send(q, ire, xmit_mp);
8591 8595
8592 8596 /* Assert that it is not deleted yet. */
8593 8597 ASSERT(save_ire->ire_ptpn != NULL);
8594 8598 IRB_REFRELE(save_ire->ire_bucket);
8595 8599 ire_refrele(save_ire);
8596 8600
8597 8601 if (copy_mp != NULL) {
8598 8602 /*
8599 8603 * If we found a (no)resolver, we ignore any
8600 8604 * trailing top priority IRE_CACHE in further
8601 8605 * loops. This ensures that we do not omit any
8602 8606 * (no)resolver.
8603 8607 * This IRE_CACHE, if any, will be processed
8604 8608 * by another thread entering ip_newroute().
8605 8609 * IRE_CACHE entries, if any, will be processed
8606 8610 * by another thread entering ip_newroute(),
8607 8611 * (upon resolver response, for instance).
8608 8612 * This aims to force parallel multirt
8609 8613 * resolutions as soon as a packet must be sent.
8610 8614 * In the best case, after the tx of only one
8611 8615 * packet, all reachable routes are resolved.
8612 8616 * Otherwise, the resolution of all RTF_MULTIRT
8613 8617 * routes would require several emissions.
8614 8618 */
8615 8619 multirt_flags &= ~MULTIRT_CACHEGW;
8616 8620
8617 8621 /*
8618 8622 * Search for the next unresolved multirt
8619 8623 * route.
8620 8624 */
8621 8625 copy_mp = NULL;
8622 8626 save_ire = NULL;
8623 8627 ire = NULL;
8624 8628 multirt_resolve_next = B_TRUE;
8625 8629 continue;
8626 8630 }
8627 8631
8628 8632 /*
8629 8633 * Don't need sire anymore
8630 8634 */
8631 8635 if (sire != NULL)
8632 8636 ire_refrele(sire);
8633 8637
8634 8638 ipif_refrele(src_ipif);
8635 8639 ill_refrele(dst_ill);
8636 8640 return;
8637 8641 }
8638 8642 case IRE_IF_RESOLVER:
8639 8643 /*
8640 8644 * We can't build an IRE_CACHE yet, but at least we
8641 8645 * found a resolver that can help.
8642 8646 */
8643 8647 res_mp = dst_ill->ill_resolver_mp;
8644 8648 if (!OK_RESOLVER_MP(res_mp))
8645 8649 break;
8646 8650
8647 8651 /*
8648 8652 * To be at this point in the code with a non-zero gw
8649 8653 * means that dst is reachable through a gateway that
8650 8654 * we have never resolved. By changing dst to the gw
8651 8655 * addr we resolve the gateway first.
8652 8656 * When ire_add_then_send() tries to put the IP dg
8653 8657 * to dst, it will reenter ip_newroute() at which
8654 8658 * time we will find the IRE_CACHE for the gw and
8655 8659 * create another IRE_CACHE in case IRE_CACHE above.
8656 8660 */
8657 8661 if (gw != INADDR_ANY) {
8658 8662 /*
8659 8663 * The source ipif that was determined above was
8660 8664 * relative to the destination address, not the
8661 8665 * gateway's. If src_ipif was not taken out of
8662 8666 * the IRE_IF_RESOLVER entry, we'll need to call
8663 8667 * ipif_select_source() again.
8664 8668 */
8665 8669 if (src_ipif != ire->ire_ipif) {
8666 8670 ipif_refrele(src_ipif);
8667 8671 src_ipif = ipif_select_source(dst_ill,
8668 8672 gw, zoneid);
8669 8673 if (src_ipif == NULL) {
8670 8674 if (ip_debug > 2) {
8671 8675 pr_addr_dbg(
8672 8676 "ip_newroute: no "
8673 8677 "src for gw %s ",
8674 8678 AF_INET, &gw);
8675 8679 printf("through "
8676 8680 "interface %s\n",
8677 8681 dst_ill->ill_name);
8678 8682 }
8679 8683 goto icmp_err_ret;
8680 8684 }
8681 8685 }
8682 8686 save_dst = dst;
8683 8687 dst = gw;
8684 8688 gw = INADDR_ANY;
8685 8689 }
8686 8690
8687 8691 /*
8688 8692 * We obtain a partial IRE_CACHE which we will pass
8689 8693 * along with the resolver query. When the response
8690 8694 * comes back it will be there ready for us to add.
8691 8695 * The ire_max_frag is atomically set under the
8692 8696 * irebucket lock in ire_add_v[46].
8693 8697 */
8694 8698
8695 8699 ire = ire_create_mp(
8696 8700 (uchar_t *)&dst, /* dest address */
8697 8701 (uchar_t *)&ip_g_all_ones, /* mask */
8698 8702 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
8699 8703 (uchar_t *)&gw, /* gateway address */
8700 8704 NULL, /* ire_max_frag */
8701 8705 NULL, /* no src nce */
8702 8706 dst_ill->ill_rq, /* recv-from queue */
8703 8707 dst_ill->ill_wq, /* send-to queue */
8704 8708 IRE_CACHE,
8705 8709 src_ipif, /* Interface ipif */
8706 8710 save_ire->ire_mask, /* Parent mask */
8707 8711 0,
8708 8712 save_ire->ire_ihandle, /* Interface handle */
8709 8713 0, /* flags if any */
8710 8714 &(save_ire->ire_uinfo),
8711 8715 NULL,
8712 8716 NULL,
8713 8717 ipst);
8714 8718
8715 8719 if (ire == NULL) {
8716 8720 ire_refrele(save_ire);
8717 8721 break;
8718 8722 }
8719 8723
8720 8724 if ((sire != NULL) &&
8721 8725 (sire->ire_flags & RTF_MULTIRT)) {
8722 8726 copy_mp = copymsg(first_mp);
8723 8727 if (copy_mp != NULL)
8724 8728 MULTIRT_DEBUG_TAG(copy_mp);
8725 8729 }
8726 8730
8727 8731 ire->ire_marks |= ire_marks;
8728 8732
8729 8733 /*
8730 8734 * Construct message chain for the resolver
8731 8735 * of the form:
8732 8736 * ARP_REQ_MBLK-->IRE_MBLK-->Packet
8733 8737 * Packet could contain a IPSEC_OUT mp.
8734 8738 *
8735 8739 * NOTE : ire will be added later when the response
8736 8740 * comes back from ARP. If the response does not
8737 8741 * come back, ARP frees the packet. For this reason,
8738 8742 * we can't REFHOLD the bucket of save_ire to prevent
8739 8743 * deletions. We may not be able to REFRELE the bucket
8740 8744 * if the response never comes back. Thus, before
8741 8745 * adding the ire, ire_add_v4 will make sure that the
8742 8746 * interface route does not get deleted. This is the
8743 8747 * only case unlike ip_newroute_v6, ip_newroute_ipif_v6
8744 8748 * where we can always prevent deletions because of
8745 8749 * the synchronous nature of adding IRES i.e
8746 8750 * ire_add_then_send is called after creating the IRE.
8747 8751 */
8748 8752 ASSERT(ire->ire_mp != NULL);
8749 8753 ire->ire_mp->b_cont = first_mp;
8750 8754 /* Have saved_mp handy, for cleanup if canput fails */
8751 8755 saved_mp = mp;
8752 8756 mp = copyb(res_mp);
8753 8757 if (mp == NULL) {
8754 8758 /* Prepare for cleanup */
8755 8759 mp = saved_mp; /* pkt */
8756 8760 ire_delete(ire); /* ire_mp */
8757 8761 ire = NULL;
8758 8762 ire_refrele(save_ire);
8759 8763 if (copy_mp != NULL) {
8760 8764 MULTIRT_DEBUG_UNTAG(copy_mp);
8761 8765 freemsg(copy_mp);
8762 8766 copy_mp = NULL;
8763 8767 }
8764 8768 break;
8765 8769 }
8766 8770 linkb(mp, ire->ire_mp);
8767 8771
8768 8772 /*
8769 8773 * Fill in the source and dest addrs for the resolver.
8770 8774 * NOTE: this depends on memory layouts imposed by
8771 8775 * ill_init().
8772 8776 */
8773 8777 areq = (areq_t *)mp->b_rptr;
8774 8778 addrp = (ipaddr_t *)((char *)areq +
8775 8779 areq->areq_sender_addr_offset);
8776 8780 if (do_attach_ill) {
8777 8781 /*
8778 8782 * This is bind to no failover case.
8779 8783 * arp packet also must go out on attach_ill.
8780 8784 */
8781 8785 ASSERT(ipha->ipha_src != NULL);
8782 8786 *addrp = ipha->ipha_src;
8783 8787 } else {
8784 8788 *addrp = save_ire->ire_src_addr;
8785 8789 }
8786 8790
8787 8791 ire_refrele(save_ire);
8788 8792 addrp = (ipaddr_t *)((char *)areq +
8789 8793 areq->areq_target_addr_offset);
8790 8794 *addrp = dst;
8791 8795 /* Up to the resolver. */
8792 8796 if (canputnext(dst_ill->ill_rq) &&
8793 8797 !(dst_ill->ill_arp_closing)) {
8794 8798 putnext(dst_ill->ill_rq, mp);
8795 8799 ire = NULL;
8796 8800 if (copy_mp != NULL) {
8797 8801 /*
8798 8802 * If we found a resolver, we ignore
8799 8803 * any trailing top priority IRE_CACHE
8800 8804 * in the further loops. This ensures
8801 8805 * that we do not omit any resolver.
8802 8806 * IRE_CACHE entries, if any, will be
8803 8807 * processed next time we enter
8804 8808 * ip_newroute().
8805 8809 */
8806 8810 multirt_flags &= ~MULTIRT_CACHEGW;
8807 8811 /*
8808 8812 * Search for the next unresolved
8809 8813 * multirt route.
8810 8814 */
8811 8815 first_mp = copy_mp;
8812 8816 copy_mp = NULL;
8813 8817 /* Prepare the next resolution loop. */
8814 8818 mp = first_mp;
8815 8819 EXTRACT_PKT_MP(mp, first_mp,
8816 8820 mctl_present);
8817 8821 if (mctl_present)
8818 8822 io = (ipsec_out_t *)
8819 8823 first_mp->b_rptr;
8820 8824 ipha = (ipha_t *)mp->b_rptr;
8821 8825
8822 8826 ASSERT(sire != NULL);
8823 8827
8824 8828 dst = save_dst;
8825 8829 multirt_resolve_next = B_TRUE;
8826 8830 continue;
8827 8831 }
8828 8832
8829 8833 if (sire != NULL)
8830 8834 ire_refrele(sire);
8831 8835
8832 8836 /*
8833 8837 * The response will come back in ip_wput
8834 8838 * with db_type IRE_DB_TYPE.
8835 8839 */
8836 8840 ipif_refrele(src_ipif);
8837 8841 ill_refrele(dst_ill);
8838 8842 return;
8839 8843 } else {
8840 8844 /* Prepare for cleanup */
8841 8845 DTRACE_PROBE1(ip__newroute__drop, mblk_t *,
8842 8846 mp);
8843 8847 mp->b_cont = NULL;
8844 8848 freeb(mp); /* areq */
8845 8849 /*
8846 8850 * this is an ire that is not added to the
8847 8851 * cache. ire_freemblk will handle the release
8848 8852 * of any resources associated with the ire.
8849 8853 */
8850 8854 ire_delete(ire); /* ire_mp */
8851 8855 mp = saved_mp; /* pkt */
8852 8856 ire = NULL;
8853 8857 if (copy_mp != NULL) {
8854 8858 MULTIRT_DEBUG_UNTAG(copy_mp);
8855 8859 freemsg(copy_mp);
8856 8860 copy_mp = NULL;
8857 8861 }
8858 8862 break;
8859 8863 }
8860 8864 default:
8861 8865 break;
8862 8866 }
8863 8867 } while (multirt_resolve_next);
8864 8868
8865 8869 ip1dbg(("ip_newroute: dropped\n"));
8866 8870 /* Did this packet originate externally? */
8867 8871 if (mp->b_prev) {
8868 8872 mp->b_next = NULL;
8869 8873 mp->b_prev = NULL;
8870 8874 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInDiscards);
8871 8875 } else {
8872 8876 if (dst_ill != NULL) {
8873 8877 BUMP_MIB(dst_ill->ill_ip_mib, ipIfStatsOutDiscards);
8874 8878 } else {
8875 8879 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutDiscards);
8876 8880 }
8877 8881 }
8878 8882 ASSERT(copy_mp == NULL);
8879 8883 MULTIRT_DEBUG_UNTAG(first_mp);
8880 8884 freemsg(first_mp);
8881 8885 if (ire != NULL)
8882 8886 ire_refrele(ire);
8883 8887 if (sire != NULL)
8884 8888 ire_refrele(sire);
8885 8889 if (src_ipif != NULL)
8886 8890 ipif_refrele(src_ipif);
8887 8891 if (dst_ill != NULL)
8888 8892 ill_refrele(dst_ill);
8889 8893 return;
8890 8894
8891 8895 icmp_err_ret:
8892 8896 ip1dbg(("ip_newroute: no route\n"));
8893 8897 if (src_ipif != NULL)
8894 8898 ipif_refrele(src_ipif);
8895 8899 if (dst_ill != NULL)
8896 8900 ill_refrele(dst_ill);
8897 8901 if (sire != NULL)
8898 8902 ire_refrele(sire);
8899 8903 /* Did this packet originate externally? */
8900 8904 if (mp->b_prev) {
8901 8905 mp->b_next = NULL;
8902 8906 mp->b_prev = NULL;
8903 8907 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsInNoRoutes);
8904 8908 q = WR(q);
8905 8909 } else {
8906 8910 /*
8907 8911 * There is no outgoing ill, so just increment the
8908 8912 * system MIB.
8909 8913 */
8910 8914 BUMP_MIB(&ipst->ips_ip_mib, ipIfStatsOutNoRoutes);
8911 8915 /*
8912 8916 * Since ip_wput() isn't close to finished, we fill
8913 8917 * in enough of the header for credible error reporting.
8914 8918 */
8915 8919 if (ip_hdr_complete(ipha, zoneid, ipst)) {
8916 8920 /* Failed */
8917 8921 MULTIRT_DEBUG_UNTAG(first_mp);
8918 8922 freemsg(first_mp);
8919 8923 if (ire != NULL)
8920 8924 ire_refrele(ire);
8921 8925 return;
8922 8926 }
8923 8927 }
8924 8928
8925 8929 /*
8926 8930 * At this point we will have ire only if RTF_BLACKHOLE
8927 8931 * or RTF_REJECT flags are set on the IRE. It will not
8928 8932 * generate ICMP_HOST_UNREACHABLE if RTF_BLACKHOLE is set.
8929 8933 */
8930 8934 if (ire != NULL) {
8931 8935 if (ire->ire_flags & RTF_BLACKHOLE) {
8932 8936 ire_refrele(ire);
8933 8937 MULTIRT_DEBUG_UNTAG(first_mp);
8934 8938 freemsg(first_mp);
8935 8939 return;
8936 8940 }
8937 8941 ire_refrele(ire);
8938 8942 }
8939 8943 if (ip_source_routed(ipha, ipst)) {
8940 8944 icmp_unreachable(q, first_mp, ICMP_SOURCE_ROUTE_FAILED,
8941 8945 zoneid, ipst);
8942 8946 return;
8943 8947 }
8944 8948 icmp_unreachable(q, first_mp, ICMP_HOST_UNREACHABLE, zoneid, ipst);
8945 8949 }
8946 8950
8947 8951 ip_opt_info_t zero_info;
8948 8952
8949 8953 /*
8950 8954 * IPv4 -
8951 8955 * ip_newroute_ipif is called by ip_wput_multicast and
8952 8956 * ip_rput_forward_multicast whenever we need to send
8953 8957 * out a packet to a destination address for which we do not have specific
8954 8958 * routing information. It is used when the packet will be sent out
8955 8959 * on a specific interface. It is also called by ip_wput() when IP_BOUND_IF
8956 8960 * socket option is set or icmp error message wants to go out on a particular
8957 8961 * interface for a unicast packet.
8958 8962 *
8959 8963 * In most cases, the destination address is resolved thanks to the ipif
8960 8964 * intrinsic resolver. However, there are some cases where the call to
8961 8965 * ip_newroute_ipif must take into account the potential presence of
8962 8966 * RTF_SETSRC and/or RTF_MULITRT flags in an IRE_OFFSUBNET ire
8963 8967 * that uses the interface. This is specified through flags,
8964 8968 * which can be a combination of:
8965 8969 * - RTF_SETSRC: if an IRE_OFFSUBNET ire exists that has the RTF_SETSRC
8966 8970 * flag, the resulting ire will inherit the IRE_OFFSUBNET source address
8967 8971 * and flags. Additionally, the packet source address has to be set to
8968 8972 * the specified address. The caller is thus expected to set this flag
8969 8973 * if the packet has no specific source address yet.
8970 8974 * - RTF_MULTIRT: if an IRE_OFFSUBNET ire exists that has the RTF_MULTIRT
8971 8975 * flag, the resulting ire will inherit the flag. All unresolved routes
8972 8976 * to the destination must be explored in the same call to
8973 8977 * ip_newroute_ipif().
8974 8978 */
8975 8979 static void
8976 8980 ip_newroute_ipif(queue_t *q, mblk_t *mp, ipif_t *ipif, ipaddr_t dst,
8977 8981 conn_t *connp, uint32_t flags, zoneid_t zoneid, ip_opt_info_t *infop)
8978 8982 {
8979 8983 areq_t *areq;
8980 8984 ire_t *ire = NULL;
8981 8985 mblk_t *res_mp;
8982 8986 ipaddr_t *addrp;
8983 8987 mblk_t *first_mp;
8984 8988 ire_t *save_ire = NULL;
8985 8989 ill_t *attach_ill = NULL; /* Bind to IPIF_NOFAILOVER */
8986 8990 ipif_t *src_ipif = NULL;
8987 8991 ushort_t ire_marks = 0;
8988 8992 ill_t *dst_ill = NULL;
8989 8993 boolean_t mctl_present;
8990 8994 ipsec_out_t *io;
8991 8995 ipha_t *ipha;
8992 8996 int ihandle = 0;
8993 8997 mblk_t *saved_mp;
8994 8998 ire_t *fire = NULL;
8995 8999 mblk_t *copy_mp = NULL;
8996 9000 boolean_t multirt_resolve_next;
8997 9001 boolean_t unspec_src;
8998 9002 ipaddr_t ipha_dst;
8999 9003 ip_stack_t *ipst = ipif->ipif_ill->ill_ipst;
9000 9004
9001 9005 /*
9002 9006 * CGTP goes in a loop which looks up a new ipif, do an ipif_refhold
9003 9007 * here for uniformity
9004 9008 */
9005 9009 ipif_refhold(ipif);
9006 9010
9007 9011 /*
9008 9012 * This loop is run only once in most cases.
9009 9013 * We loop to resolve further routes only when the destination
9010 9014 * can be reached through multiple RTF_MULTIRT-flagged ires.
9011 9015 */
9012 9016 do {
9013 9017 if (dst_ill != NULL) {
9014 9018 ill_refrele(dst_ill);
9015 9019 dst_ill = NULL;
9016 9020 }
9017 9021 if (src_ipif != NULL) {
9018 9022 ipif_refrele(src_ipif);
9019 9023 src_ipif = NULL;
9020 9024 }
9021 9025 multirt_resolve_next = B_FALSE;
9022 9026
9023 9027 ip1dbg(("ip_newroute_ipif: dst 0x%x, if %s\n", ntohl(dst),
9024 9028 ipif->ipif_ill->ill_name));
9025 9029
9026 9030 EXTRACT_PKT_MP(mp, first_mp, mctl_present);
9027 9031 if (mctl_present)
9028 9032 io = (ipsec_out_t *)first_mp->b_rptr;
9029 9033
9030 9034 ipha = (ipha_t *)mp->b_rptr;
9031 9035
9032 9036 /*
9033 9037 * Save the packet destination address, we may need it after
9034 9038 * the packet has been consumed.
9035 9039 */
9036 9040 ipha_dst = ipha->ipha_dst;
9037 9041
9038 9042 /*
9039 9043 * If the interface is a pt-pt interface we look for an
9040 9044 * IRE_IF_RESOLVER or IRE_IF_NORESOLVER that matches both the
9041 9045 * local_address and the pt-pt destination address. Otherwise
9042 9046 * we just match the local address.
9043 9047 * NOTE: dst could be different than ipha->ipha_dst in case
9044 9048 * of sending igmp multicast packets over a point-to-point
9045 9049 * connection.
9046 9050 * Thus we must be careful enough to check ipha_dst to be a
9047 9051 * multicast address, otherwise it will take xmit_if path for
9048 9052 * multicast packets resulting into kernel stack overflow by
9049 9053 * repeated calls to ip_newroute_ipif from ire_send().
9050 9054 */
9051 9055 if (CLASSD(ipha_dst) &&
9052 9056 !(ipif->ipif_ill->ill_flags & ILLF_MULTICAST)) {
9053 9057 goto err_ret;
9054 9058 }
9055 9059
9056 9060 /*
9057 9061 * We check if an IRE_OFFSUBNET for the addr that goes through
9058 9062 * ipif exists. We need it to determine if the RTF_SETSRC and/or
9059 9063 * RTF_MULTIRT flags must be honored. This IRE_OFFSUBNET ire may
9060 9064 * propagate its flags to the new ire.
9061 9065 */
9062 9066 if (CLASSD(ipha_dst) && (flags & (RTF_MULTIRT | RTF_SETSRC))) {
9063 9067 fire = ipif_lookup_multi_ire(ipif, ipha_dst);
9064 9068 ip2dbg(("ip_newroute_ipif: "
9065 9069 "ipif_lookup_multi_ire("
9066 9070 "ipif %p, dst %08x) = fire %p\n",
9067 9071 (void *)ipif, ntohl(dst), (void *)fire));
9068 9072 }
9069 9073
9070 9074 if (mctl_present && io->ipsec_out_attach_if) {
9071 9075 attach_ill = ip_grab_attach_ill(NULL, first_mp,
9072 9076 io->ipsec_out_ill_index, B_FALSE, ipst);
9073 9077
9074 9078 /* Failure case frees things for us. */
9075 9079 if (attach_ill == NULL) {
9076 9080 ipif_refrele(ipif);
9077 9081 if (fire != NULL)
9078 9082 ire_refrele(fire);
9079 9083 return;
9080 9084 }
9081 9085
9082 9086 /*
9083 9087 * Check if we need an ire that will not be
9084 9088 * looked up by anybody else i.e. HIDDEN.
9085 9089 */
9086 9090 if (ill_is_probeonly(attach_ill)) {
9087 9091 ire_marks = IRE_MARK_HIDDEN;
9088 9092 }
9089 9093 /*
9090 9094 * ip_wput passes the right ipif for IPIF_NOFAILOVER
9091 9095 * case.
9092 9096 */
9093 9097 dst_ill = ipif->ipif_ill;
9094 9098 /* attach_ill has been refheld by ip_grab_attach_ill */
9095 9099 ASSERT(dst_ill == attach_ill);
9096 9100 } else {
9097 9101 /*
9098 9102 * If the interface belongs to an interface group,
9099 9103 * make sure the next possible interface in the group
9100 9104 * is used. This encourages load spreading among
9101 9105 * peers in an interface group.
9102 9106 * Note: load spreading is disabled for RTF_MULTIRT
9103 9107 * routes.
9104 9108 */
9105 9109 if ((flags & RTF_MULTIRT) && (fire != NULL) &&
9106 9110 (fire->ire_flags & RTF_MULTIRT)) {
9107 9111 /*
9108 9112 * Don't perform outbound load spreading
9109 9113 * in the case of an RTF_MULTIRT issued route,
9110 9114 * we actually typically want to replicate
9111 9115 * outgoing packets through particular
9112 9116 * interfaces.
9113 9117 */
9114 9118 dst_ill = ipif->ipif_ill;
9115 9119 ill_refhold(dst_ill);
9116 9120 } else {
9117 9121 dst_ill = ip_newroute_get_dst_ill(
9118 9122 ipif->ipif_ill);
9119 9123 }
9120 9124 if (dst_ill == NULL) {
9121 9125 if (ip_debug > 2) {
9122 9126 pr_addr_dbg("ip_newroute_ipif: "
9123 9127 "no dst ill for dst %s\n",
9124 9128 AF_INET, &dst);
9125 9129 }
9126 9130 goto err_ret;
9127 9131 }
9128 9132 }
9129 9133
9130 9134 /*
9131 9135 * Pick a source address preferring non-deprecated ones.
9132 9136 * Unlike ip_newroute, we don't do any source address
9133 9137 * selection here since for multicast it really does not help
9134 9138 * in inbound load spreading as in the unicast case.
9135 9139 */
9136 9140 if ((flags & RTF_SETSRC) && (fire != NULL) &&
9137 9141 (fire->ire_flags & RTF_SETSRC)) {
9138 9142 /*
9139 9143 * As requested by flags, an IRE_OFFSUBNET was looked up
9140 9144 * on that interface. This ire has RTF_SETSRC flag, so
9141 9145 * the source address of the packet must be changed.
9142 9146 * Check that the ipif matching the requested source
9143 9147 * address still exists.
9144 9148 */
9145 9149 src_ipif = ipif_lookup_addr(fire->ire_src_addr, NULL,
9146 9150 zoneid, NULL, NULL, NULL, NULL, ipst);
9147 9151 }
9148 9152
9149 9153 unspec_src = (connp != NULL && connp->conn_unspec_src);
9150 9154
9151 9155 if (((!ipif->ipif_isv6 && ipif->ipif_lcl_addr == INADDR_ANY) ||
9152 9156 (ipif->ipif_flags & (IPIF_DEPRECATED|IPIF_UP)) != IPIF_UP ||
9153 9157 (connp != NULL && ipif->ipif_zoneid != zoneid &&
9154 9158 ipif->ipif_zoneid != ALL_ZONES)) &&
9155 9159 (src_ipif == NULL) &&
9156 9160 (!unspec_src || ipha->ipha_src != INADDR_ANY)) {
9157 9161 src_ipif = ipif_select_source(dst_ill, dst, zoneid);
9158 9162 if (src_ipif == NULL) {
9159 9163 if (ip_debug > 2) {
9160 9164 /* ip1dbg */
9161 9165 pr_addr_dbg("ip_newroute_ipif: "
9162 9166 "no src for dst %s",
9163 9167 AF_INET, &dst);
9164 9168 }
9165 9169 ip1dbg((" through interface %s\n",
9166 9170 dst_ill->ill_name));
9167 9171 goto err_ret;
9168 9172 }
9169 9173 ipif_refrele(ipif);
9170 9174 ipif = src_ipif;
9171 9175 ipif_refhold(ipif);
9172 9176 }
9173 9177 if (src_ipif == NULL) {
9174 9178 src_ipif = ipif;
9175 9179 ipif_refhold(src_ipif);
9176 9180 }
9177 9181
9178 9182 /*
9179 9183 * Assign a source address while we have the conn.
9180 9184 * We can't have ip_wput_ire pick a source address when the
9181 9185 * packet returns from arp since conn_unspec_src might be set
9182 9186 * and we lose the conn when going through arp.
9183 9187 */
9184 9188 if (ipha->ipha_src == INADDR_ANY && !unspec_src)
9185 9189 ipha->ipha_src = src_ipif->ipif_src_addr;
9186 9190
9187 9191 /*
9188 9192 * In the case of IP_BOUND_IF and IP_PKTINFO, it is possible
9189 9193 * that the outgoing interface does not have an interface ire.
9190 9194 */
9191 9195 if (CLASSD(ipha_dst) && (connp == NULL ||
9192 9196 connp->conn_outgoing_ill == NULL) &&
9193 9197 infop->ip_opt_ill_index == 0) {
9194 9198 /* ipif_to_ire returns an held ire */
9195 9199 ire = ipif_to_ire(ipif);
9196 9200 if (ire == NULL)
9197 9201 goto err_ret;
9198 9202 if (ire->ire_flags & (RTF_REJECT | RTF_BLACKHOLE))
9199 9203 goto err_ret;
9200 9204 /*
9201 9205 * ihandle is needed when the ire is added to
9202 9206 * cache table.
9203 9207 */
9204 9208 save_ire = ire;
9205 9209 ihandle = save_ire->ire_ihandle;
9206 9210
9207 9211 ip2dbg(("ip_newroute_ipif: ire %p, ipif %p, "
9208 9212 "flags %04x\n",
9209 9213 (void *)ire, (void *)ipif, flags));
9210 9214 if ((flags & RTF_MULTIRT) && (fire != NULL) &&
9211 9215 (fire->ire_flags & RTF_MULTIRT)) {
9212 9216 /*
9213 9217 * As requested by flags, an IRE_OFFSUBNET was
9214 9218 * looked up on that interface. This ire has
9215 9219 * RTF_MULTIRT flag, so the resolution loop will
9216 9220 * be re-entered to resolve additional routes on
9217 9221 * other interfaces. For that purpose, a copy of
9218 9222 * the packet is performed at this point.
9219 9223 */
9220 9224 fire->ire_last_used_time = lbolt;
9221 9225 copy_mp = copymsg(first_mp);
9222 9226 if (copy_mp) {
9223 9227 MULTIRT_DEBUG_TAG(copy_mp);
9224 9228 }
9225 9229 }
9226 9230 if ((flags & RTF_SETSRC) && (fire != NULL) &&
9227 9231 (fire->ire_flags & RTF_SETSRC)) {
9228 9232 /*
9229 9233 * As requested by flags, an IRE_OFFSUBET was
9230 9234 * looked up on that interface. This ire has
9231 9235 * RTF_SETSRC flag, so the source address of the
9232 9236 * packet must be changed.
9233 9237 */
9234 9238 ipha->ipha_src = fire->ire_src_addr;
9235 9239 }
9236 9240 } else {
9237 9241 ASSERT((connp == NULL) ||
9238 9242 (connp->conn_outgoing_ill != NULL) ||
9239 9243 (connp->conn_dontroute) ||
9240 9244 infop->ip_opt_ill_index != 0);
9241 9245 /*
9242 9246 * The only ways we can come here are:
9243 9247 * 1) IP_BOUND_IF socket option is set
9244 9248 * 2) SO_DONTROUTE socket option is set
9245 9249 * 3) IP_PKTINFO option is passed in as ancillary data.
9246 9250 * In all cases, the new ire will not be added
9247 9251 * into cache table.
9248 9252 */
9249 9253 ire_marks |= IRE_MARK_NOADD;
9250 9254 }
9251 9255
9252 9256 switch (ipif->ipif_net_type) {
9253 9257 case IRE_IF_NORESOLVER: {
9254 9258 /* We have what we need to build an IRE_CACHE. */
9255 9259
9256 9260 if ((dst_ill->ill_phys_addr_length != IP_ADDR_LEN) &&
9257 9261 (dst_ill->ill_resolver_mp == NULL)) {
9258 9262 ip1dbg(("ip_newroute_ipif: dst_ill %p "
9259 9263 "for IRE_IF_NORESOLVER ire %p has "
9260 9264 "no ill_resolver_mp\n",
9261 9265 (void *)dst_ill, (void *)ire));
9262 9266 break;
9263 9267 }
9264 9268
9265 9269 /*
9266 9270 * The new ire inherits the IRE_OFFSUBNET flags
9267 9271 * and source address, if this was requested.
9268 9272 */
9269 9273 ire = ire_create(
9270 9274 (uchar_t *)&dst, /* dest address */
9271 9275 (uchar_t *)&ip_g_all_ones, /* mask */
9272 9276 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
9273 9277 NULL, /* gateway address */
9274 9278 &ipif->ipif_mtu,
9275 9279 NULL, /* no src nce */
9276 9280 dst_ill->ill_rq, /* recv-from queue */
9277 9281 dst_ill->ill_wq, /* send-to queue */
9278 9282 IRE_CACHE,
9279 9283 src_ipif,
9280 9284 (save_ire != NULL ? save_ire->ire_mask : 0),
9281 9285 (fire != NULL) ? /* Parent handle */
9282 9286 fire->ire_phandle : 0,
9283 9287 ihandle, /* Interface handle */
9284 9288 (fire != NULL) ?
9285 9289 (fire->ire_flags &
9286 9290 (RTF_SETSRC | RTF_MULTIRT)) : 0,
9287 9291 (save_ire == NULL ? &ire_uinfo_null :
9288 9292 &save_ire->ire_uinfo),
9289 9293 NULL,
9290 9294 NULL,
9291 9295 ipst);
9292 9296
9293 9297 if (ire == NULL) {
9294 9298 if (save_ire != NULL)
9295 9299 ire_refrele(save_ire);
9296 9300 break;
9297 9301 }
9298 9302
9299 9303 ire->ire_marks |= ire_marks;
9300 9304
9301 9305 /*
9302 9306 * If IRE_MARK_NOADD is set then we need to convert
9303 9307 * the max_fragp to a useable value now. This is
9304 9308 * normally done in ire_add_v[46]. We also need to
9305 9309 * associate the ire with an nce (normally would be
9306 9310 * done in ip_wput_nondata()).
9307 9311 *
9308 9312 * Note that IRE_MARK_NOADD packets created here
9309 9313 * do not have a non-null ire_mp pointer. The null
9310 9314 * value of ire_bucket indicates that they were
9311 9315 * never added.
9312 9316 */
9313 9317 if (ire->ire_marks & IRE_MARK_NOADD) {
9314 9318 uint_t max_frag;
9315 9319
9316 9320 max_frag = *ire->ire_max_fragp;
9317 9321 ire->ire_max_fragp = NULL;
9318 9322 ire->ire_max_frag = max_frag;
9319 9323
9320 9324 if ((ire->ire_nce = ndp_lookup_v4(
9321 9325 ire_to_ill(ire),
9322 9326 (ire->ire_gateway_addr != INADDR_ANY ?
9323 9327 &ire->ire_gateway_addr : &ire->ire_addr),
9324 9328 B_FALSE)) == NULL) {
9325 9329 if (save_ire != NULL)
9326 9330 ire_refrele(save_ire);
9327 9331 break;
9328 9332 }
9329 9333 ASSERT(ire->ire_nce->nce_state ==
9330 9334 ND_REACHABLE);
9331 9335 NCE_REFHOLD_TO_REFHOLD_NOTR(ire->ire_nce);
9332 9336 }
9333 9337
9334 9338 /* Prevent save_ire from getting deleted */
9335 9339 if (save_ire != NULL) {
9336 9340 IRB_REFHOLD(save_ire->ire_bucket);
9337 9341 /* Has it been removed already ? */
9338 9342 if (save_ire->ire_marks & IRE_MARK_CONDEMNED) {
9339 9343 IRB_REFRELE(save_ire->ire_bucket);
9340 9344 ire_refrele(save_ire);
9341 9345 break;
9342 9346 }
9343 9347 }
9344 9348
9345 9349 ire_add_then_send(q, ire, first_mp);
9346 9350
9347 9351 /* Assert that save_ire is not deleted yet. */
9348 9352 if (save_ire != NULL) {
9349 9353 ASSERT(save_ire->ire_ptpn != NULL);
9350 9354 IRB_REFRELE(save_ire->ire_bucket);
9351 9355 ire_refrele(save_ire);
9352 9356 save_ire = NULL;
9353 9357 }
9354 9358 if (fire != NULL) {
9355 9359 ire_refrele(fire);
9356 9360 fire = NULL;
9357 9361 }
9358 9362
9359 9363 /*
9360 9364 * the resolution loop is re-entered if this
9361 9365 * was requested through flags and if we
9362 9366 * actually are in a multirouting case.
9363 9367 */
9364 9368 if ((flags & RTF_MULTIRT) && (copy_mp != NULL)) {
9365 9369 boolean_t need_resolve =
9366 9370 ire_multirt_need_resolve(ipha_dst,
9367 9371 MBLK_GETLABEL(copy_mp), ipst);
9368 9372 if (!need_resolve) {
9369 9373 MULTIRT_DEBUG_UNTAG(copy_mp);
9370 9374 freemsg(copy_mp);
9371 9375 copy_mp = NULL;
9372 9376 } else {
9373 9377 /*
9374 9378 * ipif_lookup_group() calls
9375 9379 * ire_lookup_multi() that uses
9376 9380 * ire_ftable_lookup() to find
9377 9381 * an IRE_INTERFACE for the group.
9378 9382 * In the multirt case,
9379 9383 * ire_lookup_multi() then invokes
9380 9384 * ire_multirt_lookup() to find
9381 9385 * the next resolvable ire.
9382 9386 * As a result, we obtain an new
9383 9387 * interface, derived from the
9384 9388 * next ire.
9385 9389 */
9386 9390 ipif_refrele(ipif);
9387 9391 ipif = ipif_lookup_group(ipha_dst,
9388 9392 zoneid, ipst);
9389 9393 ip2dbg(("ip_newroute_ipif: "
9390 9394 "multirt dst %08x, ipif %p\n",
9391 9395 htonl(dst), (void *)ipif));
9392 9396 if (ipif != NULL) {
9393 9397 mp = copy_mp;
9394 9398 copy_mp = NULL;
9395 9399 multirt_resolve_next = B_TRUE;
9396 9400 continue;
9397 9401 } else {
9398 9402 freemsg(copy_mp);
9399 9403 }
9400 9404 }
9401 9405 }
9402 9406 if (ipif != NULL)
9403 9407 ipif_refrele(ipif);
9404 9408 ill_refrele(dst_ill);
9405 9409 ipif_refrele(src_ipif);
9406 9410 return;
9407 9411 }
9408 9412 case IRE_IF_RESOLVER:
9409 9413 /*
9410 9414 * We can't build an IRE_CACHE yet, but at least
9411 9415 * we found a resolver that can help.
9412 9416 */
9413 9417 res_mp = dst_ill->ill_resolver_mp;
9414 9418 if (!OK_RESOLVER_MP(res_mp))
9415 9419 break;
9416 9420
9417 9421 /*
9418 9422 * We obtain a partial IRE_CACHE which we will pass
9419 9423 * along with the resolver query. When the response
9420 9424 * comes back it will be there ready for us to add.
9421 9425 * The new ire inherits the IRE_OFFSUBNET flags
9422 9426 * and source address, if this was requested.
9423 9427 * The ire_max_frag is atomically set under the
9424 9428 * irebucket lock in ire_add_v[46]. Only in the
9425 9429 * case of IRE_MARK_NOADD, we set it here itself.
9426 9430 */
9427 9431 ire = ire_create_mp(
9428 9432 (uchar_t *)&dst, /* dest address */
9429 9433 (uchar_t *)&ip_g_all_ones, /* mask */
9430 9434 (uchar_t *)&src_ipif->ipif_src_addr, /* src addr */
9431 9435 NULL, /* gateway address */
9432 9436 (ire_marks & IRE_MARK_NOADD) ?
9433 9437 ipif->ipif_mtu : 0, /* max_frag */
9434 9438 NULL, /* no src nce */
9435 9439 dst_ill->ill_rq, /* recv-from queue */
9436 9440 dst_ill->ill_wq, /* send-to queue */
9437 9441 IRE_CACHE,
9438 9442 src_ipif,
9439 9443 (save_ire != NULL ? save_ire->ire_mask : 0),
9440 9444 (fire != NULL) ? /* Parent handle */
9441 9445 fire->ire_phandle : 0,
9442 9446 ihandle, /* Interface handle */
9443 9447 (fire != NULL) ? /* flags if any */
9444 9448 (fire->ire_flags &
9445 9449 (RTF_SETSRC | RTF_MULTIRT)) : 0,
944