send.c 20 KB

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