unicast.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  1. /* Copyright (C) 2010-2013 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. uint8_t *packet_pos;
  40. up = (struct batadv_unicast_frag_packet *)skb->data;
  41. /* set skb to the first part and tmp_skb to the second part */
  42. if (up->flags & BATADV_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. packet_pos = skb_pull(skb, uni_diff);
  61. unicast_packet = (struct batadv_unicast_packet *)packet_pos;
  62. unicast_packet->header.packet_type = BATADV_UNICAST;
  63. return skb;
  64. err:
  65. /* free buffered skb, skb will be freed later */
  66. kfree_skb(tfp->skb);
  67. return NULL;
  68. }
  69. static void batadv_frag_create_entry(struct list_head *head,
  70. struct sk_buff *skb)
  71. {
  72. struct batadv_frag_packet_list_entry *tfp;
  73. struct batadv_unicast_frag_packet *up;
  74. up = (struct batadv_unicast_frag_packet *)skb->data;
  75. /* free and oldest packets stand at the end */
  76. tfp = list_entry((head)->prev, typeof(*tfp), list);
  77. kfree_skb(tfp->skb);
  78. tfp->seqno = ntohs(up->seqno);
  79. tfp->skb = skb;
  80. list_move(&tfp->list, head);
  81. return;
  82. }
  83. static int batadv_frag_create_buffer(struct list_head *head)
  84. {
  85. int i;
  86. struct batadv_frag_packet_list_entry *tfp;
  87. for (i = 0; i < BATADV_FRAG_BUFFER_SIZE; i++) {
  88. tfp = kmalloc(sizeof(*tfp), GFP_ATOMIC);
  89. if (!tfp) {
  90. batadv_frag_list_free(head);
  91. return -ENOMEM;
  92. }
  93. tfp->skb = NULL;
  94. tfp->seqno = 0;
  95. INIT_LIST_HEAD(&tfp->list);
  96. list_add(&tfp->list, head);
  97. }
  98. return 0;
  99. }
  100. static struct batadv_frag_packet_list_entry *
  101. batadv_frag_search_packet(struct list_head *head,
  102. const struct batadv_unicast_frag_packet *up)
  103. {
  104. struct batadv_frag_packet_list_entry *tfp;
  105. struct batadv_unicast_frag_packet *tmp_up = NULL;
  106. int is_head_tmp, is_head;
  107. uint16_t search_seqno;
  108. if (up->flags & BATADV_UNI_FRAG_HEAD)
  109. search_seqno = ntohs(up->seqno)+1;
  110. else
  111. search_seqno = ntohs(up->seqno)-1;
  112. is_head = !!(up->flags & BATADV_UNI_FRAG_HEAD);
  113. list_for_each_entry(tfp, head, list) {
  114. if (!tfp->skb)
  115. continue;
  116. if (tfp->seqno == ntohs(up->seqno))
  117. goto mov_tail;
  118. tmp_up = (struct batadv_unicast_frag_packet *)tfp->skb->data;
  119. if (tfp->seqno == search_seqno) {
  120. is_head_tmp = !!(tmp_up->flags & BATADV_UNI_FRAG_HEAD);
  121. if (is_head_tmp != is_head)
  122. return tfp;
  123. else
  124. goto mov_tail;
  125. }
  126. }
  127. return NULL;
  128. mov_tail:
  129. list_move_tail(&tfp->list, head);
  130. return NULL;
  131. }
  132. void batadv_frag_list_free(struct list_head *head)
  133. {
  134. struct batadv_frag_packet_list_entry *pf, *tmp_pf;
  135. if (!list_empty(head)) {
  136. list_for_each_entry_safe(pf, tmp_pf, head, list) {
  137. kfree_skb(pf->skb);
  138. list_del(&pf->list);
  139. kfree(pf);
  140. }
  141. }
  142. return;
  143. }
  144. /* frag_reassemble_skb():
  145. * returns NET_RX_DROP if the operation failed - skb is left intact
  146. * returns NET_RX_SUCCESS if the fragment was buffered (skb_new will be NULL)
  147. * or the skb could be reassembled (skb_new will point to the new packet and
  148. * skb was freed)
  149. */
  150. int batadv_frag_reassemble_skb(struct sk_buff *skb,
  151. struct batadv_priv *bat_priv,
  152. struct sk_buff **new_skb)
  153. {
  154. struct batadv_orig_node *orig_node;
  155. struct batadv_frag_packet_list_entry *tmp_frag_entry;
  156. int ret = NET_RX_DROP;
  157. struct batadv_unicast_frag_packet *unicast_packet;
  158. unicast_packet = (struct batadv_unicast_frag_packet *)skb->data;
  159. *new_skb = NULL;
  160. orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->orig);
  161. if (!orig_node)
  162. goto out;
  163. orig_node->last_frag_packet = jiffies;
  164. if (list_empty(&orig_node->frag_list) &&
  165. batadv_frag_create_buffer(&orig_node->frag_list)) {
  166. pr_debug("couldn't create frag buffer\n");
  167. goto out;
  168. }
  169. tmp_frag_entry = batadv_frag_search_packet(&orig_node->frag_list,
  170. unicast_packet);
  171. if (!tmp_frag_entry) {
  172. batadv_frag_create_entry(&orig_node->frag_list, skb);
  173. ret = NET_RX_SUCCESS;
  174. goto out;
  175. }
  176. *new_skb = batadv_frag_merge_packet(&orig_node->frag_list,
  177. tmp_frag_entry, skb);
  178. /* if not, merge failed */
  179. if (*new_skb)
  180. ret = NET_RX_SUCCESS;
  181. out:
  182. if (orig_node)
  183. batadv_orig_node_free_ref(orig_node);
  184. return ret;
  185. }
  186. int batadv_frag_send_skb(struct sk_buff *skb, struct batadv_priv *bat_priv,
  187. struct batadv_hard_iface *hard_iface,
  188. const uint8_t dstaddr[])
  189. {
  190. struct batadv_unicast_packet tmp_uc, *unicast_packet;
  191. struct batadv_hard_iface *primary_if;
  192. struct sk_buff *frag_skb;
  193. struct batadv_unicast_frag_packet *frag1, *frag2;
  194. int uc_hdr_len = sizeof(*unicast_packet);
  195. int ucf_hdr_len = sizeof(*frag1);
  196. int data_len = skb->len - uc_hdr_len;
  197. int large_tail = 0, ret = NET_RX_DROP;
  198. uint16_t seqno;
  199. primary_if = batadv_primary_if_get_selected(bat_priv);
  200. if (!primary_if)
  201. goto dropped;
  202. frag_skb = dev_alloc_skb(data_len - (data_len / 2) + ucf_hdr_len);
  203. if (!frag_skb)
  204. goto dropped;
  205. skb_reserve(frag_skb, ucf_hdr_len);
  206. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  207. memcpy(&tmp_uc, unicast_packet, uc_hdr_len);
  208. skb_split(skb, frag_skb, data_len / 2 + uc_hdr_len);
  209. if (batadv_skb_head_push(skb, ucf_hdr_len - uc_hdr_len) < 0 ||
  210. batadv_skb_head_push(frag_skb, ucf_hdr_len) < 0)
  211. goto drop_frag;
  212. frag1 = (struct batadv_unicast_frag_packet *)skb->data;
  213. frag2 = (struct batadv_unicast_frag_packet *)frag_skb->data;
  214. memcpy(frag1, &tmp_uc, sizeof(tmp_uc));
  215. frag1->header.ttl--;
  216. frag1->header.version = BATADV_COMPAT_VERSION;
  217. frag1->header.packet_type = BATADV_UNICAST_FRAG;
  218. memcpy(frag1->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  219. memcpy(frag2, frag1, sizeof(*frag2));
  220. if (data_len & 1)
  221. large_tail = BATADV_UNI_FRAG_LARGETAIL;
  222. frag1->flags = BATADV_UNI_FRAG_HEAD | large_tail;
  223. frag2->flags = large_tail;
  224. seqno = atomic_add_return(2, &hard_iface->frag_seqno);
  225. frag1->seqno = htons(seqno - 1);
  226. frag2->seqno = htons(seqno);
  227. batadv_send_skb_packet(skb, hard_iface, dstaddr);
  228. batadv_send_skb_packet(frag_skb, hard_iface, dstaddr);
  229. ret = NET_RX_SUCCESS;
  230. goto out;
  231. drop_frag:
  232. kfree_skb(frag_skb);
  233. dropped:
  234. kfree_skb(skb);
  235. out:
  236. if (primary_if)
  237. batadv_hardif_free_ref(primary_if);
  238. return ret;
  239. }
  240. /**
  241. * batadv_unicast_push_and_fill_skb - extends the buffer and initializes the
  242. * common fields for unicast packets
  243. * @skb: packet
  244. * @hdr_size: amount of bytes to push at the beginning of the skb
  245. * @orig_node: the destination node
  246. *
  247. * Returns false if the buffer extension was not possible or true otherwise
  248. */
  249. static bool batadv_unicast_push_and_fill_skb(struct sk_buff *skb, int hdr_size,
  250. struct batadv_orig_node *orig_node)
  251. {
  252. struct batadv_unicast_packet *unicast_packet;
  253. uint8_t ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  254. if (batadv_skb_head_push(skb, hdr_size) < 0)
  255. return false;
  256. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  257. unicast_packet->header.version = BATADV_COMPAT_VERSION;
  258. /* batman packet type: unicast */
  259. unicast_packet->header.packet_type = BATADV_UNICAST;
  260. /* set unicast ttl */
  261. unicast_packet->header.ttl = BATADV_TTL;
  262. /* copy the destination for faster routing */
  263. memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
  264. /* set the destination tt version number */
  265. unicast_packet->ttvn = ttvn;
  266. return true;
  267. }
  268. /**
  269. * batadv_unicast_prepare_skb - encapsulate an skb with a unicast header
  270. * @skb: the skb containing the payload to encapsulate
  271. * @orig_node: the destination node
  272. *
  273. * Returns false if the payload could not be encapsulated or true otherwise
  274. */
  275. static bool batadv_unicast_prepare_skb(struct sk_buff *skb,
  276. struct batadv_orig_node *orig_node)
  277. {
  278. size_t uni_size = sizeof(struct batadv_unicast_packet);
  279. return batadv_unicast_push_and_fill_skb(skb, uni_size, orig_node);
  280. }
  281. /**
  282. * batadv_unicast_4addr_prepare_skb - encapsulate an skb with a unicast4addr
  283. * header
  284. * @bat_priv: the bat priv with all the soft interface information
  285. * @skb: the skb containing the payload to encapsulate
  286. * @orig_node: the destination node
  287. * @packet_subtype: the batman 4addr packet subtype to use
  288. *
  289. * Returns false if the payload could not be encapsulated or true otherwise
  290. */
  291. bool batadv_unicast_4addr_prepare_skb(struct batadv_priv *bat_priv,
  292. struct sk_buff *skb,
  293. struct batadv_orig_node *orig,
  294. int packet_subtype)
  295. {
  296. struct batadv_hard_iface *primary_if;
  297. struct batadv_unicast_4addr_packet *unicast_4addr_packet;
  298. bool ret = false;
  299. primary_if = batadv_primary_if_get_selected(bat_priv);
  300. if (!primary_if)
  301. goto out;
  302. /* pull the header space and fill the unicast_packet substructure.
  303. * We can do that because the first member of the unicast_4addr_packet
  304. * is of type struct unicast_packet
  305. */
  306. if (!batadv_unicast_push_and_fill_skb(skb,
  307. sizeof(*unicast_4addr_packet),
  308. orig))
  309. goto out;
  310. unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
  311. unicast_4addr_packet->u.header.packet_type = BATADV_UNICAST_4ADDR;
  312. memcpy(unicast_4addr_packet->src, primary_if->net_dev->dev_addr,
  313. ETH_ALEN);
  314. unicast_4addr_packet->subtype = packet_subtype;
  315. unicast_4addr_packet->reserved = 0;
  316. ret = true;
  317. out:
  318. if (primary_if)
  319. batadv_hardif_free_ref(primary_if);
  320. return ret;
  321. }
  322. /**
  323. * batadv_unicast_generic_send_skb - send an skb as unicast
  324. * @bat_priv: the bat priv with all the soft interface information
  325. * @skb: payload to send
  326. * @packet_type: the batman unicast packet type to use
  327. * @packet_subtype: the batman packet subtype. It is ignored if packet_type is
  328. * not BATADV_UNICAT_4ADDR
  329. *
  330. * Returns 1 in case of error or 0 otherwise
  331. */
  332. int batadv_unicast_generic_send_skb(struct batadv_priv *bat_priv,
  333. struct sk_buff *skb, int packet_type,
  334. int packet_subtype)
  335. {
  336. struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
  337. struct batadv_unicast_packet *unicast_packet;
  338. struct batadv_orig_node *orig_node;
  339. struct batadv_neigh_node *neigh_node;
  340. int data_len = skb->len;
  341. int ret = NET_RX_DROP;
  342. unsigned int dev_mtu;
  343. /* get routing information */
  344. if (is_multicast_ether_addr(ethhdr->h_dest)) {
  345. orig_node = batadv_gw_get_selected_orig(bat_priv);
  346. if (orig_node)
  347. goto find_router;
  348. }
  349. /* check for tt host - increases orig_node refcount.
  350. * returns NULL in case of AP isolation
  351. */
  352. orig_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
  353. ethhdr->h_dest);
  354. find_router:
  355. /* find_router():
  356. * - if orig_node is NULL it returns NULL
  357. * - increases neigh_nodes refcount if found.
  358. */
  359. neigh_node = batadv_find_router(bat_priv, orig_node, NULL);
  360. if (!neigh_node)
  361. goto out;
  362. switch (packet_type) {
  363. case BATADV_UNICAST:
  364. batadv_unicast_prepare_skb(skb, orig_node);
  365. break;
  366. case BATADV_UNICAST_4ADDR:
  367. batadv_unicast_4addr_prepare_skb(bat_priv, skb, orig_node,
  368. packet_subtype);
  369. break;
  370. default:
  371. /* this function supports UNICAST and UNICAST_4ADDR only. It
  372. * should never be invoked with any other packet type
  373. */
  374. goto out;
  375. }
  376. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  377. /* inform the destination node that we are still missing a correct route
  378. * for this client. The destination will receive this packet and will
  379. * try to reroute it because the ttvn contained in the header is less
  380. * than the current one
  381. */
  382. if (batadv_tt_global_client_is_roaming(bat_priv, ethhdr->h_dest))
  383. unicast_packet->ttvn = unicast_packet->ttvn - 1;
  384. dev_mtu = neigh_node->if_incoming->net_dev->mtu;
  385. /* fragmentation mechanism only works for UNICAST (now) */
  386. if (packet_type == BATADV_UNICAST &&
  387. atomic_read(&bat_priv->fragmentation) &&
  388. data_len + sizeof(*unicast_packet) > dev_mtu) {
  389. /* send frag skb decreases ttl */
  390. unicast_packet->header.ttl++;
  391. ret = batadv_frag_send_skb(skb, bat_priv,
  392. neigh_node->if_incoming,
  393. neigh_node->addr);
  394. goto out;
  395. }
  396. if (batadv_send_skb_to_orig(skb, orig_node, NULL))
  397. ret = 0;
  398. out:
  399. if (neigh_node)
  400. batadv_neigh_node_free_ref(neigh_node);
  401. if (orig_node)
  402. batadv_orig_node_free_ref(orig_node);
  403. if (ret == NET_RX_DROP)
  404. kfree_skb(skb);
  405. return ret;
  406. }