send.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  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 "vis.h"
  27. #include "gateway_common.h"
  28. #include "originator.h"
  29. #include "network-coding.h"
  30. #include <linux/if_ether.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 = __constant_htons(ETH_P_BATMAN);
  58. skb_set_network_header(skb, ETH_HLEN);
  59. skb->priority = TC_PRIO_CONTROL;
  60. skb->protocol = __constant_htons(ETH_P_BATMAN);
  61. skb->dev = hard_iface->net_dev;
  62. /* Save a clone of the skb to use when decoding coded packets */
  63. batadv_nc_skb_store_for_decoding(bat_priv, skb);
  64. /* dev_queue_xmit() returns a negative result on error. However on
  65. * congestion and traffic shaping, it drops and returns NET_XMIT_DROP
  66. * (which is > 0). This will not be treated as an error.
  67. */
  68. return dev_queue_xmit(skb);
  69. send_skb_err:
  70. kfree_skb(skb);
  71. return NET_XMIT_DROP;
  72. }
  73. /**
  74. * batadv_send_skb_to_orig - Lookup next-hop and transmit skb.
  75. * @skb: Packet to be transmitted.
  76. * @orig_node: Final destination of the packet.
  77. * @recv_if: Interface used when receiving the packet (can be NULL).
  78. *
  79. * Looks up the best next-hop towards the passed originator and passes the
  80. * skb on for preparation of MAC header. If the packet originated from this
  81. * host, NULL can be passed as recv_if and no interface alternating is
  82. * attempted.
  83. *
  84. * Returns TRUE on success; FALSE otherwise.
  85. */
  86. bool 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. /* batadv_find_router() increases neigh_nodes refcount if found. */
  93. neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
  94. if (!neigh_node)
  95. return false;
  96. /* route it */
  97. batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  98. batadv_neigh_node_free_ref(neigh_node);
  99. return true;
  100. }
  101. void batadv_schedule_bat_ogm(struct batadv_hard_iface *hard_iface)
  102. {
  103. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  104. if ((hard_iface->if_status == BATADV_IF_NOT_IN_USE) ||
  105. (hard_iface->if_status == BATADV_IF_TO_BE_REMOVED))
  106. return;
  107. /* the interface gets activated here to avoid race conditions between
  108. * the moment of activating the interface in
  109. * hardif_activate_interface() where the originator mac is set and
  110. * outdated packets (especially uninitialized mac addresses) in the
  111. * packet queue
  112. */
  113. if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
  114. hard_iface->if_status = BATADV_IF_ACTIVE;
  115. bat_priv->bat_algo_ops->bat_ogm_schedule(hard_iface);
  116. }
  117. static void batadv_forw_packet_free(struct batadv_forw_packet *forw_packet)
  118. {
  119. if (forw_packet->skb)
  120. kfree_skb(forw_packet->skb);
  121. if (forw_packet->if_incoming)
  122. batadv_hardif_free_ref(forw_packet->if_incoming);
  123. kfree(forw_packet);
  124. }
  125. static void
  126. _batadv_add_bcast_packet_to_list(struct batadv_priv *bat_priv,
  127. struct batadv_forw_packet *forw_packet,
  128. unsigned long send_time)
  129. {
  130. /* add new packet to packet list */
  131. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  132. hlist_add_head(&forw_packet->list, &bat_priv->forw_bcast_list);
  133. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  134. /* start timer for this packet */
  135. queue_delayed_work(batadv_event_workqueue, &forw_packet->delayed_work,
  136. send_time);
  137. }
  138. /* add a broadcast packet to the queue and setup timers. broadcast packets
  139. * are sent multiple times to increase probability for being received.
  140. *
  141. * This function returns NETDEV_TX_OK on success and NETDEV_TX_BUSY on
  142. * errors.
  143. *
  144. * The skb is not consumed, so the caller should make sure that the
  145. * skb is freed.
  146. */
  147. int batadv_add_bcast_packet_to_list(struct batadv_priv *bat_priv,
  148. const struct sk_buff *skb,
  149. unsigned long delay)
  150. {
  151. struct batadv_hard_iface *primary_if = NULL;
  152. struct batadv_forw_packet *forw_packet;
  153. struct batadv_bcast_packet *bcast_packet;
  154. struct sk_buff *newskb;
  155. if (!batadv_atomic_dec_not_zero(&bat_priv->bcast_queue_left)) {
  156. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  157. "bcast packet queue full\n");
  158. goto out;
  159. }
  160. primary_if = batadv_primary_if_get_selected(bat_priv);
  161. if (!primary_if)
  162. goto out_and_inc;
  163. forw_packet = kmalloc(sizeof(*forw_packet), GFP_ATOMIC);
  164. if (!forw_packet)
  165. goto out_and_inc;
  166. newskb = skb_copy(skb, GFP_ATOMIC);
  167. if (!newskb)
  168. goto packet_free;
  169. /* as we have a copy now, it is safe to decrease the TTL */
  170. bcast_packet = (struct batadv_bcast_packet *)newskb->data;
  171. bcast_packet->header.ttl--;
  172. skb_reset_mac_header(newskb);
  173. forw_packet->skb = newskb;
  174. forw_packet->if_incoming = primary_if;
  175. /* how often did we send the bcast packet ? */
  176. forw_packet->num_packets = 0;
  177. INIT_DELAYED_WORK(&forw_packet->delayed_work,
  178. batadv_send_outstanding_bcast_packet);
  179. _batadv_add_bcast_packet_to_list(bat_priv, forw_packet, delay);
  180. return NETDEV_TX_OK;
  181. packet_free:
  182. kfree(forw_packet);
  183. out_and_inc:
  184. atomic_inc(&bat_priv->bcast_queue_left);
  185. out:
  186. if (primary_if)
  187. batadv_hardif_free_ref(primary_if);
  188. return NETDEV_TX_BUSY;
  189. }
  190. static void batadv_send_outstanding_bcast_packet(struct work_struct *work)
  191. {
  192. struct batadv_hard_iface *hard_iface;
  193. struct delayed_work *delayed_work;
  194. struct batadv_forw_packet *forw_packet;
  195. struct sk_buff *skb1;
  196. struct net_device *soft_iface;
  197. struct batadv_priv *bat_priv;
  198. delayed_work = container_of(work, struct delayed_work, work);
  199. forw_packet = container_of(delayed_work, struct batadv_forw_packet,
  200. delayed_work);
  201. soft_iface = forw_packet->if_incoming->soft_iface;
  202. bat_priv = netdev_priv(soft_iface);
  203. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  204. hlist_del(&forw_packet->list);
  205. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  206. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
  207. goto out;
  208. if (batadv_dat_drop_broadcast_packet(bat_priv, forw_packet))
  209. goto out;
  210. /* rebroadcast packet */
  211. rcu_read_lock();
  212. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  213. if (hard_iface->soft_iface != soft_iface)
  214. continue;
  215. if (forw_packet->num_packets >= hard_iface->num_bcasts)
  216. continue;
  217. /* send a copy of the saved skb */
  218. skb1 = skb_clone(forw_packet->skb, GFP_ATOMIC);
  219. if (skb1)
  220. batadv_send_skb_packet(skb1, hard_iface,
  221. batadv_broadcast_addr);
  222. }
  223. rcu_read_unlock();
  224. forw_packet->num_packets++;
  225. /* if we still have some more bcasts to send */
  226. if (forw_packet->num_packets < BATADV_NUM_BCASTS_MAX) {
  227. _batadv_add_bcast_packet_to_list(bat_priv, forw_packet,
  228. msecs_to_jiffies(5));
  229. return;
  230. }
  231. out:
  232. batadv_forw_packet_free(forw_packet);
  233. atomic_inc(&bat_priv->bcast_queue_left);
  234. }
  235. void batadv_send_outstanding_bat_ogm_packet(struct work_struct *work)
  236. {
  237. struct delayed_work *delayed_work;
  238. struct batadv_forw_packet *forw_packet;
  239. struct batadv_priv *bat_priv;
  240. delayed_work = container_of(work, struct delayed_work, work);
  241. forw_packet = container_of(delayed_work, struct batadv_forw_packet,
  242. delayed_work);
  243. bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
  244. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  245. hlist_del(&forw_packet->list);
  246. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  247. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
  248. goto out;
  249. bat_priv->bat_algo_ops->bat_ogm_emit(forw_packet);
  250. /* we have to have at least one packet in the queue
  251. * to determine the queues wake up time unless we are
  252. * shutting down
  253. */
  254. if (forw_packet->own)
  255. batadv_schedule_bat_ogm(forw_packet->if_incoming);
  256. out:
  257. /* don't count own packet */
  258. if (!forw_packet->own)
  259. atomic_inc(&bat_priv->batman_queue_left);
  260. batadv_forw_packet_free(forw_packet);
  261. }
  262. void
  263. batadv_purge_outstanding_packets(struct batadv_priv *bat_priv,
  264. const struct batadv_hard_iface *hard_iface)
  265. {
  266. struct batadv_forw_packet *forw_packet;
  267. struct hlist_node *safe_tmp_node;
  268. bool pending;
  269. if (hard_iface)
  270. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  271. "purge_outstanding_packets(): %s\n",
  272. hard_iface->net_dev->name);
  273. else
  274. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  275. "purge_outstanding_packets()\n");
  276. /* free bcast list */
  277. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  278. hlist_for_each_entry_safe(forw_packet, safe_tmp_node,
  279. &bat_priv->forw_bcast_list, list) {
  280. /* if purge_outstanding_packets() was called with an argument
  281. * we delete only packets belonging to the given interface
  282. */
  283. if ((hard_iface) &&
  284. (forw_packet->if_incoming != hard_iface))
  285. continue;
  286. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  287. /* batadv_send_outstanding_bcast_packet() will lock the list to
  288. * delete the item from the list
  289. */
  290. pending = cancel_delayed_work_sync(&forw_packet->delayed_work);
  291. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  292. if (pending) {
  293. hlist_del(&forw_packet->list);
  294. batadv_forw_packet_free(forw_packet);
  295. }
  296. }
  297. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  298. /* free batman packet list */
  299. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  300. hlist_for_each_entry_safe(forw_packet, safe_tmp_node,
  301. &bat_priv->forw_bat_list, list) {
  302. /* if purge_outstanding_packets() was called with an argument
  303. * we delete only packets belonging to the given interface
  304. */
  305. if ((hard_iface) &&
  306. (forw_packet->if_incoming != hard_iface))
  307. continue;
  308. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  309. /* send_outstanding_bat_packet() will lock the list to
  310. * delete the item from the list
  311. */
  312. pending = cancel_delayed_work_sync(&forw_packet->delayed_work);
  313. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  314. if (pending) {
  315. hlist_del(&forw_packet->list);
  316. batadv_forw_packet_free(forw_packet);
  317. }
  318. }
  319. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  320. }