unicast.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /*
  2. * Copyright (C) 2010 B.A.T.M.A.N. contributors:
  3. *
  4. * Andreas Langer
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA
  19. *
  20. */
  21. #include "main.h"
  22. #include "unicast.h"
  23. #include "send.h"
  24. #include "soft-interface.h"
  25. #include "gateway_client.h"
  26. #include "originator.h"
  27. #include "hash.h"
  28. #include "translation-table.h"
  29. #include "routing.h"
  30. #include "hard-interface.h"
  31. static struct sk_buff *frag_merge_packet(struct list_head *head,
  32. struct frag_packet_list_entry *tfp,
  33. struct sk_buff *skb)
  34. {
  35. struct unicast_frag_packet *up =
  36. (struct unicast_frag_packet *)skb->data;
  37. struct sk_buff *tmp_skb;
  38. struct unicast_packet *unicast_packet;
  39. int hdr_len = sizeof(struct unicast_packet),
  40. uni_diff = sizeof(struct unicast_frag_packet) - hdr_len;
  41. /* set skb to the first part and tmp_skb to the second part */
  42. if (up->flags & UNI_FRAG_HEAD) {
  43. tmp_skb = tfp->skb;
  44. } else {
  45. tmp_skb = skb;
  46. skb = tfp->skb;
  47. }
  48. skb_pull(tmp_skb, sizeof(struct unicast_frag_packet));
  49. if (pskb_expand_head(skb, 0, tmp_skb->len, GFP_ATOMIC) < 0) {
  50. /* free buffered skb, skb will be freed later */
  51. kfree_skb(tfp->skb);
  52. return NULL;
  53. }
  54. /* move free entry to end */
  55. tfp->skb = NULL;
  56. tfp->seqno = 0;
  57. list_move_tail(&tfp->list, head);
  58. memcpy(skb_put(skb, tmp_skb->len), tmp_skb->data, tmp_skb->len);
  59. kfree_skb(tmp_skb);
  60. memmove(skb->data + uni_diff, skb->data, hdr_len);
  61. unicast_packet = (struct unicast_packet *) skb_pull(skb, uni_diff);
  62. unicast_packet->packet_type = BAT_UNICAST;
  63. return skb;
  64. }
  65. static void frag_create_entry(struct list_head *head, struct sk_buff *skb)
  66. {
  67. struct frag_packet_list_entry *tfp;
  68. struct unicast_frag_packet *up =
  69. (struct unicast_frag_packet *)skb->data;
  70. /* free and oldest packets stand at the end */
  71. tfp = list_entry((head)->prev, typeof(*tfp), list);
  72. kfree_skb(tfp->skb);
  73. tfp->seqno = ntohs(up->seqno);
  74. tfp->skb = skb;
  75. list_move(&tfp->list, head);
  76. return;
  77. }
  78. static int frag_create_buffer(struct list_head *head)
  79. {
  80. int i;
  81. struct frag_packet_list_entry *tfp;
  82. for (i = 0; i < FRAG_BUFFER_SIZE; i++) {
  83. tfp = kmalloc(sizeof(struct frag_packet_list_entry),
  84. GFP_ATOMIC);
  85. if (!tfp) {
  86. 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. 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 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 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. spin_lock_bh(&bat_priv->orig_hash_lock);
  153. orig_node = ((struct orig_node *)
  154. hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
  155. unicast_packet->orig));
  156. if (!orig_node) {
  157. pr_debug("couldn't find originator in orig_hash\n");
  158. goto out;
  159. }
  160. orig_node->last_frag_packet = jiffies;
  161. if (list_empty(&orig_node->frag_list) &&
  162. frag_create_buffer(&orig_node->frag_list)) {
  163. pr_debug("couldn't create frag buffer\n");
  164. goto out;
  165. }
  166. tmp_frag_entry = frag_search_packet(&orig_node->frag_list,
  167. unicast_packet);
  168. if (!tmp_frag_entry) {
  169. frag_create_entry(&orig_node->frag_list, skb);
  170. ret = NET_RX_SUCCESS;
  171. goto out;
  172. }
  173. *new_skb = frag_merge_packet(&orig_node->frag_list, tmp_frag_entry,
  174. skb);
  175. /* if not, merge failed */
  176. if (*new_skb)
  177. ret = NET_RX_SUCCESS;
  178. out:
  179. spin_unlock_bh(&bat_priv->orig_hash_lock);
  180. return ret;
  181. }
  182. int frag_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv,
  183. struct batman_if *batman_if, uint8_t dstaddr[])
  184. {
  185. struct unicast_packet tmp_uc, *unicast_packet;
  186. struct sk_buff *frag_skb;
  187. struct unicast_frag_packet *frag1, *frag2;
  188. int uc_hdr_len = sizeof(struct unicast_packet);
  189. int ucf_hdr_len = sizeof(struct unicast_frag_packet);
  190. int data_len = skb->len;
  191. if (!bat_priv->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. unicast_packet = (struct unicast_packet *) skb->data;
  197. memcpy(&tmp_uc, unicast_packet, uc_hdr_len);
  198. skb_split(skb, frag_skb, data_len / 2);
  199. if (my_skb_head_push(skb, ucf_hdr_len - uc_hdr_len) < 0 ||
  200. my_skb_head_push(frag_skb, ucf_hdr_len) < 0)
  201. goto drop_frag;
  202. frag1 = (struct unicast_frag_packet *)skb->data;
  203. frag2 = (struct unicast_frag_packet *)frag_skb->data;
  204. memcpy(frag1, &tmp_uc, sizeof(struct unicast_packet));
  205. frag1->ttl--;
  206. frag1->version = COMPAT_VERSION;
  207. frag1->packet_type = BAT_UNICAST_FRAG;
  208. memcpy(frag1->orig, bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
  209. memcpy(frag2, frag1, sizeof(struct unicast_frag_packet));
  210. frag1->flags |= UNI_FRAG_HEAD;
  211. frag2->flags &= ~UNI_FRAG_HEAD;
  212. frag1->seqno = htons((uint16_t)atomic_inc_return(
  213. &batman_if->frag_seqno));
  214. frag2->seqno = htons((uint16_t)atomic_inc_return(
  215. &batman_if->frag_seqno));
  216. send_skb_packet(skb, batman_if, dstaddr);
  217. send_skb_packet(frag_skb, batman_if, dstaddr);
  218. return NET_RX_SUCCESS;
  219. drop_frag:
  220. kfree_skb(frag_skb);
  221. dropped:
  222. kfree_skb(skb);
  223. return NET_RX_DROP;
  224. }
  225. int unicast_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv)
  226. {
  227. struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
  228. struct unicast_packet *unicast_packet;
  229. struct orig_node *orig_node;
  230. struct batman_if *batman_if;
  231. struct neigh_node *router;
  232. int data_len = skb->len;
  233. uint8_t dstaddr[6];
  234. spin_lock_bh(&bat_priv->orig_hash_lock);
  235. /* get routing information */
  236. if (is_multicast_ether_addr(ethhdr->h_dest))
  237. orig_node = (struct orig_node *)gw_get_selected(bat_priv);
  238. else
  239. orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
  240. compare_orig,
  241. choose_orig,
  242. ethhdr->h_dest));
  243. /* check for hna host */
  244. if (!orig_node)
  245. orig_node = transtable_search(bat_priv, ethhdr->h_dest);
  246. router = find_router(bat_priv, orig_node, NULL);
  247. if (!router)
  248. goto unlock;
  249. /* don't lock while sending the packets ... we therefore
  250. * copy the required data before sending */
  251. batman_if = router->if_incoming;
  252. memcpy(dstaddr, router->addr, ETH_ALEN);
  253. spin_unlock_bh(&bat_priv->orig_hash_lock);
  254. if (batman_if->if_status != IF_ACTIVE)
  255. goto dropped;
  256. if (my_skb_head_push(skb, sizeof(struct unicast_packet)) < 0)
  257. goto dropped;
  258. unicast_packet = (struct unicast_packet *)skb->data;
  259. unicast_packet->version = COMPAT_VERSION;
  260. /* batman packet type: unicast */
  261. unicast_packet->packet_type = BAT_UNICAST;
  262. /* set unicast ttl */
  263. unicast_packet->ttl = TTL;
  264. /* copy the destination for faster routing */
  265. memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
  266. if (atomic_read(&bat_priv->fragmentation) &&
  267. data_len + sizeof(struct unicast_packet) >
  268. batman_if->net_dev->mtu) {
  269. /* send frag skb decreases ttl */
  270. unicast_packet->ttl++;
  271. return frag_send_skb(skb, bat_priv, batman_if,
  272. dstaddr);
  273. }
  274. send_skb_packet(skb, batman_if, dstaddr);
  275. return 0;
  276. unlock:
  277. spin_unlock_bh(&bat_priv->orig_hash_lock);
  278. dropped:
  279. kfree_skb(skb);
  280. return 1;
  281. }