xref: /illumos-gate/usr/src/uts/common/inet/ip_stack.h (revision 7d1e83948cb684521e72cab96020be241508f449)
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 2009 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #ifndef	_INET_IP_STACK_H
28 #define	_INET_IP_STACK_H
29 
30 #ifdef	__cplusplus
31 extern "C" {
32 #endif
33 
34 #include <sys/netstack.h>
35 #include <netinet/igmp_var.h>
36 #include <sys/modhash.h>
37 
38 #ifdef _KERNEL
39 #include <sys/list.h>
40 
41 /*
42  * IP statistics.
43  */
44 #define	IP_STAT(ipst, x)	((ipst)->ips_ip_statistics.x.value.ui64++)
45 #define	IP_STAT_UPDATE(ipst, x, n) \
46 		((ipst)->ips_ip_statistics.x.value.ui64 += (n))
47 
48 typedef struct ip_stat {
49 	kstat_named_t	ipsec_fanout_proto;
50 	kstat_named_t	ip_udp_fannorm;
51 	kstat_named_t	ip_udp_fanmb;
52 	kstat_named_t	ip_udp_fanothers;
53 	kstat_named_t	ip_udp_fast_path;
54 	kstat_named_t	ip_udp_slow_path;
55 	kstat_named_t	ip_udp_input_err;
56 	kstat_named_t	ip_tcppullup;
57 	kstat_named_t	ip_tcpoptions;
58 	kstat_named_t	ip_multipkttcp;
59 	kstat_named_t	ip_tcp_fast_path;
60 	kstat_named_t	ip_tcp_slow_path;
61 	kstat_named_t	ip_tcp_input_error;
62 	kstat_named_t	ip_db_ref;
63 	kstat_named_t	ip_notaligned1;
64 	kstat_named_t	ip_notaligned2;
65 	kstat_named_t	ip_multimblk3;
66 	kstat_named_t	ip_multimblk4;
67 	kstat_named_t	ip_ipoptions;
68 	kstat_named_t	ip_classify_fail;
69 	kstat_named_t	ip_opt;
70 	kstat_named_t	ip_udp_rput_local;
71 	kstat_named_t	ipsec_proto_ahesp;
72 	kstat_named_t	ip_conn_flputbq;
73 	kstat_named_t	ip_conn_walk_drain;
74 	kstat_named_t   ip_out_sw_cksum;
75 	kstat_named_t   ip_in_sw_cksum;
76 	kstat_named_t   ip_trash_ire_reclaim_calls;
77 	kstat_named_t   ip_trash_ire_reclaim_success;
78 	kstat_named_t   ip_ire_arp_timer_expired;
79 	kstat_named_t   ip_ire_redirect_timer_expired;
80 	kstat_named_t	ip_ire_pmtu_timer_expired;
81 	kstat_named_t	ip_input_multi_squeue;
82 	kstat_named_t	ip_tcp_in_full_hw_cksum_err;
83 	kstat_named_t	ip_tcp_in_part_hw_cksum_err;
84 	kstat_named_t	ip_tcp_in_sw_cksum_err;
85 	kstat_named_t	ip_tcp_out_sw_cksum_bytes;
86 	kstat_named_t	ip_udp_in_full_hw_cksum_err;
87 	kstat_named_t	ip_udp_in_part_hw_cksum_err;
88 	kstat_named_t	ip_udp_in_sw_cksum_err;
89 	kstat_named_t	ip_udp_out_sw_cksum_bytes;
90 	kstat_named_t	ip_frag_mdt_pkt_out;
91 	kstat_named_t	ip_frag_mdt_discarded;
92 	kstat_named_t	ip_frag_mdt_allocfail;
93 	kstat_named_t	ip_frag_mdt_addpdescfail;
94 	kstat_named_t	ip_frag_mdt_allocd;
95 } ip_stat_t;
96 
97 
98 /*
99  * IP6 statistics.
100  */
101 #define	IP6_STAT(ipst, x)	((ipst)->ips_ip6_statistics.x.value.ui64++)
102 #define	IP6_STAT_UPDATE(ipst, x, n)	\
103 	((ipst)->ips_ip6_statistics.x.value.ui64 += (n))
104 
105 typedef struct ip6_stat {
106 	kstat_named_t	ip6_udp_fast_path;
107 	kstat_named_t	ip6_udp_slow_path;
108 	kstat_named_t	ip6_udp_fannorm;
109 	kstat_named_t	ip6_udp_fanmb;
110 	kstat_named_t   ip6_out_sw_cksum;
111 	kstat_named_t   ip6_in_sw_cksum;
112 	kstat_named_t	ip6_tcp_in_full_hw_cksum_err;
113 	kstat_named_t	ip6_tcp_in_part_hw_cksum_err;
114 	kstat_named_t	ip6_tcp_in_sw_cksum_err;
115 	kstat_named_t	ip6_tcp_out_sw_cksum_bytes;
116 	kstat_named_t	ip6_udp_in_full_hw_cksum_err;
117 	kstat_named_t	ip6_udp_in_part_hw_cksum_err;
118 	kstat_named_t	ip6_udp_in_sw_cksum_err;
119 	kstat_named_t	ip6_udp_out_sw_cksum_bytes;
120 	kstat_named_t	ip6_frag_mdt_pkt_out;
121 	kstat_named_t	ip6_frag_mdt_discarded;
122 	kstat_named_t	ip6_frag_mdt_allocfail;
123 	kstat_named_t	ip6_frag_mdt_addpdescfail;
124 	kstat_named_t	ip6_frag_mdt_allocd;
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 	struct ipparam_s	*ips_param_arr; 	/* ndd variable table */
154 	struct ipndp_s		*ips_ndp_arr;
155 
156 	mib2_ipIfStatsEntry_t	ips_ip_mib;	/* SNMP fixed size info */
157 	mib2_icmp_t	ips_icmp_mib;
158 	/*
159 	 * IPv6 mibs when the interface (ill) is not known.
160 	 * When the ill is known the per-interface mib in the ill is used.
161 	 */
162 	mib2_ipIfStatsEntry_t	ips_ip6_mib;
163 	mib2_ipv6IfIcmpEntry_t	ips_icmp6_mib;
164 
165 	struct igmpstat		ips_igmpstat;
166 
167 	kstat_t		*ips_ip_mibkp;	/* kstat exporting ip_mib data */
168 	kstat_t		*ips_icmp_mibkp; /* kstat exporting icmp_mib data */
169 	kstat_t		*ips_ip_kstat;
170 	ip_stat_t	ips_ip_statistics;
171 	kstat_t		*ips_ip6_kstat;
172 	ip6_stat_t	ips_ip6_statistics;
173 
174 /* ip.c */
175 	krwlock_t	ips_ip_g_nd_lock;
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 	krwlock_t	ips_ipsec_capab_ills_lock;
182 				/* protects the list of IPsec capable ills */
183 	struct ipsec_capab_ill_s *ips_ipsec_capab_ills_ah;
184 	struct ipsec_capab_ill_s *ips_ipsec_capab_ills_esp;
185 
186 	krwlock_t	ips_ill_g_usesrc_lock;
187 
188 	/* Taskq dispatcher for capability operations */
189 	kmutex_t	ips_capab_taskq_lock;
190 	kcondvar_t	ips_capab_taskq_cv;
191 	mblk_t		*ips_capab_taskq_head;
192 	mblk_t		*ips_capab_taskq_tail;
193 	kthread_t	*ips_capab_taskq_thread;
194 	boolean_t	ips_capab_taskq_quit;
195 
196 /* ipclassifier.c - keep in ip_stack_t */
197 	/* ipclassifier hash tables */
198 	struct connf_s	*ips_rts_clients;
199 	struct connf_s	*ips_ipcl_conn_fanout;
200 	struct connf_s	*ips_ipcl_bind_fanout;
201 	struct connf_s	*ips_ipcl_proto_fanout;
202 	struct connf_s	*ips_ipcl_proto_fanout_v6;
203 	struct connf_s	*ips_ipcl_udp_fanout;
204 	struct connf_s	*ips_ipcl_raw_fanout;
205 	uint_t		ips_ipcl_conn_fanout_size;
206 	uint_t		ips_ipcl_bind_fanout_size;
207 	uint_t		ips_ipcl_udp_fanout_size;
208 	uint_t		ips_ipcl_raw_fanout_size;
209 	struct connf_s	*ips_ipcl_globalhash_fanout;
210 	int		ips_conn_g_index;
211 
212 /* ip.c */
213 	/* Following protected by igmp_timer_lock */
214 	int 		ips_igmp_time_to_next;	/* Time since last timeout */
215 	int 		ips_igmp_timer_scheduled_last;
216 	int		ips_igmp_deferred_next;
217 	timeout_id_t	ips_igmp_timeout_id;
218 	boolean_t	ips_igmp_timer_setter_active;
219 
220 	/* Following protected by mld_timer_lock */
221 	int 		ips_mld_time_to_next;	/* Time since last timeout */
222 	int 		ips_mld_timer_scheduled_last;
223 	int		ips_mld_deferred_next;
224 	timeout_id_t	ips_mld_timeout_id;
225 	boolean_t	ips_mld_timer_setter_active;
226 
227 	/* Protected by igmp_slowtimeout_lock */
228 	timeout_id_t	ips_igmp_slowtimeout_id;
229 	kmutex_t	ips_igmp_slowtimeout_lock;
230 
231 	/* Protected by mld_slowtimeout_lock */
232 	timeout_id_t	ips_mld_slowtimeout_id;
233 	kmutex_t	ips_mld_slowtimeout_lock;
234 
235 	/* IPv4 forwarding table */
236 	struct radix_node_head *ips_ip_ftable;
237 
238 	/* This is dynamically allocated in ip_ire_init */
239 	struct irb	 *ips_ip_cache_table;
240 
241 #define	IPV6_ABITS		128
242 #define	IP6_MASK_TABLE_SIZE	(IPV6_ABITS + 1)	/* 129 ptrs */
243 
244 	struct irb	*ips_ip_forwarding_table_v6[IP6_MASK_TABLE_SIZE];
245 	/* This is dynamically allocated in ip_ire_init */
246 	struct irb	*ips_ip_cache_table_v6;
247 
248 	uint32_t	ips_ire_handle;
249 	/*
250 	 * ire_ft_init_lock is used while initializing ip_forwarding_table
251 	 * dynamically in ire_add.
252 	 */
253 	kmutex_t	ips_ire_ft_init_lock;
254 	kmutex_t	ips_ire_handle_lock;	/* Protects ire_handle */
255 
256 	uint32_t	ips_ip_cache_table_size;
257 	uint32_t	ips_ip6_cache_table_size;
258 	uint32_t	ips_ip6_ftable_hash_size;
259 
260 	ire_stats_t 	ips_ire_stats_v4;	/* IPv4 ire statistics */
261 	ire_stats_t 	ips_ire_stats_v6;	/* IPv6 ire statistics */
262 
263 	/* pending binds */
264 	mblk_t		*ips_ip6_asp_pending_ops;
265 	mblk_t		*ips_ip6_asp_pending_ops_tail;
266 
267 	/* Synchronize updates with table usage */
268 	mblk_t		*ips_ip6_asp_pending_update; /* pending table updates */
269 
270 	boolean_t	ips_ip6_asp_uip;	/* table update in progress */
271 	kmutex_t	ips_ip6_asp_lock;	/* protect all the above */
272 	uint32_t	ips_ip6_asp_refcnt;	/* outstanding references */
273 
274 	struct ip6_asp	*ips_ip6_asp_table;
275 	/* The number of policy entries in the table */
276 	uint_t		ips_ip6_asp_table_count;
277 
278 	int		ips_ip_g_forward;
279 	int		ips_ipv6_forward;
280 
281 	time_t		ips_ip_g_frag_timeout;
282 	clock_t		ips_ip_g_frag_timo_ms;
283 	time_t		ips_ipv6_frag_timeout;
284 	clock_t		ips_ipv6_frag_timo_ms;
285 
286 	struct conn_s	*ips_ip_g_mrouter;
287 
288 	/* Time since last icmp_pkt_err */
289 	clock_t		ips_icmp_pkt_err_last;
290 	/* Number of packets sent in burst */
291 	uint_t		ips_icmp_pkt_err_sent;
292 
293 	/* Protected by ip_mi_lock */
294 	void		*ips_ip_g_head;		/* Instance Data List Head */
295 
296 	caddr_t		ips_ip_g_nd;		/* Named Dispatch List Head */
297 
298 	/* Multirouting stuff */
299 	/* Interval (in ms) between consecutive 'bad MTU' warnings */
300 	hrtime_t	ips_ip_multirt_log_interval;
301 	/* Time since last warning issued. */
302 	hrtime_t	ips_multirt_bad_mtu_last_time;
303 
304 	struct cgtp_filter_ops *ips_ip_cgtp_filter_ops;	/* CGTP hooks */
305 	boolean_t	ips_ip_cgtp_filter;	/* Enable/disable CGTP hooks */
306 
307 	kmutex_t	ips_ip_trash_timer_lock;
308 	timeout_id_t	ips_ip_ire_expire_id;	/* IRE expiration timer. */
309 	struct ipsq_s	*ips_ipsq_g_head;
310 	uint_t		ips_ill_index;	/* Used to assign interface indicies */
311 	/* When set search for unused index */
312 	boolean_t	ips_ill_index_wrap;
313 
314 	clock_t		ips_ip_ire_arp_time_elapsed;
315 			/* Time since IRE cache last flushed */
316 	clock_t		ips_ip_ire_rd_time_elapsed;
317 			/* ... redirect IREs last flushed */
318 	clock_t		ips_ip_ire_pmtu_time_elapsed;
319 			/* Time since path mtu increase */
320 
321 	uint_t		ips_ip_redirect_cnt;
322 			/* Num of redirect routes in ftable */
323 	uint_t		ips_ipv6_ire_default_count;
324 			/* Number of IPv6 IRE_DEFAULT entries */
325 	uint_t		ips_ipv6_ire_default_index;
326 			/* Walking IPv6 index used to mod in */
327 
328 	uint_t		ips_loopback_packets;
329 
330 	/* NDP/NCE structures for IPv4 and IPv6 */
331 	struct ndp_g_s	*ips_ndp4;
332 	struct ndp_g_s	*ips_ndp6;
333 
334 	/* ip_mroute stuff */
335 	kmutex_t	ips_ip_g_mrouter_mutex;
336 
337 	struct mrtstat	*ips_mrtstat;	/* Stats for netstat */
338 	int		ips_saved_ip_g_forward;
339 
340 	/* numvifs is only a hint about the max interface being used. */
341 	ushort_t	ips_numvifs;
342 	kmutex_t	ips_numvifs_mutex;
343 
344 	struct vif	*ips_vifs;
345 	struct mfcb	*ips_mfcs;	/* kernel routing table	*/
346 	struct tbf	*ips_tbfs;
347 	/*
348 	 * One-back cache used to locate a tunnel's vif,
349 	 * given a datagram's src ip address.
350 	 */
351 	ipaddr_t	ips_last_encap_src;
352 	struct vif	*ips_last_encap_vif;
353 	kmutex_t	ips_last_encap_lock;	/* Protects the above */
354 
355 	/*
356 	 * reg_vif_num is protected by numvifs_mutex
357 	 */
358 	/* Whether or not special PIM assert processing is enabled. */
359 	ushort_t	ips_reg_vif_num; 	/* Index to Register vif */
360 	int		ips_pim_assert;
361 
362 	union ill_g_head_u *ips_ill_g_heads;   /* ILL List Head */
363 
364 	kstat_t		*ips_loopback_ksp;
365 
366 	/* Array of conn drain lists */
367 	struct idl_tx_list_s	*ips_idl_tx_list;
368 	uint_t		ips_conn_drain_list_cnt; /* Count of conn_drain_list */
369 
370 	/*
371 	 * ID used to assign next free one.
372 	 * Increases by one. Once it wraps we search for an unused ID.
373 	 */
374 	uint_t		ips_ip_src_id;
375 	boolean_t	ips_srcid_wrapped;
376 
377 	struct srcid_map *ips_srcid_head;
378 	krwlock_t	ips_srcid_lock;
379 
380 	uint64_t	ips_ipif_g_seqid;
381 	union phyint_list_u *ips_phyint_g_list;	/* start of phyint list */
382 
383 /* ip_neti.c */
384 	hook_family_t	ips_ipv4root;
385 	hook_family_t	ips_ipv6root;
386 
387 	/*
388 	 * Hooks for firewalling
389 	 */
390 	hook_event_t		ips_ip4_physical_in_event;
391 	hook_event_t		ips_ip4_physical_out_event;
392 	hook_event_t		ips_ip4_forwarding_event;
393 	hook_event_t		ips_ip4_loopback_in_event;
394 	hook_event_t		ips_ip4_loopback_out_event;
395 	hook_event_t		ips_ip4_nic_events;
396 	hook_event_t		ips_ip6_physical_in_event;
397 	hook_event_t		ips_ip6_physical_out_event;
398 	hook_event_t		ips_ip6_forwarding_event;
399 	hook_event_t		ips_ip6_loopback_in_event;
400 	hook_event_t		ips_ip6_loopback_out_event;
401 	hook_event_t		ips_ip6_nic_events;
402 
403 	hook_event_token_t	ips_ipv4firewall_physical_in;
404 	hook_event_token_t	ips_ipv4firewall_physical_out;
405 	hook_event_token_t	ips_ipv4firewall_forwarding;
406 	hook_event_token_t	ips_ipv4firewall_loopback_in;
407 	hook_event_token_t	ips_ipv4firewall_loopback_out;
408 	hook_event_token_t	ips_ipv4nicevents;
409 	hook_event_token_t	ips_ipv6firewall_physical_in;
410 	hook_event_token_t	ips_ipv6firewall_physical_out;
411 	hook_event_token_t	ips_ipv6firewall_forwarding;
412 	hook_event_token_t	ips_ipv6firewall_loopback_in;
413 	hook_event_token_t	ips_ipv6firewall_loopback_out;
414 	hook_event_token_t	ips_ipv6nicevents;
415 
416 	net_handle_t		ips_ipv4_net_data;
417 	net_handle_t		ips_ipv6_net_data;
418 
419 	boolean_t		ips_ipobs_enabled;
420 	list_t			ips_ipobs_cb_list;
421 	kmutex_t		ips_ipobs_cb_lock;
422 	uint_t			ips_ipobs_cb_nwalkers;
423 	kcondvar_t		ips_ipobs_cb_cv;
424 
425 	struct __ldi_ident	*ips_ldi_ident;
426 
427 /* ipmp.c */
428 	krwlock_t		ips_ipmp_lock;
429 	mod_hash_t		*ips_ipmp_grp_hash;
430 
431 /* igmp.c */
432 	/* multicast restart timers thread logic */
433 	kmutex_t		ips_mrt_lock;
434 	uint_t			ips_mrt_flags;
435 	kcondvar_t		ips_mrt_cv;
436 	kcondvar_t		ips_mrt_done_cv;
437 	kthread_t		*ips_mrt_thread;
438 };
439 typedef struct ip_stack ip_stack_t;
440 
441 /* Finding an ip_stack_t */
442 #define	CONNQ_TO_IPST(_q)	(Q_TO_CONN(_q)->conn_netstack->netstack_ip)
443 #define	ILLQ_TO_IPST(_q)	(((ill_t *)(_q)->q_ptr)->ill_ipst)
444 #define	PHYINT_TO_IPST(phyi)	((phyi)->phyint_ipsq->ipsq_ipst)
445 
446 #else /* _KERNEL */
447 typedef int ip_stack_t;
448 #endif /* _KERNEL */
449 
450 #ifdef	__cplusplus
451 }
452 #endif
453 
454 #endif	/* _INET_IP_STACK_H */
455