send.c 12 KB

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