txrx.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  1. /*
  2. * WiMedia Logical Link Control Protocol (WLP)
  3. * Message exchange infrastructure
  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. */
  26. #include <linux/etherdevice.h>
  27. #include <linux/wlp.h>
  28. #define D_LOCAL 5
  29. #include <linux/uwb/debug.h>
  30. #include "wlp-internal.h"
  31. /**
  32. * Direct incoming association msg to correct parsing routine
  33. *
  34. * We only expect D1, E1, C1, C3 messages as new. All other incoming
  35. * association messages should form part of an established session that is
  36. * handled elsewhere.
  37. * The handling of these messages often require calling sleeping functions
  38. * - this cannot be done in interrupt context. We use the kernel's
  39. * workqueue to handle these messages.
  40. */
  41. static
  42. void wlp_direct_assoc_frame(struct wlp *wlp, struct sk_buff *skb,
  43. struct uwb_dev_addr *src)
  44. {
  45. struct device *dev = &wlp->rc->uwb_dev.dev;
  46. struct wlp_frame_assoc *assoc = (void *) skb->data;
  47. struct wlp_assoc_frame_ctx *frame_ctx;
  48. d_fnstart(5, dev, "wlp %p, skb %p\n", wlp, skb);
  49. frame_ctx = kmalloc(sizeof(*frame_ctx), GFP_ATOMIC);
  50. if (frame_ctx == NULL) {
  51. dev_err(dev, "WLP: Unable to allocate memory for association "
  52. "frame handling.\n");
  53. kfree_skb(skb);
  54. goto out;
  55. }
  56. frame_ctx->wlp = wlp;
  57. frame_ctx->skb = skb;
  58. frame_ctx->src = *src;
  59. switch (assoc->type) {
  60. case WLP_ASSOC_D1:
  61. d_printf(5, dev, "Received a D1 frame.\n");
  62. INIT_WORK(&frame_ctx->ws, wlp_handle_d1_frame);
  63. schedule_work(&frame_ctx->ws);
  64. break;
  65. case WLP_ASSOC_E1:
  66. d_printf(5, dev, "Received a E1 frame. FIXME?\n");
  67. kfree_skb(skb); /* Temporary until we handle it */
  68. kfree(frame_ctx); /* Temporary until we handle it */
  69. break;
  70. case WLP_ASSOC_C1:
  71. d_printf(5, dev, "Received a C1 frame.\n");
  72. INIT_WORK(&frame_ctx->ws, wlp_handle_c1_frame);
  73. schedule_work(&frame_ctx->ws);
  74. break;
  75. case WLP_ASSOC_C3:
  76. d_printf(5, dev, "Received a C3 frame.\n");
  77. INIT_WORK(&frame_ctx->ws, wlp_handle_c3_frame);
  78. schedule_work(&frame_ctx->ws);
  79. break;
  80. default:
  81. dev_err(dev, "Received unexpected association frame. "
  82. "Type = %d \n", assoc->type);
  83. kfree_skb(skb);
  84. kfree(frame_ctx);
  85. break;
  86. }
  87. out:
  88. d_fnend(5, dev, "wlp %p\n", wlp);
  89. }
  90. /**
  91. * Process incoming association frame
  92. *
  93. * Although it could be possible to deal with some incoming association
  94. * messages without creating a new session we are keeping things simple. We
  95. * do not accept new association messages if there is a session in progress
  96. * and the messages do not belong to that session.
  97. *
  98. * If an association message arrives that causes the creation of a session
  99. * (WLP_ASSOC_E1) while we are in the process of creating a session then we
  100. * rely on the neighbor mutex to protect the data. That is, the new session
  101. * will not be started until the previous is completed.
  102. */
  103. static
  104. void wlp_receive_assoc_frame(struct wlp *wlp, struct sk_buff *skb,
  105. struct uwb_dev_addr *src)
  106. {
  107. struct device *dev = &wlp->rc->uwb_dev.dev;
  108. struct wlp_frame_assoc *assoc = (void *) skb->data;
  109. struct wlp_session *session = wlp->session;
  110. u8 version;
  111. d_fnstart(5, dev, "wlp %p, skb %p\n", wlp, skb);
  112. if (wlp_get_version(wlp, &assoc->version, &version,
  113. sizeof(assoc->version)) < 0)
  114. goto error;
  115. if (version != WLP_VERSION) {
  116. dev_err(dev, "Unsupported WLP version in association "
  117. "message.\n");
  118. goto error;
  119. }
  120. if (session != NULL) {
  121. /* Function that created this session is still holding the
  122. * &wlp->mutex to protect this session. */
  123. if (assoc->type == session->exp_message ||
  124. assoc->type == WLP_ASSOC_F0) {
  125. if (!memcmp(&session->neighbor_addr, src,
  126. sizeof(*src))) {
  127. session->data = skb;
  128. (session->cb)(wlp);
  129. } else {
  130. dev_err(dev, "Received expected message from "
  131. "unexpected source. Expected message "
  132. "%d or F0 from %02x:%02x, but received "
  133. "it from %02x:%02x. Dropping.\n",
  134. session->exp_message,
  135. session->neighbor_addr.data[1],
  136. session->neighbor_addr.data[0],
  137. src->data[1], src->data[0]);
  138. goto error;
  139. }
  140. } else {
  141. dev_err(dev, "Association already in progress. "
  142. "Dropping.\n");
  143. goto error;
  144. }
  145. } else {
  146. wlp_direct_assoc_frame(wlp, skb, src);
  147. }
  148. d_fnend(5, dev, "wlp %p\n", wlp);
  149. return;
  150. error:
  151. kfree_skb(skb);
  152. d_fnend(5, dev, "wlp %p\n", wlp);
  153. }
  154. /**
  155. * Verify incoming frame is from connected neighbor, prep to pass to WLP client
  156. *
  157. * Verification proceeds according to WLP 0.99 [7.3.1]. The source address
  158. * is used to determine which neighbor is sending the frame and the WSS tag
  159. * is used to know to which WSS the frame belongs (we only support one WSS
  160. * so this test is straight forward).
  161. * With the WSS found we need to ensure that we are connected before
  162. * allowing the exchange of data frames.
  163. */
  164. static
  165. int wlp_verify_prep_rx_frame(struct wlp *wlp, struct sk_buff *skb,
  166. struct uwb_dev_addr *src)
  167. {
  168. struct device *dev = &wlp->rc->uwb_dev.dev;
  169. int result = -EINVAL;
  170. struct wlp_eda_node eda_entry;
  171. struct wlp_frame_std_abbrv_hdr *hdr = (void *) skb->data;
  172. d_fnstart(6, dev, "wlp %p, skb %p \n", wlp, skb);
  173. /*verify*/
  174. result = wlp_copy_eda_node(&wlp->eda, src, &eda_entry);
  175. if (result < 0) {
  176. if (printk_ratelimit())
  177. dev_err(dev, "WLP: Incoming frame is from unknown "
  178. "neighbor %02x:%02x.\n", src->data[1],
  179. src->data[0]);
  180. goto out;
  181. }
  182. if (hdr->tag != eda_entry.tag) {
  183. if (printk_ratelimit())
  184. dev_err(dev, "WLP: Tag of incoming frame from "
  185. "%02x:%02x does not match expected tag. "
  186. "Received 0x%02x, expected 0x%02x. \n",
  187. src->data[1], src->data[0], hdr->tag,
  188. eda_entry.tag);
  189. result = -EINVAL;
  190. goto out;
  191. }
  192. if (eda_entry.state != WLP_WSS_CONNECTED) {
  193. if (printk_ratelimit())
  194. dev_err(dev, "WLP: Incoming frame from "
  195. "%02x:%02x does is not from connected WSS.\n",
  196. src->data[1], src->data[0]);
  197. result = -EINVAL;
  198. goto out;
  199. }
  200. /*prep*/
  201. skb_pull(skb, sizeof(*hdr));
  202. out:
  203. d_fnend(6, dev, "wlp %p, skb %p, result = %d \n", wlp, skb, result);
  204. return result;
  205. }
  206. /**
  207. * Receive a WLP frame from device
  208. *
  209. * @returns: 1 if calling function should free the skb
  210. * 0 if it successfully handled skb and freed it
  211. * 0 if error occured, will free skb in this case
  212. */
  213. int wlp_receive_frame(struct device *dev, struct wlp *wlp, struct sk_buff *skb,
  214. struct uwb_dev_addr *src)
  215. {
  216. unsigned len = skb->len;
  217. void *ptr = skb->data;
  218. struct wlp_frame_hdr *hdr;
  219. int result = 0;
  220. d_fnstart(6, dev, "skb (%p), len (%u)\n", skb, len);
  221. if (len < sizeof(*hdr)) {
  222. dev_err(dev, "Not enough data to parse WLP header.\n");
  223. result = -EINVAL;
  224. goto out;
  225. }
  226. hdr = ptr;
  227. d_dump(6, dev, hdr, sizeof(*hdr));
  228. if (le16_to_cpu(hdr->mux_hdr) != WLP_PROTOCOL_ID) {
  229. dev_err(dev, "Not a WLP frame type.\n");
  230. result = -EINVAL;
  231. goto out;
  232. }
  233. switch (hdr->type) {
  234. case WLP_FRAME_STANDARD:
  235. if (len < sizeof(struct wlp_frame_std_abbrv_hdr)) {
  236. dev_err(dev, "Not enough data to parse Standard "
  237. "WLP header.\n");
  238. goto out;
  239. }
  240. result = wlp_verify_prep_rx_frame(wlp, skb, src);
  241. if (result < 0) {
  242. if (printk_ratelimit())
  243. dev_err(dev, "WLP: Verification of frame "
  244. "from neighbor %02x:%02x failed.\n",
  245. src->data[1], src->data[0]);
  246. goto out;
  247. }
  248. result = 1;
  249. break;
  250. case WLP_FRAME_ABBREVIATED:
  251. dev_err(dev, "Abbreviated frame received. FIXME?\n");
  252. kfree_skb(skb);
  253. break;
  254. case WLP_FRAME_CONTROL:
  255. dev_err(dev, "Control frame received. FIXME?\n");
  256. kfree_skb(skb);
  257. break;
  258. case WLP_FRAME_ASSOCIATION:
  259. if (len < sizeof(struct wlp_frame_assoc)) {
  260. dev_err(dev, "Not enough data to parse Association "
  261. "WLP header.\n");
  262. goto out;
  263. }
  264. d_printf(5, dev, "Association frame received.\n");
  265. wlp_receive_assoc_frame(wlp, skb, src);
  266. break;
  267. default:
  268. dev_err(dev, "Invalid frame received.\n");
  269. result = -EINVAL;
  270. break;
  271. }
  272. out:
  273. if (result < 0) {
  274. kfree_skb(skb);
  275. result = 0;
  276. }
  277. d_fnend(6, dev, "skb (%p)\n", skb);
  278. return result;
  279. }
  280. EXPORT_SYMBOL_GPL(wlp_receive_frame);
  281. /**
  282. * Verify frame from network stack, prepare for further transmission
  283. *
  284. * @skb: the socket buffer that needs to be prepared for transmission (it
  285. * is in need of a WLP header). If this is a broadcast frame we take
  286. * over the entire transmission.
  287. * If it is a unicast the WSS connection should already be established
  288. * and transmission will be done by the calling function.
  289. * @dst: On return this will contain the device address to which the
  290. * frame is destined.
  291. * @returns: 0 on success no tx : WLP header sucessfully applied to skb buffer,
  292. * calling function can proceed with tx
  293. * 1 on success with tx : WLP will take over transmission of this
  294. * frame
  295. * <0 on error
  296. *
  297. * The network stack (WLP client) is attempting to transmit a frame. We can
  298. * only transmit data if a local WSS is at least active (connection will be
  299. * done here if this is a broadcast frame and neighbor also has the WSS
  300. * active).
  301. *
  302. * The frame can be either broadcast or unicast. Broadcast in a WSS is
  303. * supported via multicast, but we don't support multicast yet (until
  304. * devices start to support MAB IEs). If a broadcast frame needs to be
  305. * transmitted it is treated as a unicast frame to each neighbor. In this
  306. * case the WLP takes over transmission of the skb and returns 1
  307. * to the caller to indicate so. Also, in this case, if a neighbor has the
  308. * same WSS activated but is not connected then the WSS connection will be
  309. * done at this time. The neighbor's virtual address will be learned at
  310. * this time.
  311. *
  312. * The destination address in a unicast frame is the virtual address of the
  313. * neighbor. This address only becomes known when a WSS connection is
  314. * established. We thus rely on a broadcast frame to trigger the setup of
  315. * WSS connections to all neighbors before we are able to send unicast
  316. * frames to them. This seems reasonable as IP would usually use ARP first
  317. * before any unicast frames are sent.
  318. *
  319. * If we are already connected to the neighbor (neighbor's virtual address
  320. * is known) we just prepare the WLP header and the caller will continue to
  321. * send the frame.
  322. *
  323. * A failure in this function usually indicates something that cannot be
  324. * fixed automatically. So, if this function fails (@return < 0) the calling
  325. * function should not retry to send the frame as it will very likely keep
  326. * failing.
  327. *
  328. */
  329. int wlp_prepare_tx_frame(struct device *dev, struct wlp *wlp,
  330. struct sk_buff *skb, struct uwb_dev_addr *dst)
  331. {
  332. int result = -EINVAL;
  333. struct ethhdr *eth_hdr = (void *) skb->data;
  334. d_fnstart(6, dev, "wlp (%p), skb (%p) \n", wlp, skb);
  335. if (is_broadcast_ether_addr(eth_hdr->h_dest)) {
  336. d_printf(6, dev, "WLP: handling broadcast frame. \n");
  337. result = wlp_eda_for_each(&wlp->eda, wlp_wss_send_copy, skb);
  338. if (result < 0) {
  339. if (printk_ratelimit())
  340. dev_err(dev, "Unable to handle broadcast "
  341. "frame from WLP client.\n");
  342. goto out;
  343. }
  344. dev_kfree_skb_irq(skb);
  345. result = 1;
  346. /* Frame will be transmitted by WLP. */
  347. } else {
  348. d_printf(6, dev, "WLP: handling unicast frame. \n");
  349. result = wlp_eda_for_virtual(&wlp->eda, eth_hdr->h_dest, dst,
  350. wlp_wss_prep_hdr, skb);
  351. if (unlikely(result < 0)) {
  352. if (printk_ratelimit())
  353. dev_err(dev, "Unable to prepare "
  354. "skb for transmission. \n");
  355. goto out;
  356. }
  357. }
  358. out:
  359. d_fnend(6, dev, "wlp (%p), skb (%p). result = %d \n", wlp, skb, result);
  360. return result;
  361. }
  362. EXPORT_SYMBOL_GPL(wlp_prepare_tx_frame);