send.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570
  1. /* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors:
  2. *
  3. * Marek Lindner, Simon Wunderlich
  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 "distributed-arp-table.h"
  21. #include "send.h"
  22. #include "routing.h"
  23. #include "translation-table.h"
  24. #include "soft-interface.h"
  25. #include "hard-interface.h"
  26. #include "gateway_common.h"
  27. #include "gateway_client.h"
  28. #include "originator.h"
  29. #include "network-coding.h"
  30. #include "fragmentation.h"
  31. static void batadv_send_outstanding_bcast_packet(struct work_struct *work);
  32. /* send out an already prepared packet to the given address via the
  33. * specified batman interface
  34. */
  35. int batadv_send_skb_packet(struct sk_buff *skb,
  36. struct batadv_hard_iface *hard_iface,
  37. const uint8_t *dst_addr)
  38. {
  39. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  40. struct ethhdr *ethhdr;
  41. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  42. goto send_skb_err;
  43. if (unlikely(!hard_iface->net_dev))
  44. goto send_skb_err;
  45. if (!(hard_iface->net_dev->flags & IFF_UP)) {
  46. pr_warn("Interface %s is not up - can't send packet via that interface!\n",
  47. hard_iface->net_dev->name);
  48. goto send_skb_err;
  49. }
  50. /* push to the ethernet header. */
  51. if (batadv_skb_head_push(skb, ETH_HLEN) < 0)
  52. goto send_skb_err;
  53. skb_reset_mac_header(skb);
  54. ethhdr = eth_hdr(skb);
  55. memcpy(ethhdr->h_source, hard_iface->net_dev->dev_addr, ETH_ALEN);
  56. memcpy(ethhdr->h_dest, dst_addr, ETH_ALEN);
  57. ethhdr->h_proto = htons(ETH_P_BATMAN);
  58. skb_set_network_header(skb, ETH_HLEN);
  59. skb->protocol = htons(ETH_P_BATMAN);
  60. skb->dev = hard_iface->net_dev;
  61. /* Save a clone of the skb to use when decoding coded packets */
  62. batadv_nc_skb_store_for_decoding(bat_priv, skb);
  63. /* dev_queue_xmit() returns a negative result on error. However on
  64. * congestion and traffic shaping, it drops and returns NET_XMIT_DROP
  65. * (which is > 0). This will not be treated as an error.
  66. */
  67. return dev_queue_xmit(skb);
  68. send_skb_err:
  69. kfree_skb(skb);
  70. return NET_XMIT_DROP;
  71. }
  72. /**
  73. * batadv_send_skb_to_orig - Lookup next-hop and transmit skb.
  74. * @skb: Packet to be transmitted.
  75. * @orig_node: Final destination of the packet.
  76. * @recv_if: Interface used when receiving the packet (can be NULL).
  77. *
  78. * Looks up the best next-hop towards the passed originator and passes the
  79. * skb on for preparation of MAC header. If the packet originated from this
  80. * host, NULL can be passed as recv_if and no interface alternating is
  81. * attempted.
  82. *
  83. * Returns NET_XMIT_SUCCESS on success, NET_XMIT_DROP on failure, or
  84. * NET_XMIT_POLICED if the skb is buffered for later transmit.
  85. */
  86. int batadv_send_skb_to_orig(struct sk_buff *skb,
  87. struct batadv_orig_node *orig_node,
  88. struct batadv_hard_iface *recv_if)
  89. {
  90. struct batadv_priv *bat_priv = orig_node->bat_priv;
  91. struct batadv_neigh_node *neigh_node;
  92. int ret = NET_XMIT_DROP;
  93. /* batadv_find_router() increases neigh_nodes refcount if found. */
  94. neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
  95. if (!neigh_node)
  96. goto out;
  97. /* Check if the skb is too large to send in one piece and fragment
  98. * it if needed.
  99. */
  100. if (atomic_read(&bat_priv->fragmentation) &&
  101. skb->len > neigh_node->if_incoming->net_dev->mtu) {
  102. /* Fragment and send packet. */
  103. if (batadv_frag_send_packet(skb, orig_node, neigh_node))
  104. ret = NET_XMIT_SUCCESS;
  105. goto out;
  106. }
  107. /* try to network code the packet, if it is received on an interface
  108. * (i.e. being forwarded). If the packet originates from this node or if
  109. * network coding fails, then send the packet as usual.
  110. */
  111. if (recv_if && batadv_nc_skb_forward(skb, neigh_node)) {
  112. ret = NET_XMIT_POLICED;
  113. } else {
  114. batadv_send_skb_packet(skb, neigh_node->if_incoming,
  115. neigh_node->addr);
  116. ret = NET_XMIT_SUCCESS;
  117. }
  118. out:
  119. if (neigh_node)
  120. batadv_neigh_node_free_ref(neigh_node);
  121. return ret;
  122. }
  123. /**
  124. * batadv_send_skb_push_fill_unicast - extend the buffer and initialize the
  125. * common fields for unicast packets
  126. * @skb: the skb carrying the unicast header to initialize
  127. * @hdr_size: amount of bytes to push at the beginning of the skb
  128. * @orig_node: the destination node
  129. *
  130. * Returns false if the buffer extension was not possible or true otherwise.
  131. */
  132. static bool
  133. batadv_send_skb_push_fill_unicast(struct sk_buff *skb, int hdr_size,
  134. struct batadv_orig_node *orig_node)
  135. {
  136. struct batadv_unicast_packet *unicast_packet;
  137. uint8_t ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  138. if (batadv_skb_head_push(skb, hdr_size) < 0)
  139. return false;
  140. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  141. unicast_packet->header.version = BATADV_COMPAT_VERSION;
  142. /* batman packet type: unicast */
  143. unicast_packet->header.packet_type = BATADV_UNICAST;
  144. /* set unicast ttl */
  145. unicast_packet->header.ttl = BATADV_TTL;
  146. /* copy the destination for faster routing */
  147. memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);
  148. /* set the destination tt version number */
  149. unicast_packet->ttvn = ttvn;
  150. return true;
  151. }
  152. /**
  153. * batadv_send_skb_prepare_unicast - encapsulate an skb with a unicast header
  154. * @skb: the skb containing the payload to encapsulate
  155. * @orig_node: the destination node
  156. *
  157. * Returns false if the payload could not be encapsulated or true otherwise.
  158. */
  159. static bool batadv_send_skb_prepare_unicast(struct sk_buff *skb,
  160. struct batadv_orig_node *orig_node)
  161. {
  162. size_t uni_size = sizeof(struct batadv_unicast_packet);
  163. return batadv_send_skb_push_fill_unicast(skb, uni_size, orig_node);
  164. }
  165. /**
  166. * batadv_send_skb_prepare_unicast_4addr - encapsulate an skb with a
  167. * unicast 4addr header
  168. * @bat_priv: the bat priv with all the soft interface information
  169. * @skb: the skb containing the payload to encapsulate
  170. * @orig_node: the destination node
  171. * @packet_subtype: the unicast 4addr packet subtype to use
  172. *
  173. * Returns false if the payload could not be encapsulated or true otherwise.
  174. */
  175. bool batadv_send_skb_prepare_unicast_4addr(struct batadv_priv *bat_priv,
  176. struct sk_buff *skb,
  177. struct batadv_orig_node *orig,
  178. int packet_subtype)
  179. {
  180. struct batadv_hard_iface *primary_if;
  181. struct batadv_unicast_4addr_packet *uc_4addr_packet;
  182. bool ret = false;
  183. primary_if = batadv_primary_if_get_selected(bat_priv);
  184. if (!primary_if)
  185. goto out;
  186. /* Pull the header space and fill the unicast_packet substructure.
  187. * We can do that because the first member of the uc_4addr_packet
  188. * is of type struct unicast_packet
  189. */
  190. if (!batadv_send_skb_push_fill_unicast(skb, sizeof(*uc_4addr_packet),
  191. orig))
  192. goto out;
  193. uc_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
  194. uc_4addr_packet->u.header.packet_type = BATADV_UNICAST_4ADDR;
  195. memcpy(uc_4addr_packet->src, primary_if->net_dev->dev_addr, ETH_ALEN);
  196. uc_4addr_packet->subtype = packet_subtype;
  197. uc_4addr_packet->reserved = 0;
  198. ret = true;
  199. out:
  200. if (primary_if)
  201. batadv_hardif_free_ref(primary_if);
  202. return ret;
  203. }
  204. /**
  205. * batadv_send_generic_unicast_skb - send an skb as unicast
  206. * @bat_priv: the bat priv with all the soft interface information
  207. * @skb: payload to send
  208. * @packet_type: the batman unicast packet type to use
  209. * @packet_subtype: the unicast 4addr packet subtype (only relevant for unicast
  210. * 4addr packets)
  211. *
  212. * Returns 1 in case of error or 0 otherwise.
  213. */
  214. int batadv_send_skb_generic_unicast(struct batadv_priv *bat_priv,
  215. struct sk_buff *skb, int packet_type,
  216. int packet_subtype)
  217. {
  218. struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
  219. struct batadv_unicast_packet *unicast_packet;
  220. struct batadv_orig_node *orig_node;
  221. int ret = NET_RX_DROP;
  222. /* get routing information */
  223. if (is_multicast_ether_addr(ethhdr->h_dest))
  224. orig_node = batadv_gw_get_selected_orig(bat_priv);
  225. else
  226. /* check for tt host - increases orig_node refcount.
  227. * returns NULL in case of AP isolation
  228. */
  229. orig_node = batadv_transtable_search(bat_priv, ethhdr->h_source,
  230. ethhdr->h_dest);
  231. if (!orig_node)
  232. goto out;
  233. switch (packet_type) {
  234. case BATADV_UNICAST:
  235. batadv_send_skb_prepare_unicast(skb, orig_node);
  236. break;
  237. case BATADV_UNICAST_4ADDR:
  238. batadv_send_skb_prepare_unicast_4addr(bat_priv, skb, orig_node,
  239. packet_subtype);
  240. break;
  241. default:
  242. /* this function supports UNICAST and UNICAST_4ADDR only. It
  243. * should never be invoked with any other packet type
  244. */
  245. goto out;
  246. }
  247. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  248. /* inform the destination node that we are still missing a correct route
  249. * for this client. The destination will receive this packet and will
  250. * try to reroute it because the ttvn contained in the header is less
  251. * than the current one
  252. */
  253. if (batadv_tt_global_client_is_roaming(bat_priv, ethhdr->h_dest))
  254. unicast_packet->ttvn = unicast_packet->ttvn - 1;
  255. if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
  256. ret = 0;
  257. out:
  258. if (orig_node)
  259. batadv_orig_node_free_ref(orig_node);
  260. if (ret == NET_RX_DROP)
  261. kfree_skb(skb);
  262. return ret;
  263. }
  264. void batadv_schedule_bat_ogm(struct batadv_hard_iface *hard_iface)
  265. {
  266. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  267. if ((hard_iface->if_status == BATADV_IF_NOT_IN_USE) ||
  268. (hard_iface->if_status == BATADV_IF_TO_BE_REMOVED))
  269. return;
  270. /* the interface gets activated here to avoid race conditions between
  271. * the moment of activating the interface in
  272. * hardif_activate_interface() where the originator mac is set and
  273. * outdated packets (especially uninitialized mac addresses) in the
  274. * packet queue
  275. */
  276. if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
  277. hard_iface->if_status = BATADV_IF_ACTIVE;
  278. bat_priv->bat_algo_ops->bat_ogm_schedule(hard_iface);
  279. }
  280. static void batadv_forw_packet_free(struct batadv_forw_packet *forw_packet)
  281. {
  282. if (forw_packet->skb)
  283. kfree_skb(forw_packet->skb);
  284. if (forw_packet->if_incoming)
  285. batadv_hardif_free_ref(forw_packet->if_incoming);
  286. kfree(forw_packet);
  287. }
  288. static void
  289. _batadv_add_bcast_packet_to_list(struct batadv_priv *bat_priv,
  290. struct batadv_forw_packet *forw_packet,
  291. unsigned long send_time)
  292. {
  293. /* add new packet to packet list */
  294. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  295. hlist_add_head(&forw_packet->list, &bat_priv->forw_bcast_list);
  296. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  297. /* start timer for this packet */
  298. queue_delayed_work(batadv_event_workqueue, &forw_packet->delayed_work,
  299. send_time);
  300. }
  301. /* add a broadcast packet to the queue and setup timers. broadcast packets
  302. * are sent multiple times to increase probability for being received.
  303. *
  304. * This function returns NETDEV_TX_OK on success and NETDEV_TX_BUSY on
  305. * errors.
  306. *
  307. * The skb is not consumed, so the caller should make sure that the
  308. * skb is freed.
  309. */
  310. int batadv_add_bcast_packet_to_list(struct batadv_priv *bat_priv,
  311. const struct sk_buff *skb,
  312. unsigned long delay)
  313. {
  314. struct batadv_hard_iface *primary_if = NULL;
  315. struct batadv_forw_packet *forw_packet;
  316. struct batadv_bcast_packet *bcast_packet;
  317. struct sk_buff *newskb;
  318. if (!batadv_atomic_dec_not_zero(&bat_priv->bcast_queue_left)) {
  319. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  320. "bcast packet queue full\n");
  321. goto out;
  322. }
  323. primary_if = batadv_primary_if_get_selected(bat_priv);
  324. if (!primary_if)
  325. goto out_and_inc;
  326. forw_packet = kmalloc(sizeof(*forw_packet), GFP_ATOMIC);
  327. if (!forw_packet)
  328. goto out_and_inc;
  329. newskb = skb_copy(skb, GFP_ATOMIC);
  330. if (!newskb)
  331. goto packet_free;
  332. /* as we have a copy now, it is safe to decrease the TTL */
  333. bcast_packet = (struct batadv_bcast_packet *)newskb->data;
  334. bcast_packet->header.ttl--;
  335. skb_reset_mac_header(newskb);
  336. forw_packet->skb = newskb;
  337. forw_packet->if_incoming = primary_if;
  338. /* how often did we send the bcast packet ? */
  339. forw_packet->num_packets = 0;
  340. INIT_DELAYED_WORK(&forw_packet->delayed_work,
  341. batadv_send_outstanding_bcast_packet);
  342. _batadv_add_bcast_packet_to_list(bat_priv, forw_packet, delay);
  343. return NETDEV_TX_OK;
  344. packet_free:
  345. kfree(forw_packet);
  346. out_and_inc:
  347. atomic_inc(&bat_priv->bcast_queue_left);
  348. out:
  349. if (primary_if)
  350. batadv_hardif_free_ref(primary_if);
  351. return NETDEV_TX_BUSY;
  352. }
  353. static void batadv_send_outstanding_bcast_packet(struct work_struct *work)
  354. {
  355. struct batadv_hard_iface *hard_iface;
  356. struct delayed_work *delayed_work;
  357. struct batadv_forw_packet *forw_packet;
  358. struct sk_buff *skb1;
  359. struct net_device *soft_iface;
  360. struct batadv_priv *bat_priv;
  361. delayed_work = container_of(work, struct delayed_work, work);
  362. forw_packet = container_of(delayed_work, struct batadv_forw_packet,
  363. delayed_work);
  364. soft_iface = forw_packet->if_incoming->soft_iface;
  365. bat_priv = netdev_priv(soft_iface);
  366. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  367. hlist_del(&forw_packet->list);
  368. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  369. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
  370. goto out;
  371. if (batadv_dat_drop_broadcast_packet(bat_priv, forw_packet))
  372. goto out;
  373. /* rebroadcast packet */
  374. rcu_read_lock();
  375. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  376. if (hard_iface->soft_iface != soft_iface)
  377. continue;
  378. if (forw_packet->num_packets >= hard_iface->num_bcasts)
  379. continue;
  380. /* send a copy of the saved skb */
  381. skb1 = skb_clone(forw_packet->skb, GFP_ATOMIC);
  382. if (skb1)
  383. batadv_send_skb_packet(skb1, hard_iface,
  384. batadv_broadcast_addr);
  385. }
  386. rcu_read_unlock();
  387. forw_packet->num_packets++;
  388. /* if we still have some more bcasts to send */
  389. if (forw_packet->num_packets < BATADV_NUM_BCASTS_MAX) {
  390. _batadv_add_bcast_packet_to_list(bat_priv, forw_packet,
  391. msecs_to_jiffies(5));
  392. return;
  393. }
  394. out:
  395. batadv_forw_packet_free(forw_packet);
  396. atomic_inc(&bat_priv->bcast_queue_left);
  397. }
  398. void batadv_send_outstanding_bat_ogm_packet(struct work_struct *work)
  399. {
  400. struct delayed_work *delayed_work;
  401. struct batadv_forw_packet *forw_packet;
  402. struct batadv_priv *bat_priv;
  403. delayed_work = container_of(work, struct delayed_work, work);
  404. forw_packet = container_of(delayed_work, struct batadv_forw_packet,
  405. delayed_work);
  406. bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
  407. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  408. hlist_del(&forw_packet->list);
  409. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  410. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
  411. goto out;
  412. bat_priv->bat_algo_ops->bat_ogm_emit(forw_packet);
  413. /* we have to have at least one packet in the queue
  414. * to determine the queues wake up time unless we are
  415. * shutting down
  416. */
  417. if (forw_packet->own)
  418. batadv_schedule_bat_ogm(forw_packet->if_incoming);
  419. out:
  420. /* don't count own packet */
  421. if (!forw_packet->own)
  422. atomic_inc(&bat_priv->batman_queue_left);
  423. batadv_forw_packet_free(forw_packet);
  424. }
  425. void
  426. batadv_purge_outstanding_packets(struct batadv_priv *bat_priv,
  427. const struct batadv_hard_iface *hard_iface)
  428. {
  429. struct batadv_forw_packet *forw_packet;
  430. struct hlist_node *safe_tmp_node;
  431. bool pending;
  432. if (hard_iface)
  433. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  434. "purge_outstanding_packets(): %s\n",
  435. hard_iface->net_dev->name);
  436. else
  437. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  438. "purge_outstanding_packets()\n");
  439. /* free bcast list */
  440. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  441. hlist_for_each_entry_safe(forw_packet, safe_tmp_node,
  442. &bat_priv->forw_bcast_list, list) {
  443. /* if purge_outstanding_packets() was called with an argument
  444. * we delete only packets belonging to the given interface
  445. */
  446. if ((hard_iface) &&
  447. (forw_packet->if_incoming != hard_iface))
  448. continue;
  449. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  450. /* batadv_send_outstanding_bcast_packet() will lock the list to
  451. * delete the item from the list
  452. */
  453. pending = cancel_delayed_work_sync(&forw_packet->delayed_work);
  454. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  455. if (pending) {
  456. hlist_del(&forw_packet->list);
  457. batadv_forw_packet_free(forw_packet);
  458. }
  459. }
  460. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  461. /* free batman packet list */
  462. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  463. hlist_for_each_entry_safe(forw_packet, safe_tmp_node,
  464. &bat_priv->forw_bat_list, list) {
  465. /* if purge_outstanding_packets() was called with an argument
  466. * we delete only packets belonging to the given interface
  467. */
  468. if ((hard_iface) &&
  469. (forw_packet->if_incoming != hard_iface))
  470. continue;
  471. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  472. /* send_outstanding_bat_packet() will lock the list to
  473. * delete the item from the list
  474. */
  475. pending = cancel_delayed_work_sync(&forw_packet->delayed_work);
  476. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  477. if (pending) {
  478. hlist_del(&forw_packet->list);
  479. batadv_forw_packet_free(forw_packet);
  480. }
  481. }
  482. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  483. }