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