send.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner, Simon Wunderlich
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA
  19. *
  20. */
  21. #include "main.h"
  22. #include "send.h"
  23. #include "routing.h"
  24. #include "translation-table.h"
  25. #include "soft-interface.h"
  26. #include "hard-interface.h"
  27. #include "vis.h"
  28. #include "gateway_common.h"
  29. #include "originator.h"
  30. static void 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. int send_skb_packet(struct sk_buff *skb, struct hard_iface *hard_iface,
  34. const uint8_t *dst_addr)
  35. {
  36. struct ethhdr *ethhdr;
  37. if (hard_iface->if_status != IF_ACTIVE)
  38. goto send_skb_err;
  39. if (unlikely(!hard_iface->net_dev))
  40. goto send_skb_err;
  41. if (!(hard_iface->net_dev->flags & IFF_UP)) {
  42. pr_warning("Interface %s is not up - can't send packet via that interface!\n",
  43. hard_iface->net_dev->name);
  44. goto send_skb_err;
  45. }
  46. /* push to the ethernet header. */
  47. if (my_skb_head_push(skb, sizeof(*ethhdr)) < 0)
  48. goto send_skb_err;
  49. skb_reset_mac_header(skb);
  50. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  51. memcpy(ethhdr->h_source, hard_iface->net_dev->dev_addr, ETH_ALEN);
  52. memcpy(ethhdr->h_dest, dst_addr, ETH_ALEN);
  53. ethhdr->h_proto = __constant_htons(ETH_P_BATMAN);
  54. skb_set_network_header(skb, ETH_HLEN);
  55. skb->priority = TC_PRIO_CONTROL;
  56. skb->protocol = __constant_htons(ETH_P_BATMAN);
  57. skb->dev = hard_iface->net_dev;
  58. /* dev_queue_xmit() returns a negative result on error. However on
  59. * congestion and traffic shaping, it drops and returns NET_XMIT_DROP
  60. * (which is > 0). This will not be treated as an error. */
  61. return dev_queue_xmit(skb);
  62. send_skb_err:
  63. kfree_skb(skb);
  64. return NET_XMIT_DROP;
  65. }
  66. static void realloc_packet_buffer(struct hard_iface *hard_iface,
  67. int new_len)
  68. {
  69. unsigned char *new_buff;
  70. new_buff = kmalloc(new_len, GFP_ATOMIC);
  71. /* keep old buffer if kmalloc should fail */
  72. if (new_buff) {
  73. memcpy(new_buff, hard_iface->packet_buff,
  74. BATMAN_OGM_LEN);
  75. kfree(hard_iface->packet_buff);
  76. hard_iface->packet_buff = new_buff;
  77. hard_iface->packet_len = new_len;
  78. }
  79. }
  80. /* when calling this function (hard_iface == primary_if) has to be true */
  81. static int prepare_packet_buffer(struct bat_priv *bat_priv,
  82. struct hard_iface *hard_iface)
  83. {
  84. int new_len;
  85. new_len = BATMAN_OGM_LEN +
  86. tt_len((uint8_t)atomic_read(&bat_priv->tt_local_changes));
  87. /* if we have too many changes for one packet don't send any
  88. * and wait for the tt table request which will be fragmented */
  89. if (new_len > hard_iface->soft_iface->mtu)
  90. new_len = BATMAN_OGM_LEN;
  91. realloc_packet_buffer(hard_iface, new_len);
  92. atomic_set(&bat_priv->tt_crc, tt_local_crc(bat_priv));
  93. /* reset the sending counter */
  94. atomic_set(&bat_priv->tt_ogm_append_cnt, TT_OGM_APPEND_MAX);
  95. return tt_changes_fill_buffer(bat_priv,
  96. hard_iface->packet_buff + BATMAN_OGM_LEN,
  97. hard_iface->packet_len - BATMAN_OGM_LEN);
  98. }
  99. static int reset_packet_buffer(struct bat_priv *bat_priv,
  100. struct hard_iface *hard_iface)
  101. {
  102. realloc_packet_buffer(hard_iface, BATMAN_OGM_LEN);
  103. return 0;
  104. }
  105. void schedule_bat_ogm(struct hard_iface *hard_iface)
  106. {
  107. struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  108. struct hard_iface *primary_if;
  109. int tt_num_changes = -1;
  110. if ((hard_iface->if_status == IF_NOT_IN_USE) ||
  111. (hard_iface->if_status == IF_TO_BE_REMOVED))
  112. return;
  113. /**
  114. * the interface gets activated here to avoid race conditions between
  115. * the moment of activating the interface in
  116. * hardif_activate_interface() where the originator mac is set and
  117. * outdated packets (especially uninitialized mac addresses) in the
  118. * packet queue
  119. */
  120. if (hard_iface->if_status == IF_TO_BE_ACTIVATED)
  121. hard_iface->if_status = IF_ACTIVE;
  122. primary_if = primary_if_get_selected(bat_priv);
  123. if (hard_iface == primary_if) {
  124. /* if at least one change happened */
  125. if (atomic_read(&bat_priv->tt_local_changes) > 0) {
  126. tt_commit_changes(bat_priv);
  127. tt_num_changes = prepare_packet_buffer(bat_priv,
  128. hard_iface);
  129. }
  130. /* if the changes have been sent often enough */
  131. if (!atomic_dec_not_zero(&bat_priv->tt_ogm_append_cnt))
  132. tt_num_changes = reset_packet_buffer(bat_priv,
  133. hard_iface);
  134. }
  135. if (primary_if)
  136. hardif_free_ref(primary_if);
  137. bat_priv->bat_algo_ops->bat_ogm_schedule(hard_iface, tt_num_changes);
  138. }
  139. static void forw_packet_free(struct forw_packet *forw_packet)
  140. {
  141. if (forw_packet->skb)
  142. kfree_skb(forw_packet->skb);
  143. if (forw_packet->if_incoming)
  144. hardif_free_ref(forw_packet->if_incoming);
  145. kfree(forw_packet);
  146. }
  147. static void _add_bcast_packet_to_list(struct bat_priv *bat_priv,
  148. struct forw_packet *forw_packet,
  149. unsigned long send_time)
  150. {
  151. INIT_HLIST_NODE(&forw_packet->list);
  152. /* add new packet to packet list */
  153. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  154. hlist_add_head(&forw_packet->list, &bat_priv->forw_bcast_list);
  155. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  156. /* start timer for this packet */
  157. INIT_DELAYED_WORK(&forw_packet->delayed_work,
  158. send_outstanding_bcast_packet);
  159. queue_delayed_work(bat_event_workqueue, &forw_packet->delayed_work,
  160. send_time);
  161. }
  162. /* add a broadcast packet to the queue and setup timers. broadcast packets
  163. * are sent multiple times to increase probability for being received.
  164. *
  165. * This function returns NETDEV_TX_OK on success and NETDEV_TX_BUSY on
  166. * errors.
  167. *
  168. * The skb is not consumed, so the caller should make sure that the
  169. * skb is freed. */
  170. int add_bcast_packet_to_list(struct bat_priv *bat_priv,
  171. const struct sk_buff *skb, unsigned long delay)
  172. {
  173. struct hard_iface *primary_if = NULL;
  174. struct forw_packet *forw_packet;
  175. struct bcast_packet *bcast_packet;
  176. struct sk_buff *newskb;
  177. if (!atomic_dec_not_zero(&bat_priv->bcast_queue_left)) {
  178. bat_dbg(DBG_BATMAN, bat_priv, "bcast packet queue full\n");
  179. goto out;
  180. }
  181. primary_if = primary_if_get_selected(bat_priv);
  182. if (!primary_if)
  183. goto out_and_inc;
  184. forw_packet = kmalloc(sizeof(*forw_packet), GFP_ATOMIC);
  185. if (!forw_packet)
  186. goto out_and_inc;
  187. newskb = skb_copy(skb, GFP_ATOMIC);
  188. if (!newskb)
  189. goto packet_free;
  190. /* as we have a copy now, it is safe to decrease the TTL */
  191. bcast_packet = (struct bcast_packet *)newskb->data;
  192. bcast_packet->header.ttl--;
  193. skb_reset_mac_header(newskb);
  194. forw_packet->skb = newskb;
  195. forw_packet->if_incoming = primary_if;
  196. /* how often did we send the bcast packet ? */
  197. forw_packet->num_packets = 0;
  198. _add_bcast_packet_to_list(bat_priv, forw_packet, delay);
  199. return NETDEV_TX_OK;
  200. packet_free:
  201. kfree(forw_packet);
  202. out_and_inc:
  203. atomic_inc(&bat_priv->bcast_queue_left);
  204. out:
  205. if (primary_if)
  206. hardif_free_ref(primary_if);
  207. return NETDEV_TX_BUSY;
  208. }
  209. static void send_outstanding_bcast_packet(struct work_struct *work)
  210. {
  211. struct hard_iface *hard_iface;
  212. struct delayed_work *delayed_work =
  213. container_of(work, struct delayed_work, work);
  214. struct forw_packet *forw_packet =
  215. container_of(delayed_work, struct forw_packet, delayed_work);
  216. struct sk_buff *skb1;
  217. struct net_device *soft_iface = forw_packet->if_incoming->soft_iface;
  218. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  219. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  220. hlist_del(&forw_packet->list);
  221. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  222. if (atomic_read(&bat_priv->mesh_state) == MESH_DEACTIVATING)
  223. goto out;
  224. /* rebroadcast packet */
  225. rcu_read_lock();
  226. list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
  227. if (hard_iface->soft_iface != soft_iface)
  228. continue;
  229. /* send a copy of the saved skb */
  230. skb1 = skb_clone(forw_packet->skb, GFP_ATOMIC);
  231. if (skb1)
  232. send_skb_packet(skb1, hard_iface, 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 < 3) {
  238. _add_bcast_packet_to_list(bat_priv, forw_packet,
  239. ((5 * HZ) / 1000));
  240. return;
  241. }
  242. out:
  243. forw_packet_free(forw_packet);
  244. atomic_inc(&bat_priv->bcast_queue_left);
  245. }
  246. void send_outstanding_bat_ogm_packet(struct work_struct *work)
  247. {
  248. struct delayed_work *delayed_work =
  249. container_of(work, struct delayed_work, work);
  250. struct forw_packet *forw_packet =
  251. container_of(delayed_work, struct forw_packet, delayed_work);
  252. struct bat_priv *bat_priv;
  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) == MESH_DEACTIVATING)
  258. goto out;
  259. bat_priv->bat_algo_ops->bat_ogm_emit(forw_packet);
  260. /**
  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. 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. forw_packet_free(forw_packet);
  272. }
  273. void purge_outstanding_packets(struct bat_priv *bat_priv,
  274. const struct hard_iface *hard_iface)
  275. {
  276. struct forw_packet *forw_packet;
  277. struct hlist_node *tmp_node, *safe_tmp_node;
  278. bool pending;
  279. if (hard_iface)
  280. bat_dbg(DBG_BATMAN, bat_priv,
  281. "purge_outstanding_packets(): %s\n",
  282. hard_iface->net_dev->name);
  283. else
  284. bat_dbg(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, tmp_node, safe_tmp_node,
  289. &bat_priv->forw_bcast_list, list) {
  290. /**
  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. /**
  299. * send_outstanding_bcast_packet() will lock the list to
  300. * delete the item from the list
  301. */
  302. pending = cancel_delayed_work_sync(&forw_packet->delayed_work);
  303. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  304. if (pending) {
  305. hlist_del(&forw_packet->list);
  306. forw_packet_free(forw_packet);
  307. }
  308. }
  309. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  310. /* free batman packet list */
  311. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  312. hlist_for_each_entry_safe(forw_packet, tmp_node, safe_tmp_node,
  313. &bat_priv->forw_bat_list, list) {
  314. /**
  315. * if purge_outstanding_packets() was called with an argument
  316. * we delete only packets belonging to the given interface
  317. */
  318. if ((hard_iface) &&
  319. (forw_packet->if_incoming != hard_iface))
  320. continue;
  321. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  322. /**
  323. * send_outstanding_bat_packet() will lock the list to
  324. * delete the item from the list
  325. */
  326. pending = cancel_delayed_work_sync(&forw_packet->delayed_work);
  327. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  328. if (pending) {
  329. hlist_del(&forw_packet->list);
  330. forw_packet_free(forw_packet);
  331. }
  332. }
  333. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  334. }