txrx.c 11 KB

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