xref: /titanic_51/usr/src/uts/common/inet/ip_stack.h (revision f875b4ebb1dd9fdbeb043557cab38ab3bf7f6e01)
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 2007 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 #pragma ident	"%Z%%M%	%I%	%E% SMI"
31 
32 #ifdef	__cplusplus
33 extern "C" {
34 #endif
35 
36 #include <sys/isa_defs.h>
37 #include <sys/md5.h>
38 #include <sys/types.h>
39 #include <inet/mib2.h>
40 #include <inet/nd.h>
41 #include <sys/atomic.h>
42 #include <sys/socket.h>
43 #include <sys/netstack.h>
44 #include <net/if_dl.h>
45 #include <net/if.h>
46 #include <netinet/ip.h>
47 #include <netinet/igmp_var.h>
48 #include <inet/ip.h>
49 #include <sys/taskq.h>
50 
51 #ifdef _KERNEL
52 #include <netinet/ip6.h>
53 #include <sys/avl.h>
54 #include <sys/vmem.h>
55 #include <sys/squeue.h>
56 #endif	/* _KERNEL */
57 
58 #ifdef _KERNEL
59 
60 
61 /*
62  * IP statistics.
63  */
64 #define	IP_STAT(ipst, x)	((ipst)->ips_ip_statistics.x.value.ui64++)
65 #define	IP_STAT_UPDATE(ipst, x, n) \
66 		((ipst)->ips_ip_statistics.x.value.ui64 += (n))
67 
68 typedef struct ip_stat {
69 	kstat_named_t	ipsec_fanout_proto;
70 	kstat_named_t	ip_udp_fannorm;
71 	kstat_named_t	ip_udp_fanmb;
72 	kstat_named_t	ip_udp_fanothers;
73 	kstat_named_t	ip_udp_fast_path;
74 	kstat_named_t	ip_udp_slow_path;
75 	kstat_named_t	ip_udp_input_err;
76 	kstat_named_t	ip_tcppullup;
77 	kstat_named_t	ip_tcpoptions;
78 	kstat_named_t	ip_multipkttcp;
79 	kstat_named_t	ip_tcp_fast_path;
80 	kstat_named_t	ip_tcp_slow_path;
81 	kstat_named_t	ip_tcp_input_error;
82 	kstat_named_t	ip_db_ref;
83 	kstat_named_t	ip_notaligned1;
84 	kstat_named_t	ip_notaligned2;
85 	kstat_named_t	ip_multimblk3;
86 	kstat_named_t	ip_multimblk4;
87 	kstat_named_t	ip_ipoptions;
88 	kstat_named_t	ip_classify_fail;
89 	kstat_named_t	ip_opt;
90 	kstat_named_t	ip_udp_rput_local;
91 	kstat_named_t	ipsec_proto_ahesp;
92 	kstat_named_t	ip_conn_flputbq;
93 	kstat_named_t	ip_conn_walk_drain;
94 	kstat_named_t   ip_out_sw_cksum;
95 	kstat_named_t   ip_in_sw_cksum;
96 	kstat_named_t   ip_trash_ire_reclaim_calls;
97 	kstat_named_t   ip_trash_ire_reclaim_success;
98 	kstat_named_t   ip_ire_arp_timer_expired;
99 	kstat_named_t   ip_ire_redirect_timer_expired;
100 	kstat_named_t	ip_ire_pmtu_timer_expired;
101 	kstat_named_t	ip_input_multi_squeue;
102 	kstat_named_t	ip_tcp_in_full_hw_cksum_err;
103 	kstat_named_t	ip_tcp_in_part_hw_cksum_err;
104 	kstat_named_t	ip_tcp_in_sw_cksum_err;
105 	kstat_named_t	ip_tcp_out_sw_cksum_bytes;
106 	kstat_named_t	ip_udp_in_full_hw_cksum_err;
107 	kstat_named_t	ip_udp_in_part_hw_cksum_err;
108 	kstat_named_t	ip_udp_in_sw_cksum_err;
109 	kstat_named_t	ip_udp_out_sw_cksum_bytes;
110 	kstat_named_t	ip_frag_mdt_pkt_out;
111 	kstat_named_t	ip_frag_mdt_discarded;
112 	kstat_named_t	ip_frag_mdt_allocfail;
113 	kstat_named_t	ip_frag_mdt_addpdescfail;
114 	kstat_named_t	ip_frag_mdt_allocd;
115 } ip_stat_t;
116 
117 
118 /*
119  * IP6 statistics.
120  */
121 #define	IP6_STAT(ipst, x)	((ipst)->ips_ip6_statistics.x.value.ui64++)
122 #define	IP6_STAT_UPDATE(ipst, x, n)	\
123 	((ipst)->ips_ip6_statistics.x.value.ui64 += (n))
124 
125 typedef struct ip6_stat {
126 	kstat_named_t	ip6_udp_fast_path;
127 	kstat_named_t	ip6_udp_slow_path;
128 	kstat_named_t	ip6_udp_fannorm;
129 	kstat_named_t	ip6_udp_fanmb;
130 	kstat_named_t   ip6_out_sw_cksum;
131 	kstat_named_t   ip6_in_sw_cksum;
132 	kstat_named_t	ip6_tcp_in_full_hw_cksum_err;
133 	kstat_named_t	ip6_tcp_in_part_hw_cksum_err;
134 	kstat_named_t	ip6_tcp_in_sw_cksum_err;
135 	kstat_named_t	ip6_tcp_out_sw_cksum_bytes;
136 	kstat_named_t	ip6_udp_in_full_hw_cksum_err;
137 	kstat_named_t	ip6_udp_in_part_hw_cksum_err;
138 	kstat_named_t	ip6_udp_in_sw_cksum_err;
139 	kstat_named_t	ip6_udp_out_sw_cksum_bytes;
140 	kstat_named_t	ip6_frag_mdt_pkt_out;
141 	kstat_named_t	ip6_frag_mdt_discarded;
142 	kstat_named_t	ip6_frag_mdt_allocfail;
143 	kstat_named_t	ip6_frag_mdt_addpdescfail;
144 	kstat_named_t	ip6_frag_mdt_allocd;
145 } ip6_stat_t;
146 
147 typedef struct ire_stats {
148 	uint64_t ire_stats_alloced;	/* # of ires alloced */
149 	uint64_t ire_stats_freed;	/* # of ires freed */
150 	uint64_t ire_stats_inserted;	/* # of ires inserted in the bucket */
151 	uint64_t ire_stats_deleted;	/* # of ires deleted from the bucket */
152 } ire_stats_t;
153 
154 
155 /*
156  * IP stack instances
157  */
158 struct ip_stack {
159 	netstack_t	*ips_netstack;	/* Common netstack */
160 
161 	struct ipparam_s	*ips_param_arr; 	/* ndd variable table */
162 	struct ipndp_s		*ips_ndp_arr;
163 
164 	mib2_ipIfStatsEntry_t	ips_ip_mib;	/* SNMP fixed size info */
165 	mib2_icmp_t	ips_icmp_mib;
166 	/*
167 	 * IPv6 mibs when the interface (ill) is not known.
168 	 * When the ill is known the per-interface mib in the ill is used.
169 	 */
170 	mib2_ipIfStatsEntry_t	ips_ip6_mib;
171 	mib2_ipv6IfIcmpEntry_t	ips_icmp6_mib;
172 
173 	struct igmpstat		ips_igmpstat;
174 
175 	kstat_t		*ips_ip_mibkp;	/* kstat exporting ip_mib data */
176 	kstat_t		*ips_icmp_mibkp; /* kstat exporting icmp_mib data */
177 	kstat_t		*ips_ip_kstat;
178 	ip_stat_t	ips_ip_statistics;
179 	kstat_t		*ips_ip6_kstat;
180 	ip6_stat_t	ips_ip6_statistics;
181 
182 /* ip.c */
183 	krwlock_t	ips_ip_g_nd_lock;
184 	kmutex_t	ips_igmp_timer_lock;
185 	kmutex_t	ips_mld_timer_lock;
186 	kmutex_t	ips_ip_mi_lock;
187 	kmutex_t	ips_ip_addr_avail_lock;
188 	krwlock_t	ips_ill_g_lock;
189 	krwlock_t	ips_ipsec_capab_ills_lock;
190 				/* protects the list of IPsec capable ills */
191 	struct ipsec_capab_ill_s *ips_ipsec_capab_ills_ah;
192 	struct ipsec_capab_ill_s *ips_ipsec_capab_ills_esp;
193 
194 	krwlock_t	ips_ill_g_usesrc_lock;
195 
196 	struct ill_group *ips_illgrp_head_v4;	/* Head of IPv4 ill groups */
197 	struct ill_group *ips_illgrp_head_v6;	/* Head of IPv6 ill groups */
198 
199 /* ipclassifier.c - keep in ip_stack_t */
200 	/* ipclassifier hash tables */
201 	struct connf_s	*ips_rts_clients;
202 	struct connf_s	*ips_ipcl_conn_fanout;
203 	struct connf_s	*ips_ipcl_bind_fanout;
204 	struct connf_s	*ips_ipcl_proto_fanout;
205 	struct connf_s	*ips_ipcl_proto_fanout_v6;
206 	struct connf_s	*ips_ipcl_udp_fanout;
207 	struct connf_s	*ips_ipcl_raw_fanout;
208 	uint_t		ips_ipcl_conn_fanout_size;
209 	uint_t		ips_ipcl_bind_fanout_size;
210 	uint_t		ips_ipcl_udp_fanout_size;
211 	uint_t		ips_ipcl_raw_fanout_size;
212 	struct connf_s	*ips_ipcl_globalhash_fanout;
213 	int		ips_conn_g_index;
214 
215 /* ip.c */
216 	/* Following protected by ips_igmp_timer_lock */
217 	int 		ips_igmp_time_to_next;	/* Time since last timeout */
218 	int 		ips_igmp_timer_fired_last;
219 	int		ips_igmp_deferred_next;
220 	timeout_id_t	ips_igmp_timeout_id;
221 	/* Protected by igmp_timer_lock */
222 	boolean_t	ips_igmp_timer_setter_active;
223 
224 	/* Following protected by mld_timer_lock */
225 	int 		ips_mld_time_to_next;	/* Time since last timeout */
226 	int 		ips_mld_timer_fired_last;
227 	int		ips_mld_deferred_next;
228 	timeout_id_t	ips_mld_timeout_id;
229 	/* Protected by mld_timer_lock */
230 	boolean_t	ips_mld_timer_setter_active;
231 
232 	/* Protected by igmp_slowtimeout_lock */
233 	timeout_id_t	ips_igmp_slowtimeout_id;
234 	kmutex_t	ips_igmp_slowtimeout_lock;
235 
236 	/* Protected by mld_slowtimeout_lock */
237 	timeout_id_t	ips_mld_slowtimeout_id;
238 	kmutex_t	ips_mld_slowtimeout_lock;
239 
240 	/* IPv4 forwarding table */
241 	struct radix_node_head *ips_ip_ftable;
242 
243 	/* This is dynamically allocated in ip_ire_init */
244 	struct irb	 *ips_ip_cache_table;
245 	/* This is dynamically allocated in ire_add_mrtun */
246 	struct irb	*ips_ip_mrtun_table;
247 
248 #define	IPV6_ABITS		128
249 #define	IP6_MASK_TABLE_SIZE	(IPV6_ABITS + 1)	/* 129 ptrs */
250 
251 	struct irb	*ips_ip_forwarding_table_v6[IP6_MASK_TABLE_SIZE];
252 	/* This is dynamically allocated in ip_ire_init */
253 	struct irb	*ips_ip_cache_table_v6;
254 
255 	uint32_t	ips_ire_handle;
256 	/*
257 	 * ire_ft_init_lock is used while initializing ip_forwarding_table
258 	 * dynamically in ire_add.
259 	 */
260 	kmutex_t	ips_ire_ft_init_lock;
261 	kmutex_t	ips_ire_mrtun_lock; /* Protects mrtun table and count */
262 	kmutex_t	ips_ire_srcif_table_lock; /* Same as above */
263 	/*
264 	 * The following counts are used to determine whether a walk is
265 	 * needed through the reverse tunnel table or through ills
266 	 */
267 	kmutex_t	ips_ire_handle_lock;	/* Protects ire_handle */
268 
269 	/* # of ires in reverse tun table */
270 	uint_t		ips_ire_mrtun_count;
271 
272 	/* # of ires in all srcif tables */
273 	uint_t		ips_ire_srcif_table_count;
274 
275 	uint32_t	ips_ip_cache_table_size;
276 	uint32_t	ips_ip6_cache_table_size;
277 	uint32_t	ips_ip6_ftable_hash_size;
278 
279 	ire_stats_t 	ips_ire_stats_v4;	/* IPv4 ire statistics */
280 	ire_stats_t 	ips_ire_stats_v6;	/* IPv6 ire statistics */
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 	int		ips_ip_g_forward;
298 	int		ips_ipv6_forward;
299 
300 	int		ips_ipmp_hook_emulation; /* ndd variable */
301 
302 	time_t		ips_ip_g_frag_timeout;
303 	clock_t		ips_ip_g_frag_timo_ms;
304 
305 	queue_t		*ips_ip_g_mrouter;
306 
307 	/* Time since last icmp_pkt_err */
308 	clock_t		ips_icmp_pkt_err_last;
309 	/* Number of packets sent in burst */
310 	uint_t		ips_icmp_pkt_err_sent;
311 	/* Used by icmp_send_redirect_v6 for picking random src. */
312 	uint_t		ips_icmp_redirect_v6_src_index;
313 
314 	/* Protected by ip_mi_lock */
315 	void		*ips_ip_g_head;		/* Instance Data List Head */
316 
317 	caddr_t		ips_ip_g_nd;		/* Named Dispatch List Head */
318 
319 	/* Multirouting stuff */
320 	/* Interval (in ms) between consecutive 'bad MTU' warnings */
321 	hrtime_t	ips_ip_multirt_log_interval;
322 	/* Time since last warning issued. */
323 	hrtime_t	ips_multirt_bad_mtu_last_time;
324 
325 	kmutex_t	ips_ip_trash_timer_lock;
326 	timeout_id_t	ips_ip_ire_expire_id;	/* IRE expiration timer. */
327 	struct ipsq_s	*ips_ipsq_g_head;
328 	uint_t		ips_ill_index;	/* Used to assign interface indicies */
329 	/* When set search for unused index */
330 	boolean_t	ips_ill_index_wrap;
331 
332 	clock_t		ips_ip_ire_arp_time_elapsed;
333 			/* Time since IRE cache last flushed */
334 	clock_t		ips_ip_ire_rd_time_elapsed;
335 			/* ... redirect IREs last flushed */
336 	clock_t		ips_ip_ire_pmtu_time_elapsed;
337 			/* Time since path mtu increase */
338 
339 	uint_t		ips_ip_redirect_cnt;
340 			/* Num of redirect routes in ftable */
341 	uint_t		ips_ipv6_ire_default_count;
342 			/* Number of IPv6 IRE_DEFAULT entries */
343 	uint_t		ips_ipv6_ire_default_index;
344 			/* Walking IPv6 index used to mod in */
345 
346 	uint_t		ips_loopback_packets;
347 
348 	/* NDP/NCE structures for IPv4 and IPv6 */
349 	struct ndp_g_s	*ips_ndp4;
350 	struct ndp_g_s	*ips_ndp6;
351 
352 	/* ip_mroute stuff */
353 	kmutex_t	ips_ip_g_mrouter_mutex;
354 
355 	struct mrtstat	*ips_mrtstat;	/* Stats for netstat */
356 	int		ips_saved_ip_g_forward;
357 
358 	/* numvifs is only a hint about the max interface being used. */
359 	ushort_t	ips_numvifs;
360 	kmutex_t	ips_numvifs_mutex;
361 
362 	struct vif	*ips_vifs;
363 	struct mfcb	*ips_mfcs;	/* kernel routing table	*/
364 	struct tbf	*ips_tbfs;
365 	/*
366 	 * One-back cache used to locate a tunnel's vif,
367 	 * given a datagram's src ip address.
368 	 */
369 	ipaddr_t	ips_last_encap_src;
370 	struct vif	*ips_last_encap_vif;
371 	kmutex_t	ips_last_encap_lock;	/* Protects the above */
372 
373 	/*
374 	 * reg_vif_num is protected by numvifs_mutex
375 	 */
376 	/* Whether or not special PIM assert processing is enabled. */
377 	ushort_t	ips_reg_vif_num; 	/* Index to Register vif */
378 	int		ips_pim_assert;
379 
380 	union ill_g_head_u *ips_ill_g_heads;   /* ILL List Head */
381 
382 	kstat_t		*ips_loopback_ksp;
383 
384 	uint_t		ips_ipif_src_random;
385 
386 	struct idl_s	*ips_conn_drain_list;	/* Array of conn drain lists */
387 	uint_t		ips_conn_drain_list_cnt; /* Count of conn_drain_list */
388 	int		ips_conn_drain_list_index; /* Next drain_list */
389 
390 	/*
391 	 * ID used to assign next free one.
392 	 * Increases by one. Once it wraps we search for an unused ID.
393 	 */
394 	uint_t		ips_ip_src_id;
395 	boolean_t	ips_srcid_wrapped;
396 
397 	struct srcid_map *ips_srcid_head;
398 	krwlock_t	ips_srcid_lock;
399 
400 	uint64_t	ips_ipif_g_seqid;
401 	union phyint_list_u *ips_phyint_g_list;	/* start of phyint list */
402 
403 	/*
404 	 * Reflects value of FAILBACK variable in IPMP config file
405 	 * /etc/default/mpathd. Default value is B_TRUE.
406 	 * Set to B_FALSE if user disabled failback by configuring
407 	 * "FAILBACK=no" in.mpathd uses SIOCSIPMPFAILBACK ioctl to pass this
408 	 * information to kernel.
409 	 */
410 	boolean_t ips_ipmp_enable_failback;
411 
412 /* ip_neti.c */
413 	hook_family_t	ips_ipv4root;
414 	hook_family_t	ips_ipv6root;
415 
416 	/*
417 	 * Hooks for firewalling
418 	 */
419 	hook_event_t		ips_ip4_physical_in_event;
420 	hook_event_t		ips_ip4_physical_out_event;
421 	hook_event_t		ips_ip4_forwarding_event;
422 	hook_event_t		ips_ip4_loopback_in_event;
423 	hook_event_t		ips_ip4_loopback_out_event;
424 	hook_event_t		ips_ip4_nic_events;
425 	hook_event_t		ips_ip6_physical_in_event;
426 	hook_event_t		ips_ip6_physical_out_event;
427 	hook_event_t		ips_ip6_forwarding_event;
428 	hook_event_t		ips_ip6_loopback_in_event;
429 	hook_event_t		ips_ip6_loopback_out_event;
430 	hook_event_t		ips_ip6_nic_events;
431 
432 	hook_event_token_t	ips_ipv4firewall_physical_in;
433 	hook_event_token_t	ips_ipv4firewall_physical_out;
434 	hook_event_token_t	ips_ipv4firewall_forwarding;
435 	hook_event_token_t	ips_ipv4firewall_loopback_in;
436 	hook_event_token_t	ips_ipv4firewall_loopback_out;
437 	hook_event_token_t	ips_ipv4nicevents;
438 	hook_event_token_t	ips_ipv6firewall_physical_in;
439 	hook_event_token_t	ips_ipv6firewall_physical_out;
440 	hook_event_token_t	ips_ipv6firewall_forwarding;
441 	hook_event_token_t	ips_ipv6firewall_loopback_in;
442 	hook_event_token_t	ips_ipv6firewall_loopback_out;
443 	hook_event_token_t	ips_ipv6nicevents;
444 
445 	net_data_t		ips_ipv4_net_data;
446 	net_data_t		ips_ipv6_net_data;
447 };
448 typedef struct ip_stack ip_stack_t;
449 
450 /* Finding an ip_stack_t */
451 #define	CONNQ_TO_IPST(_q)	(Q_TO_CONN(_q)->conn_netstack->netstack_ip)
452 #define	ILLQ_TO_IPST(_q)	(((ill_t *)(_q)->q_ptr)->ill_ipst)
453 
454 #else /* _KERNEL */
455 typedef int ip_stack_t;
456 #endif /* _KERNEL */
457 
458 #ifdef	__cplusplus
459 }
460 #endif
461 
462 #endif	/* _INET_IP_STACK_H */
463