unicast.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. /* Copyright (C) 2010-2012 B.A.T.M.A.N. contributors:
  2. *
  3. * Andreas Langer
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA
  18. */
  19. #include "main.h"
  20. #include "unicast.h"
  21. #include "send.h"
  22. #include "soft-interface.h"
  23. #include "gateway_client.h"
  24. #include "originator.h"
  25. #include "hash.h"
  26. #include "translation-table.h"
  27. #include "routing.h"
  28. #include "hard-interface.h"
  29. static struct sk_buff *
  30. batadv_frag_merge_packet(struct list_head *head,
  31. struct frag_packet_list_entry *tfp,
  32. struct sk_buff *skb)
  33. {
  34. struct unicast_frag_packet *up =
  35. (struct unicast_frag_packet *)skb->data;
  36. struct sk_buff *tmp_skb;
  37. struct unicast_packet *unicast_packet;
  38. int hdr_len = sizeof(*unicast_packet);
  39. int uni_diff = sizeof(*up) - hdr_len;
  40. /* set skb to the first part and tmp_skb to the second part */
  41. if (up->flags & UNI_FRAG_HEAD) {
  42. tmp_skb = tfp->skb;
  43. } else {
  44. tmp_skb = skb;
  45. skb = tfp->skb;
  46. }
  47. if (skb_linearize(skb) < 0 || skb_linearize(tmp_skb) < 0)
  48. goto err;
  49. skb_pull(tmp_skb, sizeof(*up));
  50. if (pskb_expand_head(skb, 0, tmp_skb->len, GFP_ATOMIC) < 0)
  51. goto err;
  52. /* move free entry to end */
  53. tfp->skb = NULL;
  54. tfp->seqno = 0;
  55. list_move_tail(&tfp->list, head);
  56. memcpy(skb_put(skb, tmp_skb->len), tmp_skb->data, tmp_skb->len);
  57. kfree_skb(tmp_skb);
  58. memmove(skb->data + uni_diff, skb->data, hdr_len);
  59. unicast_packet = (struct unicast_packet *)skb_pull(skb, uni_diff);
  60. unicast_packet->header.packet_type = BAT_UNICAST;
  61. return skb;
  62. err:
  63. /* free buffered skb, skb will be freed later */
  64. kfree_skb(tfp->skb);
  65. return NULL;
  66. }
  67. static void batadv_frag_create_entry(struct list_head *head,
  68. struct sk_buff *skb)
  69. {
  70. struct frag_packet_list_entry *tfp;
  71. struct unicast_frag_packet *up =
  72. (struct unicast_frag_packet *)skb->data;
  73. /* free and oldest packets stand at the end */
  74. tfp = list_entry((head)->prev, typeof(*tfp), list);
  75. kfree_skb(tfp->skb);
  76. tfp->seqno = ntohs(up->seqno);
  77. tfp->skb = skb;
  78. list_move(&tfp->list, head);
  79. return;
  80. }
  81. static int batadv_frag_create_buffer(struct list_head *head)
  82. {
  83. int i;
  84. struct frag_packet_list_entry *tfp;
  85. for (i = 0; i < BATADV_FRAG_BUFFER_SIZE; i++) {
  86. tfp = kmalloc(sizeof(*tfp), GFP_ATOMIC);
  87. if (!tfp) {
  88. batadv_frag_list_free(head);
  89. return -ENOMEM;
  90. }
  91. tfp->skb = NULL;
  92. tfp->seqno = 0;
  93. INIT_LIST_HEAD(&tfp->list);
  94. list_add(&tfp->list, head);
  95. }
  96. return 0;
  97. }
  98. static struct frag_packet_list_entry *
  99. batadv_frag_search_packet(struct list_head *head,
  100. const struct unicast_frag_packet *up)
  101. {
  102. struct frag_packet_list_entry *tfp;
  103. struct unicast_frag_packet *tmp_up = NULL;
  104. uint16_t search_seqno;
  105. if (up->flags & UNI_FRAG_HEAD)
  106. search_seqno = ntohs(up->seqno)+1;
  107. else
  108. search_seqno = ntohs(up->seqno)-1;
  109. list_for_each_entry(tfp, head, list) {
  110. if (!tfp->skb)
  111. continue;
  112. if (tfp->seqno == ntohs(up->seqno))
  113. goto mov_tail;
  114. tmp_up = (struct unicast_frag_packet *)tfp->skb->data;
  115. if (tfp->seqno == search_seqno) {
  116. if ((tmp_up->flags & UNI_FRAG_HEAD) !=
  117. (up->flags & UNI_FRAG_HEAD))
  118. return tfp;
  119. else
  120. goto mov_tail;
  121. }
  122. }
  123. return NULL;
  124. mov_tail:
  125. list_move_tail(&tfp->list, head);
  126. return NULL;
  127. }
  128. void batadv_frag_list_free(struct list_head *head)
  129. {
  130. struct frag_packet_list_entry *pf, *tmp_pf;
  131. if (!list_empty(head)) {
  132. list_for_each_entry_safe(pf, tmp_pf, head, list) {
  133. kfree_skb(pf->skb);
  134. list_del(&pf->list);
  135. kfree(pf);
  136. }
  137. }
  138. return;
  139. }
  140. /* frag_reassemble_skb():
  141. * returns NET_RX_DROP if the operation failed - skb is left intact
  142. * returns NET_RX_SUCCESS if the fragment was buffered (skb_new will be NULL)
  143. * or the skb could be reassembled (skb_new will point to the new packet and
  144. * skb was freed)
  145. */
  146. int batadv_frag_reassemble_skb(struct sk_buff *skb, struct bat_priv *bat_priv,
  147. struct sk_buff **new_skb)
  148. {
  149. struct orig_node *orig_node;
  150. struct frag_packet_list_entry *tmp_frag_entry;
  151. int ret = NET_RX_DROP;
  152. struct unicast_frag_packet *unicast_packet =
  153. (struct unicast_frag_packet *)skb->data;
  154. *new_skb = NULL;
  155. orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->orig);
  156. if (!orig_node)
  157. goto out;
  158. orig_node->last_frag_packet = jiffies;
  159. if (list_empty(&orig_node->frag_list) &&
  160. batadv_frag_create_buffer(&orig_node->frag_list)) {
  161. pr_debug("couldn't create frag buffer\n");
  162. goto out;
  163. }
  164. tmp_frag_entry = batadv_frag_search_packet(&orig_node->frag_list,
  165. unicast_packet);
  166. if (!tmp_frag_entry) {
  167. batadv_frag_create_entry(&orig_node->frag_list, skb);
  168. ret = NET_RX_SUCCESS;
  169. goto out;
  170. }
  171. *new_skb = batadv_frag_merge_packet(&orig_node->frag_list,
  172. tmp_frag_entry, skb);
  173. /* if not, merge failed */
  174. if (*new_skb)
  175. ret = NET_RX_SUCCESS;
  176. out:
  177. if (orig_node)
  178. batadv_orig_node_free_ref(orig_node);
  179. return ret;
  180. }
  181. int batadv_frag_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv,
  182. struct hard_iface *hard_iface, const uint8_t dstaddr[])
  183. {
  184. struct unicast_packet tmp_uc, *unicast_packet;
  185. struct hard_iface *primary_if;
  186. struct sk_buff *frag_skb;
  187. struct unicast_frag_packet *frag1, *frag2;
  188. int uc_hdr_len = sizeof(*unicast_packet);
  189. int ucf_hdr_len = sizeof(*frag1);
  190. int data_len = skb->len - uc_hdr_len;
  191. int large_tail = 0, ret = NET_RX_DROP;
  192. uint16_t seqno;
  193. primary_if = batadv_primary_if_get_selected(bat_priv);
  194. if (!primary_if)
  195. goto dropped;
  196. frag_skb = dev_alloc_skb(data_len - (data_len / 2) + ucf_hdr_len);
  197. if (!frag_skb)
  198. goto dropped;
  199. skb_reserve(frag_skb, ucf_hdr_len);
  200. unicast_packet = (struct unicast_packet *)skb->data;
  201. memcpy(&tmp_uc, unicast_packet, uc_hdr_len);
  202. skb_split(skb, frag_skb, data_len / 2 + uc_hdr_len);
  203. if (batadv_skb_head_push(skb, ucf_hdr_len - uc_hdr_len) < 0 ||
  204. batadv_skb_head_push(frag_skb, ucf_hdr_len) < 0)
  205. goto drop_frag;
  206. frag1 = (struct unicast_frag_packet *)skb->data;
  207. frag2 = (struct unicast_frag_packet *)frag_skb->data;
  208. memcpy(frag1, &tmp_uc, sizeof(tmp_uc));
  209. frag1->header.ttl--;
  210. frag1->header.version = BATADV_COMPAT_VERSION;
  211. frag1->header.packet_type = BAT_UNICAST_FRAG;
  212. memcpy(frag1->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  213. memcpy(frag2, frag1, sizeof(*frag2));
  214. if (data_len & 1)
  215. large_tail = UNI_FRAG_LARGETAIL;
  216. frag1->flags = UNI_FRAG_HEAD | large_tail;
  217. frag2->flags = large_tail;
  218. seqno = atomic_add_return(2, &hard_iface->frag_seqno);
  219. frag1->seqno = htons(seqno - 1);
  220. frag2->seqno = htons(seqno);
  221. batadv_send_skb_packet(skb, hard_iface, dstaddr);
  222. batadv_send_skb_packet(frag_skb, hard_iface, dstaddr);
  223. ret = NET_RX_SUCCESS;
  224. goto out;
  225. drop_frag:
  226. kfree_skb(frag_skb);
  227. dropped:
  228. kfree_skb(skb);
  229. out:
  230. if (primary_if)
  231. batadv_hardif_free_ref(primary_if);
  232. return ret;
  233. }
  234. int batadv_unicast_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv)
  235. {
  236. struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
  237. struct unicast_packet *unicast_packet;
  238. struct orig_node *orig_node;
  239. struct neigh_node *neigh_node;
  240. int data_len = skb->len;
  241. int ret = 1;
  242. /* get routing information */
  243. if (is_multicast_ether_addr(ethhdr->h_dest)) {
  244. orig_node = batadv_gw_get_selected_orig(bat_priv);
  245. if (orig_node)
  246. goto find_router;
  247. }
  248. /* check for tt host - increases orig_node refcount.
  249. * returns NULL in case of AP isolation
  250. */
  251. orig_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
  252. ethhdr->h_dest);
  253. find_router:
  254. /* find_router():
  255. * - if orig_node is NULL it returns NULL
  256. * - increases neigh_nodes refcount if found.
  257. */
  258. neigh_node = batadv_find_router(bat_priv, orig_node, NULL);
  259. if (!neigh_node)
  260. goto out;
  261. if (batadv_skb_head_push(skb, sizeof(*unicast_packet)) < 0)
  262. goto out;
  263. unicast_packet = (struct unicast_packet *)skb->data;
  264. unicast_packet->header.version = BATADV_COMPAT_VERSION;
  265. /* batman packet type: unicast */
  266. unicast_packet->header.packet_type = BAT_UNICAST;
  267. /* set unicast ttl */
  268. unicast_packet->header.ttl = BATADV_TTL;
  269. /* copy the destination for faster routing */
  270. memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
  271. /* set the destination tt version number */
  272. unicast_packet->ttvn =
  273. (uint8_t)atomic_read(&orig_node->last_ttvn);
  274. /* inform the destination node that we are still missing a correct route
  275. * for this client. The destination will receive this packet and will
  276. * try to reroute it because the ttvn contained in the header is less
  277. * than the current one
  278. */
  279. if (batadv_tt_global_client_is_roaming(bat_priv, ethhdr->h_dest))
  280. unicast_packet->ttvn = unicast_packet->ttvn - 1;
  281. if (atomic_read(&bat_priv->fragmentation) &&
  282. data_len + sizeof(*unicast_packet) >
  283. neigh_node->if_incoming->net_dev->mtu) {
  284. /* send frag skb decreases ttl */
  285. unicast_packet->header.ttl++;
  286. ret = batadv_frag_send_skb(skb, bat_priv,
  287. neigh_node->if_incoming,
  288. neigh_node->addr);
  289. goto out;
  290. }
  291. batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  292. ret = 0;
  293. goto out;
  294. out:
  295. if (neigh_node)
  296. batadv_neigh_node_free_ref(neigh_node);
  297. if (orig_node)
  298. batadv_orig_node_free_ref(orig_node);
  299. if (ret == 1)
  300. kfree_skb(skb);
  301. return ret;
  302. }