1 /*
2  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
3  * Use is subject to license terms.
4  */
5 
6 /*
7  * Copyright (c) 2003-2004, Jouni Malinen <jkmaline@cc.hut.fi>
8  * Sun elects to license this software under the BSD license.
9  * See README for more details.
10  */
11 #ifndef __WPA_IMPL_H
12 #define	__WPA_IMPL_H
13 
14 #pragma ident	"%Z%%M%	%I%	%E% SMI"
15 
16 #include <net/wpa.h>
17 #include <libdladm.h>
18 #include <libdllink.h>
19 
20 #ifdef	__cplusplus
21 extern "C" {
22 #endif
23 
24 #define	BIT(n)			(1 << (n))
25 
26 #define	WPA_CIPHER_NONE		BIT(0)
27 #define	WPA_CIPHER_WEP40	BIT(1)
28 #define	WPA_CIPHER_WEP104	BIT(2)
29 #define	WPA_CIPHER_TKIP		BIT(3)
30 #define	WPA_CIPHER_CCMP		BIT(4)
31 
32 #define	WPA_KEY_MGMT_IEEE8021X	BIT(0)
33 #define	WPA_KEY_MGMT_PSK	BIT(1)
34 #define	WPA_KEY_MGMT_NONE	BIT(2)
35 #define	WPA_KEY_MGMT_IEEE8021X_NO_WPA	BIT(3)
36 
37 #define	WPA_PROTO_WPA		BIT(0)
38 #define	WPA_PROTO_RSN		BIT(1)
39 
40 #pragma pack(1)
41 struct ieee802_1x_hdr {
42 	uint8_t		version;
43 	uint8_t		type;
44 	uint16_t	length;
45 	/* followed by length octets of data */
46 };
47 #pragma pack()
48 
49 #define	EAPOL_VERSION	2
50 
51 enum {	IEEE802_1X_TYPE_EAP_PACKET	= 0,
52 	IEEE802_1X_TYPE_EAPOL_START	= 1,
53 	IEEE802_1X_TYPE_EAPOL_LOGOFF	= 2,
54 	IEEE802_1X_TYPE_EAPOL_KEY	= 3,
55 	IEEE802_1X_TYPE_EAPOL_ENCAPSULATED_ASF_ALERT	= 4
56 };
57 
58 enum {	EAPOL_KEY_TYPE_RC4 = 1,
59 	EAPOL_KEY_TYPE_RSN = 2,
60 	EAPOL_KEY_TYPE_WPA = 254
61 };
62 
63 #define	WPA_NONCE_LEN		32
64 #define	WPA_REPLAY_COUNTER_LEN	8
65 #define	MAX_PSK_LENGTH		64
66 #define	WPA_PMK_LEN		32
67 
68 #pragma pack(1)
69 struct wpa_eapol_key {
70 	uint8_t		type;
71 	uint16_t	key_info;
72 	uint16_t	key_length;
73 	uint8_t		replay_counter[WPA_REPLAY_COUNTER_LEN];
74 	uint8_t		key_nonce[WPA_NONCE_LEN];
75 	uint8_t		key_iv[16];
76 	uint8_t		key_rsc[8];
77 	uint8_t		key_id[8]; /* Reserved in IEEE 802.11i/RSN */
78 	uint8_t		key_mic[16];
79 	uint16_t	key_data_length;
80 	/* followed by key_data_length bytes of key_data */
81 };
82 #pragma pack()
83 
84 #define	WPA_KEY_INFO_TYPE_MASK		(BIT(0) | BIT(1) | BIT(2))
85 #define	WPA_KEY_INFO_TYPE_HMAC_MD5_RC4	BIT(0)
86 #define	WPA_KEY_INFO_TYPE_HMAC_SHA1_AES	BIT(1)
87 #define	WPA_KEY_INFO_KEY_TYPE		BIT(3) /* 1: Pairwise, 0: Group key */
88 /* bit4..5 is used in WPA, but is reserved in IEEE 802.11i/RSN */
89 #define	WPA_KEY_INFO_KEY_INDEX_MASK	(BIT(4) | BIT(5))
90 #define	WPA_KEY_INFO_KEY_INDEX_SHIFT	4
91 #define	WPA_KEY_INFO_INSTALL		BIT(6) /* pairwise */
92 #define	WPA_KEY_INFO_TXRX		BIT(6) /* group */
93 #define	WPA_KEY_INFO_ACK		BIT(7)
94 #define	WPA_KEY_INFO_MIC		BIT(8)
95 #define	WPA_KEY_INFO_SECURE		BIT(9)
96 #define	WPA_KEY_INFO_ERROR		BIT(10)
97 #define	WPA_KEY_INFO_REQUEST		BIT(11)
98 #define	WPA_KEY_INFO_ENCR_KEY_DATA	BIT(12) /* IEEE 802.11i/RSN only */
99 
100 #define	WPA_CAPABILITY_PREAUTH		BIT(0)
101 
102 #define	GENERIC_INFO_ELEM		0xdd
103 #define	RSN_INFO_ELEM			0x30
104 
105 #define	MAX_LOGBUF			4096
106 #define	MAX_SCANRESULTS			64
107 
108 enum {
109 	REASON_UNSPECIFIED			= 1,
110 	REASON_DEAUTH_LEAVING			= 3,
111 	REASON_INVALID_IE			= 13,
112 	REASON_MICHAEL_MIC_FAILURE		= 14,
113 	REASON_4WAY_HANDSHAKE_TIMEOUT		= 15,
114 	REASON_GROUP_KEY_UPDATE_TIMEOUT		= 16,
115 	REASON_IE_IN_4WAY_DIFFERS		= 17,
116 	REASON_GROUP_CIPHER_NOT_VALID		= 18,
117 	REASON_PAIRWISE_CIPHER_NOT_VALID	= 19,
118 	REASON_AKMP_NOT_VALID			= 20,
119 	REASON_UNSUPPORTED_RSN_IE_VERSION	= 21,
120 	REASON_INVALID_RSN_IE_CAPAB		= 22,
121 	REASON_IEEE_802_1X_AUTH_FAILED		= 23,
122 	REASON_CIPHER_SUITE_REJECTED		= 24
123 };
124 
125 /*
126  * wpa_supplicant
127  */
128 #define	PMKID_LEN 			16
129 #define	PMK_LEN				32
130 
131 #define	MAC2STR(a) (a)[0], (a)[1], (a)[2], (a)[3], (a)[4], (a)[5]
132 #define	MACSTR "%02x:%02x:%02x:%02x:%02x:%02x"
133 
134 struct rsn_pmksa_cache {
135 	struct rsn_pmksa_cache	*next;
136 	uint8_t			pmkid[PMKID_LEN];
137 	uint8_t			pmk[PMK_LEN];
138 	time_t			expiration;
139 	int			akmp; /* WPA_KEY_MGMT_* */
140 	uint8_t			aa[IEEE80211_ADDR_LEN];
141 };
142 
143 struct rsn_pmksa_candidate {
144 	struct rsn_pmksa_candidate *next;
145 	uint8_t			bssid[IEEE80211_ADDR_LEN];
146 };
147 
148 
149 #pragma pack(1)
150 struct wpa_ptk {
151 	uint8_t mic_key[16]; /* EAPOL-Key MIC Key (MK) */
152 	uint8_t encr_key[16]; /* EAPOL-Key Encryption Key (EK) */
153 	uint8_t tk1[16]; /* Temporal Key 1 (TK1) */
154 	union {
155 		uint8_t tk2[16]; /* Temporal Key 2 (TK2) */
156 		struct {
157 			uint8_t tx_mic_key[8];
158 			uint8_t rx_mic_key[8];
159 		} auth;
160 	} u;
161 };
162 #pragma pack()
163 
164 
165 struct wpa_supplicant {
166 	struct l2_packet_data	*l2;
167 	unsigned char		own_addr[IEEE80211_ADDR_LEN];
168 
169 	datalink_id_t		linkid;
170 	char			kname[DLADM_SECOBJ_NAME_MAX];
171 
172 	uint8_t			pmk[PMK_LEN];
173 
174 	uint8_t			snonce[WPA_NONCE_LEN];
175 	uint8_t			anonce[WPA_NONCE_LEN];
176 	/* ANonce from the last 1/4 msg */
177 
178 	struct wpa_ptk		ptk, tptk;
179 	int			ptk_set, tptk_set;
180 	int			renew_snonce;
181 
182 	struct wpa_config	*conf;
183 
184 	uint8_t			request_counter[WPA_REPLAY_COUNTER_LEN];
185 	uint8_t			rx_replay_counter[WPA_REPLAY_COUNTER_LEN];
186 	int			rx_replay_counter_set;
187 
188 	uint8_t			bssid[IEEE80211_ADDR_LEN];
189 	int			reassociate; /* reassociation requested */
190 
191 	uint8_t			*ap_wpa_ie;
192 	size_t			ap_wpa_ie_len;
193 
194 	/*
195 	 * Selected configuration
196 	 * based on Beacon/ProbeResp WPA IE
197 	 */
198 	int			proto;
199 	int 			pairwise_cipher;
200 	int 			group_cipher;
201 	int			key_mgmt;
202 
203 	struct wpa_driver_ops	*driver;
204 
205 	enum {
206 		WPA_DISCONNECTED,
207 		WPA_SCANNING,
208 		WPA_ASSOCIATING,
209 		WPA_ASSOCIATED,
210 		WPA_4WAY_HANDSHAKE,
211 		WPA_GROUP_HANDSHAKE,
212 		WPA_COMPLETED
213 	} wpa_state;
214 
215 	struct rsn_pmksa_cache	*pmksa; /* PMKSA cache */
216 	int	pmksa_count; /* number of entries in PMKSA cache */
217 	struct rsn_pmksa_cache	*cur_pmksa; /* current PMKSA entry */
218 	struct rsn_pmksa_candidate	*pmksa_candidates;
219 
220 	/*
221 	 * number of EAPOL packets received after the
222 	 * previous association event
223 	 */
224 	int			eapol_received;
225 };
226 
227 struct wpa_ie_data {
228 	int	proto;
229 	int	pairwise_cipher;
230 	int	group_cipher;
231 	int	key_mgmt;
232 	int	capabilities;
233 };
234 
235 /* WPA configuration */
236 struct wpa_ssid {
237 	uint8_t	*ssid;
238 	size_t	ssid_len;
239 
240 	uint8_t	bssid[IEEE80211_ADDR_LEN];
241 	int	bssid_set;
242 
243 	uint8_t	psk[PMK_LEN];
244 	int	psk_set;
245 	char	*passphrase;
246 
247 	/* Bitfields of allowed Pairwise/Group Ciphers, WPA_CIPHER_* */
248 	int	pairwise_cipher;
249 	int	group_cipher;
250 
251 	int	key_mgmt;
252 	int	proto; /* Bitfield of allowed protocols (WPA_PROTO_*) */
253 };
254 
255 struct wpa_config {
256 	struct wpa_ssid *ssid; /* global network list */
257 	int eapol_version;
258 	/* int ap_scan; */
259 };
260 
261 struct wpa_config *wpa_config_read(void *);
262 void wpa_config_free(struct wpa_config *);
263 
264 /*
265  * Debugging function - conditional printf and hex dump.
266  * Driver wrappers can use these for debugging purposes.
267  */
268 enum { MSG_MSGDUMP, MSG_DEBUG, MSG_INFO, MSG_WARNING, MSG_ERROR };
269 
270 void wpa_printf(int, char *, ...);
271 void wpa_hexdump(int, const char *, const uint8_t *, size_t);
272 
273 void wpa_event_handler(void *, wpa_event_type);
274 void wpa_supplicant_rx_eapol(void *, unsigned char *, unsigned char *, size_t);
275 
276 void wpa_supplicant_scan(void *, void *);
277 void wpa_supplicant_req_scan(struct wpa_supplicant *, int, int);
278 
279 void wpa_supplicant_req_auth_timeout(struct wpa_supplicant *, int, int);
280 void wpa_supplicant_cancel_auth_timeout(struct wpa_supplicant *);
281 void wpa_supplicant_disassociate(struct wpa_supplicant *, int);
282 
283 void pmksa_cache_free(struct wpa_supplicant *);
284 void pmksa_candidate_free(struct wpa_supplicant *);
285 struct rsn_pmksa_cache *pmksa_cache_get(struct wpa_supplicant *,
286     uint8_t *, uint8_t *);
287 
288 int wpa_parse_wpa_ie(struct wpa_supplicant *, uint8_t *,
289 	size_t, struct wpa_ie_data *);
290 int wpa_gen_wpa_ie(struct wpa_supplicant *, uint8_t *);
291 
292 #ifdef __cplusplus
293 }
294 #endif
295 
296 #endif /* __WPA_IMPL_H */
297