xref: /titanic_41/usr/src/uts/common/inet/ip_ire.h (revision 24a1f0af9f770e0e795ef1fa1c6dece8dd8dc959)
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  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /* Copyright (c) 1990 Mentat Inc. */
26 
27 #ifndef	_INET_IP_IRE_H
28 #define	_INET_IP_IRE_H
29 
30 #ifdef	__cplusplus
31 extern "C" {
32 #endif
33 
34 #define	IPV6_LL_PREFIXLEN	10	/* Number of bits in link-local pref */
35 
36 #define	IP_CACHE_TABLE_SIZE	256
37 #define	IP_MASK_TABLE_SIZE	(IP_ABITS + 1)		/* 33 ptrs */
38 
39 #define	IP6_FTABLE_HASH_SIZE	32	/* size of each hash table in ptrs */
40 #define	IP6_CACHE_TABLE_SIZE	256
41 #define	IP6_MASK_TABLE_SIZE	(IPV6_ABITS + 1)	/* 129 ptrs */
42 
43 /*
44  * We use the common modulo hash function.  In ip_ire_init(), we make
45  * sure that the cache table size is always a power of 2.  That's why
46  * we can use & instead of %.  Also note that we try hard to make sure
47  * the lower bits of an address capture most info from the whole address.
48  * The reason being that since our hash table is probably a lot smaller
49  * than 2^32 buckets so the lower bits are the most important.
50  */
51 #define	IRE_ADDR_HASH(addr, table_size) \
52 	(((addr) ^ ((addr) >> 8) ^ ((addr) >> 16) ^ ((addr) >> 24)) &	\
53 	((table_size) - 1))
54 
55 /*
56  * To make a byte-order neutral hash for IPv6, just take all the
57  * bytes in the bottom 32 bits into account.
58  */
59 #define	IRE_ADDR_HASH_V6(addr, table_size) 				\
60 	IRE_ADDR_HASH((addr).s6_addr32[3], table_size)
61 
62 /* This assumes that the ftable size is a power of 2. */
63 #define	IRE_ADDR_MASK_HASH_V6(addr, mask, table_size) 			\
64 	((((addr).s6_addr8[8] & (mask).s6_addr8[8]) ^ 			\
65 	((addr).s6_addr8[9] & (mask).s6_addr8[9]) ^			\
66 	((addr).s6_addr8[10] & (mask).s6_addr8[10]) ^ 			\
67 	((addr).s6_addr8[13] & (mask).s6_addr8[13]) ^ 			\
68 	((addr).s6_addr8[14] & (mask).s6_addr8[14]) ^ 			\
69 	((addr).s6_addr8[15] & (mask).s6_addr8[15])) & ((table_size) - 1))
70 
71 /*
72  * match parameter definitions for IRE lookup routines.
73  */
74 #define	MATCH_IRE_DSTONLY	0x0000	/* Match just the address */
75 #define	MATCH_IRE_TYPE		0x0001	/* Match IRE type */
76 #define	MATCH_IRE_SRC		0x0002	/* Match IRE source address */
77 #define	MATCH_IRE_MASK		0x0004	/* Match IRE mask */
78 #define	MATCH_IRE_WQ		0x0008	/* Match IRE ire_stq to write queue */
79 #define	MATCH_IRE_GW		0x0010	/* Match IRE gateway */
80 #define	MATCH_IRE_IPIF		0x0020	/* Match IRE ipif */
81 #define	MATCH_IRE_RECURSIVE	0x0040	/* Do recursive lookup if necessary */
82 #define	MATCH_IRE_DEFAULT	0x0080	/* Return default route if no route */
83 					/* found. */
84 #define	MATCH_IRE_RJ_BHOLE	0x0100	/* During lookup if we hit an ire */
85 					/* with RTF_REJECT or RTF_BLACKHOLE, */
86 					/* return the ire. No recursive */
87 					/* lookup should be done. */
88 #define	MATCH_IRE_IHANDLE	0x0200	/* Match IRE on ihandle */
89 #define	MATCH_IRE_MARK_HIDDEN	0x0400	/* Match IRE ire_marks with */
90 					/* IRE_MARK_HIDDEN. */
91 /*
92  * MATCH_IRE_ILL is used whenever we want to specifically match an IRE
93  * whose ire_ipif->ipif_ill or (ill_t *)ire_stq->q_ptr matches a given
94  * ill. When MATCH_IRE_ILL is used to locate an IRE_CACHE, it implies
95  * that the packet will not be load balanced. This is normally used
96  * by in.mpathd to send out failure detection probes.
97  *
98  * MATCH_IRE_ILL_GROUP is used whenever we are not specific about which
99  * interface (ill) the packet should be sent out. This implies that the
100  * packets will be subjected to load balancing and it might go out on
101  * any interface in the group. When there is only interface in the group,
102  * MATCH_IRE_ILL_GROUP becomes MATCH_IRE_ILL. Most of the code uses
103  * MATCH_IRE_ILL_GROUP and MATCH_IRE_ILL is used in very few cases where
104  * we want to disable load balancing.
105  *
106  * MATCH_IRE_PARENT is used whenever we unconditionally want to get the
107  * parent IRE (sire) while recursively searching IREs for an offsubnet
108  * destination. With this flag, even if no IRE_CACHETABLE or IRE_INTERFACE
109  * is found to help resolving IRE_OFFSUBNET in lookup routines, the
110  * IRE_OFFSUBNET sire, if any, is returned to the caller.
111  */
112 #define	MATCH_IRE_ILL_GROUP	0x0800	/* Match IRE on ill or the ill_group. */
113 #define	MATCH_IRE_ILL		0x1000	/* Match IRE on the ill only */
114 
115 #define	MATCH_IRE_PARENT	0x2000	/* Match parent ire, if any, */
116 					/* even if ire is not matched. */
117 #define	MATCH_IRE_ZONEONLY	0x4000	/* Match IREs in specified zone, ie */
118 					/* don't match IRE_LOCALs from other */
119 					/* zones or shared IREs */
120 #define	MATCH_IRE_MARK_PRIVATE_ADDR	0x8000	/* Match IRE ire_marks with */
121 						/* IRE_MARK_PRIVATE_ADDR. */
122 #define	MATCH_IRE_SECATTR	0x10000	/* Match gateway security attributes */
123 #define	MATCH_IRE_COMPLETE	0x20000	/* ire_ftable_lookup() can return */
124 					/* IRE_CACHE entry only if it is  */
125 					/* ND_REACHABLE			  */
126 
127 /*
128  * Any ire to nce association is long term, and
129  * the refhold and refrele may be done by different
130  * threads. So all cases of making or breaking ire to
131  * nce association should all effectively use the NOTR variants.
132  * To understand the *effectively* part read on.
133  *
134  * ndp_lookup() and ndp_add_v4()/ndp_add_v6() implicitly do
135  * NCE_REFHOLD. So wherever we make ire to nce association after
136  * calling these functions, we effectively want to end up with
137  * NCE_REFHOLD_NOTR. We call this macro to achieve this effect. This
138  * macro changes a NCE_REFHOLD to a NCE_REFHOLD_NOTR. The macro's
139  * NCE_REFRELE cancels off ndp_lookup[ndp_add]'s implicit NCE_REFHOLD,
140  * and what you are left with is a NCE_REFHOLD_NOTR
141  */
142 #define	NCE_REFHOLD_TO_REFHOLD_NOTR(nce) {	\
143 	NCE_REFHOLD_NOTR(nce);			\
144 	NCE_REFRELE(nce);			\
145 }
146 
147 /*
148  * find the next ire_t entry in the ire_next chain starting at ire
149  * that is not CONDEMNED.  ire is set to NULL if we reach the end of the list.
150  * Caller must hold the ire_bucket lock.
151  */
152 
153 #define	IRE_FIND_NEXT_ORIGIN(ire) {					\
154 	while ((ire) != NULL && ((ire)->ire_marks & IRE_MARK_CONDEMNED))\
155 		(ire) = (ire)->ire_next;				\
156 }
157 
158 
159 /* Structure for ire_cache_count() */
160 typedef struct {
161 	int	icc_total;	/* Total number of IRE_CACHE */
162 	int	icc_unused;	/* # off/no PMTU unused since last reclaim */
163 	int	icc_offlink;	/* # offlink without PMTU information */
164 	int	icc_pmtu;	/* # offlink with PMTU information */
165 	int	icc_onlink;	/* # onlink */
166 } ire_cache_count_t;
167 
168 /*
169  * Structure for ire_cache_reclaim(). Each field is a fraction i.e. 1 meaning
170  * reclaim all, N meaning reclaim 1/Nth of all entries, 0 meaning reclaim none.
171  *
172  * The comment below (and for other netstack_t references) refers
173  * to the fact that we only do netstack_hold in particular cases,
174  * such as the references from open streams (ill_t and conn_t's
175  * pointers). Internally within IP we rely on IP's ability to cleanup e.g.
176  * ire_t's when an ill goes away.
177  */
178 typedef struct {
179 	int	icr_unused;	/* Fraction for unused since last reclaim */
180 	int	icr_offlink;	/* Fraction for offlink without PMTU info */
181 	int	icr_pmtu;	/* Fraction for offlink with PMTU info */
182 	int	icr_onlink;	/* Fraction for onlink */
183 	ip_stack_t *icr_ipst;	/* Does not have a netstack_hold */
184 } ire_cache_reclaim_t;
185 
186 /*
187  * We use atomics so that we get an accurate accounting on the ires.
188  * Otherwise we can't determine leaks correctly.
189  */
190 #define	BUMP_IRE_STATS(ire_stats, x) atomic_add_64(&(ire_stats).x, 1)
191 
192 #ifdef _KERNEL
193 /*
194  * Structure for passing args for the IRE cache lookup functions.
195  */
196 typedef struct ire_ctable_args_s {
197 	void			*ict_addr;
198 	void			*ict_gateway;
199 	int			ict_type;
200 	const ipif_t		*ict_ipif;
201 	zoneid_t		ict_zoneid;
202 	const ts_label_t	*ict_tsl;
203 	int			ict_flags;
204 	ip_stack_t		*ict_ipst;
205 	queue_t			*ict_wq;
206 } ire_ctable_args_t;
207 
208 struct ts_label_s;
209 struct nce_s;
210 
211 extern	ipaddr_t	ip_plen_to_mask(uint_t);
212 extern	in6_addr_t	*ip_plen_to_mask_v6(uint_t, in6_addr_t *);
213 
214 extern	int	ip_ire_advise(queue_t *, mblk_t *, cred_t *);
215 extern	int	ip_ire_delete(queue_t *, mblk_t *, cred_t *);
216 extern	boolean_t ip_ire_clookup_and_delete(ipaddr_t, ipif_t *, ip_stack_t *);
217 extern	void	ip_ire_clookup_and_delete_v6(const in6_addr_t *,
218     ip_stack_t *);
219 
220 extern	void	ip_ire_req(queue_t *, mblk_t *);
221 
222 extern	int	ip_mask_to_plen(ipaddr_t);
223 extern	int	ip_mask_to_plen_v6(const in6_addr_t *);
224 
225 extern	ire_t	*ipif_to_ire(const ipif_t *);
226 extern	ire_t	*ipif_to_ire_v6(const ipif_t *);
227 
228 extern	int	ire_add(ire_t **, queue_t *, mblk_t *, ipsq_func_t, boolean_t);
229 extern	void	ire_add_then_send(queue_t *, ire_t *, mblk_t *);
230 extern	int	ire_add_v6(ire_t **, queue_t *, mblk_t *, ipsq_func_t);
231 extern	int	ire_atomic_start(irb_t *irb_ptr, ire_t *ire, queue_t *q,
232     mblk_t *mp, ipsq_func_t func);
233 extern	void	ire_atomic_end(irb_t *irb_ptr, ire_t *ire);
234 
235 extern	void	ire_cache_count(ire_t *, char *);
236 extern	ire_t	*ire_cache_lookup(ipaddr_t, zoneid_t,
237     const struct ts_label_s *, ip_stack_t *);
238 extern	ire_t	*ire_cache_lookup_v6(const in6_addr_t *, zoneid_t,
239     const struct ts_label_s *, ip_stack_t *);
240 extern	void	ire_cache_reclaim(ire_t *, char *);
241 
242 extern	ire_t	*ire_create_mp(uchar_t *, uchar_t *, uchar_t *, uchar_t *,
243     uint_t, struct nce_s *, queue_t *, queue_t *, ushort_t, ipif_t *, ipaddr_t,
244     uint32_t, uint32_t, uint32_t, const iulp_t *, tsol_gc_t *, tsol_gcgrp_t *,
245     ip_stack_t *);
246 extern	ire_t	*ire_create(uchar_t *, uchar_t *, uchar_t *, uchar_t *,
247     uint_t *, struct nce_s *, queue_t *, queue_t *, ushort_t, ipif_t *,
248     ipaddr_t, uint32_t, uint32_t, uint32_t, const iulp_t *, tsol_gc_t *,
249     tsol_gcgrp_t *, ip_stack_t *);
250 
251 extern	ire_t	**ire_check_and_create_bcast(ipif_t *, ipaddr_t,
252     ire_t **, int);
253 extern	ire_t	**ire_create_bcast(ipif_t *, ipaddr_t, ire_t **);
254 extern	ire_t	*ire_init(ire_t *, uchar_t *, uchar_t *, uchar_t *, uchar_t *,
255     uint_t *, struct nce_s *, queue_t *, queue_t *, ushort_t, ipif_t *,
256     ipaddr_t, uint32_t, uint32_t, uint32_t, const iulp_t *, tsol_gc_t *,
257     tsol_gcgrp_t *, ip_stack_t *);
258 
259 extern	boolean_t ire_init_common(ire_t *, uint_t *, struct nce_s *, queue_t *,
260     queue_t *, ushort_t, ipif_t *, uint32_t, uint32_t, uint32_t, uchar_t,
261     const iulp_t *, tsol_gc_t *, tsol_gcgrp_t *, ip_stack_t *);
262 
263 extern	ire_t	*ire_create_v6(const in6_addr_t *, const in6_addr_t *,
264     const in6_addr_t *, const in6_addr_t *, uint_t *, struct nce_s *, queue_t *,
265     queue_t *, ushort_t, ipif_t *,
266     const in6_addr_t *, uint32_t, uint32_t, uint_t, const iulp_t *,
267     tsol_gc_t *, tsol_gcgrp_t *, ip_stack_t *);
268 
269 extern	ire_t	*ire_create_mp_v6(const in6_addr_t *, const in6_addr_t *,
270     const in6_addr_t *, const in6_addr_t *, struct nce_s *, queue_t *,
271     queue_t *, ushort_t, ipif_t *,
272     const in6_addr_t *, uint32_t, uint32_t, uint_t, const iulp_t *,
273     tsol_gc_t *, tsol_gcgrp_t *, ip_stack_t *);
274 
275 
276 extern	void	ire_clookup_delete_cache_gw(ipaddr_t, zoneid_t,
277     ip_stack_t *);
278 extern	void	ire_clookup_delete_cache_gw_v6(const in6_addr_t *, zoneid_t,
279     ip_stack_t *);
280 
281 extern	ire_t	*ire_ctable_lookup(ipaddr_t, ipaddr_t, int, const ipif_t *,
282     zoneid_t, const struct ts_label_s *, int, ip_stack_t *);
283 
284 extern	ire_t	*ire_ctable_lookup_v6(const in6_addr_t *, const in6_addr_t *,
285     int, const ipif_t *, zoneid_t, const struct ts_label_s *, int,
286     ip_stack_t *);
287 
288 extern	void	ire_delete(ire_t *);
289 extern	void	ire_delete_cache_gw(ire_t *, char *);
290 extern	void	ire_delete_cache_gw_v6(ire_t *, char *);
291 extern	void	ire_delete_cache_v6(ire_t *, char *);
292 extern	void	ire_delete_v6(ire_t *);
293 
294 extern	void	ire_expire(ire_t *, char *);
295 
296 extern	void	ire_flush_cache_v4(ire_t *, int);
297 extern	void	ire_flush_cache_v6(ire_t *, int);
298 
299 extern	ire_t	*ire_ftable_lookup_v6(const in6_addr_t *, const in6_addr_t *,
300     const in6_addr_t *, int, const ipif_t *, ire_t **, zoneid_t,
301     uint32_t, const struct ts_label_s *, int, ip_stack_t *);
302 
303 extern	ire_t	*ire_ihandle_lookup_onlink(ire_t *);
304 extern	ire_t	*ire_ihandle_lookup_offlink(ire_t *, ire_t *);
305 extern	ire_t	*ire_ihandle_lookup_offlink_v6(ire_t *, ire_t *);
306 
307 extern	boolean_t	ire_local_same_ill_group(ire_t *, ire_t *);
308 extern	boolean_t	ire_local_ok_across_zones(ire_t *, zoneid_t, void *,
309     const struct ts_label_s *, ip_stack_t *);
310 
311 extern	ire_t 	*ire_lookup_local(zoneid_t, ip_stack_t *);
312 extern	ire_t 	*ire_lookup_local_v6(zoneid_t, ip_stack_t *);
313 
314 extern  ire_t	*ire_lookup_multi(ipaddr_t, zoneid_t, ip_stack_t *);
315 extern  ire_t	*ire_lookup_multi_v6(const in6_addr_t *, zoneid_t,
316     ip_stack_t *);
317 
318 extern	void	ire_refrele(ire_t *);
319 extern	void	ire_refrele_notr(ire_t *);
320 extern	ire_t	*ire_route_lookup(ipaddr_t, ipaddr_t, ipaddr_t, int,
321     const ipif_t *, ire_t **, zoneid_t, const struct ts_label_s *, int,
322     ip_stack_t *);
323 
324 extern	ire_t	*ire_route_lookup_v6(const in6_addr_t *, const in6_addr_t *,
325     const in6_addr_t *, int, const ipif_t *, ire_t **, zoneid_t,
326     const struct ts_label_s *, int, ip_stack_t *);
327 
328 extern ill_t	*ire_to_ill(const ire_t *);
329 
330 extern	void	ire_walk(pfv_t, void *, ip_stack_t *);
331 extern	void	ire_walk_ill(uint_t, uint_t, pfv_t, void *, ill_t *);
332 extern	void	ire_walk_ill_v4(uint_t, uint_t, pfv_t, void *, ill_t *);
333 extern	void	ire_walk_ill_v6(uint_t, uint_t, pfv_t, void *, ill_t *);
334 extern	void	ire_walk_v4(pfv_t, void *, zoneid_t, ip_stack_t *);
335 extern  void	ire_walk_ill_tables(uint_t match_flags, uint_t ire_type,
336     pfv_t func, void *arg, size_t ftbl_sz, size_t htbl_sz,
337     irb_t **ipftbl, size_t ctbl_sz, irb_t *ipctbl, ill_t *ill,
338     zoneid_t zoneid, ip_stack_t *);
339 extern	void	ire_walk_v6(pfv_t, void *, zoneid_t, ip_stack_t *);
340 
341 extern boolean_t	ire_multirt_lookup(ire_t **, ire_t **, uint32_t,
342     const struct ts_label_s *, ip_stack_t *);
343 extern boolean_t	ire_multirt_need_resolve(ipaddr_t,
344     const struct ts_label_s *, ip_stack_t *);
345 extern boolean_t	ire_multirt_lookup_v6(ire_t **, ire_t **, uint32_t,
346     const struct ts_label_s *, ip_stack_t *);
347 extern boolean_t	ire_multirt_need_resolve_v6(const in6_addr_t *,
348     const struct ts_label_s *, ip_stack_t *);
349 
350 extern ire_t	*ipif_lookup_multi_ire(ipif_t *, ipaddr_t);
351 extern ire_t	*ipif_lookup_multi_ire_v6(ipif_t *, const in6_addr_t *);
352 
353 extern ire_t	*ire_get_next_bcast_ire(ire_t *, ire_t *);
354 extern ire_t	*ire_get_next_default_ire(ire_t *, ire_t *);
355 
356 extern  void	ire_arpresolve(ire_t *,  ill_t *);
357 extern  void	ire_freemblk(ire_t *);
358 extern boolean_t	ire_match_args(ire_t *, ipaddr_t, ipaddr_t, ipaddr_t,
359     int, const ipif_t *, zoneid_t, uint32_t, const struct ts_label_s *, int,
360     queue_t *);
361 extern  int	ire_nce_init(ire_t *, struct nce_s *);
362 extern  boolean_t	ire_walk_ill_match(uint_t, uint_t, ire_t *, ill_t *,
363     zoneid_t, ip_stack_t *);
364 extern	ire_t	*ire_arpresolve_lookup(ipaddr_t, ipaddr_t, ipif_t *, zoneid_t,
365     ip_stack_t *, queue_t *);
366 
367 #endif /* _KERNEL */
368 
369 #ifdef	__cplusplus
370 }
371 #endif
372 
373 #endif	/* _INET_IP_IRE_H */
374