send.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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 = (struct ethhdr *)skb_mac_header(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. INIT_HLIST_NODE(&forw_packet->list);
  131. /* add new packet to packet list */
  132. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  133. hlist_add_head(&forw_packet->list, &bat_priv->forw_bcast_list);
  134. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  135. /* start timer for this packet */
  136. queue_delayed_work(batadv_event_workqueue, &forw_packet->delayed_work,
  137. send_time);
  138. }
  139. /* add a broadcast packet to the queue and setup timers. broadcast packets
  140. * are sent multiple times to increase probability for being received.
  141. *
  142. * This function returns NETDEV_TX_OK on success and NETDEV_TX_BUSY on
  143. * errors.
  144. *
  145. * The skb is not consumed, so the caller should make sure that the
  146. * skb is freed.
  147. */
  148. int batadv_add_bcast_packet_to_list(struct batadv_priv *bat_priv,
  149. const struct sk_buff *skb,
  150. unsigned long delay)
  151. {
  152. struct batadv_hard_iface *primary_if = NULL;
  153. struct batadv_forw_packet *forw_packet;
  154. struct batadv_bcast_packet *bcast_packet;
  155. struct sk_buff *newskb;
  156. if (!batadv_atomic_dec_not_zero(&bat_priv->bcast_queue_left)) {
  157. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  158. "bcast packet queue full\n");
  159. goto out;
  160. }
  161. primary_if = batadv_primary_if_get_selected(bat_priv);
  162. if (!primary_if)
  163. goto out_and_inc;
  164. forw_packet = kmalloc(sizeof(*forw_packet), GFP_ATOMIC);
  165. if (!forw_packet)
  166. goto out_and_inc;
  167. newskb = skb_copy(skb, GFP_ATOMIC);
  168. if (!newskb)
  169. goto packet_free;
  170. /* as we have a copy now, it is safe to decrease the TTL */
  171. bcast_packet = (struct batadv_bcast_packet *)newskb->data;
  172. bcast_packet->header.ttl--;
  173. skb_reset_mac_header(newskb);
  174. forw_packet->skb = newskb;
  175. forw_packet->if_incoming = primary_if;
  176. /* how often did we send the bcast packet ? */
  177. forw_packet->num_packets = 0;
  178. INIT_DELAYED_WORK(&forw_packet->delayed_work,
  179. batadv_send_outstanding_bcast_packet);
  180. _batadv_add_bcast_packet_to_list(bat_priv, forw_packet, delay);
  181. return NETDEV_TX_OK;
  182. packet_free:
  183. kfree(forw_packet);
  184. out_and_inc:
  185. atomic_inc(&bat_priv->bcast_queue_left);
  186. out:
  187. if (primary_if)
  188. batadv_hardif_free_ref(primary_if);
  189. return NETDEV_TX_BUSY;
  190. }
  191. static void batadv_send_outstanding_bcast_packet(struct work_struct *work)
  192. {
  193. struct batadv_hard_iface *hard_iface;
  194. struct delayed_work *delayed_work;
  195. struct batadv_forw_packet *forw_packet;
  196. struct sk_buff *skb1;
  197. struct net_device *soft_iface;
  198. struct batadv_priv *bat_priv;
  199. delayed_work = container_of(work, struct delayed_work, work);
  200. forw_packet = container_of(delayed_work, struct batadv_forw_packet,
  201. delayed_work);
  202. soft_iface = forw_packet->if_incoming->soft_iface;
  203. bat_priv = netdev_priv(soft_iface);
  204. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  205. hlist_del(&forw_packet->list);
  206. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  207. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
  208. goto out;
  209. if (batadv_dat_drop_broadcast_packet(bat_priv, forw_packet))
  210. goto out;
  211. /* rebroadcast packet */
  212. rcu_read_lock();
  213. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  214. if (hard_iface->soft_iface != soft_iface)
  215. continue;
  216. /* send a copy of the saved skb */
  217. skb1 = skb_clone(forw_packet->skb, GFP_ATOMIC);
  218. if (skb1)
  219. batadv_send_skb_packet(skb1, hard_iface,
  220. batadv_broadcast_addr);
  221. }
  222. rcu_read_unlock();
  223. forw_packet->num_packets++;
  224. /* if we still have some more bcasts to send */
  225. if (forw_packet->num_packets < 3) {
  226. _batadv_add_bcast_packet_to_list(bat_priv, forw_packet,
  227. msecs_to_jiffies(5));
  228. return;
  229. }
  230. out:
  231. batadv_forw_packet_free(forw_packet);
  232. atomic_inc(&bat_priv->bcast_queue_left);
  233. }
  234. void batadv_send_outstanding_bat_ogm_packet(struct work_struct *work)
  235. {
  236. struct delayed_work *delayed_work;
  237. struct batadv_forw_packet *forw_packet;
  238. struct batadv_priv *bat_priv;
  239. delayed_work = container_of(work, struct delayed_work, work);
  240. forw_packet = container_of(delayed_work, struct batadv_forw_packet,
  241. delayed_work);
  242. bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
  243. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  244. hlist_del(&forw_packet->list);
  245. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  246. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
  247. goto out;
  248. bat_priv->bat_algo_ops->bat_ogm_emit(forw_packet);
  249. /* we have to have at least one packet in the queue
  250. * to determine the queues wake up time unless we are
  251. * shutting down
  252. */
  253. if (forw_packet->own)
  254. batadv_schedule_bat_ogm(forw_packet->if_incoming);
  255. out:
  256. /* don't count own packet */
  257. if (!forw_packet->own)
  258. atomic_inc(&bat_priv->batman_queue_left);
  259. batadv_forw_packet_free(forw_packet);
  260. }
  261. void
  262. batadv_purge_outstanding_packets(struct batadv_priv *bat_priv,
  263. const struct batadv_hard_iface *hard_iface)
  264. {
  265. struct batadv_forw_packet *forw_packet;
  266. struct hlist_node *safe_tmp_node;
  267. bool pending;
  268. if (hard_iface)
  269. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  270. "purge_outstanding_packets(): %s\n",
  271. hard_iface->net_dev->name);
  272. else
  273. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  274. "purge_outstanding_packets()\n");
  275. /* free bcast list */
  276. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  277. hlist_for_each_entry_safe(forw_packet, safe_tmp_node,
  278. &bat_priv->forw_bcast_list, list) {
  279. /* if purge_outstanding_packets() was called with an argument
  280. * we delete only packets belonging to the given interface
  281. */
  282. if ((hard_iface) &&
  283. (forw_packet->if_incoming != hard_iface))
  284. continue;
  285. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  286. /* batadv_send_outstanding_bcast_packet() will lock the list to
  287. * delete the item from the list
  288. */
  289. pending = cancel_delayed_work_sync(&forw_packet->delayed_work);
  290. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  291. if (pending) {
  292. hlist_del(&forw_packet->list);
  293. batadv_forw_packet_free(forw_packet);
  294. }
  295. }
  296. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  297. /* free batman packet list */
  298. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  299. hlist_for_each_entry_safe(forw_packet, safe_tmp_node,
  300. &bat_priv->forw_bat_list, list) {
  301. /* if purge_outstanding_packets() was called with an argument
  302. * we delete only packets belonging to the given interface
  303. */
  304. if ((hard_iface) &&
  305. (forw_packet->if_incoming != hard_iface))
  306. continue;
  307. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  308. /* send_outstanding_bat_packet() will lock the list to
  309. * delete the item from the list
  310. */
  311. pending = cancel_delayed_work_sync(&forw_packet->delayed_work);
  312. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  313. if (pending) {
  314. hlist_del(&forw_packet->list);
  315. batadv_forw_packet_free(forw_packet);
  316. }
  317. }
  318. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  319. }