wss-lc.c 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. /*
  2. * WiMedia Logical Link Control Protocol (WLP)
  3. *
  4. * Copyright (C) 2007 Intel Corporation
  5. * Reinette Chatre <reinette.chatre@intel.com>
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License version
  9. * 2 as published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  19. * 02110-1301, USA.
  20. *
  21. *
  22. * Implementation of the WLP association protocol.
  23. *
  24. * FIXME: Docs
  25. *
  26. * A UWB network interface will configure a WSS through wlp_wss_setup() after
  27. * the interface has been assigned a MAC address, typically after
  28. * "ifconfig" has been called. When the interface goes down it should call
  29. * wlp_wss_remove().
  30. *
  31. * When the WSS is ready for use the user interacts via sysfs to create,
  32. * discover, and activate WSS.
  33. *
  34. * wlp_wss_enroll_activate()
  35. *
  36. * wlp_wss_create_activate()
  37. * wlp_wss_set_wssid_hash()
  38. * wlp_wss_comp_wssid_hash()
  39. * wlp_wss_sel_bcast_addr()
  40. * wlp_wss_sysfs_add()
  41. *
  42. * Called when no more references to WSS exist:
  43. * wlp_wss_release()
  44. * wlp_wss_reset()
  45. */
  46. #include <linux/etherdevice.h> /* for is_valid_ether_addr */
  47. #include <linux/skbuff.h>
  48. #include <linux/slab.h>
  49. #include <linux/wlp.h>
  50. #include "wlp-internal.h"
  51. size_t wlp_wss_key_print(char *buf, size_t bufsize, u8 *key)
  52. {
  53. size_t result;
  54. result = scnprintf(buf, bufsize,
  55. "%02x %02x %02x %02x %02x %02x "
  56. "%02x %02x %02x %02x %02x %02x "
  57. "%02x %02x %02x %02x",
  58. key[0], key[1], key[2], key[3],
  59. key[4], key[5], key[6], key[7],
  60. key[8], key[9], key[10], key[11],
  61. key[12], key[13], key[14], key[15]);
  62. return result;
  63. }
  64. /**
  65. * Compute WSSID hash
  66. * WLP Draft 0.99 [7.2.1]
  67. *
  68. * The WSSID hash for a WSSID is the result of an octet-wise exclusive-OR
  69. * of all octets in the WSSID.
  70. */
  71. static
  72. u8 wlp_wss_comp_wssid_hash(struct wlp_uuid *wssid)
  73. {
  74. return wssid->data[0] ^ wssid->data[1] ^ wssid->data[2]
  75. ^ wssid->data[3] ^ wssid->data[4] ^ wssid->data[5]
  76. ^ wssid->data[6] ^ wssid->data[7] ^ wssid->data[8]
  77. ^ wssid->data[9] ^ wssid->data[10] ^ wssid->data[11]
  78. ^ wssid->data[12] ^ wssid->data[13] ^ wssid->data[14]
  79. ^ wssid->data[15];
  80. }
  81. /**
  82. * Select a multicast EUI-48 for the WSS broadcast address.
  83. * WLP Draft 0.99 [7.2.1]
  84. *
  85. * Selected based on the WiMedia Alliance OUI, 00-13-88, within the WLP
  86. * range, [01-13-88-00-01-00, 01-13-88-00-01-FF] inclusive.
  87. *
  88. * This address is currently hardcoded.
  89. * FIXME?
  90. */
  91. static
  92. struct uwb_mac_addr wlp_wss_sel_bcast_addr(struct wlp_wss *wss)
  93. {
  94. struct uwb_mac_addr bcast = {
  95. .data = { 0x01, 0x13, 0x88, 0x00, 0x01, 0x00 }
  96. };
  97. return bcast;
  98. }
  99. /**
  100. * Clear the contents of the WSS structure - all except kobj, mutex, virtual
  101. *
  102. * We do not want to reinitialize - the internal kobj should not change as
  103. * it still points to the parent received during setup. The mutex should
  104. * remain also. We thus just reset values individually.
  105. * The virutal address assigned to WSS will remain the same for the
  106. * lifetime of the WSS. We only reset the fields that can change during its
  107. * lifetime.
  108. */
  109. void wlp_wss_reset(struct wlp_wss *wss)
  110. {
  111. memset(&wss->wssid, 0, sizeof(wss->wssid));
  112. wss->hash = 0;
  113. memset(&wss->name[0], 0, sizeof(wss->name));
  114. memset(&wss->bcast, 0, sizeof(wss->bcast));
  115. wss->secure_status = WLP_WSS_UNSECURE;
  116. memset(&wss->master_key[0], 0, sizeof(wss->master_key));
  117. wss->tag = 0;
  118. wss->state = WLP_WSS_STATE_NONE;
  119. }
  120. /**
  121. * Create sysfs infrastructure for WSS
  122. *
  123. * The WSS is configured to have the interface as parent (see wlp_wss_setup())
  124. * a new sysfs directory that includes wssid as its name is created in the
  125. * interface's sysfs directory. The group of files interacting with WSS are
  126. * created also.
  127. */
  128. static
  129. int wlp_wss_sysfs_add(struct wlp_wss *wss, char *wssid_str)
  130. {
  131. struct wlp *wlp = container_of(wss, struct wlp, wss);
  132. struct device *dev = &wlp->rc->uwb_dev.dev;
  133. int result;
  134. result = kobject_set_name(&wss->kobj, "wss-%s", wssid_str);
  135. if (result < 0)
  136. return result;
  137. wss->kobj.ktype = &wss_ktype;
  138. result = kobject_init_and_add(&wss->kobj,
  139. &wss_ktype, wss->kobj.parent, "wlp");
  140. if (result < 0) {
  141. dev_err(dev, "WLP: Cannot register WSS kobject.\n");
  142. goto error_kobject_register;
  143. }
  144. result = sysfs_create_group(&wss->kobj, &wss_attr_group);
  145. if (result < 0) {
  146. dev_err(dev, "WLP: Cannot register WSS attributes: %d\n",
  147. result);
  148. goto error_sysfs_create_group;
  149. }
  150. return 0;
  151. error_sysfs_create_group:
  152. kobject_put(&wss->kobj); /* will free name if needed */
  153. return result;
  154. error_kobject_register:
  155. kfree(wss->kobj.name);
  156. wss->kobj.name = NULL;
  157. wss->kobj.ktype = NULL;
  158. return result;
  159. }
  160. /**
  161. * Release WSS
  162. *
  163. * No more references exist to this WSS. We should undo everything that was
  164. * done in wlp_wss_create_activate() except removing the group. The group
  165. * is not removed because an object can be unregistered before the group is
  166. * created. We also undo any additional operations on the WSS after this
  167. * (addition of members).
  168. *
  169. * If memory was allocated for the kobject's name then it will
  170. * be freed by the kobject system during this time.
  171. *
  172. * The EDA cache is removed and reinitilized when the WSS is removed. We
  173. * thus loose knowledge of members of this WSS at that time and need not do
  174. * it here.
  175. */
  176. void wlp_wss_release(struct kobject *kobj)
  177. {
  178. struct wlp_wss *wss = container_of(kobj, struct wlp_wss, kobj);
  179. wlp_wss_reset(wss);
  180. }
  181. /**
  182. * Enroll into a WSS using provided neighbor as registrar
  183. *
  184. * First search the neighborhood information to learn which neighbor is
  185. * referred to, next proceed with enrollment.
  186. *
  187. * &wss->mutex is held
  188. */
  189. static
  190. int wlp_wss_enroll_target(struct wlp_wss *wss, struct wlp_uuid *wssid,
  191. struct uwb_dev_addr *dest)
  192. {
  193. struct wlp *wlp = container_of(wss, struct wlp, wss);
  194. struct device *dev = &wlp->rc->uwb_dev.dev;
  195. struct wlp_neighbor_e *neighbor;
  196. int result = -ENXIO;
  197. struct uwb_dev_addr *dev_addr;
  198. mutex_lock(&wlp->nbmutex);
  199. list_for_each_entry(neighbor, &wlp->neighbors, node) {
  200. dev_addr = &neighbor->uwb_dev->dev_addr;
  201. if (!memcmp(dest, dev_addr, sizeof(*dest))) {
  202. result = wlp_enroll_neighbor(wlp, neighbor, wss, wssid);
  203. break;
  204. }
  205. }
  206. if (result == -ENXIO)
  207. dev_err(dev, "WLP: Cannot find neighbor %02x:%02x. \n",
  208. dest->data[1], dest->data[0]);
  209. mutex_unlock(&wlp->nbmutex);
  210. return result;
  211. }
  212. /**
  213. * Enroll into a WSS previously discovered
  214. *
  215. * User provides WSSID of WSS, search for neighbor that has this WSS
  216. * activated and attempt to enroll.
  217. *
  218. * &wss->mutex is held
  219. */
  220. static
  221. int wlp_wss_enroll_discovered(struct wlp_wss *wss, struct wlp_uuid *wssid)
  222. {
  223. struct wlp *wlp = container_of(wss, struct wlp, wss);
  224. struct device *dev = &wlp->rc->uwb_dev.dev;
  225. struct wlp_neighbor_e *neighbor;
  226. struct wlp_wssid_e *wssid_e;
  227. char buf[WLP_WSS_UUID_STRSIZE];
  228. int result = -ENXIO;
  229. mutex_lock(&wlp->nbmutex);
  230. list_for_each_entry(neighbor, &wlp->neighbors, node) {
  231. list_for_each_entry(wssid_e, &neighbor->wssid, node) {
  232. if (!memcmp(wssid, &wssid_e->wssid, sizeof(*wssid))) {
  233. result = wlp_enroll_neighbor(wlp, neighbor,
  234. wss, wssid);
  235. if (result == 0) /* enrollment success */
  236. goto out;
  237. break;
  238. }
  239. }
  240. }
  241. out:
  242. if (result == -ENXIO) {
  243. wlp_wss_uuid_print(buf, sizeof(buf), wssid);
  244. dev_err(dev, "WLP: Cannot find WSSID %s in cache. \n", buf);
  245. }
  246. mutex_unlock(&wlp->nbmutex);
  247. return result;
  248. }
  249. /**
  250. * Enroll into WSS with provided WSSID, registrar may be provided
  251. *
  252. * @wss: out WSS that will be enrolled
  253. * @wssid: wssid of neighboring WSS that we want to enroll in
  254. * @devaddr: registrar can be specified, will be broadcast (ff:ff) if any
  255. * neighbor can be used as registrar.
  256. *
  257. * &wss->mutex is held
  258. */
  259. static
  260. int wlp_wss_enroll(struct wlp_wss *wss, struct wlp_uuid *wssid,
  261. struct uwb_dev_addr *devaddr)
  262. {
  263. int result;
  264. struct wlp *wlp = container_of(wss, struct wlp, wss);
  265. struct device *dev = &wlp->rc->uwb_dev.dev;
  266. char buf[WLP_WSS_UUID_STRSIZE];
  267. struct uwb_dev_addr bcast = {.data = {0xff, 0xff} };
  268. wlp_wss_uuid_print(buf, sizeof(buf), wssid);
  269. if (wss->state != WLP_WSS_STATE_NONE) {
  270. dev_err(dev, "WLP: Already enrolled in WSS %s.\n", buf);
  271. result = -EEXIST;
  272. goto error;
  273. }
  274. if (!memcmp(&bcast, devaddr, sizeof(bcast)))
  275. result = wlp_wss_enroll_discovered(wss, wssid);
  276. else
  277. result = wlp_wss_enroll_target(wss, wssid, devaddr);
  278. if (result < 0) {
  279. dev_err(dev, "WLP: Unable to enroll into WSS %s, result %d \n",
  280. buf, result);
  281. goto error;
  282. }
  283. dev_dbg(dev, "Successfully enrolled into WSS %s \n", buf);
  284. result = wlp_wss_sysfs_add(wss, buf);
  285. if (result < 0) {
  286. dev_err(dev, "WLP: Unable to set up sysfs for WSS kobject.\n");
  287. wlp_wss_reset(wss);
  288. }
  289. error:
  290. return result;
  291. }
  292. /**
  293. * Activate given WSS
  294. *
  295. * Prior to activation a WSS must be enrolled. To activate a WSS a device
  296. * includes the WSS hash in the WLP IE in its beacon in each superframe.
  297. * WLP 0.99 [7.2.5].
  298. *
  299. * The WSS tag is also computed at this time. We only support one activated
  300. * WSS so we can use the hash as a tag - there will never be a conflict.
  301. *
  302. * We currently only support one activated WSS so only one WSS hash is
  303. * included in the WLP IE.
  304. */
  305. static
  306. int wlp_wss_activate(struct wlp_wss *wss)
  307. {
  308. struct wlp *wlp = container_of(wss, struct wlp, wss);
  309. struct device *dev = &wlp->rc->uwb_dev.dev;
  310. struct uwb_rc *uwb_rc = wlp->rc;
  311. int result;
  312. struct {
  313. struct wlp_ie wlp_ie;
  314. u8 hash; /* only include one hash */
  315. } ie_data;
  316. BUG_ON(wss->state != WLP_WSS_STATE_ENROLLED);
  317. wss->hash = wlp_wss_comp_wssid_hash(&wss->wssid);
  318. wss->tag = wss->hash;
  319. memset(&ie_data, 0, sizeof(ie_data));
  320. ie_data.wlp_ie.hdr.element_id = UWB_IE_WLP;
  321. ie_data.wlp_ie.hdr.length = sizeof(ie_data) - sizeof(struct uwb_ie_hdr);
  322. wlp_ie_set_hash_length(&ie_data.wlp_ie, sizeof(ie_data.hash));
  323. ie_data.hash = wss->hash;
  324. result = uwb_rc_ie_add(uwb_rc, &ie_data.wlp_ie.hdr,
  325. sizeof(ie_data));
  326. if (result < 0) {
  327. dev_err(dev, "WLP: Unable to add WLP IE to beacon. "
  328. "result = %d.\n", result);
  329. goto error_wlp_ie;
  330. }
  331. wss->state = WLP_WSS_STATE_ACTIVE;
  332. result = 0;
  333. error_wlp_ie:
  334. return result;
  335. }
  336. /**
  337. * Enroll in and activate WSS identified by provided WSSID
  338. *
  339. * The neighborhood cache should contain a list of all neighbors and the
  340. * WSS they have activated. Based on that cache we search which neighbor we
  341. * can perform the association process with. The user also has option to
  342. * specify which neighbor it prefers as registrar.
  343. * Successful enrollment is followed by activation.
  344. * Successful activation will create the sysfs directory containing
  345. * specific information regarding this WSS.
  346. */
  347. int wlp_wss_enroll_activate(struct wlp_wss *wss, struct wlp_uuid *wssid,
  348. struct uwb_dev_addr *devaddr)
  349. {
  350. struct wlp *wlp = container_of(wss, struct wlp, wss);
  351. struct device *dev = &wlp->rc->uwb_dev.dev;
  352. int result = 0;
  353. char buf[WLP_WSS_UUID_STRSIZE];
  354. mutex_lock(&wss->mutex);
  355. result = wlp_wss_enroll(wss, wssid, devaddr);
  356. if (result < 0) {
  357. wlp_wss_uuid_print(buf, sizeof(buf), &wss->wssid);
  358. dev_err(dev, "WLP: Enrollment into WSS %s failed.\n", buf);
  359. goto error_enroll;
  360. }
  361. result = wlp_wss_activate(wss);
  362. if (result < 0) {
  363. dev_err(dev, "WLP: Unable to activate WSS. Undoing enrollment "
  364. "result = %d \n", result);
  365. /* Undo enrollment */
  366. wlp_wss_reset(wss);
  367. goto error_activate;
  368. }
  369. error_activate:
  370. error_enroll:
  371. mutex_unlock(&wss->mutex);
  372. return result;
  373. }
  374. /**
  375. * Create, enroll, and activate a new WSS
  376. *
  377. * @wssid: new wssid provided by user
  378. * @name: WSS name requested by used.
  379. * @sec_status: security status requested by user
  380. *
  381. * A user requested the creation of a new WSS. All operations are done
  382. * locally. The new WSS will be stored locally, the hash will be included
  383. * in the WLP IE, and the sysfs infrastructure for this WSS will be
  384. * created.
  385. */
  386. int wlp_wss_create_activate(struct wlp_wss *wss, struct wlp_uuid *wssid,
  387. char *name, unsigned sec_status, unsigned accept)
  388. {
  389. struct wlp *wlp = container_of(wss, struct wlp, wss);
  390. struct device *dev = &wlp->rc->uwb_dev.dev;
  391. int result = 0;
  392. char buf[WLP_WSS_UUID_STRSIZE];
  393. result = wlp_wss_uuid_print(buf, sizeof(buf), wssid);
  394. if (!mutex_trylock(&wss->mutex)) {
  395. dev_err(dev, "WLP: WLP association session in progress.\n");
  396. return -EBUSY;
  397. }
  398. if (wss->state != WLP_WSS_STATE_NONE) {
  399. dev_err(dev, "WLP: WSS already exists. Not creating new.\n");
  400. result = -EEXIST;
  401. goto out;
  402. }
  403. if (wss->kobj.parent == NULL) {
  404. dev_err(dev, "WLP: WSS parent not ready. Is network interface "
  405. "up?\n");
  406. result = -ENXIO;
  407. goto out;
  408. }
  409. if (sec_status == WLP_WSS_SECURE) {
  410. dev_err(dev, "WLP: FIXME Creation of secure WSS not "
  411. "supported yet.\n");
  412. result = -EINVAL;
  413. goto out;
  414. }
  415. wss->wssid = *wssid;
  416. memcpy(wss->name, name, sizeof(wss->name));
  417. wss->bcast = wlp_wss_sel_bcast_addr(wss);
  418. wss->secure_status = sec_status;
  419. wss->accept_enroll = accept;
  420. /*wss->virtual_addr is initialized in call to wlp_wss_setup*/
  421. /* sysfs infrastructure */
  422. result = wlp_wss_sysfs_add(wss, buf);
  423. if (result < 0) {
  424. dev_err(dev, "Cannot set up sysfs for WSS kobject.\n");
  425. wlp_wss_reset(wss);
  426. goto out;
  427. } else
  428. result = 0;
  429. wss->state = WLP_WSS_STATE_ENROLLED;
  430. result = wlp_wss_activate(wss);
  431. if (result < 0) {
  432. dev_err(dev, "WLP: Unable to activate WSS. Undoing "
  433. "enrollment\n");
  434. wlp_wss_reset(wss);
  435. goto out;
  436. }
  437. result = 0;
  438. out:
  439. mutex_unlock(&wss->mutex);
  440. return result;
  441. }
  442. /**
  443. * Determine if neighbor has WSS activated
  444. *
  445. * @returns: 1 if neighbor has WSS activated, zero otherwise
  446. *
  447. * This can be done in two ways:
  448. * - send a C1 frame, parse C2/F0 response
  449. * - examine the WLP IE sent by the neighbor
  450. *
  451. * The WLP IE is not fully supported in hardware so we use the C1/C2 frame
  452. * exchange to determine if a WSS is activated. Using the WLP IE should be
  453. * faster and should be used when it becomes possible.
  454. */
  455. int wlp_wss_is_active(struct wlp *wlp, struct wlp_wss *wss,
  456. struct uwb_dev_addr *dev_addr)
  457. {
  458. int result = 0;
  459. struct device *dev = &wlp->rc->uwb_dev.dev;
  460. DECLARE_COMPLETION_ONSTACK(completion);
  461. struct wlp_session session;
  462. struct sk_buff *skb;
  463. struct wlp_frame_assoc *resp;
  464. struct wlp_uuid wssid;
  465. mutex_lock(&wlp->mutex);
  466. /* Send C1 association frame */
  467. result = wlp_send_assoc_frame(wlp, wss, dev_addr, WLP_ASSOC_C1);
  468. if (result < 0) {
  469. dev_err(dev, "Unable to send C1 frame to neighbor "
  470. "%02x:%02x (%d)\n", dev_addr->data[1],
  471. dev_addr->data[0], result);
  472. result = 0;
  473. goto out;
  474. }
  475. /* Create session, wait for response */
  476. session.exp_message = WLP_ASSOC_C2;
  477. session.cb = wlp_session_cb;
  478. session.cb_priv = &completion;
  479. session.neighbor_addr = *dev_addr;
  480. BUG_ON(wlp->session != NULL);
  481. wlp->session = &session;
  482. /* Wait for C2/F0 frame */
  483. result = wait_for_completion_interruptible_timeout(&completion,
  484. WLP_PER_MSG_TIMEOUT * HZ);
  485. if (result == 0) {
  486. dev_err(dev, "Timeout while sending C1 to neighbor "
  487. "%02x:%02x.\n", dev_addr->data[1],
  488. dev_addr->data[0]);
  489. goto out;
  490. }
  491. if (result < 0) {
  492. dev_err(dev, "Unable to send C1 to neighbor %02x:%02x.\n",
  493. dev_addr->data[1], dev_addr->data[0]);
  494. result = 0;
  495. goto out;
  496. }
  497. /* Parse message in session->data: it will be either C2 or F0 */
  498. skb = session.data;
  499. resp = (void *) skb->data;
  500. if (resp->type == WLP_ASSOC_F0) {
  501. result = wlp_parse_f0(wlp, skb);
  502. if (result < 0)
  503. dev_err(dev, "WLP: unable to parse incoming F0 "
  504. "frame from neighbor %02x:%02x.\n",
  505. dev_addr->data[1], dev_addr->data[0]);
  506. result = 0;
  507. goto error_resp_parse;
  508. }
  509. /* WLP version and message type fields have already been parsed */
  510. result = wlp_get_wssid(wlp, (void *)resp + sizeof(*resp), &wssid,
  511. skb->len - sizeof(*resp));
  512. if (result < 0) {
  513. dev_err(dev, "WLP: unable to obtain WSSID from C2 frame.\n");
  514. result = 0;
  515. goto error_resp_parse;
  516. }
  517. if (!memcmp(&wssid, &wss->wssid, sizeof(wssid)))
  518. result = 1;
  519. else {
  520. dev_err(dev, "WLP: Received a C2 frame without matching "
  521. "WSSID.\n");
  522. result = 0;
  523. }
  524. error_resp_parse:
  525. kfree_skb(skb);
  526. out:
  527. wlp->session = NULL;
  528. mutex_unlock(&wlp->mutex);
  529. return result;
  530. }
  531. /**
  532. * Activate connection with neighbor by updating EDA cache
  533. *
  534. * @wss: local WSS to which neighbor wants to connect
  535. * @dev_addr: neighbor's address
  536. * @wssid: neighbor's WSSID - must be same as our WSS's WSSID
  537. * @tag: neighbor's WSS tag used to identify frames transmitted by it
  538. * @virt_addr: neighbor's virtual EUI-48
  539. */
  540. static
  541. int wlp_wss_activate_connection(struct wlp *wlp, struct wlp_wss *wss,
  542. struct uwb_dev_addr *dev_addr,
  543. struct wlp_uuid *wssid, u8 *tag,
  544. struct uwb_mac_addr *virt_addr)
  545. {
  546. struct device *dev = &wlp->rc->uwb_dev.dev;
  547. int result = 0;
  548. if (!memcmp(wssid, &wss->wssid, sizeof(*wssid))) {
  549. /* Update EDA cache */
  550. result = wlp_eda_update_node(&wlp->eda, dev_addr, wss,
  551. (void *) virt_addr->data, *tag,
  552. WLP_WSS_CONNECTED);
  553. if (result < 0)
  554. dev_err(dev, "WLP: Unable to update EDA cache "
  555. "with new connected neighbor information.\n");
  556. } else {
  557. dev_err(dev, "WLP: Neighbor does not have matching WSSID.\n");
  558. result = -EINVAL;
  559. }
  560. return result;
  561. }
  562. /**
  563. * Connect to WSS neighbor
  564. *
  565. * Use C3/C4 exchange to determine if neighbor has WSS activated and
  566. * retrieve the WSS tag and virtual EUI-48 of the neighbor.
  567. */
  568. static
  569. int wlp_wss_connect_neighbor(struct wlp *wlp, struct wlp_wss *wss,
  570. struct uwb_dev_addr *dev_addr)
  571. {
  572. int result;
  573. struct device *dev = &wlp->rc->uwb_dev.dev;
  574. struct wlp_uuid wssid;
  575. u8 tag;
  576. struct uwb_mac_addr virt_addr;
  577. DECLARE_COMPLETION_ONSTACK(completion);
  578. struct wlp_session session;
  579. struct wlp_frame_assoc *resp;
  580. struct sk_buff *skb;
  581. mutex_lock(&wlp->mutex);
  582. /* Send C3 association frame */
  583. result = wlp_send_assoc_frame(wlp, wss, dev_addr, WLP_ASSOC_C3);
  584. if (result < 0) {
  585. dev_err(dev, "Unable to send C3 frame to neighbor "
  586. "%02x:%02x (%d)\n", dev_addr->data[1],
  587. dev_addr->data[0], result);
  588. goto out;
  589. }
  590. /* Create session, wait for response */
  591. session.exp_message = WLP_ASSOC_C4;
  592. session.cb = wlp_session_cb;
  593. session.cb_priv = &completion;
  594. session.neighbor_addr = *dev_addr;
  595. BUG_ON(wlp->session != NULL);
  596. wlp->session = &session;
  597. /* Wait for C4/F0 frame */
  598. result = wait_for_completion_interruptible_timeout(&completion,
  599. WLP_PER_MSG_TIMEOUT * HZ);
  600. if (result == 0) {
  601. dev_err(dev, "Timeout while sending C3 to neighbor "
  602. "%02x:%02x.\n", dev_addr->data[1],
  603. dev_addr->data[0]);
  604. result = -ETIMEDOUT;
  605. goto out;
  606. }
  607. if (result < 0) {
  608. dev_err(dev, "Unable to send C3 to neighbor %02x:%02x.\n",
  609. dev_addr->data[1], dev_addr->data[0]);
  610. goto out;
  611. }
  612. /* Parse message in session->data: it will be either C4 or F0 */
  613. skb = session.data;
  614. resp = (void *) skb->data;
  615. if (resp->type == WLP_ASSOC_F0) {
  616. result = wlp_parse_f0(wlp, skb);
  617. if (result < 0)
  618. dev_err(dev, "WLP: unable to parse incoming F0 "
  619. "frame from neighbor %02x:%02x.\n",
  620. dev_addr->data[1], dev_addr->data[0]);
  621. result = -EINVAL;
  622. goto error_resp_parse;
  623. }
  624. result = wlp_parse_c3c4_frame(wlp, skb, &wssid, &tag, &virt_addr);
  625. if (result < 0) {
  626. dev_err(dev, "WLP: Unable to parse C4 frame from neighbor.\n");
  627. goto error_resp_parse;
  628. }
  629. result = wlp_wss_activate_connection(wlp, wss, dev_addr, &wssid, &tag,
  630. &virt_addr);
  631. if (result < 0) {
  632. dev_err(dev, "WLP: Unable to activate connection to "
  633. "neighbor %02x:%02x.\n", dev_addr->data[1],
  634. dev_addr->data[0]);
  635. goto error_resp_parse;
  636. }
  637. error_resp_parse:
  638. kfree_skb(skb);
  639. out:
  640. /* Record that we unsuccessfully tried to connect to this neighbor */
  641. if (result < 0)
  642. wlp_eda_update_node_state(&wlp->eda, dev_addr,
  643. WLP_WSS_CONNECT_FAILED);
  644. wlp->session = NULL;
  645. mutex_unlock(&wlp->mutex);
  646. return result;
  647. }
  648. /**
  649. * Connect to neighbor with common WSS, send pending frame
  650. *
  651. * This function is scheduled when a frame is destined to a neighbor with
  652. * which we do not have a connection. A copy of the EDA cache entry is
  653. * provided - not the actual cache entry (because it is protected by a
  654. * spinlock).
  655. *
  656. * First determine if neighbor has the same WSS activated, connect if it
  657. * does. The C3/C4 exchange is dual purpose to determine if neighbor has
  658. * WSS activated and proceed with the connection.
  659. *
  660. * The frame that triggered the connection setup is sent after connection
  661. * setup.
  662. *
  663. * network queue is stopped - we need to restart when done
  664. *
  665. */
  666. static
  667. void wlp_wss_connect_send(struct work_struct *ws)
  668. {
  669. struct wlp_assoc_conn_ctx *conn_ctx = container_of(ws,
  670. struct wlp_assoc_conn_ctx,
  671. ws);
  672. struct wlp *wlp = conn_ctx->wlp;
  673. struct sk_buff *skb = conn_ctx->skb;
  674. struct wlp_eda_node *eda_entry = &conn_ctx->eda_entry;
  675. struct uwb_dev_addr *dev_addr = &eda_entry->dev_addr;
  676. struct wlp_wss *wss = &wlp->wss;
  677. int result;
  678. struct device *dev = &wlp->rc->uwb_dev.dev;
  679. mutex_lock(&wss->mutex);
  680. if (wss->state < WLP_WSS_STATE_ACTIVE) {
  681. if (printk_ratelimit())
  682. dev_err(dev, "WLP: Attempting to connect with "
  683. "WSS that is not active or connected.\n");
  684. dev_kfree_skb(skb);
  685. goto out;
  686. }
  687. /* Establish connection - send C3 rcv C4 */
  688. result = wlp_wss_connect_neighbor(wlp, wss, dev_addr);
  689. if (result < 0) {
  690. if (printk_ratelimit())
  691. dev_err(dev, "WLP: Unable to establish connection "
  692. "with neighbor %02x:%02x.\n",
  693. dev_addr->data[1], dev_addr->data[0]);
  694. dev_kfree_skb(skb);
  695. goto out;
  696. }
  697. /* EDA entry changed, update the local copy being used */
  698. result = wlp_copy_eda_node(&wlp->eda, dev_addr, eda_entry);
  699. if (result < 0) {
  700. if (printk_ratelimit())
  701. dev_err(dev, "WLP: Cannot find EDA entry for "
  702. "neighbor %02x:%02x \n",
  703. dev_addr->data[1], dev_addr->data[0]);
  704. }
  705. result = wlp_wss_prep_hdr(wlp, eda_entry, skb);
  706. if (result < 0) {
  707. if (printk_ratelimit())
  708. dev_err(dev, "WLP: Unable to prepare frame header for "
  709. "transmission (neighbor %02x:%02x). \n",
  710. dev_addr->data[1], dev_addr->data[0]);
  711. dev_kfree_skb(skb);
  712. goto out;
  713. }
  714. BUG_ON(wlp->xmit_frame == NULL);
  715. result = wlp->xmit_frame(wlp, skb, dev_addr);
  716. if (result < 0) {
  717. if (printk_ratelimit())
  718. dev_err(dev, "WLP: Unable to transmit frame: %d\n",
  719. result);
  720. if (result == -ENXIO)
  721. dev_err(dev, "WLP: Is network interface up? \n");
  722. /* We could try again ... */
  723. dev_kfree_skb(skb);/*we need to free if tx fails */
  724. }
  725. out:
  726. kfree(conn_ctx);
  727. BUG_ON(wlp->start_queue == NULL);
  728. wlp->start_queue(wlp);
  729. mutex_unlock(&wss->mutex);
  730. }
  731. /**
  732. * Add WLP header to outgoing skb
  733. *
  734. * @eda_entry: pointer to neighbor's entry in the EDA cache
  735. * @_skb: skb containing data destined to the neighbor
  736. */
  737. int wlp_wss_prep_hdr(struct wlp *wlp, struct wlp_eda_node *eda_entry,
  738. void *_skb)
  739. {
  740. struct device *dev = &wlp->rc->uwb_dev.dev;
  741. int result = 0;
  742. unsigned char *eth_addr = eda_entry->eth_addr;
  743. struct uwb_dev_addr *dev_addr = &eda_entry->dev_addr;
  744. struct sk_buff *skb = _skb;
  745. struct wlp_frame_std_abbrv_hdr *std_hdr;
  746. if (eda_entry->state == WLP_WSS_CONNECTED) {
  747. /* Add WLP header */
  748. BUG_ON(skb_headroom(skb) < sizeof(*std_hdr));
  749. std_hdr = (void *) __skb_push(skb, sizeof(*std_hdr));
  750. std_hdr->hdr.mux_hdr = cpu_to_le16(WLP_PROTOCOL_ID);
  751. std_hdr->hdr.type = WLP_FRAME_STANDARD;
  752. std_hdr->tag = eda_entry->wss->tag;
  753. } else {
  754. if (printk_ratelimit())
  755. dev_err(dev, "WLP: Destination neighbor (Ethernet: "
  756. "%02x:%02x:%02x:%02x:%02x:%02x, Dev: "
  757. "%02x:%02x) is not connected. \n", eth_addr[0],
  758. eth_addr[1], eth_addr[2], eth_addr[3],
  759. eth_addr[4], eth_addr[5], dev_addr->data[1],
  760. dev_addr->data[0]);
  761. result = -EINVAL;
  762. }
  763. return result;
  764. }
  765. /**
  766. * Prepare skb for neighbor: connect if not already and prep WLP header
  767. *
  768. * This function is called in interrupt context, but it needs to sleep. We
  769. * temporarily stop the net queue to establish the WLP connection.
  770. * Setup of the WLP connection and restart of queue is scheduled
  771. * on the default work queue.
  772. *
  773. * run with eda->lock held (spinlock)
  774. */
  775. int wlp_wss_connect_prep(struct wlp *wlp, struct wlp_eda_node *eda_entry,
  776. void *_skb)
  777. {
  778. int result = 0;
  779. struct device *dev = &wlp->rc->uwb_dev.dev;
  780. struct sk_buff *skb = _skb;
  781. struct wlp_assoc_conn_ctx *conn_ctx;
  782. if (eda_entry->state == WLP_WSS_UNCONNECTED) {
  783. /* We don't want any more packets while we set up connection */
  784. BUG_ON(wlp->stop_queue == NULL);
  785. wlp->stop_queue(wlp);
  786. conn_ctx = kmalloc(sizeof(*conn_ctx), GFP_ATOMIC);
  787. if (conn_ctx == NULL) {
  788. if (printk_ratelimit())
  789. dev_err(dev, "WLP: Unable to allocate memory "
  790. "for connection handling.\n");
  791. result = -ENOMEM;
  792. goto out;
  793. }
  794. conn_ctx->wlp = wlp;
  795. conn_ctx->skb = skb;
  796. conn_ctx->eda_entry = *eda_entry;
  797. INIT_WORK(&conn_ctx->ws, wlp_wss_connect_send);
  798. schedule_work(&conn_ctx->ws);
  799. result = 1;
  800. } else if (eda_entry->state == WLP_WSS_CONNECT_FAILED) {
  801. /* Previous connection attempts failed, don't retry - see
  802. * conditions for connection in WLP 0.99 [7.6.2] */
  803. if (printk_ratelimit())
  804. dev_err(dev, "Could not connect to neighbor "
  805. "previously. Not retrying. \n");
  806. result = -ENONET;
  807. goto out;
  808. } else /* eda_entry->state == WLP_WSS_CONNECTED */
  809. result = wlp_wss_prep_hdr(wlp, eda_entry, skb);
  810. out:
  811. return result;
  812. }
  813. /**
  814. * Emulate broadcast: copy skb, send copy to neighbor (connect if not already)
  815. *
  816. * We need to copy skbs in the case where we emulate broadcast through
  817. * unicast. We copy instead of clone because we are modifying the data of
  818. * the frame after copying ... clones share data so we cannot emulate
  819. * broadcast using clones.
  820. *
  821. * run with eda->lock held (spinlock)
  822. */
  823. int wlp_wss_send_copy(struct wlp *wlp, struct wlp_eda_node *eda_entry,
  824. void *_skb)
  825. {
  826. int result = -ENOMEM;
  827. struct device *dev = &wlp->rc->uwb_dev.dev;
  828. struct sk_buff *skb = _skb;
  829. struct sk_buff *copy;
  830. struct uwb_dev_addr *dev_addr = &eda_entry->dev_addr;
  831. copy = skb_copy(skb, GFP_ATOMIC);
  832. if (copy == NULL) {
  833. if (printk_ratelimit())
  834. dev_err(dev, "WLP: Unable to copy skb for "
  835. "transmission.\n");
  836. goto out;
  837. }
  838. result = wlp_wss_connect_prep(wlp, eda_entry, copy);
  839. if (result < 0) {
  840. if (printk_ratelimit())
  841. dev_err(dev, "WLP: Unable to connect/send skb "
  842. "to neighbor.\n");
  843. dev_kfree_skb_irq(copy);
  844. goto out;
  845. } else if (result == 1)
  846. /* Frame will be transmitted separately */
  847. goto out;
  848. BUG_ON(wlp->xmit_frame == NULL);
  849. result = wlp->xmit_frame(wlp, copy, dev_addr);
  850. if (result < 0) {
  851. if (printk_ratelimit())
  852. dev_err(dev, "WLP: Unable to transmit frame: %d\n",
  853. result);
  854. if ((result == -ENXIO) && printk_ratelimit())
  855. dev_err(dev, "WLP: Is network interface up? \n");
  856. /* We could try again ... */
  857. dev_kfree_skb_irq(copy);/*we need to free if tx fails */
  858. }
  859. out:
  860. return result;
  861. }
  862. /**
  863. * Setup WSS
  864. *
  865. * Should be called by network driver after the interface has been given a
  866. * MAC address.
  867. */
  868. int wlp_wss_setup(struct net_device *net_dev, struct wlp_wss *wss)
  869. {
  870. struct wlp *wlp = container_of(wss, struct wlp, wss);
  871. struct device *dev = &wlp->rc->uwb_dev.dev;
  872. int result = 0;
  873. mutex_lock(&wss->mutex);
  874. wss->kobj.parent = &net_dev->dev.kobj;
  875. if (!is_valid_ether_addr(net_dev->dev_addr)) {
  876. dev_err(dev, "WLP: Invalid MAC address. Cannot use for"
  877. "virtual.\n");
  878. result = -EINVAL;
  879. goto out;
  880. }
  881. memcpy(wss->virtual_addr.data, net_dev->dev_addr,
  882. sizeof(wss->virtual_addr.data));
  883. out:
  884. mutex_unlock(&wss->mutex);
  885. return result;
  886. }
  887. EXPORT_SYMBOL_GPL(wlp_wss_setup);
  888. /**
  889. * Remove WSS
  890. *
  891. * Called by client that configured WSS through wlp_wss_setup(). This
  892. * function is called when client no longer needs WSS, eg. client shuts
  893. * down.
  894. *
  895. * We remove the WLP IE from the beacon before initiating local cleanup.
  896. */
  897. void wlp_wss_remove(struct wlp_wss *wss)
  898. {
  899. struct wlp *wlp = container_of(wss, struct wlp, wss);
  900. mutex_lock(&wss->mutex);
  901. if (wss->state == WLP_WSS_STATE_ACTIVE)
  902. uwb_rc_ie_rm(wlp->rc, UWB_IE_WLP);
  903. if (wss->state != WLP_WSS_STATE_NONE) {
  904. sysfs_remove_group(&wss->kobj, &wss_attr_group);
  905. kobject_put(&wss->kobj);
  906. }
  907. wss->kobj.parent = NULL;
  908. memset(&wss->virtual_addr, 0, sizeof(wss->virtual_addr));
  909. /* Cleanup EDA cache */
  910. wlp_eda_release(&wlp->eda);
  911. wlp_eda_init(&wlp->eda);
  912. mutex_unlock(&wss->mutex);
  913. }
  914. EXPORT_SYMBOL_GPL(wlp_wss_remove);