messages.c 54 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781
  1. /*
  2. * WiMedia Logical Link Control Protocol (WLP)
  3. * Message construction and parsing
  4. *
  5. * Copyright (C) 2007 Intel Corporation
  6. * Reinette Chatre <reinette.chatre@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/wlp.h>
  26. #include "wlp-internal.h"
  27. static
  28. const char *__wlp_assoc_frame[] = {
  29. [WLP_ASSOC_D1] = "WLP_ASSOC_D1",
  30. [WLP_ASSOC_D2] = "WLP_ASSOC_D2",
  31. [WLP_ASSOC_M1] = "WLP_ASSOC_M1",
  32. [WLP_ASSOC_M2] = "WLP_ASSOC_M2",
  33. [WLP_ASSOC_M3] = "WLP_ASSOC_M3",
  34. [WLP_ASSOC_M4] = "WLP_ASSOC_M4",
  35. [WLP_ASSOC_M5] = "WLP_ASSOC_M5",
  36. [WLP_ASSOC_M6] = "WLP_ASSOC_M6",
  37. [WLP_ASSOC_M7] = "WLP_ASSOC_M7",
  38. [WLP_ASSOC_M8] = "WLP_ASSOC_M8",
  39. [WLP_ASSOC_F0] = "WLP_ASSOC_F0",
  40. [WLP_ASSOC_E1] = "WLP_ASSOC_E1",
  41. [WLP_ASSOC_E2] = "WLP_ASSOC_E2",
  42. [WLP_ASSOC_C1] = "WLP_ASSOC_C1",
  43. [WLP_ASSOC_C2] = "WLP_ASSOC_C2",
  44. [WLP_ASSOC_C3] = "WLP_ASSOC_C3",
  45. [WLP_ASSOC_C4] = "WLP_ASSOC_C4",
  46. };
  47. static const char *wlp_assoc_frame_str(unsigned id)
  48. {
  49. if (id >= ARRAY_SIZE(__wlp_assoc_frame))
  50. return "unknown association frame";
  51. return __wlp_assoc_frame[id];
  52. }
  53. static const char *__wlp_assc_error[] = {
  54. "none",
  55. "Authenticator Failure",
  56. "Rogue activity suspected",
  57. "Device busy",
  58. "Setup Locked",
  59. "Registrar not ready",
  60. "Invalid WSS selection",
  61. "Message timeout",
  62. "Enrollment session timeout",
  63. "Device password invalid",
  64. "Unsupported version",
  65. "Internal error",
  66. "Undefined error",
  67. "Numeric comparison failure",
  68. "Waiting for user input",
  69. };
  70. static const char *wlp_assc_error_str(unsigned id)
  71. {
  72. if (id >= ARRAY_SIZE(__wlp_assc_error))
  73. return "unknown WLP association error";
  74. return __wlp_assc_error[id];
  75. }
  76. static inline void wlp_set_attr_hdr(struct wlp_attr_hdr *hdr, unsigned type,
  77. size_t len)
  78. {
  79. hdr->type = cpu_to_le16(type);
  80. hdr->length = cpu_to_le16(len);
  81. }
  82. /*
  83. * Populate fields of a constant sized attribute
  84. *
  85. * @returns: total size of attribute including size of new value
  86. *
  87. * We have two instances of this function (wlp_pset and wlp_set): one takes
  88. * the value as a parameter, the other takes a pointer to the value as
  89. * parameter. They thus only differ in how the value is assigned to the
  90. * attribute.
  91. *
  92. * We use sizeof(*attr) - sizeof(struct wlp_attr_hdr) instead of
  93. * sizeof(type) to be able to use this same code for the structures that
  94. * contain 8bit enum values and be able to deal with pointer types.
  95. */
  96. #define wlp_set(type, type_code, name) \
  97. static size_t wlp_set_##name(struct wlp_attr_##name *attr, type value) \
  98. { \
  99. wlp_set_attr_hdr(&attr->hdr, type_code, \
  100. sizeof(*attr) - sizeof(struct wlp_attr_hdr)); \
  101. attr->name = value; \
  102. return sizeof(*attr); \
  103. }
  104. #define wlp_pset(type, type_code, name) \
  105. static size_t wlp_set_##name(struct wlp_attr_##name *attr, type value) \
  106. { \
  107. wlp_set_attr_hdr(&attr->hdr, type_code, \
  108. sizeof(*attr) - sizeof(struct wlp_attr_hdr)); \
  109. attr->name = *value; \
  110. return sizeof(*attr); \
  111. }
  112. /**
  113. * Populate fields of a variable attribute
  114. *
  115. * @returns: total size of attribute including size of new value
  116. *
  117. * Provided with a pointer to the memory area reserved for the
  118. * attribute structure, the field is populated with the value. The
  119. * reserved memory has to contain enough space for the value.
  120. */
  121. #define wlp_vset(type, type_code, name) \
  122. static size_t wlp_set_##name(struct wlp_attr_##name *attr, type value, \
  123. size_t len) \
  124. { \
  125. wlp_set_attr_hdr(&attr->hdr, type_code, len); \
  126. memcpy(attr->name, value, len); \
  127. return sizeof(*attr) + len; \
  128. }
  129. wlp_vset(char *, WLP_ATTR_DEV_NAME, dev_name)
  130. wlp_vset(char *, WLP_ATTR_MANUF, manufacturer)
  131. wlp_set(enum wlp_assoc_type, WLP_ATTR_MSG_TYPE, msg_type)
  132. wlp_vset(char *, WLP_ATTR_MODEL_NAME, model_name)
  133. wlp_vset(char *, WLP_ATTR_MODEL_NR, model_nr)
  134. wlp_vset(char *, WLP_ATTR_SERIAL, serial)
  135. wlp_vset(char *, WLP_ATTR_WSS_NAME, wss_name)
  136. wlp_pset(struct wlp_uuid *, WLP_ATTR_UUID_E, uuid_e)
  137. wlp_pset(struct wlp_uuid *, WLP_ATTR_UUID_R, uuid_r)
  138. wlp_pset(struct wlp_uuid *, WLP_ATTR_WSSID, wssid)
  139. wlp_pset(struct wlp_dev_type *, WLP_ATTR_PRI_DEV_TYPE, prim_dev_type)
  140. /*wlp_pset(struct wlp_dev_type *, WLP_ATTR_SEC_DEV_TYPE, sec_dev_type)*/
  141. wlp_set(u8, WLP_ATTR_WLP_VER, version)
  142. wlp_set(enum wlp_assc_error, WLP_ATTR_WLP_ASSC_ERR, wlp_assc_err)
  143. wlp_set(enum wlp_wss_sel_mthd, WLP_ATTR_WSS_SEL_MTHD, wss_sel_mthd)
  144. wlp_set(u8, WLP_ATTR_ACC_ENRL, accept_enrl)
  145. wlp_set(u8, WLP_ATTR_WSS_SEC_STAT, wss_sec_status)
  146. wlp_pset(struct uwb_mac_addr *, WLP_ATTR_WSS_BCAST, wss_bcast)
  147. wlp_pset(struct wlp_nonce *, WLP_ATTR_ENRL_NONCE, enonce)
  148. wlp_pset(struct wlp_nonce *, WLP_ATTR_REG_NONCE, rnonce)
  149. wlp_set(u8, WLP_ATTR_WSS_TAG, wss_tag)
  150. wlp_pset(struct uwb_mac_addr *, WLP_ATTR_WSS_VIRT, wss_virt)
  151. /**
  152. * Fill in the WSS information attributes
  153. *
  154. * We currently only support one WSS, and this is assumed in this function
  155. * that can populate only one WSS information attribute.
  156. */
  157. static size_t wlp_set_wss_info(struct wlp_attr_wss_info *attr,
  158. struct wlp_wss *wss)
  159. {
  160. size_t datalen;
  161. void *ptr = attr->wss_info;
  162. size_t used = sizeof(*attr);
  163. datalen = sizeof(struct wlp_wss_info) + strlen(wss->name);
  164. wlp_set_attr_hdr(&attr->hdr, WLP_ATTR_WSS_INFO, datalen);
  165. used = wlp_set_wssid(ptr, &wss->wssid);
  166. used += wlp_set_wss_name(ptr + used, wss->name, strlen(wss->name));
  167. used += wlp_set_accept_enrl(ptr + used, wss->accept_enroll);
  168. used += wlp_set_wss_sec_status(ptr + used, wss->secure_status);
  169. used += wlp_set_wss_bcast(ptr + used, &wss->bcast);
  170. return sizeof(*attr) + used;
  171. }
  172. /**
  173. * Verify attribute header
  174. *
  175. * @hdr: Pointer to attribute header that will be verified.
  176. * @type: Expected attribute type.
  177. * @len: Expected length of attribute value (excluding header).
  178. *
  179. * Most attribute values have a known length even when they do have a
  180. * length field. This knowledge can be used via this function to verify
  181. * that the length field matches the expected value.
  182. */
  183. static int wlp_check_attr_hdr(struct wlp *wlp, struct wlp_attr_hdr *hdr,
  184. enum wlp_attr_type type, unsigned len)
  185. {
  186. struct device *dev = &wlp->rc->uwb_dev.dev;
  187. if (le16_to_cpu(hdr->type) != type) {
  188. dev_err(dev, "WLP: unexpected header type. Expected "
  189. "%u, got %u.\n", type, le16_to_cpu(hdr->type));
  190. return -EINVAL;
  191. }
  192. if (le16_to_cpu(hdr->length) != len) {
  193. dev_err(dev, "WLP: unexpected length in header. Expected "
  194. "%u, got %u.\n", len, le16_to_cpu(hdr->length));
  195. return -EINVAL;
  196. }
  197. return 0;
  198. }
  199. /**
  200. * Check if header of WSS information attribute valid
  201. *
  202. * @returns: length of WSS attributes (value of length attribute field) if
  203. * valid WSS information attribute found
  204. * -ENODATA if no WSS information attribute found
  205. * -EIO other error occured
  206. *
  207. * The WSS information attribute is optional. The function will be provided
  208. * with a pointer to data that could _potentially_ be a WSS information
  209. * attribute. If a valid WSS information attribute is found it will return
  210. * 0, if no WSS information attribute is found it will return -ENODATA, and
  211. * another error will be returned if it is a WSS information attribute, but
  212. * some parsing failure occured.
  213. */
  214. static int wlp_check_wss_info_attr_hdr(struct wlp *wlp,
  215. struct wlp_attr_hdr *hdr, size_t buflen)
  216. {
  217. struct device *dev = &wlp->rc->uwb_dev.dev;
  218. size_t len;
  219. int result = 0;
  220. if (buflen < sizeof(*hdr)) {
  221. dev_err(dev, "WLP: Not enough space in buffer to parse"
  222. " WSS information attribute header.\n");
  223. result = -EIO;
  224. goto out;
  225. }
  226. if (le16_to_cpu(hdr->type) != WLP_ATTR_WSS_INFO) {
  227. /* WSS information is optional */
  228. result = -ENODATA;
  229. goto out;
  230. }
  231. len = le16_to_cpu(hdr->length);
  232. if (buflen < sizeof(*hdr) + len) {
  233. dev_err(dev, "WLP: Not enough space in buffer to parse "
  234. "variable data. Got %d, expected %d.\n",
  235. (int)buflen, (int)(sizeof(*hdr) + len));
  236. result = -EIO;
  237. goto out;
  238. }
  239. result = len;
  240. out:
  241. return result;
  242. }
  243. /**
  244. * Get value of attribute from fixed size attribute field.
  245. *
  246. * @attr: Pointer to attribute field.
  247. * @value: Pointer to variable in which attribute value will be placed.
  248. * @buflen: Size of buffer in which attribute field (including header)
  249. * can be found.
  250. * @returns: Amount of given buffer consumed by parsing for this attribute.
  251. *
  252. * The size and type of the value is known by the type of the attribute.
  253. */
  254. #define wlp_get(type, type_code, name) \
  255. ssize_t wlp_get_##name(struct wlp *wlp, struct wlp_attr_##name *attr, \
  256. type *value, ssize_t buflen) \
  257. { \
  258. struct device *dev = &wlp->rc->uwb_dev.dev; \
  259. if (buflen < 0) \
  260. return -EINVAL; \
  261. if (buflen < sizeof(*attr)) { \
  262. dev_err(dev, "WLP: Not enough space in buffer to parse" \
  263. " attribute field. Need %d, received %zu\n", \
  264. (int)sizeof(*attr), buflen); \
  265. return -EIO; \
  266. } \
  267. if (wlp_check_attr_hdr(wlp, &attr->hdr, type_code, \
  268. sizeof(attr->name)) < 0) { \
  269. dev_err(dev, "WLP: Header verification failed. \n"); \
  270. return -EINVAL; \
  271. } \
  272. *value = attr->name; \
  273. return sizeof(*attr); \
  274. }
  275. #define wlp_get_sparse(type, type_code, name) \
  276. static wlp_get(type, type_code, name)
  277. /**
  278. * Get value of attribute from variable sized attribute field.
  279. *
  280. * @max: The maximum size of this attribute. This value is dictated by
  281. * the maximum value from the WLP specification.
  282. *
  283. * @attr: Pointer to attribute field.
  284. * @value: Pointer to variable that will contain the value. The memory
  285. * must already have been allocated for this value.
  286. * @buflen: Size of buffer in which attribute field (including header)
  287. * can be found.
  288. * @returns: Amount of given bufferconsumed by parsing for this attribute.
  289. */
  290. #define wlp_vget(type_val, type_code, name, max) \
  291. static ssize_t wlp_get_##name(struct wlp *wlp, \
  292. struct wlp_attr_##name *attr, \
  293. type_val *value, ssize_t buflen) \
  294. { \
  295. struct device *dev = &wlp->rc->uwb_dev.dev; \
  296. size_t len; \
  297. if (buflen < 0) \
  298. return -EINVAL; \
  299. if (buflen < sizeof(*attr)) { \
  300. dev_err(dev, "WLP: Not enough space in buffer to parse" \
  301. " header.\n"); \
  302. return -EIO; \
  303. } \
  304. if (le16_to_cpu(attr->hdr.type) != type_code) { \
  305. dev_err(dev, "WLP: Unexpected attribute type. Got %u, " \
  306. "expected %u.\n", le16_to_cpu(attr->hdr.type), \
  307. type_code); \
  308. return -EINVAL; \
  309. } \
  310. len = le16_to_cpu(attr->hdr.length); \
  311. if (len > max) { \
  312. dev_err(dev, "WLP: Attribute larger than maximum " \
  313. "allowed. Received %zu, max is %d.\n", len, \
  314. (int)max); \
  315. return -EFBIG; \
  316. } \
  317. if (buflen < sizeof(*attr) + len) { \
  318. dev_err(dev, "WLP: Not enough space in buffer to parse "\
  319. "variable data.\n"); \
  320. return -EIO; \
  321. } \
  322. memcpy(value, (void *) attr + sizeof(*attr), len); \
  323. return sizeof(*attr) + len; \
  324. }
  325. wlp_get(u8, WLP_ATTR_WLP_VER, version)
  326. wlp_get_sparse(enum wlp_wss_sel_mthd, WLP_ATTR_WSS_SEL_MTHD, wss_sel_mthd)
  327. wlp_get_sparse(struct wlp_dev_type, WLP_ATTR_PRI_DEV_TYPE, prim_dev_type)
  328. wlp_get_sparse(enum wlp_assc_error, WLP_ATTR_WLP_ASSC_ERR, wlp_assc_err)
  329. wlp_get_sparse(struct wlp_uuid, WLP_ATTR_UUID_E, uuid_e)
  330. wlp_get_sparse(struct wlp_uuid, WLP_ATTR_UUID_R, uuid_r)
  331. wlp_get(struct wlp_uuid, WLP_ATTR_WSSID, wssid)
  332. wlp_get_sparse(u8, WLP_ATTR_ACC_ENRL, accept_enrl)
  333. wlp_get_sparse(u8, WLP_ATTR_WSS_SEC_STAT, wss_sec_status)
  334. wlp_get_sparse(struct uwb_mac_addr, WLP_ATTR_WSS_BCAST, wss_bcast)
  335. wlp_get_sparse(u8, WLP_ATTR_WSS_TAG, wss_tag)
  336. wlp_get_sparse(struct uwb_mac_addr, WLP_ATTR_WSS_VIRT, wss_virt)
  337. wlp_get_sparse(struct wlp_nonce, WLP_ATTR_ENRL_NONCE, enonce)
  338. wlp_get_sparse(struct wlp_nonce, WLP_ATTR_REG_NONCE, rnonce)
  339. /* The buffers for the device info attributes can be found in the
  340. * wlp_device_info struct. These buffers contain one byte more than the
  341. * max allowed by the spec - this is done to be able to add the
  342. * terminating \0 for user display. This terminating byte is not required
  343. * in the actual attribute field (because it has a length field) so the
  344. * maximum allowed for this value is one less than its size in the
  345. * structure.
  346. */
  347. wlp_vget(char, WLP_ATTR_WSS_NAME, wss_name,
  348. FIELD_SIZEOF(struct wlp_wss, name) - 1)
  349. wlp_vget(char, WLP_ATTR_DEV_NAME, dev_name,
  350. FIELD_SIZEOF(struct wlp_device_info, name) - 1)
  351. wlp_vget(char, WLP_ATTR_MANUF, manufacturer,
  352. FIELD_SIZEOF(struct wlp_device_info, manufacturer) - 1)
  353. wlp_vget(char, WLP_ATTR_MODEL_NAME, model_name,
  354. FIELD_SIZEOF(struct wlp_device_info, model_name) - 1)
  355. wlp_vget(char, WLP_ATTR_MODEL_NR, model_nr,
  356. FIELD_SIZEOF(struct wlp_device_info, model_nr) - 1)
  357. wlp_vget(char, WLP_ATTR_SERIAL, serial,
  358. FIELD_SIZEOF(struct wlp_device_info, serial) - 1)
  359. /**
  360. * Retrieve WSS Name, Accept enroll, Secure status, Broadcast from WSS info
  361. *
  362. * @attr: pointer to WSS name attribute in WSS information attribute field
  363. * @info: structure that will be populated with data from WSS information
  364. * field (WSS name, Accept enroll, secure status, broadcast address)
  365. * @buflen: size of buffer
  366. *
  367. * Although the WSSID attribute forms part of the WSS info attribute it is
  368. * retrieved separately and stored in a different location.
  369. */
  370. static ssize_t wlp_get_wss_info_attrs(struct wlp *wlp,
  371. struct wlp_attr_hdr *attr,
  372. struct wlp_wss_tmp_info *info,
  373. ssize_t buflen)
  374. {
  375. struct device *dev = &wlp->rc->uwb_dev.dev;
  376. void *ptr = attr;
  377. size_t used = 0;
  378. ssize_t result = -EINVAL;
  379. result = wlp_get_wss_name(wlp, ptr, info->name, buflen);
  380. if (result < 0) {
  381. dev_err(dev, "WLP: unable to obtain WSS name from "
  382. "WSS info in D2 message.\n");
  383. goto error_parse;
  384. }
  385. used += result;
  386. result = wlp_get_accept_enrl(wlp, ptr + used, &info->accept_enroll,
  387. buflen - used);
  388. if (result < 0) {
  389. dev_err(dev, "WLP: unable to obtain accepting "
  390. "enrollment from WSS info in D2 message.\n");
  391. goto error_parse;
  392. }
  393. if (info->accept_enroll != 0 && info->accept_enroll != 1) {
  394. dev_err(dev, "WLP: invalid value for accepting "
  395. "enrollment in D2 message.\n");
  396. result = -EINVAL;
  397. goto error_parse;
  398. }
  399. used += result;
  400. result = wlp_get_wss_sec_status(wlp, ptr + used, &info->sec_status,
  401. buflen - used);
  402. if (result < 0) {
  403. dev_err(dev, "WLP: unable to obtain secure "
  404. "status from WSS info in D2 message.\n");
  405. goto error_parse;
  406. }
  407. if (info->sec_status != 0 && info->sec_status != 1) {
  408. dev_err(dev, "WLP: invalid value for secure "
  409. "status in D2 message.\n");
  410. result = -EINVAL;
  411. goto error_parse;
  412. }
  413. used += result;
  414. result = wlp_get_wss_bcast(wlp, ptr + used, &info->bcast,
  415. buflen - used);
  416. if (result < 0) {
  417. dev_err(dev, "WLP: unable to obtain broadcast "
  418. "address from WSS info in D2 message.\n");
  419. goto error_parse;
  420. }
  421. used += result;
  422. result = used;
  423. error_parse:
  424. return result;
  425. }
  426. /**
  427. * Create a new WSSID entry for the neighbor, allocate temporary storage
  428. *
  429. * Each neighbor can have many WSS active. We maintain a list of WSSIDs
  430. * advertised by neighbor. During discovery we also cache information about
  431. * these WSS in temporary storage.
  432. *
  433. * The temporary storage will be removed after it has been used (eg.
  434. * displayed to user), the wssid element will be removed from the list when
  435. * the neighbor is rediscovered or when it disappears.
  436. */
  437. static struct wlp_wssid_e *wlp_create_wssid_e(struct wlp *wlp,
  438. struct wlp_neighbor_e *neighbor)
  439. {
  440. struct device *dev = &wlp->rc->uwb_dev.dev;
  441. struct wlp_wssid_e *wssid_e;
  442. wssid_e = kzalloc(sizeof(*wssid_e), GFP_KERNEL);
  443. if (wssid_e == NULL) {
  444. dev_err(dev, "WLP: unable to allocate memory "
  445. "for WSS information.\n");
  446. goto error_alloc;
  447. }
  448. wssid_e->info = kzalloc(sizeof(struct wlp_wss_tmp_info), GFP_KERNEL);
  449. if (wssid_e->info == NULL) {
  450. dev_err(dev, "WLP: unable to allocate memory "
  451. "for temporary WSS information.\n");
  452. kfree(wssid_e);
  453. wssid_e = NULL;
  454. goto error_alloc;
  455. }
  456. list_add(&wssid_e->node, &neighbor->wssid);
  457. error_alloc:
  458. return wssid_e;
  459. }
  460. /**
  461. * Parse WSS information attribute
  462. *
  463. * @attr: pointer to WSS information attribute header
  464. * @buflen: size of buffer in which WSS information attribute appears
  465. * @wssid: will place wssid from WSS info attribute in this location
  466. * @wss_info: will place other information from WSS information attribute
  467. * in this location
  468. *
  469. * memory for @wssid and @wss_info must be allocated when calling this
  470. */
  471. static ssize_t wlp_get_wss_info(struct wlp *wlp, struct wlp_attr_wss_info *attr,
  472. size_t buflen, struct wlp_uuid *wssid,
  473. struct wlp_wss_tmp_info *wss_info)
  474. {
  475. struct device *dev = &wlp->rc->uwb_dev.dev;
  476. ssize_t result;
  477. size_t len;
  478. size_t used = 0;
  479. void *ptr;
  480. result = wlp_check_wss_info_attr_hdr(wlp, (struct wlp_attr_hdr *)attr,
  481. buflen);
  482. if (result < 0)
  483. goto out;
  484. len = result;
  485. used = sizeof(*attr);
  486. ptr = attr;
  487. result = wlp_get_wssid(wlp, ptr + used, wssid, buflen - used);
  488. if (result < 0) {
  489. dev_err(dev, "WLP: unable to obtain WSSID from WSS info.\n");
  490. goto out;
  491. }
  492. used += result;
  493. result = wlp_get_wss_info_attrs(wlp, ptr + used, wss_info,
  494. buflen - used);
  495. if (result < 0) {
  496. dev_err(dev, "WLP: unable to obtain WSS information "
  497. "from WSS information attributes. \n");
  498. goto out;
  499. }
  500. used += result;
  501. if (len + sizeof(*attr) != used) {
  502. dev_err(dev, "WLP: Amount of data parsed does not "
  503. "match length field. Parsed %zu, length "
  504. "field %zu. \n", used, len);
  505. result = -EINVAL;
  506. goto out;
  507. }
  508. result = used;
  509. out:
  510. return result;
  511. }
  512. /**
  513. * Retrieve WSS info from association frame
  514. *
  515. * @attr: pointer to WSS information attribute
  516. * @neighbor: ptr to neighbor being discovered, NULL if enrollment in
  517. * progress
  518. * @wss: ptr to WSS being enrolled in, NULL if discovery in progress
  519. * @buflen: size of buffer in which WSS information appears
  520. *
  521. * The WSS information attribute appears in the D2 association message.
  522. * This message is used in two ways: to discover all neighbors or to enroll
  523. * into a WSS activated by a neighbor. During discovery we only want to
  524. * store the WSS info in a cache, to be deleted right after it has been
  525. * used (eg. displayed to the user). During enrollment we store the WSS
  526. * information for the lifetime of enrollment.
  527. *
  528. * During discovery we are interested in all WSS information, during
  529. * enrollment we are only interested in the WSS being enrolled in. Even so,
  530. * when in enrollment we keep parsing the message after finding the WSS of
  531. * interest, this simplifies the calling routine in that it can be sure
  532. * that all WSS information attributes have been parsed out of the message.
  533. *
  534. * Association frame is process with nbmutex held. The list access is safe.
  535. */
  536. static ssize_t wlp_get_all_wss_info(struct wlp *wlp,
  537. struct wlp_attr_wss_info *attr,
  538. struct wlp_neighbor_e *neighbor,
  539. struct wlp_wss *wss, ssize_t buflen)
  540. {
  541. struct device *dev = &wlp->rc->uwb_dev.dev;
  542. size_t used = 0;
  543. ssize_t result = -EINVAL;
  544. struct wlp_attr_wss_info *cur;
  545. struct wlp_uuid wssid;
  546. struct wlp_wss_tmp_info wss_info;
  547. unsigned enroll; /* 0 - discovery to cache, 1 - enrollment */
  548. struct wlp_wssid_e *wssid_e;
  549. char buf[WLP_WSS_UUID_STRSIZE];
  550. if (buflen < 0)
  551. goto out;
  552. if (neighbor != NULL && wss == NULL)
  553. enroll = 0; /* discovery */
  554. else if (wss != NULL && neighbor == NULL)
  555. enroll = 1; /* enrollment */
  556. else
  557. goto out;
  558. cur = attr;
  559. while (buflen - used > 0) {
  560. memset(&wss_info, 0, sizeof(wss_info));
  561. cur = (void *)cur + used;
  562. result = wlp_get_wss_info(wlp, cur, buflen - used, &wssid,
  563. &wss_info);
  564. if (result == -ENODATA) {
  565. result = used;
  566. goto out;
  567. } else if (result < 0) {
  568. dev_err(dev, "WLP: Unable to parse WSS information "
  569. "from WSS information attribute. \n");
  570. result = -EINVAL;
  571. goto error_parse;
  572. }
  573. if (enroll && !memcmp(&wssid, &wss->wssid, sizeof(wssid))) {
  574. if (wss_info.accept_enroll != 1) {
  575. dev_err(dev, "WLP: Requested WSS does "
  576. "not accept enrollment.\n");
  577. result = -EINVAL;
  578. goto out;
  579. }
  580. memcpy(wss->name, wss_info.name, sizeof(wss->name));
  581. wss->bcast = wss_info.bcast;
  582. wss->secure_status = wss_info.sec_status;
  583. wss->accept_enroll = wss_info.accept_enroll;
  584. wss->state = WLP_WSS_STATE_PART_ENROLLED;
  585. wlp_wss_uuid_print(buf, sizeof(buf), &wssid);
  586. dev_dbg(dev, "WLP: Found WSS %s. Enrolling.\n", buf);
  587. } else {
  588. wssid_e = wlp_create_wssid_e(wlp, neighbor);
  589. if (wssid_e == NULL) {
  590. dev_err(dev, "WLP: Cannot create new WSSID "
  591. "entry for neighbor %02x:%02x.\n",
  592. neighbor->uwb_dev->dev_addr.data[1],
  593. neighbor->uwb_dev->dev_addr.data[0]);
  594. result = -ENOMEM;
  595. goto out;
  596. }
  597. wssid_e->wssid = wssid;
  598. *wssid_e->info = wss_info;
  599. }
  600. used += result;
  601. }
  602. result = used;
  603. error_parse:
  604. if (result < 0 && !enroll) /* this was a discovery */
  605. wlp_remove_neighbor_tmp_info(neighbor);
  606. out:
  607. return result;
  608. }
  609. /**
  610. * Parse WSS information attributes into cache for discovery
  611. *
  612. * @attr: the first WSS information attribute in message
  613. * @neighbor: the neighbor whose cache will be populated
  614. * @buflen: size of the input buffer
  615. */
  616. static ssize_t wlp_get_wss_info_to_cache(struct wlp *wlp,
  617. struct wlp_attr_wss_info *attr,
  618. struct wlp_neighbor_e *neighbor,
  619. ssize_t buflen)
  620. {
  621. return wlp_get_all_wss_info(wlp, attr, neighbor, NULL, buflen);
  622. }
  623. /**
  624. * Parse WSS information attributes into WSS struct for enrollment
  625. *
  626. * @attr: the first WSS information attribute in message
  627. * @wss: the WSS that will be enrolled
  628. * @buflen: size of the input buffer
  629. */
  630. static ssize_t wlp_get_wss_info_to_enroll(struct wlp *wlp,
  631. struct wlp_attr_wss_info *attr,
  632. struct wlp_wss *wss, ssize_t buflen)
  633. {
  634. return wlp_get_all_wss_info(wlp, attr, NULL, wss, buflen);
  635. }
  636. /**
  637. * Construct a D1 association frame
  638. *
  639. * We use the radio control functions to determine the values of the device
  640. * properties. These are of variable length and the total space needed is
  641. * tallied first before we start constructing the message. The radio
  642. * control functions return strings that are terminated with \0. This
  643. * character should not be included in the message (there is a length field
  644. * accompanying it in the attribute).
  645. */
  646. static int wlp_build_assoc_d1(struct wlp *wlp, struct wlp_wss *wss,
  647. struct sk_buff **skb)
  648. {
  649. struct device *dev = &wlp->rc->uwb_dev.dev;
  650. int result = 0;
  651. struct wlp_device_info *info;
  652. size_t used = 0;
  653. struct wlp_frame_assoc *_d1;
  654. struct sk_buff *_skb;
  655. void *d1_itr;
  656. if (wlp->dev_info == NULL) {
  657. result = __wlp_setup_device_info(wlp);
  658. if (result < 0) {
  659. dev_err(dev, "WLP: Unable to setup device "
  660. "information for D1 message.\n");
  661. goto error;
  662. }
  663. }
  664. info = wlp->dev_info;
  665. _skb = dev_alloc_skb(sizeof(*_d1)
  666. + sizeof(struct wlp_attr_uuid_e)
  667. + sizeof(struct wlp_attr_wss_sel_mthd)
  668. + sizeof(struct wlp_attr_dev_name)
  669. + strlen(info->name)
  670. + sizeof(struct wlp_attr_manufacturer)
  671. + strlen(info->manufacturer)
  672. + sizeof(struct wlp_attr_model_name)
  673. + strlen(info->model_name)
  674. + sizeof(struct wlp_attr_model_nr)
  675. + strlen(info->model_nr)
  676. + sizeof(struct wlp_attr_serial)
  677. + strlen(info->serial)
  678. + sizeof(struct wlp_attr_prim_dev_type)
  679. + sizeof(struct wlp_attr_wlp_assc_err));
  680. if (_skb == NULL) {
  681. dev_err(dev, "WLP: Cannot allocate memory for association "
  682. "message.\n");
  683. result = -ENOMEM;
  684. goto error;
  685. }
  686. _d1 = (void *) _skb->data;
  687. _d1->hdr.mux_hdr = cpu_to_le16(WLP_PROTOCOL_ID);
  688. _d1->hdr.type = WLP_FRAME_ASSOCIATION;
  689. _d1->type = WLP_ASSOC_D1;
  690. wlp_set_version(&_d1->version, WLP_VERSION);
  691. wlp_set_msg_type(&_d1->msg_type, WLP_ASSOC_D1);
  692. d1_itr = _d1->attr;
  693. used = wlp_set_uuid_e(d1_itr, &wlp->uuid);
  694. used += wlp_set_wss_sel_mthd(d1_itr + used, WLP_WSS_REG_SELECT);
  695. used += wlp_set_dev_name(d1_itr + used, info->name,
  696. strlen(info->name));
  697. used += wlp_set_manufacturer(d1_itr + used, info->manufacturer,
  698. strlen(info->manufacturer));
  699. used += wlp_set_model_name(d1_itr + used, info->model_name,
  700. strlen(info->model_name));
  701. used += wlp_set_model_nr(d1_itr + used, info->model_nr,
  702. strlen(info->model_nr));
  703. used += wlp_set_serial(d1_itr + used, info->serial,
  704. strlen(info->serial));
  705. used += wlp_set_prim_dev_type(d1_itr + used, &info->prim_dev_type);
  706. used += wlp_set_wlp_assc_err(d1_itr + used, WLP_ASSOC_ERROR_NONE);
  707. skb_put(_skb, sizeof(*_d1) + used);
  708. *skb = _skb;
  709. error:
  710. return result;
  711. }
  712. /**
  713. * Construct a D2 association frame
  714. *
  715. * We use the radio control functions to determine the values of the device
  716. * properties. These are of variable length and the total space needed is
  717. * tallied first before we start constructing the message. The radio
  718. * control functions return strings that are terminated with \0. This
  719. * character should not be included in the message (there is a length field
  720. * accompanying it in the attribute).
  721. */
  722. static
  723. int wlp_build_assoc_d2(struct wlp *wlp, struct wlp_wss *wss,
  724. struct sk_buff **skb, struct wlp_uuid *uuid_e)
  725. {
  726. struct device *dev = &wlp->rc->uwb_dev.dev;
  727. int result = 0;
  728. struct wlp_device_info *info;
  729. size_t used = 0;
  730. struct wlp_frame_assoc *_d2;
  731. struct sk_buff *_skb;
  732. void *d2_itr;
  733. size_t mem_needed;
  734. if (wlp->dev_info == NULL) {
  735. result = __wlp_setup_device_info(wlp);
  736. if (result < 0) {
  737. dev_err(dev, "WLP: Unable to setup device "
  738. "information for D2 message.\n");
  739. goto error;
  740. }
  741. }
  742. info = wlp->dev_info;
  743. mem_needed = sizeof(*_d2)
  744. + sizeof(struct wlp_attr_uuid_e)
  745. + sizeof(struct wlp_attr_uuid_r)
  746. + sizeof(struct wlp_attr_dev_name)
  747. + strlen(info->name)
  748. + sizeof(struct wlp_attr_manufacturer)
  749. + strlen(info->manufacturer)
  750. + sizeof(struct wlp_attr_model_name)
  751. + strlen(info->model_name)
  752. + sizeof(struct wlp_attr_model_nr)
  753. + strlen(info->model_nr)
  754. + sizeof(struct wlp_attr_serial)
  755. + strlen(info->serial)
  756. + sizeof(struct wlp_attr_prim_dev_type)
  757. + sizeof(struct wlp_attr_wlp_assc_err);
  758. if (wlp->wss.state >= WLP_WSS_STATE_ACTIVE)
  759. mem_needed += sizeof(struct wlp_attr_wss_info)
  760. + sizeof(struct wlp_wss_info)
  761. + strlen(wlp->wss.name);
  762. _skb = dev_alloc_skb(mem_needed);
  763. if (_skb == NULL) {
  764. dev_err(dev, "WLP: Cannot allocate memory for association "
  765. "message.\n");
  766. result = -ENOMEM;
  767. goto error;
  768. }
  769. _d2 = (void *) _skb->data;
  770. _d2->hdr.mux_hdr = cpu_to_le16(WLP_PROTOCOL_ID);
  771. _d2->hdr.type = WLP_FRAME_ASSOCIATION;
  772. _d2->type = WLP_ASSOC_D2;
  773. wlp_set_version(&_d2->version, WLP_VERSION);
  774. wlp_set_msg_type(&_d2->msg_type, WLP_ASSOC_D2);
  775. d2_itr = _d2->attr;
  776. used = wlp_set_uuid_e(d2_itr, uuid_e);
  777. used += wlp_set_uuid_r(d2_itr + used, &wlp->uuid);
  778. if (wlp->wss.state >= WLP_WSS_STATE_ACTIVE)
  779. used += wlp_set_wss_info(d2_itr + used, &wlp->wss);
  780. used += wlp_set_dev_name(d2_itr + used, info->name,
  781. strlen(info->name));
  782. used += wlp_set_manufacturer(d2_itr + used, info->manufacturer,
  783. strlen(info->manufacturer));
  784. used += wlp_set_model_name(d2_itr + used, info->model_name,
  785. strlen(info->model_name));
  786. used += wlp_set_model_nr(d2_itr + used, info->model_nr,
  787. strlen(info->model_nr));
  788. used += wlp_set_serial(d2_itr + used, info->serial,
  789. strlen(info->serial));
  790. used += wlp_set_prim_dev_type(d2_itr + used, &info->prim_dev_type);
  791. used += wlp_set_wlp_assc_err(d2_itr + used, WLP_ASSOC_ERROR_NONE);
  792. skb_put(_skb, sizeof(*_d2) + used);
  793. *skb = _skb;
  794. error:
  795. return result;
  796. }
  797. /**
  798. * Allocate memory for and populate fields of F0 association frame
  799. *
  800. * Currently (while focusing on unsecure enrollment) we ignore the
  801. * nonce's that could be placed in the message. Only the error field is
  802. * populated by the value provided by the caller.
  803. */
  804. static
  805. int wlp_build_assoc_f0(struct wlp *wlp, struct sk_buff **skb,
  806. enum wlp_assc_error error)
  807. {
  808. struct device *dev = &wlp->rc->uwb_dev.dev;
  809. int result = -ENOMEM;
  810. struct {
  811. struct wlp_frame_assoc f0_hdr;
  812. struct wlp_attr_enonce enonce;
  813. struct wlp_attr_rnonce rnonce;
  814. struct wlp_attr_wlp_assc_err assc_err;
  815. } *f0;
  816. struct sk_buff *_skb;
  817. struct wlp_nonce tmp;
  818. _skb = dev_alloc_skb(sizeof(*f0));
  819. if (_skb == NULL) {
  820. dev_err(dev, "WLP: Unable to allocate memory for F0 "
  821. "association frame. \n");
  822. goto error_alloc;
  823. }
  824. f0 = (void *) _skb->data;
  825. f0->f0_hdr.hdr.mux_hdr = cpu_to_le16(WLP_PROTOCOL_ID);
  826. f0->f0_hdr.hdr.type = WLP_FRAME_ASSOCIATION;
  827. f0->f0_hdr.type = WLP_ASSOC_F0;
  828. wlp_set_version(&f0->f0_hdr.version, WLP_VERSION);
  829. wlp_set_msg_type(&f0->f0_hdr.msg_type, WLP_ASSOC_F0);
  830. memset(&tmp, 0, sizeof(tmp));
  831. wlp_set_enonce(&f0->enonce, &tmp);
  832. wlp_set_rnonce(&f0->rnonce, &tmp);
  833. wlp_set_wlp_assc_err(&f0->assc_err, error);
  834. skb_put(_skb, sizeof(*f0));
  835. *skb = _skb;
  836. result = 0;
  837. error_alloc:
  838. return result;
  839. }
  840. /**
  841. * Parse F0 frame
  842. *
  843. * We just retrieve the values and print it as an error to the user.
  844. * Calling function already knows an error occured (F0 indicates error), so
  845. * we just parse the content as debug for higher layers.
  846. */
  847. int wlp_parse_f0(struct wlp *wlp, struct sk_buff *skb)
  848. {
  849. struct device *dev = &wlp->rc->uwb_dev.dev;
  850. struct wlp_frame_assoc *f0 = (void *) skb->data;
  851. void *ptr = skb->data;
  852. size_t len = skb->len;
  853. size_t used;
  854. ssize_t result;
  855. struct wlp_nonce enonce, rnonce;
  856. enum wlp_assc_error assc_err;
  857. char enonce_buf[WLP_WSS_NONCE_STRSIZE];
  858. char rnonce_buf[WLP_WSS_NONCE_STRSIZE];
  859. used = sizeof(*f0);
  860. result = wlp_get_enonce(wlp, ptr + used, &enonce, len - used);
  861. if (result < 0) {
  862. dev_err(dev, "WLP: unable to obtain Enrollee nonce "
  863. "attribute from F0 message.\n");
  864. goto error_parse;
  865. }
  866. used += result;
  867. result = wlp_get_rnonce(wlp, ptr + used, &rnonce, len - used);
  868. if (result < 0) {
  869. dev_err(dev, "WLP: unable to obtain Registrar nonce "
  870. "attribute from F0 message.\n");
  871. goto error_parse;
  872. }
  873. used += result;
  874. result = wlp_get_wlp_assc_err(wlp, ptr + used, &assc_err, len - used);
  875. if (result < 0) {
  876. dev_err(dev, "WLP: unable to obtain WLP Association error "
  877. "attribute from F0 message.\n");
  878. goto error_parse;
  879. }
  880. wlp_wss_nonce_print(enonce_buf, sizeof(enonce_buf), &enonce);
  881. wlp_wss_nonce_print(rnonce_buf, sizeof(rnonce_buf), &rnonce);
  882. dev_err(dev, "WLP: Received F0 error frame from neighbor. Enrollee "
  883. "nonce: %s, Registrar nonce: %s, WLP Association error: %s.\n",
  884. enonce_buf, rnonce_buf, wlp_assc_error_str(assc_err));
  885. result = 0;
  886. error_parse:
  887. return result;
  888. }
  889. /**
  890. * Retrieve variable device information from association message
  891. *
  892. * The device information parsed is not required in any message. This
  893. * routine will thus not fail if an attribute is not present.
  894. * The attributes are expected in a certain order, even if all are not
  895. * present. The "attribute type" value is used to ensure the attributes
  896. * are parsed in the correct order.
  897. *
  898. * If an error is encountered during parsing the function will return an
  899. * error code, when this happens the given device_info structure may be
  900. * partially filled.
  901. */
  902. static
  903. int wlp_get_variable_info(struct wlp *wlp, void *data,
  904. struct wlp_device_info *dev_info, ssize_t len)
  905. {
  906. struct device *dev = &wlp->rc->uwb_dev.dev;
  907. size_t used = 0;
  908. struct wlp_attr_hdr *hdr;
  909. ssize_t result = 0;
  910. unsigned last = 0;
  911. while (len - used > 0) {
  912. if (len - used < sizeof(*hdr)) {
  913. dev_err(dev, "WLP: Partial data in frame, cannot "
  914. "parse. \n");
  915. goto error_parse;
  916. }
  917. hdr = data + used;
  918. switch (le16_to_cpu(hdr->type)) {
  919. case WLP_ATTR_MANUF:
  920. if (last >= WLP_ATTR_MANUF) {
  921. dev_err(dev, "WLP: Incorrect order of "
  922. "attribute values in D1 msg.\n");
  923. goto error_parse;
  924. }
  925. result = wlp_get_manufacturer(wlp, data + used,
  926. dev_info->manufacturer,
  927. len - used);
  928. if (result < 0) {
  929. dev_err(dev, "WLP: Unable to obtain "
  930. "Manufacturer attribute from D1 "
  931. "message.\n");
  932. goto error_parse;
  933. }
  934. last = WLP_ATTR_MANUF;
  935. used += result;
  936. break;
  937. case WLP_ATTR_MODEL_NAME:
  938. if (last >= WLP_ATTR_MODEL_NAME) {
  939. dev_err(dev, "WLP: Incorrect order of "
  940. "attribute values in D1 msg.\n");
  941. goto error_parse;
  942. }
  943. result = wlp_get_model_name(wlp, data + used,
  944. dev_info->model_name,
  945. len - used);
  946. if (result < 0) {
  947. dev_err(dev, "WLP: Unable to obtain Model "
  948. "name attribute from D1 message.\n");
  949. goto error_parse;
  950. }
  951. last = WLP_ATTR_MODEL_NAME;
  952. used += result;
  953. break;
  954. case WLP_ATTR_MODEL_NR:
  955. if (last >= WLP_ATTR_MODEL_NR) {
  956. dev_err(dev, "WLP: Incorrect order of "
  957. "attribute values in D1 msg.\n");
  958. goto error_parse;
  959. }
  960. result = wlp_get_model_nr(wlp, data + used,
  961. dev_info->model_nr,
  962. len - used);
  963. if (result < 0) {
  964. dev_err(dev, "WLP: Unable to obtain Model "
  965. "number attribute from D1 message.\n");
  966. goto error_parse;
  967. }
  968. last = WLP_ATTR_MODEL_NR;
  969. used += result;
  970. break;
  971. case WLP_ATTR_SERIAL:
  972. if (last >= WLP_ATTR_SERIAL) {
  973. dev_err(dev, "WLP: Incorrect order of "
  974. "attribute values in D1 msg.\n");
  975. goto error_parse;
  976. }
  977. result = wlp_get_serial(wlp, data + used,
  978. dev_info->serial, len - used);
  979. if (result < 0) {
  980. dev_err(dev, "WLP: Unable to obtain Serial "
  981. "number attribute from D1 message.\n");
  982. goto error_parse;
  983. }
  984. last = WLP_ATTR_SERIAL;
  985. used += result;
  986. break;
  987. case WLP_ATTR_PRI_DEV_TYPE:
  988. if (last >= WLP_ATTR_PRI_DEV_TYPE) {
  989. dev_err(dev, "WLP: Incorrect order of "
  990. "attribute values in D1 msg.\n");
  991. goto error_parse;
  992. }
  993. result = wlp_get_prim_dev_type(wlp, data + used,
  994. &dev_info->prim_dev_type,
  995. len - used);
  996. if (result < 0) {
  997. dev_err(dev, "WLP: Unable to obtain Primary "
  998. "device type attribute from D1 "
  999. "message.\n");
  1000. goto error_parse;
  1001. }
  1002. dev_info->prim_dev_type.category =
  1003. le16_to_cpu(dev_info->prim_dev_type.category);
  1004. dev_info->prim_dev_type.subID =
  1005. le16_to_cpu(dev_info->prim_dev_type.subID);
  1006. last = WLP_ATTR_PRI_DEV_TYPE;
  1007. used += result;
  1008. break;
  1009. default:
  1010. /* This is not variable device information. */
  1011. goto out;
  1012. break;
  1013. }
  1014. }
  1015. out:
  1016. return used;
  1017. error_parse:
  1018. return -EINVAL;
  1019. }
  1020. /**
  1021. * Parse incoming D1 frame, populate attribute values
  1022. *
  1023. * Caller provides pointers to memory already allocated for attributes
  1024. * expected in the D1 frame. These variables will be populated.
  1025. */
  1026. static
  1027. int wlp_parse_d1_frame(struct wlp *wlp, struct sk_buff *skb,
  1028. struct wlp_uuid *uuid_e,
  1029. enum wlp_wss_sel_mthd *sel_mthd,
  1030. struct wlp_device_info *dev_info,
  1031. enum wlp_assc_error *assc_err)
  1032. {
  1033. struct device *dev = &wlp->rc->uwb_dev.dev;
  1034. struct wlp_frame_assoc *d1 = (void *) skb->data;
  1035. void *ptr = skb->data;
  1036. size_t len = skb->len;
  1037. size_t used;
  1038. ssize_t result;
  1039. used = sizeof(*d1);
  1040. result = wlp_get_uuid_e(wlp, ptr + used, uuid_e, len - used);
  1041. if (result < 0) {
  1042. dev_err(dev, "WLP: unable to obtain UUID-E attribute from D1 "
  1043. "message.\n");
  1044. goto error_parse;
  1045. }
  1046. used += result;
  1047. result = wlp_get_wss_sel_mthd(wlp, ptr + used, sel_mthd, len - used);
  1048. if (result < 0) {
  1049. dev_err(dev, "WLP: unable to obtain WSS selection method "
  1050. "from D1 message.\n");
  1051. goto error_parse;
  1052. }
  1053. used += result;
  1054. result = wlp_get_dev_name(wlp, ptr + used, dev_info->name,
  1055. len - used);
  1056. if (result < 0) {
  1057. dev_err(dev, "WLP: unable to obtain Device Name from D1 "
  1058. "message.\n");
  1059. goto error_parse;
  1060. }
  1061. used += result;
  1062. result = wlp_get_variable_info(wlp, ptr + used, dev_info, len - used);
  1063. if (result < 0) {
  1064. dev_err(dev, "WLP: unable to obtain Device Information from "
  1065. "D1 message.\n");
  1066. goto error_parse;
  1067. }
  1068. used += result;
  1069. result = wlp_get_wlp_assc_err(wlp, ptr + used, assc_err, len - used);
  1070. if (result < 0) {
  1071. dev_err(dev, "WLP: unable to obtain WLP Association Error "
  1072. "Information from D1 message.\n");
  1073. goto error_parse;
  1074. }
  1075. result = 0;
  1076. error_parse:
  1077. return result;
  1078. }
  1079. /**
  1080. * Handle incoming D1 frame
  1081. *
  1082. * The frame has already been verified to contain an Association header with
  1083. * the correct version number. Parse the incoming frame, construct and send
  1084. * a D2 frame in response.
  1085. *
  1086. * It is not clear what to do with most fields in the incoming D1 frame. We
  1087. * retrieve and discard the information here for now.
  1088. */
  1089. void wlp_handle_d1_frame(struct work_struct *ws)
  1090. {
  1091. struct wlp_assoc_frame_ctx *frame_ctx = container_of(ws,
  1092. struct wlp_assoc_frame_ctx,
  1093. ws);
  1094. struct wlp *wlp = frame_ctx->wlp;
  1095. struct wlp_wss *wss = &wlp->wss;
  1096. struct sk_buff *skb = frame_ctx->skb;
  1097. struct uwb_dev_addr *src = &frame_ctx->src;
  1098. int result;
  1099. struct device *dev = &wlp->rc->uwb_dev.dev;
  1100. struct wlp_uuid uuid_e;
  1101. enum wlp_wss_sel_mthd sel_mthd = 0;
  1102. struct wlp_device_info dev_info;
  1103. enum wlp_assc_error assc_err;
  1104. struct sk_buff *resp = NULL;
  1105. /* Parse D1 frame */
  1106. mutex_lock(&wss->mutex);
  1107. mutex_lock(&wlp->mutex); /* to access wlp->uuid */
  1108. memset(&dev_info, 0, sizeof(dev_info));
  1109. result = wlp_parse_d1_frame(wlp, skb, &uuid_e, &sel_mthd, &dev_info,
  1110. &assc_err);
  1111. if (result < 0) {
  1112. dev_err(dev, "WLP: Unable to parse incoming D1 frame.\n");
  1113. kfree_skb(skb);
  1114. goto out;
  1115. }
  1116. kfree_skb(skb);
  1117. if (!wlp_uuid_is_set(&wlp->uuid)) {
  1118. dev_err(dev, "WLP: UUID is not set. Set via sysfs to "
  1119. "proceed. Respong to D1 message with error F0.\n");
  1120. result = wlp_build_assoc_f0(wlp, &resp,
  1121. WLP_ASSOC_ERROR_NOT_READY);
  1122. if (result < 0) {
  1123. dev_err(dev, "WLP: Unable to construct F0 message.\n");
  1124. goto out;
  1125. }
  1126. } else {
  1127. /* Construct D2 frame */
  1128. result = wlp_build_assoc_d2(wlp, wss, &resp, &uuid_e);
  1129. if (result < 0) {
  1130. dev_err(dev, "WLP: Unable to construct D2 message.\n");
  1131. goto out;
  1132. }
  1133. }
  1134. /* Send D2 frame */
  1135. BUG_ON(wlp->xmit_frame == NULL);
  1136. result = wlp->xmit_frame(wlp, resp, src);
  1137. if (result < 0) {
  1138. dev_err(dev, "WLP: Unable to transmit D2 association "
  1139. "message: %d\n", result);
  1140. if (result == -ENXIO)
  1141. dev_err(dev, "WLP: Is network interface up? \n");
  1142. /* We could try again ... */
  1143. dev_kfree_skb_any(resp); /* we need to free if tx fails */
  1144. }
  1145. out:
  1146. kfree(frame_ctx);
  1147. mutex_unlock(&wlp->mutex);
  1148. mutex_unlock(&wss->mutex);
  1149. }
  1150. /**
  1151. * Parse incoming D2 frame, create and populate temporary cache
  1152. *
  1153. * @skb: socket buffer in which D2 frame can be found
  1154. * @neighbor: the neighbor that sent the D2 frame
  1155. *
  1156. * Will allocate memory for temporary storage of information learned during
  1157. * discovery.
  1158. */
  1159. int wlp_parse_d2_frame_to_cache(struct wlp *wlp, struct sk_buff *skb,
  1160. struct wlp_neighbor_e *neighbor)
  1161. {
  1162. struct device *dev = &wlp->rc->uwb_dev.dev;
  1163. struct wlp_frame_assoc *d2 = (void *) skb->data;
  1164. void *ptr = skb->data;
  1165. size_t len = skb->len;
  1166. size_t used;
  1167. ssize_t result;
  1168. struct wlp_uuid uuid_e;
  1169. struct wlp_device_info *nb_info;
  1170. enum wlp_assc_error assc_err;
  1171. used = sizeof(*d2);
  1172. result = wlp_get_uuid_e(wlp, ptr + used, &uuid_e, len - used);
  1173. if (result < 0) {
  1174. dev_err(dev, "WLP: unable to obtain UUID-E attribute from D2 "
  1175. "message.\n");
  1176. goto error_parse;
  1177. }
  1178. if (memcmp(&uuid_e, &wlp->uuid, sizeof(uuid_e))) {
  1179. dev_err(dev, "WLP: UUID-E in incoming D2 does not match "
  1180. "local UUID sent in D1. \n");
  1181. goto error_parse;
  1182. }
  1183. used += result;
  1184. result = wlp_get_uuid_r(wlp, ptr + used, &neighbor->uuid, len - used);
  1185. if (result < 0) {
  1186. dev_err(dev, "WLP: unable to obtain UUID-R attribute from D2 "
  1187. "message.\n");
  1188. goto error_parse;
  1189. }
  1190. used += result;
  1191. result = wlp_get_wss_info_to_cache(wlp, ptr + used, neighbor,
  1192. len - used);
  1193. if (result < 0) {
  1194. dev_err(dev, "WLP: unable to obtain WSS information "
  1195. "from D2 message.\n");
  1196. goto error_parse;
  1197. }
  1198. used += result;
  1199. neighbor->info = kzalloc(sizeof(struct wlp_device_info), GFP_KERNEL);
  1200. if (neighbor->info == NULL) {
  1201. dev_err(dev, "WLP: cannot allocate memory to store device "
  1202. "info.\n");
  1203. result = -ENOMEM;
  1204. goto error_parse;
  1205. }
  1206. nb_info = neighbor->info;
  1207. result = wlp_get_dev_name(wlp, ptr + used, nb_info->name,
  1208. len - used);
  1209. if (result < 0) {
  1210. dev_err(dev, "WLP: unable to obtain Device Name from D2 "
  1211. "message.\n");
  1212. goto error_parse;
  1213. }
  1214. used += result;
  1215. result = wlp_get_variable_info(wlp, ptr + used, nb_info, len - used);
  1216. if (result < 0) {
  1217. dev_err(dev, "WLP: unable to obtain Device Information from "
  1218. "D2 message.\n");
  1219. goto error_parse;
  1220. }
  1221. used += result;
  1222. result = wlp_get_wlp_assc_err(wlp, ptr + used, &assc_err, len - used);
  1223. if (result < 0) {
  1224. dev_err(dev, "WLP: unable to obtain WLP Association Error "
  1225. "Information from D2 message.\n");
  1226. goto error_parse;
  1227. }
  1228. if (assc_err != WLP_ASSOC_ERROR_NONE) {
  1229. dev_err(dev, "WLP: neighbor device returned association "
  1230. "error %d\n", assc_err);
  1231. result = -EINVAL;
  1232. goto error_parse;
  1233. }
  1234. result = 0;
  1235. error_parse:
  1236. if (result < 0)
  1237. wlp_remove_neighbor_tmp_info(neighbor);
  1238. return result;
  1239. }
  1240. /**
  1241. * Parse incoming D2 frame, populate attribute values of WSS bein enrolled in
  1242. *
  1243. * @wss: our WSS that will be enrolled
  1244. * @skb: socket buffer in which D2 frame can be found
  1245. * @neighbor: the neighbor that sent the D2 frame
  1246. * @wssid: the wssid of the WSS in which we want to enroll
  1247. *
  1248. * Forms part of enrollment sequence. We are trying to enroll in WSS with
  1249. * @wssid by using @neighbor as registrar. A D1 message was sent to
  1250. * @neighbor and now we need to parse the D2 response. The neighbor's
  1251. * response is searched for the requested WSS and if found (and it accepts
  1252. * enrollment), we store the information.
  1253. */
  1254. int wlp_parse_d2_frame_to_enroll(struct wlp_wss *wss, struct sk_buff *skb,
  1255. struct wlp_neighbor_e *neighbor,
  1256. struct wlp_uuid *wssid)
  1257. {
  1258. struct wlp *wlp = container_of(wss, struct wlp, wss);
  1259. struct device *dev = &wlp->rc->uwb_dev.dev;
  1260. void *ptr = skb->data;
  1261. size_t len = skb->len;
  1262. size_t used;
  1263. ssize_t result;
  1264. struct wlp_uuid uuid_e;
  1265. struct wlp_uuid uuid_r;
  1266. struct wlp_device_info nb_info;
  1267. enum wlp_assc_error assc_err;
  1268. char uuid_bufA[WLP_WSS_UUID_STRSIZE];
  1269. char uuid_bufB[WLP_WSS_UUID_STRSIZE];
  1270. used = sizeof(struct wlp_frame_assoc);
  1271. result = wlp_get_uuid_e(wlp, ptr + used, &uuid_e, len - used);
  1272. if (result < 0) {
  1273. dev_err(dev, "WLP: unable to obtain UUID-E attribute from D2 "
  1274. "message.\n");
  1275. goto error_parse;
  1276. }
  1277. if (memcmp(&uuid_e, &wlp->uuid, sizeof(uuid_e))) {
  1278. dev_err(dev, "WLP: UUID-E in incoming D2 does not match "
  1279. "local UUID sent in D1. \n");
  1280. goto error_parse;
  1281. }
  1282. used += result;
  1283. result = wlp_get_uuid_r(wlp, ptr + used, &uuid_r, len - used);
  1284. if (result < 0) {
  1285. dev_err(dev, "WLP: unable to obtain UUID-R attribute from D2 "
  1286. "message.\n");
  1287. goto error_parse;
  1288. }
  1289. if (memcmp(&uuid_r, &neighbor->uuid, sizeof(uuid_r))) {
  1290. wlp_wss_uuid_print(uuid_bufA, sizeof(uuid_bufA),
  1291. &neighbor->uuid);
  1292. wlp_wss_uuid_print(uuid_bufB, sizeof(uuid_bufB), &uuid_r);
  1293. dev_err(dev, "WLP: UUID of neighbor does not match UUID "
  1294. "learned during discovery. Originally discovered: %s, "
  1295. "now from D2 message: %s\n", uuid_bufA, uuid_bufB);
  1296. result = -EINVAL;
  1297. goto error_parse;
  1298. }
  1299. used += result;
  1300. wss->wssid = *wssid;
  1301. result = wlp_get_wss_info_to_enroll(wlp, ptr + used, wss, len - used);
  1302. if (result < 0) {
  1303. dev_err(dev, "WLP: unable to obtain WSS information "
  1304. "from D2 message.\n");
  1305. goto error_parse;
  1306. }
  1307. if (wss->state != WLP_WSS_STATE_PART_ENROLLED) {
  1308. dev_err(dev, "WLP: D2 message did not contain information "
  1309. "for successful enrollment. \n");
  1310. result = -EINVAL;
  1311. goto error_parse;
  1312. }
  1313. used += result;
  1314. /* Place device information on stack to continue parsing of message */
  1315. result = wlp_get_dev_name(wlp, ptr + used, nb_info.name,
  1316. len - used);
  1317. if (result < 0) {
  1318. dev_err(dev, "WLP: unable to obtain Device Name from D2 "
  1319. "message.\n");
  1320. goto error_parse;
  1321. }
  1322. used += result;
  1323. result = wlp_get_variable_info(wlp, ptr + used, &nb_info, len - used);
  1324. if (result < 0) {
  1325. dev_err(dev, "WLP: unable to obtain Device Information from "
  1326. "D2 message.\n");
  1327. goto error_parse;
  1328. }
  1329. used += result;
  1330. result = wlp_get_wlp_assc_err(wlp, ptr + used, &assc_err, len - used);
  1331. if (result < 0) {
  1332. dev_err(dev, "WLP: unable to obtain WLP Association Error "
  1333. "Information from D2 message.\n");
  1334. goto error_parse;
  1335. }
  1336. if (assc_err != WLP_ASSOC_ERROR_NONE) {
  1337. dev_err(dev, "WLP: neighbor device returned association "
  1338. "error %d\n", assc_err);
  1339. if (wss->state == WLP_WSS_STATE_PART_ENROLLED) {
  1340. dev_err(dev, "WLP: Enrolled in WSS (should not "
  1341. "happen according to spec). Undoing. \n");
  1342. wlp_wss_reset(wss);
  1343. }
  1344. result = -EINVAL;
  1345. goto error_parse;
  1346. }
  1347. result = 0;
  1348. error_parse:
  1349. return result;
  1350. }
  1351. /**
  1352. * Parse C3/C4 frame into provided variables
  1353. *
  1354. * @wssid: will point to copy of wssid retrieved from C3/C4 frame
  1355. * @tag: will point to copy of tag retrieved from C3/C4 frame
  1356. * @virt_addr: will point to copy of virtual address retrieved from C3/C4
  1357. * frame.
  1358. *
  1359. * Calling function has to allocate memory for these values.
  1360. *
  1361. * skb contains a valid C3/C4 frame, return the individual fields of this
  1362. * frame in the provided variables.
  1363. */
  1364. int wlp_parse_c3c4_frame(struct wlp *wlp, struct sk_buff *skb,
  1365. struct wlp_uuid *wssid, u8 *tag,
  1366. struct uwb_mac_addr *virt_addr)
  1367. {
  1368. struct device *dev = &wlp->rc->uwb_dev.dev;
  1369. int result;
  1370. void *ptr = skb->data;
  1371. size_t len = skb->len;
  1372. size_t used;
  1373. struct wlp_frame_assoc *assoc = ptr;
  1374. used = sizeof(*assoc);
  1375. result = wlp_get_wssid(wlp, ptr + used, wssid, len - used);
  1376. if (result < 0) {
  1377. dev_err(dev, "WLP: unable to obtain WSSID attribute from "
  1378. "%s message.\n", wlp_assoc_frame_str(assoc->type));
  1379. goto error_parse;
  1380. }
  1381. used += result;
  1382. result = wlp_get_wss_tag(wlp, ptr + used, tag, len - used);
  1383. if (result < 0) {
  1384. dev_err(dev, "WLP: unable to obtain WSS tag attribute from "
  1385. "%s message.\n", wlp_assoc_frame_str(assoc->type));
  1386. goto error_parse;
  1387. }
  1388. used += result;
  1389. result = wlp_get_wss_virt(wlp, ptr + used, virt_addr, len - used);
  1390. if (result < 0) {
  1391. dev_err(dev, "WLP: unable to obtain WSS virtual address "
  1392. "attribute from %s message.\n",
  1393. wlp_assoc_frame_str(assoc->type));
  1394. goto error_parse;
  1395. }
  1396. error_parse:
  1397. return result;
  1398. }
  1399. /**
  1400. * Allocate memory for and populate fields of C1 or C2 association frame
  1401. *
  1402. * The C1 and C2 association frames appear identical - except for the type.
  1403. */
  1404. static
  1405. int wlp_build_assoc_c1c2(struct wlp *wlp, struct wlp_wss *wss,
  1406. struct sk_buff **skb, enum wlp_assoc_type type)
  1407. {
  1408. struct device *dev = &wlp->rc->uwb_dev.dev;
  1409. int result = -ENOMEM;
  1410. struct {
  1411. struct wlp_frame_assoc c_hdr;
  1412. struct wlp_attr_wssid wssid;
  1413. } *c;
  1414. struct sk_buff *_skb;
  1415. _skb = dev_alloc_skb(sizeof(*c));
  1416. if (_skb == NULL) {
  1417. dev_err(dev, "WLP: Unable to allocate memory for C1/C2 "
  1418. "association frame. \n");
  1419. goto error_alloc;
  1420. }
  1421. c = (void *) _skb->data;
  1422. c->c_hdr.hdr.mux_hdr = cpu_to_le16(WLP_PROTOCOL_ID);
  1423. c->c_hdr.hdr.type = WLP_FRAME_ASSOCIATION;
  1424. c->c_hdr.type = type;
  1425. wlp_set_version(&c->c_hdr.version, WLP_VERSION);
  1426. wlp_set_msg_type(&c->c_hdr.msg_type, type);
  1427. wlp_set_wssid(&c->wssid, &wss->wssid);
  1428. skb_put(_skb, sizeof(*c));
  1429. *skb = _skb;
  1430. result = 0;
  1431. error_alloc:
  1432. return result;
  1433. }
  1434. static
  1435. int wlp_build_assoc_c1(struct wlp *wlp, struct wlp_wss *wss,
  1436. struct sk_buff **skb)
  1437. {
  1438. return wlp_build_assoc_c1c2(wlp, wss, skb, WLP_ASSOC_C1);
  1439. }
  1440. static
  1441. int wlp_build_assoc_c2(struct wlp *wlp, struct wlp_wss *wss,
  1442. struct sk_buff **skb)
  1443. {
  1444. return wlp_build_assoc_c1c2(wlp, wss, skb, WLP_ASSOC_C2);
  1445. }
  1446. /**
  1447. * Allocate memory for and populate fields of C3 or C4 association frame
  1448. *
  1449. * The C3 and C4 association frames appear identical - except for the type.
  1450. */
  1451. static
  1452. int wlp_build_assoc_c3c4(struct wlp *wlp, struct wlp_wss *wss,
  1453. struct sk_buff **skb, enum wlp_assoc_type type)
  1454. {
  1455. struct device *dev = &wlp->rc->uwb_dev.dev;
  1456. int result = -ENOMEM;
  1457. struct {
  1458. struct wlp_frame_assoc c_hdr;
  1459. struct wlp_attr_wssid wssid;
  1460. struct wlp_attr_wss_tag wss_tag;
  1461. struct wlp_attr_wss_virt wss_virt;
  1462. } *c;
  1463. struct sk_buff *_skb;
  1464. _skb = dev_alloc_skb(sizeof(*c));
  1465. if (_skb == NULL) {
  1466. dev_err(dev, "WLP: Unable to allocate memory for C3/C4 "
  1467. "association frame. \n");
  1468. goto error_alloc;
  1469. }
  1470. c = (void *) _skb->data;
  1471. c->c_hdr.hdr.mux_hdr = cpu_to_le16(WLP_PROTOCOL_ID);
  1472. c->c_hdr.hdr.type = WLP_FRAME_ASSOCIATION;
  1473. c->c_hdr.type = type;
  1474. wlp_set_version(&c->c_hdr.version, WLP_VERSION);
  1475. wlp_set_msg_type(&c->c_hdr.msg_type, type);
  1476. wlp_set_wssid(&c->wssid, &wss->wssid);
  1477. wlp_set_wss_tag(&c->wss_tag, wss->tag);
  1478. wlp_set_wss_virt(&c->wss_virt, &wss->virtual_addr);
  1479. skb_put(_skb, sizeof(*c));
  1480. *skb = _skb;
  1481. result = 0;
  1482. error_alloc:
  1483. return result;
  1484. }
  1485. static
  1486. int wlp_build_assoc_c3(struct wlp *wlp, struct wlp_wss *wss,
  1487. struct sk_buff **skb)
  1488. {
  1489. return wlp_build_assoc_c3c4(wlp, wss, skb, WLP_ASSOC_C3);
  1490. }
  1491. static
  1492. int wlp_build_assoc_c4(struct wlp *wlp, struct wlp_wss *wss,
  1493. struct sk_buff **skb)
  1494. {
  1495. return wlp_build_assoc_c3c4(wlp, wss, skb, WLP_ASSOC_C4);
  1496. }
  1497. #define wlp_send_assoc(type, id) \
  1498. static int wlp_send_assoc_##type(struct wlp *wlp, struct wlp_wss *wss, \
  1499. struct uwb_dev_addr *dev_addr) \
  1500. { \
  1501. struct device *dev = &wlp->rc->uwb_dev.dev; \
  1502. int result; \
  1503. struct sk_buff *skb = NULL; \
  1504. \
  1505. /* Build the frame */ \
  1506. result = wlp_build_assoc_##type(wlp, wss, &skb); \
  1507. if (result < 0) { \
  1508. dev_err(dev, "WLP: Unable to construct %s association " \
  1509. "frame: %d\n", wlp_assoc_frame_str(id), result);\
  1510. goto error_build_assoc; \
  1511. } \
  1512. /* Send the frame */ \
  1513. BUG_ON(wlp->xmit_frame == NULL); \
  1514. result = wlp->xmit_frame(wlp, skb, dev_addr); \
  1515. if (result < 0) { \
  1516. dev_err(dev, "WLP: Unable to transmit %s association " \
  1517. "message: %d\n", wlp_assoc_frame_str(id), \
  1518. result); \
  1519. if (result == -ENXIO) \
  1520. dev_err(dev, "WLP: Is network interface " \
  1521. "up? \n"); \
  1522. goto error_xmit; \
  1523. } \
  1524. return 0; \
  1525. error_xmit: \
  1526. /* We could try again ... */ \
  1527. dev_kfree_skb_any(skb);/*we need to free if tx fails*/ \
  1528. error_build_assoc: \
  1529. return result; \
  1530. }
  1531. wlp_send_assoc(d1, WLP_ASSOC_D1)
  1532. wlp_send_assoc(c1, WLP_ASSOC_C1)
  1533. wlp_send_assoc(c3, WLP_ASSOC_C3)
  1534. int wlp_send_assoc_frame(struct wlp *wlp, struct wlp_wss *wss,
  1535. struct uwb_dev_addr *dev_addr,
  1536. enum wlp_assoc_type type)
  1537. {
  1538. int result = 0;
  1539. struct device *dev = &wlp->rc->uwb_dev.dev;
  1540. switch (type) {
  1541. case WLP_ASSOC_D1:
  1542. result = wlp_send_assoc_d1(wlp, wss, dev_addr);
  1543. break;
  1544. case WLP_ASSOC_C1:
  1545. result = wlp_send_assoc_c1(wlp, wss, dev_addr);
  1546. break;
  1547. case WLP_ASSOC_C3:
  1548. result = wlp_send_assoc_c3(wlp, wss, dev_addr);
  1549. break;
  1550. default:
  1551. dev_err(dev, "WLP: Received request to send unknown "
  1552. "association message.\n");
  1553. result = -EINVAL;
  1554. break;
  1555. }
  1556. return result;
  1557. }
  1558. /**
  1559. * Handle incoming C1 frame
  1560. *
  1561. * The frame has already been verified to contain an Association header with
  1562. * the correct version number. Parse the incoming frame, construct and send
  1563. * a C2 frame in response.
  1564. */
  1565. void wlp_handle_c1_frame(struct work_struct *ws)
  1566. {
  1567. struct wlp_assoc_frame_ctx *frame_ctx = container_of(ws,
  1568. struct wlp_assoc_frame_ctx,
  1569. ws);
  1570. struct wlp *wlp = frame_ctx->wlp;
  1571. struct wlp_wss *wss = &wlp->wss;
  1572. struct device *dev = &wlp->rc->uwb_dev.dev;
  1573. struct wlp_frame_assoc *c1 = (void *) frame_ctx->skb->data;
  1574. unsigned int len = frame_ctx->skb->len;
  1575. struct uwb_dev_addr *src = &frame_ctx->src;
  1576. int result;
  1577. struct wlp_uuid wssid;
  1578. struct sk_buff *resp = NULL;
  1579. /* Parse C1 frame */
  1580. mutex_lock(&wss->mutex);
  1581. result = wlp_get_wssid(wlp, (void *)c1 + sizeof(*c1), &wssid,
  1582. len - sizeof(*c1));
  1583. if (result < 0) {
  1584. dev_err(dev, "WLP: unable to obtain WSSID from C1 frame.\n");
  1585. goto out;
  1586. }
  1587. if (!memcmp(&wssid, &wss->wssid, sizeof(wssid))
  1588. && wss->state == WLP_WSS_STATE_ACTIVE) {
  1589. /* Construct C2 frame */
  1590. result = wlp_build_assoc_c2(wlp, wss, &resp);
  1591. if (result < 0) {
  1592. dev_err(dev, "WLP: Unable to construct C2 message.\n");
  1593. goto out;
  1594. }
  1595. } else {
  1596. /* Construct F0 frame */
  1597. result = wlp_build_assoc_f0(wlp, &resp, WLP_ASSOC_ERROR_INV);
  1598. if (result < 0) {
  1599. dev_err(dev, "WLP: Unable to construct F0 message.\n");
  1600. goto out;
  1601. }
  1602. }
  1603. /* Send C2 frame */
  1604. BUG_ON(wlp->xmit_frame == NULL);
  1605. result = wlp->xmit_frame(wlp, resp, src);
  1606. if (result < 0) {
  1607. dev_err(dev, "WLP: Unable to transmit response association "
  1608. "message: %d\n", result);
  1609. if (result == -ENXIO)
  1610. dev_err(dev, "WLP: Is network interface up? \n");
  1611. /* We could try again ... */
  1612. dev_kfree_skb_any(resp); /* we need to free if tx fails */
  1613. }
  1614. out:
  1615. kfree_skb(frame_ctx->skb);
  1616. kfree(frame_ctx);
  1617. mutex_unlock(&wss->mutex);
  1618. }
  1619. /**
  1620. * Handle incoming C3 frame
  1621. *
  1622. * The frame has already been verified to contain an Association header with
  1623. * the correct version number. Parse the incoming frame, construct and send
  1624. * a C4 frame in response. If the C3 frame identifies a WSS that is locally
  1625. * active then we connect to this neighbor (add it to our EDA cache).
  1626. */
  1627. void wlp_handle_c3_frame(struct work_struct *ws)
  1628. {
  1629. struct wlp_assoc_frame_ctx *frame_ctx = container_of(ws,
  1630. struct wlp_assoc_frame_ctx,
  1631. ws);
  1632. struct wlp *wlp = frame_ctx->wlp;
  1633. struct wlp_wss *wss = &wlp->wss;
  1634. struct device *dev = &wlp->rc->uwb_dev.dev;
  1635. struct sk_buff *skb = frame_ctx->skb;
  1636. struct uwb_dev_addr *src = &frame_ctx->src;
  1637. int result;
  1638. struct sk_buff *resp = NULL;
  1639. struct wlp_uuid wssid;
  1640. u8 tag;
  1641. struct uwb_mac_addr virt_addr;
  1642. /* Parse C3 frame */
  1643. mutex_lock(&wss->mutex);
  1644. result = wlp_parse_c3c4_frame(wlp, skb, &wssid, &tag, &virt_addr);
  1645. if (result < 0) {
  1646. dev_err(dev, "WLP: unable to obtain values from C3 frame.\n");
  1647. goto out;
  1648. }
  1649. if (!memcmp(&wssid, &wss->wssid, sizeof(wssid))
  1650. && wss->state >= WLP_WSS_STATE_ACTIVE) {
  1651. result = wlp_eda_update_node(&wlp->eda, src, wss,
  1652. (void *) virt_addr.data, tag,
  1653. WLP_WSS_CONNECTED);
  1654. if (result < 0) {
  1655. dev_err(dev, "WLP: Unable to update EDA cache "
  1656. "with new connected neighbor information.\n");
  1657. result = wlp_build_assoc_f0(wlp, &resp,
  1658. WLP_ASSOC_ERROR_INT);
  1659. if (result < 0) {
  1660. dev_err(dev, "WLP: Unable to construct F0 "
  1661. "message.\n");
  1662. goto out;
  1663. }
  1664. } else {
  1665. wss->state = WLP_WSS_STATE_CONNECTED;
  1666. /* Construct C4 frame */
  1667. result = wlp_build_assoc_c4(wlp, wss, &resp);
  1668. if (result < 0) {
  1669. dev_err(dev, "WLP: Unable to construct C4 "
  1670. "message.\n");
  1671. goto out;
  1672. }
  1673. }
  1674. } else {
  1675. /* Construct F0 frame */
  1676. result = wlp_build_assoc_f0(wlp, &resp, WLP_ASSOC_ERROR_INV);
  1677. if (result < 0) {
  1678. dev_err(dev, "WLP: Unable to construct F0 message.\n");
  1679. goto out;
  1680. }
  1681. }
  1682. /* Send C4 frame */
  1683. BUG_ON(wlp->xmit_frame == NULL);
  1684. result = wlp->xmit_frame(wlp, resp, src);
  1685. if (result < 0) {
  1686. dev_err(dev, "WLP: Unable to transmit response association "
  1687. "message: %d\n", result);
  1688. if (result == -ENXIO)
  1689. dev_err(dev, "WLP: Is network interface up? \n");
  1690. /* We could try again ... */
  1691. dev_kfree_skb_any(resp); /* we need to free if tx fails */
  1692. }
  1693. out:
  1694. kfree_skb(frame_ctx->skb);
  1695. kfree(frame_ctx);
  1696. mutex_unlock(&wss->mutex);
  1697. }