wps.h 14 KB

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