send.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner, Simon Wunderlich
  5. */
  6. #include "send.h"
  7. #include "main.h"
  8. #include <linux/atomic.h>
  9. #include <linux/bug.h>
  10. #include <linux/byteorder/generic.h>
  11. #include <linux/container_of.h>
  12. #include <linux/errno.h>
  13. #include <linux/etherdevice.h>
  14. #include <linux/gfp.h>
  15. #include <linux/if.h>
  16. #include <linux/if_ether.h>
  17. #include <linux/jiffies.h>
  18. #include <linux/kref.h>
  19. #include <linux/list.h>
  20. #include <linux/netdevice.h>
  21. #include <linux/printk.h>
  22. #include <linux/rcupdate.h>
  23. #include <linux/skbuff.h>
  24. #include <linux/slab.h>
  25. #include <linux/spinlock.h>
  26. #include <linux/stddef.h>
  27. #include <linux/workqueue.h>
  28. #include "distributed-arp-table.h"
  29. #include "fragmentation.h"
  30. #include "gateway_client.h"
  31. #include "hard-interface.h"
  32. #include "log.h"
  33. #include "mesh-interface.h"
  34. #include "originator.h"
  35. #include "routing.h"
  36. #include "translation-table.h"
  37. static void batadv_send_outstanding_bcast_packet(struct work_struct *work);
  38. /**
  39. * batadv_send_skb_packet() - send an already prepared packet
  40. * @skb: the packet to send
  41. * @hard_iface: the interface to use to send the broadcast packet
  42. * @dst_addr: the payload destination
  43. *
  44. * Send out an already prepared packet to the given neighbor or broadcast it
  45. * using the specified interface. Either hard_iface or neigh_node must be not
  46. * NULL.
  47. * If neigh_node is NULL, then the packet is broadcasted using hard_iface,
  48. * otherwise it is sent as unicast to the given neighbor.
  49. *
  50. * Regardless of the return value, the skb is consumed.
  51. *
  52. * Return: A negative errno code is returned on a failure. A success does not
  53. * guarantee the frame will be transmitted as it may be dropped due
  54. * to congestion or traffic shaping.
  55. */
  56. int batadv_send_skb_packet(struct sk_buff *skb,
  57. struct batadv_hard_iface *hard_iface,
  58. const u8 *dst_addr)
  59. {
  60. struct ethhdr *ethhdr;
  61. int ret;
  62. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  63. goto send_skb_err;
  64. if (unlikely(!hard_iface->net_dev))
  65. goto send_skb_err;
  66. if (!(hard_iface->net_dev->flags & IFF_UP)) {
  67. pr_warn("Interface %s is not up - can't send packet via that interface!\n",
  68. hard_iface->net_dev->name);
  69. goto send_skb_err;
  70. }
  71. /* push to the ethernet header. */
  72. if (batadv_skb_head_push(skb, ETH_HLEN) < 0)
  73. goto send_skb_err;
  74. skb_reset_mac_header(skb);
  75. ethhdr = eth_hdr(skb);
  76. ether_addr_copy(ethhdr->h_source, hard_iface->net_dev->dev_addr);
  77. ether_addr_copy(ethhdr->h_dest, dst_addr);
  78. ethhdr->h_proto = htons(ETH_P_BATMAN);
  79. skb_set_network_header(skb, ETH_HLEN);
  80. skb->protocol = htons(ETH_P_BATMAN);
  81. skb->dev = hard_iface->net_dev;
  82. /* dev_queue_xmit() returns a negative result on error. However on
  83. * congestion and traffic shaping, it drops and returns NET_XMIT_DROP
  84. * (which is > 0). This will not be treated as an error.
  85. */
  86. ret = dev_queue_xmit(skb);
  87. return net_xmit_eval(ret);
  88. send_skb_err:
  89. kfree_skb(skb);
  90. return NET_XMIT_DROP;
  91. }
  92. /**
  93. * batadv_send_broadcast_skb() - Send broadcast packet via hard interface
  94. * @skb: packet to be transmitted (with batadv header and no outer eth header)
  95. * @hard_iface: outgoing interface
  96. *
  97. * Return: A negative errno code is returned on a failure. A success does not
  98. * guarantee the frame will be transmitted as it may be dropped due
  99. * to congestion or traffic shaping.
  100. */
  101. int batadv_send_broadcast_skb(struct sk_buff *skb,
  102. struct batadv_hard_iface *hard_iface)
  103. {
  104. static const u8 broadcast_addr[] = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
  105. return batadv_send_skb_packet(skb, hard_iface, broadcast_addr);
  106. }
  107. /**
  108. * batadv_send_unicast_skb() - Send unicast packet to neighbor
  109. * @skb: packet to be transmitted (with batadv header and no outer eth header)
  110. * @neigh: neighbor which is used as next hop to destination
  111. *
  112. * Return: A negative errno code is returned on a failure. A success does not
  113. * guarantee the frame will be transmitted as it may be dropped due
  114. * to congestion or traffic shaping.
  115. */
  116. int batadv_send_unicast_skb(struct sk_buff *skb,
  117. struct batadv_neigh_node *neigh)
  118. {
  119. #ifdef CONFIG_BATMAN_ADV_BATMAN_V
  120. struct batadv_hardif_neigh_node *hardif_neigh;
  121. #endif
  122. int ret;
  123. ret = batadv_send_skb_packet(skb, neigh->if_incoming, neigh->addr);
  124. #ifdef CONFIG_BATMAN_ADV_BATMAN_V
  125. hardif_neigh = batadv_hardif_neigh_get(neigh->if_incoming, neigh->addr);
  126. if (hardif_neigh && ret != NET_XMIT_DROP)
  127. hardif_neigh->bat_v.last_unicast_tx = jiffies;
  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: negative errno code on a failure, -EINPROGRESS if the skb is
  144. * buffered for later transmit or the NET_XMIT status returned by the
  145. * lower routine if the packet has been passed down.
  146. */
  147. int batadv_send_skb_to_orig(struct sk_buff *skb,
  148. struct batadv_orig_node *orig_node,
  149. struct batadv_hard_iface *recv_if)
  150. {
  151. struct batadv_priv *bat_priv = orig_node->bat_priv;
  152. struct batadv_neigh_node *neigh_node;
  153. int ret;
  154. /* batadv_find_router() increases neigh_nodes refcount if found. */
  155. neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
  156. if (!neigh_node) {
  157. ret = -EINVAL;
  158. goto free_skb;
  159. }
  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. /* skb was consumed */
  168. skb = NULL;
  169. goto put_neigh_node;
  170. }
  171. ret = batadv_send_unicast_skb(skb, neigh_node);
  172. /* skb was consumed */
  173. skb = NULL;
  174. put_neigh_node:
  175. batadv_neigh_node_put(neigh_node);
  176. free_skb:
  177. kfree_skb(skb);
  178. return ret;
  179. }
  180. /**
  181. * batadv_send_skb_push_fill_unicast() - extend the buffer and initialize the
  182. * common fields for unicast packets
  183. * @skb: the skb carrying the unicast header to initialize
  184. * @hdr_size: amount of bytes to push at the beginning of the skb
  185. * @orig_node: the destination node
  186. *
  187. * Return: false if the buffer extension was not possible or true otherwise.
  188. */
  189. static bool
  190. batadv_send_skb_push_fill_unicast(struct sk_buff *skb, int hdr_size,
  191. struct batadv_orig_node *orig_node)
  192. {
  193. struct batadv_unicast_packet *unicast_packet;
  194. u8 ttvn = (u8)atomic_read(&orig_node->last_ttvn);
  195. if (batadv_skb_head_push(skb, hdr_size) < 0)
  196. return false;
  197. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  198. unicast_packet->version = BATADV_COMPAT_VERSION;
  199. /* batman packet type: unicast */
  200. unicast_packet->packet_type = BATADV_UNICAST;
  201. /* set unicast ttl */
  202. unicast_packet->ttl = BATADV_TTL;
  203. /* copy the destination for faster routing */
  204. ether_addr_copy(unicast_packet->dest, orig_node->orig);
  205. /* set the destination tt version number */
  206. unicast_packet->ttvn = ttvn;
  207. return true;
  208. }
  209. /**
  210. * batadv_send_skb_prepare_unicast() - encapsulate an skb with a unicast header
  211. * @skb: the skb containing the payload to encapsulate
  212. * @orig_node: the destination node
  213. *
  214. * Return: false if the payload could not be encapsulated or true otherwise.
  215. */
  216. static bool batadv_send_skb_prepare_unicast(struct sk_buff *skb,
  217. struct batadv_orig_node *orig_node)
  218. {
  219. size_t uni_size = sizeof(struct batadv_unicast_packet);
  220. return batadv_send_skb_push_fill_unicast(skb, uni_size, orig_node);
  221. }
  222. /**
  223. * batadv_send_skb_prepare_unicast_4addr() - encapsulate an skb with a
  224. * unicast 4addr header
  225. * @bat_priv: the bat priv with all the mesh interface information
  226. * @skb: the skb containing the payload to encapsulate
  227. * @orig: the destination node
  228. * @packet_subtype: the unicast 4addr packet subtype to use
  229. *
  230. * Return: false if the payload could not be encapsulated or true otherwise.
  231. */
  232. bool batadv_send_skb_prepare_unicast_4addr(struct batadv_priv *bat_priv,
  233. struct sk_buff *skb,
  234. struct batadv_orig_node *orig,
  235. int packet_subtype)
  236. {
  237. struct batadv_hard_iface *primary_if;
  238. struct batadv_unicast_4addr_packet *uc_4addr_packet;
  239. bool ret = false;
  240. primary_if = batadv_primary_if_get_selected(bat_priv);
  241. if (!primary_if)
  242. goto out;
  243. /* Pull the header space and fill the unicast_packet substructure.
  244. * We can do that because the first member of the uc_4addr_packet
  245. * is of type struct unicast_packet
  246. */
  247. if (!batadv_send_skb_push_fill_unicast(skb, sizeof(*uc_4addr_packet),
  248. orig))
  249. goto out;
  250. uc_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
  251. uc_4addr_packet->u.packet_type = BATADV_UNICAST_4ADDR;
  252. ether_addr_copy(uc_4addr_packet->src, primary_if->net_dev->dev_addr);
  253. uc_4addr_packet->subtype = packet_subtype;
  254. uc_4addr_packet->reserved = 0;
  255. ret = true;
  256. out:
  257. batadv_hardif_put(primary_if);
  258. return ret;
  259. }
  260. /**
  261. * batadv_send_skb_unicast() - encapsulate and send an skb via unicast
  262. * @bat_priv: the bat priv with all the mesh interface information
  263. * @skb: payload to send
  264. * @packet_type: the batman unicast packet type to use
  265. * @packet_subtype: the unicast 4addr packet subtype (only relevant for unicast
  266. * 4addr packets)
  267. * @orig_node: the originator to send the packet to
  268. * @vid: the vid to be used to search the translation table
  269. *
  270. * Wrap the given skb into a batman-adv unicast or unicast-4addr header
  271. * depending on whether BATADV_UNICAST or BATADV_UNICAST_4ADDR was supplied
  272. * as packet_type. Then send this frame to the given orig_node.
  273. *
  274. * Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
  275. */
  276. int batadv_send_skb_unicast(struct batadv_priv *bat_priv,
  277. struct sk_buff *skb, int packet_type,
  278. int packet_subtype,
  279. struct batadv_orig_node *orig_node,
  280. unsigned short vid)
  281. {
  282. struct batadv_unicast_packet *unicast_packet;
  283. struct ethhdr *ethhdr;
  284. int ret = NET_XMIT_DROP;
  285. if (!orig_node)
  286. goto out;
  287. switch (packet_type) {
  288. case BATADV_UNICAST:
  289. if (!batadv_send_skb_prepare_unicast(skb, orig_node))
  290. goto out;
  291. break;
  292. case BATADV_UNICAST_4ADDR:
  293. if (!batadv_send_skb_prepare_unicast_4addr(bat_priv, skb,
  294. orig_node,
  295. packet_subtype))
  296. goto out;
  297. break;
  298. default:
  299. /* this function supports UNICAST and UNICAST_4ADDR only. It
  300. * should never be invoked with any other packet type
  301. */
  302. goto out;
  303. }
  304. /* skb->data might have been reallocated by
  305. * batadv_send_skb_prepare_unicast{,_4addr}()
  306. */
  307. ethhdr = eth_hdr(skb);
  308. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  309. /* inform the destination node that we are still missing a correct route
  310. * for this client. The destination will receive this packet and will
  311. * try to reroute it because the ttvn contained in the header is less
  312. * than the current one
  313. */
  314. if (batadv_tt_global_client_is_roaming(bat_priv, ethhdr->h_dest, vid))
  315. unicast_packet->ttvn = unicast_packet->ttvn - 1;
  316. ret = batadv_send_skb_to_orig(skb, orig_node, NULL);
  317. /* skb was consumed */
  318. skb = NULL;
  319. out:
  320. kfree_skb(skb);
  321. return ret;
  322. }
  323. /**
  324. * batadv_send_skb_via_tt_generic() - send an skb via TT lookup
  325. * @bat_priv: the bat priv with all the mesh interface information
  326. * @skb: payload to send
  327. * @packet_type: the batman unicast packet type to use
  328. * @packet_subtype: the unicast 4addr packet subtype (only relevant for unicast
  329. * 4addr packets)
  330. * @dst_hint: can be used to override the destination contained in the skb
  331. * @vid: the vid to be used to search the translation table
  332. *
  333. * Look up the recipient node for the destination address in the ethernet
  334. * header via the translation table. Wrap the given skb into a batman-adv
  335. * unicast or unicast-4addr header depending on whether BATADV_UNICAST or
  336. * BATADV_UNICAST_4ADDR was supplied as packet_type. Then send this frame
  337. * to the according destination node.
  338. *
  339. * Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
  340. */
  341. int batadv_send_skb_via_tt_generic(struct batadv_priv *bat_priv,
  342. struct sk_buff *skb, int packet_type,
  343. int packet_subtype, u8 *dst_hint,
  344. unsigned short vid)
  345. {
  346. struct ethhdr *ethhdr = (struct ethhdr *)skb->data;
  347. struct batadv_orig_node *orig_node;
  348. u8 *src, *dst;
  349. int ret;
  350. src = ethhdr->h_source;
  351. dst = ethhdr->h_dest;
  352. /* if we got an hint! let's send the packet to this client (if any) */
  353. if (dst_hint) {
  354. src = NULL;
  355. dst = dst_hint;
  356. }
  357. orig_node = batadv_transtable_search(bat_priv, src, dst, vid);
  358. ret = batadv_send_skb_unicast(bat_priv, skb, packet_type,
  359. packet_subtype, orig_node, vid);
  360. batadv_orig_node_put(orig_node);
  361. return ret;
  362. }
  363. /**
  364. * batadv_send_skb_via_gw() - send an skb via gateway lookup
  365. * @bat_priv: the bat priv with all the mesh interface information
  366. * @skb: payload to send
  367. * @vid: the vid to be used to search the translation table
  368. *
  369. * Look up the currently selected gateway. Wrap the given skb into a batman-adv
  370. * unicast header and send this frame to this gateway node.
  371. *
  372. * Return: NET_XMIT_DROP in case of error or NET_XMIT_SUCCESS otherwise.
  373. */
  374. int batadv_send_skb_via_gw(struct batadv_priv *bat_priv, struct sk_buff *skb,
  375. unsigned short vid)
  376. {
  377. struct batadv_orig_node *orig_node;
  378. int ret;
  379. orig_node = batadv_gw_get_selected_orig(bat_priv);
  380. ret = batadv_send_skb_unicast(bat_priv, skb, BATADV_UNICAST_4ADDR,
  381. BATADV_P_DATA, orig_node, vid);
  382. batadv_orig_node_put(orig_node);
  383. return ret;
  384. }
  385. /**
  386. * batadv_forw_packet_free() - free a forwarding packet
  387. * @forw_packet: The packet to free
  388. * @dropped: whether the packet is freed because is dropped
  389. *
  390. * This frees a forwarding packet and releases any resources it might
  391. * have claimed.
  392. */
  393. void batadv_forw_packet_free(struct batadv_forw_packet *forw_packet,
  394. bool dropped)
  395. {
  396. if (dropped)
  397. kfree_skb(forw_packet->skb);
  398. else
  399. consume_skb(forw_packet->skb);
  400. batadv_hardif_put(forw_packet->if_incoming);
  401. batadv_hardif_put(forw_packet->if_outgoing);
  402. if (forw_packet->queue_left)
  403. atomic_inc(forw_packet->queue_left);
  404. kfree(forw_packet);
  405. }
  406. /**
  407. * batadv_forw_packet_alloc() - allocate a forwarding packet
  408. * @if_incoming: The (optional) if_incoming to be grabbed
  409. * @if_outgoing: The (optional) if_outgoing to be grabbed
  410. * @queue_left: The (optional) queue counter to decrease
  411. * @bat_priv: The bat_priv for the mesh of this forw_packet
  412. * @skb: The raw packet this forwarding packet shall contain
  413. *
  414. * Allocates a forwarding packet and tries to get a reference to the
  415. * (optional) if_incoming, if_outgoing and queue_left. If queue_left
  416. * is NULL then bat_priv is optional, too.
  417. *
  418. * Return: An allocated forwarding packet on success, NULL otherwise.
  419. */
  420. struct batadv_forw_packet *
  421. batadv_forw_packet_alloc(struct batadv_hard_iface *if_incoming,
  422. struct batadv_hard_iface *if_outgoing,
  423. atomic_t *queue_left,
  424. struct batadv_priv *bat_priv,
  425. struct sk_buff *skb)
  426. {
  427. struct batadv_forw_packet *forw_packet;
  428. const char *qname;
  429. if (queue_left && !batadv_atomic_dec_not_zero(queue_left)) {
  430. qname = "unknown";
  431. if (queue_left == &bat_priv->bcast_queue_left)
  432. qname = "bcast";
  433. if (queue_left == &bat_priv->batman_queue_left)
  434. qname = "batman";
  435. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  436. "%s queue is full\n", qname);
  437. return NULL;
  438. }
  439. forw_packet = kmalloc_obj(*forw_packet, GFP_ATOMIC);
  440. if (!forw_packet)
  441. goto err;
  442. if (if_incoming)
  443. kref_get(&if_incoming->refcount);
  444. if (if_outgoing)
  445. kref_get(&if_outgoing->refcount);
  446. INIT_HLIST_NODE(&forw_packet->list);
  447. INIT_HLIST_NODE(&forw_packet->cleanup_list);
  448. forw_packet->skb = skb;
  449. forw_packet->queue_left = queue_left;
  450. forw_packet->if_incoming = if_incoming;
  451. forw_packet->if_outgoing = if_outgoing;
  452. forw_packet->num_packets = 1;
  453. return forw_packet;
  454. err:
  455. if (queue_left)
  456. atomic_inc(queue_left);
  457. return NULL;
  458. }
  459. /**
  460. * batadv_forw_packet_was_stolen() - check whether someone stole this packet
  461. * @forw_packet: the forwarding packet to check
  462. *
  463. * This function checks whether the given forwarding packet was claimed by
  464. * someone else for free().
  465. *
  466. * Return: True if someone stole it, false otherwise.
  467. */
  468. static bool
  469. batadv_forw_packet_was_stolen(struct batadv_forw_packet *forw_packet)
  470. {
  471. return !hlist_unhashed(&forw_packet->cleanup_list);
  472. }
  473. /**
  474. * batadv_forw_packet_steal() - claim a forw_packet for free()
  475. * @forw_packet: the forwarding packet to steal
  476. * @lock: a key to the store to steal from (e.g. forw_{bat,bcast}_list_lock)
  477. *
  478. * This function tries to steal a specific forw_packet from global
  479. * visibility for the purpose of getting it for free(). That means
  480. * the caller is *not* allowed to requeue it afterwards.
  481. *
  482. * Return: True if stealing was successful. False if someone else stole it
  483. * before us.
  484. */
  485. bool batadv_forw_packet_steal(struct batadv_forw_packet *forw_packet,
  486. spinlock_t *lock)
  487. {
  488. /* did purging routine steal it earlier? */
  489. spin_lock_bh(lock);
  490. if (batadv_forw_packet_was_stolen(forw_packet)) {
  491. spin_unlock_bh(lock);
  492. return false;
  493. }
  494. hlist_del_init(&forw_packet->list);
  495. /* Just to spot misuse of this function */
  496. hlist_add_fake(&forw_packet->cleanup_list);
  497. spin_unlock_bh(lock);
  498. return true;
  499. }
  500. /**
  501. * batadv_forw_packet_list_steal() - claim a list of forward packets for free()
  502. * @forw_list: the to be stolen forward packets
  503. * @cleanup_list: a backup pointer, to be able to dispose the packet later
  504. * @hard_iface: the interface to steal forward packets from
  505. *
  506. * This function claims responsibility to free any forw_packet queued on the
  507. * given hard_iface. If hard_iface is NULL forwarding packets on all hard
  508. * interfaces will be claimed.
  509. *
  510. * The packets are being moved from the forw_list to the cleanup_list. This
  511. * makes it possible for already running threads to notice the claim.
  512. */
  513. static void
  514. batadv_forw_packet_list_steal(struct hlist_head *forw_list,
  515. struct hlist_head *cleanup_list,
  516. const struct batadv_hard_iface *hard_iface)
  517. {
  518. struct batadv_forw_packet *forw_packet;
  519. struct hlist_node *safe_tmp_node;
  520. hlist_for_each_entry_safe(forw_packet, safe_tmp_node,
  521. forw_list, list) {
  522. /* if purge_outstanding_packets() was called with an argument
  523. * we delete only packets belonging to the given interface
  524. */
  525. if (hard_iface &&
  526. forw_packet->if_incoming != hard_iface &&
  527. forw_packet->if_outgoing != hard_iface)
  528. continue;
  529. hlist_del(&forw_packet->list);
  530. hlist_add_head(&forw_packet->cleanup_list, cleanup_list);
  531. }
  532. }
  533. /**
  534. * batadv_forw_packet_list_free() - free a list of forward packets
  535. * @head: a list of to be freed forw_packets
  536. *
  537. * This function cancels the scheduling of any packet in the provided list,
  538. * waits for any possibly running packet forwarding thread to finish and
  539. * finally, safely frees this forward packet.
  540. *
  541. * This function might sleep.
  542. */
  543. static void batadv_forw_packet_list_free(struct hlist_head *head)
  544. {
  545. struct batadv_forw_packet *forw_packet;
  546. struct hlist_node *safe_tmp_node;
  547. hlist_for_each_entry_safe(forw_packet, safe_tmp_node, head,
  548. cleanup_list) {
  549. cancel_delayed_work_sync(&forw_packet->delayed_work);
  550. hlist_del(&forw_packet->cleanup_list);
  551. batadv_forw_packet_free(forw_packet, true);
  552. }
  553. }
  554. /**
  555. * batadv_forw_packet_queue() - try to queue a forwarding packet
  556. * @forw_packet: the forwarding packet to queue
  557. * @lock: a key to the store (e.g. forw_{bat,bcast}_list_lock)
  558. * @head: the shelve to queue it on (e.g. forw_{bat,bcast}_list)
  559. * @send_time: timestamp (jiffies) when the packet is to be sent
  560. *
  561. * This function tries to (re)queue a forwarding packet. Requeuing
  562. * is prevented if the according interface is shutting down
  563. * (e.g. if batadv_forw_packet_list_steal() was called for this
  564. * packet earlier).
  565. *
  566. * Calling batadv_forw_packet_queue() after a call to
  567. * batadv_forw_packet_steal() is forbidden!
  568. *
  569. * Caller needs to ensure that forw_packet->delayed_work was initialized.
  570. */
  571. static void batadv_forw_packet_queue(struct batadv_forw_packet *forw_packet,
  572. spinlock_t *lock, struct hlist_head *head,
  573. unsigned long send_time)
  574. {
  575. spin_lock_bh(lock);
  576. /* did purging routine steal it from us? */
  577. if (batadv_forw_packet_was_stolen(forw_packet)) {
  578. /* If you got it for free() without trouble, then
  579. * don't get back into the queue after stealing...
  580. */
  581. WARN_ONCE(hlist_fake(&forw_packet->cleanup_list),
  582. "Requeuing after batadv_forw_packet_steal() not allowed!\n");
  583. spin_unlock_bh(lock);
  584. return;
  585. }
  586. hlist_del_init(&forw_packet->list);
  587. hlist_add_head(&forw_packet->list, head);
  588. queue_delayed_work(batadv_event_workqueue,
  589. &forw_packet->delayed_work,
  590. send_time - jiffies);
  591. spin_unlock_bh(lock);
  592. }
  593. /**
  594. * batadv_forw_packet_bcast_queue() - try to queue a broadcast packet
  595. * @bat_priv: the bat priv with all the mesh interface information
  596. * @forw_packet: the forwarding packet to queue
  597. * @send_time: timestamp (jiffies) when the packet is to be sent
  598. *
  599. * This function tries to (re)queue a broadcast packet.
  600. *
  601. * Caller needs to ensure that forw_packet->delayed_work was initialized.
  602. */
  603. static void
  604. batadv_forw_packet_bcast_queue(struct batadv_priv *bat_priv,
  605. struct batadv_forw_packet *forw_packet,
  606. unsigned long send_time)
  607. {
  608. batadv_forw_packet_queue(forw_packet, &bat_priv->forw_bcast_list_lock,
  609. &bat_priv->forw_bcast_list, send_time);
  610. }
  611. /**
  612. * batadv_forw_packet_ogmv1_queue() - try to queue an OGMv1 packet
  613. * @bat_priv: the bat priv with all the mesh interface information
  614. * @forw_packet: the forwarding packet to queue
  615. * @send_time: timestamp (jiffies) when the packet is to be sent
  616. *
  617. * This function tries to (re)queue an OGMv1 packet.
  618. *
  619. * Caller needs to ensure that forw_packet->delayed_work was initialized.
  620. */
  621. void batadv_forw_packet_ogmv1_queue(struct batadv_priv *bat_priv,
  622. struct batadv_forw_packet *forw_packet,
  623. unsigned long send_time)
  624. {
  625. batadv_forw_packet_queue(forw_packet, &bat_priv->forw_bat_list_lock,
  626. &bat_priv->forw_bat_list, send_time);
  627. }
  628. /**
  629. * batadv_forw_bcast_packet_to_list() - queue broadcast packet for transmissions
  630. * @bat_priv: the bat priv with all the mesh interface information
  631. * @skb: broadcast packet to add
  632. * @delay: number of jiffies to wait before sending
  633. * @own_packet: true if it is a self-generated broadcast packet
  634. * @if_in: the interface where the packet was received on
  635. * @if_out: the outgoing interface to queue on
  636. *
  637. * Adds a broadcast packet to the queue and sets up timers. Broadcast packets
  638. * are sent multiple times to increase probability for being received.
  639. *
  640. * This call clones the given skb, hence the caller needs to take into
  641. * account that the data segment of the original skb might not be
  642. * modifiable anymore.
  643. *
  644. * Return: NETDEV_TX_OK on success and NETDEV_TX_BUSY on errors.
  645. */
  646. static int batadv_forw_bcast_packet_to_list(struct batadv_priv *bat_priv,
  647. struct sk_buff *skb,
  648. unsigned long delay,
  649. bool own_packet,
  650. struct batadv_hard_iface *if_in,
  651. struct batadv_hard_iface *if_out)
  652. {
  653. struct batadv_forw_packet *forw_packet;
  654. unsigned long send_time = jiffies;
  655. struct sk_buff *newskb;
  656. newskb = skb_clone(skb, GFP_ATOMIC);
  657. if (!newskb)
  658. goto err;
  659. forw_packet = batadv_forw_packet_alloc(if_in, if_out,
  660. &bat_priv->bcast_queue_left,
  661. bat_priv, newskb);
  662. if (!forw_packet)
  663. goto err_packet_free;
  664. forw_packet->own = own_packet;
  665. INIT_DELAYED_WORK(&forw_packet->delayed_work,
  666. batadv_send_outstanding_bcast_packet);
  667. send_time += delay ? delay : msecs_to_jiffies(5);
  668. batadv_forw_packet_bcast_queue(bat_priv, forw_packet, send_time);
  669. return NETDEV_TX_OK;
  670. err_packet_free:
  671. kfree_skb(newskb);
  672. err:
  673. return NETDEV_TX_BUSY;
  674. }
  675. /**
  676. * batadv_forw_bcast_packet_if() - forward and queue a broadcast packet
  677. * @bat_priv: the bat priv with all the mesh interface information
  678. * @skb: broadcast packet to add
  679. * @delay: number of jiffies to wait before sending
  680. * @own_packet: true if it is a self-generated broadcast packet
  681. * @if_in: the interface where the packet was received on
  682. * @if_out: the outgoing interface to forward to
  683. *
  684. * Transmits a broadcast packet on the specified interface either immediately
  685. * or if a delay is given after that. Furthermore, queues additional
  686. * retransmissions if this interface is a wireless one.
  687. *
  688. * This call clones the given skb, hence the caller needs to take into
  689. * account that the data segment of the original skb might not be
  690. * modifiable anymore.
  691. *
  692. * Return: NETDEV_TX_OK on success and NETDEV_TX_BUSY on errors.
  693. */
  694. static int batadv_forw_bcast_packet_if(struct batadv_priv *bat_priv,
  695. struct sk_buff *skb,
  696. unsigned long delay,
  697. bool own_packet,
  698. struct batadv_hard_iface *if_in,
  699. struct batadv_hard_iface *if_out)
  700. {
  701. unsigned int num_bcasts = if_out->num_bcasts;
  702. struct sk_buff *newskb;
  703. int ret = NETDEV_TX_OK;
  704. if (!delay) {
  705. newskb = skb_clone(skb, GFP_ATOMIC);
  706. if (!newskb)
  707. return NETDEV_TX_BUSY;
  708. batadv_send_broadcast_skb(newskb, if_out);
  709. num_bcasts--;
  710. }
  711. /* delayed broadcast or rebroadcasts? */
  712. if (num_bcasts >= 1) {
  713. BATADV_SKB_CB(skb)->num_bcasts = num_bcasts;
  714. ret = batadv_forw_bcast_packet_to_list(bat_priv, skb, delay,
  715. own_packet, if_in,
  716. if_out);
  717. }
  718. return ret;
  719. }
  720. /**
  721. * batadv_send_no_broadcast() - check whether (re)broadcast is necessary
  722. * @bat_priv: the bat priv with all the mesh interface information
  723. * @skb: broadcast packet to check
  724. * @own_packet: true if it is a self-generated broadcast packet
  725. * @if_out: the outgoing interface checked and considered for (re)broadcast
  726. *
  727. * Return: False if a packet needs to be (re)broadcasted on the given interface,
  728. * true otherwise.
  729. */
  730. static bool batadv_send_no_broadcast(struct batadv_priv *bat_priv,
  731. struct sk_buff *skb, bool own_packet,
  732. struct batadv_hard_iface *if_out)
  733. {
  734. struct batadv_hardif_neigh_node *neigh_node = NULL;
  735. struct batadv_bcast_packet *bcast_packet;
  736. u8 *orig_neigh;
  737. u8 *neigh_addr;
  738. char *type;
  739. int ret;
  740. if (!own_packet) {
  741. neigh_addr = eth_hdr(skb)->h_source;
  742. neigh_node = batadv_hardif_neigh_get(if_out,
  743. neigh_addr);
  744. }
  745. bcast_packet = (struct batadv_bcast_packet *)skb->data;
  746. orig_neigh = neigh_node ? neigh_node->orig : NULL;
  747. ret = batadv_hardif_no_broadcast(if_out, bcast_packet->orig,
  748. orig_neigh);
  749. batadv_hardif_neigh_put(neigh_node);
  750. /* ok, may broadcast */
  751. if (!ret)
  752. return false;
  753. /* no broadcast */
  754. switch (ret) {
  755. case BATADV_HARDIF_BCAST_NORECIPIENT:
  756. type = "no neighbor";
  757. break;
  758. case BATADV_HARDIF_BCAST_DUPFWD:
  759. type = "single neighbor is source";
  760. break;
  761. case BATADV_HARDIF_BCAST_DUPORIG:
  762. type = "single neighbor is originator";
  763. break;
  764. default:
  765. type = "unknown";
  766. }
  767. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  768. "BCAST packet from orig %pM on %s suppressed: %s\n",
  769. bcast_packet->orig,
  770. if_out->net_dev->name, type);
  771. return true;
  772. }
  773. /**
  774. * __batadv_forw_bcast_packet() - forward and queue a broadcast packet
  775. * @bat_priv: the bat priv with all the mesh interface information
  776. * @skb: broadcast packet to add
  777. * @delay: number of jiffies to wait before sending
  778. * @own_packet: true if it is a self-generated broadcast packet
  779. *
  780. * Transmits a broadcast packet either immediately or if a delay is given
  781. * after that. Furthermore, queues additional retransmissions on wireless
  782. * interfaces.
  783. *
  784. * This call clones the given skb, hence the caller needs to take into
  785. * account that the data segment of the given skb might not be
  786. * modifiable anymore.
  787. *
  788. * Return: NETDEV_TX_OK on success and NETDEV_TX_BUSY on errors.
  789. */
  790. static int __batadv_forw_bcast_packet(struct batadv_priv *bat_priv,
  791. struct sk_buff *skb,
  792. unsigned long delay,
  793. bool own_packet)
  794. {
  795. struct batadv_hard_iface *hard_iface;
  796. struct batadv_hard_iface *primary_if;
  797. struct list_head *iter;
  798. int ret = NETDEV_TX_OK;
  799. primary_if = batadv_primary_if_get_selected(bat_priv);
  800. if (!primary_if)
  801. return NETDEV_TX_BUSY;
  802. rcu_read_lock();
  803. netdev_for_each_lower_private_rcu(bat_priv->mesh_iface, hard_iface, iter) {
  804. if (!kref_get_unless_zero(&hard_iface->refcount))
  805. continue;
  806. if (batadv_send_no_broadcast(bat_priv, skb, own_packet,
  807. hard_iface)) {
  808. batadv_hardif_put(hard_iface);
  809. continue;
  810. }
  811. ret = batadv_forw_bcast_packet_if(bat_priv, skb, delay,
  812. own_packet, primary_if,
  813. hard_iface);
  814. batadv_hardif_put(hard_iface);
  815. if (ret == NETDEV_TX_BUSY)
  816. break;
  817. }
  818. rcu_read_unlock();
  819. batadv_hardif_put(primary_if);
  820. return ret;
  821. }
  822. /**
  823. * batadv_forw_bcast_packet() - forward and queue a broadcast packet
  824. * @bat_priv: the bat priv with all the mesh interface information
  825. * @skb: broadcast packet to add
  826. * @delay: number of jiffies to wait before sending
  827. * @own_packet: true if it is a self-generated broadcast packet
  828. *
  829. * Transmits a broadcast packet either immediately or if a delay is given
  830. * after that. Furthermore, queues additional retransmissions on wireless
  831. * interfaces.
  832. *
  833. * Return: NETDEV_TX_OK on success and NETDEV_TX_BUSY on errors.
  834. */
  835. int batadv_forw_bcast_packet(struct batadv_priv *bat_priv,
  836. struct sk_buff *skb,
  837. unsigned long delay,
  838. bool own_packet)
  839. {
  840. return __batadv_forw_bcast_packet(bat_priv, skb, delay, own_packet);
  841. }
  842. /**
  843. * batadv_send_bcast_packet() - send and queue a broadcast packet
  844. * @bat_priv: the bat priv with all the mesh interface information
  845. * @skb: broadcast packet to add
  846. * @delay: number of jiffies to wait before sending
  847. * @own_packet: true if it is a self-generated broadcast packet
  848. *
  849. * Transmits a broadcast packet either immediately or if a delay is given
  850. * after that. Furthermore, queues additional retransmissions on wireless
  851. * interfaces.
  852. *
  853. * Consumes the provided skb.
  854. */
  855. void batadv_send_bcast_packet(struct batadv_priv *bat_priv,
  856. struct sk_buff *skb,
  857. unsigned long delay,
  858. bool own_packet)
  859. {
  860. __batadv_forw_bcast_packet(bat_priv, skb, delay, own_packet);
  861. consume_skb(skb);
  862. }
  863. /**
  864. * batadv_forw_packet_bcasts_left() - check if a retransmission is necessary
  865. * @forw_packet: the forwarding packet to check
  866. *
  867. * Checks whether a given packet has any (re)transmissions left on the provided
  868. * interface.
  869. *
  870. * hard_iface may be NULL: In that case the number of transmissions this skb had
  871. * so far is compared with the maximum amount of retransmissions independent of
  872. * any interface instead.
  873. *
  874. * Return: True if (re)transmissions are left, false otherwise.
  875. */
  876. static bool
  877. batadv_forw_packet_bcasts_left(struct batadv_forw_packet *forw_packet)
  878. {
  879. return BATADV_SKB_CB(forw_packet->skb)->num_bcasts;
  880. }
  881. /**
  882. * batadv_forw_packet_bcasts_dec() - decrement retransmission counter of a
  883. * packet
  884. * @forw_packet: the packet to decrease the counter for
  885. */
  886. static void
  887. batadv_forw_packet_bcasts_dec(struct batadv_forw_packet *forw_packet)
  888. {
  889. BATADV_SKB_CB(forw_packet->skb)->num_bcasts--;
  890. }
  891. /**
  892. * batadv_forw_packet_is_rebroadcast() - check packet for previous transmissions
  893. * @forw_packet: the packet to check
  894. *
  895. * Return: True if this packet was transmitted before, false otherwise.
  896. */
  897. bool batadv_forw_packet_is_rebroadcast(struct batadv_forw_packet *forw_packet)
  898. {
  899. unsigned char num_bcasts = BATADV_SKB_CB(forw_packet->skb)->num_bcasts;
  900. return num_bcasts != forw_packet->if_outgoing->num_bcasts;
  901. }
  902. /**
  903. * batadv_send_outstanding_bcast_packet() - transmit a queued broadcast packet
  904. * @work: work queue item
  905. *
  906. * Transmits a queued broadcast packet and if necessary reschedules it.
  907. */
  908. static void batadv_send_outstanding_bcast_packet(struct work_struct *work)
  909. {
  910. unsigned long send_time = jiffies + msecs_to_jiffies(5);
  911. struct batadv_forw_packet *forw_packet;
  912. struct delayed_work *delayed_work;
  913. struct batadv_priv *bat_priv;
  914. struct sk_buff *skb1;
  915. bool dropped = false;
  916. delayed_work = to_delayed_work(work);
  917. forw_packet = container_of(delayed_work, struct batadv_forw_packet,
  918. delayed_work);
  919. bat_priv = netdev_priv(forw_packet->if_incoming->mesh_iface);
  920. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_DEACTIVATING) {
  921. dropped = true;
  922. goto out;
  923. }
  924. if (batadv_dat_drop_broadcast_packet(bat_priv, forw_packet)) {
  925. dropped = true;
  926. goto out;
  927. }
  928. /* send a copy of the saved skb */
  929. skb1 = skb_clone(forw_packet->skb, GFP_ATOMIC);
  930. if (!skb1)
  931. goto out;
  932. batadv_send_broadcast_skb(skb1, forw_packet->if_outgoing);
  933. batadv_forw_packet_bcasts_dec(forw_packet);
  934. if (batadv_forw_packet_bcasts_left(forw_packet)) {
  935. batadv_forw_packet_bcast_queue(bat_priv, forw_packet,
  936. send_time);
  937. return;
  938. }
  939. out:
  940. /* do we get something for free()? */
  941. if (batadv_forw_packet_steal(forw_packet,
  942. &bat_priv->forw_bcast_list_lock))
  943. batadv_forw_packet_free(forw_packet, dropped);
  944. }
  945. /**
  946. * batadv_purge_outstanding_packets() - stop/purge scheduled bcast/OGMv1 packets
  947. * @bat_priv: the bat priv with all the mesh interface information
  948. * @hard_iface: the hard interface to cancel and purge bcast/ogm packets on
  949. *
  950. * This method cancels and purges any broadcast and OGMv1 packet on the given
  951. * hard_iface. If hard_iface is NULL, broadcast and OGMv1 packets on all hard
  952. * interfaces will be canceled and purged.
  953. *
  954. * This function might sleep.
  955. */
  956. void
  957. batadv_purge_outstanding_packets(struct batadv_priv *bat_priv,
  958. const struct batadv_hard_iface *hard_iface)
  959. {
  960. struct hlist_head head = HLIST_HEAD_INIT;
  961. if (hard_iface)
  962. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  963. "%s(): %s\n",
  964. __func__, hard_iface->net_dev->name);
  965. else
  966. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  967. "%s()\n", __func__);
  968. /* claim bcast list for free() */
  969. spin_lock_bh(&bat_priv->forw_bcast_list_lock);
  970. batadv_forw_packet_list_steal(&bat_priv->forw_bcast_list, &head,
  971. hard_iface);
  972. spin_unlock_bh(&bat_priv->forw_bcast_list_lock);
  973. /* claim batman packet list for free() */
  974. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  975. batadv_forw_packet_list_steal(&bat_priv->forw_bat_list, &head,
  976. hard_iface);
  977. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  978. /* then cancel or wait for packet workers to finish and free */
  979. batadv_forw_packet_list_free(&head);
  980. }