unicast.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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. if (skb_linearize(skb) < 0 || skb_linearize(tmp_skb) < 0)
  49. goto err;
  50. skb_pull(tmp_skb, sizeof(struct unicast_frag_packet));
  51. if (pskb_expand_head(skb, 0, tmp_skb->len, GFP_ATOMIC) < 0)
  52. goto err;
  53. /* move free entry to end */
  54. tfp->skb = NULL;
  55. tfp->seqno = 0;
  56. list_move_tail(&tfp->list, head);
  57. memcpy(skb_put(skb, tmp_skb->len), tmp_skb->data, tmp_skb->len);
  58. kfree_skb(tmp_skb);
  59. memmove(skb->data + uni_diff, skb->data, hdr_len);
  60. unicast_packet = (struct unicast_packet *) skb_pull(skb, uni_diff);
  61. unicast_packet->packet_type = BAT_UNICAST;
  62. return skb;
  63. err:
  64. /* free buffered skb, skb will be freed later */
  65. kfree_skb(tfp->skb);
  66. return NULL;
  67. }
  68. static void frag_create_entry(struct list_head *head, 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 frag_create_buffer(struct list_head *head)
  82. {
  83. int i;
  84. struct frag_packet_list_entry *tfp;
  85. for (i = 0; i < FRAG_BUFFER_SIZE; i++) {
  86. tfp = kmalloc(sizeof(struct frag_packet_list_entry),
  87. GFP_ATOMIC);
  88. if (!tfp) {
  89. frag_list_free(head);
  90. return -ENOMEM;
  91. }
  92. tfp->skb = NULL;
  93. tfp->seqno = 0;
  94. INIT_LIST_HEAD(&tfp->list);
  95. list_add(&tfp->list, head);
  96. }
  97. return 0;
  98. }
  99. static struct frag_packet_list_entry *frag_search_packet(struct list_head *head,
  100. 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 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 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. spin_lock_bh(&bat_priv->orig_hash_lock);
  156. orig_node = ((struct orig_node *)
  157. hash_find(bat_priv->orig_hash, compare_orig, choose_orig,
  158. unicast_packet->orig));
  159. if (!orig_node) {
  160. pr_debug("couldn't find originator in orig_hash\n");
  161. goto out;
  162. }
  163. orig_node->last_frag_packet = jiffies;
  164. if (list_empty(&orig_node->frag_list) &&
  165. frag_create_buffer(&orig_node->frag_list)) {
  166. pr_debug("couldn't create frag buffer\n");
  167. goto out;
  168. }
  169. tmp_frag_entry = frag_search_packet(&orig_node->frag_list,
  170. unicast_packet);
  171. if (!tmp_frag_entry) {
  172. frag_create_entry(&orig_node->frag_list, skb);
  173. ret = NET_RX_SUCCESS;
  174. goto out;
  175. }
  176. *new_skb = frag_merge_packet(&orig_node->frag_list, tmp_frag_entry,
  177. skb);
  178. /* if not, merge failed */
  179. if (*new_skb)
  180. ret = NET_RX_SUCCESS;
  181. out:
  182. spin_unlock_bh(&bat_priv->orig_hash_lock);
  183. return ret;
  184. }
  185. int frag_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv,
  186. struct batman_if *batman_if, uint8_t dstaddr[])
  187. {
  188. struct unicast_packet tmp_uc, *unicast_packet;
  189. struct sk_buff *frag_skb;
  190. struct unicast_frag_packet *frag1, *frag2;
  191. int uc_hdr_len = sizeof(struct unicast_packet);
  192. int ucf_hdr_len = sizeof(struct unicast_frag_packet);
  193. int data_len = skb->len;
  194. if (!bat_priv->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. unicast_packet = (struct unicast_packet *) skb->data;
  200. memcpy(&tmp_uc, unicast_packet, uc_hdr_len);
  201. skb_split(skb, frag_skb, data_len / 2);
  202. if (my_skb_head_push(skb, ucf_hdr_len - uc_hdr_len) < 0 ||
  203. my_skb_head_push(frag_skb, ucf_hdr_len) < 0)
  204. goto drop_frag;
  205. frag1 = (struct unicast_frag_packet *)skb->data;
  206. frag2 = (struct unicast_frag_packet *)frag_skb->data;
  207. memcpy(frag1, &tmp_uc, sizeof(struct unicast_packet));
  208. frag1->ttl--;
  209. frag1->version = COMPAT_VERSION;
  210. frag1->packet_type = BAT_UNICAST_FRAG;
  211. memcpy(frag1->orig, bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
  212. memcpy(frag2, frag1, sizeof(struct unicast_frag_packet));
  213. frag1->flags |= UNI_FRAG_HEAD;
  214. frag2->flags &= ~UNI_FRAG_HEAD;
  215. frag1->seqno = htons((uint16_t)atomic_inc_return(
  216. &batman_if->frag_seqno));
  217. frag2->seqno = htons((uint16_t)atomic_inc_return(
  218. &batman_if->frag_seqno));
  219. send_skb_packet(skb, batman_if, dstaddr);
  220. send_skb_packet(frag_skb, batman_if, dstaddr);
  221. return NET_RX_SUCCESS;
  222. drop_frag:
  223. kfree_skb(frag_skb);
  224. dropped:
  225. kfree_skb(skb);
  226. return NET_RX_DROP;
  227. }
  228. int unicast_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv)
  229. {
  230. struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
  231. struct unicast_packet *unicast_packet;
  232. struct orig_node *orig_node;
  233. struct batman_if *batman_if;
  234. struct neigh_node *router;
  235. int data_len = skb->len;
  236. uint8_t dstaddr[6];
  237. spin_lock_bh(&bat_priv->orig_hash_lock);
  238. /* get routing information */
  239. if (is_multicast_ether_addr(ethhdr->h_dest))
  240. orig_node = (struct orig_node *)gw_get_selected(bat_priv);
  241. else
  242. orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
  243. compare_orig,
  244. choose_orig,
  245. ethhdr->h_dest));
  246. /* check for hna host */
  247. if (!orig_node)
  248. orig_node = transtable_search(bat_priv, ethhdr->h_dest);
  249. router = find_router(bat_priv, orig_node, NULL);
  250. if (!router)
  251. goto unlock;
  252. /* don't lock while sending the packets ... we therefore
  253. * copy the required data before sending */
  254. batman_if = router->if_incoming;
  255. memcpy(dstaddr, router->addr, ETH_ALEN);
  256. spin_unlock_bh(&bat_priv->orig_hash_lock);
  257. if (batman_if->if_status != IF_ACTIVE)
  258. goto dropped;
  259. if (my_skb_head_push(skb, sizeof(struct unicast_packet)) < 0)
  260. goto dropped;
  261. unicast_packet = (struct unicast_packet *)skb->data;
  262. unicast_packet->version = COMPAT_VERSION;
  263. /* batman packet type: unicast */
  264. unicast_packet->packet_type = BAT_UNICAST;
  265. /* set unicast ttl */
  266. unicast_packet->ttl = TTL;
  267. /* copy the destination for faster routing */
  268. memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
  269. if (atomic_read(&bat_priv->fragmentation) &&
  270. data_len + sizeof(struct unicast_packet) >
  271. batman_if->net_dev->mtu) {
  272. /* send frag skb decreases ttl */
  273. unicast_packet->ttl++;
  274. return frag_send_skb(skb, bat_priv, batman_if,
  275. dstaddr);
  276. }
  277. send_skb_packet(skb, batman_if, dstaddr);
  278. return 0;
  279. unlock:
  280. spin_unlock_bh(&bat_priv->orig_hash_lock);
  281. dropped:
  282. kfree_skb(skb);
  283. return 1;
  284. }