unicast.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  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 hard_iface *primary_if;
  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 - uc_hdr_len;
  191. int large_tail = 0, ret = NET_RX_DROP;
  192. uint16_t seqno;
  193. primary_if = 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 (my_skb_head_push(skb, ucf_hdr_len - uc_hdr_len) < 0 ||
  204. my_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(struct unicast_packet));
  209. frag1->ttl--;
  210. frag1->version = COMPAT_VERSION;
  211. frag1->packet_type = BAT_UNICAST_FRAG;
  212. memcpy(frag1->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  213. memcpy(frag2, frag1, sizeof(struct unicast_frag_packet));
  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. send_skb_packet(skb, hard_iface, dstaddr);
  222. 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. hardif_free_ref(primary_if);
  232. return ret;
  233. }
  234. int 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 = (struct orig_node *)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. orig_node = transtable_search(bat_priv, ethhdr->h_dest);
  250. find_router:
  251. /**
  252. * find_router():
  253. * - if orig_node is NULL it returns NULL
  254. * - increases neigh_nodes refcount if found.
  255. */
  256. neigh_node = find_router(bat_priv, orig_node, NULL);
  257. if (!neigh_node)
  258. goto out;
  259. if (neigh_node->if_incoming->if_status != IF_ACTIVE)
  260. goto out;
  261. if (my_skb_head_push(skb, sizeof(struct unicast_packet)) < 0)
  262. goto out;
  263. unicast_packet = (struct unicast_packet *)skb->data;
  264. unicast_packet->version = COMPAT_VERSION;
  265. /* batman packet type: unicast */
  266. unicast_packet->packet_type = BAT_UNICAST;
  267. /* set unicast ttl */
  268. unicast_packet->ttl = TTL;
  269. /* copy the destination for faster routing */
  270. memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
  271. if (atomic_read(&bat_priv->fragmentation) &&
  272. data_len + sizeof(struct unicast_packet) >
  273. neigh_node->if_incoming->net_dev->mtu) {
  274. /* send frag skb decreases ttl */
  275. unicast_packet->ttl++;
  276. ret = frag_send_skb(skb, bat_priv,
  277. neigh_node->if_incoming, neigh_node->addr);
  278. goto out;
  279. }
  280. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  281. ret = 0;
  282. goto out;
  283. out:
  284. if (neigh_node)
  285. neigh_node_free_ref(neigh_node);
  286. if (orig_node)
  287. orig_node_free_ref(orig_node);
  288. if (ret == 1)
  289. kfree_skb(skb);
  290. return ret;
  291. }