xref: /titanic_44/usr/src/uts/common/inet/ip/ip_input.c (revision 53f3aea0943e36e5fed2615ad5f9fd1f17de51d2)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 /* Copyright (c) 1990 Mentat Inc. */
27 
28 #include <sys/types.h>
29 #include <sys/stream.h>
30 #include <sys/dlpi.h>
31 #include <sys/stropts.h>
32 #include <sys/sysmacros.h>
33 #include <sys/strsubr.h>
34 #include <sys/strlog.h>
35 #include <sys/strsun.h>
36 #include <sys/zone.h>
37 #define	_SUN_TPI_VERSION 2
38 #include <sys/tihdr.h>
39 #include <sys/xti_inet.h>
40 #include <sys/ddi.h>
41 #include <sys/sunddi.h>
42 #include <sys/cmn_err.h>
43 #include <sys/debug.h>
44 #include <sys/kobj.h>
45 #include <sys/modctl.h>
46 #include <sys/atomic.h>
47 #include <sys/policy.h>
48 #include <sys/priv.h>
49 
50 #include <sys/systm.h>
51 #include <sys/param.h>
52 #include <sys/kmem.h>
53 #include <sys/sdt.h>
54 #include <sys/socket.h>
55 #include <sys/vtrace.h>
56 #include <sys/isa_defs.h>
57 #include <sys/mac.h>
58 #include <net/if.h>
59 #include <net/if_arp.h>
60 #include <net/route.h>
61 #include <sys/sockio.h>
62 #include <netinet/in.h>
63 #include <net/if_dl.h>
64 
65 #include <inet/common.h>
66 #include <inet/mi.h>
67 #include <inet/mib2.h>
68 #include <inet/nd.h>
69 #include <inet/arp.h>
70 #include <inet/snmpcom.h>
71 #include <inet/kstatcom.h>
72 
73 #include <netinet/igmp_var.h>
74 #include <netinet/ip6.h>
75 #include <netinet/icmp6.h>
76 #include <netinet/sctp.h>
77 
78 #include <inet/ip.h>
79 #include <inet/ip_impl.h>
80 #include <inet/ip6.h>
81 #include <inet/ip6_asp.h>
82 #include <inet/optcom.h>
83 #include <inet/tcp.h>
84 #include <inet/tcp_impl.h>
85 #include <inet/ip_multi.h>
86 #include <inet/ip_if.h>
87 #include <inet/ip_ire.h>
88 #include <inet/ip_ftable.h>
89 #include <inet/ip_rts.h>
90 #include <inet/ip_ndp.h>
91 #include <inet/ip_listutils.h>
92 #include <netinet/igmp.h>
93 #include <netinet/ip_mroute.h>
94 #include <inet/ipp_common.h>
95 
96 #include <net/pfkeyv2.h>
97 #include <inet/sadb.h>
98 #include <inet/ipsec_impl.h>
99 #include <inet/ipdrop.h>
100 #include <inet/ip_netinfo.h>
101 #include <inet/ilb_ip.h>
102 #include <sys/squeue_impl.h>
103 #include <sys/squeue.h>
104 
105 #include <sys/ethernet.h>
106 #include <net/if_types.h>
107 #include <sys/cpuvar.h>
108 
109 #include <ipp/ipp.h>
110 #include <ipp/ipp_impl.h>
111 #include <ipp/ipgpc/ipgpc.h>
112 
113 #include <sys/pattr.h>
114 #include <inet/ipclassifier.h>
115 #include <inet/sctp_ip.h>
116 #include <inet/sctp/sctp_impl.h>
117 #include <inet/udp_impl.h>
118 #include <sys/sunddi.h>
119 
120 #include <sys/tsol/label.h>
121 #include <sys/tsol/tnet.h>
122 
123 #include <sys/clock_impl.h>	/* For LBOLT_FASTPATH{,64} */
124 
125 #ifdef	DEBUG
126 extern boolean_t skip_sctp_cksum;
127 #endif
128 
129 static void	ip_input_local_v4(ire_t *, mblk_t *, ipha_t *,
130     ip_recv_attr_t *);
131 
132 static void	ip_input_broadcast_v4(ire_t *, mblk_t *, ipha_t *,
133     ip_recv_attr_t *);
134 static void	ip_input_multicast_v4(ire_t *, mblk_t *, ipha_t *,
135     ip_recv_attr_t *);
136 
137 #pragma inline(ip_input_common_v4, ip_input_local_v4, ip_forward_xmit_v4)
138 
139 /*
140  * Direct read side procedure capable of dealing with chains. GLDv3 based
141  * drivers call this function directly with mblk chains while STREAMS
142  * read side procedure ip_rput() calls this for single packet with ip_ring
143  * set to NULL to process one packet at a time.
144  *
145  * The ill will always be valid if this function is called directly from
146  * the driver.
147  *
148  * If ip_input() is called from GLDv3:
149  *
150  *   - This must be a non-VLAN IP stream.
151  *   - 'mp' is either an untagged or a special priority-tagged packet.
152  *   - Any VLAN tag that was in the MAC header has been stripped.
153  *
154  * If the IP header in packet is not 32-bit aligned, every message in the
155  * chain will be aligned before further operations. This is required on SPARC
156  * platform.
157  */
158 void
159 ip_input(ill_t *ill, ill_rx_ring_t *ip_ring, mblk_t *mp_chain,
160     struct mac_header_info_s *mhip)
161 {
162 	(void) ip_input_common_v4(ill, ip_ring, mp_chain, mhip, NULL, NULL,
163 	    NULL);
164 }
165 
166 /*
167  * ip_accept_tcp() - This function is called by the squeue when it retrieves
168  * a chain of packets in the poll mode. The packets have gone through the
169  * data link processing but not IP processing. For performance and latency
170  * reasons, the squeue wants to process the chain in line instead of feeding
171  * it back via ip_input path.
172  *
173  * We set up the ip_recv_attr_t with IRAF_TARGET_SQP to that ip_fanout_v4
174  * will pass back any TCP packets matching the target sqp to
175  * ip_input_common_v4 using ira_target_sqp_mp. Other packets are handled by
176  * ip_input_v4 and ip_fanout_v4 as normal.
177  * The TCP packets that match the target squeue are returned to the caller
178  * as a b_next chain after each packet has been prepend with an mblk
179  * from ip_recv_attr_to_mblk.
180  */
181 mblk_t *
182 ip_accept_tcp(ill_t *ill, ill_rx_ring_t *ip_ring, squeue_t *target_sqp,
183     mblk_t *mp_chain, mblk_t **last, uint_t *cnt)
184 {
185 	return (ip_input_common_v4(ill, ip_ring, mp_chain, NULL, target_sqp,
186 	    last, cnt));
187 }
188 
189 /*
190  * Used by ip_input and ip_accept_tcp
191  * The last three arguments are only used by ip_accept_tcp, and mhip is
192  * only used by ip_input.
193  */
194 mblk_t *
195 ip_input_common_v4(ill_t *ill, ill_rx_ring_t *ip_ring, mblk_t *mp_chain,
196     struct mac_header_info_s *mhip, squeue_t *target_sqp,
197     mblk_t **last, uint_t *cnt)
198 {
199 	mblk_t		*mp;
200 	ipha_t		*ipha;
201 	ip_recv_attr_t	iras;	/* Receive attributes */
202 	rtc_t		rtc;
203 	iaflags_t	chain_flags = 0;	/* Fixed for chain */
204 	mblk_t 		*ahead = NULL;	/* Accepted head */
205 	mblk_t		*atail = NULL;	/* Accepted tail */
206 	uint_t		acnt = 0;	/* Accepted count */
207 
208 	ASSERT(mp_chain != NULL);
209 	ASSERT(ill != NULL);
210 
211 	/* These ones do not change as we loop over packets */
212 	iras.ira_ill = iras.ira_rill = ill;
213 	iras.ira_ruifindex = ill->ill_phyint->phyint_ifindex;
214 	iras.ira_rifindex = iras.ira_ruifindex;
215 	iras.ira_sqp = NULL;
216 	iras.ira_ring = ip_ring;
217 	/* For ECMP and outbound transmit ring selection */
218 	iras.ira_xmit_hint = ILL_RING_TO_XMIT_HINT(ip_ring);
219 
220 	iras.ira_target_sqp = target_sqp;
221 	iras.ira_target_sqp_mp = NULL;
222 	if (target_sqp != NULL)
223 		chain_flags |= IRAF_TARGET_SQP;
224 
225 	/*
226 	 * We try to have a mhip pointer when possible, but
227 	 * it might be NULL in some cases. In those cases we
228 	 * have to assume unicast.
229 	 */
230 	iras.ira_mhip = mhip;
231 	iras.ira_flags = 0;
232 	if (mhip != NULL) {
233 		switch (mhip->mhi_dsttype) {
234 		case MAC_ADDRTYPE_MULTICAST :
235 			chain_flags |= IRAF_L2DST_MULTICAST;
236 			break;
237 		case MAC_ADDRTYPE_BROADCAST :
238 			chain_flags |= IRAF_L2DST_BROADCAST;
239 			break;
240 		}
241 	}
242 
243 	/*
244 	 * Initialize the one-element route cache.
245 	 *
246 	 * We do ire caching from one iteration to
247 	 * another. In the event the packet chain contains
248 	 * all packets from the same dst, this caching saves
249 	 * an ire_route_recursive for each of the succeeding
250 	 * packets in a packet chain.
251 	 */
252 	rtc.rtc_ire = NULL;
253 	rtc.rtc_ipaddr = INADDR_ANY;
254 
255 	/* Loop over b_next */
256 	for (mp = mp_chain; mp != NULL; mp = mp_chain) {
257 		mp_chain = mp->b_next;
258 		mp->b_next = NULL;
259 
260 		ASSERT(DB_TYPE(mp) == M_DATA);
261 
262 
263 		/*
264 		 * if db_ref > 1 then copymsg and free original. Packet
265 		 * may be changed and we do not want the other entity
266 		 * who has a reference to this message to trip over the
267 		 * changes. This is a blind change because trying to
268 		 * catch all places that might change the packet is too
269 		 * difficult.
270 		 *
271 		 * This corresponds to the fast path case, where we have
272 		 * a chain of M_DATA mblks.  We check the db_ref count
273 		 * of only the 1st data block in the mblk chain. There
274 		 * doesn't seem to be a reason why a device driver would
275 		 * send up data with varying db_ref counts in the mblk
276 		 * chain. In any case the Fast path is a private
277 		 * interface, and our drivers don't do such a thing.
278 		 * Given the above assumption, there is no need to walk
279 		 * down the entire mblk chain (which could have a
280 		 * potential performance problem)
281 		 *
282 		 * The "(DB_REF(mp) > 1)" check was moved from ip_rput()
283 		 * to here because of exclusive ip stacks and vnics.
284 		 * Packets transmitted from exclusive stack over vnic
285 		 * can have db_ref > 1 and when it gets looped back to
286 		 * another vnic in a different zone, you have ip_input()
287 		 * getting dblks with db_ref > 1. So if someone
288 		 * complains of TCP performance under this scenario,
289 		 * take a serious look here on the impact of copymsg().
290 		 */
291 		if (DB_REF(mp) > 1) {
292 			if ((mp = ip_fix_dbref(mp, &iras)) == NULL) {
293 				/* mhip might point into 1st packet in chain */
294 				iras.ira_mhip = NULL;
295 				continue;
296 			}
297 		}
298 
299 		/*
300 		 * IP header ptr not aligned?
301 		 * OR IP header not complete in first mblk
302 		 */
303 		ipha = (ipha_t *)mp->b_rptr;
304 		if (!OK_32PTR(ipha) || MBLKL(mp) < IP_SIMPLE_HDR_LENGTH) {
305 			mp = ip_check_and_align_header(mp, IP_SIMPLE_HDR_LENGTH,
306 			    &iras);
307 			if (mp == NULL) {
308 				/* mhip might point into 1st packet in chain */
309 				iras.ira_mhip = NULL;
310 				continue;
311 			}
312 			ipha = (ipha_t *)mp->b_rptr;
313 		}
314 
315 		/* Protect against a mix of Ethertypes and IP versions */
316 		if (IPH_HDR_VERSION(ipha) != IPV4_VERSION) {
317 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInHdrErrors);
318 			ip_drop_input("ipIfStatsInHdrErrors", mp, ill);
319 			freemsg(mp);
320 			/* mhip might point into 1st packet in the chain. */
321 			iras.ira_mhip = NULL;
322 			continue;
323 		}
324 
325 		/*
326 		 * Check for Martian addrs; we have to explicitly
327 		 * test for for zero dst since this is also used as
328 		 * an indication that the rtc is not used.
329 		 */
330 		if (ipha->ipha_dst == INADDR_ANY) {
331 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInAddrErrors);
332 			ip_drop_input("ipIfStatsInAddrErrors", mp, ill);
333 			freemsg(mp);
334 			/* mhip might point into 1st packet in the chain. */
335 			iras.ira_mhip = NULL;
336 			continue;
337 		}
338 
339 		/*
340 		 * Keep L2SRC from a previous packet in chain since mhip
341 		 * might point into an earlier packet in the chain.
342 		 * Keep IRAF_VERIFIED_SRC to avoid redoing broadcast
343 		 * source check in forwarding path.
344 		 */
345 		chain_flags |= (iras.ira_flags &
346 		    (IRAF_L2SRC_SET|IRAF_VERIFIED_SRC));
347 
348 		iras.ira_flags = IRAF_IS_IPV4 | IRAF_VERIFY_IP_CKSUM |
349 		    IRAF_VERIFY_ULP_CKSUM | chain_flags;
350 		iras.ira_free_flags = 0;
351 		iras.ira_cred = NULL;
352 		iras.ira_cpid = NOPID;
353 		iras.ira_tsl = NULL;
354 		iras.ira_zoneid = ALL_ZONES;	/* Default for forwarding */
355 
356 		/*
357 		 * We must count all incoming packets, even if they end
358 		 * up being dropped later on. Defer counting bytes until
359 		 * we have the whole IP header in first mblk.
360 		 */
361 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInReceives);
362 
363 		iras.ira_pktlen = ntohs(ipha->ipha_length);
364 		UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCInOctets,
365 		    iras.ira_pktlen);
366 
367 		/*
368 		 * Call one of:
369 		 * 	ill_input_full_v4
370 		 *	ill_input_short_v4
371 		 * The former is used in unusual cases. See ill_set_inputfn().
372 		 */
373 		(*ill->ill_inputfn)(mp, ipha, &ipha->ipha_dst, &iras, &rtc);
374 
375 		/* Any references to clean up? No hold on ira_ill */
376 		if (iras.ira_flags & (IRAF_IPSEC_SECURE|IRAF_SYSTEM_LABELED))
377 			ira_cleanup(&iras, B_FALSE);
378 
379 		if (iras.ira_target_sqp_mp != NULL) {
380 			/* Better be called from ip_accept_tcp */
381 			ASSERT(target_sqp != NULL);
382 
383 			/* Found one packet to accept */
384 			mp = iras.ira_target_sqp_mp;
385 			iras.ira_target_sqp_mp = NULL;
386 			ASSERT(ip_recv_attr_is_mblk(mp));
387 
388 			if (atail != NULL)
389 				atail->b_next = mp;
390 			else
391 				ahead = mp;
392 			atail = mp;
393 			acnt++;
394 			mp = NULL;
395 		}
396 		/* mhip might point into 1st packet in the chain. */
397 		iras.ira_mhip = NULL;
398 	}
399 	/* Any remaining references to the route cache? */
400 	if (rtc.rtc_ire != NULL) {
401 		ASSERT(rtc.rtc_ipaddr != INADDR_ANY);
402 		ire_refrele(rtc.rtc_ire);
403 	}
404 
405 	if (ahead != NULL) {
406 		/* Better be called from ip_accept_tcp */
407 		ASSERT(target_sqp != NULL);
408 		*last = atail;
409 		*cnt = acnt;
410 		return (ahead);
411 	}
412 
413 	return (NULL);
414 }
415 
416 /*
417  * This input function is used when
418  *  - is_system_labeled()
419  *  - CGTP filtering
420  *  - DHCP unicast before we have an IP address configured
421  *  - there is an listener for IPPROTO_RSVP
422  */
423 void
424 ill_input_full_v4(mblk_t *mp, void *iph_arg, void *nexthop_arg,
425     ip_recv_attr_t *ira, rtc_t *rtc)
426 {
427 	ipha_t		*ipha = (ipha_t *)iph_arg;
428 	ipaddr_t	nexthop = *(ipaddr_t *)nexthop_arg;
429 	ill_t		*ill = ira->ira_ill;
430 	ip_stack_t	*ipst = ill->ill_ipst;
431 	int		cgtp_flt_pkt;
432 
433 	ASSERT(ira->ira_tsl == NULL);
434 
435 	/*
436 	 * Attach any necessary label information to
437 	 * this packet
438 	 */
439 	if (is_system_labeled()) {
440 		ira->ira_flags |= IRAF_SYSTEM_LABELED;
441 
442 		/*
443 		 * This updates ira_cred, ira_tsl and ira_free_flags based
444 		 * on the label.
445 		 */
446 		if (!tsol_get_pkt_label(mp, IPV4_VERSION, ira)) {
447 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
448 			ip_drop_input("ipIfStatsInDiscards", mp, ill);
449 			freemsg(mp);
450 			return;
451 		}
452 		/* Note that ira_tsl can be NULL here. */
453 
454 		/* tsol_get_pkt_label sometimes does pullupmsg */
455 		ipha = (ipha_t *)mp->b_rptr;
456 	}
457 
458 	/*
459 	 * Invoke the CGTP (multirouting) filtering module to process
460 	 * the incoming packet. Packets identified as duplicates
461 	 * must be discarded. Filtering is active only if the
462 	 * the ip_cgtp_filter ndd variable is non-zero.
463 	 */
464 	cgtp_flt_pkt = CGTP_IP_PKT_NOT_CGTP;
465 	if (ipst->ips_ip_cgtp_filter &&
466 	    ipst->ips_ip_cgtp_filter_ops != NULL) {
467 		netstackid_t stackid;
468 
469 		stackid = ipst->ips_netstack->netstack_stackid;
470 		/*
471 		 * CGTP and IPMP are mutually exclusive so
472 		 * phyint_ifindex is fine here.
473 		 */
474 		cgtp_flt_pkt =
475 		    ipst->ips_ip_cgtp_filter_ops->cfo_filter(stackid,
476 		    ill->ill_phyint->phyint_ifindex, mp);
477 		if (cgtp_flt_pkt == CGTP_IP_PKT_DUPLICATE) {
478 			ip_drop_input("CGTP_IP_PKT_DUPLICATE", mp, ill);
479 			freemsg(mp);
480 			return;
481 		}
482 	}
483 
484 	/*
485 	 * Brutal hack for DHCPv4 unicast: RFC2131 allows a DHCP
486 	 * server to unicast DHCP packets to a DHCP client using the
487 	 * IP address it is offering to the client.  This can be
488 	 * disabled through the "broadcast bit", but not all DHCP
489 	 * servers honor that bit.  Therefore, to interoperate with as
490 	 * many DHCP servers as possible, the DHCP client allows the
491 	 * server to unicast, but we treat those packets as broadcast
492 	 * here.  Note that we don't rewrite the packet itself since
493 	 * (a) that would mess up the checksums and (b) the DHCP
494 	 * client conn is bound to INADDR_ANY so ip_fanout_udp() will
495 	 * hand it the packet regardless.
496 	 */
497 	if (ill->ill_dhcpinit != 0 &&
498 	    ipha->ipha_version_and_hdr_length == IP_SIMPLE_HDR_VERSION &&
499 	    ipha->ipha_protocol == IPPROTO_UDP) {
500 		udpha_t *udpha;
501 
502 		ipha = ip_pullup(mp, sizeof (ipha_t) + sizeof (udpha_t), ira);
503 		if (ipha == NULL) {
504 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
505 			ip_drop_input("ipIfStatsInDiscards - dhcp", mp, ill);
506 			freemsg(mp);
507 			return;
508 		}
509 		/* Reload since pullupmsg() can change b_rptr. */
510 		udpha = (udpha_t *)&ipha[1];
511 
512 		if (ntohs(udpha->uha_dst_port) == IPPORT_BOOTPC) {
513 			DTRACE_PROBE2(ip4__dhcpinit__pkt, ill_t *, ill,
514 			    mblk_t *, mp);
515 			/*
516 			 * This assumes that we deliver to all conns for
517 			 * multicast and broadcast packets.
518 			 */
519 			nexthop = INADDR_BROADCAST;
520 			ira->ira_flags |= IRAF_DHCP_UNICAST;
521 		}
522 	}
523 
524 	/*
525 	 * If rsvpd is running, let RSVP daemon handle its processing
526 	 * and forwarding of RSVP multicast/unicast packets.
527 	 * If rsvpd is not running but mrouted is running, RSVP
528 	 * multicast packets are forwarded as multicast traffic
529 	 * and RSVP unicast packets are forwarded by unicast router.
530 	 * If neither rsvpd nor mrouted is running, RSVP multicast
531 	 * packets are not forwarded, but the unicast packets are
532 	 * forwarded like unicast traffic.
533 	 */
534 	if (ipha->ipha_protocol == IPPROTO_RSVP &&
535 	    ipst->ips_ipcl_proto_fanout_v4[IPPROTO_RSVP].connf_head != NULL) {
536 		/* RSVP packet and rsvpd running. Treat as ours */
537 		ip2dbg(("ip_input: RSVP for us: 0x%x\n", ntohl(nexthop)));
538 		/*
539 		 * We use a multicast address to get the packet to
540 		 * ire_recv_multicast_v4. There will not be a membership
541 		 * check since we set IRAF_RSVP
542 		 */
543 		nexthop = htonl(INADDR_UNSPEC_GROUP);
544 		ira->ira_flags |= IRAF_RSVP;
545 	}
546 
547 	ill_input_short_v4(mp, ipha, &nexthop, ira, rtc);
548 }
549 
550 /*
551  * This is the tail-end of the full receive side packet handling.
552  * It can be used directly when the configuration is simple.
553  */
554 void
555 ill_input_short_v4(mblk_t *mp, void *iph_arg, void *nexthop_arg,
556     ip_recv_attr_t *ira, rtc_t *rtc)
557 {
558 	ire_t		*ire;
559 	uint_t		opt_len;
560 	ill_t		*ill = ira->ira_ill;
561 	ip_stack_t	*ipst = ill->ill_ipst;
562 	uint_t		pkt_len;
563 	ssize_t 	len;
564 	ipha_t		*ipha = (ipha_t *)iph_arg;
565 	ipaddr_t	nexthop = *(ipaddr_t *)nexthop_arg;
566 	ilb_stack_t	*ilbs = ipst->ips_netstack->netstack_ilb;
567 	uint_t		irr_flags;
568 #define	rptr	((uchar_t *)ipha)
569 
570 	ASSERT(DB_TYPE(mp) == M_DATA);
571 
572 	/*
573 	 * The following test for loopback is faster than
574 	 * IP_LOOPBACK_ADDR(), because it avoids any bitwise
575 	 * operations.
576 	 * Note that these addresses are always in network byte order
577 	 */
578 	if (((*(uchar_t *)&ipha->ipha_dst) == 127) ||
579 	    ((*(uchar_t *)&ipha->ipha_src) == 127)) {
580 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsInAddrErrors);
581 		ip_drop_input("ipIfStatsInAddrErrors", mp, ill);
582 		freemsg(mp);
583 		return;
584 	}
585 
586 	len = mp->b_wptr - rptr;
587 	pkt_len = ira->ira_pktlen;
588 
589 	/* multiple mblk or too short */
590 	len -= pkt_len;
591 	if (len != 0) {
592 		mp = ip_check_length(mp, rptr, len, pkt_len,
593 		    IP_SIMPLE_HDR_LENGTH, ira);
594 		if (mp == NULL)
595 			return;
596 		ipha = (ipha_t *)mp->b_rptr;
597 	}
598 
599 	DTRACE_IP7(receive, mblk_t *, mp, conn_t *, NULL, void_ip_t *,
600 	    ipha, __dtrace_ipsr_ill_t *, ill, ipha_t *, ipha, ip6_t *, NULL,
601 	    int, 0);
602 
603 	/*
604 	 * The event for packets being received from a 'physical'
605 	 * interface is placed after validation of the source and/or
606 	 * destination address as being local so that packets can be
607 	 * redirected to loopback addresses using ipnat.
608 	 */
609 	DTRACE_PROBE4(ip4__physical__in__start,
610 	    ill_t *, ill, ill_t *, NULL,
611 	    ipha_t *, ipha, mblk_t *, mp);
612 
613 	if (HOOKS4_INTERESTED_PHYSICAL_IN(ipst)) {
614 		int	ll_multicast = 0;
615 		int	error;
616 		ipaddr_t orig_dst = ipha->ipha_dst;
617 
618 		if (ira->ira_flags & IRAF_L2DST_MULTICAST)
619 			ll_multicast = HPE_MULTICAST;
620 		else if (ira->ira_flags & IRAF_L2DST_BROADCAST)
621 			ll_multicast = HPE_BROADCAST;
622 
623 		FW_HOOKS(ipst->ips_ip4_physical_in_event,
624 		    ipst->ips_ipv4firewall_physical_in,
625 		    ill, NULL, ipha, mp, mp, ll_multicast, ipst, error);
626 
627 		DTRACE_PROBE1(ip4__physical__in__end, mblk_t *, mp);
628 
629 		if (mp == NULL)
630 			return;
631 		/* The length could have changed */
632 		ipha = (ipha_t *)mp->b_rptr;
633 		ira->ira_pktlen = ntohs(ipha->ipha_length);
634 		pkt_len = ira->ira_pktlen;
635 
636 		/*
637 		 * In case the destination changed we override any previous
638 		 * change to nexthop.
639 		 */
640 		if (orig_dst != ipha->ipha_dst)
641 			nexthop = ipha->ipha_dst;
642 		if (nexthop == INADDR_ANY) {
643 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInAddrErrors);
644 			ip_drop_input("ipIfStatsInAddrErrors", mp, ill);
645 			freemsg(mp);
646 			return;
647 		}
648 	}
649 
650 	if (ipst->ips_ip4_observe.he_interested) {
651 		zoneid_t dzone;
652 
653 		/*
654 		 * On the inbound path the src zone will be unknown as
655 		 * this packet has come from the wire.
656 		 */
657 		dzone = ip_get_zoneid_v4(nexthop, mp, ira, ALL_ZONES);
658 		ipobs_hook(mp, IPOBS_HOOK_INBOUND, ALL_ZONES, dzone, ill, ipst);
659 	}
660 
661 	/*
662 	 * If there is a good HW IP header checksum we clear the need
663 	 * look at the IP header checksum.
664 	 */
665 	if ((DB_CKSUMFLAGS(mp) & HCK_IPV4_HDRCKSUM) &&
666 	    ILL_HCKSUM_CAPABLE(ill) && dohwcksum) {
667 		/* Header checksum was ok. Clear the flag */
668 		DB_CKSUMFLAGS(mp) &= ~HCK_IPV4_HDRCKSUM;
669 		ira->ira_flags &= ~IRAF_VERIFY_IP_CKSUM;
670 	}
671 
672 	/*
673 	 * Here we check to see if we machine is setup as
674 	 * L3 loadbalancer and if the incoming packet is for a VIP
675 	 *
676 	 * Check the following:
677 	 * - there is at least a rule
678 	 * - protocol of the packet is supported
679 	 */
680 	if (ilb_has_rules(ilbs) && ILB_SUPP_L4(ipha->ipha_protocol)) {
681 		ipaddr_t	lb_dst;
682 		int		lb_ret;
683 
684 		/* For convenience, we pull up the mblk. */
685 		if (mp->b_cont != NULL) {
686 			if (pullupmsg(mp, -1) == 0) {
687 				BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
688 				ip_drop_input("ipIfStatsInDiscards - pullupmsg",
689 				    mp, ill);
690 				freemsg(mp);
691 				return;
692 			}
693 			ipha = (ipha_t *)mp->b_rptr;
694 		}
695 
696 		/*
697 		 * We just drop all fragments going to any VIP, at
698 		 * least for now....
699 		 */
700 		if (ntohs(ipha->ipha_fragment_offset_and_flags) &
701 		    (IPH_MF | IPH_OFFSET)) {
702 			if (!ilb_rule_match_vip_v4(ilbs, nexthop, NULL)) {
703 				goto after_ilb;
704 			}
705 
706 			ILB_KSTAT_UPDATE(ilbs, ip_frag_in, 1);
707 			ILB_KSTAT_UPDATE(ilbs, ip_frag_dropped, 1);
708 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
709 			ip_drop_input("ILB fragment", mp, ill);
710 			freemsg(mp);
711 			return;
712 		}
713 		lb_ret = ilb_check_v4(ilbs, ill, mp, ipha, ipha->ipha_protocol,
714 		    (uint8_t *)ipha + IPH_HDR_LENGTH(ipha), &lb_dst);
715 
716 		if (lb_ret == ILB_DROPPED) {
717 			/* Is this the right counter to increase? */
718 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
719 			ip_drop_input("ILB_DROPPED", mp, ill);
720 			freemsg(mp);
721 			return;
722 		}
723 		if (lb_ret == ILB_BALANCED) {
724 			/* Set the dst to that of the chosen server */
725 			nexthop = lb_dst;
726 			DB_CKSUMFLAGS(mp) = 0;
727 		}
728 	}
729 
730 after_ilb:
731 	opt_len = ipha->ipha_version_and_hdr_length - IP_SIMPLE_HDR_VERSION;
732 	ira->ira_ip_hdr_length = IP_SIMPLE_HDR_LENGTH;
733 	if (opt_len != 0) {
734 		int error = 0;
735 
736 		ira->ira_ip_hdr_length += (opt_len << 2);
737 		ira->ira_flags |= IRAF_IPV4_OPTIONS;
738 
739 		/* IP Options present!  Validate the length. */
740 		mp = ip_check_optlen(mp, ipha, opt_len, pkt_len, ira);
741 		if (mp == NULL)
742 			return;
743 
744 		/* Might have changed */
745 		ipha = (ipha_t *)mp->b_rptr;
746 
747 		/* Verify IP header checksum before parsing the options */
748 		if ((ira->ira_flags & IRAF_VERIFY_IP_CKSUM) &&
749 		    ip_csum_hdr(ipha)) {
750 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs);
751 			ip_drop_input("ipIfStatsInCksumErrs", mp, ill);
752 			freemsg(mp);
753 			return;
754 		}
755 		ira->ira_flags &= ~IRAF_VERIFY_IP_CKSUM;
756 
757 		/*
758 		 * Go off to ip_input_options which returns the next hop
759 		 * destination address, which may have been affected
760 		 * by source routing.
761 		 */
762 		IP_STAT(ipst, ip_opt);
763 
764 		nexthop = ip_input_options(ipha, nexthop, mp, ira, &error);
765 		if (error != 0) {
766 			/*
767 			 * An ICMP error has been sent and the packet has
768 			 * been dropped.
769 			 */
770 			return;
771 		}
772 	}
773 
774 	if (ill->ill_flags & ILLF_ROUTER)
775 		irr_flags = IRR_ALLOCATE;
776 	else
777 		irr_flags = IRR_NONE;
778 
779 	/* Can not use route cache with TX since the labels can differ */
780 	if (ira->ira_flags & IRAF_SYSTEM_LABELED) {
781 		if (CLASSD(nexthop)) {
782 			ire = ire_multicast(ill);
783 		} else {
784 			/* Match destination and label */
785 			ire = ire_route_recursive_v4(nexthop, 0, NULL,
786 			    ALL_ZONES, ira->ira_tsl, MATCH_IRE_SECATTR,
787 			    irr_flags, ira->ira_xmit_hint, ipst, NULL, NULL,
788 			    NULL);
789 		}
790 		/* Update the route cache so we do the ire_refrele */
791 		ASSERT(ire != NULL);
792 		if (rtc->rtc_ire != NULL)
793 			ire_refrele(rtc->rtc_ire);
794 		rtc->rtc_ire = ire;
795 		rtc->rtc_ipaddr = nexthop;
796 	} else if (nexthop == rtc->rtc_ipaddr) {
797 		/* Use the route cache */
798 		ASSERT(rtc->rtc_ire != NULL);
799 		ire = rtc->rtc_ire;
800 	} else {
801 		/* Update the route cache */
802 		if (CLASSD(nexthop)) {
803 			ire = ire_multicast(ill);
804 		} else {
805 			/* Just match the destination */
806 			ire = ire_route_recursive_dstonly_v4(nexthop, irr_flags,
807 			    ira->ira_xmit_hint, ipst);
808 		}
809 		ASSERT(ire != NULL);
810 		if (rtc->rtc_ire != NULL)
811 			ire_refrele(rtc->rtc_ire);
812 		rtc->rtc_ire = ire;
813 		rtc->rtc_ipaddr = nexthop;
814 	}
815 
816 	ire->ire_ib_pkt_count++;
817 
818 	/*
819 	 * Based on ire_type and ire_flags call one of:
820 	 *	ire_recv_local_v4 - for IRE_LOCAL
821 	 *	ire_recv_loopback_v4 - for IRE_LOOPBACK
822 	 *	ire_recv_multirt_v4 - if RTF_MULTIRT
823 	 *	ire_recv_noroute_v4 - if RTF_REJECT or RTF_BLACHOLE
824 	 *	ire_recv_multicast_v4 - for IRE_MULTICAST
825 	 *	ire_recv_broadcast_v4 - for IRE_BROADCAST
826 	 *	ire_recv_noaccept_v4 - for ire_noaccept ones
827 	 *	ire_recv_forward_v4 - for the rest.
828 	 */
829 	(*ire->ire_recvfn)(ire, mp, ipha, ira);
830 }
831 #undef rptr
832 
833 /*
834  * ire_recvfn for IREs that need forwarding
835  */
836 void
837 ire_recv_forward_v4(ire_t *ire, mblk_t *mp, void *iph_arg, ip_recv_attr_t *ira)
838 {
839 	ipha_t		*ipha = (ipha_t *)iph_arg;
840 	ill_t		*ill = ira->ira_ill;
841 	ip_stack_t	*ipst = ill->ill_ipst;
842 	ill_t		*dst_ill;
843 	nce_t		*nce;
844 	ipaddr_t	src = ipha->ipha_src;
845 	uint32_t	added_tx_len;
846 	uint32_t	mtu, iremtu;
847 
848 	if (ira->ira_flags & (IRAF_L2DST_MULTICAST|IRAF_L2DST_BROADCAST)) {
849 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits);
850 		ip_drop_input("l2 multicast not forwarded", mp, ill);
851 		freemsg(mp);
852 		return;
853 	}
854 
855 	if (!(ill->ill_flags & ILLF_ROUTER) && !ip_source_routed(ipha, ipst)) {
856 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits);
857 		ip_drop_input("ipIfStatsForwProhibits", mp, ill);
858 		freemsg(mp);
859 		return;
860 	}
861 
862 	/*
863 	 * Either ire_nce_capable or ire_dep_parent would be set for the IRE
864 	 * when it is found by ire_route_recursive, but that some other thread
865 	 * could have changed the routes with the effect of clearing
866 	 * ire_dep_parent. In that case we'd end up dropping the packet, or
867 	 * finding a new nce below.
868 	 * Get, allocate, or update the nce.
869 	 * We get a refhold on ire_nce_cache as a result of this to avoid races
870 	 * where ire_nce_cache is deleted.
871 	 *
872 	 * This ensures that we don't forward if the interface is down since
873 	 * ipif_down removes all the nces.
874 	 */
875 	mutex_enter(&ire->ire_lock);
876 	nce = ire->ire_nce_cache;
877 	if (nce == NULL) {
878 		/* Not yet set up - try to set one up */
879 		mutex_exit(&ire->ire_lock);
880 		(void) ire_revalidate_nce(ire);
881 		mutex_enter(&ire->ire_lock);
882 		nce = ire->ire_nce_cache;
883 		if (nce == NULL) {
884 			mutex_exit(&ire->ire_lock);
885 			/* The ire_dep_parent chain went bad, or no memory */
886 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
887 			ip_drop_input("No ire_dep_parent", mp, ill);
888 			freemsg(mp);
889 			return;
890 		}
891 	}
892 	nce_refhold(nce);
893 	mutex_exit(&ire->ire_lock);
894 
895 	if (nce->nce_is_condemned) {
896 		nce_t *nce1;
897 
898 		nce1 = ire_handle_condemned_nce(nce, ire, ipha, NULL, B_FALSE);
899 		nce_refrele(nce);
900 		if (nce1 == NULL) {
901 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
902 			ip_drop_input("No nce", mp, ill);
903 			freemsg(mp);
904 			return;
905 		}
906 		nce = nce1;
907 	}
908 	dst_ill = nce->nce_ill;
909 
910 	/*
911 	 * Unless we are forwarding, drop the packet.
912 	 * We have to let source routed packets through if they go out
913 	 * the same interface i.e., they are 'ping -l' packets.
914 	 */
915 	if (!(dst_ill->ill_flags & ILLF_ROUTER) &&
916 	    !(ip_source_routed(ipha, ipst) && dst_ill == ill)) {
917 		if (ip_source_routed(ipha, ipst)) {
918 			ip_drop_input("ICMP_SOURCE_ROUTE_FAILED", mp, ill);
919 			icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira);
920 			nce_refrele(nce);
921 			return;
922 		}
923 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits);
924 		ip_drop_input("ipIfStatsForwProhibits", mp, ill);
925 		freemsg(mp);
926 		nce_refrele(nce);
927 		return;
928 	}
929 
930 	if (ire->ire_zoneid != GLOBAL_ZONEID && ire->ire_zoneid != ALL_ZONES) {
931 		ipaddr_t	dst = ipha->ipha_dst;
932 
933 		ire->ire_ib_pkt_count--;
934 		/*
935 		 * Should only use IREs that are visible from the
936 		 * global zone for forwarding.
937 		 * Take a source route into account the same way as ip_input
938 		 * did.
939 		 */
940 		if (ira->ira_flags & IRAF_IPV4_OPTIONS) {
941 			int		error = 0;
942 
943 			dst = ip_input_options(ipha, dst, mp, ira, &error);
944 			ASSERT(error == 0);	/* ip_input checked */
945 		}
946 		ire = ire_route_recursive_v4(dst, 0, NULL, GLOBAL_ZONEID,
947 		    ira->ira_tsl, MATCH_IRE_SECATTR,
948 		    (ill->ill_flags & ILLF_ROUTER) ? IRR_ALLOCATE : IRR_NONE,
949 		    ira->ira_xmit_hint, ipst, NULL, NULL, NULL);
950 		ire->ire_ib_pkt_count++;
951 		(*ire->ire_recvfn)(ire, mp, ipha, ira);
952 		ire_refrele(ire);
953 		nce_refrele(nce);
954 		return;
955 	}
956 
957 	/*
958 	 * ipIfStatsHCInForwDatagrams should only be increment if there
959 	 * will be an attempt to forward the packet, which is why we
960 	 * increment after the above condition has been checked.
961 	 */
962 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams);
963 
964 	/* Initiate Read side IPPF processing */
965 	if (IPP_ENABLED(IPP_FWD_IN, ipst)) {
966 		/* ip_process translates an IS_UNDER_IPMP */
967 		mp = ip_process(IPP_FWD_IN, mp, ill, ill);
968 		if (mp == NULL) {
969 			/* ip_drop_packet and MIB done */
970 			ip2dbg(("ire_recv_forward_v4: pkt dropped/deferred "
971 			    "during IPPF processing\n"));
972 			nce_refrele(nce);
973 			return;
974 		}
975 	}
976 
977 	DTRACE_PROBE4(ip4__forwarding__start,
978 	    ill_t *, ill, ill_t *, dst_ill, ipha_t *, ipha, mblk_t *, mp);
979 
980 	if (HOOKS4_INTERESTED_FORWARDING(ipst)) {
981 		int error;
982 
983 		FW_HOOKS(ipst->ips_ip4_forwarding_event,
984 		    ipst->ips_ipv4firewall_forwarding,
985 		    ill, dst_ill, ipha, mp, mp, 0, ipst, error);
986 
987 		DTRACE_PROBE1(ip4__forwarding__end, mblk_t *, mp);
988 
989 		if (mp == NULL) {
990 			nce_refrele(nce);
991 			return;
992 		}
993 		/*
994 		 * Even if the destination was changed by the filter we use the
995 		 * forwarding decision that was made based on the address
996 		 * in ip_input.
997 		 */
998 
999 		/* Might have changed */
1000 		ipha = (ipha_t *)mp->b_rptr;
1001 		ira->ira_pktlen = ntohs(ipha->ipha_length);
1002 	}
1003 
1004 	/* Packet is being forwarded. Turning off hwcksum flag. */
1005 	DB_CKSUMFLAGS(mp) = 0;
1006 
1007 	/*
1008 	 * Martian Address Filtering [RFC 1812, Section 5.3.7]
1009 	 * The loopback address check for both src and dst has already
1010 	 * been checked in ip_input
1011 	 * In the future one can envision adding RPF checks using number 3.
1012 	 * If we already checked the same source address we can skip this.
1013 	 */
1014 	if (!(ira->ira_flags & IRAF_VERIFIED_SRC) ||
1015 	    src != ira->ira_verified_src) {
1016 		switch (ipst->ips_src_check) {
1017 		case 0:
1018 			break;
1019 		case 2:
1020 			if (ip_type_v4(src, ipst) == IRE_BROADCAST) {
1021 				BUMP_MIB(ill->ill_ip_mib,
1022 				    ipIfStatsForwProhibits);
1023 				BUMP_MIB(ill->ill_ip_mib,
1024 				    ipIfStatsInAddrErrors);
1025 				ip_drop_input("ipIfStatsInAddrErrors", mp, ill);
1026 				freemsg(mp);
1027 				nce_refrele(nce);
1028 				return;
1029 			}
1030 			/* FALLTHRU */
1031 
1032 		case 1:
1033 			if (CLASSD(src)) {
1034 				BUMP_MIB(ill->ill_ip_mib,
1035 				    ipIfStatsForwProhibits);
1036 				BUMP_MIB(ill->ill_ip_mib,
1037 				    ipIfStatsInAddrErrors);
1038 				ip_drop_input("ipIfStatsInAddrErrors", mp, ill);
1039 				freemsg(mp);
1040 				nce_refrele(nce);
1041 				return;
1042 			}
1043 			break;
1044 		}
1045 		/* Remember for next packet */
1046 		ira->ira_flags |= IRAF_VERIFIED_SRC;
1047 		ira->ira_verified_src = src;
1048 	}
1049 
1050 	/*
1051 	 * Check if packet is going out the same link on which it arrived.
1052 	 * Means we might need to send a redirect.
1053 	 */
1054 	if (IS_ON_SAME_LAN(dst_ill, ill) && ipst->ips_ip_g_send_redirects) {
1055 		ip_send_potential_redirect_v4(mp, ipha, ire, ira);
1056 	}
1057 
1058 	added_tx_len = 0;
1059 	if (ira->ira_flags & IRAF_SYSTEM_LABELED) {
1060 		mblk_t		*mp1;
1061 		uint32_t	old_pkt_len = ira->ira_pktlen;
1062 
1063 		/* Verify IP header checksum before adding/removing options */
1064 		if ((ira->ira_flags & IRAF_VERIFY_IP_CKSUM) &&
1065 		    ip_csum_hdr(ipha)) {
1066 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs);
1067 			ip_drop_input("ipIfStatsInCksumErrs", mp, ill);
1068 			freemsg(mp);
1069 			nce_refrele(nce);
1070 			return;
1071 		}
1072 		ira->ira_flags &= ~IRAF_VERIFY_IP_CKSUM;
1073 
1074 		/*
1075 		 * Check if it can be forwarded and add/remove
1076 		 * CIPSO options as needed.
1077 		 */
1078 		if ((mp1 = tsol_ip_forward(ire, mp, ira)) == NULL) {
1079 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits);
1080 			ip_drop_input("tsol_ip_forward", mp, ill);
1081 			freemsg(mp);
1082 			nce_refrele(nce);
1083 			return;
1084 		}
1085 		/*
1086 		 * Size may have changed. Remember amount added in case
1087 		 * IP needs to send an ICMP too big.
1088 		 */
1089 		mp = mp1;
1090 		ipha = (ipha_t *)mp->b_rptr;
1091 		ira->ira_pktlen = ntohs(ipha->ipha_length);
1092 		ira->ira_ip_hdr_length = IPH_HDR_LENGTH(ipha);
1093 		if (ira->ira_pktlen > old_pkt_len)
1094 			added_tx_len = ira->ira_pktlen - old_pkt_len;
1095 
1096 		/* Options can have been added or removed */
1097 		if (ira->ira_ip_hdr_length != IP_SIMPLE_HDR_LENGTH)
1098 			ira->ira_flags |= IRAF_IPV4_OPTIONS;
1099 		else
1100 			ira->ira_flags &= ~IRAF_IPV4_OPTIONS;
1101 	}
1102 
1103 	mtu = dst_ill->ill_mtu;
1104 	if ((iremtu = ire->ire_metrics.iulp_mtu) != 0 && iremtu < mtu)
1105 		mtu = iremtu;
1106 	ip_forward_xmit_v4(nce, ill, mp, ipha, ira, mtu, added_tx_len);
1107 	nce_refrele(nce);
1108 }
1109 
1110 /*
1111  * Used for sending out unicast and multicast packets that are
1112  * forwarded.
1113  */
1114 void
1115 ip_forward_xmit_v4(nce_t *nce, ill_t *ill, mblk_t *mp, ipha_t *ipha,
1116     ip_recv_attr_t *ira, uint32_t mtu, uint32_t added_tx_len)
1117 {
1118 	ill_t		*dst_ill = nce->nce_ill;
1119 	uint32_t	pkt_len;
1120 	uint32_t	sum;
1121 	iaflags_t	iraflags = ira->ira_flags;
1122 	ip_stack_t	*ipst = ill->ill_ipst;
1123 	iaflags_t	ixaflags;
1124 
1125 	if (ipha->ipha_ttl <= 1) {
1126 		/* Perhaps the checksum was bad */
1127 		if ((iraflags & IRAF_VERIFY_IP_CKSUM) && ip_csum_hdr(ipha)) {
1128 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs);
1129 			ip_drop_input("ipIfStatsInCksumErrs", mp, ill);
1130 			freemsg(mp);
1131 			return;
1132 		}
1133 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1134 		ip_drop_input("ICMP_TTL_EXCEEDED", mp, ill);
1135 		icmp_time_exceeded(mp, ICMP_TTL_EXCEEDED, ira);
1136 		return;
1137 	}
1138 	ipha->ipha_ttl--;
1139 	/* Adjust the checksum to reflect the ttl decrement. */
1140 	sum = (int)ipha->ipha_hdr_checksum + IP_HDR_CSUM_TTL_ADJUST;
1141 	ipha->ipha_hdr_checksum = (uint16_t)(sum + (sum >> 16));
1142 
1143 	/* Check if there are options to update */
1144 	if (iraflags & IRAF_IPV4_OPTIONS) {
1145 		ASSERT(ipha->ipha_version_and_hdr_length !=
1146 		    IP_SIMPLE_HDR_VERSION);
1147 		ASSERT(!(iraflags & IRAF_VERIFY_IP_CKSUM));
1148 
1149 		if (!ip_forward_options(mp, ipha, dst_ill, ira)) {
1150 			/* ipIfStatsForwProhibits and ip_drop_input done */
1151 			return;
1152 		}
1153 
1154 		ipha->ipha_hdr_checksum = 0;
1155 		ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
1156 	}
1157 
1158 	/* Initiate Write side IPPF processing before any fragmentation */
1159 	if (IPP_ENABLED(IPP_FWD_OUT, ipst)) {
1160 		/* ip_process translates an IS_UNDER_IPMP */
1161 		mp = ip_process(IPP_FWD_OUT, mp, dst_ill, dst_ill);
1162 		if (mp == NULL) {
1163 			/* ip_drop_packet and MIB done */
1164 			ip2dbg(("ire_recv_forward_v4: pkt dropped/deferred" \
1165 			    " during IPPF processing\n"));
1166 			return;
1167 		}
1168 	}
1169 
1170 	pkt_len = ira->ira_pktlen;
1171 
1172 	BUMP_MIB(dst_ill->ill_ip_mib, ipIfStatsHCOutForwDatagrams);
1173 
1174 	ixaflags = IXAF_IS_IPV4 | IXAF_NO_DEV_FLOW_CTL;
1175 
1176 	if (pkt_len > mtu) {
1177 		/*
1178 		 * It needs fragging on its way out.  If we haven't
1179 		 * verified the header checksum yet we do it now since
1180 		 * are going to put a surely good checksum in the
1181 		 * outgoing header, we have to make sure that it
1182 		 * was good coming in.
1183 		 */
1184 		if ((iraflags & IRAF_VERIFY_IP_CKSUM) && ip_csum_hdr(ipha)) {
1185 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs);
1186 			ip_drop_input("ipIfStatsInCksumErrs", mp, ill);
1187 			freemsg(mp);
1188 			return;
1189 		}
1190 		if (ipha->ipha_fragment_offset_and_flags & IPH_DF_HTONS) {
1191 			BUMP_MIB(dst_ill->ill_ip_mib, ipIfStatsOutFragFails);
1192 			ip_drop_output("ipIfStatsOutFragFails", mp, dst_ill);
1193 			if (iraflags & IRAF_SYSTEM_LABELED) {
1194 				/*
1195 				 * Remove any CIPSO option added by
1196 				 * tsol_ip_forward, and make sure we report
1197 				 * a path MTU so that there
1198 				 * is room to add such a CIPSO option for future
1199 				 * packets.
1200 				 */
1201 				mtu = tsol_pmtu_adjust(mp, mtu, added_tx_len,
1202 				    AF_INET);
1203 			}
1204 
1205 			icmp_frag_needed(mp, mtu, ira);
1206 			return;
1207 		}
1208 
1209 		(void) ip_fragment_v4(mp, nce, ixaflags, pkt_len, mtu,
1210 		    ira->ira_xmit_hint, GLOBAL_ZONEID, 0, ip_xmit, NULL);
1211 		return;
1212 	}
1213 
1214 	ASSERT(pkt_len == ntohs(((ipha_t *)mp->b_rptr)->ipha_length));
1215 	if (iraflags & IRAF_LOOPBACK_COPY) {
1216 		/*
1217 		 * IXAF_NO_LOOP_ZONEID is not set hence 7th arg
1218 		 * is don't care
1219 		 */
1220 		(void) ip_postfrag_loopcheck(mp, nce,
1221 		    ixaflags | IXAF_LOOPBACK_COPY,
1222 		    pkt_len, ira->ira_xmit_hint, GLOBAL_ZONEID, 0, NULL);
1223 	} else {
1224 		(void) ip_xmit(mp, nce, ixaflags, pkt_len, ira->ira_xmit_hint,
1225 		    GLOBAL_ZONEID, 0, NULL);
1226 	}
1227 }
1228 
1229 /*
1230  * ire_recvfn for RTF_REJECT and RTF_BLACKHOLE routes, including IRE_NOROUTE,
1231  * which is what ire_route_recursive returns when there is no matching ire.
1232  * Send ICMP unreachable unless blackhole.
1233  */
1234 void
1235 ire_recv_noroute_v4(ire_t *ire, mblk_t *mp, void *iph_arg, ip_recv_attr_t *ira)
1236 {
1237 	ipha_t		*ipha = (ipha_t *)iph_arg;
1238 	ill_t		*ill = ira->ira_ill;
1239 	ip_stack_t	*ipst = ill->ill_ipst;
1240 
1241 	/* Would we have forwarded this packet if we had a route? */
1242 	if (ira->ira_flags & (IRAF_L2DST_MULTICAST|IRAF_L2DST_BROADCAST)) {
1243 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits);
1244 		ip_drop_input("l2 multicast not forwarded", mp, ill);
1245 		freemsg(mp);
1246 		return;
1247 	}
1248 
1249 	if (!(ill->ill_flags & ILLF_ROUTER)) {
1250 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits);
1251 		ip_drop_input("ipIfStatsForwProhibits", mp, ill);
1252 		freemsg(mp);
1253 		return;
1254 	}
1255 	/*
1256 	 * If we had a route this could have been forwarded. Count as such.
1257 	 *
1258 	 * ipIfStatsHCInForwDatagrams should only be increment if there
1259 	 * will be an attempt to forward the packet, which is why we
1260 	 * increment after the above condition has been checked.
1261 	 */
1262 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInForwDatagrams);
1263 
1264 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsInNoRoutes);
1265 
1266 	ip_rts_change(RTM_MISS, ipha->ipha_dst, 0, 0, 0, 0, 0, 0, RTA_DST,
1267 	    ipst);
1268 
1269 	if (ire->ire_flags & RTF_BLACKHOLE) {
1270 		ip_drop_input("ipIfStatsInNoRoutes RTF_BLACKHOLE", mp, ill);
1271 		freemsg(mp);
1272 	} else {
1273 		ip_drop_input("ipIfStatsInNoRoutes RTF_REJECT", mp, ill);
1274 
1275 		if (ip_source_routed(ipha, ipst)) {
1276 			icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED, ira);
1277 		} else {
1278 			icmp_unreachable(mp, ICMP_HOST_UNREACHABLE, ira);
1279 		}
1280 	}
1281 }
1282 
1283 /*
1284  * ire_recvfn for IRE_LOCALs marked with ire_noaccept. Such IREs are used for
1285  * VRRP when in noaccept mode.
1286  * We silently drop the packet. ARP handles packets even if noaccept is set.
1287  */
1288 /* ARGSUSED */
1289 void
1290 ire_recv_noaccept_v4(ire_t *ire, mblk_t *mp, void *iph_arg,
1291     ip_recv_attr_t *ira)
1292 {
1293 	ill_t		*ill = ira->ira_ill;
1294 
1295 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1296 	ip_drop_input("ipIfStatsInDiscards - noaccept", mp, ill);
1297 	freemsg(mp);
1298 }
1299 
1300 /*
1301  * ire_recvfn for IRE_BROADCAST.
1302  */
1303 void
1304 ire_recv_broadcast_v4(ire_t *ire, mblk_t *mp, void *iph_arg,
1305     ip_recv_attr_t *ira)
1306 {
1307 	ipha_t		*ipha = (ipha_t *)iph_arg;
1308 	ill_t		*ill = ira->ira_ill;
1309 	ill_t		*dst_ill = ire->ire_ill;
1310 	ip_stack_t	*ipst = ill->ill_ipst;
1311 	ire_t		*alt_ire;
1312 	nce_t		*nce;
1313 	ipaddr_t	ipha_dst;
1314 
1315 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInBcastPkts);
1316 
1317 	/* Tag for higher-level protocols */
1318 	ira->ira_flags |= IRAF_BROADCAST;
1319 
1320 	/*
1321 	 * Whether local or directed broadcast forwarding: don't allow
1322 	 * for TCP.
1323 	 */
1324 	if (ipha->ipha_protocol == IPPROTO_TCP) {
1325 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1326 		ip_drop_input("ipIfStatsInDiscards", mp, ill);
1327 		freemsg(mp);
1328 		return;
1329 	}
1330 
1331 	/*
1332 	 * So that we don't end up with dups, only one ill an IPMP group is
1333 	 * nominated to receive broadcast traffic.
1334 	 * If we have no cast_ill we are liberal and accept everything.
1335 	 */
1336 	if (IS_UNDER_IPMP(ill)) {
1337 		/* For an under ill_grp can change under lock */
1338 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
1339 		if (!ill->ill_nom_cast && ill->ill_grp != NULL &&
1340 		    ill->ill_grp->ig_cast_ill != NULL) {
1341 			rw_exit(&ipst->ips_ill_g_lock);
1342 			/* No MIB since this is normal operation */
1343 			ip_drop_input("not nom_cast", mp, ill);
1344 			freemsg(mp);
1345 			return;
1346 		}
1347 		rw_exit(&ipst->ips_ill_g_lock);
1348 
1349 		ira->ira_ruifindex = ill_get_upper_ifindex(ill);
1350 	}
1351 
1352 	/*
1353 	 * After reassembly and IPsec we will need to duplicate the
1354 	 * broadcast packet for all matching zones on the ill.
1355 	 */
1356 	ira->ira_zoneid = ALL_ZONES;
1357 
1358 	/*
1359 	 * Check for directed broadcast i.e. ire->ire_ill is different than
1360 	 * the incoming ill.
1361 	 * The same broadcast address can be assigned to multiple interfaces
1362 	 * so have to check explicitly for that case by looking up the alt_ire
1363 	 */
1364 	if (dst_ill == ill && !(ire->ire_flags & RTF_MULTIRT)) {
1365 		/* Reassemble on the ill on which the packet arrived */
1366 		ip_input_local_v4(ire, mp, ipha, ira);
1367 		/* Restore */
1368 		ira->ira_ruifindex = ill->ill_phyint->phyint_ifindex;
1369 		return;
1370 	}
1371 
1372 	/* Is there an IRE_BROADCAST on the incoming ill? */
1373 	ipha_dst = ((ira->ira_flags & IRAF_DHCP_UNICAST) ? INADDR_BROADCAST :
1374 	    ipha->ipha_dst);
1375 	alt_ire = ire_ftable_lookup_v4(ipha_dst, 0, 0, IRE_BROADCAST, ill,
1376 	    ALL_ZONES, ira->ira_tsl,
1377 	    MATCH_IRE_TYPE|MATCH_IRE_ILL|MATCH_IRE_SECATTR, 0, ipst, NULL);
1378 	if (alt_ire != NULL) {
1379 		/* Not a directed broadcast */
1380 		/*
1381 		 * In the special case of multirouted broadcast
1382 		 * packets, we unconditionally need to "gateway"
1383 		 * them to the appropriate interface here so that reassembly
1384 		 * works. We know that the IRE_BROADCAST on cgtp0 doesn't
1385 		 * have RTF_MULTIRT set so we look for such an IRE in the
1386 		 * bucket.
1387 		 */
1388 		if (alt_ire->ire_flags & RTF_MULTIRT) {
1389 			irb_t		*irb;
1390 			ire_t		*ire1;
1391 
1392 			irb = ire->ire_bucket;
1393 			irb_refhold(irb);
1394 			for (ire1 = irb->irb_ire; ire1 != NULL;
1395 			    ire1 = ire1->ire_next) {
1396 				if (IRE_IS_CONDEMNED(ire1))
1397 					continue;
1398 				if (!(ire1->ire_type & IRE_BROADCAST) ||
1399 				    (ire1->ire_flags & RTF_MULTIRT))
1400 					continue;
1401 				ill = ire1->ire_ill;
1402 				ill_refhold(ill);
1403 				break;
1404 			}
1405 			irb_refrele(irb);
1406 			if (ire1 != NULL) {
1407 				ill_t *orig_ill = ira->ira_ill;
1408 
1409 				ire_refrele(alt_ire);
1410 				/* Reassemble on the new ill */
1411 				ira->ira_ill = ill;
1412 				ip_input_local_v4(ire, mp, ipha, ira);
1413 				ill_refrele(ill);
1414 				/* Restore */
1415 				ira->ira_ill = orig_ill;
1416 				ira->ira_ruifindex =
1417 				    orig_ill->ill_phyint->phyint_ifindex;
1418 				return;
1419 			}
1420 		}
1421 		ire_refrele(alt_ire);
1422 		/* Reassemble on the ill on which the packet arrived */
1423 		ip_input_local_v4(ire, mp, ipha, ira);
1424 		goto done;
1425 	}
1426 
1427 	/*
1428 	 * This is a directed broadcast
1429 	 *
1430 	 * If directed broadcast is allowed, then forward the packet out
1431 	 * the destination interface with IXAF_LOOPBACK_COPY set. That will
1432 	 * result in ip_input() receiving a copy of the packet on the
1433 	 * appropriate ill. (We could optimize this to avoid the extra trip
1434 	 * via ip_input(), but since directed broadcasts are normally disabled
1435 	 * it doesn't make sense to optimize it.)
1436 	 */
1437 	if (!ipst->ips_ip_g_forward_directed_bcast ||
1438 	    (ira->ira_flags & (IRAF_L2DST_MULTICAST|IRAF_L2DST_BROADCAST))) {
1439 		ip_drop_input("directed broadcast not allowed", mp, ill);
1440 		freemsg(mp);
1441 		goto done;
1442 	}
1443 	if ((ira->ira_flags & IRAF_VERIFY_IP_CKSUM) && ip_csum_hdr(ipha)) {
1444 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs);
1445 		ip_drop_input("ipIfStatsInCksumErrs", mp, ill);
1446 		freemsg(mp);
1447 		goto done;
1448 	}
1449 
1450 	/*
1451 	 * Clear the indication that this may have hardware
1452 	 * checksum as we are not using it for forwarding.
1453 	 */
1454 	DB_CKSUMFLAGS(mp) = 0;
1455 
1456 	/*
1457 	 * Adjust ttl to 2 (1+1 - the forward engine will decrement it by one.
1458 	 */
1459 	ipha->ipha_ttl = ipst->ips_ip_broadcast_ttl + 1;
1460 	ipha->ipha_hdr_checksum = 0;
1461 	ipha->ipha_hdr_checksum = ip_csum_hdr(ipha);
1462 
1463 	/*
1464 	 * We use ip_forward_xmit to do any fragmentation.
1465 	 * and loopback copy on the outbound interface.
1466 	 *
1467 	 * Make it so that IXAF_LOOPBACK_COPY to be set on transmit side.
1468 	 */
1469 	ira->ira_flags |= IRAF_LOOPBACK_COPY;
1470 
1471 	nce = arp_nce_init(dst_ill, ipha->ipha_dst, IRE_BROADCAST);
1472 	if (nce == NULL) {
1473 		BUMP_MIB(dst_ill->ill_ip_mib, ipIfStatsOutDiscards);
1474 		ip_drop_output("No nce", mp, dst_ill);
1475 		freemsg(mp);
1476 		goto done;
1477 	}
1478 
1479 	ip_forward_xmit_v4(nce, ill, mp, ipha, ira, dst_ill->ill_mtu, 0);
1480 	nce_refrele(nce);
1481 done:
1482 	/* Restore */
1483 	ira->ira_ruifindex = ill->ill_phyint->phyint_ifindex;
1484 }
1485 
1486 /*
1487  * ire_recvfn for IRE_MULTICAST.
1488  */
1489 void
1490 ire_recv_multicast_v4(ire_t *ire, mblk_t *mp, void *iph_arg,
1491     ip_recv_attr_t *ira)
1492 {
1493 	ipha_t		*ipha = (ipha_t *)iph_arg;
1494 	ill_t		*ill = ira->ira_ill;
1495 	ip_stack_t	*ipst = ill->ill_ipst;
1496 
1497 	ASSERT(ire->ire_ill == ira->ira_ill);
1498 
1499 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInMcastPkts);
1500 	UPDATE_MIB(ill->ill_ip_mib, ipIfStatsHCInMcastOctets, ira->ira_pktlen);
1501 
1502 	/* RSVP hook */
1503 	if (ira->ira_flags & IRAF_RSVP)
1504 		goto forus;
1505 
1506 	/* Tag for higher-level protocols */
1507 	ira->ira_flags |= IRAF_MULTICAST;
1508 
1509 	/*
1510 	 * So that we don't end up with dups, only one ill an IPMP group is
1511 	 * nominated to receive multicast traffic.
1512 	 * If we have no cast_ill we are liberal and accept everything.
1513 	 */
1514 	if (IS_UNDER_IPMP(ill)) {
1515 		ip_stack_t	*ipst = ill->ill_ipst;
1516 
1517 		/* For an under ill_grp can change under lock */
1518 		rw_enter(&ipst->ips_ill_g_lock, RW_READER);
1519 		if (!ill->ill_nom_cast && ill->ill_grp != NULL &&
1520 		    ill->ill_grp->ig_cast_ill != NULL) {
1521 			rw_exit(&ipst->ips_ill_g_lock);
1522 			ip_drop_input("not on cast ill", mp, ill);
1523 			freemsg(mp);
1524 			return;
1525 		}
1526 		rw_exit(&ipst->ips_ill_g_lock);
1527 		/*
1528 		 * We switch to the upper ill so that mrouter and hasmembers
1529 		 * can operate on upper here and in ip_input_multicast.
1530 		 */
1531 		ill = ipmp_ill_hold_ipmp_ill(ill);
1532 		if (ill != NULL) {
1533 			ASSERT(ill != ira->ira_ill);
1534 			ASSERT(ire->ire_ill == ira->ira_ill);
1535 			ira->ira_ill = ill;
1536 			ira->ira_ruifindex = ill->ill_phyint->phyint_ifindex;
1537 		} else {
1538 			ill = ira->ira_ill;
1539 		}
1540 	}
1541 
1542 	/*
1543 	 * Check if we are a multicast router - send ip_mforward a copy of
1544 	 * the packet.
1545 	 * Due to mroute_decap tunnels we consider forwarding packets even if
1546 	 * mrouted has not joined the allmulti group on this interface.
1547 	 */
1548 	if (ipst->ips_ip_g_mrouter) {
1549 		int retval;
1550 
1551 		/*
1552 		 * Clear the indication that this may have hardware
1553 		 * checksum as we are not using it for forwarding.
1554 		 */
1555 		DB_CKSUMFLAGS(mp) = 0;
1556 
1557 		/*
1558 		 * ip_mforward helps us make these distinctions: If received
1559 		 * on tunnel and not IGMP, then drop.
1560 		 * If IGMP packet, then don't check membership
1561 		 * If received on a phyint and IGMP or PIM, then
1562 		 * don't check membership
1563 		 */
1564 		retval = ip_mforward(mp, ira);
1565 		/* ip_mforward updates mib variables if needed */
1566 
1567 		switch (retval) {
1568 		case 0:
1569 			/*
1570 			 * pkt is okay and arrived on phyint.
1571 			 *
1572 			 * If we are running as a multicast router
1573 			 * we need to see all IGMP and/or PIM packets.
1574 			 */
1575 			if ((ipha->ipha_protocol == IPPROTO_IGMP) ||
1576 			    (ipha->ipha_protocol == IPPROTO_PIM)) {
1577 				goto forus;
1578 			}
1579 			break;
1580 		case -1:
1581 			/* pkt is mal-formed, toss it */
1582 			freemsg(mp);
1583 			goto done;
1584 		case 1:
1585 			/*
1586 			 * pkt is okay and arrived on a tunnel
1587 			 *
1588 			 * If we are running a multicast router
1589 			 * we need to see all igmp packets.
1590 			 */
1591 			if (ipha->ipha_protocol == IPPROTO_IGMP) {
1592 				goto forus;
1593 			}
1594 			ip_drop_input("Multicast on tunnel ignored", mp, ill);
1595 			freemsg(mp);
1596 			goto done;
1597 		}
1598 	}
1599 
1600 	/*
1601 	 * Check if we have members on this ill. This is not necessary for
1602 	 * correctness because even if the NIC/GLD had a leaky filter, we
1603 	 * filter before passing to each conn_t.
1604 	 */
1605 	if (!ill_hasmembers_v4(ill, ipha->ipha_dst)) {
1606 		/*
1607 		 * Nobody interested
1608 		 *
1609 		 * This might just be caused by the fact that
1610 		 * multiple IP Multicast addresses map to the same
1611 		 * link layer multicast - no need to increment counter!
1612 		 */
1613 		ip_drop_input("Multicast with no members", mp, ill);
1614 		freemsg(mp);
1615 		goto done;
1616 	}
1617 forus:
1618 	ip2dbg(("ire_recv_multicast_v4: multicast for us: 0x%x\n",
1619 	    ntohl(ipha->ipha_dst)));
1620 
1621 	/*
1622 	 * After reassembly and IPsec we will need to duplicate the
1623 	 * multicast packet for all matching zones on the ill.
1624 	 */
1625 	ira->ira_zoneid = ALL_ZONES;
1626 
1627 	/* Reassemble on the ill on which the packet arrived */
1628 	ip_input_local_v4(ire, mp, ipha, ira);
1629 done:
1630 	if (ill != ire->ire_ill) {
1631 		ill_refrele(ill);
1632 		ira->ira_ill = ire->ire_ill;
1633 		ira->ira_ruifindex = ira->ira_ill->ill_phyint->phyint_ifindex;
1634 	}
1635 }
1636 
1637 /*
1638  * ire_recvfn for IRE_OFFLINK with RTF_MULTIRT.
1639  * Drop packets since we don't forward out multirt routes.
1640  */
1641 /* ARGSUSED */
1642 void
1643 ire_recv_multirt_v4(ire_t *ire, mblk_t *mp, void *iph_arg, ip_recv_attr_t *ira)
1644 {
1645 	ill_t		*ill = ira->ira_ill;
1646 
1647 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsInNoRoutes);
1648 	ip_drop_input("Not forwarding out MULTIRT", mp, ill);
1649 	freemsg(mp);
1650 }
1651 
1652 /*
1653  * ire_recvfn for IRE_LOOPBACK. This is only used when a FW_HOOK
1654  * has rewritten the packet to have a loopback destination address (We
1655  * filter out packet with a loopback destination from arriving over the wire).
1656  * We don't know what zone to use, thus we always use the GLOBAL_ZONEID.
1657  */
1658 void
1659 ire_recv_loopback_v4(ire_t *ire, mblk_t *mp, void *iph_arg, ip_recv_attr_t *ira)
1660 {
1661 	ipha_t		*ipha = (ipha_t *)iph_arg;
1662 	ill_t		*ill = ira->ira_ill;
1663 	ill_t		*ire_ill = ire->ire_ill;
1664 
1665 	ira->ira_zoneid = GLOBAL_ZONEID;
1666 
1667 	/* Switch to the lo0 ill for further processing  */
1668 	if (ire_ill != ill) {
1669 		/*
1670 		 * Update ira_ill to be the ILL on which the IP address
1671 		 * is hosted.
1672 		 * No need to hold the ill since we have a hold on the ire
1673 		 */
1674 		ASSERT(ira->ira_ill == ira->ira_rill);
1675 		ira->ira_ill = ire_ill;
1676 
1677 		ip_input_local_v4(ire, mp, ipha, ira);
1678 
1679 		/* Restore */
1680 		ASSERT(ira->ira_ill == ire_ill);
1681 		ira->ira_ill = ill;
1682 		return;
1683 
1684 	}
1685 	ip_input_local_v4(ire, mp, ipha, ira);
1686 }
1687 
1688 /*
1689  * ire_recvfn for IRE_LOCAL.
1690  */
1691 void
1692 ire_recv_local_v4(ire_t *ire, mblk_t *mp, void *iph_arg, ip_recv_attr_t *ira)
1693 {
1694 	ipha_t		*ipha = (ipha_t *)iph_arg;
1695 	ill_t		*ill = ira->ira_ill;
1696 	ill_t		*ire_ill = ire->ire_ill;
1697 
1698 	/* Make a note for DAD that this address is in use */
1699 	ire->ire_last_used_time = LBOLT_FASTPATH;
1700 
1701 	/* Only target the IRE_LOCAL with the right zoneid. */
1702 	ira->ira_zoneid = ire->ire_zoneid;
1703 
1704 	/*
1705 	 * If the packet arrived on the wrong ill, we check that
1706 	 * this is ok.
1707 	 * If it is, then we ensure that we do the reassembly on
1708 	 * the ill on which the address is hosted. We keep ira_rill as
1709 	 * the one on which the packet arrived, so that IP_PKTINFO and
1710 	 * friends can report this.
1711 	 */
1712 	if (ire_ill != ill) {
1713 		ire_t *new_ire;
1714 
1715 		new_ire = ip_check_multihome(&ipha->ipha_dst, ire, ill);
1716 		if (new_ire == NULL) {
1717 			/* Drop packet */
1718 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsForwProhibits);
1719 			ip_drop_input("ipIfStatsInForwProhibits", mp, ill);
1720 			freemsg(mp);
1721 			return;
1722 		}
1723 		/*
1724 		 * Update ira_ill to be the ILL on which the IP address
1725 		 * is hosted. No need to hold the ill since we have a
1726 		 * hold on the ire. Note that we do the switch even if
1727 		 * new_ire == ire (for IPMP, ire would be the one corresponding
1728 		 * to the IPMP ill).
1729 		 */
1730 		ASSERT(ira->ira_ill == ira->ira_rill);
1731 		ira->ira_ill = new_ire->ire_ill;
1732 
1733 		/* ira_ruifindex tracks the upper for ira_rill */
1734 		if (IS_UNDER_IPMP(ill))
1735 			ira->ira_ruifindex = ill_get_upper_ifindex(ill);
1736 
1737 		ip_input_local_v4(new_ire, mp, ipha, ira);
1738 
1739 		/* Restore */
1740 		ASSERT(ira->ira_ill == new_ire->ire_ill);
1741 		ira->ira_ill = ill;
1742 		ira->ira_ruifindex = ill->ill_phyint->phyint_ifindex;
1743 
1744 		if (new_ire != ire)
1745 			ire_refrele(new_ire);
1746 		return;
1747 	}
1748 
1749 	ip_input_local_v4(ire, mp, ipha, ira);
1750 }
1751 
1752 /*
1753  * Common function for packets arriving for the host. Handles
1754  * checksum verification, reassembly checks, etc.
1755  */
1756 static void
1757 ip_input_local_v4(ire_t *ire, mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
1758 {
1759 	ill_t		*ill = ira->ira_ill;
1760 	iaflags_t	iraflags = ira->ira_flags;
1761 
1762 	/*
1763 	 * Verify IP header checksum. If the packet was AH or ESP then
1764 	 * this flag has already been cleared. Likewise if the packet
1765 	 * had a hardware checksum.
1766 	 */
1767 	if ((iraflags & IRAF_VERIFY_IP_CKSUM) && ip_csum_hdr(ipha)) {
1768 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsInCksumErrs);
1769 		ip_drop_input("ipIfStatsInCksumErrs", mp, ill);
1770 		freemsg(mp);
1771 		return;
1772 	}
1773 
1774 	if (iraflags & IRAF_IPV4_OPTIONS) {
1775 		if (!ip_input_local_options(mp, ipha, ira)) {
1776 			/* Error has been sent and mp consumed */
1777 			return;
1778 		}
1779 		/*
1780 		 * Some old hardware does partial checksum by including the
1781 		 * whole IP header, so the partial checksum value might have
1782 		 * become invalid if any option in the packet have been
1783 		 * updated. Always clear partial checksum flag here.
1784 		 */
1785 		DB_CKSUMFLAGS(mp) &= ~HCK_PARTIALCKSUM;
1786 	}
1787 
1788 	/*
1789 	 * Is packet part of fragmented IP packet?
1790 	 * We compare against defined values in network byte order
1791 	 */
1792 	if (ipha->ipha_fragment_offset_and_flags &
1793 	    (IPH_MF_HTONS | IPH_OFFSET_HTONS)) {
1794 		/*
1795 		 * Make sure we have ira_l2src before we loose the original
1796 		 * mblk
1797 		 */
1798 		if (!(ira->ira_flags & IRAF_L2SRC_SET))
1799 			ip_setl2src(mp, ira, ira->ira_rill);
1800 
1801 		mp = ip_input_fragment(mp, ipha, ira);
1802 		if (mp == NULL)
1803 			return;
1804 		/* Completed reassembly */
1805 		ipha = (ipha_t *)mp->b_rptr;
1806 	}
1807 
1808 	/*
1809 	 * For broadcast and multicast we need some extra work before
1810 	 * we call ip_fanout_v4(), since in the case of shared-IP zones
1811 	 * we need to pretend that a packet arrived for each zoneid.
1812 	 */
1813 	if (iraflags & IRAF_MULTIBROADCAST) {
1814 		if (iraflags & IRAF_BROADCAST)
1815 			ip_input_broadcast_v4(ire, mp, ipha, ira);
1816 		else
1817 			ip_input_multicast_v4(ire, mp, ipha, ira);
1818 		return;
1819 	}
1820 	ip_fanout_v4(mp, ipha, ira);
1821 }
1822 
1823 
1824 /*
1825  * Handle multiple zones which match the same broadcast address
1826  * and ill by delivering a packet to each of them.
1827  * Walk the bucket and look for different ire_zoneid but otherwise
1828  * the same IRE (same ill/addr/mask/type).
1829  * Note that ire_add() tracks IREs that are identical in all
1830  * fields (addr/mask/type/gw/ill/zoneid) within a single IRE by
1831  * increasing ire_identical_cnt. Thus we don't need to be concerned
1832  * about those.
1833  */
1834 static void
1835 ip_input_broadcast_v4(ire_t *ire, mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
1836 {
1837 	ill_t		*ill = ira->ira_ill;
1838 	ip_stack_t	*ipst = ill->ill_ipst;
1839 	netstack_t	*ns = ipst->ips_netstack;
1840 	irb_t		*irb;
1841 	ire_t		*ire1;
1842 	mblk_t		*mp1;
1843 	ipha_t		*ipha1;
1844 
1845 	irb = ire->ire_bucket;
1846 
1847 	/*
1848 	 * If we don't have more than one shared-IP zone, or if
1849 	 * there can't be more than one IRE_BROADCAST for this
1850 	 * IP address, then just set the zoneid and proceed.
1851 	 */
1852 	if (ns->netstack_numzones == 1 || irb->irb_ire_cnt == 1) {
1853 		ira->ira_zoneid = ire->ire_zoneid;
1854 
1855 		ip_fanout_v4(mp, ipha, ira);
1856 		return;
1857 	}
1858 	irb_refhold(irb);
1859 	for (ire1 = irb->irb_ire; ire1 != NULL; ire1 = ire1->ire_next) {
1860 		/* We do the main IRE after the end of the loop */
1861 		if (ire1 == ire)
1862 			continue;
1863 
1864 		/*
1865 		 * Only IREs for the same IP address should be in the same
1866 		 * bucket.
1867 		 * But could have IRE_HOSTs in the case of CGTP.
1868 		 */
1869 		ASSERT(ire1->ire_addr == ire->ire_addr);
1870 		if (!(ire1->ire_type & IRE_BROADCAST))
1871 			continue;
1872 
1873 		if (IRE_IS_CONDEMNED(ire1))
1874 			continue;
1875 
1876 		mp1 = copymsg(mp);
1877 		if (mp1 == NULL) {
1878 			/* Failed to deliver to one zone */
1879 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1880 			ip_drop_input("ipIfStatsInDiscards", mp, ill);
1881 			continue;
1882 		}
1883 		ira->ira_zoneid = ire1->ire_zoneid;
1884 		ipha1 = (ipha_t *)mp1->b_rptr;
1885 		ip_fanout_v4(mp1, ipha1, ira);
1886 	}
1887 	irb_refrele(irb);
1888 	/* Do the main ire */
1889 	ira->ira_zoneid = ire->ire_zoneid;
1890 	ip_fanout_v4(mp, ipha, ira);
1891 }
1892 
1893 /*
1894  * Handle multiple zones which want to receive the same multicast packets
1895  * on this ill by delivering a packet to each of them.
1896  *
1897  * Note that for packets delivered to transports we could instead do this
1898  * as part of the fanout code, but since we need to handle icmp_inbound
1899  * it is simpler to have multicast work the same as broadcast.
1900  *
1901  * The ip_fanout matching for multicast matches based on ilm independent of
1902  * zoneid since the zoneid restriction is applied when joining a multicast
1903  * group.
1904  */
1905 /* ARGSUSED */
1906 static void
1907 ip_input_multicast_v4(ire_t *ire, mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
1908 {
1909 	ill_t		*ill = ira->ira_ill;
1910 	iaflags_t	iraflags = ira->ira_flags;
1911 	ip_stack_t	*ipst = ill->ill_ipst;
1912 	netstack_t	*ns = ipst->ips_netstack;
1913 	zoneid_t	zoneid;
1914 	mblk_t		*mp1;
1915 	ipha_t		*ipha1;
1916 
1917 	/* ire_recv_multicast has switched to the upper ill for IPMP */
1918 	ASSERT(!IS_UNDER_IPMP(ill));
1919 
1920 	/*
1921 	 * If we don't have more than one shared-IP zone, or if
1922 	 * there are no members in anything but the global zone,
1923 	 * then just set the zoneid and proceed.
1924 	 */
1925 	if (ns->netstack_numzones == 1 ||
1926 	    !ill_hasmembers_otherzones_v4(ill, ipha->ipha_dst,
1927 	    GLOBAL_ZONEID)) {
1928 		ira->ira_zoneid = GLOBAL_ZONEID;
1929 
1930 		/* If sender didn't want this zone to receive it, drop */
1931 		if ((iraflags & IRAF_NO_LOOP_ZONEID_SET) &&
1932 		    ira->ira_no_loop_zoneid == ira->ira_zoneid) {
1933 			ip_drop_input("Multicast but wrong zoneid", mp, ill);
1934 			freemsg(mp);
1935 			return;
1936 		}
1937 		ip_fanout_v4(mp, ipha, ira);
1938 		return;
1939 	}
1940 
1941 	/*
1942 	 * Here we loop over all zoneids that have members in the group
1943 	 * and deliver a packet to ip_fanout for each zoneid.
1944 	 *
1945 	 * First find any members in the lowest numeric zoneid by looking for
1946 	 * first zoneid larger than -1 (ALL_ZONES).
1947 	 * We terminate the loop when we receive -1 (ALL_ZONES).
1948 	 */
1949 	zoneid = ill_hasmembers_nextzone_v4(ill, ipha->ipha_dst, ALL_ZONES);
1950 	for (; zoneid != ALL_ZONES;
1951 	    zoneid = ill_hasmembers_nextzone_v4(ill, ipha->ipha_dst, zoneid)) {
1952 		/*
1953 		 * Avoid an extra copymsg/freemsg by skipping global zone here
1954 		 * and doing that at the end.
1955 		 */
1956 		if (zoneid == GLOBAL_ZONEID)
1957 			continue;
1958 
1959 		ira->ira_zoneid = zoneid;
1960 
1961 		/* If sender didn't want this zone to receive it, skip */
1962 		if ((iraflags & IRAF_NO_LOOP_ZONEID_SET) &&
1963 		    ira->ira_no_loop_zoneid == ira->ira_zoneid)
1964 			continue;
1965 
1966 		mp1 = copymsg(mp);
1967 		if (mp1 == NULL) {
1968 			/* Failed to deliver to one zone */
1969 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
1970 			ip_drop_input("ipIfStatsInDiscards", mp, ill);
1971 			continue;
1972 		}
1973 		ipha1 = (ipha_t *)mp1->b_rptr;
1974 		ip_fanout_v4(mp1, ipha1, ira);
1975 	}
1976 
1977 	/* Do the main ire */
1978 	ira->ira_zoneid = GLOBAL_ZONEID;
1979 	/* If sender didn't want this zone to receive it, drop */
1980 	if ((iraflags & IRAF_NO_LOOP_ZONEID_SET) &&
1981 	    ira->ira_no_loop_zoneid == ira->ira_zoneid) {
1982 		ip_drop_input("Multicast but wrong zoneid", mp, ill);
1983 		freemsg(mp);
1984 	} else {
1985 		ip_fanout_v4(mp, ipha, ira);
1986 	}
1987 }
1988 
1989 
1990 /*
1991  * Determine the zoneid and IRAF_TX_* flags if trusted extensions
1992  * is in use. Updates ira_zoneid and ira_flags as a result.
1993  */
1994 static void
1995 ip_fanout_tx_v4(mblk_t *mp, ipha_t *ipha, uint8_t protocol,
1996     uint_t ip_hdr_length, ip_recv_attr_t *ira)
1997 {
1998 	uint16_t	*up;
1999 	uint16_t	lport;
2000 	zoneid_t	zoneid;
2001 
2002 	ASSERT(ira->ira_flags & IRAF_SYSTEM_LABELED);
2003 
2004 	/*
2005 	 * If the packet is unlabeled we might allow read-down
2006 	 * for MAC_EXEMPT. Below we clear this if it is a multi-level
2007 	 * port (MLP).
2008 	 * Note that ira_tsl can be NULL here.
2009 	 */
2010 	if (ira->ira_tsl != NULL && ira->ira_tsl->tsl_flags & TSLF_UNLABELED)
2011 		ira->ira_flags |= IRAF_TX_MAC_EXEMPTABLE;
2012 
2013 	if (ira->ira_zoneid != ALL_ZONES)
2014 		return;
2015 
2016 	ira->ira_flags |= IRAF_TX_SHARED_ADDR;
2017 
2018 	up = (uint16_t *)((uchar_t *)ipha + ip_hdr_length);
2019 	switch (protocol) {
2020 	case IPPROTO_TCP:
2021 	case IPPROTO_SCTP:
2022 	case IPPROTO_UDP:
2023 		/* Caller ensures this */
2024 		ASSERT(((uchar_t *)ipha) + ip_hdr_length +4 <= mp->b_wptr);
2025 
2026 		/*
2027 		 * Only these transports support MLP.
2028 		 * We know their destination port numbers is in
2029 		 * the same place in the header.
2030 		 */
2031 		lport = up[1];
2032 
2033 		/*
2034 		 * No need to handle exclusive-stack zones
2035 		 * since ALL_ZONES only applies to the shared IP instance.
2036 		 */
2037 		zoneid = tsol_mlp_findzone(protocol, lport);
2038 		/*
2039 		 * If no shared MLP is found, tsol_mlp_findzone returns
2040 		 * ALL_ZONES.  In that case, we assume it's SLP, and
2041 		 * search for the zone based on the packet label.
2042 		 *
2043 		 * If there is such a zone, we prefer to find a
2044 		 * connection in it.  Otherwise, we look for a
2045 		 * MAC-exempt connection in any zone whose label
2046 		 * dominates the default label on the packet.
2047 		 */
2048 		if (zoneid == ALL_ZONES)
2049 			zoneid = tsol_attr_to_zoneid(ira);
2050 		else
2051 			ira->ira_flags &= ~IRAF_TX_MAC_EXEMPTABLE;
2052 		break;
2053 	default:
2054 		/* Handle shared address for other protocols */
2055 		zoneid = tsol_attr_to_zoneid(ira);
2056 		break;
2057 	}
2058 	ira->ira_zoneid = zoneid;
2059 }
2060 
2061 /*
2062  * Increment checksum failure statistics
2063  */
2064 static void
2065 ip_input_cksum_err_v4(uint8_t protocol, uint16_t hck_flags, ill_t *ill)
2066 {
2067 	ip_stack_t	*ipst = ill->ill_ipst;
2068 
2069 	switch (protocol) {
2070 	case IPPROTO_TCP:
2071 		BUMP_MIB(ill->ill_ip_mib, tcpIfStatsInErrs);
2072 
2073 		if (hck_flags & HCK_FULLCKSUM)
2074 			IP_STAT(ipst, ip_tcp_in_full_hw_cksum_err);
2075 		else if (hck_flags & HCK_PARTIALCKSUM)
2076 			IP_STAT(ipst, ip_tcp_in_part_hw_cksum_err);
2077 		else
2078 			IP_STAT(ipst, ip_tcp_in_sw_cksum_err);
2079 		break;
2080 	case IPPROTO_UDP:
2081 		BUMP_MIB(ill->ill_ip_mib, udpIfStatsInCksumErrs);
2082 		if (hck_flags & HCK_FULLCKSUM)
2083 			IP_STAT(ipst, ip_udp_in_full_hw_cksum_err);
2084 		else if (hck_flags & HCK_PARTIALCKSUM)
2085 			IP_STAT(ipst, ip_udp_in_part_hw_cksum_err);
2086 		else
2087 			IP_STAT(ipst, ip_udp_in_sw_cksum_err);
2088 		break;
2089 	case IPPROTO_ICMP:
2090 		BUMP_MIB(&ipst->ips_icmp_mib, icmpInCksumErrs);
2091 		break;
2092 	default:
2093 		ASSERT(0);
2094 		break;
2095 	}
2096 }
2097 
2098 /* Calculate the IPv4 pseudo-header checksum */
2099 uint32_t
2100 ip_input_cksum_pseudo_v4(ipha_t *ipha, ip_recv_attr_t *ira)
2101 {
2102 	uint_t		ulp_len;
2103 	uint32_t	cksum;
2104 	uint8_t		protocol = ira->ira_protocol;
2105 	uint16_t	ip_hdr_length = ira->ira_ip_hdr_length;
2106 
2107 #define	iphs    ((uint16_t *)ipha)
2108 
2109 	switch (protocol) {
2110 	case IPPROTO_TCP:
2111 		ulp_len = ira->ira_pktlen - ip_hdr_length;
2112 
2113 		/* Protocol and length */
2114 		cksum = htons(ulp_len) + IP_TCP_CSUM_COMP;
2115 		/* IP addresses */
2116 		cksum += iphs[6] + iphs[7] + iphs[8] + iphs[9];
2117 		break;
2118 
2119 	case IPPROTO_UDP: {
2120 		udpha_t		*udpha;
2121 
2122 		udpha = (udpha_t  *)((uchar_t *)ipha + ip_hdr_length);
2123 
2124 		/* Protocol and length */
2125 		cksum = udpha->uha_length + IP_UDP_CSUM_COMP;
2126 		/* IP addresses */
2127 		cksum += iphs[6] + iphs[7] + iphs[8] + iphs[9];
2128 		break;
2129 	}
2130 
2131 	default:
2132 		cksum = 0;
2133 		break;
2134 	}
2135 #undef	iphs
2136 	return (cksum);
2137 }
2138 
2139 
2140 /*
2141  * Software verification of the ULP checksums.
2142  * Returns B_TRUE if ok.
2143  * Increments statistics of failed.
2144  */
2145 static boolean_t
2146 ip_input_sw_cksum_v4(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
2147 {
2148 	ip_stack_t	*ipst = ira->ira_ill->ill_ipst;
2149 	uint32_t	cksum;
2150 	uint8_t		protocol = ira->ira_protocol;
2151 	uint16_t	ip_hdr_length = ira->ira_ip_hdr_length;
2152 
2153 	IP_STAT(ipst, ip_in_sw_cksum);
2154 
2155 	ASSERT(protocol == IPPROTO_TCP || protocol == IPPROTO_UDP);
2156 
2157 	cksum = ip_input_cksum_pseudo_v4(ipha, ira);
2158 	cksum = IP_CSUM(mp, ip_hdr_length, cksum);
2159 	if (cksum == 0)
2160 		return (B_TRUE);
2161 
2162 	ip_input_cksum_err_v4(protocol, 0, ira->ira_ill);
2163 	return (B_FALSE);
2164 }
2165 
2166 /*
2167  * Verify the ULP checksums.
2168  * Returns B_TRUE if ok, or if the ULP doesn't have a well-defined checksum
2169  * algorithm.
2170  * Increments statistics if failed.
2171  */
2172 static boolean_t
2173 ip_input_cksum_v4(iaflags_t iraflags, mblk_t *mp, ipha_t *ipha,
2174     ip_recv_attr_t *ira)
2175 {
2176 	ill_t		*ill = ira->ira_rill;
2177 	uint16_t	hck_flags;
2178 	uint32_t	cksum;
2179 	mblk_t		*mp1;
2180 	int32_t		len;
2181 	uint8_t		protocol = ira->ira_protocol;
2182 	uint16_t	ip_hdr_length = ira->ira_ip_hdr_length;
2183 
2184 
2185 	switch (protocol) {
2186 	case IPPROTO_TCP:
2187 		break;
2188 
2189 	case IPPROTO_UDP: {
2190 		udpha_t		*udpha;
2191 
2192 		udpha = (udpha_t  *)((uchar_t *)ipha + ip_hdr_length);
2193 		if (udpha->uha_checksum == 0) {
2194 			/* Packet doesn't have a UDP checksum */
2195 			return (B_TRUE);
2196 		}
2197 		break;
2198 	}
2199 	case IPPROTO_SCTP: {
2200 		sctp_hdr_t	*sctph;
2201 		uint32_t	pktsum;
2202 
2203 		sctph = (sctp_hdr_t *)((uchar_t *)ipha + ip_hdr_length);
2204 #ifdef	DEBUG
2205 		if (skip_sctp_cksum)
2206 			return (B_TRUE);
2207 #endif
2208 		pktsum = sctph->sh_chksum;
2209 		sctph->sh_chksum = 0;
2210 		cksum = sctp_cksum(mp, ip_hdr_length);
2211 		sctph->sh_chksum = pktsum;
2212 		if (cksum == pktsum)
2213 			return (B_TRUE);
2214 
2215 		/*
2216 		 * Defer until later whether a bad checksum is ok
2217 		 * in order to allow RAW sockets to use Adler checksum
2218 		 * with SCTP.
2219 		 */
2220 		ira->ira_flags |= IRAF_SCTP_CSUM_ERR;
2221 		return (B_TRUE);
2222 	}
2223 
2224 	default:
2225 		/* No ULP checksum to verify. */
2226 		return (B_TRUE);
2227 	}
2228 	/*
2229 	 * Revert to software checksum calculation if the interface
2230 	 * isn't capable of checksum offload.
2231 	 * We clear DB_CKSUMFLAGS when going through IPsec in ip_fanout.
2232 	 * Note: IRAF_NO_HW_CKSUM is not currently used.
2233 	 */
2234 	ASSERT(!IS_IPMP(ill));
2235 	if ((iraflags & IRAF_NO_HW_CKSUM) || !ILL_HCKSUM_CAPABLE(ill) ||
2236 	    !dohwcksum) {
2237 		return (ip_input_sw_cksum_v4(mp, ipha, ira));
2238 	}
2239 
2240 	/*
2241 	 * We apply this for all ULP protocols. Does the HW know to
2242 	 * not set the flags for SCTP and other protocols.
2243 	 */
2244 
2245 	hck_flags = DB_CKSUMFLAGS(mp);
2246 
2247 	if (hck_flags & HCK_FULLCKSUM) {
2248 		/*
2249 		 * Full checksum has been computed by the hardware
2250 		 * and has been attached.  If the driver wants us to
2251 		 * verify the correctness of the attached value, in
2252 		 * order to protect against faulty hardware, compare
2253 		 * it against -0 (0xFFFF) to see if it's valid.
2254 		 */
2255 		if (hck_flags & HCK_FULLCKSUM_OK)
2256 			return (B_TRUE);
2257 
2258 		cksum = DB_CKSUM16(mp);
2259 		if (cksum == 0xFFFF)
2260 			return (B_TRUE);
2261 		ip_input_cksum_err_v4(protocol, hck_flags, ira->ira_ill);
2262 		return (B_FALSE);
2263 	}
2264 
2265 	mp1 = mp->b_cont;
2266 	if ((hck_flags & HCK_PARTIALCKSUM) &&
2267 	    (mp1 == NULL || mp1->b_cont == NULL) &&
2268 	    ip_hdr_length >= DB_CKSUMSTART(mp) &&
2269 	    ((len = ip_hdr_length - DB_CKSUMSTART(mp)) & 1) == 0) {
2270 		uint32_t	adj;
2271 		uchar_t		*cksum_start;
2272 
2273 		cksum = ip_input_cksum_pseudo_v4(ipha, ira);
2274 
2275 		cksum_start = ((uchar_t *)ipha + DB_CKSUMSTART(mp));
2276 
2277 		/*
2278 		 * Partial checksum has been calculated by hardware
2279 		 * and attached to the packet; in addition, any
2280 		 * prepended extraneous data is even byte aligned,
2281 		 * and there are at most two mblks associated with
2282 		 * the packet.  If any such data exists, we adjust
2283 		 * the checksum; also take care any postpended data.
2284 		 */
2285 		IP_ADJCKSUM_PARTIAL(cksum_start, mp, mp1, len, adj);
2286 		/*
2287 		 * One's complement subtract extraneous checksum
2288 		 */
2289 		cksum += DB_CKSUM16(mp);
2290 		if (adj >= cksum)
2291 			cksum = ~(adj - cksum) & 0xFFFF;
2292 		else
2293 			cksum -= adj;
2294 		cksum = (cksum & 0xFFFF) + ((int)cksum >> 16);
2295 		cksum = (cksum & 0xFFFF) + ((int)cksum >> 16);
2296 		if (!(~cksum & 0xFFFF))
2297 			return (B_TRUE);
2298 
2299 		ip_input_cksum_err_v4(protocol, hck_flags, ira->ira_ill);
2300 		return (B_FALSE);
2301 	}
2302 	return (ip_input_sw_cksum_v4(mp, ipha, ira));
2303 }
2304 
2305 
2306 /*
2307  * Handle fanout of received packets.
2308  * Unicast packets that are looped back (from ire_send_local_v4) and packets
2309  * from the wire are differentiated by checking IRAF_VERIFY_ULP_CKSUM.
2310  *
2311  * IPQoS Notes
2312  * Before sending it to the client, invoke IPPF processing. Policy processing
2313  * takes place only if the callout_position, IPP_LOCAL_IN, is enabled.
2314  */
2315 void
2316 ip_fanout_v4(mblk_t *mp, ipha_t *ipha, ip_recv_attr_t *ira)
2317 {
2318 	ill_t		*ill = ira->ira_ill;
2319 	iaflags_t	iraflags = ira->ira_flags;
2320 	ip_stack_t	*ipst = ill->ill_ipst;
2321 	uint8_t		protocol = ipha->ipha_protocol;
2322 	conn_t		*connp;
2323 #define	rptr	((uchar_t *)ipha)
2324 	uint_t		ip_hdr_length;
2325 	uint_t		min_ulp_header_length;
2326 	int		offset;
2327 	ssize_t		len;
2328 	netstack_t	*ns = ipst->ips_netstack;
2329 	ipsec_stack_t	*ipss = ns->netstack_ipsec;
2330 	ill_t		*rill = ira->ira_rill;
2331 
2332 	ASSERT(ira->ira_pktlen == ntohs(ipha->ipha_length));
2333 
2334 	ip_hdr_length = ira->ira_ip_hdr_length;
2335 	ira->ira_protocol = protocol;
2336 
2337 	/*
2338 	 * Time for IPP once we've done reassembly and IPsec.
2339 	 * We skip this for loopback packets since we don't do IPQoS
2340 	 * on loopback.
2341 	 */
2342 	if (IPP_ENABLED(IPP_LOCAL_IN, ipst) &&
2343 	    !(iraflags & IRAF_LOOPBACK) &&
2344 	    (protocol != IPPROTO_ESP || protocol != IPPROTO_AH)) {
2345 		/*
2346 		 * Use the interface on which the packet arrived - not where
2347 		 * the IP address is hosted.
2348 		 */
2349 		/* ip_process translates an IS_UNDER_IPMP */
2350 		mp = ip_process(IPP_LOCAL_IN, mp, rill, ill);
2351 		if (mp == NULL) {
2352 			/* ip_drop_packet and MIB done */
2353 			return;
2354 		}
2355 	}
2356 
2357 	/* Determine the minimum required size of the upper-layer header */
2358 	/* Need to do this for at least the set of ULPs that TX handles. */
2359 	switch (protocol) {
2360 	case IPPROTO_TCP:
2361 		min_ulp_header_length = TCP_MIN_HEADER_LENGTH;
2362 		break;
2363 	case IPPROTO_SCTP:
2364 		min_ulp_header_length = SCTP_COMMON_HDR_LENGTH;
2365 		break;
2366 	case IPPROTO_UDP:
2367 		min_ulp_header_length = UDPH_SIZE;
2368 		break;
2369 	case IPPROTO_ICMP:
2370 		min_ulp_header_length = ICMPH_SIZE;
2371 		break;
2372 	default:
2373 		min_ulp_header_length = 0;
2374 		break;
2375 	}
2376 	/* Make sure we have the min ULP header length */
2377 	len = mp->b_wptr - rptr;
2378 	if (len < ip_hdr_length + min_ulp_header_length) {
2379 		if (ira->ira_pktlen < ip_hdr_length + min_ulp_header_length) {
2380 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInTruncatedPkts);
2381 			ip_drop_input("ipIfStatsInTruncatedPkts", mp, ill);
2382 			freemsg(mp);
2383 			return;
2384 		}
2385 		IP_STAT(ipst, ip_recv_pullup);
2386 		ipha = ip_pullup(mp, ip_hdr_length + min_ulp_header_length,
2387 		    ira);
2388 		if (ipha == NULL)
2389 			goto discard;
2390 		len = mp->b_wptr - rptr;
2391 	}
2392 
2393 	/*
2394 	 * If trusted extensions then determine the zoneid and TX specific
2395 	 * ira_flags.
2396 	 */
2397 	if (iraflags & IRAF_SYSTEM_LABELED) {
2398 		/* This can update ira->ira_flags and ira->ira_zoneid */
2399 		ip_fanout_tx_v4(mp, ipha, protocol, ip_hdr_length, ira);
2400 		iraflags = ira->ira_flags;
2401 	}
2402 
2403 
2404 	/* Verify ULP checksum. Handles TCP, UDP, and SCTP */
2405 	if (iraflags & IRAF_VERIFY_ULP_CKSUM) {
2406 		if (!ip_input_cksum_v4(iraflags, mp, ipha, ira)) {
2407 			/* Bad checksum. Stats are already incremented */
2408 			ip_drop_input("Bad ULP checksum", mp, ill);
2409 			freemsg(mp);
2410 			return;
2411 		}
2412 		/* IRAF_SCTP_CSUM_ERR could have been set */
2413 		iraflags = ira->ira_flags;
2414 	}
2415 	switch (protocol) {
2416 	case IPPROTO_TCP:
2417 		/* For TCP, discard broadcast and multicast packets. */
2418 		if (iraflags & IRAF_MULTIBROADCAST)
2419 			goto discard;
2420 
2421 		/* First mblk contains IP+TCP headers per above check */
2422 		ASSERT(len >= ip_hdr_length + TCP_MIN_HEADER_LENGTH);
2423 
2424 		/* TCP options present? */
2425 		offset = ((uchar_t *)ipha)[ip_hdr_length + 12] >> 4;
2426 		if (offset != 5) {
2427 			if (offset < 5)
2428 				goto discard;
2429 
2430 			/*
2431 			 * There must be TCP options.
2432 			 * Make sure we can grab them.
2433 			 */
2434 			offset <<= 2;
2435 			offset += ip_hdr_length;
2436 			if (len < offset) {
2437 				if (ira->ira_pktlen < offset) {
2438 					BUMP_MIB(ill->ill_ip_mib,
2439 					    ipIfStatsInTruncatedPkts);
2440 					ip_drop_input(
2441 					    "ipIfStatsInTruncatedPkts",
2442 					    mp, ill);
2443 					freemsg(mp);
2444 					return;
2445 				}
2446 				IP_STAT(ipst, ip_recv_pullup);
2447 				ipha = ip_pullup(mp, offset, ira);
2448 				if (ipha == NULL)
2449 					goto discard;
2450 				len = mp->b_wptr - rptr;
2451 			}
2452 		}
2453 
2454 		/*
2455 		 * Pass up a squeue hint to tcp.
2456 		 * If ira_sqp is already set (this is loopback) we leave it
2457 		 * alone.
2458 		 */
2459 		if (ira->ira_sqp == NULL) {
2460 			ira->ira_sqp = ip_squeue_get(ira->ira_ring);
2461 		}
2462 
2463 		/* Look for AF_INET or AF_INET6 that matches */
2464 		connp = ipcl_classify_v4(mp, IPPROTO_TCP, ip_hdr_length,
2465 		    ira, ipst);
2466 		if (connp == NULL) {
2467 			/* Send the TH_RST */
2468 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
2469 			tcp_xmit_listeners_reset(mp, ira, ipst, NULL);
2470 			return;
2471 		}
2472 		if (connp->conn_incoming_ifindex != 0 &&
2473 		    connp->conn_incoming_ifindex != ira->ira_ruifindex) {
2474 			CONN_DEC_REF(connp);
2475 
2476 			/* Send the TH_RST */
2477 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
2478 			tcp_xmit_listeners_reset(mp, ira, ipst, NULL);
2479 			return;
2480 		}
2481 		if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) ||
2482 		    (iraflags & IRAF_IPSEC_SECURE)) {
2483 			mp = ipsec_check_inbound_policy(mp, connp,
2484 			    ipha, NULL, ira);
2485 			if (mp == NULL) {
2486 				BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2487 				/* Note that mp is NULL */
2488 				ip_drop_input("ipIfStatsInDiscards", mp, ill);
2489 				CONN_DEC_REF(connp);
2490 				return;
2491 			}
2492 		}
2493 		/* Found a client; up it goes */
2494 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
2495 		ira->ira_ill = ira->ira_rill = NULL;
2496 		if (!IPCL_IS_TCP(connp)) {
2497 			/* Not TCP; must be SOCK_RAW, IPPROTO_TCP */
2498 			(connp->conn_recv)(connp, mp, NULL, ira);
2499 			CONN_DEC_REF(connp);
2500 			ira->ira_ill = ill;
2501 			ira->ira_rill = rill;
2502 			return;
2503 		}
2504 
2505 		/*
2506 		 * We do different processing whether called from
2507 		 * ip_accept_tcp and we match the target, don't match
2508 		 * the target, and when we are called by ip_input.
2509 		 */
2510 		if (iraflags & IRAF_TARGET_SQP) {
2511 			if (ira->ira_target_sqp == connp->conn_sqp) {
2512 				mblk_t	*attrmp;
2513 
2514 				attrmp = ip_recv_attr_to_mblk(ira);
2515 				if (attrmp == NULL) {
2516 					BUMP_MIB(ill->ill_ip_mib,
2517 					    ipIfStatsInDiscards);
2518 					ip_drop_input("ipIfStatsInDiscards",
2519 					    mp, ill);
2520 					freemsg(mp);
2521 					CONN_DEC_REF(connp);
2522 				} else {
2523 					SET_SQUEUE(attrmp, connp->conn_recv,
2524 					    connp);
2525 					attrmp->b_cont = mp;
2526 					ASSERT(ira->ira_target_sqp_mp == NULL);
2527 					ira->ira_target_sqp_mp = attrmp;
2528 					/*
2529 					 * Conn ref release when drained from
2530 					 * the squeue.
2531 					 */
2532 				}
2533 			} else {
2534 				SQUEUE_ENTER_ONE(connp->conn_sqp, mp,
2535 				    connp->conn_recv, connp, ira, SQ_FILL,
2536 				    SQTAG_IP_TCP_INPUT);
2537 			}
2538 		} else {
2539 			SQUEUE_ENTER_ONE(connp->conn_sqp, mp, connp->conn_recv,
2540 			    connp, ira, ip_squeue_flag, SQTAG_IP_TCP_INPUT);
2541 		}
2542 		ira->ira_ill = ill;
2543 		ira->ira_rill = rill;
2544 		return;
2545 
2546 	case IPPROTO_SCTP: {
2547 		sctp_hdr_t	*sctph;
2548 		in6_addr_t	map_src, map_dst;
2549 		uint32_t	ports;	/* Source and destination ports */
2550 		sctp_stack_t	*sctps = ipst->ips_netstack->netstack_sctp;
2551 
2552 		/* For SCTP, discard broadcast and multicast packets. */
2553 		if (iraflags & IRAF_MULTIBROADCAST)
2554 			goto discard;
2555 
2556 		/*
2557 		 * Since there is no SCTP h/w cksum support yet, just
2558 		 * clear the flag.
2559 		 */
2560 		DB_CKSUMFLAGS(mp) = 0;
2561 
2562 		/* Length ensured above */
2563 		ASSERT(MBLKL(mp) >= ip_hdr_length + SCTP_COMMON_HDR_LENGTH);
2564 		sctph = (sctp_hdr_t *)(rptr + ip_hdr_length);
2565 
2566 		/* get the ports */
2567 		ports = *(uint32_t *)&sctph->sh_sport;
2568 
2569 		IN6_IPADDR_TO_V4MAPPED(ipha->ipha_dst, &map_dst);
2570 		IN6_IPADDR_TO_V4MAPPED(ipha->ipha_src, &map_src);
2571 		if (iraflags & IRAF_SCTP_CSUM_ERR) {
2572 			/*
2573 			 * No potential sctp checksum errors go to the Sun
2574 			 * sctp stack however they might be Adler-32 summed
2575 			 * packets a userland stack bound to a raw IP socket
2576 			 * could reasonably use. Note though that Adler-32 is
2577 			 * a long deprecated algorithm and customer sctp
2578 			 * networks should eventually migrate to CRC-32 at
2579 			 * which time this facility should be removed.
2580 			 */
2581 			ip_fanout_sctp_raw(mp, ipha, NULL, ports, ira);
2582 			return;
2583 		}
2584 		connp = sctp_fanout(&map_src, &map_dst, ports, ira, mp, sctps);
2585 		if (connp == NULL) {
2586 			/* Check for raw socket or OOTB handling */
2587 			ip_fanout_sctp_raw(mp, ipha, NULL, ports, ira);
2588 			return;
2589 		}
2590 		if (connp->conn_incoming_ifindex != 0 &&
2591 		    connp->conn_incoming_ifindex != ira->ira_ruifindex) {
2592 			CONN_DEC_REF(connp);
2593 			/* Check for raw socket or OOTB handling */
2594 			ip_fanout_sctp_raw(mp, ipha, NULL, ports, ira);
2595 			return;
2596 		}
2597 
2598 		/* Found a client; up it goes */
2599 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
2600 		sctp_input(connp, ipha, NULL, mp, ira);
2601 		/* sctp_input does a rele of the sctp_t */
2602 		return;
2603 	}
2604 
2605 	case IPPROTO_UDP:
2606 		/* First mblk contains IP+UDP headers as checked above */
2607 		ASSERT(MBLKL(mp) >= ip_hdr_length + UDPH_SIZE);
2608 
2609 		if (iraflags & IRAF_MULTIBROADCAST) {
2610 			uint16_t *up;	/* Pointer to ports in ULP header */
2611 
2612 			up = (uint16_t *)((uchar_t *)ipha + ip_hdr_length);
2613 			ip_fanout_udp_multi_v4(mp, ipha, up[1], up[0], ira);
2614 			return;
2615 		}
2616 
2617 		/* Look for AF_INET or AF_INET6 that matches */
2618 		connp = ipcl_classify_v4(mp, IPPROTO_UDP, ip_hdr_length,
2619 		    ira, ipst);
2620 		if (connp == NULL) {
2621 	no_udp_match:
2622 			if (ipst->ips_ipcl_proto_fanout_v4[IPPROTO_UDP].
2623 			    connf_head != NULL) {
2624 				ASSERT(ira->ira_protocol == IPPROTO_UDP);
2625 				ip_fanout_proto_v4(mp, ipha, ira);
2626 			} else {
2627 				ip_fanout_send_icmp_v4(mp,
2628 				    ICMP_DEST_UNREACHABLE,
2629 				    ICMP_PORT_UNREACHABLE, ira);
2630 			}
2631 			return;
2632 
2633 		}
2634 		if (connp->conn_incoming_ifindex != 0 &&
2635 		    connp->conn_incoming_ifindex != ira->ira_ruifindex) {
2636 			CONN_DEC_REF(connp);
2637 			goto no_udp_match;
2638 		}
2639 		if (IPCL_IS_NONSTR(connp) ? connp->conn_flow_cntrld :
2640 		    !canputnext(connp->conn_rq)) {
2641 			CONN_DEC_REF(connp);
2642 			BUMP_MIB(ill->ill_ip_mib, udpIfStatsInOverflows);
2643 			ip_drop_input("udpIfStatsInOverflows", mp, ill);
2644 			freemsg(mp);
2645 			return;
2646 		}
2647 		if (CONN_INBOUND_POLICY_PRESENT(connp, ipss) ||
2648 		    (iraflags & IRAF_IPSEC_SECURE)) {
2649 			mp = ipsec_check_inbound_policy(mp, connp,
2650 			    ipha, NULL, ira);
2651 			if (mp == NULL) {
2652 				BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2653 				/* Note that mp is NULL */
2654 				ip_drop_input("ipIfStatsInDiscards", mp, ill);
2655 				CONN_DEC_REF(connp);
2656 				return;
2657 			}
2658 		}
2659 		/*
2660 		 * Remove 0-spi if it's 0, or move everything behind
2661 		 * the UDP header over it and forward to ESP via
2662 		 * ip_fanout_v4().
2663 		 */
2664 		if (connp->conn_udp->udp_nat_t_endpoint) {
2665 			if (iraflags & IRAF_IPSEC_SECURE) {
2666 				ip_drop_packet(mp, B_TRUE, ira->ira_ill,
2667 				    DROPPER(ipss, ipds_esp_nat_t_ipsec),
2668 				    &ipss->ipsec_dropper);
2669 				CONN_DEC_REF(connp);
2670 				return;
2671 			}
2672 
2673 			mp = zero_spi_check(mp, ira);
2674 			if (mp == NULL) {
2675 				/*
2676 				 * Packet was consumed - probably sent to
2677 				 * ip_fanout_v4.
2678 				 */
2679 				CONN_DEC_REF(connp);
2680 				return;
2681 			}
2682 			/* Else continue like a normal UDP packet. */
2683 			ipha = (ipha_t *)mp->b_rptr;
2684 			protocol = ipha->ipha_protocol;
2685 			ira->ira_protocol = protocol;
2686 		}
2687 		/* Found a client; up it goes */
2688 		IP_STAT(ipst, ip_udp_fannorm);
2689 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
2690 		ira->ira_ill = ira->ira_rill = NULL;
2691 		(connp->conn_recv)(connp, mp, NULL, ira);
2692 		CONN_DEC_REF(connp);
2693 		ira->ira_ill = ill;
2694 		ira->ira_rill = rill;
2695 		return;
2696 	default:
2697 		break;
2698 	}
2699 
2700 	/*
2701 	 * Clear hardware checksumming flag as it is currently only
2702 	 * used by TCP and UDP.
2703 	 */
2704 	DB_CKSUMFLAGS(mp) = 0;
2705 
2706 	switch (protocol) {
2707 	case IPPROTO_ICMP:
2708 		/*
2709 		 * We need to accomodate icmp messages coming in clear
2710 		 * until we get everything secure from the wire. If
2711 		 * icmp_accept_clear_messages is zero we check with
2712 		 * the global policy and act accordingly. If it is
2713 		 * non-zero, we accept the message without any checks.
2714 		 * But *this does not mean* that this will be delivered
2715 		 * to RAW socket clients. By accepting we might send
2716 		 * replies back, change our MTU value etc.,
2717 		 * but delivery to the ULP/clients depends on their
2718 		 * policy dispositions.
2719 		 */
2720 		if (ipst->ips_icmp_accept_clear_messages == 0) {
2721 			mp = ipsec_check_global_policy(mp, NULL,
2722 			    ipha, NULL, ira, ns);
2723 			if (mp == NULL)
2724 				return;
2725 		}
2726 
2727 		/*
2728 		 * On a labeled system, we have to check whether the zone
2729 		 * itself is permitted to receive raw traffic.
2730 		 */
2731 		if (ira->ira_flags & IRAF_SYSTEM_LABELED) {
2732 			if (!tsol_can_accept_raw(mp, ira, B_FALSE)) {
2733 				BUMP_MIB(&ipst->ips_icmp_mib, icmpInErrors);
2734 				ip_drop_input("tsol_can_accept_raw", mp, ill);
2735 				freemsg(mp);
2736 				return;
2737 			}
2738 		}
2739 
2740 		/*
2741 		 * ICMP header checksum, including checksum field,
2742 		 * should be zero.
2743 		 */
2744 		if (IP_CSUM(mp, ip_hdr_length, 0)) {
2745 			BUMP_MIB(&ipst->ips_icmp_mib, icmpInCksumErrs);
2746 			ip_drop_input("icmpInCksumErrs", mp, ill);
2747 			freemsg(mp);
2748 			return;
2749 		}
2750 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
2751 		mp = icmp_inbound_v4(mp, ira);
2752 		if (mp == NULL) {
2753 			/* No need to pass to RAW sockets */
2754 			return;
2755 		}
2756 		break;
2757 
2758 	case IPPROTO_IGMP:
2759 		/*
2760 		 * If we are not willing to accept IGMP packets in clear,
2761 		 * then check with global policy.
2762 		 */
2763 		if (ipst->ips_igmp_accept_clear_messages == 0) {
2764 			mp = ipsec_check_global_policy(mp, NULL,
2765 			    ipha, NULL, ira, ns);
2766 			if (mp == NULL)
2767 				return;
2768 		}
2769 		if ((ira->ira_flags & IRAF_SYSTEM_LABELED) &&
2770 		    !tsol_can_accept_raw(mp, ira, B_TRUE)) {
2771 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2772 			ip_drop_input("ipIfStatsInDiscards", mp, ill);
2773 			freemsg(mp);
2774 			return;
2775 		}
2776 		/*
2777 		 * Validate checksum
2778 		 */
2779 		if (IP_CSUM(mp, ip_hdr_length, 0)) {
2780 			++ipst->ips_igmpstat.igps_rcv_badsum;
2781 			ip_drop_input("igps_rcv_badsum", mp, ill);
2782 			freemsg(mp);
2783 			return;
2784 		}
2785 
2786 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
2787 		mp = igmp_input(mp, ira);
2788 		if (mp == NULL) {
2789 			/* Bad packet - discarded by igmp_input */
2790 			return;
2791 		}
2792 		break;
2793 	case IPPROTO_PIM:
2794 		/*
2795 		 * If we are not willing to accept PIM packets in clear,
2796 		 * then check with global policy.
2797 		 */
2798 		if (ipst->ips_pim_accept_clear_messages == 0) {
2799 			mp = ipsec_check_global_policy(mp, NULL,
2800 			    ipha, NULL, ira, ns);
2801 			if (mp == NULL)
2802 				return;
2803 		}
2804 		if ((ira->ira_flags & IRAF_SYSTEM_LABELED) &&
2805 		    !tsol_can_accept_raw(mp, ira, B_TRUE)) {
2806 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2807 			ip_drop_input("ipIfStatsInDiscards", mp, ill);
2808 			freemsg(mp);
2809 			return;
2810 		}
2811 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
2812 
2813 		/* Checksum is verified in pim_input */
2814 		mp = pim_input(mp, ira);
2815 		if (mp == NULL) {
2816 			/* Bad packet - discarded by pim_input */
2817 			return;
2818 		}
2819 		break;
2820 	case IPPROTO_AH:
2821 	case IPPROTO_ESP: {
2822 		/*
2823 		 * Fast path for AH/ESP.
2824 		 */
2825 		netstack_t *ns = ipst->ips_netstack;
2826 		ipsec_stack_t *ipss = ns->netstack_ipsec;
2827 
2828 		IP_STAT(ipst, ipsec_proto_ahesp);
2829 
2830 		if (!ipsec_loaded(ipss)) {
2831 			ip_proto_not_sup(mp, ira);
2832 			return;
2833 		}
2834 
2835 		BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
2836 		/* select inbound SA and have IPsec process the pkt */
2837 		if (protocol == IPPROTO_ESP) {
2838 			esph_t *esph;
2839 			boolean_t esp_in_udp_sa;
2840 			boolean_t esp_in_udp_packet;
2841 
2842 			mp = ipsec_inbound_esp_sa(mp, ira, &esph);
2843 			if (mp == NULL)
2844 				return;
2845 
2846 			ASSERT(esph != NULL);
2847 			ASSERT(ira->ira_flags & IRAF_IPSEC_SECURE);
2848 			ASSERT(ira->ira_ipsec_esp_sa != NULL);
2849 			ASSERT(ira->ira_ipsec_esp_sa->ipsa_input_func != NULL);
2850 
2851 			esp_in_udp_sa = ((ira->ira_ipsec_esp_sa->ipsa_flags &
2852 			    IPSA_F_NATT) != 0);
2853 			esp_in_udp_packet =
2854 			    (ira->ira_flags & IRAF_ESP_UDP_PORTS) != 0;
2855 
2856 			/*
2857 			 * The following is a fancy, but quick, way of saying:
2858 			 * ESP-in-UDP SA and Raw ESP packet --> drop
2859 			 *    OR
2860 			 * ESP SA and ESP-in-UDP packet --> drop
2861 			 */
2862 			if (esp_in_udp_sa != esp_in_udp_packet) {
2863 				BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2864 				ip_drop_packet(mp, B_TRUE, ira->ira_ill,
2865 				    DROPPER(ipss, ipds_esp_no_sa),
2866 				    &ipss->ipsec_dropper);
2867 				return;
2868 			}
2869 			mp = ira->ira_ipsec_esp_sa->ipsa_input_func(mp, esph,
2870 			    ira);
2871 		} else {
2872 			ah_t *ah;
2873 
2874 			mp = ipsec_inbound_ah_sa(mp, ira, &ah);
2875 			if (mp == NULL)
2876 				return;
2877 
2878 			ASSERT(ah != NULL);
2879 			ASSERT(ira->ira_flags & IRAF_IPSEC_SECURE);
2880 			ASSERT(ira->ira_ipsec_ah_sa != NULL);
2881 			ASSERT(ira->ira_ipsec_ah_sa->ipsa_input_func != NULL);
2882 			mp = ira->ira_ipsec_ah_sa->ipsa_input_func(mp, ah,
2883 			    ira);
2884 		}
2885 
2886 		if (mp == NULL) {
2887 			/*
2888 			 * Either it failed or is pending. In the former case
2889 			 * ipIfStatsInDiscards was increased.
2890 			 */
2891 			return;
2892 		}
2893 		/* we're done with IPsec processing, send it up */
2894 		ip_input_post_ipsec(mp, ira);
2895 		return;
2896 	}
2897 	case IPPROTO_ENCAP: {
2898 		ipha_t		*inner_ipha;
2899 
2900 		/*
2901 		 * Handle self-encapsulated packets (IP-in-IP where
2902 		 * the inner addresses == the outer addresses).
2903 		 */
2904 		if ((uchar_t *)ipha + ip_hdr_length + sizeof (ipha_t) >
2905 		    mp->b_wptr) {
2906 			if (ira->ira_pktlen <
2907 			    ip_hdr_length + sizeof (ipha_t)) {
2908 				BUMP_MIB(ill->ill_ip_mib,
2909 				    ipIfStatsInTruncatedPkts);
2910 				ip_drop_input("ipIfStatsInTruncatedPkts",
2911 				    mp, ill);
2912 				freemsg(mp);
2913 				return;
2914 			}
2915 			ipha = ip_pullup(mp, (uchar_t *)ipha + ip_hdr_length +
2916 			    sizeof (ipha_t) - mp->b_rptr, ira);
2917 			if (ipha == NULL) {
2918 				BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2919 				ip_drop_input("ipIfStatsInDiscards", mp, ill);
2920 				freemsg(mp);
2921 				return;
2922 			}
2923 		}
2924 		inner_ipha = (ipha_t *)((uchar_t *)ipha + ip_hdr_length);
2925 		/*
2926 		 * Check the sanity of the inner IP header.
2927 		 */
2928 		if ((IPH_HDR_VERSION(inner_ipha) != IPV4_VERSION)) {
2929 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2930 			ip_drop_input("ipIfStatsInDiscards", mp, ill);
2931 			freemsg(mp);
2932 			return;
2933 		}
2934 		if (IPH_HDR_LENGTH(inner_ipha) < sizeof (ipha_t)) {
2935 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2936 			ip_drop_input("ipIfStatsInDiscards", mp, ill);
2937 			freemsg(mp);
2938 			return;
2939 		}
2940 		if (inner_ipha->ipha_src != ipha->ipha_src ||
2941 		    inner_ipha->ipha_dst != ipha->ipha_dst) {
2942 			/* We fallthru to iptun fanout below */
2943 			goto iptun;
2944 		}
2945 
2946 		/*
2947 		 * Self-encapsulated tunnel packet. Remove
2948 		 * the outer IP header and fanout again.
2949 		 * We also need to make sure that the inner
2950 		 * header is pulled up until options.
2951 		 */
2952 		mp->b_rptr = (uchar_t *)inner_ipha;
2953 		ipha = inner_ipha;
2954 		ip_hdr_length = IPH_HDR_LENGTH(ipha);
2955 		if ((uchar_t *)ipha + ip_hdr_length > mp->b_wptr) {
2956 			if (ira->ira_pktlen <
2957 			    (uchar_t *)ipha + ip_hdr_length - mp->b_rptr) {
2958 				BUMP_MIB(ill->ill_ip_mib,
2959 				    ipIfStatsInTruncatedPkts);
2960 				ip_drop_input("ipIfStatsInTruncatedPkts",
2961 				    mp, ill);
2962 				freemsg(mp);
2963 				return;
2964 			}
2965 			ipha = ip_pullup(mp,
2966 			    (uchar_t *)ipha + ip_hdr_length - mp->b_rptr, ira);
2967 			if (ipha == NULL) {
2968 				BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
2969 				ip_drop_input("ipIfStatsInDiscards", mp, ill);
2970 				freemsg(mp);
2971 				return;
2972 			}
2973 		}
2974 		if (ip_hdr_length > sizeof (ipha_t)) {
2975 			/* We got options on the inner packet. */
2976 			ipaddr_t	dst = ipha->ipha_dst;
2977 			int		error = 0;
2978 
2979 			dst = ip_input_options(ipha, dst, mp, ira, &error);
2980 			if (error != 0) {
2981 				/*
2982 				 * An ICMP error has been sent and the packet
2983 				 * has been dropped.
2984 				 */
2985 				return;
2986 			}
2987 			if (dst != ipha->ipha_dst) {
2988 				/*
2989 				 * Someone put a source-route in
2990 				 * the inside header of a self-
2991 				 * encapsulated packet.  Drop it
2992 				 * with extreme prejudice and let
2993 				 * the sender know.
2994 				 */
2995 				ip_drop_input("ICMP_SOURCE_ROUTE_FAILED",
2996 				    mp, ill);
2997 				icmp_unreachable(mp, ICMP_SOURCE_ROUTE_FAILED,
2998 				    ira);
2999 				return;
3000 			}
3001 		}
3002 		if (!(ira->ira_flags & IRAF_IPSEC_SECURE)) {
3003 			/*
3004 			 * This means that somebody is sending
3005 			 * Self-encapsualted packets without AH/ESP.
3006 			 *
3007 			 * Send this packet to find a tunnel endpoint.
3008 			 * if I can't find one, an ICMP
3009 			 * PROTOCOL_UNREACHABLE will get sent.
3010 			 */
3011 			protocol = ipha->ipha_protocol;
3012 			ira->ira_protocol = protocol;
3013 			goto iptun;
3014 		}
3015 
3016 		/* Update based on removed IP header */
3017 		ira->ira_ip_hdr_length = ip_hdr_length;
3018 		ira->ira_pktlen = ntohs(ipha->ipha_length);
3019 
3020 		if (ira->ira_flags & IRAF_IPSEC_DECAPS) {
3021 			/*
3022 			 * This packet is self-encapsulated multiple
3023 			 * times. We don't want to recurse infinitely.
3024 			 * To keep it simple, drop the packet.
3025 			 */
3026 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
3027 			ip_drop_input("ipIfStatsInDiscards", mp, ill);
3028 			freemsg(mp);
3029 			return;
3030 		}
3031 		ASSERT(ira->ira_flags & IRAF_IPSEC_SECURE);
3032 		ira->ira_flags |= IRAF_IPSEC_DECAPS;
3033 
3034 		ip_input_post_ipsec(mp, ira);
3035 		return;
3036 	}
3037 
3038 	iptun:	/* IPPROTO_ENCAPS that is not self-encapsulated */
3039 	case IPPROTO_IPV6:
3040 		/* iptun will verify trusted label */
3041 		connp = ipcl_classify_v4(mp, protocol, ip_hdr_length,
3042 		    ira, ipst);
3043 		if (connp != NULL) {
3044 			BUMP_MIB(ill->ill_ip_mib, ipIfStatsHCInDelivers);
3045 			ira->ira_ill = ira->ira_rill = NULL;
3046 			(connp->conn_recv)(connp, mp, NULL, ira);
3047 			CONN_DEC_REF(connp);
3048 			ira->ira_ill = ill;
3049 			ira->ira_rill = rill;
3050 			return;
3051 		}
3052 		/* FALLTHRU */
3053 	default:
3054 		/*
3055 		 * On a labeled system, we have to check whether the zone
3056 		 * itself is permitted to receive raw traffic.
3057 		 */
3058 		if (ira->ira_flags & IRAF_SYSTEM_LABELED) {
3059 			if (!tsol_can_accept_raw(mp, ira, B_FALSE)) {
3060 				BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
3061 				ip_drop_input("ipIfStatsInDiscards", mp, ill);
3062 				freemsg(mp);
3063 				return;
3064 			}
3065 		}
3066 		break;
3067 	}
3068 
3069 	/*
3070 	 * The above input functions may have returned the pulled up message.
3071 	 * So ipha need to be reinitialized.
3072 	 */
3073 	ipha = (ipha_t *)mp->b_rptr;
3074 	ira->ira_protocol = protocol = ipha->ipha_protocol;
3075 	if (ipst->ips_ipcl_proto_fanout_v4[protocol].connf_head == NULL) {
3076 		/*
3077 		 * No user-level listener for these packets packets.
3078 		 * Check for IPPROTO_ENCAP...
3079 		 */
3080 		if (protocol == IPPROTO_ENCAP && ipst->ips_ip_g_mrouter) {
3081 			/*
3082 			 * Check policy here,
3083 			 * THEN ship off to ip_mroute_decap().
3084 			 *
3085 			 * BTW,  If I match a configured IP-in-IP
3086 			 * tunnel above, this path will not be reached, and
3087 			 * ip_mroute_decap will never be called.
3088 			 */
3089 			mp = ipsec_check_global_policy(mp, connp,
3090 			    ipha, NULL, ira, ns);
3091 			if (mp != NULL) {
3092 				ip_mroute_decap(mp, ira);
3093 			} /* Else we already freed everything! */
3094 		} else {
3095 			ip_proto_not_sup(mp, ira);
3096 		}
3097 		return;
3098 	}
3099 
3100 	/*
3101 	 * Handle fanout to raw sockets.  There
3102 	 * can be more than one stream bound to a particular
3103 	 * protocol.  When this is the case, each one gets a copy
3104 	 * of any incoming packets.
3105 	 */
3106 	ASSERT(ira->ira_protocol == ipha->ipha_protocol);
3107 	ip_fanout_proto_v4(mp, ipha, ira);
3108 	return;
3109 
3110 discard:
3111 	BUMP_MIB(ill->ill_ip_mib, ipIfStatsInDiscards);
3112 	ip_drop_input("ipIfStatsInDiscards", mp, ill);
3113 	freemsg(mp);
3114 #undef rptr
3115 }
3116