unicast.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Copyright (C) 2010-2012 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(*unicast_packet);
  40. int uni_diff = sizeof(*up) - 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(*up));
  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->header.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(*tfp), GFP_ATOMIC);
  87. if (!tfp) {
  88. frag_list_free(head);
  89. return -ENOMEM;
  90. }
  91. tfp->skb = NULL;
  92. tfp->seqno = 0;
  93. INIT_LIST_HEAD(&tfp->list);
  94. list_add(&tfp->list, head);
  95. }
  96. return 0;
  97. }
  98. static struct frag_packet_list_entry *frag_search_packet(struct list_head *head,
  99. const struct unicast_frag_packet *up)
  100. {
  101. struct frag_packet_list_entry *tfp;
  102. struct unicast_frag_packet *tmp_up = NULL;
  103. uint16_t search_seqno;
  104. if (up->flags & UNI_FRAG_HEAD)
  105. search_seqno = ntohs(up->seqno)+1;
  106. else
  107. search_seqno = ntohs(up->seqno)-1;
  108. list_for_each_entry(tfp, head, list) {
  109. if (!tfp->skb)
  110. continue;
  111. if (tfp->seqno == ntohs(up->seqno))
  112. goto mov_tail;
  113. tmp_up = (struct unicast_frag_packet *)tfp->skb->data;
  114. if (tfp->seqno == search_seqno) {
  115. if ((tmp_up->flags & UNI_FRAG_HEAD) !=
  116. (up->flags & UNI_FRAG_HEAD))
  117. return tfp;
  118. else
  119. goto mov_tail;
  120. }
  121. }
  122. return NULL;
  123. mov_tail:
  124. list_move_tail(&tfp->list, head);
  125. return NULL;
  126. }
  127. void frag_list_free(struct list_head *head)
  128. {
  129. struct frag_packet_list_entry *pf, *tmp_pf;
  130. if (!list_empty(head)) {
  131. list_for_each_entry_safe(pf, tmp_pf, head, list) {
  132. kfree_skb(pf->skb);
  133. list_del(&pf->list);
  134. kfree(pf);
  135. }
  136. }
  137. return;
  138. }
  139. /* frag_reassemble_skb():
  140. * returns NET_RX_DROP if the operation failed - skb is left intact
  141. * returns NET_RX_SUCCESS if the fragment was buffered (skb_new will be NULL)
  142. * or the skb could be reassembled (skb_new will point to the new packet and
  143. * skb was freed)
  144. */
  145. int frag_reassemble_skb(struct sk_buff *skb, struct bat_priv *bat_priv,
  146. struct sk_buff **new_skb)
  147. {
  148. struct orig_node *orig_node;
  149. struct frag_packet_list_entry *tmp_frag_entry;
  150. int ret = NET_RX_DROP;
  151. struct unicast_frag_packet *unicast_packet =
  152. (struct unicast_frag_packet *)skb->data;
  153. *new_skb = NULL;
  154. orig_node = orig_hash_find(bat_priv, unicast_packet->orig);
  155. if (!orig_node)
  156. goto out;
  157. orig_node->last_frag_packet = jiffies;
  158. if (list_empty(&orig_node->frag_list) &&
  159. frag_create_buffer(&orig_node->frag_list)) {
  160. pr_debug("couldn't create frag buffer\n");
  161. goto out;
  162. }
  163. tmp_frag_entry = frag_search_packet(&orig_node->frag_list,
  164. unicast_packet);
  165. if (!tmp_frag_entry) {
  166. frag_create_entry(&orig_node->frag_list, skb);
  167. ret = NET_RX_SUCCESS;
  168. goto out;
  169. }
  170. *new_skb = frag_merge_packet(&orig_node->frag_list, tmp_frag_entry,
  171. skb);
  172. /* if not, merge failed */
  173. if (*new_skb)
  174. ret = NET_RX_SUCCESS;
  175. out:
  176. if (orig_node)
  177. orig_node_free_ref(orig_node);
  178. return ret;
  179. }
  180. int frag_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv,
  181. struct hard_iface *hard_iface, const uint8_t dstaddr[])
  182. {
  183. struct unicast_packet tmp_uc, *unicast_packet;
  184. struct hard_iface *primary_if;
  185. struct sk_buff *frag_skb;
  186. struct unicast_frag_packet *frag1, *frag2;
  187. int uc_hdr_len = sizeof(*unicast_packet);
  188. int ucf_hdr_len = sizeof(*frag1);
  189. int data_len = skb->len - uc_hdr_len;
  190. int large_tail = 0, ret = NET_RX_DROP;
  191. uint16_t seqno;
  192. primary_if = primary_if_get_selected(bat_priv);
  193. if (!primary_if)
  194. goto dropped;
  195. frag_skb = dev_alloc_skb(data_len - (data_len / 2) + ucf_hdr_len);
  196. if (!frag_skb)
  197. goto dropped;
  198. skb_reserve(frag_skb, ucf_hdr_len);
  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 + uc_hdr_len);
  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(tmp_uc));
  208. frag1->header.ttl--;
  209. frag1->header.version = COMPAT_VERSION;
  210. frag1->header.packet_type = BAT_UNICAST_FRAG;
  211. memcpy(frag1->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  212. memcpy(frag2, frag1, sizeof(*frag2));
  213. if (data_len & 1)
  214. large_tail = UNI_FRAG_LARGETAIL;
  215. frag1->flags = UNI_FRAG_HEAD | large_tail;
  216. frag2->flags = large_tail;
  217. seqno = atomic_add_return(2, &hard_iface->frag_seqno);
  218. frag1->seqno = htons(seqno - 1);
  219. frag2->seqno = htons(seqno);
  220. send_skb_packet(skb, hard_iface, dstaddr);
  221. send_skb_packet(frag_skb, hard_iface, dstaddr);
  222. ret = NET_RX_SUCCESS;
  223. goto out;
  224. drop_frag:
  225. kfree_skb(frag_skb);
  226. dropped:
  227. kfree_skb(skb);
  228. out:
  229. if (primary_if)
  230. hardif_free_ref(primary_if);
  231. return ret;
  232. }
  233. int unicast_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv)
  234. {
  235. struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
  236. struct unicast_packet *unicast_packet;
  237. struct orig_node *orig_node;
  238. struct neigh_node *neigh_node;
  239. int data_len = skb->len;
  240. int ret = 1;
  241. /* get routing information */
  242. if (is_multicast_ether_addr(ethhdr->h_dest)) {
  243. orig_node = gw_get_selected_orig(bat_priv);
  244. if (orig_node)
  245. goto find_router;
  246. }
  247. /* check for tt host - increases orig_node refcount.
  248. * returns NULL in case of AP isolation */
  249. orig_node = transtable_search(bat_priv, ethhdr->h_source,
  250. ethhdr->h_dest);
  251. find_router:
  252. /**
  253. * find_router():
  254. * - if orig_node is NULL it returns NULL
  255. * - increases neigh_nodes refcount if found.
  256. */
  257. neigh_node = find_router(bat_priv, orig_node, NULL);
  258. if (!neigh_node)
  259. goto out;
  260. if (my_skb_head_push(skb, sizeof(*unicast_packet)) < 0)
  261. goto out;
  262. unicast_packet = (struct unicast_packet *)skb->data;
  263. unicast_packet->header.version = COMPAT_VERSION;
  264. /* batman packet type: unicast */
  265. unicast_packet->header.packet_type = BAT_UNICAST;
  266. /* set unicast ttl */
  267. unicast_packet->header.ttl = TTL;
  268. /* copy the destination for faster routing */
  269. memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
  270. /* set the destination tt version number */
  271. unicast_packet->ttvn =
  272. (uint8_t)atomic_read(&orig_node->last_ttvn);
  273. if (atomic_read(&bat_priv->fragmentation) &&
  274. data_len + sizeof(*unicast_packet) >
  275. neigh_node->if_incoming->net_dev->mtu) {
  276. /* send frag skb decreases ttl */
  277. unicast_packet->header.ttl++;
  278. ret = frag_send_skb(skb, bat_priv,
  279. neigh_node->if_incoming, neigh_node->addr);
  280. goto out;
  281. }
  282. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  283. ret = 0;
  284. goto out;
  285. out:
  286. if (neigh_node)
  287. neigh_node_free_ref(neigh_node);
  288. if (orig_node)
  289. orig_node_free_ref(orig_node);
  290. if (ret == 1)
  291. kfree_skb(skb);
  292. return ret;
  293. }