wusb.h 9.9 KB

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