1 /*
2  * hostapd - IEEE 802.11i-2004 / WPA Authenticator: Internal definitions
3  * Copyright (c) 2004-2015, 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_I_H
10 #define WPA_AUTH_I_H
11 
12 /* max(dot11RSNAConfigGroupUpdateCount,dot11RSNAConfigPairwiseUpdateCount) */
13 #define RSNA_MAX_EAPOL_RETRIES 4
14 
15 struct wpa_group;
16 
17 struct wpa_stsl_negotiation {
18 	struct wpa_stsl_negotiation *next;
19 	u8 initiator[ETH_ALEN];
20 	u8 peer[ETH_ALEN];
21 };
22 
23 
24 struct wpa_state_machine {
25 	struct wpa_authenticator *wpa_auth;
26 	struct wpa_group *group;
27 
28 	u8 addr[ETH_ALEN];
29 
30 	enum {
31 		WPA_PTK_INITIALIZE, WPA_PTK_DISCONNECT, WPA_PTK_DISCONNECTED,
32 		WPA_PTK_AUTHENTICATION, WPA_PTK_AUTHENTICATION2,
33 		WPA_PTK_INITPMK, WPA_PTK_INITPSK, WPA_PTK_PTKSTART,
34 		WPA_PTK_PTKCALCNEGOTIATING, WPA_PTK_PTKCALCNEGOTIATING2,
35 		WPA_PTK_PTKINITNEGOTIATING, WPA_PTK_PTKINITDONE
36 	} wpa_ptk_state;
37 
38 	enum {
39 		WPA_PTK_GROUP_IDLE = 0,
40 		WPA_PTK_GROUP_REKEYNEGOTIATING,
41 		WPA_PTK_GROUP_REKEYESTABLISHED,
42 		WPA_PTK_GROUP_KEYERROR
43 	} wpa_ptk_group_state;
44 
45 	Boolean Init;
46 	Boolean DeauthenticationRequest;
47 	Boolean AuthenticationRequest;
48 	Boolean ReAuthenticationRequest;
49 	Boolean Disconnect;
50 	u16 disconnect_reason; /* specific reason code to use with Disconnect */
51 	int TimeoutCtr;
52 	int GTimeoutCtr;
53 	Boolean TimeoutEvt;
54 	Boolean EAPOLKeyReceived;
55 	Boolean EAPOLKeyPairwise;
56 	Boolean EAPOLKeyRequest;
57 	Boolean MICVerified;
58 	Boolean GUpdateStationKeys;
59 	u8 ANonce[WPA_NONCE_LEN];
60 	u8 SNonce[WPA_NONCE_LEN];
61 	u8 PMK[PMK_LEN];
62 	unsigned int pmk_len;
63 	u8 pmkid[PMKID_LEN];
64 	struct wpa_ptk PTK;
65 	Boolean PTK_valid;
66 	Boolean pairwise_set;
67 	int keycount;
68 	Boolean Pair;
69 	struct wpa_key_replay_counter {
70 		u8 counter[WPA_REPLAY_COUNTER_LEN];
71 		Boolean valid;
72 	} key_replay[RSNA_MAX_EAPOL_RETRIES],
73 		prev_key_replay[RSNA_MAX_EAPOL_RETRIES];
74 	Boolean PInitAKeys; /* WPA only, not in IEEE 802.11i */
75 	Boolean PTKRequest; /* not in IEEE 802.11i state machine */
76 	Boolean has_GTK;
77 	Boolean PtkGroupInit; /* init request for PTK Group state machine */
78 
79 	u8 *last_rx_eapol_key; /* starting from IEEE 802.1X header */
80 	size_t last_rx_eapol_key_len;
81 
82 	unsigned int changed:1;
83 	unsigned int in_step_loop:1;
84 	unsigned int pending_deinit:1;
85 	unsigned int started:1;
86 	unsigned int mgmt_frame_prot:1;
87 	unsigned int rx_eapol_key_secure:1;
88 	unsigned int update_snonce:1;
89 #ifdef CONFIG_IEEE80211R_AP
90 	unsigned int ft_completed:1;
91 	unsigned int pmk_r1_name_valid:1;
92 #endif /* CONFIG_IEEE80211R */
93 	unsigned int is_wnmsleep:1;
94 	unsigned int pmkid_set:1;
95 
96 	u8 req_replay_counter[WPA_REPLAY_COUNTER_LEN];
97 	int req_replay_counter_used;
98 
99 	u8 *wpa_ie;
100 	size_t wpa_ie_len;
101 	u8 *rsnxe;
102 	size_t rsnxe_len;
103 
104 	enum {
105 		WPA_VERSION_NO_WPA = 0 /* WPA not used */,
106 		WPA_VERSION_WPA = 1 /* WPA / IEEE 802.11i/D3.0 */,
107 		WPA_VERSION_WPA2 = 2 /* WPA2 / IEEE 802.11i */
108 	} wpa;
109 	int pairwise; /* Pairwise cipher suite, WPA_CIPHER_* */
110 	int wpa_key_mgmt; /* the selected WPA_KEY_MGMT_* */
111 	struct rsn_pmksa_cache_entry *pmksa;
112 
113 #ifdef CONFIG_IEEE80211R_AP
114 	u8 xxkey[PMK_LEN_MAX]; /* PSK or the second 256 bits of MSK, or the
115 				* first 384 bits of MSK */
116 	size_t xxkey_len;
117 	u8 pmk_r1[PMK_LEN_MAX];
118 	unsigned int pmk_r1_len;
119 	u8 pmk_r1_name[WPA_PMK_NAME_LEN]; /* PMKR1Name derived from FT Auth
120 					   * Request */
121 	u8 r0kh_id[FT_R0KH_ID_MAX_LEN]; /* R0KH-ID from FT Auth Request */
122 	size_t r0kh_id_len;
123 	u8 sup_pmk_r1_name[WPA_PMK_NAME_LEN]; /* PMKR1Name from EAPOL-Key
124 					       * message 2/4 */
125 	u8 *assoc_resp_ftie;
126 
127 	void (*ft_pending_cb)(void *ctx, const u8 *dst, const u8 *bssid,
128 			      u16 auth_transaction, u16 status,
129 			      const u8 *ies, size_t ies_len);
130 	void *ft_pending_cb_ctx;
131 	struct wpabuf *ft_pending_req_ies;
132 	u8 ft_pending_pull_nonce[FT_RRB_NONCE_LEN];
133 	u8 ft_pending_auth_transaction;
134 	u8 ft_pending_current_ap[ETH_ALEN];
135 	int ft_pending_pull_left_retries;
136 #endif /* CONFIG_IEEE80211R_AP */
137 
138 	int pending_1_of_4_timeout;
139 	u32 index;
140 	struct rsn_sppamsdu_sup spp_sup;
141 };
142 
143 
144 /* per group key state machine data */
145 struct wpa_group {
146 	struct wpa_group *next;
147 	int vlan_id;
148 
149 	Boolean GInit;
150 	int GKeyDoneStations;
151 	Boolean GTKReKey;
152 	int GTK_len;
153 	int GN, GM;
154 	Boolean GTKAuthenticator;
155 	u8 Counter[WPA_NONCE_LEN];
156 
157 	enum {
158 		WPA_GROUP_GTK_INIT = 0,
159 		WPA_GROUP_SETKEYS, WPA_GROUP_SETKEYSDONE
160 	} wpa_group_state;
161 
162 	u8 GMK[WPA_GMK_LEN];
163 	u8 GTK[2][WPA_GTK_MAX_LEN];
164 	u8 GNonce[WPA_NONCE_LEN];
165 	Boolean changed;
166 	Boolean first_sta_seen;
167 	Boolean reject_4way_hs_for_entropy;
168 #ifdef CONFIG_IEEE80211W
169 	u8 IGTK[2][WPA_IGTK_LEN];
170 	int GN_igtk, GM_igtk;
171 #endif /* CONFIG_IEEE80211W */
172 };
173 
174 
175 struct wpa_ft_pmk_cache;
176 
177 /* per authenticator data */
178 struct wpa_authenticator {
179 	struct wpa_group *group;
180 
181 	u8 dot11RSNAPMKIDUsed[PMKID_LEN];
182 
183 	struct wpa_auth_config conf;
184 
185 	u8 *wpa_ie;
186 	size_t wpa_ie_len;
187 	struct rsn_pmksa_cache *pmksa;
188 
189 	u8 addr[ETH_ALEN];
190 #ifdef CONFIG_IEEE80211R
191 	struct wpa_ft_pmk_cache *ft_pmk_cache;
192 #endif
193 
194 };
195 
196 
197 int wpa_write_rsn_ie(struct wpa_auth_config *conf, u8 *buf, size_t len,
198 		     const u8 *pmkid);
199 int wpa_write_rsnxe(struct wpa_auth_config *conf, u8 *buf, size_t len);
200 void __wpa_send_eapol(struct wpa_authenticator *wpa_auth,
201 		      struct wpa_state_machine *sm, int key_info,
202 		      const u8 *key_rsc, const u8 *nonce,
203 		      const u8 *kde, size_t kde_len,
204 		      int keyidx, int encr, int force_version);
205 int wpa_auth_for_each_sta(struct wpa_authenticator *wpa_auth,
206 			  int (*cb)(struct wpa_state_machine *sm, void *ctx),
207 			  void *cb_ctx);
208 int wpa_auth_for_each_auth(struct wpa_authenticator *wpa_auth,
209 			   int (*cb)(struct wpa_authenticator *a, void *ctx),
210 			   void *cb_ctx);
211 
212 #ifdef CONFIG_IEEE80211R
213 int wpa_write_mdie(struct wpa_auth_config *conf, u8 *buf, size_t len);
214 int wpa_write_ftie(struct wpa_auth_config *conf, const u8 *r0kh_id,
215 		   size_t r0kh_id_len,
216 		   const u8 *anonce, const u8 *snonce,
217 		   u8 *buf, size_t len, const u8 *subelem,
218 		   size_t subelem_len);
219 int wpa_auth_derive_ptk_ft(struct wpa_state_machine *sm, const u8 *pmk,
220 			   struct wpa_ptk *ptk);
221 struct wpa_ft_pmk_cache * wpa_ft_pmk_cache_init(void);
222 void wpa_ft_pmk_cache_deinit(struct wpa_ft_pmk_cache *cache);
223 void wpa_ft_install_ptk(struct wpa_state_machine *sm);
224 #endif /* CONFIG_IEEE80211R */
225 
226 #endif /* WPA_AUTH_I_H */
227