unicast.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371
  1. /* Copyright (C) 2010-2012 B.A.T.M.A.N. contributors:
  2. *
  3. * Andreas Langer
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA
  18. */
  19. #include "main.h"
  20. #include "unicast.h"
  21. #include "send.h"
  22. #include "soft-interface.h"
  23. #include "gateway_client.h"
  24. #include "originator.h"
  25. #include "hash.h"
  26. #include "translation-table.h"
  27. #include "routing.h"
  28. #include "hard-interface.h"
  29. static struct sk_buff *
  30. batadv_frag_merge_packet(struct list_head *head,
  31. struct batadv_frag_packet_list_entry *tfp,
  32. struct sk_buff *skb)
  33. {
  34. struct batadv_unicast_frag_packet *up;
  35. struct sk_buff *tmp_skb;
  36. struct batadv_unicast_packet *unicast_packet;
  37. int hdr_len = sizeof(*unicast_packet);
  38. int uni_diff = sizeof(*up) - hdr_len;
  39. up = (struct batadv_unicast_frag_packet *)skb->data;
  40. /* set skb to the first part and tmp_skb to the second part */
  41. if (up->flags & BATADV_UNI_FRAG_HEAD) {
  42. tmp_skb = tfp->skb;
  43. } else {
  44. tmp_skb = skb;
  45. skb = tfp->skb;
  46. }
  47. if (skb_linearize(skb) < 0 || skb_linearize(tmp_skb) < 0)
  48. goto err;
  49. skb_pull(tmp_skb, sizeof(*up));
  50. if (pskb_expand_head(skb, 0, tmp_skb->len, GFP_ATOMIC) < 0)
  51. goto err;
  52. /* move free entry to end */
  53. tfp->skb = NULL;
  54. tfp->seqno = 0;
  55. list_move_tail(&tfp->list, head);
  56. memcpy(skb_put(skb, tmp_skb->len), tmp_skb->data, tmp_skb->len);
  57. kfree_skb(tmp_skb);
  58. memmove(skb->data + uni_diff, skb->data, hdr_len);
  59. unicast_packet = (struct batadv_unicast_packet *)skb_pull(skb,
  60. uni_diff);
  61. unicast_packet->header.packet_type = BATADV_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 batadv_frag_create_entry(struct list_head *head,
  69. struct sk_buff *skb)
  70. {
  71. struct batadv_frag_packet_list_entry *tfp;
  72. struct batadv_unicast_frag_packet *up;
  73. up = (struct batadv_unicast_frag_packet *)skb->data;
  74. /* free and oldest packets stand at the end */
  75. tfp = list_entry((head)->prev, typeof(*tfp), list);
  76. kfree_skb(tfp->skb);
  77. tfp->seqno = ntohs(up->seqno);
  78. tfp->skb = skb;
  79. list_move(&tfp->list, head);
  80. return;
  81. }
  82. static int batadv_frag_create_buffer(struct list_head *head)
  83. {
  84. int i;
  85. struct batadv_frag_packet_list_entry *tfp;
  86. for (i = 0; i < BATADV_FRAG_BUFFER_SIZE; i++) {
  87. tfp = kmalloc(sizeof(*tfp), GFP_ATOMIC);
  88. if (!tfp) {
  89. batadv_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 batadv_frag_packet_list_entry *
  100. batadv_frag_search_packet(struct list_head *head,
  101. const struct batadv_unicast_frag_packet *up)
  102. {
  103. struct batadv_frag_packet_list_entry *tfp;
  104. struct batadv_unicast_frag_packet *tmp_up = NULL;
  105. uint16_t search_seqno;
  106. if (up->flags & BATADV_UNI_FRAG_HEAD)
  107. search_seqno = ntohs(up->seqno)+1;
  108. else
  109. search_seqno = ntohs(up->seqno)-1;
  110. list_for_each_entry(tfp, head, list) {
  111. if (!tfp->skb)
  112. continue;
  113. if (tfp->seqno == ntohs(up->seqno))
  114. goto mov_tail;
  115. tmp_up = (struct batadv_unicast_frag_packet *)tfp->skb->data;
  116. if (tfp->seqno == search_seqno) {
  117. if ((tmp_up->flags & BATADV_UNI_FRAG_HEAD) !=
  118. (up->flags & BATADV_UNI_FRAG_HEAD))
  119. return tfp;
  120. else
  121. goto mov_tail;
  122. }
  123. }
  124. return NULL;
  125. mov_tail:
  126. list_move_tail(&tfp->list, head);
  127. return NULL;
  128. }
  129. void batadv_frag_list_free(struct list_head *head)
  130. {
  131. struct batadv_frag_packet_list_entry *pf, *tmp_pf;
  132. if (!list_empty(head)) {
  133. list_for_each_entry_safe(pf, tmp_pf, head, list) {
  134. kfree_skb(pf->skb);
  135. list_del(&pf->list);
  136. kfree(pf);
  137. }
  138. }
  139. return;
  140. }
  141. /* frag_reassemble_skb():
  142. * returns NET_RX_DROP if the operation failed - skb is left intact
  143. * returns NET_RX_SUCCESS if the fragment was buffered (skb_new will be NULL)
  144. * or the skb could be reassembled (skb_new will point to the new packet and
  145. * skb was freed)
  146. */
  147. int batadv_frag_reassemble_skb(struct sk_buff *skb,
  148. struct batadv_priv *bat_priv,
  149. struct sk_buff **new_skb)
  150. {
  151. struct batadv_orig_node *orig_node;
  152. struct batadv_frag_packet_list_entry *tmp_frag_entry;
  153. int ret = NET_RX_DROP;
  154. struct batadv_unicast_frag_packet *unicast_packet;
  155. unicast_packet = (struct batadv_unicast_frag_packet *)skb->data;
  156. *new_skb = NULL;
  157. orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->orig);
  158. if (!orig_node)
  159. goto out;
  160. orig_node->last_frag_packet = jiffies;
  161. if (list_empty(&orig_node->frag_list) &&
  162. batadv_frag_create_buffer(&orig_node->frag_list)) {
  163. pr_debug("couldn't create frag buffer\n");
  164. goto out;
  165. }
  166. tmp_frag_entry = batadv_frag_search_packet(&orig_node->frag_list,
  167. unicast_packet);
  168. if (!tmp_frag_entry) {
  169. batadv_frag_create_entry(&orig_node->frag_list, skb);
  170. ret = NET_RX_SUCCESS;
  171. goto out;
  172. }
  173. *new_skb = batadv_frag_merge_packet(&orig_node->frag_list,
  174. tmp_frag_entry, skb);
  175. /* if not, merge failed */
  176. if (*new_skb)
  177. ret = NET_RX_SUCCESS;
  178. out:
  179. if (orig_node)
  180. batadv_orig_node_free_ref(orig_node);
  181. return ret;
  182. }
  183. int batadv_frag_send_skb(struct sk_buff *skb, struct batadv_priv *bat_priv,
  184. struct batadv_hard_iface *hard_iface,
  185. const uint8_t dstaddr[])
  186. {
  187. struct batadv_unicast_packet tmp_uc, *unicast_packet;
  188. struct batadv_hard_iface *primary_if;
  189. struct sk_buff *frag_skb;
  190. struct batadv_unicast_frag_packet *frag1, *frag2;
  191. int uc_hdr_len = sizeof(*unicast_packet);
  192. int ucf_hdr_len = sizeof(*frag1);
  193. int data_len = skb->len - uc_hdr_len;
  194. int large_tail = 0, ret = NET_RX_DROP;
  195. uint16_t seqno;
  196. primary_if = batadv_primary_if_get_selected(bat_priv);
  197. if (!primary_if)
  198. goto dropped;
  199. frag_skb = dev_alloc_skb(data_len - (data_len / 2) + ucf_hdr_len);
  200. if (!frag_skb)
  201. goto dropped;
  202. skb_reserve(frag_skb, ucf_hdr_len);
  203. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  204. memcpy(&tmp_uc, unicast_packet, uc_hdr_len);
  205. skb_split(skb, frag_skb, data_len / 2 + uc_hdr_len);
  206. if (batadv_skb_head_push(skb, ucf_hdr_len - uc_hdr_len) < 0 ||
  207. batadv_skb_head_push(frag_skb, ucf_hdr_len) < 0)
  208. goto drop_frag;
  209. frag1 = (struct batadv_unicast_frag_packet *)skb->data;
  210. frag2 = (struct batadv_unicast_frag_packet *)frag_skb->data;
  211. memcpy(frag1, &tmp_uc, sizeof(tmp_uc));
  212. frag1->header.ttl--;
  213. frag1->header.version = BATADV_COMPAT_VERSION;
  214. frag1->header.packet_type = BATADV_UNICAST_FRAG;
  215. memcpy(frag1->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  216. memcpy(frag2, frag1, sizeof(*frag2));
  217. if (data_len & 1)
  218. large_tail = BATADV_UNI_FRAG_LARGETAIL;
  219. frag1->flags = BATADV_UNI_FRAG_HEAD | large_tail;
  220. frag2->flags = large_tail;
  221. seqno = atomic_add_return(2, &hard_iface->frag_seqno);
  222. frag1->seqno = htons(seqno - 1);
  223. frag2->seqno = htons(seqno);
  224. batadv_send_skb_packet(skb, hard_iface, dstaddr);
  225. batadv_send_skb_packet(frag_skb, hard_iface, dstaddr);
  226. ret = NET_RX_SUCCESS;
  227. goto out;
  228. drop_frag:
  229. kfree_skb(frag_skb);
  230. dropped:
  231. kfree_skb(skb);
  232. out:
  233. if (primary_if)
  234. batadv_hardif_free_ref(primary_if);
  235. return ret;
  236. }
  237. int batadv_unicast_send_skb(struct sk_buff *skb, struct batadv_priv *bat_priv)
  238. {
  239. struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
  240. struct batadv_unicast_packet *unicast_packet;
  241. struct batadv_orig_node *orig_node;
  242. struct batadv_neigh_node *neigh_node;
  243. int data_len = skb->len;
  244. int ret = 1;
  245. unsigned int dev_mtu;
  246. /* get routing information */
  247. if (is_multicast_ether_addr(ethhdr->h_dest)) {
  248. orig_node = batadv_gw_get_selected_orig(bat_priv);
  249. if (orig_node)
  250. goto find_router;
  251. }
  252. /* check for tt host - increases orig_node refcount.
  253. * returns NULL in case of AP isolation
  254. */
  255. orig_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
  256. ethhdr->h_dest);
  257. find_router:
  258. /* find_router():
  259. * - if orig_node is NULL it returns NULL
  260. * - increases neigh_nodes refcount if found.
  261. */
  262. neigh_node = batadv_find_router(bat_priv, orig_node, NULL);
  263. if (!neigh_node)
  264. goto out;
  265. if (batadv_skb_head_push(skb, sizeof(*unicast_packet)) < 0)
  266. goto out;
  267. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  268. unicast_packet->header.version = BATADV_COMPAT_VERSION;
  269. /* batman packet type: unicast */
  270. unicast_packet->header.packet_type = BATADV_UNICAST;
  271. /* set unicast ttl */
  272. unicast_packet->header.ttl = BATADV_TTL;
  273. /* copy the destination for faster routing */
  274. memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
  275. /* set the destination tt version number */
  276. unicast_packet->ttvn =
  277. (uint8_t)atomic_read(&orig_node->last_ttvn);
  278. /* inform the destination node that we are still missing a correct route
  279. * for this client. The destination will receive this packet and will
  280. * try to reroute it because the ttvn contained in the header is less
  281. * than the current one
  282. */
  283. if (batadv_tt_global_client_is_roaming(bat_priv, ethhdr->h_dest))
  284. unicast_packet->ttvn = unicast_packet->ttvn - 1;
  285. dev_mtu = neigh_node->if_incoming->net_dev->mtu;
  286. if (atomic_read(&bat_priv->fragmentation) &&
  287. data_len + sizeof(*unicast_packet) > dev_mtu) {
  288. /* send frag skb decreases ttl */
  289. unicast_packet->header.ttl++;
  290. ret = batadv_frag_send_skb(skb, bat_priv,
  291. neigh_node->if_incoming,
  292. neigh_node->addr);
  293. goto out;
  294. }
  295. batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  296. ret = 0;
  297. goto out;
  298. out:
  299. if (neigh_node)
  300. batadv_neigh_node_free_ref(neigh_node);
  301. if (orig_node)
  302. batadv_orig_node_free_ref(orig_node);
  303. if (ret == 1)
  304. kfree_skb(skb);
  305. return ret;
  306. }