send.c 9.8 KB

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