xref: /titanic_44/usr/src/uts/common/inet/ip_ndp.h (revision 55434c770c89aa1b84474f2559a106803511aba0)
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 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 #define	NCE_F_BCAST		0x100
132 
133 #define	NCE_EXTERNAL_FLAGS_MASK \
134 	(NCE_F_PERMANENT | NCE_F_MAPPING | NCE_F_ISROUTER | NCE_F_NONUD | \
135 	NCE_F_ANYCAST | NCE_F_UNSOL_ADV)
136 
137 /* State REACHABLE, STALE, DELAY or PROBE */
138 #define	NCE_ISREACHABLE(nce)			\
139 	(((((nce)->nce_state) >= ND_REACHABLE) &&	\
140 	((nce)->nce_state) <= ND_PROBE))
141 
142 /* NDP flags set in SOL/ADV requests */
143 #define	NDP_UNICAST		0x1
144 #define	NDP_ISROUTER		0x2
145 #define	NDP_SOLICITED		0x4
146 #define	NDP_ORIDE		0x8
147 #define	NDP_PROBE		0x10
148 
149 /* Number of packets queued in NDP for a neighbor */
150 #define	ND_MAX_Q		4
151 
152 
153 #ifdef NCE_DEBUG
154 #define	NCE_TRACE_REF(nce)		nce_trace_ref(nce)
155 #define	NCE_UNTRACE_REF(nce)		nce_untrace_ref(nce)
156 #else
157 #define	NCE_TRACE_REF(nce)
158 #define	NCE_UNTRACE_REF(nce)
159 #endif
160 
161 #define	NCE_REFHOLD(nce) {		\
162 	mutex_enter(&(nce)->nce_lock);	\
163 	(nce)->nce_refcnt++;		\
164 	ASSERT((nce)->nce_refcnt != 0);	\
165 	NCE_TRACE_REF(nce);		\
166 	mutex_exit(&(nce)->nce_lock);	\
167 }
168 
169 #define	NCE_REFHOLD_NOTR(nce) {		\
170 	mutex_enter(&(nce)->nce_lock);	\
171 	(nce)->nce_refcnt++;		\
172 	ASSERT((nce)->nce_refcnt != 0);	\
173 	mutex_exit(&(nce)->nce_lock);	\
174 }
175 
176 #define	NCE_REFHOLD_LOCKED(nce) {		\
177 	ASSERT(MUTEX_HELD(&(nce)->nce_lock));	\
178 	(nce)->nce_refcnt++;			\
179 	NCE_TRACE_REF(nce);			\
180 }
181 
182 /* nce_inactive destroys the mutex thus no mutex_exit is needed */
183 #define	NCE_REFRELE(nce) {		\
184 	mutex_enter(&(nce)->nce_lock);	\
185 	NCE_UNTRACE_REF(nce);		\
186 	ASSERT((nce)->nce_refcnt != 0);	\
187 	if (--(nce)->nce_refcnt == 0)	\
188 		ndp_inactive(nce);	\
189 	else {				\
190 		mutex_exit(&(nce)->nce_lock);\
191 	}				\
192 }
193 
194 #define	NCE_REFRELE_NOTR(nce) {		\
195 	mutex_enter(&(nce)->nce_lock);	\
196 	ASSERT((nce)->nce_refcnt != 0);	\
197 	if (--(nce)->nce_refcnt == 0)	\
198 		ndp_inactive(nce);	\
199 	else {				\
200 		mutex_exit(&(nce)->nce_lock);\
201 	}				\
202 }
203 
204 #define	NDP_RESTART_TIMER(nce, ms) {	\
205 	ASSERT(!MUTEX_HELD(&(nce)->nce_lock));				\
206 	if ((nce)->nce_timeout_id != 0) {				\
207 		/* Ok to untimeout bad id. we don't hold a lock. */	\
208 		(void) untimeout((nce)->nce_timeout_id);		\
209 	}								\
210 	mutex_enter(&(nce)->nce_lock);					\
211 	/* Don't start the timer if the nce has been deleted */		\
212 	if (!((nce)->nce_flags & NCE_F_CONDEMNED)) 			\
213 		nce->nce_timeout_id = timeout(ndp_timer, nce, 		\
214 		    MSEC_TO_TICK(ms) == 0 ? 1 : MSEC_TO_TICK(ms));	\
215 	mutex_exit(&(nce)->nce_lock);					\
216 }
217 
218 /* Structure for ndp_cache_count() */
219 typedef struct {
220 	int	ncc_total;	/* Total number of NCEs */
221 	int	ncc_host;	/* NCE entries without R bit set */
222 } ncc_cache_count_t;
223 
224 /*
225  * Structure of ndp_cache_reclaim().  Each field is a fraction i.e. 1 means
226  * reclaim all, N means reclaim 1/Nth of all entries, 0 means reclaim none.
227  */
228 typedef struct {
229 	int	ncr_host;	/* Fraction for host entries */
230 } nce_cache_reclaim_t;
231 
232 /*
233  * Structure for nce_delete_hw_changed; specifies an IPv4 address to link-layer
234  * address mapping.  Any route that has a cached copy of a mapping for that
235  * IPv4 address that doesn't match the given mapping must be purged.
236  */
237 typedef struct {
238 	ipaddr_t hwm_addr;	/* IPv4 address */
239 	uint_t hwm_hwlen;	/* Length of hardware address (may be 0) */
240 	uchar_t *hwm_hwaddr;	/* Pointer to new hardware address, if any */
241 } nce_hw_map_t;
242 
243 /* When SAP is greater than zero address appears before SAP */
244 #define	NCE_LL_ADDR_OFFSET(ill)	(((ill)->ill_sap_length) < 0 ? \
245 	(sizeof (dl_unitdata_req_t)) : \
246 	((sizeof (dl_unitdata_req_t)) + (ABS((ill)->ill_sap_length))))
247 
248 #define	NCE_LL_SAP_OFFSET(ill) (((ill)->ill_sap_length) < 0 ? \
249 	((sizeof (dl_unitdata_req_t)) + ((ill)->ill_phys_addr_length)) : \
250 	(sizeof (dl_unitdata_req_t)))
251 
252 #ifdef _BIG_ENDIAN
253 #define	NCE_LL_SAP_COPY(ill, mp) \
254 	{ \
255 	size_t abs_sap_len = ABS((ill)->ill_sap_length); \
256 	if (abs_sap_len > 0) { \
257 		ASSERT(abs_sap_len <= sizeof (uint32_t)); \
258 		ASSERT((mp)->b_rptr + NCE_LL_SAP_OFFSET(ill) + \
259 		    abs_sap_len <= ((mp)->b_wptr)); \
260 		bcopy((uint8_t *)&(ill)->ill_sap + sizeof (ill->ill_sap) - \
261 		    abs_sap_len, \
262 		    ((mp)->b_rptr + NCE_LL_SAP_OFFSET(ill)), \
263 		    abs_sap_len); \
264 	} \
265 	}
266 #else
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 		uint32_t abs_sap_len = ABS((ill)->ill_sap_length); \
272 		ASSERT(abs_sap_len <= sizeof (uint32_t)); \
273 		ASSERT((mp)->b_rptr + NCE_LL_SAP_OFFSET(ill) + \
274 		    abs_sap_len <= ((mp)->b_wptr)); \
275 		bcopy(&((ill)->ill_sap), \
276 		((mp)->b_rptr + NCE_LL_SAP_OFFSET(ill)), \
277 		abs_sap_len); \
278 	} \
279 	}
280 #endif
281 
282 /*
283  * Exclusive-or the 6 bytes that are likely to contain the MAC
284  * address. Assumes table_size does not exceed 256.
285  * Assumes EUI-64 format for good hashing.
286  */
287 #define	NCE_ADDR_HASH_V6(addr, table_size)				\
288 	(((addr).s6_addr8[8] ^ (addr).s6_addr8[9] ^			\
289 	(addr).s6_addr8[10] ^ (addr).s6_addr8[13] ^			\
290 	(addr).s6_addr8[14] ^ (addr).s6_addr8[15]) % (table_size))
291 
292 extern	void	ndp_cache_count(nce_t *, char *);
293 extern	void	ndp_cache_reclaim(nce_t *, char *);
294 extern	void	ndp_delete(nce_t *);
295 extern	void	ndp_delete_per_ill(nce_t *, uchar_t *);
296 extern	void	ndp_fastpath_flush(nce_t *, char  *);
297 extern	boolean_t ndp_fastpath_update(nce_t *, void  *);
298 extern	nd_opt_hdr_t *ndp_get_option(nd_opt_hdr_t *, int, int);
299 extern	void	ndp_inactive(nce_t *);
300 extern	void	ndp_input(ill_t *, mblk_t *, mblk_t *);
301 extern	boolean_t ndp_lookup_ipaddr(in_addr_t);
302 extern	nce_t	*ndp_lookup_v6(ill_t *, const in6_addr_t *, boolean_t);
303 extern	nce_t	*ndp_lookup_v4(ill_t *, const in_addr_t *, boolean_t);
304 extern	int	ndp_lookup_then_add(ill_t *, uchar_t *, const void *,
305     const void *, const void *, uint32_t, uint16_t,
306     uint16_t, nce_t **, mblk_t *, mblk_t *);
307 extern	int	ndp_mcastreq(ill_t *, const in6_addr_t *, uint32_t, uint32_t,
308     mblk_t *);
309 extern	int	ndp_noresolver(ill_t *, const in6_addr_t *);
310 extern	void	ndp_process(nce_t *, uchar_t *, uint32_t, boolean_t);
311 extern	int	ndp_query(ill_t *, lif_nd_req_t *);
312 extern	int	ndp_report(queue_t *, mblk_t *, caddr_t, cred_t *);
313 extern	int	ndp_resolver(ill_t *, const in6_addr_t *, mblk_t *, zoneid_t);
314 extern	int	ndp_sioc_update(ill_t *, lif_nd_req_t *);
315 extern	boolean_t	ndp_verify_optlen(nd_opt_hdr_t *, int);
316 extern	void	ndp_timer(void *);
317 extern	void	ndp_walk(ill_t *, pfi_t, void *);
318 extern	void	ndp_walk_common(ndp_g_t *, ill_t *, pfi_t,
319     void *, boolean_t);
320 extern	int	ndp_add(ill_t *, uchar_t *, const void *,
321 		    const void *, const void *,
322 		    uint32_t, uint16_t, uint16_t, nce_t **, mblk_t *, mblk_t *);
323 extern	boolean_t	ndp_restart_dad(nce_t *);
324 extern	void	ndp_do_recovery(ipif_t *);
325 extern	void	nce_resolv_failed(nce_t *);
326 extern	void	arp_resolv_failed(nce_t *);
327 extern	void	nce_fastpath_list_add(nce_t *);
328 extern	void	nce_fastpath_list_delete(nce_t *);
329 extern	void	nce_fastpath_list_dispatch(ill_t *,
330     boolean_t (*)(nce_t *, void  *), void *);
331 extern	void	nce_queue_mp_common(nce_t *, mblk_t *, boolean_t);
332 extern	void	ndp_flush_qd_mp(nce_t *);
333 extern	nce_t	*nce_reinit(nce_t *);
334 extern	void	nce_delete_hw_changed(nce_t *, void *);
335 extern	void	nce_fastpath(nce_t *);
336 
337 #ifdef NCE_DEBUG
338 extern	void	nce_trace_inactive(nce_t *);
339 extern	void	nce_trace_ref(nce_t *);
340 extern	void	nce_untrace_ref(nce_t *);
341 extern	int	nce_thread_exit(nce_t *, caddr_t);
342 #endif
343 
344 #endif	/* _KERNEL */
345 
346 #ifdef	__cplusplus
347 }
348 #endif
349 
350 #endif	/* _INET_IP_NDP_H */
351