unicast.c 9.3 KB

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