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