send.c 11 KB

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