wps.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505
  1. /*
  2. * Wi-Fi Protected Setup
  3. * Copyright (c) 2007-2008, Jouni Malinen <j@w1.fi>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. *
  9. * Alternatively, this software may be distributed under the terms of BSD
  10. * license.
  11. *
  12. * See README and COPYING for more details.
  13. */
  14. #ifndef WPS_H
  15. #define WPS_H
  16. #include "wps_defs.h"
  17. /**
  18. * enum wsc_op_code - EAP-WSC OP-Code values
  19. */
  20. enum wsc_op_code {
  21. WSC_UPnP = 0 /* No OP Code in UPnP transport */,
  22. WSC_Start = 0x01,
  23. WSC_ACK = 0x02,
  24. WSC_NACK = 0x03,
  25. WSC_MSG = 0x04,
  26. WSC_Done = 0x05,
  27. WSC_FRAG_ACK = 0x06
  28. };
  29. struct wps_registrar;
  30. struct upnp_wps_device_sm;
  31. /**
  32. * struct wps_credential - WPS Credential
  33. * @ssid: SSID
  34. * @ssid_len: Length of SSID
  35. * @auth_type: Authentication Type (WPS_AUTH_OPEN, .. flags)
  36. * @encr_type: Encryption Type (WPS_ENCR_NONE, .. flags)
  37. * @key_idx: Key index
  38. * @key: Key
  39. * @key_len: Key length in octets
  40. * @mac_addr: MAC address of the peer
  41. * @cred_attr: Unparsed Credential attribute data (used only in cred_cb());
  42. * this may be %NULL, if not used
  43. * @cred_attr_len: Length of cred_attr in octets
  44. */
  45. struct wps_credential {
  46. u8 ssid[32];
  47. size_t ssid_len;
  48. u16 auth_type;
  49. u16 encr_type;
  50. u8 key_idx;
  51. u8 key[64];
  52. size_t key_len;
  53. u8 mac_addr[ETH_ALEN];
  54. const u8 *cred_attr;
  55. size_t cred_attr_len;
  56. };
  57. /**
  58. * struct wps_device_data - WPS Device Data
  59. * @mac_addr: Device MAC address
  60. * @device_name: Device Name (0..32 octets encoded in UTF-8)
  61. * @manufacturer: Manufacturer (0..64 octets encoded in UTF-8)
  62. * @model_name: Model Name (0..32 octets encoded in UTF-8)
  63. * @model_number: Model Number (0..32 octets encoded in UTF-8)
  64. * @serial_number: Serial Number (0..32 octets encoded in UTF-8)
  65. * @categ: Primary Device Category
  66. * @oui: Primary Device OUI
  67. * @sub_categ: Primary Device Sub-Category
  68. * @os_version: OS Version
  69. * @rf_bands: RF bands (WPS_RF_24GHZ, WPS_RF_50GHZ flags)
  70. */
  71. struct wps_device_data {
  72. u8 mac_addr[ETH_ALEN];
  73. char *device_name;
  74. char *manufacturer;
  75. char *model_name;
  76. char *model_number;
  77. char *serial_number;
  78. u16 categ;
  79. u32 oui;
  80. u16 sub_categ;
  81. u32 os_version;
  82. u8 rf_bands;
  83. };
  84. /**
  85. * struct wps_config - WPS configuration for a single registration protocol run
  86. */
  87. struct wps_config {
  88. /**
  89. * wps - Pointer to long term WPS context
  90. */
  91. struct wps_context *wps;
  92. /**
  93. * registrar - Whether this end is a Registrar
  94. */
  95. int registrar;
  96. /**
  97. * pin - Enrollee Device Password (%NULL for Registrar or PBC)
  98. */
  99. const u8 *pin;
  100. /**
  101. * pin_len - Length on pin in octets
  102. */
  103. size_t pin_len;
  104. /**
  105. * pbc - Whether this is protocol run uses PBC
  106. */
  107. int pbc;
  108. /**
  109. * assoc_wps_ie: (Re)AssocReq WPS IE (in AP; %NULL if not AP)
  110. */
  111. const struct wpabuf *assoc_wps_ie;
  112. };
  113. struct wps_data * wps_init(const struct wps_config *cfg);
  114. void wps_deinit(struct wps_data *data);
  115. /**
  116. * enum wps_process_res - WPS message processing result
  117. */
  118. enum wps_process_res {
  119. /**
  120. * WPS_DONE - Processing done
  121. */
  122. WPS_DONE,
  123. /**
  124. * WPS_CONTINUE - Processing continues
  125. */
  126. WPS_CONTINUE,
  127. /**
  128. * WPS_FAILURE - Processing failed
  129. */
  130. WPS_FAILURE,
  131. /**
  132. * WPS_PENDING - Processing continues, but waiting for an external
  133. * event (e.g., UPnP message from an external Registrar)
  134. */
  135. WPS_PENDING
  136. };
  137. enum wps_process_res wps_process_msg(struct wps_data *wps,
  138. enum wsc_op_code op_code,
  139. const struct wpabuf *msg);
  140. struct wpabuf * wps_get_msg(struct wps_data *wps, enum wsc_op_code *op_code);
  141. int wps_is_selected_pbc_registrar(const struct wpabuf *msg);
  142. int wps_is_selected_pin_registrar(const struct wpabuf *msg);
  143. const u8 * wps_get_uuid_e(const struct wpabuf *msg);
  144. struct wpabuf * wps_build_assoc_req_ie(enum wps_request_type req_type);
  145. struct wpabuf * wps_build_probe_req_ie(int pbc, struct wps_device_data *dev,
  146. const u8 *uuid,
  147. enum wps_request_type req_type);
  148. /**
  149. * struct wps_registrar_config - WPS Registrar configuration
  150. */
  151. struct wps_registrar_config {
  152. /**
  153. * new_psk_cb - Callback for new PSK
  154. * @ctx: Higher layer context data (cb_ctx)
  155. * @mac_addr: MAC address of the Enrollee
  156. * @psk: The new PSK
  157. * @psk_len: The length of psk in octets
  158. * Returns: 0 on success, -1 on failure
  159. *
  160. * This callback is called when a new per-device PSK is provisioned.
  161. */
  162. int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
  163. size_t psk_len);
  164. /**
  165. * set_ie_cb - Callback for WPS IE changes
  166. * @ctx: Higher layer context data (cb_ctx)
  167. * @beacon_ie: WPS IE for Beacon
  168. * @beacon_ie_len: WPS IE length for Beacon
  169. * @probe_resp_ie: WPS IE for Probe Response
  170. * @probe_resp_ie_len: WPS IE length for Probe Response
  171. * Returns: 0 on success, -1 on failure
  172. *
  173. * This callback is called whenever the WPS IE in Beacon or Probe
  174. * Response frames needs to be changed (AP only).
  175. */
  176. int (*set_ie_cb)(void *ctx, const u8 *beacon_ie, size_t beacon_ie_len,
  177. const u8 *probe_resp_ie, size_t probe_resp_ie_len);
  178. /**
  179. * pin_needed_cb - Callback for requesting a PIN
  180. * @ctx: Higher layer context data (cb_ctx)
  181. * @uuid_e: UUID-E of the unknown Enrollee
  182. * @dev: Device Data from the unknown Enrollee
  183. *
  184. * This callback is called whenever an unknown Enrollee requests to use
  185. * PIN method and a matching PIN (Device Password) is not found in
  186. * Registrar data.
  187. */
  188. void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
  189. const struct wps_device_data *dev);
  190. /**
  191. * reg_success_cb - Callback for reporting successful registration
  192. * @ctx: Higher layer context data (cb_ctx)
  193. * @mac_addr: MAC address of the Enrollee
  194. * @uuid_e: UUID-E of the Enrollee
  195. *
  196. * This callback is called whenever an Enrollee completes registration
  197. * successfully.
  198. */
  199. void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
  200. const u8 *uuid_e);
  201. /**
  202. * cb_ctx: Higher layer context data for Registrar callbacks
  203. */
  204. void *cb_ctx;
  205. /**
  206. * skip_cred_build: Do not build credential
  207. *
  208. * This option can be used to disable internal code that builds
  209. * Credential attribute into M8 based on the current network
  210. * configuration and Enrollee capabilities. The extra_cred data will
  211. * then be used as the Credential(s).
  212. */
  213. int skip_cred_build;
  214. /**
  215. * extra_cred: Additional Credential attribute(s)
  216. *
  217. * This optional data (set to %NULL to disable) can be used to add
  218. * Credential attribute(s) for other networks into M8. If
  219. * skip_cred_build is set, this will also override the automatically
  220. * generated Credential attribute.
  221. */
  222. const u8 *extra_cred;
  223. /**
  224. * extra_cred_len: Length of extra_cred in octets
  225. */
  226. size_t extra_cred_len;
  227. /**
  228. * disable_auto_conf - Disable auto-configuration on first registration
  229. *
  230. * By default, the AP that is started in not configured state will
  231. * generate a random PSK and move to configured state when the first
  232. * registration protocol run is completed successfully. This option can
  233. * be used to disable this functionality and leave it up to an external
  234. * program to take care of configuration. This requires the extra_cred
  235. * to be set with a suitable Credential and skip_cred_build being used.
  236. */
  237. int disable_auto_conf;
  238. };
  239. /**
  240. * enum wps_event - WPS event types
  241. */
  242. enum wps_event {
  243. /**
  244. * WPS_EV_M2D - M2D received (Registrar did not know us)
  245. */
  246. WPS_EV_M2D,
  247. /**
  248. * WPS_EV_FAIL - Registration failed
  249. */
  250. WPS_EV_FAIL,
  251. /**
  252. * WPS_EV_SUCCESS - Registration succeeded
  253. */
  254. WPS_EV_SUCCESS,
  255. /**
  256. * WPS_EV_PWD_AUTH_FAIL - Password authentication failed
  257. */
  258. WPS_EV_PWD_AUTH_FAIL
  259. };
  260. /**
  261. * union wps_event_data - WPS event data
  262. */
  263. union wps_event_data {
  264. /**
  265. * struct wps_event_m2d - M2D event data
  266. */
  267. struct wps_event_m2d {
  268. u16 config_methods;
  269. const u8 *manufacturer;
  270. size_t manufacturer_len;
  271. const u8 *model_name;
  272. size_t model_name_len;
  273. const u8 *model_number;
  274. size_t model_number_len;
  275. const u8 *serial_number;
  276. size_t serial_number_len;
  277. const u8 *dev_name;
  278. size_t dev_name_len;
  279. const u8 *primary_dev_type; /* 8 octets */
  280. u16 config_error;
  281. u16 dev_password_id;
  282. } m2d;
  283. /**
  284. * struct wps_event_fail - Registration failure information
  285. * @msg: enum wps_msg_type
  286. */
  287. struct wps_event_fail {
  288. int msg;
  289. } fail;
  290. struct wps_event_pwd_auth_fail {
  291. int enrollee;
  292. int part;
  293. } pwd_auth_fail;
  294. };
  295. /**
  296. * struct wps_context - Long term WPS context data
  297. *
  298. * This data is stored at the higher layer Authenticator or Supplicant data
  299. * structures and it is maintained over multiple registration protocol runs.
  300. */
  301. struct wps_context {
  302. /**
  303. * ap - Whether the local end is an access point
  304. */
  305. int ap;
  306. /**
  307. * registrar - Pointer to WPS registrar data from wps_registrar_init()
  308. */
  309. struct wps_registrar *registrar;
  310. /**
  311. * wps_state - Current WPS state
  312. */
  313. enum wps_state wps_state;
  314. /**
  315. * ap_setup_locked - Whether AP setup is locked (only used at AP)
  316. */
  317. int ap_setup_locked;
  318. /**
  319. * uuid - Own UUID
  320. */
  321. u8 uuid[16];
  322. /**
  323. * ssid - SSID
  324. *
  325. * This SSID is used by the Registrar to fill in information for
  326. * Credentials. In addition, AP uses it when acting as an Enrollee to
  327. * notify Registrar of the current configuration.
  328. */
  329. u8 ssid[32];
  330. /**
  331. * ssid_len - Length of ssid in octets
  332. */
  333. size_t ssid_len;
  334. /**
  335. * dev - Own WPS device data
  336. */
  337. struct wps_device_data dev;
  338. /**
  339. * config_methods - Enabled configuration methods
  340. *
  341. * Bit field of WPS_CONFIG_*
  342. */
  343. u16 config_methods;
  344. /**
  345. * encr_types - Enabled encryption types (bit field of WPS_ENCR_*)
  346. */
  347. u16 encr_types;
  348. /**
  349. * auth_types - Authentication types (bit field of WPS_AUTH_*)
  350. */
  351. u16 auth_types;
  352. /**
  353. * network_key - The current Network Key (PSK) or %NULL to generate new
  354. *
  355. * If %NULL, Registrar will generate per-device PSK. In addition, AP
  356. * uses this when acting as an Enrollee to notify Registrar of the
  357. * current configuration.
  358. */
  359. u8 *network_key;
  360. /**
  361. * network_key_len - Length of network_key in octets
  362. */
  363. size_t network_key_len;
  364. /**
  365. * ap_settings - AP Settings override for M7 (only used at AP)
  366. *
  367. * If %NULL, AP Settings attributes will be generated based on the
  368. * current network configuration.
  369. */
  370. u8 *ap_settings;
  371. /**
  372. * ap_settings_len - Length of ap_settings in octets
  373. */
  374. size_t ap_settings_len;
  375. /**
  376. * friendly_name - Friendly Name (required for UPnP)
  377. */
  378. char *friendly_name;
  379. /**
  380. * manufacturer_url - Manufacturer URL (optional for UPnP)
  381. */
  382. char *manufacturer_url;
  383. /**
  384. * model_description - Model Description (recommended for UPnP)
  385. */
  386. char *model_description;
  387. /**
  388. * model_url - Model URL (optional for UPnP)
  389. */
  390. char *model_url;
  391. /**
  392. * upc - Universal Product Code (optional for UPnP)
  393. */
  394. char *upc;
  395. /**
  396. * cred_cb - Callback to notify that new Credentials were received
  397. * @ctx: Higher layer context data (cb_ctx)
  398. * @cred: The received Credential
  399. * Return: 0 on success, -1 on failure
  400. */
  401. int (*cred_cb)(void *ctx, const struct wps_credential *cred);
  402. /**
  403. * event_cb - Event callback (state information about progress)
  404. * @ctx: Higher layer context data (cb_ctx)
  405. * @event: Event type
  406. * @data: Event data
  407. */
  408. void (*event_cb)(void *ctx, enum wps_event event,
  409. union wps_event_data *data);
  410. /**
  411. * cb_ctx: Higher layer context data for callbacks
  412. */
  413. void *cb_ctx;
  414. struct upnp_wps_device_sm *wps_upnp;
  415. /* TODO: support multiple pending messages from UPnP PutWLANResponse */
  416. u8 upnp_msg_addr[ETH_ALEN];
  417. struct wpabuf *upnp_msg;
  418. void *pending_session;
  419. };
  420. struct wps_registrar *
  421. wps_registrar_init(struct wps_context *wps,
  422. const struct wps_registrar_config *cfg);
  423. void wps_registrar_deinit(struct wps_registrar *reg);
  424. int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
  425. const u8 *pin, size_t pin_len);
  426. int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid);
  427. int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid);
  428. int wps_registrar_button_pushed(struct wps_registrar *reg);
  429. void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
  430. const struct wpabuf *wps_data);
  431. int wps_registrar_update_ie(struct wps_registrar *reg);
  432. int wps_registrar_set_selected_registrar(struct wps_registrar *reg,
  433. const struct wpabuf *msg);
  434. unsigned int wps_pin_checksum(unsigned int pin);
  435. unsigned int wps_pin_valid(unsigned int pin);
  436. unsigned int wps_generate_pin(void);
  437. #endif /* WPS_H */