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