unicast.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Copyright (C) 2010-2011 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. int 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. orig_node = 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. frag_create_buffer(&orig_node->frag_list)) {
  161. pr_debug("couldn't create frag buffer\n");
  162. goto out;
  163. }
  164. tmp_frag_entry = frag_search_packet(&orig_node->frag_list,
  165. unicast_packet);
  166. if (!tmp_frag_entry) {
  167. frag_create_entry(&orig_node->frag_list, skb);
  168. ret = NET_RX_SUCCESS;
  169. goto out;
  170. }
  171. *new_skb = frag_merge_packet(&orig_node->frag_list, tmp_frag_entry,
  172. skb);
  173. /* if not, merge failed */
  174. if (*new_skb)
  175. ret = NET_RX_SUCCESS;
  176. out:
  177. if (orig_node)
  178. orig_node_free_ref(orig_node);
  179. return ret;
  180. }
  181. int frag_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv,
  182. struct hard_iface *hard_iface, uint8_t dstaddr[])
  183. {
  184. struct unicast_packet tmp_uc, *unicast_packet;
  185. struct sk_buff *frag_skb;
  186. struct unicast_frag_packet *frag1, *frag2;
  187. int uc_hdr_len = sizeof(struct unicast_packet);
  188. int ucf_hdr_len = sizeof(struct unicast_frag_packet);
  189. int data_len = skb->len - uc_hdr_len;
  190. int large_tail = 0;
  191. uint16_t seqno;
  192. if (!bat_priv->primary_if)
  193. goto dropped;
  194. frag_skb = dev_alloc_skb(data_len - (data_len / 2) + ucf_hdr_len);
  195. if (!frag_skb)
  196. goto dropped;
  197. skb_reserve(frag_skb, ucf_hdr_len);
  198. unicast_packet = (struct unicast_packet *) skb->data;
  199. memcpy(&tmp_uc, unicast_packet, uc_hdr_len);
  200. skb_split(skb, frag_skb, data_len / 2 + uc_hdr_len);
  201. if (my_skb_head_push(skb, ucf_hdr_len - uc_hdr_len) < 0 ||
  202. my_skb_head_push(frag_skb, ucf_hdr_len) < 0)
  203. goto drop_frag;
  204. frag1 = (struct unicast_frag_packet *)skb->data;
  205. frag2 = (struct unicast_frag_packet *)frag_skb->data;
  206. memcpy(frag1, &tmp_uc, sizeof(struct unicast_packet));
  207. frag1->ttl--;
  208. frag1->version = COMPAT_VERSION;
  209. frag1->packet_type = BAT_UNICAST_FRAG;
  210. memcpy(frag1->orig, bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
  211. memcpy(frag2, frag1, sizeof(struct unicast_frag_packet));
  212. if (data_len & 1)
  213. large_tail = UNI_FRAG_LARGETAIL;
  214. frag1->flags = UNI_FRAG_HEAD | large_tail;
  215. frag2->flags = large_tail;
  216. seqno = atomic_add_return(2, &hard_iface->frag_seqno);
  217. frag1->seqno = htons(seqno - 1);
  218. frag2->seqno = htons(seqno);
  219. send_skb_packet(skb, hard_iface, dstaddr);
  220. send_skb_packet(frag_skb, hard_iface, 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 neigh_node *neigh_node;
  234. int data_len = skb->len;
  235. int ret = 1;
  236. /* get routing information */
  237. if (is_multicast_ether_addr(ethhdr->h_dest)) {
  238. orig_node = (struct orig_node *)gw_get_selected(bat_priv);
  239. if (orig_node)
  240. goto find_router;
  241. }
  242. /* check for hna host - increases orig_node refcount */
  243. orig_node = transtable_search(bat_priv, ethhdr->h_dest);
  244. find_router:
  245. /**
  246. * find_router():
  247. * - if orig_node is NULL it returns NULL
  248. * - increases neigh_nodes refcount if found.
  249. */
  250. neigh_node = find_router(bat_priv, orig_node, NULL);
  251. if (!neigh_node)
  252. goto out;
  253. if (neigh_node->if_incoming->if_status != IF_ACTIVE)
  254. goto out;
  255. if (my_skb_head_push(skb, sizeof(struct unicast_packet)) < 0)
  256. goto out;
  257. unicast_packet = (struct unicast_packet *)skb->data;
  258. unicast_packet->version = COMPAT_VERSION;
  259. /* batman packet type: unicast */
  260. unicast_packet->packet_type = BAT_UNICAST;
  261. /* set unicast ttl */
  262. unicast_packet->ttl = TTL;
  263. /* copy the destination for faster routing */
  264. memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
  265. if (atomic_read(&bat_priv->fragmentation) &&
  266. data_len + sizeof(struct unicast_packet) >
  267. neigh_node->if_incoming->net_dev->mtu) {
  268. /* send frag skb decreases ttl */
  269. unicast_packet->ttl++;
  270. ret = frag_send_skb(skb, bat_priv,
  271. neigh_node->if_incoming, neigh_node->addr);
  272. goto out;
  273. }
  274. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  275. ret = 0;
  276. goto out;
  277. out:
  278. if (neigh_node)
  279. neigh_node_free_ref(neigh_node);
  280. if (orig_node)
  281. orig_node_free_ref(orig_node);
  282. if (ret == 1)
  283. kfree_skb(skb);
  284. return ret;
  285. }