send.c 21 KB

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