1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 
  22 /*
  23  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 /*
  28  * Copyright 2019 Joyent, Inc.
  29  * Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
  30  */
  31 
  32 #ifndef _INET_IP_STACK_H
  33 #define _INET_IP_STACK_H
  34 
  35 #ifdef  __cplusplus
  36 extern "C" {
  37 #endif
  38 
  39 #include <sys/netstack.h>
  40 #include <netinet/igmp_var.h>
  41 #include <sys/modhash.h>
  42 
  43 #ifdef _KERNEL
  44 #include <sys/list.h>
  45 
  46 
  47 /*
  48  * IP statistics.
  49  */
  50 #define IP_STAT(ipst, x)        ((ipst)->ips_ip_statistics.x.value.ui64++)
  51 #define IP_STAT_UPDATE(ipst, x, n) \
  52                 ((ipst)->ips_ip_statistics.x.value.ui64 += (n))
  53 
  54 typedef struct ip_stat {
  55         kstat_named_t   ip_udp_fannorm;
  56         kstat_named_t   ip_udp_fanmb;
  57         kstat_named_t   ip_recv_pullup;
  58         kstat_named_t   ip_db_ref;
  59         kstat_named_t   ip_notaligned;
  60         kstat_named_t   ip_multimblk;
  61         kstat_named_t   ip_opt;
  62         kstat_named_t   ipsec_proto_ahesp;
  63         kstat_named_t   ip_conn_flputbq;
  64         kstat_named_t   ip_conn_walk_drain;
  65         kstat_named_t   ip_out_sw_cksum;
  66         kstat_named_t   ip_out_sw_cksum_bytes;
  67         kstat_named_t   ip_in_sw_cksum;
  68         kstat_named_t   ip_ire_reclaim_calls;
  69         kstat_named_t   ip_ire_reclaim_deleted;
  70         kstat_named_t   ip_nce_reclaim_calls;
  71         kstat_named_t   ip_nce_reclaim_deleted;
  72         kstat_named_t   ip_nce_mcast_reclaim_calls;
  73         kstat_named_t   ip_nce_mcast_reclaim_deleted;
  74         kstat_named_t   ip_nce_mcast_reclaim_tqfail;
  75         kstat_named_t   ip_dce_reclaim_calls;
  76         kstat_named_t   ip_dce_reclaim_deleted;
  77         kstat_named_t   ip_tcp_in_full_hw_cksum_err;
  78         kstat_named_t   ip_tcp_in_part_hw_cksum_err;
  79         kstat_named_t   ip_tcp_in_sw_cksum_err;
  80         kstat_named_t   ip_udp_in_full_hw_cksum_err;
  81         kstat_named_t   ip_udp_in_part_hw_cksum_err;
  82         kstat_named_t   ip_udp_in_sw_cksum_err;
  83         kstat_named_t   conn_in_recvdstaddr;
  84         kstat_named_t   conn_in_recvopts;
  85         kstat_named_t   conn_in_recvif;
  86         kstat_named_t   conn_in_recvslla;
  87         kstat_named_t   conn_in_recvucred;
  88         kstat_named_t   conn_in_recvttl;
  89         kstat_named_t   conn_in_recvtos;
  90         kstat_named_t   conn_in_recvhopopts;
  91         kstat_named_t   conn_in_recvhoplimit;
  92         kstat_named_t   conn_in_recvdstopts;
  93         kstat_named_t   conn_in_recvrthdrdstopts;
  94         kstat_named_t   conn_in_recvrthdr;
  95         kstat_named_t   conn_in_recvpktinfo;
  96         kstat_named_t   conn_in_recvtclass;
  97         kstat_named_t   conn_in_timestamp;
  98 } ip_stat_t;
  99 
 100 
 101 /*
 102  * IP6 statistics.
 103  */
 104 #define IP6_STAT(ipst, x)       ((ipst)->ips_ip6_statistics.x.value.ui64++)
 105 #define IP6_STAT_UPDATE(ipst, x, n)     \
 106         ((ipst)->ips_ip6_statistics.x.value.ui64 += (n))
 107 
 108 typedef struct ip6_stat {
 109         kstat_named_t   ip6_udp_fannorm;
 110         kstat_named_t   ip6_udp_fanmb;
 111         kstat_named_t   ip6_recv_pullup;
 112         kstat_named_t   ip6_db_ref;
 113         kstat_named_t   ip6_notaligned;
 114         kstat_named_t   ip6_multimblk;
 115         kstat_named_t   ipsec_proto_ahesp;
 116         kstat_named_t   ip6_out_sw_cksum;
 117         kstat_named_t   ip6_out_sw_cksum_bytes;
 118         kstat_named_t   ip6_in_sw_cksum;
 119         kstat_named_t   ip6_tcp_in_full_hw_cksum_err;
 120         kstat_named_t   ip6_tcp_in_part_hw_cksum_err;
 121         kstat_named_t   ip6_tcp_in_sw_cksum_err;
 122         kstat_named_t   ip6_udp_in_full_hw_cksum_err;
 123         kstat_named_t   ip6_udp_in_part_hw_cksum_err;
 124         kstat_named_t   ip6_udp_in_sw_cksum_err;
 125 } ip6_stat_t;
 126 
 127 typedef struct ire_stats {
 128         uint64_t ire_stats_alloced;     /* # of ires alloced */
 129         uint64_t ire_stats_freed;       /* # of ires freed */
 130         uint64_t ire_stats_inserted;    /* # of ires inserted in the bucket */
 131         uint64_t ire_stats_deleted;     /* # of ires deleted from the bucket */
 132 } ire_stats_t;
 133 
 134 #define TX_FANOUT_SIZE  128
 135 #define IDLHASHINDEX(X) \
 136         ((((uintptr_t)(X) >> 2) + ((uintptr_t)(X) >> 9)) & (TX_FANOUT_SIZE - 1))
 137 
 138 /* Data structure to represent addresses */
 139 typedef struct srcid_map {
 140         struct srcid_map        *sm_next;
 141         in6_addr_t              sm_addr;        /* Local address */
 142         uint_t                  sm_srcid;       /* source id */
 143         uint_t                  sm_refcnt;      /* > 1 ipif with same addr? */
 144         zoneid_t                sm_zoneid;      /* zone id */
 145 } srcid_map_t;
 146 
 147 /*
 148  * IP stack instances
 149  */
 150 struct ip_stack {
 151         netstack_t      *ips_netstack;  /* Common netstack */
 152 
 153         uint_t                  ips_src_generation;     /* Both IPv4 and IPv6 */
 154 
 155         struct mod_prop_info_s  *ips_propinfo_tbl;      /* ip tunables table */
 156 
 157         mib2_ipIfStatsEntry_t   ips_ip_mib;     /* SNMP fixed size info */
 158         mib2_icmp_t     ips_icmp_mib;
 159         /*
 160          * IPv6 mibs when the interface (ill) is not known.
 161          * When the ill is known the per-interface mib in the ill is used.
 162          */
 163         mib2_ipIfStatsEntry_t   ips_ip6_mib;
 164         mib2_ipv6IfIcmpEntry_t  ips_icmp6_mib;
 165 
 166         struct igmpstat         ips_igmpstat;
 167 
 168         kstat_t         *ips_ip_mibkp;  /* kstat exporting ip_mib data */
 169         kstat_t         *ips_icmp_mibkp; /* kstat exporting icmp_mib data */
 170         kstat_t         *ips_ip_kstat;
 171         ip_stat_t       ips_ip_statistics;
 172         kstat_t         *ips_ip6_kstat;
 173         ip6_stat_t      ips_ip6_statistics;
 174 
 175 /* ip.c */
 176         kmutex_t        ips_igmp_timer_lock;
 177         kmutex_t        ips_mld_timer_lock;
 178         kmutex_t        ips_ip_mi_lock;
 179         kmutex_t        ips_ip_addr_avail_lock;
 180         krwlock_t       ips_ill_g_lock;
 181 
 182         krwlock_t       ips_ill_g_usesrc_lock;
 183 
 184         /* Taskq dispatcher for capability operations */
 185         kmutex_t        ips_capab_taskq_lock;
 186         kcondvar_t      ips_capab_taskq_cv;
 187         mblk_t          *ips_capab_taskq_head;
 188         mblk_t          *ips_capab_taskq_tail;
 189         kthread_t       *ips_capab_taskq_thread;
 190         boolean_t       ips_capab_taskq_quit;
 191 
 192 /* ipclassifier.c - keep in ip_stack_t */
 193         /* ipclassifier hash tables */
 194         struct connf_s  *ips_rts_clients;
 195         struct connf_s  *ips_ipcl_conn_fanout;
 196         struct connf_s  *ips_ipcl_bind_fanout;
 197         struct connf_s  *ips_ipcl_proto_fanout_v4;
 198         struct connf_s  *ips_ipcl_proto_fanout_v6;
 199         struct connf_s  *ips_ipcl_udp_fanout;
 200         struct connf_s  *ips_ipcl_raw_fanout;           /* RAW SCTP sockets */
 201         struct connf_s  *ips_ipcl_iptun_fanout;
 202         uint_t          ips_ipcl_conn_fanout_size;
 203         uint_t          ips_ipcl_bind_fanout_size;
 204         uint_t          ips_ipcl_udp_fanout_size;
 205         uint_t          ips_ipcl_raw_fanout_size;
 206         uint_t          ips_ipcl_iptun_fanout_size;
 207         struct connf_s  *ips_ipcl_globalhash_fanout;
 208         int             ips_conn_g_index;
 209 
 210 /* ip.c */
 211         /* Following protected by igmp_timer_lock */
 212         int             ips_igmp_time_to_next;  /* Time since last timeout */
 213         int             ips_igmp_timer_scheduled_last;
 214         int             ips_igmp_deferred_next;
 215         timeout_id_t    ips_igmp_timeout_id;
 216         boolean_t       ips_igmp_timer_setter_active;
 217         boolean_t       ips_igmp_timer_quiesce;
 218 
 219         /* Following protected by mld_timer_lock */
 220         int             ips_mld_time_to_next;   /* Time since last timeout */
 221         int             ips_mld_timer_scheduled_last;
 222         int             ips_mld_deferred_next;
 223         timeout_id_t    ips_mld_timeout_id;
 224         boolean_t       ips_mld_timer_setter_active;
 225         boolean_t       ips_mld_timer_quiesce;
 226 
 227         /* Protected by igmp_slowtimeout_lock */
 228         timeout_id_t    ips_igmp_slowtimeout_id;
 229         kmutex_t        ips_igmp_slowtimeout_lock;
 230         boolean_t       ips_igmp_slowtimeout_quiesce;
 231 
 232         /* Protected by mld_slowtimeout_lock */
 233         timeout_id_t    ips_mld_slowtimeout_id;
 234         kmutex_t        ips_mld_slowtimeout_lock;
 235         boolean_t       ips_mld_slowtimeout_quiesce;
 236 
 237         /* IPv4 forwarding table */
 238         struct radix_node_head *ips_ip_ftable;
 239 
 240 #define IPV6_ABITS              128
 241 #define IP6_MASK_TABLE_SIZE     (IPV6_ABITS + 1)        /* 129 ptrs */
 242         struct irb      *ips_ip_forwarding_table_v6[IP6_MASK_TABLE_SIZE];
 243 
 244         /*
 245          * ire_ft_init_lock is used while initializing ip_forwarding_table
 246          * dynamically in ire_add.
 247          */
 248         kmutex_t        ips_ire_ft_init_lock;
 249 
 250         /*
 251          * This is the IPv6 counterpart of RADIX_NODE_HEAD_LOCK. It is used
 252          * to prevent adds and deletes while we are doing a ftable_lookup
 253          * and extracting the ire_generation.
 254          */
 255         krwlock_t       ips_ip6_ire_head_lock;
 256 
 257         uint32_t        ips_ip6_ftable_hash_size;
 258 
 259         ire_stats_t     ips_ire_stats_v4;       /* IPv4 ire statistics */
 260         ire_stats_t     ips_ire_stats_v6;       /* IPv6 ire statistics */
 261 
 262         /* Count how many condemned objects for kmem_cache callbacks */
 263         uint32_t        ips_num_ire_condemned;
 264         uint32_t        ips_num_nce_condemned;
 265         uint32_t        ips_num_dce_condemned;
 266 
 267         struct ire_s    *ips_ire_reject_v4;     /* For unreachable dests */
 268         struct ire_s    *ips_ire_reject_v6;     /* For unreachable dests */
 269         struct ire_s    *ips_ire_blackhole_v4;  /* For temporary failures */
 270         struct ire_s    *ips_ire_blackhole_v6;  /* For temporary failures */
 271 
 272         /* ips_ire_dep_lock protects ire_dep_* relationship between IREs */
 273         krwlock_t       ips_ire_dep_lock;
 274 
 275         /* Destination Cache Entries */
 276         struct dce_s    *ips_dce_default;
 277         uint_t          ips_dce_hashsize;
 278         struct dcb_s    *ips_dce_hash_v4;
 279         struct dcb_s    *ips_dce_hash_v6;
 280         uint_t          ips_dce_reclaim_needed;
 281 
 282         /* pending binds */
 283         mblk_t          *ips_ip6_asp_pending_ops;
 284         mblk_t          *ips_ip6_asp_pending_ops_tail;
 285 
 286         /* Synchronize updates with table usage */
 287         mblk_t          *ips_ip6_asp_pending_update; /* pending table updates */
 288 
 289         boolean_t       ips_ip6_asp_uip;        /* table update in progress */
 290         kmutex_t        ips_ip6_asp_lock;       /* protect all the above */
 291         uint32_t        ips_ip6_asp_refcnt;     /* outstanding references */
 292 
 293         struct ip6_asp  *ips_ip6_asp_table;
 294         /* The number of policy entries in the table */
 295         uint_t          ips_ip6_asp_table_count;
 296 
 297         struct conn_s   *ips_ip_g_mrouter;
 298 
 299         /* Time since last icmp_pkt_err */
 300         clock_t         ips_icmp_pkt_err_last;
 301         /* Number of packets sent in burst */
 302         uint_t          ips_icmp_pkt_err_sent;
 303 
 304         /* Protected by ip_mi_lock */
 305         void            *ips_ip_g_head; /* IP Instance Data List Head */
 306         void            *ips_arp_g_head; /* ARP Instance Data List Head */
 307 
 308         /* Multirouting stuff */
 309         /* Interval (in ms) between consecutive 'bad MTU' warnings */
 310         hrtime_t        ips_ip_multirt_log_interval;
 311         /* Time since last warning issued. */
 312         hrtime_t        ips_multirt_bad_mtu_last_time;
 313 
 314         /*
 315          * CGTP hooks. Enabling and disabling of hooks is controlled by an
 316          * IP tunable 'ips_ip_cgtp_filter'.
 317          */
 318         struct cgtp_filter_ops *ips_ip_cgtp_filter_ops;
 319 
 320         struct ipsq_s   *ips_ipsq_g_head;
 321         uint_t          ips_ill_index;  /* Used to assign interface indicies */
 322         /* When set search for unused index */
 323         boolean_t       ips_ill_index_wrap;
 324 
 325         uint_t          ips_loopback_packets;
 326 
 327         /* NDP/NCE structures for IPv4 and IPv6 */
 328         struct ndp_g_s  *ips_ndp4;
 329         struct ndp_g_s  *ips_ndp6;
 330 
 331         /* ip_mroute stuff */
 332         kmutex_t        ips_ip_g_mrouter_mutex;
 333 
 334         struct mrtstat  *ips_mrtstat;   /* Stats for netstat */
 335         int             ips_saved_ip_forwarding;
 336 
 337         /* numvifs is only a hint about the max interface being used. */
 338         ushort_t        ips_numvifs;
 339         kmutex_t        ips_numvifs_mutex;
 340 
 341         struct vif      *ips_vifs;
 342         struct mfcb     *ips_mfcs;      /* kernel routing table */
 343         struct tbf      *ips_tbfs;
 344         /*
 345          * One-back cache used to locate a tunnel's vif,
 346          * given a datagram's src ip address.
 347          */
 348         ipaddr_t        ips_last_encap_src;
 349         struct vif      *ips_last_encap_vif;
 350         kmutex_t        ips_last_encap_lock;    /* Protects the above */
 351 
 352         /*
 353          * reg_vif_num is protected by numvifs_mutex
 354          */
 355         /* Whether or not special PIM assert processing is enabled. */
 356         ushort_t        ips_reg_vif_num;        /* Index to Register vif */
 357         int             ips_pim_assert;
 358 
 359         union ill_g_head_u *ips_ill_g_heads;   /* ILL List Head */
 360 
 361         kstat_t         *ips_loopback_ksp;
 362 
 363         /* Array of conn drain lists */
 364         struct idl_tx_list_s    *ips_idl_tx_list;
 365         uint_t          ips_conn_drain_list_cnt; /* Count of conn_drain_list */
 366 
 367         /*
 368          * ID used to assign next free one.
 369          * Increases by one. Once it wraps we search for an unused ID.
 370          */
 371         uint_t          ips_ip_src_id;
 372         boolean_t       ips_srcid_wrapped;
 373 
 374         struct srcid_map *ips_srcid_head;
 375         krwlock_t       ips_srcid_lock;
 376 
 377         uint64_t        ips_ipif_g_seqid;       /* Used only for sctp_addr.c */
 378         union phyint_list_u *ips_phyint_g_list; /* start of phyint list */
 379 
 380 /* ip_netinfo.c */
 381         hook_family_t   ips_ipv4root;
 382         hook_family_t   ips_ipv6root;
 383         hook_family_t   ips_arproot;
 384 
 385         net_handle_t            ips_ipv4_net_data;
 386         net_handle_t            ips_ipv6_net_data;
 387         net_handle_t            ips_arp_net_data;
 388 
 389         /*
 390          * Hooks for firewalling
 391          */
 392         hook_event_t            ips_ip4_physical_in_event;
 393         hook_event_t            ips_ip4_physical_out_event;
 394         hook_event_t            ips_ip4_forwarding_event;
 395         hook_event_t            ips_ip4_loopback_in_event;
 396         hook_event_t            ips_ip4_loopback_out_event;
 397 
 398         hook_event_t            ips_ip6_physical_in_event;
 399         hook_event_t            ips_ip6_physical_out_event;
 400         hook_event_t            ips_ip6_forwarding_event;
 401         hook_event_t            ips_ip6_loopback_in_event;
 402         hook_event_t            ips_ip6_loopback_out_event;
 403 
 404         hook_event_t            ips_arp_physical_in_event;
 405         hook_event_t            ips_arp_physical_out_event;
 406         hook_event_t            ips_arp_nic_events;
 407 
 408         hook_event_token_t      ips_ipv4firewall_physical_in;
 409         hook_event_token_t      ips_ipv4firewall_physical_out;
 410         hook_event_token_t      ips_ipv4firewall_forwarding;
 411         hook_event_token_t      ips_ipv4firewall_loopback_in;
 412         hook_event_token_t      ips_ipv4firewall_loopback_out;
 413 
 414         hook_event_token_t      ips_ipv6firewall_physical_in;
 415         hook_event_token_t      ips_ipv6firewall_physical_out;
 416         hook_event_token_t      ips_ipv6firewall_forwarding;
 417         hook_event_token_t      ips_ipv6firewall_loopback_in;
 418         hook_event_token_t      ips_ipv6firewall_loopback_out;
 419 
 420         hook_event_t            ips_ip4_nic_events;
 421         hook_event_t            ips_ip6_nic_events;
 422         hook_event_token_t      ips_ipv4nicevents;
 423         hook_event_token_t      ips_ipv6nicevents;
 424 
 425         hook_event_token_t      ips_arp_physical_in;
 426         hook_event_token_t      ips_arp_physical_out;
 427         hook_event_token_t      ips_arpnicevents;
 428 
 429         net_handle_t            ips_ip4_observe_pr;
 430         net_handle_t            ips_ip6_observe_pr;
 431         hook_event_t            ips_ip4_observe;
 432         hook_event_t            ips_ip6_observe;
 433         hook_event_token_t      ips_ipv4observing;
 434         hook_event_token_t      ips_ipv6observing;
 435 
 436         struct __ldi_ident      *ips_ldi_ident;
 437 
 438 /* ipmp.c */
 439         krwlock_t               ips_ipmp_lock;
 440         mod_hash_t              *ips_ipmp_grp_hash;
 441 
 442 };
 443 typedef struct ip_stack ip_stack_t;
 444 
 445 /* Finding an ip_stack_t */
 446 #define CONNQ_TO_IPST(_q)       (Q_TO_CONN(_q)->conn_netstack->netstack_ip)
 447 #define ILLQ_TO_IPST(_q)        (((ill_t *)(_q)->q_ptr)->ill_ipst)
 448 #define PHYINT_TO_IPST(phyi)    ((phyi)->phyint_ipsq->ipsq_ipst)
 449 
 450 #else /* _KERNEL */
 451 typedef int ip_stack_t;
 452 #endif /* _KERNEL */
 453 
 454 #ifdef  __cplusplus
 455 }
 456 #endif
 457 
 458 #endif  /* _INET_IP_STACK_H */