messages.c 62 KB

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