wps.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586
  1. /*
  2. * Wi-Fi Protected Setup
  3. * Copyright (c) 2007-2009, 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. struct oob_conf_data {
  85. enum {
  86. OOB_METHOD_UNKNOWN = 0,
  87. OOB_METHOD_DEV_PWD_E,
  88. OOB_METHOD_DEV_PWD_R,
  89. OOB_METHOD_CRED,
  90. } oob_method;
  91. struct wpabuf *dev_password;
  92. struct wpabuf *pubkey_hash;
  93. };
  94. /**
  95. * struct wps_config - WPS configuration for a single registration protocol run
  96. */
  97. struct wps_config {
  98. /**
  99. * wps - Pointer to long term WPS context
  100. */
  101. struct wps_context *wps;
  102. /**
  103. * registrar - Whether this end is a Registrar
  104. */
  105. int registrar;
  106. /**
  107. * pin - Enrollee Device Password (%NULL for Registrar or PBC)
  108. */
  109. const u8 *pin;
  110. /**
  111. * pin_len - Length on pin in octets
  112. */
  113. size_t pin_len;
  114. /**
  115. * pbc - Whether this is protocol run uses PBC
  116. */
  117. int pbc;
  118. /**
  119. * assoc_wps_ie: (Re)AssocReq WPS IE (in AP; %NULL if not AP)
  120. */
  121. const struct wpabuf *assoc_wps_ie;
  122. /**
  123. * new_ap_settings - New AP settings (%NULL if not used)
  124. *
  125. * This parameter provides new AP settings when using a wireless
  126. * stations as a Registrar to configure the AP. %NULL means that AP
  127. * will not be reconfigured, i.e., the station will only learn the
  128. * current AP settings by using AP PIN.
  129. */
  130. const struct wps_credential *new_ap_settings;
  131. };
  132. struct wps_data * wps_init(const struct wps_config *cfg);
  133. void wps_deinit(struct wps_data *data);
  134. /**
  135. * enum wps_process_res - WPS message processing result
  136. */
  137. enum wps_process_res {
  138. /**
  139. * WPS_DONE - Processing done
  140. */
  141. WPS_DONE,
  142. /**
  143. * WPS_CONTINUE - Processing continues
  144. */
  145. WPS_CONTINUE,
  146. /**
  147. * WPS_FAILURE - Processing failed
  148. */
  149. WPS_FAILURE,
  150. /**
  151. * WPS_PENDING - Processing continues, but waiting for an external
  152. * event (e.g., UPnP message from an external Registrar)
  153. */
  154. WPS_PENDING
  155. };
  156. enum wps_process_res wps_process_msg(struct wps_data *wps,
  157. enum wsc_op_code op_code,
  158. const struct wpabuf *msg);
  159. struct wpabuf * wps_get_msg(struct wps_data *wps, enum wsc_op_code *op_code);
  160. int wps_is_selected_pbc_registrar(const struct wpabuf *msg);
  161. int wps_is_selected_pin_registrar(const struct wpabuf *msg);
  162. const u8 * wps_get_uuid_e(const struct wpabuf *msg);
  163. struct wpabuf * wps_build_assoc_req_ie(enum wps_request_type req_type);
  164. struct wpabuf * wps_build_probe_req_ie(int pbc, struct wps_device_data *dev,
  165. const u8 *uuid,
  166. enum wps_request_type req_type);
  167. /**
  168. * struct wps_registrar_config - WPS Registrar configuration
  169. */
  170. struct wps_registrar_config {
  171. /**
  172. * new_psk_cb - Callback for new PSK
  173. * @ctx: Higher layer context data (cb_ctx)
  174. * @mac_addr: MAC address of the Enrollee
  175. * @psk: The new PSK
  176. * @psk_len: The length of psk in octets
  177. * Returns: 0 on success, -1 on failure
  178. *
  179. * This callback is called when a new per-device PSK is provisioned.
  180. */
  181. int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
  182. size_t psk_len);
  183. /**
  184. * set_ie_cb - Callback for WPS IE changes
  185. * @ctx: Higher layer context data (cb_ctx)
  186. * @beacon_ie: WPS IE for Beacon
  187. * @beacon_ie_len: WPS IE length for Beacon
  188. * @probe_resp_ie: WPS IE for Probe Response
  189. * @probe_resp_ie_len: WPS IE length for Probe Response
  190. * Returns: 0 on success, -1 on failure
  191. *
  192. * This callback is called whenever the WPS IE in Beacon or Probe
  193. * Response frames needs to be changed (AP only).
  194. */
  195. int (*set_ie_cb)(void *ctx, const u8 *beacon_ie, size_t beacon_ie_len,
  196. const u8 *probe_resp_ie, size_t probe_resp_ie_len);
  197. /**
  198. * pin_needed_cb - Callback for requesting a PIN
  199. * @ctx: Higher layer context data (cb_ctx)
  200. * @uuid_e: UUID-E of the unknown Enrollee
  201. * @dev: Device Data from the unknown Enrollee
  202. *
  203. * This callback is called whenever an unknown Enrollee requests to use
  204. * PIN method and a matching PIN (Device Password) is not found in
  205. * Registrar data.
  206. */
  207. void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
  208. const struct wps_device_data *dev);
  209. /**
  210. * reg_success_cb - Callback for reporting successful registration
  211. * @ctx: Higher layer context data (cb_ctx)
  212. * @mac_addr: MAC address of the Enrollee
  213. * @uuid_e: UUID-E of the Enrollee
  214. *
  215. * This callback is called whenever an Enrollee completes registration
  216. * successfully.
  217. */
  218. void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
  219. const u8 *uuid_e);
  220. /**
  221. * cb_ctx: Higher layer context data for Registrar callbacks
  222. */
  223. void *cb_ctx;
  224. /**
  225. * skip_cred_build: Do not build credential
  226. *
  227. * This option can be used to disable internal code that builds
  228. * Credential attribute into M8 based on the current network
  229. * configuration and Enrollee capabilities. The extra_cred data will
  230. * then be used as the Credential(s).
  231. */
  232. int skip_cred_build;
  233. /**
  234. * extra_cred: Additional Credential attribute(s)
  235. *
  236. * This optional data (set to %NULL to disable) can be used to add
  237. * Credential attribute(s) for other networks into M8. If
  238. * skip_cred_build is set, this will also override the automatically
  239. * generated Credential attribute.
  240. */
  241. const u8 *extra_cred;
  242. /**
  243. * extra_cred_len: Length of extra_cred in octets
  244. */
  245. size_t extra_cred_len;
  246. /**
  247. * disable_auto_conf - Disable auto-configuration on first registration
  248. *
  249. * By default, the AP that is started in not configured state will
  250. * generate a random PSK and move to configured state when the first
  251. * registration protocol run is completed successfully. This option can
  252. * be used to disable this functionality and leave it up to an external
  253. * program to take care of configuration. This requires the extra_cred
  254. * to be set with a suitable Credential and skip_cred_build being used.
  255. */
  256. int disable_auto_conf;
  257. /**
  258. * static_wep_only - Whether the BSS supports only static WEP
  259. */
  260. int static_wep_only;
  261. };
  262. /**
  263. * enum wps_event - WPS event types
  264. */
  265. enum wps_event {
  266. /**
  267. * WPS_EV_M2D - M2D received (Registrar did not know us)
  268. */
  269. WPS_EV_M2D,
  270. /**
  271. * WPS_EV_FAIL - Registration failed
  272. */
  273. WPS_EV_FAIL,
  274. /**
  275. * WPS_EV_SUCCESS - Registration succeeded
  276. */
  277. WPS_EV_SUCCESS,
  278. /**
  279. * WPS_EV_PWD_AUTH_FAIL - Password authentication failed
  280. */
  281. WPS_EV_PWD_AUTH_FAIL
  282. };
  283. /**
  284. * union wps_event_data - WPS event data
  285. */
  286. union wps_event_data {
  287. /**
  288. * struct wps_event_m2d - M2D event data
  289. */
  290. struct wps_event_m2d {
  291. u16 config_methods;
  292. const u8 *manufacturer;
  293. size_t manufacturer_len;
  294. const u8 *model_name;
  295. size_t model_name_len;
  296. const u8 *model_number;
  297. size_t model_number_len;
  298. const u8 *serial_number;
  299. size_t serial_number_len;
  300. const u8 *dev_name;
  301. size_t dev_name_len;
  302. const u8 *primary_dev_type; /* 8 octets */
  303. u16 config_error;
  304. u16 dev_password_id;
  305. } m2d;
  306. /**
  307. * struct wps_event_fail - Registration failure information
  308. * @msg: enum wps_msg_type
  309. */
  310. struct wps_event_fail {
  311. int msg;
  312. } fail;
  313. struct wps_event_pwd_auth_fail {
  314. int enrollee;
  315. int part;
  316. } pwd_auth_fail;
  317. };
  318. /**
  319. * struct upnp_pending_message - Pending PutWLANResponse messages
  320. * @next: Pointer to next pending message or %NULL
  321. * @addr: NewWLANEventMAC
  322. * @msg: NewMessage
  323. * @type: Message Type
  324. */
  325. struct upnp_pending_message {
  326. struct upnp_pending_message *next;
  327. u8 addr[ETH_ALEN];
  328. struct wpabuf *msg;
  329. enum wps_msg_type type;
  330. };
  331. /**
  332. * struct wps_context - Long term WPS context data
  333. *
  334. * This data is stored at the higher layer Authenticator or Supplicant data
  335. * structures and it is maintained over multiple registration protocol runs.
  336. */
  337. struct wps_context {
  338. /**
  339. * ap - Whether the local end is an access point
  340. */
  341. int ap;
  342. /**
  343. * registrar - Pointer to WPS registrar data from wps_registrar_init()
  344. */
  345. struct wps_registrar *registrar;
  346. /**
  347. * wps_state - Current WPS state
  348. */
  349. enum wps_state wps_state;
  350. /**
  351. * ap_setup_locked - Whether AP setup is locked (only used at AP)
  352. */
  353. int ap_setup_locked;
  354. /**
  355. * uuid - Own UUID
  356. */
  357. u8 uuid[16];
  358. /**
  359. * ssid - SSID
  360. *
  361. * This SSID is used by the Registrar to fill in information for
  362. * Credentials. In addition, AP uses it when acting as an Enrollee to
  363. * notify Registrar of the current configuration.
  364. */
  365. u8 ssid[32];
  366. /**
  367. * ssid_len - Length of ssid in octets
  368. */
  369. size_t ssid_len;
  370. /**
  371. * dev - Own WPS device data
  372. */
  373. struct wps_device_data dev;
  374. /**
  375. * oob_conf - OOB Config data
  376. */
  377. struct oob_conf_data oob_conf;
  378. /**
  379. * oob_dev_pw_id - OOB Device password id
  380. */
  381. u16 oob_dev_pw_id;
  382. /**
  383. * dh_privkey - Diffie-Hellman private key
  384. */
  385. struct wpabuf *dh_privkey;
  386. /**
  387. * dh_pubkey_oob - Diffie-Hellman public key
  388. */
  389. struct wpabuf *dh_pubkey;
  390. /**
  391. * config_methods - Enabled configuration methods
  392. *
  393. * Bit field of WPS_CONFIG_*
  394. */
  395. u16 config_methods;
  396. /**
  397. * encr_types - Enabled encryption types (bit field of WPS_ENCR_*)
  398. */
  399. u16 encr_types;
  400. /**
  401. * auth_types - Authentication types (bit field of WPS_AUTH_*)
  402. */
  403. u16 auth_types;
  404. /**
  405. * network_key - The current Network Key (PSK) or %NULL to generate new
  406. *
  407. * If %NULL, Registrar will generate per-device PSK. In addition, AP
  408. * uses this when acting as an Enrollee to notify Registrar of the
  409. * current configuration.
  410. */
  411. u8 *network_key;
  412. /**
  413. * network_key_len - Length of network_key in octets
  414. */
  415. size_t network_key_len;
  416. /**
  417. * ap_settings - AP Settings override for M7 (only used at AP)
  418. *
  419. * If %NULL, AP Settings attributes will be generated based on the
  420. * current network configuration.
  421. */
  422. u8 *ap_settings;
  423. /**
  424. * ap_settings_len - Length of ap_settings in octets
  425. */
  426. size_t ap_settings_len;
  427. /**
  428. * friendly_name - Friendly Name (required for UPnP)
  429. */
  430. char *friendly_name;
  431. /**
  432. * manufacturer_url - Manufacturer URL (optional for UPnP)
  433. */
  434. char *manufacturer_url;
  435. /**
  436. * model_description - Model Description (recommended for UPnP)
  437. */
  438. char *model_description;
  439. /**
  440. * model_url - Model URL (optional for UPnP)
  441. */
  442. char *model_url;
  443. /**
  444. * upc - Universal Product Code (optional for UPnP)
  445. */
  446. char *upc;
  447. /**
  448. * cred_cb - Callback to notify that new Credentials were received
  449. * @ctx: Higher layer context data (cb_ctx)
  450. * @cred: The received Credential
  451. * Return: 0 on success, -1 on failure
  452. */
  453. int (*cred_cb)(void *ctx, const struct wps_credential *cred);
  454. /**
  455. * event_cb - Event callback (state information about progress)
  456. * @ctx: Higher layer context data (cb_ctx)
  457. * @event: Event type
  458. * @data: Event data
  459. */
  460. void (*event_cb)(void *ctx, enum wps_event event,
  461. union wps_event_data *data);
  462. /**
  463. * cb_ctx: Higher layer context data for callbacks
  464. */
  465. void *cb_ctx;
  466. struct upnp_wps_device_sm *wps_upnp;
  467. /* Pending messages from UPnP PutWLANResponse */
  468. struct upnp_pending_message *upnp_msgs;
  469. };
  470. struct oob_device_data {
  471. char *device_name;
  472. char *device_path;
  473. void * (*init_func)(struct wps_context *, struct oob_device_data *,
  474. int);
  475. struct wpabuf * (*read_func)(void *);
  476. int (*write_func)(void *, struct wpabuf *);
  477. void (*deinit_func)(void *);
  478. };
  479. struct oob_nfc_device_data {
  480. int (*init_func)(char *);
  481. void * (*read_func)(size_t *);
  482. int (*write_func)(void *, size_t);
  483. void (*deinit_func)(void);
  484. };
  485. struct wps_registrar *
  486. wps_registrar_init(struct wps_context *wps,
  487. const struct wps_registrar_config *cfg);
  488. void wps_registrar_deinit(struct wps_registrar *reg);
  489. int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
  490. const u8 *pin, size_t pin_len, int timeout);
  491. int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid);
  492. int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid);
  493. int wps_registrar_button_pushed(struct wps_registrar *reg);
  494. void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
  495. const struct wpabuf *wps_data);
  496. int wps_registrar_update_ie(struct wps_registrar *reg);
  497. int wps_registrar_set_selected_registrar(struct wps_registrar *reg,
  498. const struct wpabuf *msg);
  499. unsigned int wps_pin_checksum(unsigned int pin);
  500. unsigned int wps_pin_valid(unsigned int pin);
  501. unsigned int wps_generate_pin(void);
  502. void wps_free_pending_msgs(struct upnp_pending_message *msgs);
  503. struct oob_device_data * wps_get_oob_device(char *device_type);
  504. struct oob_nfc_device_data * wps_get_oob_nfc_device(char *device_name);
  505. int wps_get_oob_method(char *method);
  506. int wps_process_oob(struct wps_context *wps, struct oob_device_data *oob_dev,
  507. int registrar);
  508. #endif /* WPS_H */