send.c 29 KB

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