address.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * Ultra Wide Band
  3. * Address management
  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. */
  25. #include <linux/errno.h>
  26. #include <linux/module.h>
  27. #include <linux/device.h>
  28. #include <linux/random.h>
  29. #include <linux/etherdevice.h>
  30. #include "uwb-internal.h"
  31. /** Device Address Management command */
  32. struct uwb_rc_cmd_dev_addr_mgmt {
  33. struct uwb_rccb rccb;
  34. u8 bmOperationType;
  35. u8 baAddr[6];
  36. } __attribute__((packed));
  37. /**
  38. * Low level command for setting/getting UWB radio's addresses
  39. *
  40. * @hwarc: HWA Radio Control interface instance
  41. * @bmOperationType:
  42. * Set/get, MAC/DEV (see WUSB1.0[8.6.2.2])
  43. * @baAddr: address buffer--assumed to have enough data to hold
  44. * the address type requested.
  45. * @reply: Pointer to reply buffer (can be stack allocated)
  46. * @returns: 0 if ok, < 0 errno code on error.
  47. *
  48. * @cmd has to be allocated because USB cannot grok USB or vmalloc
  49. * buffers depending on your combination of host architecture.
  50. */
  51. static
  52. int uwb_rc_dev_addr_mgmt(struct uwb_rc *rc,
  53. u8 bmOperationType, const u8 *baAddr,
  54. struct uwb_rc_evt_dev_addr_mgmt *reply)
  55. {
  56. int result;
  57. struct uwb_rc_cmd_dev_addr_mgmt *cmd;
  58. result = -ENOMEM;
  59. cmd = kzalloc(sizeof(*cmd), GFP_KERNEL);
  60. if (cmd == NULL)
  61. goto error_kzalloc;
  62. cmd->rccb.bCommandType = UWB_RC_CET_GENERAL;
  63. cmd->rccb.wCommand = cpu_to_le16(UWB_RC_CMD_DEV_ADDR_MGMT);
  64. cmd->bmOperationType = bmOperationType;
  65. if (baAddr) {
  66. size_t size = 0;
  67. switch (bmOperationType >> 1) {
  68. case 0: size = 2; break;
  69. case 1: size = 6; break;
  70. default: BUG();
  71. }
  72. memcpy(cmd->baAddr, baAddr, size);
  73. }
  74. reply->rceb.bEventType = UWB_RC_CET_GENERAL;
  75. reply->rceb.wEvent = UWB_RC_CMD_DEV_ADDR_MGMT;
  76. result = uwb_rc_cmd(rc, "DEV-ADDR-MGMT",
  77. &cmd->rccb, sizeof(*cmd),
  78. &reply->rceb, sizeof(*reply));
  79. if (result < 0)
  80. goto error_cmd;
  81. if (result < sizeof(*reply)) {
  82. dev_err(&rc->uwb_dev.dev,
  83. "DEV-ADDR-MGMT: not enough data replied: "
  84. "%d vs %zu bytes needed\n", result, sizeof(*reply));
  85. result = -ENOMSG;
  86. } else if (reply->bResultCode != UWB_RC_RES_SUCCESS) {
  87. dev_err(&rc->uwb_dev.dev,
  88. "DEV-ADDR-MGMT: command execution failed: %s (%d)\n",
  89. uwb_rc_strerror(reply->bResultCode),
  90. reply->bResultCode);
  91. result = -EIO;
  92. } else
  93. result = 0;
  94. error_cmd:
  95. kfree(cmd);
  96. error_kzalloc:
  97. return result;
  98. }
  99. /**
  100. * Set the UWB RC MAC or device address.
  101. *
  102. * @rc: UWB Radio Controller
  103. * @_addr: Pointer to address to write [assumed to be either a
  104. * 'struct uwb_mac_addr *' or a 'struct uwb_dev_addr *'].
  105. * @type: Type of address to set (UWB_ADDR_DEV or UWB_ADDR_MAC).
  106. * @returns: 0 if ok, < 0 errno code on error.
  107. *
  108. * Some anal retentivity here: even if both 'struct
  109. * uwb_{dev,mac}_addr' have the actual byte array in the same offset
  110. * and I could just pass _addr to hwarc_cmd_dev_addr_mgmt(), I prefer
  111. * to use some syntatic sugar in case someday we decide to change the
  112. * format of the structs. The compiler will optimize it out anyway.
  113. */
  114. static int uwb_rc_addr_set(struct uwb_rc *rc,
  115. const void *_addr, enum uwb_addr_type type)
  116. {
  117. int result;
  118. u8 bmOperationType = 0x1; /* Set address */
  119. const struct uwb_dev_addr *dev_addr = _addr;
  120. const struct uwb_mac_addr *mac_addr = _addr;
  121. struct uwb_rc_evt_dev_addr_mgmt reply;
  122. const u8 *baAddr;
  123. result = -EINVAL;
  124. switch (type) {
  125. case UWB_ADDR_DEV:
  126. baAddr = dev_addr->data;
  127. break;
  128. case UWB_ADDR_MAC:
  129. baAddr = mac_addr->data;
  130. bmOperationType |= 0x2;
  131. break;
  132. default:
  133. return result;
  134. }
  135. return uwb_rc_dev_addr_mgmt(rc, bmOperationType, baAddr, &reply);
  136. }
  137. /**
  138. * Get the UWB radio's MAC or device address.
  139. *
  140. * @rc: UWB Radio Controller
  141. * @_addr: Where to write the address data [assumed to be either a
  142. * 'struct uwb_mac_addr *' or a 'struct uwb_dev_addr *'].
  143. * @type: Type of address to get (UWB_ADDR_DEV or UWB_ADDR_MAC).
  144. * @returns: 0 if ok (and *_addr set), < 0 errno code on error.
  145. *
  146. * See comment in uwb_rc_addr_set() about anal retentivity in the
  147. * type handling of the address variables.
  148. */
  149. static int uwb_rc_addr_get(struct uwb_rc *rc,
  150. void *_addr, enum uwb_addr_type type)
  151. {
  152. int result;
  153. u8 bmOperationType = 0x0; /* Get address */
  154. struct uwb_rc_evt_dev_addr_mgmt evt;
  155. struct uwb_dev_addr *dev_addr = _addr;
  156. struct uwb_mac_addr *mac_addr = _addr;
  157. u8 *baAddr;
  158. result = -EINVAL;
  159. switch (type) {
  160. case UWB_ADDR_DEV:
  161. baAddr = dev_addr->data;
  162. break;
  163. case UWB_ADDR_MAC:
  164. bmOperationType |= 0x2;
  165. baAddr = mac_addr->data;
  166. break;
  167. default:
  168. return result;
  169. }
  170. result = uwb_rc_dev_addr_mgmt(rc, bmOperationType, baAddr, &evt);
  171. if (result == 0)
  172. switch (type) {
  173. case UWB_ADDR_DEV:
  174. memcpy(&dev_addr->data, evt.baAddr,
  175. sizeof(dev_addr->data));
  176. break;
  177. case UWB_ADDR_MAC:
  178. memcpy(&mac_addr->data, evt.baAddr,
  179. sizeof(mac_addr->data));
  180. break;
  181. default: /* shut gcc up */
  182. BUG();
  183. }
  184. return result;
  185. }
  186. /** Get @rc's MAC address to @addr */
  187. int uwb_rc_mac_addr_get(struct uwb_rc *rc,
  188. struct uwb_mac_addr *addr) {
  189. return uwb_rc_addr_get(rc, addr, UWB_ADDR_MAC);
  190. }
  191. EXPORT_SYMBOL_GPL(uwb_rc_mac_addr_get);
  192. /** Get @rc's device address to @addr */
  193. int uwb_rc_dev_addr_get(struct uwb_rc *rc,
  194. struct uwb_dev_addr *addr) {
  195. return uwb_rc_addr_get(rc, addr, UWB_ADDR_DEV);
  196. }
  197. EXPORT_SYMBOL_GPL(uwb_rc_dev_addr_get);
  198. /** Set @rc's address to @addr */
  199. int uwb_rc_mac_addr_set(struct uwb_rc *rc,
  200. const struct uwb_mac_addr *addr)
  201. {
  202. int result = -EINVAL;
  203. mutex_lock(&rc->uwb_dev.mutex);
  204. result = uwb_rc_addr_set(rc, addr, UWB_ADDR_MAC);
  205. mutex_unlock(&rc->uwb_dev.mutex);
  206. return result;
  207. }
  208. /** Set @rc's address to @addr */
  209. int uwb_rc_dev_addr_set(struct uwb_rc *rc,
  210. const struct uwb_dev_addr *addr)
  211. {
  212. int result = -EINVAL;
  213. mutex_lock(&rc->uwb_dev.mutex);
  214. result = uwb_rc_addr_set(rc, addr, UWB_ADDR_DEV);
  215. rc->uwb_dev.dev_addr = *addr;
  216. mutex_unlock(&rc->uwb_dev.mutex);
  217. return result;
  218. }
  219. /* Returns !0 if given address is already assigned to device. */
  220. int __uwb_mac_addr_assigned_check(struct device *dev, void *_addr)
  221. {
  222. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  223. struct uwb_mac_addr *addr = _addr;
  224. if (!uwb_mac_addr_cmp(addr, &uwb_dev->mac_addr))
  225. return !0;
  226. return 0;
  227. }
  228. /* Returns !0 if given address is already assigned to device. */
  229. int __uwb_dev_addr_assigned_check(struct device *dev, void *_addr)
  230. {
  231. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  232. struct uwb_dev_addr *addr = _addr;
  233. if (!uwb_dev_addr_cmp(addr, &uwb_dev->dev_addr))
  234. return !0;
  235. return 0;
  236. }
  237. /**
  238. * uwb_dev_addr_assign - assigned a generated DevAddr to a radio controller
  239. * @rc: the (local) radio controller device requiring a new DevAddr
  240. *
  241. * A new DevAddr is required when:
  242. * - first setting up a radio controller
  243. * - if the hardware reports a DevAddr conflict
  244. *
  245. * The DevAddr is randomly generated in the generated DevAddr range
  246. * [0x100, 0xfeff]. The number of devices in a beacon group is limited
  247. * by mMaxBPLength (96) so this address space will never be exhausted.
  248. *
  249. * [ECMA-368] 17.1.1, 17.16.
  250. */
  251. int uwb_rc_dev_addr_assign(struct uwb_rc *rc)
  252. {
  253. struct uwb_dev_addr new_addr;
  254. do {
  255. get_random_bytes(new_addr.data, sizeof(new_addr.data));
  256. } while (new_addr.data[0] == 0x00 || new_addr.data[0] == 0xff
  257. || __uwb_dev_addr_assigned(rc, &new_addr));
  258. return uwb_rc_dev_addr_set(rc, &new_addr);
  259. }
  260. /**
  261. * uwbd_evt_handle_rc_dev_addr_conflict - handle a DEV_ADDR_CONFLICT event
  262. * @evt: the DEV_ADDR_CONFLICT notification from the radio controller
  263. *
  264. * A new (non-conflicting) DevAddr is assigned to the radio controller.
  265. *
  266. * [ECMA-368] 17.1.1.1.
  267. */
  268. int uwbd_evt_handle_rc_dev_addr_conflict(struct uwb_event *evt)
  269. {
  270. struct uwb_rc *rc = evt->rc;
  271. return uwb_rc_dev_addr_assign(rc);
  272. }
  273. /*
  274. * Print the 48-bit EUI MAC address of the radio controller when
  275. * reading /sys/class/uwb_rc/XX/mac_address
  276. */
  277. static ssize_t uwb_rc_mac_addr_show(struct device *dev,
  278. struct device_attribute *attr, char *buf)
  279. {
  280. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  281. struct uwb_rc *rc = uwb_dev->rc;
  282. struct uwb_mac_addr addr;
  283. ssize_t result;
  284. mutex_lock(&rc->uwb_dev.mutex);
  285. result = uwb_rc_addr_get(rc, &addr, UWB_ADDR_MAC);
  286. mutex_unlock(&rc->uwb_dev.mutex);
  287. if (result >= 0) {
  288. result = uwb_mac_addr_print(buf, UWB_ADDR_STRSIZE, &addr);
  289. buf[result++] = '\n';
  290. }
  291. return result;
  292. }
  293. /*
  294. * Parse a 48 bit address written to /sys/class/uwb_rc/XX/mac_address
  295. * and if correct, set it.
  296. */
  297. static ssize_t uwb_rc_mac_addr_store(struct device *dev,
  298. struct device_attribute *attr,
  299. const char *buf, size_t size)
  300. {
  301. struct uwb_dev *uwb_dev = to_uwb_dev(dev);
  302. struct uwb_rc *rc = uwb_dev->rc;
  303. struct uwb_mac_addr addr;
  304. ssize_t result;
  305. result = sscanf(buf, "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx\n",
  306. &addr.data[0], &addr.data[1], &addr.data[2],
  307. &addr.data[3], &addr.data[4], &addr.data[5]);
  308. if (result != 6) {
  309. result = -EINVAL;
  310. goto out;
  311. }
  312. if (is_multicast_ether_addr(addr.data)) {
  313. dev_err(&rc->uwb_dev.dev, "refusing to set multicast "
  314. "MAC address %s\n", buf);
  315. result = -EINVAL;
  316. goto out;
  317. }
  318. result = uwb_rc_mac_addr_set(rc, &addr);
  319. if (result == 0)
  320. rc->uwb_dev.mac_addr = addr;
  321. out:
  322. return result < 0 ? result : size;
  323. }
  324. DEVICE_ATTR(mac_address, S_IRUGO | S_IWUSR, uwb_rc_mac_addr_show, uwb_rc_mac_addr_store);
  325. /** Print @addr to @buf, @return bytes written */
  326. size_t __uwb_addr_print(char *buf, size_t buf_size, const unsigned char *addr,
  327. int type)
  328. {
  329. size_t result;
  330. if (type)
  331. result = scnprintf(buf, buf_size,
  332. "%02x:%02x:%02x:%02x:%02x:%02x",
  333. addr[0], addr[1], addr[2],
  334. addr[3], addr[4], addr[5]);
  335. else
  336. result = scnprintf(buf, buf_size, "%02x:%02x",
  337. addr[1], addr[0]);
  338. return result;
  339. }
  340. EXPORT_SYMBOL_GPL(__uwb_addr_print);