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