send.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /* Copyright (C) 2007-2012 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. static void batadv_send_outstanding_bcast_packet(struct work_struct *work);
  30. /* send out an already prepared packet to the given address via the
  31. * specified batman interface
  32. */
  33. int batadv_send_skb_packet(struct sk_buff *skb,
  34. struct batadv_hard_iface *hard_iface,
  35. const uint8_t *dst_addr)
  36. {
  37. struct ethhdr *ethhdr;
  38. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  39. goto send_skb_err;
  40. if (unlikely(!hard_iface->net_dev))
  41. goto send_skb_err;
  42. if (!(hard_iface->net_dev->flags & IFF_UP)) {
  43. pr_warn("Interface %s is not up - can't send packet via that interface!\n",
  44. hard_iface->net_dev->name);
  45. goto send_skb_err;
  46. }
  47. /* push to the ethernet header. */
  48. if (batadv_skb_head_push(skb, ETH_HLEN) < 0)
  49. goto send_skb_err;
  50. skb_reset_mac_header(skb);
  51. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  52. memcpy(ethhdr->h_source, hard_iface->net_dev->dev_addr, ETH_ALEN);
  53. memcpy(ethhdr->h_dest, dst_addr, ETH_ALEN);
  54. ethhdr->h_proto = __constant_htons(BATADV_ETH_P_BATMAN);
  55. skb_set_network_header(skb, ETH_HLEN);
  56. skb->priority = TC_PRIO_CONTROL;
  57. skb->protocol = __constant_htons(BATADV_ETH_P_BATMAN);
  58. skb->dev = hard_iface->net_dev;
  59. /* dev_queue_xmit() returns a negative result on error. However on
  60. * congestion and traffic shaping, it drops and returns NET_XMIT_DROP
  61. * (which is > 0). This will not be treated as an error.
  62. */
  63. return dev_queue_xmit(skb);
  64. send_skb_err:
  65. kfree_skb(skb);
  66. return NET_XMIT_DROP;
  67. }
  68. void batadv_schedule_bat_ogm(struct batadv_hard_iface *hard_iface)
  69. {
  70. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  71. if ((hard_iface->if_status == BATADV_IF_NOT_IN_USE) ||
  72. (hard_iface->if_status == BATADV_IF_TO_BE_REMOVED))
  73. return;
  74. /* the interface gets activated here to avoid race conditions between
  75. * the moment of activating the interface in
  76. * hardif_activate_interface() where the originator mac is set and
  77. * outdated packets (especially uninitialized mac addresses) in the
  78. * packet queue
  79. */
  80. if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
  81. hard_iface->if_status = BATADV_IF_ACTIVE;
  82. bat_priv->bat_algo_ops->bat_ogm_schedule(hard_iface);
  83. }
  84. static void batadv_forw_packet_free(struct batadv_forw_packet *forw_packet)
  85. {
  86. if (forw_packet->skb)
  87. kfree_skb(forw_packet->skb);
  88. if (forw_packet->if_incoming)
  89. batadv_hardif_free_ref(forw_packet->if_incoming);
  90. kfree(forw_packet);
  91. }
  92. static void
  93. _batadv_add_bcast_packet_to_list(struct batadv_priv *bat_priv,
  94. struct batadv_forw_packet *forw_packet,
  95. unsigned long send_time)
  96. {
  97. INIT_HLIST_NODE(&forw_packet->list);
  98. /* add new packet to packet list */
  99. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  100. hlist_add_head(&forw_packet->list, &bat_priv->forw_bcast_list);
  101. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  102. /* start timer for this packet */
  103. INIT_DELAYED_WORK(&forw_packet->delayed_work,
  104. batadv_send_outstanding_bcast_packet);
  105. queue_delayed_work(batadv_event_workqueue, &forw_packet->delayed_work,
  106. send_time);
  107. }
  108. /* add a broadcast packet to the queue and setup timers. broadcast packets
  109. * are sent multiple times to increase probability for being received.
  110. *
  111. * This function returns NETDEV_TX_OK on success and NETDEV_TX_BUSY on
  112. * errors.
  113. *
  114. * The skb is not consumed, so the caller should make sure that the
  115. * skb is freed.
  116. */
  117. int batadv_add_bcast_packet_to_list(struct batadv_priv *bat_priv,
  118. const struct sk_buff *skb,
  119. unsigned long delay)
  120. {
  121. struct batadv_hard_iface *primary_if = NULL;
  122. struct batadv_forw_packet *forw_packet;
  123. struct batadv_bcast_packet *bcast_packet;
  124. struct sk_buff *newskb;
  125. if (!batadv_atomic_dec_not_zero(&bat_priv->bcast_queue_left)) {
  126. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  127. "bcast packet queue full\n");
  128. goto out;
  129. }
  130. primary_if = batadv_primary_if_get_selected(bat_priv);
  131. if (!primary_if)
  132. goto out_and_inc;
  133. forw_packet = kmalloc(sizeof(*forw_packet), GFP_ATOMIC);
  134. if (!forw_packet)
  135. goto out_and_inc;
  136. newskb = skb_copy(skb, GFP_ATOMIC);
  137. if (!newskb)
  138. goto packet_free;
  139. /* as we have a copy now, it is safe to decrease the TTL */
  140. bcast_packet = (struct batadv_bcast_packet *)newskb->data;
  141. bcast_packet->header.ttl--;
  142. skb_reset_mac_header(newskb);
  143. forw_packet->skb = newskb;
  144. forw_packet->if_incoming = primary_if;
  145. /* how often did we send the bcast packet ? */
  146. forw_packet->num_packets = 0;
  147. _batadv_add_bcast_packet_to_list(bat_priv, forw_packet, delay);
  148. return NETDEV_TX_OK;
  149. packet_free:
  150. kfree(forw_packet);
  151. out_and_inc:
  152. atomic_inc(&bat_priv->bcast_queue_left);
  153. out:
  154. if (primary_if)
  155. batadv_hardif_free_ref(primary_if);
  156. return NETDEV_TX_BUSY;
  157. }
  158. static void batadv_send_outstanding_bcast_packet(struct work_struct *work)
  159. {
  160. struct batadv_hard_iface *hard_iface;
  161. struct delayed_work *delayed_work;
  162. struct batadv_forw_packet *forw_packet;
  163. struct sk_buff *skb1;
  164. struct net_device *soft_iface;
  165. struct batadv_priv *bat_priv;
  166. delayed_work = container_of(work, struct delayed_work, work);
  167. forw_packet = container_of(delayed_work, struct batadv_forw_packet,
  168. delayed_work);
  169. soft_iface = forw_packet->if_incoming->soft_iface;
  170. bat_priv = netdev_priv(soft_iface);
  171. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  172. hlist_del(&forw_packet->list);
  173. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  174. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
  175. goto out;
  176. if (batadv_dat_drop_broadcast_packet(bat_priv, forw_packet))
  177. goto out;
  178. /* rebroadcast packet */
  179. rcu_read_lock();
  180. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  181. if (hard_iface->soft_iface != soft_iface)
  182. continue;
  183. /* send a copy of the saved skb */
  184. skb1 = skb_clone(forw_packet->skb, GFP_ATOMIC);
  185. if (skb1)
  186. batadv_send_skb_packet(skb1, hard_iface,
  187. batadv_broadcast_addr);
  188. }
  189. rcu_read_unlock();
  190. forw_packet->num_packets++;
  191. /* if we still have some more bcasts to send */
  192. if (forw_packet->num_packets < 3) {
  193. _batadv_add_bcast_packet_to_list(bat_priv, forw_packet,
  194. msecs_to_jiffies(5));
  195. return;
  196. }
  197. out:
  198. batadv_forw_packet_free(forw_packet);
  199. atomic_inc(&bat_priv->bcast_queue_left);
  200. }
  201. void batadv_send_outstanding_bat_ogm_packet(struct work_struct *work)
  202. {
  203. struct delayed_work *delayed_work;
  204. struct batadv_forw_packet *forw_packet;
  205. struct batadv_priv *bat_priv;
  206. delayed_work = container_of(work, struct delayed_work, work);
  207. forw_packet = container_of(delayed_work, struct batadv_forw_packet,
  208. delayed_work);
  209. bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
  210. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  211. hlist_del(&forw_packet->list);
  212. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  213. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
  214. goto out;
  215. bat_priv->bat_algo_ops->bat_ogm_emit(forw_packet);
  216. /* we have to have at least one packet in the queue
  217. * to determine the queues wake up time unless we are
  218. * shutting down
  219. */
  220. if (forw_packet->own)
  221. batadv_schedule_bat_ogm(forw_packet->if_incoming);
  222. out:
  223. /* don't count own packet */
  224. if (!forw_packet->own)
  225. atomic_inc(&bat_priv->batman_queue_left);
  226. batadv_forw_packet_free(forw_packet);
  227. }
  228. void
  229. batadv_purge_outstanding_packets(struct batadv_priv *bat_priv,
  230. const struct batadv_hard_iface *hard_iface)
  231. {
  232. struct batadv_forw_packet *forw_packet;
  233. struct hlist_node *tmp_node, *safe_tmp_node;
  234. bool pending;
  235. if (hard_iface)
  236. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  237. "purge_outstanding_packets(): %s\n",
  238. hard_iface->net_dev->name);
  239. else
  240. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  241. "purge_outstanding_packets()\n");
  242. /* free bcast list */
  243. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  244. hlist_for_each_entry_safe(forw_packet, tmp_node, safe_tmp_node,
  245. &bat_priv->forw_bcast_list, list) {
  246. /* if purge_outstanding_packets() was called with an argument
  247. * we delete only packets belonging to the given interface
  248. */
  249. if ((hard_iface) &&
  250. (forw_packet->if_incoming != hard_iface))
  251. continue;
  252. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  253. /* batadv_send_outstanding_bcast_packet() will lock the list to
  254. * delete the item from the list
  255. */
  256. pending = cancel_delayed_work_sync(&forw_packet->delayed_work);
  257. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  258. if (pending) {
  259. hlist_del(&forw_packet->list);
  260. batadv_forw_packet_free(forw_packet);
  261. }
  262. }
  263. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  264. /* free batman packet list */
  265. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  266. hlist_for_each_entry_safe(forw_packet, tmp_node, safe_tmp_node,
  267. &bat_priv->forw_bat_list, list) {
  268. /* if purge_outstanding_packets() was called with an argument
  269. * we delete only packets belonging to the given interface
  270. */
  271. if ((hard_iface) &&
  272. (forw_packet->if_incoming != hard_iface))
  273. continue;
  274. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  275. /* send_outstanding_bat_packet() will lock the list to
  276. * delete the item from the list
  277. */
  278. pending = cancel_delayed_work_sync(&forw_packet->delayed_work);
  279. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  280. if (pending) {
  281. hlist_del(&forw_packet->list);
  282. batadv_forw_packet_free(forw_packet);
  283. }
  284. }
  285. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  286. }