send.c 9.6 KB

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