send.c 30 KB

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