send.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670
  1. /* Copyright (C) 2007-2015 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, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "send.h"
  18. #include "main.h"
  19. #include <linux/atomic.h>
  20. #include <linux/byteorder/generic.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/fs.h>
  23. #include <linux/if_ether.h>
  24. #include <linux/if.h>
  25. #include <linux/jiffies.h>
  26. #include <linux/kernel.h>
  27. #include <linux/list.h>
  28. #include <linux/netdevice.h>
  29. #include <linux/printk.h>
  30. #include <linux/rculist.h>
  31. #include <linux/rcupdate.h>
  32. #include <linux/skbuff.h>
  33. #include <linux/slab.h>
  34. #include <linux/spinlock.h>
  35. #include <linux/stddef.h>
  36. #include <linux/workqueue.h>
  37. #include "distributed-arp-table.h"
  38. #include "fragmentation.h"
  39. #include "gateway_client.h"
  40. #include "hard-interface.h"
  41. #include "network-coding.h"
  42. #include "originator.h"
  43. #include "routing.h"
  44. #include "soft-interface.h"
  45. #include "translation-table.h"
  46. static void batadv_send_outstanding_bcast_packet(struct work_struct *work);
  47. /* send out an already prepared packet to the given address via the
  48. * specified batman interface
  49. */
  50. int batadv_send_skb_packet(struct sk_buff *skb,
  51. struct batadv_hard_iface *hard_iface,
  52. const u8 *dst_addr)
  53. {
  54. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  55. struct ethhdr *ethhdr;
  56. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  57. goto send_skb_err;
  58. if (unlikely(!hard_iface->net_dev))
  59. goto send_skb_err;
  60. if (!(hard_iface->net_dev->flags & IFF_UP)) {
  61. pr_warn("Interface %s is not up - can't send packet via that interface!\n",
  62. hard_iface->net_dev->name);
  63. goto send_skb_err;
  64. }
  65. /* push to the ethernet header. */
  66. if (batadv_skb_head_push(skb, ETH_HLEN) < 0)
  67. goto send_skb_err;
  68. skb_reset_mac_header(skb);
  69. ethhdr = eth_hdr(skb);
  70. ether_addr_copy(ethhdr->h_source, hard_iface->net_dev->dev_addr);
  71. ether_addr_copy(ethhdr->h_dest, dst_addr);
  72. ethhdr->h_proto = htons(ETH_P_BATMAN);
  73. skb_set_network_header(skb, ETH_HLEN);
  74. skb->protocol = htons(ETH_P_BATMAN);
  75. skb->dev = hard_iface->net_dev;
  76. /* Save a clone of the skb to use when decoding coded packets */
  77. batadv_nc_skb_store_for_decoding(bat_priv, skb);
  78. /* dev_queue_xmit() returns a negative result on error. However on
  79. * congestion and traffic shaping, it drops and returns NET_XMIT_DROP
  80. * (which is > 0). This will not be treated as an error.
  81. */
  82. return dev_queue_xmit(skb);
  83. send_skb_err:
  84. kfree_skb(skb);
  85. return NET_XMIT_DROP;
  86. }
  87. /**
  88. * batadv_send_skb_to_orig - Lookup next-hop and transmit skb.
  89. * @skb: Packet to be transmitted.
  90. * @orig_node: Final destination of the packet.
  91. * @recv_if: Interface used when receiving the packet (can be NULL).
  92. *
  93. * Looks up the best next-hop towards the passed originator and passes the
  94. * skb on for preparation of MAC header. If the packet originated from this
  95. * host, NULL can be passed as recv_if and no interface alternating is
  96. * attempted.
  97. *
  98. * Returns NET_XMIT_SUCCESS on success, NET_XMIT_DROP on failure, or
  99. * NET_XMIT_POLICED if the skb is buffered for later transmit.
  100. */
  101. int batadv_send_skb_to_orig(struct sk_buff *skb,
  102. struct batadv_orig_node *orig_node,
  103. struct batadv_hard_iface *recv_if)
  104. {
  105. struct batadv_priv *bat_priv = orig_node->bat_priv;
  106. struct batadv_neigh_node *neigh_node;
  107. int ret = NET_XMIT_DROP;
  108. /* batadv_find_router() increases neigh_nodes refcount if found. */
  109. neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
  110. if (!neigh_node)
  111. goto out;
  112. /* Check if the skb is too large to send in one piece and fragment
  113. * it if needed.
  114. */
  115. if (atomic_read(&bat_priv->fragmentation) &&
  116. skb->len > neigh_node->if_incoming->net_dev->mtu) {
  117. /* Fragment and send packet. */
  118. if (batadv_frag_send_packet(skb, orig_node, neigh_node))
  119. ret = NET_XMIT_SUCCESS;
  120. goto out;
  121. }
  122. /* try to network code the packet, if it is received on an interface
  123. * (i.e. being forwarded). If the packet originates from this node or if
  124. * network coding fails, then send the packet as usual.
  125. */
  126. if (recv_if && batadv_nc_skb_forward(skb, neigh_node)) {
  127. ret = NET_XMIT_POLICED;
  128. } else {
  129. batadv_send_skb_packet(skb, neigh_node->if_incoming,
  130. neigh_node->addr);
  131. ret = NET_XMIT_SUCCESS;
  132. }
  133. out:
  134. if (neigh_node)
  135. batadv_neigh_node_free_ref(neigh_node);
  136. return ret;
  137. }
  138. /**
  139. * batadv_send_skb_push_fill_unicast - extend the buffer and initialize the
  140. * common fields for unicast packets
  141. * @skb: the skb carrying the unicast header to initialize
  142. * @hdr_size: amount of bytes to push at the beginning of the skb
  143. * @orig_node: the destination node
  144. *
  145. * Returns false if the buffer extension was not possible or true otherwise.
  146. */
  147. static bool
  148. batadv_send_skb_push_fill_unicast(struct sk_buff *skb, int hdr_size,
  149. struct batadv_orig_node *orig_node)
  150. {
  151. struct batadv_unicast_packet *unicast_packet;
  152. u8 ttvn = (u8)atomic_read(&orig_node->last_ttvn);
  153. if (batadv_skb_head_push(skb, hdr_size) < 0)
  154. return false;
  155. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  156. unicast_packet->version = BATADV_COMPAT_VERSION;
  157. /* batman packet type: unicast */
  158. unicast_packet->packet_type = BATADV_UNICAST;
  159. /* set unicast ttl */
  160. unicast_packet->ttl = BATADV_TTL;
  161. /* copy the destination for faster routing */
  162. ether_addr_copy(unicast_packet->dest, orig_node->orig);
  163. /* set the destination tt version number */
  164. unicast_packet->ttvn = ttvn;
  165. return true;
  166. }
  167. /**
  168. * batadv_send_skb_prepare_unicast - encapsulate an skb with a unicast header
  169. * @skb: the skb containing the payload to encapsulate
  170. * @orig_node: the destination node
  171. *
  172. * Returns false if the payload could not be encapsulated or true otherwise.
  173. */
  174. static bool batadv_send_skb_prepare_unicast(struct sk_buff *skb,
  175. struct batadv_orig_node *orig_node)
  176. {
  177. size_t uni_size = sizeof(struct batadv_unicast_packet);
  178. return batadv_send_skb_push_fill_unicast(skb, uni_size, orig_node);
  179. }
  180. /**
  181. * batadv_send_skb_prepare_unicast_4addr - encapsulate an skb with a
  182. * unicast 4addr header
  183. * @bat_priv: the bat priv with all the soft interface information
  184. * @skb: the skb containing the payload to encapsulate
  185. * @orig_node: the destination node
  186. * @packet_subtype: the unicast 4addr packet subtype to use
  187. *
  188. * Returns false if the payload could not be encapsulated or true otherwise.
  189. */
  190. bool batadv_send_skb_prepare_unicast_4addr(struct batadv_priv *bat_priv,
  191. struct sk_buff *skb,
  192. struct batadv_orig_node *orig,
  193. int packet_subtype)
  194. {
  195. struct batadv_hard_iface *primary_if;
  196. struct batadv_unicast_4addr_packet *uc_4addr_packet;
  197. bool ret = false;
  198. primary_if = batadv_primary_if_get_selected(bat_priv);
  199. if (!primary_if)
  200. goto out;
  201. /* Pull the header space and fill the unicast_packet substructure.
  202. * We can do that because the first member of the uc_4addr_packet
  203. * is of type struct unicast_packet
  204. */
  205. if (!batadv_send_skb_push_fill_unicast(skb, sizeof(*uc_4addr_packet),
  206. orig))
  207. goto out;
  208. uc_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
  209. uc_4addr_packet->u.packet_type = BATADV_UNICAST_4ADDR;
  210. ether_addr_copy(uc_4addr_packet->src, primary_if->net_dev->dev_addr);
  211. uc_4addr_packet->subtype = packet_subtype;
  212. uc_4addr_packet->reserved = 0;
  213. ret = true;
  214. out:
  215. if (primary_if)
  216. batadv_hardif_free_ref(primary_if);
  217. return ret;
  218. }
  219. /**
  220. * batadv_send_skb_unicast - encapsulate and send an skb via unicast
  221. * @bat_priv: the bat priv with all the soft interface information
  222. * @skb: payload to send
  223. * @packet_type: the batman unicast packet type to use
  224. * @packet_subtype: the unicast 4addr packet subtype (only relevant for unicast
  225. * 4addr packets)
  226. * @orig_node: the originator to send the packet to
  227. * @vid: the vid to be used to search the translation table
  228. *
  229. * Wrap the given skb into a batman-adv unicast or unicast-4addr header
  230. * depending on whether BATADV_UNICAST or BATADV_UNICAST_4ADDR was supplied
  231. * as packet_type. Then send this frame to the given orig_node and release a
  232. * reference to this orig_node.
  233. *
  234. * Returns NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
  235. */
  236. int batadv_send_skb_unicast(struct batadv_priv *bat_priv,
  237. struct sk_buff *skb, int packet_type,
  238. int packet_subtype,
  239. struct batadv_orig_node *orig_node,
  240. unsigned short vid)
  241. {
  242. struct batadv_unicast_packet *unicast_packet;
  243. struct ethhdr *ethhdr;
  244. int ret = NET_XMIT_DROP;
  245. if (!orig_node)
  246. goto out;
  247. switch (packet_type) {
  248. case BATADV_UNICAST:
  249. if (!batadv_send_skb_prepare_unicast(skb, orig_node))
  250. goto out;
  251. break;
  252. case BATADV_UNICAST_4ADDR:
  253. if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, skb,
  254. orig_node,
  255. packet_subtype))
  256. goto out;
  257. break;
  258. default:
  259. /* this function supports UNICAST and UNICAST_4ADDR only. It
  260. * should never be invoked with any other packet type
  261. */
  262. goto out;
  263. }
  264. /* skb->data might have been reallocated by
  265. * batadv_send_skb_prepare_unicast{,_4addr}()
  266. */
  267. ethhdr = eth_hdr(skb);
  268. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  269. /* inform the destination node that we are still missing a correct route
  270. * for this client. The destination will receive this packet and will
  271. * try to reroute it because the ttvn contained in the header is less
  272. * than the current one
  273. */
  274. if (batadv_tt_global_client_is_roaming(bat_priv, ethhdr->h_dest, vid))
  275. unicast_packet->ttvn = unicast_packet->ttvn - 1;
  276. if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
  277. ret = NET_XMIT_SUCCESS;
  278. out:
  279. if (orig_node)
  280. batadv_orig_node_free_ref(orig_node);
  281. if (ret == NET_XMIT_DROP)
  282. kfree_skb(skb);
  283. return ret;
  284. }
  285. /**
  286. * batadv_send_skb_via_tt_generic - send an skb via TT lookup
  287. * @bat_priv: the bat priv with all the soft interface information
  288. * @skb: payload to send
  289. * @packet_type: the batman unicast packet type to use
  290. * @packet_subtype: the unicast 4addr packet subtype (only relevant for unicast
  291. * 4addr packets)
  292. * @dst_hint: can be used to override the destination contained in the skb
  293. * @vid: the vid to be used to search the translation table
  294. *
  295. * Look up the recipient node for the destination address in the ethernet
  296. * header via the translation table. Wrap the given skb into a batman-adv
  297. * unicast or unicast-4addr header depending on whether BATADV_UNICAST or
  298. * BATADV_UNICAST_4ADDR was supplied as packet_type. Then send this frame
  299. * to the according destination node.
  300. *
  301. * Returns NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
  302. */
  303. int batadv_send_skb_via_tt_generic(struct batadv_priv *bat_priv,
  304. struct sk_buff *skb, int packet_type,
  305. int packet_subtype, u8 *dst_hint,
  306. unsigned short vid)
  307. {
  308. struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
  309. struct batadv_orig_node *orig_node;
  310. u8 *src, *dst;
  311. src = ethhdr->h_source;
  312. dst = ethhdr->h_dest;
  313. /* if we got an hint! let's send the packet to this client (if any) */
  314. if (dst_hint) {
  315. src = NULL;
  316. dst = dst_hint;
  317. }
  318. orig_node = batadv_transtable_search(bat_priv, src, dst, vid);
  319. return batadv_send_skb_unicast(bat_priv, skb, packet_type,
  320. packet_subtype, orig_node, vid);
  321. }
  322. /**
  323. * batadv_send_skb_via_gw - send an skb via gateway lookup
  324. * @bat_priv: the bat priv with all the soft interface information
  325. * @skb: payload to send
  326. * @vid: the vid to be used to search the translation table
  327. *
  328. * Look up the currently selected gateway. Wrap the given skb into a batman-adv
  329. * unicast header and send this frame to this gateway node.
  330. *
  331. * Returns NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
  332. */
  333. int batadv_send_skb_via_gw(struct batadv_priv *bat_priv, struct sk_buff *skb,
  334. unsigned short vid)
  335. {
  336. struct batadv_orig_node *orig_node;
  337. orig_node = batadv_gw_get_selected_orig(bat_priv);
  338. return batadv_send_skb_unicast(bat_priv, skb, BATADV_UNICAST, 0,
  339. orig_node, vid);
  340. }
  341. void batadv_schedule_bat_ogm(struct batadv_hard_iface *hard_iface)
  342. {
  343. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  344. if ((hard_iface->if_status == BATADV_IF_NOT_IN_USE) ||
  345. (hard_iface->if_status == BATADV_IF_TO_BE_REMOVED))
  346. return;
  347. /* the interface gets activated here to avoid race conditions between
  348. * the moment of activating the interface in
  349. * hardif_activate_interface() where the originator mac is set and
  350. * outdated packets (especially uninitialized mac addresses) in the
  351. * packet queue
  352. */
  353. if (hard_iface->if_status == BATADV_IF_TO_BE_ACTIVATED)
  354. hard_iface->if_status = BATADV_IF_ACTIVE;
  355. bat_priv->bat_algo_ops->bat_ogm_schedule(hard_iface);
  356. }
  357. static void batadv_forw_packet_free(struct batadv_forw_packet *forw_packet)
  358. {
  359. if (forw_packet->skb)
  360. kfree_skb(forw_packet->skb);
  361. if (forw_packet->if_incoming)
  362. batadv_hardif_free_ref(forw_packet->if_incoming);
  363. if (forw_packet->if_outgoing)
  364. batadv_hardif_free_ref(forw_packet->if_outgoing);
  365. kfree(forw_packet);
  366. }
  367. static void
  368. _batadv_add_bcast_packet_to_list(struct batadv_priv *bat_priv,
  369. struct batadv_forw_packet *forw_packet,
  370. unsigned long send_time)
  371. {
  372. /* add new packet to packet list */
  373. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  374. hlist_add_head(&forw_packet->list, &bat_priv->forw_bcast_list);
  375. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  376. /* start timer for this packet */
  377. queue_delayed_work(batadv_event_workqueue, &forw_packet->delayed_work,
  378. send_time);
  379. }
  380. /* add a broadcast packet to the queue and setup timers. broadcast packets
  381. * are sent multiple times to increase probability for being received.
  382. *
  383. * This function returns NETDEV_TX_OK on success and NETDEV_TX_BUSY on
  384. * errors.
  385. *
  386. * The skb is not consumed, so the caller should make sure that the
  387. * skb is freed.
  388. */
  389. int batadv_add_bcast_packet_to_list(struct batadv_priv *bat_priv,
  390. const struct sk_buff *skb,
  391. unsigned long delay)
  392. {
  393. struct batadv_hard_iface *primary_if = NULL;
  394. struct batadv_forw_packet *forw_packet;
  395. struct batadv_bcast_packet *bcast_packet;
  396. struct sk_buff *newskb;
  397. if (!batadv_atomic_dec_not_zero(&bat_priv->bcast_queue_left)) {
  398. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  399. "bcast packet queue full\n");
  400. goto out;
  401. }
  402. primary_if = batadv_primary_if_get_selected(bat_priv);
  403. if (!primary_if)
  404. goto out_and_inc;
  405. forw_packet = kmalloc(sizeof(*forw_packet), GFP_ATOMIC);
  406. if (!forw_packet)
  407. goto out_and_inc;
  408. newskb = skb_copy(skb, GFP_ATOMIC);
  409. if (!newskb)
  410. goto packet_free;
  411. /* as we have a copy now, it is safe to decrease the TTL */
  412. bcast_packet = (struct batadv_bcast_packet *)newskb->data;
  413. bcast_packet->ttl--;
  414. skb_reset_mac_header(newskb);
  415. forw_packet->skb = newskb;
  416. forw_packet->if_incoming = primary_if;
  417. forw_packet->if_outgoing = NULL;
  418. /* how often did we send the bcast packet ? */
  419. forw_packet->num_packets = 0;
  420. INIT_DELAYED_WORK(&forw_packet->delayed_work,
  421. batadv_send_outstanding_bcast_packet);
  422. _batadv_add_bcast_packet_to_list(bat_priv, forw_packet, delay);
  423. return NETDEV_TX_OK;
  424. packet_free:
  425. kfree(forw_packet);
  426. out_and_inc:
  427. atomic_inc(&bat_priv->bcast_queue_left);
  428. out:
  429. if (primary_if)
  430. batadv_hardif_free_ref(primary_if);
  431. return NETDEV_TX_BUSY;
  432. }
  433. static void batadv_send_outstanding_bcast_packet(struct work_struct *work)
  434. {
  435. struct batadv_hard_iface *hard_iface;
  436. struct delayed_work *delayed_work;
  437. struct batadv_forw_packet *forw_packet;
  438. struct sk_buff *skb1;
  439. struct net_device *soft_iface;
  440. struct batadv_priv *bat_priv;
  441. delayed_work = container_of(work, struct delayed_work, work);
  442. forw_packet = container_of(delayed_work, struct batadv_forw_packet,
  443. delayed_work);
  444. soft_iface = forw_packet->if_incoming->soft_iface;
  445. bat_priv = netdev_priv(soft_iface);
  446. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  447. hlist_del(&forw_packet->list);
  448. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  449. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
  450. goto out;
  451. if (batadv_dat_drop_broadcast_packet(bat_priv, forw_packet))
  452. goto out;
  453. /* rebroadcast packet */
  454. rcu_read_lock();
  455. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  456. if (hard_iface->soft_iface != soft_iface)
  457. continue;
  458. if (forw_packet->num_packets >= hard_iface->num_bcasts)
  459. continue;
  460. /* send a copy of the saved skb */
  461. skb1 = skb_clone(forw_packet->skb, GFP_ATOMIC);
  462. if (skb1)
  463. batadv_send_skb_packet(skb1, hard_iface,
  464. batadv_broadcast_addr);
  465. }
  466. rcu_read_unlock();
  467. forw_packet->num_packets++;
  468. /* if we still have some more bcasts to send */
  469. if (forw_packet->num_packets < BATADV_NUM_BCASTS_MAX) {
  470. _batadv_add_bcast_packet_to_list(bat_priv, forw_packet,
  471. msecs_to_jiffies(5));
  472. return;
  473. }
  474. out:
  475. batadv_forw_packet_free(forw_packet);
  476. atomic_inc(&bat_priv->bcast_queue_left);
  477. }
  478. void batadv_send_outstanding_bat_ogm_packet(struct work_struct *work)
  479. {
  480. struct delayed_work *delayed_work;
  481. struct batadv_forw_packet *forw_packet;
  482. struct batadv_priv *bat_priv;
  483. delayed_work = container_of(work, struct delayed_work, work);
  484. forw_packet = container_of(delayed_work, struct batadv_forw_packet,
  485. delayed_work);
  486. bat_priv = netdev_priv(forw_packet->if_incoming->soft_iface);
  487. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  488. hlist_del(&forw_packet->list);
  489. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  490. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING)
  491. goto out;
  492. bat_priv->bat_algo_ops->bat_ogm_emit(forw_packet);
  493. /* we have to have at least one packet in the queue to determine the
  494. * queues wake up time unless we are shutting down.
  495. *
  496. * only re-schedule if this is the "original" copy, e.g. the OGM of the
  497. * primary interface should only be rescheduled once per period, but
  498. * this function will be called for the forw_packet instances of the
  499. * other secondary interfaces as well.
  500. */
  501. if (forw_packet->own &&
  502. forw_packet->if_incoming == forw_packet->if_outgoing)
  503. batadv_schedule_bat_ogm(forw_packet->if_incoming);
  504. out:
  505. /* don't count own packet */
  506. if (!forw_packet->own)
  507. atomic_inc(&bat_priv->batman_queue_left);
  508. batadv_forw_packet_free(forw_packet);
  509. }
  510. void
  511. batadv_purge_outstanding_packets(struct batadv_priv *bat_priv,
  512. const struct batadv_hard_iface *hard_iface)
  513. {
  514. struct batadv_forw_packet *forw_packet;
  515. struct hlist_node *safe_tmp_node;
  516. bool pending;
  517. if (hard_iface)
  518. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  519. "purge_outstanding_packets(): %s\n",
  520. hard_iface->net_dev->name);
  521. else
  522. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  523. "purge_outstanding_packets()\n");
  524. /* free bcast list */
  525. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  526. hlist_for_each_entry_safe(forw_packet, safe_tmp_node,
  527. &bat_priv->forw_bcast_list, list) {
  528. /* if purge_outstanding_packets() was called with an argument
  529. * we delete only packets belonging to the given interface
  530. */
  531. if ((hard_iface) &&
  532. (forw_packet->if_incoming != hard_iface) &&
  533. (forw_packet->if_outgoing != hard_iface))
  534. continue;
  535. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  536. /* batadv_send_outstanding_bcast_packet() will lock the list to
  537. * delete the item from the list
  538. */
  539. pending = cancel_delayed_work_sync(&forw_packet->delayed_work);
  540. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  541. if (pending) {
  542. hlist_del(&forw_packet->list);
  543. if (!forw_packet->own)
  544. atomic_inc(&bat_priv->bcast_queue_left);
  545. batadv_forw_packet_free(forw_packet);
  546. }
  547. }
  548. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  549. /* free batman packet list */
  550. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  551. hlist_for_each_entry_safe(forw_packet, safe_tmp_node,
  552. &bat_priv->forw_bat_list, list) {
  553. /* if purge_outstanding_packets() was called with an argument
  554. * we delete only packets belonging to the given interface
  555. */
  556. if ((hard_iface) &&
  557. (forw_packet->if_incoming != hard_iface) &&
  558. (forw_packet->if_outgoing != hard_iface))
  559. continue;
  560. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  561. /* send_outstanding_bat_packet() will lock the list to
  562. * delete the item from the list
  563. */
  564. pending = cancel_delayed_work_sync(&forw_packet->delayed_work);
  565. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  566. if (pending) {
  567. hlist_del(&forw_packet->list);
  568. if (!forw_packet->own)
  569. atomic_inc(&bat_priv->batman_queue_left);
  570. batadv_forw_packet_free(forw_packet);
  571. }
  572. }
  573. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  574. }