wps.h 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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. struct wps_er;
  32. /**
  33. * struct wps_credential - WPS Credential
  34. * @ssid: SSID
  35. * @ssid_len: Length of SSID
  36. * @auth_type: Authentication Type (WPS_AUTH_OPEN, .. flags)
  37. * @encr_type: Encryption Type (WPS_ENCR_NONE, .. flags)
  38. * @key_idx: Key index
  39. * @key: Key
  40. * @key_len: Key length in octets
  41. * @mac_addr: MAC address of the Credential receiver
  42. * @cred_attr: Unparsed Credential attribute data (used only in cred_cb());
  43. * this may be %NULL, if not used
  44. * @cred_attr_len: Length of cred_attr in octets
  45. */
  46. struct wps_credential {
  47. u8 ssid[32];
  48. size_t ssid_len;
  49. u16 auth_type;
  50. u16 encr_type;
  51. u8 key_idx;
  52. u8 key[64];
  53. size_t key_len;
  54. u8 mac_addr[ETH_ALEN];
  55. const u8 *cred_attr;
  56. size_t cred_attr_len;
  57. };
  58. /**
  59. * struct wps_device_data - WPS Device Data
  60. * @mac_addr: Device MAC address
  61. * @device_name: Device Name (0..32 octets encoded in UTF-8)
  62. * @manufacturer: Manufacturer (0..64 octets encoded in UTF-8)
  63. * @model_name: Model Name (0..32 octets encoded in UTF-8)
  64. * @model_number: Model Number (0..32 octets encoded in UTF-8)
  65. * @serial_number: Serial Number (0..32 octets encoded in UTF-8)
  66. * @categ: Primary Device Category
  67. * @oui: Primary Device OUI
  68. * @sub_categ: Primary Device Sub-Category
  69. * @os_version: OS Version
  70. * @rf_bands: RF bands (WPS_RF_24GHZ, WPS_RF_50GHZ flags)
  71. */
  72. struct wps_device_data {
  73. u8 mac_addr[ETH_ALEN];
  74. char *device_name;
  75. char *manufacturer;
  76. char *model_name;
  77. char *model_number;
  78. char *serial_number;
  79. u16 categ;
  80. u32 oui;
  81. u16 sub_categ;
  82. u32 os_version;
  83. u8 rf_bands;
  84. };
  85. struct oob_conf_data {
  86. enum {
  87. OOB_METHOD_UNKNOWN = 0,
  88. OOB_METHOD_DEV_PWD_E,
  89. OOB_METHOD_DEV_PWD_R,
  90. OOB_METHOD_CRED,
  91. } oob_method;
  92. struct wpabuf *dev_password;
  93. struct wpabuf *pubkey_hash;
  94. };
  95. /**
  96. * struct wps_config - WPS configuration for a single registration protocol run
  97. */
  98. struct wps_config {
  99. /**
  100. * wps - Pointer to long term WPS context
  101. */
  102. struct wps_context *wps;
  103. /**
  104. * registrar - Whether this end is a Registrar
  105. */
  106. int registrar;
  107. /**
  108. * pin - Enrollee Device Password (%NULL for Registrar or PBC)
  109. */
  110. const u8 *pin;
  111. /**
  112. * pin_len - Length on pin in octets
  113. */
  114. size_t pin_len;
  115. /**
  116. * pbc - Whether this is protocol run uses PBC
  117. */
  118. int pbc;
  119. /**
  120. * assoc_wps_ie: (Re)AssocReq WPS IE (in AP; %NULL if not AP)
  121. */
  122. const struct wpabuf *assoc_wps_ie;
  123. /**
  124. * new_ap_settings - New AP settings (%NULL if not used)
  125. *
  126. * This parameter provides new AP settings when using a wireless
  127. * stations as a Registrar to configure the AP. %NULL means that AP
  128. * will not be reconfigured, i.e., the station will only learn the
  129. * current AP settings by using AP PIN.
  130. */
  131. const struct wps_credential *new_ap_settings;
  132. /**
  133. * peer_addr: MAC address of the peer in AP; %NULL if not AP
  134. */
  135. const u8 *peer_addr;
  136. };
  137. struct wps_data * wps_init(const struct wps_config *cfg);
  138. void wps_deinit(struct wps_data *data);
  139. /**
  140. * enum wps_process_res - WPS message processing result
  141. */
  142. enum wps_process_res {
  143. /**
  144. * WPS_DONE - Processing done
  145. */
  146. WPS_DONE,
  147. /**
  148. * WPS_CONTINUE - Processing continues
  149. */
  150. WPS_CONTINUE,
  151. /**
  152. * WPS_FAILURE - Processing failed
  153. */
  154. WPS_FAILURE,
  155. /**
  156. * WPS_PENDING - Processing continues, but waiting for an external
  157. * event (e.g., UPnP message from an external Registrar)
  158. */
  159. WPS_PENDING
  160. };
  161. enum wps_process_res wps_process_msg(struct wps_data *wps,
  162. enum wsc_op_code op_code,
  163. const struct wpabuf *msg);
  164. struct wpabuf * wps_get_msg(struct wps_data *wps, enum wsc_op_code *op_code);
  165. int wps_is_selected_pbc_registrar(const struct wpabuf *msg);
  166. int wps_is_selected_pin_registrar(const struct wpabuf *msg);
  167. const u8 * wps_get_uuid_e(const struct wpabuf *msg);
  168. struct wpabuf * wps_build_assoc_req_ie(enum wps_request_type req_type);
  169. struct wpabuf * wps_build_probe_req_ie(int pbc, struct wps_device_data *dev,
  170. const u8 *uuid,
  171. enum wps_request_type req_type);
  172. /**
  173. * struct wps_registrar_config - WPS Registrar configuration
  174. */
  175. struct wps_registrar_config {
  176. /**
  177. * new_psk_cb - Callback for new PSK
  178. * @ctx: Higher layer context data (cb_ctx)
  179. * @mac_addr: MAC address of the Enrollee
  180. * @psk: The new PSK
  181. * @psk_len: The length of psk in octets
  182. * Returns: 0 on success, -1 on failure
  183. *
  184. * This callback is called when a new per-device PSK is provisioned.
  185. */
  186. int (*new_psk_cb)(void *ctx, const u8 *mac_addr, const u8 *psk,
  187. size_t psk_len);
  188. /**
  189. * set_ie_cb - Callback for WPS IE changes
  190. * @ctx: Higher layer context data (cb_ctx)
  191. * @beacon_ie: WPS IE for Beacon
  192. * @beacon_ie_len: WPS IE length for Beacon
  193. * @probe_resp_ie: WPS IE for Probe Response
  194. * @probe_resp_ie_len: WPS IE length for Probe Response
  195. * Returns: 0 on success, -1 on failure
  196. *
  197. * This callback is called whenever the WPS IE in Beacon or Probe
  198. * Response frames needs to be changed (AP only).
  199. */
  200. int (*set_ie_cb)(void *ctx, const u8 *beacon_ie, size_t beacon_ie_len,
  201. const u8 *probe_resp_ie, size_t probe_resp_ie_len);
  202. /**
  203. * pin_needed_cb - Callback for requesting a PIN
  204. * @ctx: Higher layer context data (cb_ctx)
  205. * @uuid_e: UUID-E of the unknown Enrollee
  206. * @dev: Device Data from the unknown Enrollee
  207. *
  208. * This callback is called whenever an unknown Enrollee requests to use
  209. * PIN method and a matching PIN (Device Password) is not found in
  210. * Registrar data.
  211. */
  212. void (*pin_needed_cb)(void *ctx, const u8 *uuid_e,
  213. const struct wps_device_data *dev);
  214. /**
  215. * reg_success_cb - Callback for reporting successful registration
  216. * @ctx: Higher layer context data (cb_ctx)
  217. * @mac_addr: MAC address of the Enrollee
  218. * @uuid_e: UUID-E of the Enrollee
  219. *
  220. * This callback is called whenever an Enrollee completes registration
  221. * successfully.
  222. */
  223. void (*reg_success_cb)(void *ctx, const u8 *mac_addr,
  224. const u8 *uuid_e);
  225. /**
  226. * set_sel_reg_cb - Callback for reporting selected registrar changes
  227. * @ctx: Higher layer context data (cb_ctx)
  228. * @sel_reg: Whether the Registrar is selected
  229. * @dev_passwd_id: Device Password ID to indicate with method or
  230. * specific password the Registrar intends to use
  231. * @sel_reg_config_methods: Bit field of active config methods
  232. *
  233. * This callback is called whenever the Selected Registrar state
  234. * changes (e.g., a new PIN becomes available or PBC is invoked). This
  235. * callback is only used by External Registrar implementation;
  236. * set_ie_cb() is used by AP implementation in similar caes, but it
  237. * provides the full WPS IE data instead of just the minimal Registrar
  238. * state information.
  239. */
  240. void (*set_sel_reg_cb)(void *ctx, int sel_reg, u16 dev_passwd_id,
  241. u16 sel_reg_config_methods);
  242. /**
  243. * cb_ctx: Higher layer context data for Registrar callbacks
  244. */
  245. void *cb_ctx;
  246. /**
  247. * skip_cred_build: Do not build credential
  248. *
  249. * This option can be used to disable internal code that builds
  250. * Credential attribute into M8 based on the current network
  251. * configuration and Enrollee capabilities. The extra_cred data will
  252. * then be used as the Credential(s).
  253. */
  254. int skip_cred_build;
  255. /**
  256. * extra_cred: Additional Credential attribute(s)
  257. *
  258. * This optional data (set to %NULL to disable) can be used to add
  259. * Credential attribute(s) for other networks into M8. If
  260. * skip_cred_build is set, this will also override the automatically
  261. * generated Credential attribute.
  262. */
  263. const u8 *extra_cred;
  264. /**
  265. * extra_cred_len: Length of extra_cred in octets
  266. */
  267. size_t extra_cred_len;
  268. /**
  269. * disable_auto_conf - Disable auto-configuration on first registration
  270. *
  271. * By default, the AP that is started in not configured state will
  272. * generate a random PSK and move to configured state when the first
  273. * registration protocol run is completed successfully. This option can
  274. * be used to disable this functionality and leave it up to an external
  275. * program to take care of configuration. This requires the extra_cred
  276. * to be set with a suitable Credential and skip_cred_build being used.
  277. */
  278. int disable_auto_conf;
  279. /**
  280. * static_wep_only - Whether the BSS supports only static WEP
  281. */
  282. int static_wep_only;
  283. };
  284. /**
  285. * enum wps_event - WPS event types
  286. */
  287. enum wps_event {
  288. /**
  289. * WPS_EV_M2D - M2D received (Registrar did not know us)
  290. */
  291. WPS_EV_M2D,
  292. /**
  293. * WPS_EV_FAIL - Registration failed
  294. */
  295. WPS_EV_FAIL,
  296. /**
  297. * WPS_EV_SUCCESS - Registration succeeded
  298. */
  299. WPS_EV_SUCCESS,
  300. /**
  301. * WPS_EV_PWD_AUTH_FAIL - Password authentication failed
  302. */
  303. WPS_EV_PWD_AUTH_FAIL,
  304. /**
  305. * WPS_EV_PBC_OVERLAP - PBC session overlap detected
  306. */
  307. WPS_EV_PBC_OVERLAP,
  308. /**
  309. * WPS_EV_PBC_TIMEOUT - PBC walktime expired before protocol run start
  310. */
  311. WPS_EV_PBC_TIMEOUT,
  312. /**
  313. * WPS_EV_ER_AP_ADD - ER: AP added
  314. */
  315. WPS_EV_ER_AP_ADD,
  316. /**
  317. * WPS_EV_ER_AP_REMOVE - ER: AP removed
  318. */
  319. WPS_EV_ER_AP_REMOVE,
  320. /**
  321. * WPS_EV_ER_ENROLLEE_ADD - ER: Enrollee added
  322. */
  323. WPS_EV_ER_ENROLLEE_ADD,
  324. /**
  325. * WPS_EV_ER_ENROLLEE_REMOVE - ER: Enrollee removed
  326. */
  327. WPS_EV_ER_ENROLLEE_REMOVE
  328. };
  329. /**
  330. * union wps_event_data - WPS event data
  331. */
  332. union wps_event_data {
  333. /**
  334. * struct wps_event_m2d - M2D event data
  335. */
  336. struct wps_event_m2d {
  337. u16 config_methods;
  338. const u8 *manufacturer;
  339. size_t manufacturer_len;
  340. const u8 *model_name;
  341. size_t model_name_len;
  342. const u8 *model_number;
  343. size_t model_number_len;
  344. const u8 *serial_number;
  345. size_t serial_number_len;
  346. const u8 *dev_name;
  347. size_t dev_name_len;
  348. const u8 *primary_dev_type; /* 8 octets */
  349. u16 config_error;
  350. u16 dev_password_id;
  351. } m2d;
  352. /**
  353. * struct wps_event_fail - Registration failure information
  354. * @msg: enum wps_msg_type
  355. */
  356. struct wps_event_fail {
  357. int msg;
  358. } fail;
  359. struct wps_event_pwd_auth_fail {
  360. int enrollee;
  361. int part;
  362. } pwd_auth_fail;
  363. struct wps_event_er_ap {
  364. const u8 *uuid;
  365. const char *friendly_name;
  366. const char *manufacturer;
  367. const char *manufacturer_url;
  368. const char *model_description;
  369. const char *model_name;
  370. const char *model_number;
  371. const char *model_url;
  372. const char *serial_number;
  373. const char *upc;
  374. } ap;
  375. struct wps_event_er_enrollee {
  376. const u8 *uuid;
  377. const u8 *mac_addr;
  378. int m1_received;
  379. u16 config_methods;
  380. u16 dev_passwd_id;
  381. const u8 *pri_dev_type;
  382. const char *dev_name;
  383. const char *manufacturer;
  384. const char *model_name;
  385. const char *model_number;
  386. const char *serial_number;
  387. } enrollee;
  388. };
  389. /**
  390. * struct upnp_pending_message - Pending PutWLANResponse messages
  391. * @next: Pointer to next pending message or %NULL
  392. * @addr: NewWLANEventMAC
  393. * @msg: NewMessage
  394. * @type: Message Type
  395. */
  396. struct upnp_pending_message {
  397. struct upnp_pending_message *next;
  398. u8 addr[ETH_ALEN];
  399. struct wpabuf *msg;
  400. enum wps_msg_type type;
  401. };
  402. /**
  403. * struct wps_context - Long term WPS context data
  404. *
  405. * This data is stored at the higher layer Authenticator or Supplicant data
  406. * structures and it is maintained over multiple registration protocol runs.
  407. */
  408. struct wps_context {
  409. /**
  410. * ap - Whether the local end is an access point
  411. */
  412. int ap;
  413. /**
  414. * registrar - Pointer to WPS registrar data from wps_registrar_init()
  415. */
  416. struct wps_registrar *registrar;
  417. /**
  418. * wps_state - Current WPS state
  419. */
  420. enum wps_state wps_state;
  421. /**
  422. * ap_setup_locked - Whether AP setup is locked (only used at AP)
  423. */
  424. int ap_setup_locked;
  425. /**
  426. * uuid - Own UUID
  427. */
  428. u8 uuid[16];
  429. /**
  430. * ssid - SSID
  431. *
  432. * This SSID is used by the Registrar to fill in information for
  433. * Credentials. In addition, AP uses it when acting as an Enrollee to
  434. * notify Registrar of the current configuration.
  435. */
  436. u8 ssid[32];
  437. /**
  438. * ssid_len - Length of ssid in octets
  439. */
  440. size_t ssid_len;
  441. /**
  442. * dev - Own WPS device data
  443. */
  444. struct wps_device_data dev;
  445. /**
  446. * oob_conf - OOB Config data
  447. */
  448. struct oob_conf_data oob_conf;
  449. /**
  450. * oob_dev_pw_id - OOB Device password id
  451. */
  452. u16 oob_dev_pw_id;
  453. /**
  454. * dh_ctx - Context data for Diffie-Hellman operation
  455. */
  456. void *dh_ctx;
  457. /**
  458. * dh_privkey - Diffie-Hellman private key
  459. */
  460. struct wpabuf *dh_privkey;
  461. /**
  462. * dh_pubkey_oob - Diffie-Hellman public key
  463. */
  464. struct wpabuf *dh_pubkey;
  465. /**
  466. * config_methods - Enabled configuration methods
  467. *
  468. * Bit field of WPS_CONFIG_*
  469. */
  470. u16 config_methods;
  471. /**
  472. * encr_types - Enabled encryption types (bit field of WPS_ENCR_*)
  473. */
  474. u16 encr_types;
  475. /**
  476. * auth_types - Authentication types (bit field of WPS_AUTH_*)
  477. */
  478. u16 auth_types;
  479. /**
  480. * network_key - The current Network Key (PSK) or %NULL to generate new
  481. *
  482. * If %NULL, Registrar will generate per-device PSK. In addition, AP
  483. * uses this when acting as an Enrollee to notify Registrar of the
  484. * current configuration.
  485. */
  486. u8 *network_key;
  487. /**
  488. * network_key_len - Length of network_key in octets
  489. */
  490. size_t network_key_len;
  491. /**
  492. * ap_settings - AP Settings override for M7 (only used at AP)
  493. *
  494. * If %NULL, AP Settings attributes will be generated based on the
  495. * current network configuration.
  496. */
  497. u8 *ap_settings;
  498. /**
  499. * ap_settings_len - Length of ap_settings in octets
  500. */
  501. size_t ap_settings_len;
  502. /**
  503. * friendly_name - Friendly Name (required for UPnP)
  504. */
  505. char *friendly_name;
  506. /**
  507. * manufacturer_url - Manufacturer URL (optional for UPnP)
  508. */
  509. char *manufacturer_url;
  510. /**
  511. * model_description - Model Description (recommended for UPnP)
  512. */
  513. char *model_description;
  514. /**
  515. * model_url - Model URL (optional for UPnP)
  516. */
  517. char *model_url;
  518. /**
  519. * upc - Universal Product Code (optional for UPnP)
  520. */
  521. char *upc;
  522. /**
  523. * cred_cb - Callback to notify that new Credentials were received
  524. * @ctx: Higher layer context data (cb_ctx)
  525. * @cred: The received Credential
  526. * Return: 0 on success, -1 on failure
  527. */
  528. int (*cred_cb)(void *ctx, const struct wps_credential *cred);
  529. /**
  530. * event_cb - Event callback (state information about progress)
  531. * @ctx: Higher layer context data (cb_ctx)
  532. * @event: Event type
  533. * @data: Event data
  534. */
  535. void (*event_cb)(void *ctx, enum wps_event event,
  536. union wps_event_data *data);
  537. /**
  538. * cb_ctx: Higher layer context data for callbacks
  539. */
  540. void *cb_ctx;
  541. struct upnp_wps_device_sm *wps_upnp;
  542. /* Pending messages from UPnP PutWLANResponse */
  543. struct upnp_pending_message *upnp_msgs;
  544. };
  545. struct oob_device_data {
  546. char *device_name;
  547. char *device_path;
  548. void * (*init_func)(struct wps_context *, struct oob_device_data *,
  549. int);
  550. struct wpabuf * (*read_func)(void *);
  551. int (*write_func)(void *, struct wpabuf *);
  552. void (*deinit_func)(void *);
  553. };
  554. struct oob_nfc_device_data {
  555. int (*init_func)(char *);
  556. void * (*read_func)(size_t *);
  557. int (*write_func)(void *, size_t);
  558. void (*deinit_func)(void);
  559. };
  560. struct wps_registrar *
  561. wps_registrar_init(struct wps_context *wps,
  562. const struct wps_registrar_config *cfg);
  563. void wps_registrar_deinit(struct wps_registrar *reg);
  564. int wps_registrar_add_pin(struct wps_registrar *reg, const u8 *uuid,
  565. const u8 *pin, size_t pin_len, int timeout);
  566. int wps_registrar_invalidate_pin(struct wps_registrar *reg, const u8 *uuid);
  567. int wps_registrar_unlock_pin(struct wps_registrar *reg, const u8 *uuid);
  568. int wps_registrar_button_pushed(struct wps_registrar *reg);
  569. void wps_registrar_probe_req_rx(struct wps_registrar *reg, const u8 *addr,
  570. const struct wpabuf *wps_data);
  571. int wps_registrar_update_ie(struct wps_registrar *reg);
  572. int wps_registrar_set_selected_registrar(struct wps_registrar *reg,
  573. const struct wpabuf *msg);
  574. int wps_registrar_get_info(struct wps_registrar *reg, const u8 *addr,
  575. char *buf, size_t buflen);
  576. unsigned int wps_pin_checksum(unsigned int pin);
  577. unsigned int wps_pin_valid(unsigned int pin);
  578. unsigned int wps_generate_pin(void);
  579. void wps_free_pending_msgs(struct upnp_pending_message *msgs);
  580. struct oob_device_data * wps_get_oob_device(char *device_type);
  581. struct oob_nfc_device_data * wps_get_oob_nfc_device(char *device_name);
  582. int wps_get_oob_method(char *method);
  583. int wps_process_oob(struct wps_context *wps, struct oob_device_data *oob_dev,
  584. int registrar);
  585. int wps_attr_text(struct wpabuf *data, char *buf, char *end);
  586. struct wps_er * wps_er_init(struct wps_context *wps, const char *ifname);
  587. void wps_er_deinit(struct wps_er *er);
  588. void wps_er_set_sel_reg(struct wps_er *er, int sel_reg, u16 dev_passwd_id,
  589. u16 sel_reg_config_methods);
  590. int wps_er_pbc(struct wps_er *er, const u8 *uuid);
  591. int wps_er_learn(struct wps_er *er, const u8 *uuid, const u8 *pin,
  592. size_t pin_len);
  593. #endif /* WPS_H */