xref: /titanic_50/usr/src/uts/common/inet/ip_ndp.h (revision 8b464eb836173b92f2b7a65623cd06c8c3c59289)
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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #ifndef	_INET_IP_NDP_H
27 #define	_INET_IP_NDP_H
28 
29 #pragma ident	"%Z%%M%	%I%	%E% SMI"
30 
31 #include <sys/mutex.h>
32 #include <sys/stream.h>
33 #include <netinet/in.h>
34 #include <netinet/icmp6.h>
35 #include <inet/ip.h>
36 
37 /*
38  * Internal definitions for the kernel implementation of the IPv6
39  * Neighbor Discovery Protocol (NDP).
40  */
41 
42 #ifdef	__cplusplus
43 extern "C" {
44 #endif
45 
46 #ifdef _KERNEL
47 #define	NCE_TABLE_SIZE	256
48 /* NDP Cache Entry */
49 typedef struct nce_s {
50 	struct	nce_s	*nce_next;	/* Hash chain next pointer */
51 	struct	nce_s	**nce_ptpn;	/* Pointer to previous next */
52 	struct 	ill_s	*nce_ill;	/* Associated ill */
53 	uint16_t	nce_flags;	/* See below */
54 	uint16_t	nce_state;	/* See reachability states in if.h */
55 	int16_t		nce_pcnt;	/* Probe counter */
56 	uint16_t	nce_rcnt;	/* Retransmit counter */
57 	in6_addr_t	nce_addr;	/* address of the nighbor */
58 	in6_addr_t	nce_mask;	/* If not all ones, mask allows an */
59 	    /* entry  to respond to requests for a group of addresses, for */
60 	    /* instantance multicast addresses				   */
61 	in6_addr_t	nce_extract_mask; /* For mappings */
62 	uint32_t	nce_ll_extract_start;	/* For mappings */
63 #define	nce_first_mp_to_free	nce_fp_mp
64 	mblk_t		*nce_fp_mp;	/* link layer fast path mp */
65 	mblk_t		*nce_res_mp;	/* DL_UNITDATA_REQ or link layer mp */
66 	mblk_t		*nce_qd_mp;	/* Head outgoing queued packets */
67 #define	nce_last_mp_to_free	nce_qd_mp
68 	mblk_t		*nce_timer_mp;	/* NDP timer mblk */
69 	mblk_t		*nce_mp;	/* mblk we are in, last to be freed */
70 	uint64_t	nce_last;	/* Time last reachable in msec */
71 	uint32_t	nce_refcnt;	/* nce active usage count */
72 	kmutex_t	nce_lock;	/* See comments on top for what */
73 					/* this field protects */
74 	int		nce_unsolicit_count; /* Unsolicited Adv count */
75 	struct nce_s	*nce_fastpath;	/* for fastpath list */
76 	timeout_id_t	nce_timeout_id;
77 	uchar_t		nce_ipversion;	/* IPv4(ARP)/IPv6(NDP) version */
78 	uint_t		nce_defense_count;	/* number of NDP conflicts */
79 	uint_t		nce_defense_time;	/* last time defended (secs) */
80 #ifdef NCE_DEBUG
81 	th_trace_t	*nce_trace[IP_TR_HASH_MAX];
82 	boolean_t	nce_trace_disable;	/* True when alloc fails */
83 #endif
84 } nce_t;
85 
86 /*
87  * The ndp_g_t structure contains protocol specific information needed
88  * to synchronize and manage neighbor cache entries for IPv4 and IPv6.
89  * There are 2 such structures, ndp4 and ndp6.
90  * ndp6 contains the data structures needed for IPv6 Neighbor Discovery.
91  * ndp4 has IPv4 link layer info in its nce_t structures
92  * Note that the nce_t is not currently used as the arp cache itself;
93  * it is used for the following purposes:
94  *   - queue packets in nce_qd_mp while waiting for arp resolution to complete
95  *   - nce_{res, fp}_mp are used to track DL_UNITDATA request/responses.
96  *   - track state of ARP resolution in the nce_state;
97  *
98  * Locking notes:
99  * ndp_g_lock protects neighbor cache tables access and
100  * insertion/removal of cache entries into/from these tables.
101  * nce_lock protects nce_pcnt, nce_rcnt, nce_qd_mp nce_state,
102  * nce_res_mp, nce_refcnt and nce_last.
103  * nce_refcnt is incremented for every ire pointing to this nce and
104  * every time ndp_lookup() finds an nce.
105  * Should there be a need to obtain nce_lock and ndp_g_lock, ndp_g_lock is
106  * acquired first.
107  * To avoid becoming exclusive when deleting NCEs, ndp_walk() routine holds
108  * the ndp_g_lock (i.e global lock) and marks NCEs to be deleted with
109  * NCE_F_CONDEMNED.  When all active users of such NCEs are gone the walk
110  * routine passes a list for deletion to nce_ire_delete_list().
111  */
112 typedef	struct ndp_g_s {
113 	kmutex_t	ndp_g_lock;	/* Lock protecting  cache hash table */
114 	nce_t		*nce_mask_entries;	/* mask not all ones */
115 	nce_t		*nce_hash_tbl[NCE_TABLE_SIZE];
116 	int		ndp_g_walker; /* # of active thread walking hash list */
117 	boolean_t	ndp_g_walker_cleanup; /* true implies defer deletion. */
118 } ndp_g_t;
119 
120 extern ndp_g_t	ndp4, ndp6;
121 
122 /* nce_flags  */
123 #define	NCE_F_PERMANENT		0x1
124 #define	NCE_F_MAPPING		0x2
125 #define	NCE_F_ISROUTER		0x4
126 #define	NCE_F_PROXY		0x8
127 #define	NCE_F_NONUD		0x10
128 #define	NCE_F_ANYCAST		0x20
129 #define	NCE_F_CONDEMNED		0x40
130 #define	NCE_F_UNSOL_ADV		0x80
131 
132 #define	NCE_EXTERNAL_FLAGS_MASK \
133 	(NCE_F_PERMANENT | NCE_F_MAPPING | NCE_F_ISROUTER | NCE_F_NONUD | \
134 	NCE_F_ANYCAST | NCE_F_UNSOL_ADV)
135 
136 /* State REACHABLE, STALE, DELAY or PROBE */
137 #define	NCE_ISREACHABLE(nce)			\
138 	(((((nce)->nce_state) >= ND_REACHABLE) &&	\
139 	((nce)->nce_state) <= ND_PROBE))
140 
141 /* NDP flags set in SOL/ADV requests */
142 #define	NDP_UNICAST		0x1
143 #define	NDP_ISROUTER		0x2
144 #define	NDP_SOLICITED		0x4
145 #define	NDP_ORIDE		0x8
146 #define	NDP_PROBE		0x10
147 
148 /* Number of packets queued in NDP for a neighbor */
149 #define	ND_MAX_Q		4
150 
151 
152 #ifdef NCE_DEBUG
153 #define	NCE_TRACE_REF(nce)		nce_trace_ref(nce)
154 #define	NCE_UNTRACE_REF(nce)		nce_untrace_ref(nce)
155 #else
156 #define	NCE_TRACE_REF(nce)
157 #define	NCE_UNTRACE_REF(nce)
158 #endif
159 
160 #define	NCE_REFHOLD(nce) {		\
161 	mutex_enter(&(nce)->nce_lock);	\
162 	(nce)->nce_refcnt++;		\
163 	ASSERT((nce)->nce_refcnt != 0);	\
164 	NCE_TRACE_REF(nce);		\
165 	mutex_exit(&(nce)->nce_lock);	\
166 }
167 
168 #define	NCE_REFHOLD_NOTR(nce) {		\
169 	mutex_enter(&(nce)->nce_lock);	\
170 	(nce)->nce_refcnt++;		\
171 	ASSERT((nce)->nce_refcnt != 0);	\
172 	mutex_exit(&(nce)->nce_lock);	\
173 }
174 
175 #define	NCE_REFHOLD_LOCKED(nce) {		\
176 	ASSERT(MUTEX_HELD(&(nce)->nce_lock));	\
177 	(nce)->nce_refcnt++;			\
178 	NCE_TRACE_REF(nce);			\
179 }
180 
181 /* nce_inactive destroys the mutex thus no mutex_exit is needed */
182 #define	NCE_REFRELE(nce) {		\
183 	mutex_enter(&(nce)->nce_lock);	\
184 	NCE_UNTRACE_REF(nce);		\
185 	ASSERT((nce)->nce_refcnt != 0);	\
186 	if (--(nce)->nce_refcnt == 0)	\
187 		ndp_inactive(nce);	\
188 	else {				\
189 		mutex_exit(&(nce)->nce_lock);\
190 	}				\
191 }
192 
193 #define	NCE_REFRELE_NOTR(nce) {		\
194 	mutex_enter(&(nce)->nce_lock);	\
195 	ASSERT((nce)->nce_refcnt != 0);	\
196 	if (--(nce)->nce_refcnt == 0)	\
197 		ndp_inactive(nce);	\
198 	else {				\
199 		mutex_exit(&(nce)->nce_lock);\
200 	}				\
201 }
202 
203 #define	NDP_RESTART_TIMER(nce, ms) {	\
204 	ASSERT(!MUTEX_HELD(&(nce)->nce_lock));				\
205 	if ((nce)->nce_timeout_id != 0) {				\
206 		/* Ok to untimeout bad id. we don't hold a lock. */	\
207 		(void) untimeout((nce)->nce_timeout_id);		\
208 	}								\
209 	mutex_enter(&(nce)->nce_lock);					\
210 	/* Don't start the timer if the nce has been deleted */		\
211 	if (!((nce)->nce_flags & NCE_F_CONDEMNED)) 			\
212 		nce->nce_timeout_id = timeout(ndp_timer, nce, 		\
213 		    MSEC_TO_TICK(ms) == 0 ? 1 : MSEC_TO_TICK(ms));	\
214 	mutex_exit(&(nce)->nce_lock);					\
215 }
216 
217 /* Structure for ndp_cache_count() */
218 typedef struct {
219 	int	ncc_total;	/* Total number of NCEs */
220 	int	ncc_host;	/* NCE entries without R bit set */
221 } ncc_cache_count_t;
222 
223 /*
224  * Structure of ndp_cache_reclaim().  Each field is a fraction i.e. 1 means
225  * reclaim all, N means reclaim 1/Nth of all entries, 0 means reclaim none.
226  */
227 typedef struct {
228 	int	ncr_host;	/* Fraction for host entries */
229 } nce_cache_reclaim_t;
230 
231 /*
232  * Structure for nce_delete_hw_changed; specifies an IPv4 address to link-layer
233  * address mapping.  Any route that has a cached copy of a mapping for that
234  * IPv4 address that doesn't match the given mapping must be purged.
235  */
236 typedef struct {
237 	ipaddr_t hwm_addr;	/* IPv4 address */
238 	uint_t hwm_hwlen;	/* Length of hardware address (may be 0) */
239 	uchar_t *hwm_hwaddr;	/* Pointer to new hardware address, if any */
240 } nce_hw_map_t;
241 
242 /* When SAP is greater than zero address appears before SAP */
243 #define	NCE_LL_ADDR_OFFSET(ill)	(((ill)->ill_sap_length) < 0 ? \
244 	(sizeof (dl_unitdata_req_t)) : \
245 	((sizeof (dl_unitdata_req_t)) + (ABS((ill)->ill_sap_length))))
246 
247 #define	NCE_LL_SAP_OFFSET(ill) (((ill)->ill_sap_length) < 0 ? \
248 	((sizeof (dl_unitdata_req_t)) + ((ill)->ill_phys_addr_length)) : \
249 	(sizeof (dl_unitdata_req_t)))
250 
251 #ifdef _BIG_ENDIAN
252 #define	NCE_LL_SAP_COPY(ill, mp) \
253 	{ \
254 	size_t abs_sap_len = ABS((ill)->ill_sap_length); \
255 	if (abs_sap_len > 0) { \
256 		ASSERT(abs_sap_len <= sizeof (uint32_t)); \
257 		ASSERT((mp)->b_rptr + NCE_LL_SAP_OFFSET(ill) + \
258 		    abs_sap_len <= ((mp)->b_wptr)); \
259 		bcopy((uint8_t *)&(ill)->ill_sap + sizeof (ill->ill_sap) - \
260 		    abs_sap_len, \
261 		    ((mp)->b_rptr + NCE_LL_SAP_OFFSET(ill)), \
262 		    abs_sap_len); \
263 	} \
264 	}
265 #else
266 #define	NCE_LL_SAP_COPY(ill, mp) \
267 	{ \
268 	size_t abs_sap_len = ABS((ill)->ill_sap_length); \
269 	if (abs_sap_len > 0) { \
270 		uint32_t abs_sap_len = ABS((ill)->ill_sap_length); \
271 		ASSERT(abs_sap_len <= sizeof (uint32_t)); \
272 		ASSERT((mp)->b_rptr + NCE_LL_SAP_OFFSET(ill) + \
273 		    abs_sap_len <= ((mp)->b_wptr)); \
274 		bcopy(&((ill)->ill_sap), \
275 		((mp)->b_rptr + NCE_LL_SAP_OFFSET(ill)), \
276 		abs_sap_len); \
277 	} \
278 	}
279 #endif
280 
281 /*
282  * Exclusive-or the 6 bytes that are likely to contain the MAC
283  * address. Assumes table_size does not exceed 256.
284  * Assumes EUI-64 format for good hashing.
285  */
286 #define	NCE_ADDR_HASH_V6(addr, table_size)				\
287 	(((addr).s6_addr8[8] ^ (addr).s6_addr8[9] ^			\
288 	(addr).s6_addr8[10] ^ (addr).s6_addr8[13] ^			\
289 	(addr).s6_addr8[14] ^ (addr).s6_addr8[15]) % (table_size))
290 
291 extern	void	ndp_cache_count(nce_t *, char *);
292 extern	void	ndp_cache_reclaim(nce_t *, char *);
293 extern	void	ndp_delete(nce_t *);
294 extern	void	ndp_delete_per_ill(nce_t *, uchar_t *);
295 extern	void	ndp_fastpath_flush(nce_t *, char  *);
296 extern	boolean_t ndp_fastpath_update(nce_t *, void  *);
297 extern	nd_opt_hdr_t *ndp_get_option(nd_opt_hdr_t *, int, int);
298 extern	void	ndp_inactive(nce_t *);
299 extern	void	ndp_input(ill_t *, mblk_t *, mblk_t *);
300 extern	boolean_t ndp_lookup_ipaddr(in_addr_t);
301 extern	nce_t	*ndp_lookup_v6(ill_t *, const in6_addr_t *, boolean_t);
302 extern	nce_t	*ndp_lookup_v4(ill_t *, const in_addr_t *, boolean_t);
303 extern	int	ndp_lookup_then_add(ill_t *, uchar_t *, const void *,
304     const void *, const void *, uint32_t, uint16_t,
305     uint16_t, nce_t **, mblk_t *, mblk_t *);
306 extern	int	ndp_mcastreq(ill_t *, const in6_addr_t *, uint32_t, uint32_t,
307     mblk_t *);
308 extern	int	ndp_noresolver(ill_t *, const in6_addr_t *);
309 extern	void	ndp_process(nce_t *, uchar_t *, uint32_t, boolean_t);
310 extern	int	ndp_query(ill_t *, lif_nd_req_t *);
311 extern	int	ndp_report(queue_t *, mblk_t *, caddr_t, cred_t *);
312 extern	int	ndp_resolver(ill_t *, const in6_addr_t *, mblk_t *, zoneid_t);
313 extern	int	ndp_sioc_update(ill_t *, lif_nd_req_t *);
314 extern	boolean_t	ndp_verify_optlen(nd_opt_hdr_t *, int);
315 extern	void	ndp_timer(void *);
316 extern	void	ndp_walk(ill_t *, pfi_t, void *);
317 extern	void	ndp_walk_common(ndp_g_t *, ill_t *, pfi_t,
318     void *, boolean_t);
319 extern	int	ndp_add(ill_t *, uchar_t *, const void *,
320 		    const void *, const void *,
321 		    uint32_t, uint16_t, uint16_t, nce_t **, mblk_t *, mblk_t *);
322 extern	boolean_t	ndp_restart_dad(nce_t *);
323 extern	void	ndp_do_recovery(ipif_t *);
324 extern	void	nce_resolv_failed(nce_t *);
325 extern	void	arp_resolv_failed(nce_t *);
326 extern	void	nce_fastpath_list_add(nce_t *);
327 extern	void	nce_fastpath_list_delete(nce_t *);
328 extern	void	nce_fastpath_list_dispatch(ill_t *,
329     boolean_t (*)(nce_t *, void  *), void *);
330 extern	void	nce_queue_mp_common(nce_t *, mblk_t *, boolean_t);
331 extern	void	ndp_flush_qd_mp(nce_t *);
332 extern	nce_t	*nce_reinit(nce_t *);
333 extern	void	nce_delete_hw_changed(nce_t *, void *);
334 
335 #ifdef NCE_DEBUG
336 extern	void	nce_trace_inactive(nce_t *);
337 extern	void	nce_trace_ref(nce_t *);
338 extern	void	nce_untrace_ref(nce_t *);
339 extern	int	nce_thread_exit(nce_t *, caddr_t);
340 #endif
341 
342 #endif	/* _KERNEL */
343 
344 #ifdef	__cplusplus
345 }
346 #endif
347 
348 #endif	/* _INET_IP_NDP_H */
349