txrx.c 11 KB

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