wusb.h 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Wireless USB Standard Definitions
  4. * Event Size Tables
  5. *
  6. * Copyright (C) 2005-2006 Intel Corporation
  7. * Inaky Perez-Gonzalez <inaky.perez-gonzalez@intel.com>
  8. *
  9. * This program is free software; you can redistribute it and/or
  10. * modify it under the terms of the GNU General Public License version
  11. * 2 as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  21. * 02110-1301, USA.
  22. *
  23. *
  24. * FIXME: docs
  25. * FIXME: organize properly, group logically
  26. *
  27. * All the event structures are defined in uwb/spec.h, as they are
  28. * common to the WHCI and WUSB radio control interfaces.
  29. */
  30. #ifndef __WUSB_H__
  31. #define __WUSB_H__
  32. #include <linux/types.h>
  33. #include <linux/kernel.h>
  34. #include <linux/uwb/spec.h>
  35. #include <linux/usb/ch9.h>
  36. #include <linux/param.h>
  37. /**
  38. * WUSB Information Element header
  39. *
  40. * I don't know why, they decided to make it different to the MBOA MAC
  41. * IE Header; beats me.
  42. */
  43. struct wuie_hdr {
  44. u8 bLength;
  45. u8 bIEIdentifier;
  46. } __attribute__((packed));
  47. enum {
  48. WUIE_ID_WCTA = 0x80,
  49. WUIE_ID_CONNECTACK,
  50. WUIE_ID_HOST_INFO,
  51. WUIE_ID_CHANGE_ANNOUNCE,
  52. WUIE_ID_DEVICE_DISCONNECT,
  53. WUIE_ID_HOST_DISCONNECT,
  54. WUIE_ID_KEEP_ALIVE = 0x89,
  55. WUIE_ID_ISOCH_DISCARD,
  56. WUIE_ID_RESET_DEVICE,
  57. };
  58. /**
  59. * Maximum number of array elements in a WUSB IE.
  60. *
  61. * WUSB1.0[7.5 before table 7-38] says that in WUSB IEs that
  62. * are "arrays" have to limited to 4 elements. So we define it
  63. * like that to ease up and submit only the neeed size.
  64. */
  65. #define WUIE_ELT_MAX 4
  66. /**
  67. * Wrapper for the data that defines a CHID, a CDID or a CK
  68. *
  69. * WUSB defines that CHIDs, CDIDs and CKs are a 16 byte string of
  70. * data. In order to avoid confusion and enforce types, we wrap it.
  71. *
  72. * Make it packed, as we use it in some hw definitions.
  73. */
  74. struct wusb_ckhdid {
  75. u8 data[16];
  76. } __attribute__((packed));
  77. static const struct wusb_ckhdid wusb_ckhdid_zero = { .data = { 0 } };
  78. #define WUSB_CKHDID_STRSIZE (3 * sizeof(struct wusb_ckhdid) + 1)
  79. /**
  80. * WUSB IE: Host Information (WUSB1.0[7.5.2])
  81. *
  82. * Used to provide information about the host to the Wireless USB
  83. * devices in range (CHID can be used as an ASCII string).
  84. */
  85. struct wuie_host_info {
  86. struct wuie_hdr hdr;
  87. __le16 attributes;
  88. struct wusb_ckhdid CHID;
  89. } __attribute__((packed));
  90. /**
  91. * WUSB IE: Connect Ack (WUSB1.0[7.5.1])
  92. *
  93. * Used to acknowledge device connect requests. See note for
  94. * WUIE_ELT_MAX.
  95. */
  96. struct wuie_connect_ack {
  97. struct wuie_hdr hdr;
  98. struct {
  99. struct wusb_ckhdid CDID;
  100. u8 bDeviceAddress; /* 0 means unused */
  101. u8 bReserved;
  102. } blk[WUIE_ELT_MAX];
  103. } __attribute__((packed));
  104. /**
  105. * WUSB IE Host Information Element, Connect Availability
  106. *
  107. * WUSB1.0[7.5.2], bmAttributes description
  108. */
  109. enum {
  110. WUIE_HI_CAP_RECONNECT = 0,
  111. WUIE_HI_CAP_LIMITED,
  112. WUIE_HI_CAP_RESERVED,
  113. WUIE_HI_CAP_ALL,
  114. };
  115. /**
  116. * WUSB IE: Channel Stop (WUSB1.0[7.5.8])
  117. *
  118. * Tells devices the host is going to stop sending MMCs and will disappear.
  119. */
  120. struct wuie_channel_stop {
  121. struct wuie_hdr hdr;
  122. u8 attributes;
  123. u8 timestamp[3];
  124. } __attribute__((packed));
  125. /**
  126. * WUSB IE: Keepalive (WUSB1.0[7.5.9])
  127. *
  128. * Ask device(s) to send keepalives.
  129. */
  130. struct wuie_keep_alive {
  131. struct wuie_hdr hdr;
  132. u8 bDeviceAddress[WUIE_ELT_MAX];
  133. } __attribute__((packed));
  134. /**
  135. * WUSB IE: Reset device (WUSB1.0[7.5.11])
  136. *
  137. * Tell device to reset; in all truth, we can fit 4 CDIDs, but we only
  138. * use it for one at the time...
  139. *
  140. * In any case, this request is a wee bit silly: why don't they target
  141. * by address??
  142. */
  143. struct wuie_reset {
  144. struct wuie_hdr hdr;
  145. struct wusb_ckhdid CDID;
  146. } __attribute__((packed));
  147. /**
  148. * WUSB IE: Disconnect device (WUSB1.0[7.5.11])
  149. *
  150. * Tell device to disconnect; we can fit 4 addresses, but we only use
  151. * it for one at the time...
  152. */
  153. struct wuie_disconnect {
  154. struct wuie_hdr hdr;
  155. u8 bDeviceAddress;
  156. u8 padding;
  157. } __attribute__((packed));
  158. /**
  159. * WUSB IE: Host disconnect ([WUSB] section 7.5.5)
  160. *
  161. * Tells all connected devices to disconnect.
  162. */
  163. struct wuie_host_disconnect {
  164. struct wuie_hdr hdr;
  165. } __attribute__((packed));
  166. /**
  167. * WUSB Device Notification header (WUSB1.0[7.6])
  168. */
  169. struct wusb_dn_hdr {
  170. u8 bType;
  171. u8 notifdata[];
  172. } __attribute__((packed));
  173. /** Device Notification codes (WUSB1.0[Table 7-54]) */
  174. enum WUSB_DN {
  175. WUSB_DN_CONNECT = 0x01,
  176. WUSB_DN_DISCONNECT = 0x02,
  177. WUSB_DN_EPRDY = 0x03,
  178. WUSB_DN_MASAVAILCHANGED = 0x04,
  179. WUSB_DN_RWAKE = 0x05,
  180. WUSB_DN_SLEEP = 0x06,
  181. WUSB_DN_ALIVE = 0x07,
  182. };
  183. /** WUSB Device Notification Connect */
  184. struct wusb_dn_connect {
  185. struct wusb_dn_hdr hdr;
  186. __le16 attributes;
  187. struct wusb_ckhdid CDID;
  188. } __attribute__((packed));
  189. static inline int wusb_dn_connect_prev_dev_addr(const struct wusb_dn_connect *dn)
  190. {
  191. return le16_to_cpu(dn->attributes) & 0xff;
  192. }
  193. static inline int wusb_dn_connect_new_connection(const struct wusb_dn_connect *dn)
  194. {
  195. return (le16_to_cpu(dn->attributes) >> 8) & 0x1;
  196. }
  197. static inline int wusb_dn_connect_beacon_behavior(const struct wusb_dn_connect *dn)
  198. {
  199. return (le16_to_cpu(dn->attributes) >> 9) & 0x03;
  200. }
  201. /** Device is alive (aka: pong) (WUSB1.0[7.6.7]) */
  202. struct wusb_dn_alive {
  203. struct wusb_dn_hdr hdr;
  204. } __attribute__((packed));
  205. /** Device is disconnecting (WUSB1.0[7.6.2]) */
  206. struct wusb_dn_disconnect {
  207. struct wusb_dn_hdr hdr;
  208. } __attribute__((packed));
  209. /* General constants */
  210. enum {
  211. WUSB_TRUST_TIMEOUT_MS = 4000, /* [WUSB] section 4.15.1 */
  212. };
  213. static inline size_t ckhdid_printf(char *pr_ckhdid, size_t size,
  214. const struct wusb_ckhdid *ckhdid)
  215. {
  216. return scnprintf(pr_ckhdid, size,
  217. "%02hx %02hx %02hx %02hx %02hx %02hx %02hx %02hx "
  218. "%02hx %02hx %02hx %02hx %02hx %02hx %02hx %02hx",
  219. ckhdid->data[0], ckhdid->data[1],
  220. ckhdid->data[2], ckhdid->data[3],
  221. ckhdid->data[4], ckhdid->data[5],
  222. ckhdid->data[6], ckhdid->data[7],
  223. ckhdid->data[8], ckhdid->data[9],
  224. ckhdid->data[10], ckhdid->data[11],
  225. ckhdid->data[12], ckhdid->data[13],
  226. ckhdid->data[14], ckhdid->data[15]);
  227. }
  228. /*
  229. * WUSB Crypto stuff (WUSB1.0[6])
  230. */
  231. extern const char *wusb_et_name(u8);
  232. /**
  233. * WUSB key index WUSB1.0[7.3.2.4], for usage when setting keys for
  234. * the host or the device.
  235. */
  236. static inline u8 wusb_key_index(int index, int type, int originator)
  237. {
  238. return (originator << 6) | (type << 4) | index;
  239. }
  240. #define WUSB_KEY_INDEX_TYPE_PTK 0 /* for HWA only */
  241. #define WUSB_KEY_INDEX_TYPE_ASSOC 1
  242. #define WUSB_KEY_INDEX_TYPE_GTK 2
  243. #define WUSB_KEY_INDEX_ORIGINATOR_HOST 0
  244. #define WUSB_KEY_INDEX_ORIGINATOR_DEVICE 1
  245. /* bits 0-3 used for the key index. */
  246. #define WUSB_KEY_INDEX_MAX 15
  247. /* A CCM Nonce, defined in WUSB1.0[6.4.1] */
  248. struct aes_ccm_nonce {
  249. u8 sfn[6]; /* Little Endian */
  250. u8 tkid[3]; /* LE */
  251. struct uwb_dev_addr dest_addr;
  252. struct uwb_dev_addr src_addr;
  253. } __attribute__((packed));
  254. /* A CCM operation label, defined on WUSB1.0[6.5.x] */
  255. struct aes_ccm_label {
  256. u8 data[14];
  257. } __attribute__((packed));
  258. /*
  259. * Input to the key derivation sequence defined in
  260. * WUSB1.0[6.5.1]. Rest of the data is in the CCM Nonce passed to the
  261. * PRF function.
  262. */
  263. struct wusb_keydvt_in {
  264. u8 hnonce[16];
  265. u8 dnonce[16];
  266. } __attribute__((packed));
  267. /*
  268. * Output from the key derivation sequence defined in
  269. * WUSB1.0[6.5.1].
  270. */
  271. struct wusb_keydvt_out {
  272. u8 kck[16];
  273. u8 ptk[16];
  274. } __attribute__((packed));
  275. /* Pseudo Random Function WUSB1.0[6.5] */
  276. extern int wusb_crypto_init(void);
  277. extern void wusb_crypto_exit(void);
  278. extern ssize_t wusb_prf(void *out, size_t out_size,
  279. const u8 key[16], const struct aes_ccm_nonce *_n,
  280. const struct aes_ccm_label *a,
  281. const void *b, size_t blen, size_t len);
  282. static inline int wusb_prf_64(void *out, size_t out_size, const u8 key[16],
  283. const struct aes_ccm_nonce *n,
  284. const struct aes_ccm_label *a,
  285. const void *b, size_t blen)
  286. {
  287. return wusb_prf(out, out_size, key, n, a, b, blen, 64);
  288. }
  289. static inline int wusb_prf_128(void *out, size_t out_size, const u8 key[16],
  290. const struct aes_ccm_nonce *n,
  291. const struct aes_ccm_label *a,
  292. const void *b, size_t blen)
  293. {
  294. return wusb_prf(out, out_size, key, n, a, b, blen, 128);
  295. }
  296. static inline int wusb_prf_256(void *out, size_t out_size, const u8 key[16],
  297. const struct aes_ccm_nonce *n,
  298. const struct aes_ccm_label *a,
  299. const void *b, size_t blen)
  300. {
  301. return wusb_prf(out, out_size, key, n, a, b, blen, 256);
  302. }
  303. /* Key derivation WUSB1.0[6.5.1] */
  304. static inline int wusb_key_derive(struct wusb_keydvt_out *keydvt_out,
  305. const u8 key[16],
  306. const struct aes_ccm_nonce *n,
  307. const struct wusb_keydvt_in *keydvt_in)
  308. {
  309. const struct aes_ccm_label a = { .data = "Pair-wise keys" };
  310. return wusb_prf_256(keydvt_out, sizeof(*keydvt_out), key, n, &a,
  311. keydvt_in, sizeof(*keydvt_in));
  312. }
  313. /*
  314. * Out-of-band MIC Generation WUSB1.0[6.5.2]
  315. *
  316. * Compute the MIC over @key, @n and @hs and place it in @mic_out.
  317. *
  318. * @mic_out: Where to place the 8 byte MIC tag
  319. * @key: KCK from the derivation process
  320. * @n: CCM nonce, n->sfn == 0, TKID as established in the
  321. * process.
  322. * @hs: Handshake struct for phase 2 of the 4-way.
  323. * hs->bStatus and hs->bReserved are zero.
  324. * hs->bMessageNumber is 2 (WUSB1.0[7.3.2.5.2]
  325. * hs->dest_addr is the device's USB address padded with 0
  326. * hs->src_addr is the hosts's UWB device address
  327. * hs->mic is ignored (as we compute that value).
  328. */
  329. static inline int wusb_oob_mic(u8 mic_out[8], const u8 key[16],
  330. const struct aes_ccm_nonce *n,
  331. const struct usb_handshake *hs)
  332. {
  333. const struct aes_ccm_label a = { .data = "out-of-bandMIC" };
  334. return wusb_prf_64(mic_out, 8, key, n, &a,
  335. hs, sizeof(*hs) - sizeof(hs->MIC));
  336. }
  337. #endif /* #ifndef __WUSB_H__ */