send.c 34 KB

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