send.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /*
  2. * Copyright (C) 2007-2011 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. #include "bat_ogm.h"
  31. static void 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. int send_skb_packet(struct sk_buff *skb, struct hard_iface *hard_iface,
  35. const uint8_t *dst_addr)
  36. {
  37. struct ethhdr *ethhdr;
  38. if (hard_iface->if_status != 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_warning("Interface %s is not up - can't send packet via "
  44. "that interface!\n", hard_iface->net_dev->name);
  45. goto send_skb_err;
  46. }
  47. /* push to the ethernet header. */
  48. if (my_skb_head_push(skb, sizeof(*ethhdr)) < 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(ETH_P_BATMAN);
  55. skb_set_network_header(skb, ETH_HLEN);
  56. skb->priority = TC_PRIO_CONTROL;
  57. skb->protocol = __constant_htons(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. return dev_queue_xmit(skb);
  63. send_skb_err:
  64. kfree_skb(skb);
  65. return NET_XMIT_DROP;
  66. }
  67. static void realloc_packet_buffer(struct hard_iface *hard_iface,
  68. int new_len)
  69. {
  70. unsigned char *new_buff;
  71. new_buff = kmalloc(new_len, GFP_ATOMIC);
  72. /* keep old buffer if kmalloc should fail */
  73. if (new_buff) {
  74. memcpy(new_buff, hard_iface->packet_buff,
  75. BATMAN_OGM_LEN);
  76. kfree(hard_iface->packet_buff);
  77. hard_iface->packet_buff = new_buff;
  78. hard_iface->packet_len = new_len;
  79. }
  80. }
  81. /* when calling this function (hard_iface == primary_if) has to be true */
  82. static int prepare_packet_buffer(struct bat_priv *bat_priv,
  83. struct hard_iface *hard_iface)
  84. {
  85. int new_len;
  86. new_len = BATMAN_OGM_LEN +
  87. tt_len((uint8_t)atomic_read(&bat_priv->tt_local_changes));
  88. /* if we have too many changes for one packet don't send any
  89. * and wait for the tt table request which will be fragmented */
  90. if (new_len > hard_iface->soft_iface->mtu)
  91. new_len = BATMAN_OGM_LEN;
  92. realloc_packet_buffer(hard_iface, new_len);
  93. atomic_set(&bat_priv->tt_crc, tt_local_crc(bat_priv));
  94. /* reset the sending counter */
  95. atomic_set(&bat_priv->tt_ogm_append_cnt, TT_OGM_APPEND_MAX);
  96. return tt_changes_fill_buffer(bat_priv,
  97. hard_iface->packet_buff + BATMAN_OGM_LEN,
  98. hard_iface->packet_len - BATMAN_OGM_LEN);
  99. }
  100. static int reset_packet_buffer(struct bat_priv *bat_priv,
  101. struct hard_iface *hard_iface)
  102. {
  103. realloc_packet_buffer(hard_iface, BATMAN_OGM_LEN);
  104. return 0;
  105. }
  106. void schedule_bat_ogm(struct hard_iface *hard_iface)
  107. {
  108. struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  109. struct hard_iface *primary_if;
  110. int tt_num_changes = -1;
  111. if ((hard_iface->if_status == IF_NOT_IN_USE) ||
  112. (hard_iface->if_status == IF_TO_BE_REMOVED))
  113. return;
  114. /**
  115. * the interface gets activated here to avoid race conditions between
  116. * the moment of activating the interface in
  117. * hardif_activate_interface() where the originator mac is set and
  118. * outdated packets (especially uninitialized mac addresses) in the
  119. * packet queue
  120. */
  121. if (hard_iface->if_status == IF_TO_BE_ACTIVATED)
  122. hard_iface->if_status = IF_ACTIVE;
  123. primary_if = primary_if_get_selected(bat_priv);
  124. if (hard_iface == primary_if) {
  125. /* if at least one change happened */
  126. if (atomic_read(&bat_priv->tt_local_changes) > 0) {
  127. tt_commit_changes(bat_priv);
  128. tt_num_changes = prepare_packet_buffer(bat_priv,
  129. hard_iface);
  130. }
  131. /* if the changes have been sent often enough */
  132. if (!atomic_dec_not_zero(&bat_priv->tt_ogm_append_cnt))
  133. tt_num_changes = reset_packet_buffer(bat_priv,
  134. hard_iface);
  135. }
  136. if (primary_if)
  137. hardif_free_ref(primary_if);
  138. bat_ogm_schedule(hard_iface, tt_num_changes);
  139. }
  140. static void forw_packet_free(struct forw_packet *forw_packet)
  141. {
  142. if (forw_packet->skb)
  143. kfree_skb(forw_packet->skb);
  144. if (forw_packet->if_incoming)
  145. hardif_free_ref(forw_packet->if_incoming);
  146. kfree(forw_packet);
  147. }
  148. static void _add_bcast_packet_to_list(struct bat_priv *bat_priv,
  149. struct forw_packet *forw_packet,
  150. unsigned long send_time)
  151. {
  152. INIT_HLIST_NODE(&forw_packet->list);
  153. /* add new packet to packet list */
  154. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  155. hlist_add_head(&forw_packet->list, &bat_priv->forw_bcast_list);
  156. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  157. /* start timer for this packet */
  158. INIT_DELAYED_WORK(&forw_packet->delayed_work,
  159. send_outstanding_bcast_packet);
  160. queue_delayed_work(bat_event_workqueue, &forw_packet->delayed_work,
  161. send_time);
  162. }
  163. /* add a broadcast packet to the queue and setup timers. broadcast packets
  164. * are sent multiple times to increase probability for being received.
  165. *
  166. * This function returns NETDEV_TX_OK on success and NETDEV_TX_BUSY on
  167. * errors.
  168. *
  169. * The skb is not consumed, so the caller should make sure that the
  170. * skb is freed. */
  171. int add_bcast_packet_to_list(struct bat_priv *bat_priv,
  172. const struct sk_buff *skb, unsigned long delay)
  173. {
  174. struct hard_iface *primary_if = NULL;
  175. struct forw_packet *forw_packet;
  176. struct bcast_packet *bcast_packet;
  177. struct sk_buff *newskb;
  178. if (!atomic_dec_not_zero(&bat_priv->bcast_queue_left)) {
  179. bat_dbg(DBG_BATMAN, bat_priv, "bcast packet queue full\n");
  180. goto out;
  181. }
  182. primary_if = primary_if_get_selected(bat_priv);
  183. if (!primary_if)
  184. goto out_and_inc;
  185. forw_packet = kmalloc(sizeof(*forw_packet), GFP_ATOMIC);
  186. if (!forw_packet)
  187. goto out_and_inc;
  188. newskb = skb_copy(skb, GFP_ATOMIC);
  189. if (!newskb)
  190. goto packet_free;
  191. /* as we have a copy now, it is safe to decrease the TTL */
  192. bcast_packet = (struct bcast_packet *)newskb->data;
  193. bcast_packet->ttl--;
  194. skb_reset_mac_header(newskb);
  195. forw_packet->skb = newskb;
  196. forw_packet->if_incoming = primary_if;
  197. /* how often did we send the bcast packet ? */
  198. forw_packet->num_packets = 0;
  199. _add_bcast_packet_to_list(bat_priv, forw_packet, delay);
  200. return NETDEV_TX_OK;
  201. packet_free:
  202. kfree(forw_packet);
  203. out_and_inc:
  204. atomic_inc(&bat_priv->bcast_queue_left);
  205. out:
  206. if (primary_if)
  207. hardif_free_ref(primary_if);
  208. return NETDEV_TX_BUSY;
  209. }
  210. static void send_outstanding_bcast_packet(struct work_struct *work)
  211. {
  212. struct hard_iface *hard_iface;
  213. struct delayed_work *delayed_work =
  214. container_of(work, struct delayed_work, work);
  215. struct forw_packet *forw_packet =
  216. container_of(delayed_work, struct forw_packet, delayed_work);
  217. struct sk_buff *skb1;
  218. struct net_device *soft_iface = forw_packet->if_incoming->soft_iface;
  219. struct bat_priv *bat_priv = netdev_priv(soft_iface);
  220. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  221. hlist_del(&forw_packet->list);
  222. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  223. if (atomic_read(&bat_priv->mesh_state) == MESH_DEACTIVATING)
  224. goto out;
  225. /* rebroadcast packet */
  226. rcu_read_lock();
  227. list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
  228. if (hard_iface->soft_iface != soft_iface)
  229. continue;
  230. /* send a copy of the saved skb */
  231. skb1 = skb_clone(forw_packet->skb, GFP_ATOMIC);
  232. if (skb1)
  233. send_skb_packet(skb1, hard_iface, broadcast_addr);
  234. }
  235. rcu_read_unlock();
  236. forw_packet->num_packets++;
  237. /* if we still have some more bcasts to send */
  238. if (forw_packet->num_packets < 3) {
  239. _add_bcast_packet_to_list(bat_priv, forw_packet,
  240. ((5 * HZ) / 1000));
  241. return;
  242. }
  243. out:
  244. forw_packet_free(forw_packet);
  245. atomic_inc(&bat_priv->bcast_queue_left);
  246. }
  247. void send_outstanding_bat_ogm_packet(struct work_struct *work)
  248. {
  249. struct delayed_work *delayed_work =
  250. container_of(work, struct delayed_work, work);
  251. struct forw_packet *forw_packet =
  252. container_of(delayed_work, struct forw_packet, delayed_work);
  253. struct bat_priv *bat_priv;
  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) == MESH_DEACTIVATING)
  259. goto out;
  260. bat_ogm_emit(forw_packet);
  261. /**
  262. * we have to have at least one packet in the queue
  263. * to determine the queues wake up time unless we are
  264. * shutting down
  265. */
  266. if (forw_packet->own)
  267. schedule_bat_ogm(forw_packet->if_incoming);
  268. out:
  269. /* don't count own packet */
  270. if (!forw_packet->own)
  271. atomic_inc(&bat_priv->batman_queue_left);
  272. forw_packet_free(forw_packet);
  273. }
  274. void purge_outstanding_packets(struct bat_priv *bat_priv,
  275. const struct hard_iface *hard_iface)
  276. {
  277. struct forw_packet *forw_packet;
  278. struct hlist_node *tmp_node, *safe_tmp_node;
  279. bool pending;
  280. if (hard_iface)
  281. bat_dbg(DBG_BATMAN, bat_priv,
  282. "purge_outstanding_packets(): %s\n",
  283. hard_iface->net_dev->name);
  284. else
  285. bat_dbg(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, tmp_node, safe_tmp_node,
  290. &bat_priv->forw_bcast_list, list) {
  291. /**
  292. * if purge_outstanding_packets() was called with an argument
  293. * we delete only packets belonging to the given interface
  294. */
  295. if ((hard_iface) &&
  296. (forw_packet->if_incoming != hard_iface))
  297. continue;
  298. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  299. /**
  300. * send_outstanding_bcast_packet() will lock the list to
  301. * delete the item from the list
  302. */
  303. pending = cancel_delayed_work_sync(&forw_packet->delayed_work);
  304. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  305. if (pending) {
  306. hlist_del(&forw_packet->list);
  307. forw_packet_free(forw_packet);
  308. }
  309. }
  310. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  311. /* free batman packet list */
  312. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  313. hlist_for_each_entry_safe(forw_packet, tmp_node, safe_tmp_node,
  314. &bat_priv->forw_bat_list, list) {
  315. /**
  316. * if purge_outstanding_packets() was called with an argument
  317. * we delete only packets belonging to the given interface
  318. */
  319. if ((hard_iface) &&
  320. (forw_packet->if_incoming != hard_iface))
  321. continue;
  322. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  323. /**
  324. * send_outstanding_bat_packet() will lock the list to
  325. * delete the item from the list
  326. */
  327. pending = cancel_delayed_work_sync(&forw_packet->delayed_work);
  328. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  329. if (pending) {
  330. hlist_del(&forw_packet->list);
  331. forw_packet_free(forw_packet);
  332. }
  333. }
  334. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  335. }