send.c 11 KB

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