1 /*
2  * hostapd - IEEE 802.11i-2004 / WPA Authenticator
3  * Copyright (c) 2004-2007, Jouni Malinen <j@w1.fi>
4  *
5  * This software may be distributed under the terms of the BSD license.
6  * See README for more details.
7  */
8 
9 #ifndef WPA_AUTH_H
10 #define WPA_AUTH_H
11 
12 #include "common/defs.h"
13 #include "common/eapol_common.h"
14 #include "common/wpa_common.h"
15 #include "common/ieee802_11_defs.h"
16 
17 #ifdef _MSC_VER
18 #pragma pack(push, 1)
19 #endif /* _MSC_VER */
20 
21 /* IEEE Std 802.11r-2008, 11A.10.3 - Remote request/response frame definition
22  */
23 struct ft_rrb_frame {
24 	u8 frame_type; /* RSN_REMOTE_FRAME_TYPE_FT_RRB */
25 	u8 packet_type; /* FT_PACKET_REQUEST/FT_PACKET_RESPONSE */
26 	le16 action_length; /* little endian length of action_frame */
27 	u8 ap_address[ETH_ALEN];
28 	/*
29 	 * Followed by action_length bytes of FT Action frame (from Category
30 	 * field to the end of Action Frame body.
31 	 */
32 } STRUCT_PACKED;
33 
34 #define RSN_REMOTE_FRAME_TYPE_FT_RRB 1
35 
36 #define FT_PACKET_REQUEST 0
37 #define FT_PACKET_RESPONSE 1
38 /* Vendor-specific types for R0KH-R1KH protocol; not defined in 802.11r */
39 #define FT_PACKET_R0KH_R1KH_PULL 200
40 #define FT_PACKET_R0KH_R1KH_RESP 201
41 #define FT_PACKET_R0KH_R1KH_PUSH 202
42 
43 #define FT_R0KH_R1KH_PULL_DATA_LEN 44
44 #define FT_R0KH_R1KH_RESP_DATA_LEN 76
45 #define FT_R0KH_R1KH_PUSH_DATA_LEN 88
46 #define FT_R0KH_R1KH_PULL_NONCE_LEN 16
47 
48 struct ft_r0kh_r1kh_pull_frame {
49 	u8 frame_type; /* RSN_REMOTE_FRAME_TYPE_FT_RRB */
50 	u8 packet_type; /* FT_PACKET_R0KH_R1KH_PULL */
51 	le16 data_length; /* little endian length of data (44) */
52 	u8 ap_address[ETH_ALEN];
53 
54 	u8 nonce[FT_R0KH_R1KH_PULL_NONCE_LEN];
55 	u8 pmk_r0_name[WPA_PMK_NAME_LEN];
56 	u8 r1kh_id[FT_R1KH_ID_LEN];
57 	u8 s1kh_id[ETH_ALEN];
58 	u8 pad[4]; /* 8-octet boundary for AES key wrap */
59 	u8 key_wrap_extra[8];
60 } STRUCT_PACKED;
61 
62 struct ft_r0kh_r1kh_resp_frame {
63 	u8 frame_type; /* RSN_REMOTE_FRAME_TYPE_FT_RRB */
64 	u8 packet_type; /* FT_PACKET_R0KH_R1KH_RESP */
65 	le16 data_length; /* little endian length of data (76) */
66 	u8 ap_address[ETH_ALEN];
67 
68 	u8 nonce[FT_R0KH_R1KH_PULL_NONCE_LEN]; /* copied from pull */
69 	u8 r1kh_id[FT_R1KH_ID_LEN]; /* copied from pull */
70 	u8 s1kh_id[ETH_ALEN]; /* copied from pull */
71 	u8 pmk_r1[PMK_LEN];
72 	u8 pmk_r1_name[WPA_PMK_NAME_LEN];
73 	le16 pairwise;
74 	u8 pad[2]; /* 8-octet boundary for AES key wrap */
75 	u8 key_wrap_extra[8];
76 } STRUCT_PACKED;
77 
78 struct ft_r0kh_r1kh_push_frame {
79 	u8 frame_type; /* RSN_REMOTE_FRAME_TYPE_FT_RRB */
80 	u8 packet_type; /* FT_PACKET_R0KH_R1KH_PUSH */
81 	le16 data_length; /* little endian length of data (88) */
82 	u8 ap_address[ETH_ALEN];
83 
84 	/* Encrypted with AES key-wrap */
85 	u8 timestamp[4]; /* current time in seconds since unix epoch, little
86 			  * endian */
87 	u8 r1kh_id[FT_R1KH_ID_LEN];
88 	u8 s1kh_id[ETH_ALEN];
89 	u8 pmk_r0_name[WPA_PMK_NAME_LEN];
90 	u8 pmk_r1[PMK_LEN];
91 	u8 pmk_r1_name[WPA_PMK_NAME_LEN];
92 	le16 pairwise;
93 	u8 pad[6]; /* 8-octet boundary for AES key wrap */
94 	u8 key_wrap_extra[8];
95 } STRUCT_PACKED;
96 
97 #ifdef _MSC_VER
98 #pragma pack(pop)
99 #endif /* _MSC_VER */
100 
101 
102 /* per STA state machine data */
103 
104 struct wpa_authenticator;
105 struct wpa_state_machine;
106 struct rsn_pmksa_cache_entry;
107 struct eapol_state_machine;
108 
109 
110 struct ft_remote_r0kh {
111 	struct ft_remote_r0kh *next;
112 	u8 addr[ETH_ALEN];
113 	u8 id[FT_R0KH_ID_MAX_LEN];
114 	size_t id_len;
115 	u8 key[16];
116 };
117 
118 
119 struct ft_remote_r1kh {
120 	struct ft_remote_r1kh *next;
121 	u8 addr[ETH_ALEN];
122 	u8 id[FT_R1KH_ID_LEN];
123 	u8 key[16];
124 };
125 
126 
127 struct wpa_auth_config {
128 	int wpa;
129 	int wpa_key_mgmt;
130 	int wpa_pairwise;
131 	int wpa_group;
132 	int wpa_group_rekey;
133 	int wpa_strict_rekey;
134 	int wpa_gmk_rekey;
135 	int wpa_ptk_rekey;
136 	int rsn_pairwise;
137 	int rsn_preauth;
138 	int eapol_version;
139 	int peerkey;
140 	int wmm_enabled;
141 	int wmm_uapsd;
142 	int disable_pmksa_caching;
143 	int okc;
144 	int tx_status;
145 #ifdef CONFIG_IEEE80211W
146 	enum mfp_options ieee80211w;
147 	int group_mgmt_cipher;
148 #endif /* CONFIG_IEEE80211W */
149 #ifdef CONFIG_IEEE80211R
150 	u8 ssid[SSID_MAX_LEN];
151 	size_t ssid_len;
152 	u8 mobility_domain[MOBILITY_DOMAIN_ID_LEN];
153 	u8 r0_key_holder[FT_R0KH_ID_MAX_LEN];
154 	size_t r0_key_holder_len;
155 	u8 r1_key_holder[FT_R1KH_ID_LEN];
156 	u32 r0_key_lifetime;
157 	u32 reassociation_deadline;
158 	struct ft_remote_r0kh *r0kh_list;
159 	struct ft_remote_r1kh *r1kh_list;
160 	int pmk_r1_push;
161 	int ft_over_ds;
162 #endif /* CONFIG_IEEE80211R */
163 	int disable_gtk;
164 	int ap_mlme;
165 #ifdef CONFIG_TESTING_OPTIONS
166 	double corrupt_gtk_rekey_mic_probability;
167 #endif /* CONFIG_TESTING_OPTIONS */
168 #ifdef CONFIG_P2P
169 	u8 ip_addr_go[4];
170 	u8 ip_addr_mask[4];
171 	u8 ip_addr_start[4];
172 	u8 ip_addr_end[4];
173 #endif /* CONFIG_P2P */
174 };
175 
176 typedef enum {
177 	LOGGER_DEBUG, LOGGER_INFO, LOGGER_WARNING
178 } logger_level;
179 
180 typedef enum {
181 	WPA_EAPOL_portEnabled, WPA_EAPOL_portValid, WPA_EAPOL_authorized,
182 	WPA_EAPOL_portControl_Auto, WPA_EAPOL_keyRun, WPA_EAPOL_keyAvailable,
183 	WPA_EAPOL_keyDone, WPA_EAPOL_inc_EapolFramesTx
184 } wpa_eapol_variable;
185 
186 struct wpa_auth_callbacks {
187 	void *ctx;
188 	void (*logger)(void *ctx, const u8 *addr, logger_level level,
189 		       const char *txt);
190 	void (*disconnect)(void *ctx, const u8 *addr, u16 reason);
191 	int (*mic_failure_report)(void *ctx, const u8 *addr);
192 	void (*psk_failure_report)(void *ctx, const u8 *addr);
193 	void (*set_eapol)(void *ctx, const u8 *addr, wpa_eapol_variable var,
194 			  int value);
195 	int (*get_eapol)(void *ctx, const u8 *addr, wpa_eapol_variable var);
196 	const u8 * (*get_psk)(void *ctx, const u8 *addr, const u8 *p2p_dev_addr,
197 			      const u8 *prev_psk);
198 	int (*get_msk)(void *ctx, const u8 *addr, u8 *msk, size_t *len);
199 	int (*set_key)(void *ctx, int vlan_id, enum wpa_alg alg,
200 		       const u8 *addr, int idx, u8 *key, size_t key_len);
201 	int (*get_seqnum)(void *ctx, const u8 *addr, int idx, u8 *seq);
202 	int (*send_eapol)(void *ctx, const u8 *addr, const u8 *data,
203 			  size_t data_len, int encrypt);
204 	int (*for_each_sta)(void *ctx, int (*cb)(struct wpa_state_machine *sm,
205 						 void *ctx), void *cb_ctx);
206 	int (*for_each_auth)(void *ctx, int (*cb)(struct wpa_authenticator *a,
207 						  void *ctx), void *cb_ctx);
208 	int (*send_ether)(void *ctx, const u8 *dst, u16 proto, const u8 *data,
209 			  size_t data_len);
210 #ifdef CONFIG_IEEE80211R
211 	struct wpa_state_machine * (*add_sta)(void *ctx, const u8 *sta_addr);
212 	int (*send_ft_action)(void *ctx, const u8 *dst,
213 			      const u8 *data, size_t data_len);
214 	int (*add_tspec)(void *ctx, const u8 *sta_addr, u8 *tspec_ie,
215 			 size_t tspec_ielen);
216 #endif /* CONFIG_IEEE80211R */
217 #ifdef CONFIG_MESH
218 	int (*start_ampe)(void *ctx, const u8 *sta_addr);
219 #endif /* CONFIG_MESH */
220 };
221 
222 struct wpa_authenticator * wpa_init(const u8 *addr,
223 				    struct wpa_auth_config *conf,
224 				    struct wpa_auth_callbacks *cb);
225 int wpa_init_keys(struct wpa_authenticator *wpa_auth);
226 void wpa_deinit(struct wpa_authenticator *wpa_auth);
227 int wpa_reconfig(struct wpa_authenticator *wpa_auth,
228 		 struct wpa_auth_config *conf);
229 
230 enum {
231 	WPA_IE_OK, WPA_INVALID_IE, WPA_INVALID_GROUP, WPA_INVALID_PAIRWISE,
232 	WPA_INVALID_AKMP, WPA_NOT_ENABLED, WPA_ALLOC_FAIL,
233 	WPA_MGMT_FRAME_PROTECTION_VIOLATION, WPA_INVALID_MGMT_GROUP_CIPHER,
234 	WPA_INVALID_MDIE, WPA_INVALID_PROTO
235 };
236 
237 int wpa_validate_wpa_ie(struct wpa_authenticator *wpa_auth,
238 			struct wpa_state_machine *sm,
239 			const u8 *wpa_ie, size_t wpa_ie_len,
240 			const u8 *mdie, size_t mdie_len);
241 int wpa_validate_osen(struct wpa_authenticator *wpa_auth,
242 		      struct wpa_state_machine *sm,
243 		      const u8 *osen_ie, size_t osen_ie_len);
244 int wpa_auth_uses_mfp(struct wpa_state_machine *sm);
245 struct wpa_state_machine *
246 wpa_auth_sta_init(struct wpa_authenticator *wpa_auth, const u8 *addr,
247 		  const u8 *p2p_dev_addr);
248 int wpa_auth_sta_associated(struct wpa_authenticator *wpa_auth,
249 			    struct wpa_state_machine *sm);
250 void wpa_auth_sta_no_wpa(struct wpa_state_machine *sm);
251 void wpa_auth_sta_deinit(struct wpa_state_machine *sm);
252 void wpa_receive(struct wpa_authenticator *wpa_auth,
253 		 struct wpa_state_machine *sm,
254 		 u8 *data, size_t data_len);
255 typedef enum {
256 	WPA_AUTH, WPA_ASSOC, WPA_DISASSOC, WPA_DEAUTH, WPA_REAUTH,
257 	WPA_REAUTH_EAPOL, WPA_ASSOC_FT
258 } wpa_event;
259 void wpa_remove_ptk(struct wpa_state_machine *sm);
260 int wpa_auth_sm_event(struct wpa_state_machine *sm, wpa_event event);
261 void wpa_auth_sm_notify(struct wpa_state_machine *sm);
262 void wpa_gtk_rekey(struct wpa_authenticator *wpa_auth);
263 int wpa_get_mib(struct wpa_authenticator *wpa_auth, char *buf, size_t buflen);
264 int wpa_get_mib_sta(struct wpa_state_machine *sm, char *buf, size_t buflen);
265 void wpa_auth_countermeasures_start(struct wpa_authenticator *wpa_auth);
266 int wpa_auth_pairwise_set(struct wpa_state_machine *sm);
267 int wpa_auth_get_pairwise(struct wpa_state_machine *sm);
268 int wpa_auth_sta_key_mgmt(struct wpa_state_machine *sm);
269 int wpa_auth_sta_wpa_version(struct wpa_state_machine *sm);
270 int wpa_auth_sta_clear_pmksa(struct wpa_state_machine *sm,
271 			     struct rsn_pmksa_cache_entry *entry);
272 struct rsn_pmksa_cache_entry *
273 wpa_auth_sta_get_pmksa(struct wpa_state_machine *sm);
274 void wpa_auth_sta_local_mic_failure_report(struct wpa_state_machine *sm);
275 const u8 * wpa_auth_get_wpa_ie(struct wpa_authenticator *wpa_auth,
276 			       size_t *len);
277 int wpa_auth_pmksa_add(struct wpa_state_machine *sm, const u8 *pmk,
278 		       int session_timeout, struct eapol_state_machine *eapol);
279 int wpa_auth_pmksa_add_preauth(struct wpa_authenticator *wpa_auth,
280 			       const u8 *pmk, size_t len, const u8 *sta_addr,
281 			       int session_timeout,
282 			       struct eapol_state_machine *eapol);
283 int wpa_auth_pmksa_add_sae(struct wpa_authenticator *wpa_auth, const u8 *addr,
284 			   const u8 *pmk);
285 void wpa_auth_pmksa_remove(struct wpa_authenticator *wpa_auth,
286 			   const u8 *sta_addr);
287 int wpa_auth_sta_set_vlan(struct wpa_state_machine *sm, int vlan_id);
288 void wpa_auth_eapol_key_tx_status(struct wpa_authenticator *wpa_auth,
289 				  struct wpa_state_machine *sm, int ack);
290 
291 #ifdef CONFIG_IEEE80211R
292 u8 * wpa_sm_write_assoc_resp_ies(struct wpa_state_machine *sm, u8 *pos,
293 				 size_t max_len, int auth_alg,
294 				 const u8 *req_ies, size_t req_ies_len);
295 void wpa_ft_process_auth(struct wpa_state_machine *sm, const u8 *bssid,
296 			 u16 auth_transaction, const u8 *ies, size_t ies_len,
297 			 void (*cb)(void *ctx, const u8 *dst, const u8 *bssid,
298 				    u16 auth_transaction, u16 resp,
299 				    const u8 *ies, size_t ies_len),
300 			 void *ctx);
301 u16 wpa_ft_validate_reassoc(struct wpa_state_machine *sm, const u8 *ies,
302 			    size_t ies_len);
303 int wpa_ft_action_rx(struct wpa_state_machine *sm, const u8 *data, size_t len);
304 int wpa_ft_rrb_rx(struct wpa_authenticator *wpa_auth, const u8 *src_addr,
305 		  const u8 *data, size_t data_len);
306 void wpa_ft_push_pmk_r1(struct wpa_authenticator *wpa_auth, const u8 *addr);
307 #endif /* CONFIG_IEEE80211R */
308 
309 void wpa_wnmsleep_rekey_gtk(struct wpa_state_machine *sm);
310 void wpa_set_wnmsleep(struct wpa_state_machine *sm, int flag);
311 int wpa_wnmsleep_gtk_subelem(struct wpa_state_machine *sm, u8 *pos);
312 int wpa_wnmsleep_igtk_subelem(struct wpa_state_machine *sm, u8 *pos);
313 
314 int wpa_auth_uses_sae(struct wpa_state_machine *sm);
315 int wpa_auth_uses_ft_sae(struct wpa_state_machine *sm);
316 
317 int wpa_auth_get_ip_addr(struct wpa_state_machine *sm, u8 *addr);
318 
319 struct radius_das_attrs;
320 int wpa_auth_radius_das_disconnect_pmksa(struct wpa_authenticator *wpa_auth,
321 					 struct radius_das_attrs *attr);
322 void wpa_auth_reconfig_group_keys(struct wpa_authenticator *wpa_auth);
323 
324 #endif /* WPA_AUTH_H */
325