send.c 12 KB

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