wss-lc.c 28 KB

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