network-coding.c 55 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840
  1. /* Copyright (C) 2012-2013 B.A.T.M.A.N. contributors:
  2. *
  3. * Martin Hundebøll, Jeppe Ledet-Pedersen
  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, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA
  18. */
  19. #include <linux/debugfs.h>
  20. #include "main.h"
  21. #include "hash.h"
  22. #include "network-coding.h"
  23. #include "send.h"
  24. #include "originator.h"
  25. #include "hard-interface.h"
  26. #include "routing.h"
  27. static struct lock_class_key batadv_nc_coding_hash_lock_class_key;
  28. static struct lock_class_key batadv_nc_decoding_hash_lock_class_key;
  29. static void batadv_nc_worker(struct work_struct *work);
  30. static int batadv_nc_recv_coded_packet(struct sk_buff *skb,
  31. struct batadv_hard_iface *recv_if);
  32. /**
  33. * batadv_nc_init - one-time initialization for network coding
  34. */
  35. int __init batadv_nc_init(void)
  36. {
  37. int ret;
  38. /* Register our packet type */
  39. ret = batadv_recv_handler_register(BATADV_CODED,
  40. batadv_nc_recv_coded_packet);
  41. return ret;
  42. }
  43. /**
  44. * batadv_nc_start_timer - initialise the nc periodic worker
  45. * @bat_priv: the bat priv with all the soft interface information
  46. */
  47. static void batadv_nc_start_timer(struct batadv_priv *bat_priv)
  48. {
  49. queue_delayed_work(batadv_event_workqueue, &bat_priv->nc.work,
  50. msecs_to_jiffies(10));
  51. }
  52. /**
  53. * batadv_nc_mesh_init - initialise coding hash table and start house keeping
  54. * @bat_priv: the bat priv with all the soft interface information
  55. */
  56. int batadv_nc_mesh_init(struct batadv_priv *bat_priv)
  57. {
  58. bat_priv->nc.timestamp_fwd_flush = jiffies;
  59. bat_priv->nc.timestamp_sniffed_purge = jiffies;
  60. if (bat_priv->nc.coding_hash || bat_priv->nc.decoding_hash)
  61. return 0;
  62. bat_priv->nc.coding_hash = batadv_hash_new(128);
  63. if (!bat_priv->nc.coding_hash)
  64. goto err;
  65. batadv_hash_set_lock_class(bat_priv->nc.coding_hash,
  66. &batadv_nc_coding_hash_lock_class_key);
  67. bat_priv->nc.decoding_hash = batadv_hash_new(128);
  68. if (!bat_priv->nc.decoding_hash)
  69. goto err;
  70. batadv_hash_set_lock_class(bat_priv->nc.coding_hash,
  71. &batadv_nc_decoding_hash_lock_class_key);
  72. INIT_DELAYED_WORK(&bat_priv->nc.work, batadv_nc_worker);
  73. batadv_nc_start_timer(bat_priv);
  74. return 0;
  75. err:
  76. return -ENOMEM;
  77. }
  78. /**
  79. * batadv_nc_init_bat_priv - initialise the nc specific bat_priv variables
  80. * @bat_priv: the bat priv with all the soft interface information
  81. */
  82. void batadv_nc_init_bat_priv(struct batadv_priv *bat_priv)
  83. {
  84. atomic_set(&bat_priv->network_coding, 1);
  85. bat_priv->nc.min_tq = 200;
  86. bat_priv->nc.max_fwd_delay = 10;
  87. bat_priv->nc.max_buffer_time = 200;
  88. }
  89. /**
  90. * batadv_nc_init_orig - initialise the nc fields of an orig_node
  91. * @orig_node: the orig_node which is going to be initialised
  92. */
  93. void batadv_nc_init_orig(struct batadv_orig_node *orig_node)
  94. {
  95. INIT_LIST_HEAD(&orig_node->in_coding_list);
  96. INIT_LIST_HEAD(&orig_node->out_coding_list);
  97. spin_lock_init(&orig_node->in_coding_list_lock);
  98. spin_lock_init(&orig_node->out_coding_list_lock);
  99. }
  100. /**
  101. * batadv_nc_node_free_rcu - rcu callback to free an nc node and remove
  102. * its refcount on the orig_node
  103. * @rcu: rcu pointer of the nc node
  104. */
  105. static void batadv_nc_node_free_rcu(struct rcu_head *rcu)
  106. {
  107. struct batadv_nc_node *nc_node;
  108. nc_node = container_of(rcu, struct batadv_nc_node, rcu);
  109. batadv_orig_node_free_ref(nc_node->orig_node);
  110. kfree(nc_node);
  111. }
  112. /**
  113. * batadv_nc_node_free_ref - decrements the nc node refcounter and possibly
  114. * frees it
  115. * @nc_node: the nc node to free
  116. */
  117. static void batadv_nc_node_free_ref(struct batadv_nc_node *nc_node)
  118. {
  119. if (atomic_dec_and_test(&nc_node->refcount))
  120. call_rcu(&nc_node->rcu, batadv_nc_node_free_rcu);
  121. }
  122. /**
  123. * batadv_nc_path_free_ref - decrements the nc path refcounter and possibly
  124. * frees it
  125. * @nc_path: the nc node to free
  126. */
  127. static void batadv_nc_path_free_ref(struct batadv_nc_path *nc_path)
  128. {
  129. if (atomic_dec_and_test(&nc_path->refcount))
  130. kfree_rcu(nc_path, rcu);
  131. }
  132. /**
  133. * batadv_nc_packet_free - frees nc packet
  134. * @nc_packet: the nc packet to free
  135. */
  136. static void batadv_nc_packet_free(struct batadv_nc_packet *nc_packet)
  137. {
  138. if (nc_packet->skb)
  139. kfree_skb(nc_packet->skb);
  140. batadv_nc_path_free_ref(nc_packet->nc_path);
  141. kfree(nc_packet);
  142. }
  143. /**
  144. * batadv_nc_to_purge_nc_node - checks whether an nc node has to be purged
  145. * @bat_priv: the bat priv with all the soft interface information
  146. * @nc_node: the nc node to check
  147. *
  148. * Returns true if the entry has to be purged now, false otherwise
  149. */
  150. static bool batadv_nc_to_purge_nc_node(struct batadv_priv *bat_priv,
  151. struct batadv_nc_node *nc_node)
  152. {
  153. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  154. return true;
  155. return batadv_has_timed_out(nc_node->last_seen, BATADV_NC_NODE_TIMEOUT);
  156. }
  157. /**
  158. * batadv_nc_to_purge_nc_path_coding - checks whether an nc path has timed out
  159. * @bat_priv: the bat priv with all the soft interface information
  160. * @nc_path: the nc path to check
  161. *
  162. * Returns true if the entry has to be purged now, false otherwise
  163. */
  164. static bool batadv_nc_to_purge_nc_path_coding(struct batadv_priv *bat_priv,
  165. struct batadv_nc_path *nc_path)
  166. {
  167. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  168. return true;
  169. /* purge the path when no packets has been added for 10 times the
  170. * max_fwd_delay time
  171. */
  172. return batadv_has_timed_out(nc_path->last_valid,
  173. bat_priv->nc.max_fwd_delay * 10);
  174. }
  175. /**
  176. * batadv_nc_to_purge_nc_path_decoding - checks whether an nc path has timed out
  177. * @bat_priv: the bat priv with all the soft interface information
  178. * @nc_path: the nc path to check
  179. *
  180. * Returns true if the entry has to be purged now, false otherwise
  181. */
  182. static bool batadv_nc_to_purge_nc_path_decoding(struct batadv_priv *bat_priv,
  183. struct batadv_nc_path *nc_path)
  184. {
  185. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  186. return true;
  187. /* purge the path when no packets has been added for 10 times the
  188. * max_buffer time
  189. */
  190. return batadv_has_timed_out(nc_path->last_valid,
  191. bat_priv->nc.max_buffer_time*10);
  192. }
  193. /**
  194. * batadv_nc_purge_orig_nc_nodes - go through list of nc nodes and purge stale
  195. * entries
  196. * @bat_priv: the bat priv with all the soft interface information
  197. * @list: list of nc nodes
  198. * @lock: nc node list lock
  199. * @to_purge: function in charge to decide whether an entry has to be purged or
  200. * not. This function takes the nc node as argument and has to return
  201. * a boolean value: true if the entry has to be deleted, false
  202. * otherwise
  203. */
  204. static void
  205. batadv_nc_purge_orig_nc_nodes(struct batadv_priv *bat_priv,
  206. struct list_head *list,
  207. spinlock_t *lock,
  208. bool (*to_purge)(struct batadv_priv *,
  209. struct batadv_nc_node *))
  210. {
  211. struct batadv_nc_node *nc_node, *nc_node_tmp;
  212. /* For each nc_node in list */
  213. spin_lock_bh(lock);
  214. list_for_each_entry_safe(nc_node, nc_node_tmp, list, list) {
  215. /* if an helper function has been passed as parameter,
  216. * ask it if the entry has to be purged or not
  217. */
  218. if (to_purge && !to_purge(bat_priv, nc_node))
  219. continue;
  220. batadv_dbg(BATADV_DBG_NC, bat_priv,
  221. "Removing nc_node %pM -> %pM\n",
  222. nc_node->addr, nc_node->orig_node->orig);
  223. list_del_rcu(&nc_node->list);
  224. batadv_nc_node_free_ref(nc_node);
  225. }
  226. spin_unlock_bh(lock);
  227. }
  228. /**
  229. * batadv_nc_purge_orig - purges all nc node data attached of the given
  230. * originator
  231. * @bat_priv: the bat priv with all the soft interface information
  232. * @orig_node: orig_node with the nc node entries to be purged
  233. * @to_purge: function in charge to decide whether an entry has to be purged or
  234. * not. This function takes the nc node as argument and has to return
  235. * a boolean value: true is the entry has to be deleted, false
  236. * otherwise
  237. */
  238. void batadv_nc_purge_orig(struct batadv_priv *bat_priv,
  239. struct batadv_orig_node *orig_node,
  240. bool (*to_purge)(struct batadv_priv *,
  241. struct batadv_nc_node *))
  242. {
  243. /* Check ingoing nc_node's of this orig_node */
  244. batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->in_coding_list,
  245. &orig_node->in_coding_list_lock,
  246. to_purge);
  247. /* Check outgoing nc_node's of this orig_node */
  248. batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->out_coding_list,
  249. &orig_node->out_coding_list_lock,
  250. to_purge);
  251. }
  252. /**
  253. * batadv_nc_purge_orig_hash - traverse entire originator hash to check if they
  254. * have timed out nc nodes
  255. * @bat_priv: the bat priv with all the soft interface information
  256. */
  257. static void batadv_nc_purge_orig_hash(struct batadv_priv *bat_priv)
  258. {
  259. struct batadv_hashtable *hash = bat_priv->orig_hash;
  260. struct hlist_head *head;
  261. struct batadv_orig_node *orig_node;
  262. uint32_t i;
  263. if (!hash)
  264. return;
  265. /* For each orig_node */
  266. for (i = 0; i < hash->size; i++) {
  267. head = &hash->table[i];
  268. rcu_read_lock();
  269. hlist_for_each_entry_rcu(orig_node, head, hash_entry)
  270. batadv_nc_purge_orig(bat_priv, orig_node,
  271. batadv_nc_to_purge_nc_node);
  272. rcu_read_unlock();
  273. }
  274. }
  275. /**
  276. * batadv_nc_purge_paths - traverse all nc paths part of the hash and remove
  277. * unused ones
  278. * @bat_priv: the bat priv with all the soft interface information
  279. * @hash: hash table containing the nc paths to check
  280. * @to_purge: function in charge to decide whether an entry has to be purged or
  281. * not. This function takes the nc node as argument and has to return
  282. * a boolean value: true is the entry has to be deleted, false
  283. * otherwise
  284. */
  285. static void batadv_nc_purge_paths(struct batadv_priv *bat_priv,
  286. struct batadv_hashtable *hash,
  287. bool (*to_purge)(struct batadv_priv *,
  288. struct batadv_nc_path *))
  289. {
  290. struct hlist_head *head;
  291. struct hlist_node *node_tmp;
  292. struct batadv_nc_path *nc_path;
  293. spinlock_t *lock; /* Protects lists in hash */
  294. uint32_t i;
  295. for (i = 0; i < hash->size; i++) {
  296. head = &hash->table[i];
  297. lock = &hash->list_locks[i];
  298. /* For each nc_path in this bin */
  299. spin_lock_bh(lock);
  300. hlist_for_each_entry_safe(nc_path, node_tmp, head, hash_entry) {
  301. /* if an helper function has been passed as parameter,
  302. * ask it if the entry has to be purged or not
  303. */
  304. if (to_purge && !to_purge(bat_priv, nc_path))
  305. continue;
  306. /* purging an non-empty nc_path should never happen, but
  307. * is observed under high CPU load. Delay the purging
  308. * until next iteration to allow the packet_list to be
  309. * emptied first.
  310. */
  311. if (!unlikely(list_empty(&nc_path->packet_list))) {
  312. net_ratelimited_function(printk,
  313. KERN_WARNING
  314. "Skipping free of non-empty nc_path (%pM -> %pM)!\n",
  315. nc_path->prev_hop,
  316. nc_path->next_hop);
  317. continue;
  318. }
  319. /* nc_path is unused, so remove it */
  320. batadv_dbg(BATADV_DBG_NC, bat_priv,
  321. "Remove nc_path %pM -> %pM\n",
  322. nc_path->prev_hop, nc_path->next_hop);
  323. hlist_del_rcu(&nc_path->hash_entry);
  324. batadv_nc_path_free_ref(nc_path);
  325. }
  326. spin_unlock_bh(lock);
  327. }
  328. }
  329. /**
  330. * batadv_nc_hash_key_gen - computes the nc_path hash key
  331. * @key: buffer to hold the final hash key
  332. * @src: source ethernet mac address going into the hash key
  333. * @dst: destination ethernet mac address going into the hash key
  334. */
  335. static void batadv_nc_hash_key_gen(struct batadv_nc_path *key, const char *src,
  336. const char *dst)
  337. {
  338. memcpy(key->prev_hop, src, sizeof(key->prev_hop));
  339. memcpy(key->next_hop, dst, sizeof(key->next_hop));
  340. }
  341. /**
  342. * batadv_nc_hash_choose - compute the hash value for an nc path
  343. * @data: data to hash
  344. * @size: size of the hash table
  345. *
  346. * Returns the selected index in the hash table for the given data.
  347. */
  348. static uint32_t batadv_nc_hash_choose(const void *data, uint32_t size)
  349. {
  350. const struct batadv_nc_path *nc_path = data;
  351. uint32_t hash = 0;
  352. hash = batadv_hash_bytes(hash, &nc_path->prev_hop,
  353. sizeof(nc_path->prev_hop));
  354. hash = batadv_hash_bytes(hash, &nc_path->next_hop,
  355. sizeof(nc_path->next_hop));
  356. hash += (hash << 3);
  357. hash ^= (hash >> 11);
  358. hash += (hash << 15);
  359. return hash % size;
  360. }
  361. /**
  362. * batadv_nc_hash_compare - comparing function used in the network coding hash
  363. * tables
  364. * @node: node in the local table
  365. * @data2: second object to compare the node to
  366. *
  367. * Returns 1 if the two entry are the same, 0 otherwise
  368. */
  369. static int batadv_nc_hash_compare(const struct hlist_node *node,
  370. const void *data2)
  371. {
  372. const struct batadv_nc_path *nc_path1, *nc_path2;
  373. nc_path1 = container_of(node, struct batadv_nc_path, hash_entry);
  374. nc_path2 = data2;
  375. /* Return 1 if the two keys are identical */
  376. if (memcmp(nc_path1->prev_hop, nc_path2->prev_hop,
  377. sizeof(nc_path1->prev_hop)) != 0)
  378. return 0;
  379. if (memcmp(nc_path1->next_hop, nc_path2->next_hop,
  380. sizeof(nc_path1->next_hop)) != 0)
  381. return 0;
  382. return 1;
  383. }
  384. /**
  385. * batadv_nc_hash_find - search for an existing nc path and return it
  386. * @hash: hash table containing the nc path
  387. * @data: search key
  388. *
  389. * Returns the nc_path if found, NULL otherwise.
  390. */
  391. static struct batadv_nc_path *
  392. batadv_nc_hash_find(struct batadv_hashtable *hash,
  393. void *data)
  394. {
  395. struct hlist_head *head;
  396. struct batadv_nc_path *nc_path, *nc_path_tmp = NULL;
  397. int index;
  398. if (!hash)
  399. return NULL;
  400. index = batadv_nc_hash_choose(data, hash->size);
  401. head = &hash->table[index];
  402. rcu_read_lock();
  403. hlist_for_each_entry_rcu(nc_path, head, hash_entry) {
  404. if (!batadv_nc_hash_compare(&nc_path->hash_entry, data))
  405. continue;
  406. if (!atomic_inc_not_zero(&nc_path->refcount))
  407. continue;
  408. nc_path_tmp = nc_path;
  409. break;
  410. }
  411. rcu_read_unlock();
  412. return nc_path_tmp;
  413. }
  414. /**
  415. * batadv_nc_send_packet - send non-coded packet and free nc_packet struct
  416. * @nc_packet: the nc packet to send
  417. */
  418. static void batadv_nc_send_packet(struct batadv_nc_packet *nc_packet)
  419. {
  420. batadv_send_skb_packet(nc_packet->skb,
  421. nc_packet->neigh_node->if_incoming,
  422. nc_packet->nc_path->next_hop);
  423. nc_packet->skb = NULL;
  424. batadv_nc_packet_free(nc_packet);
  425. }
  426. /**
  427. * batadv_nc_sniffed_purge - Checks timestamp of given sniffed nc_packet.
  428. * @bat_priv: the bat priv with all the soft interface information
  429. * @nc_path: the nc path the packet belongs to
  430. * @nc_packet: the nc packet to be checked
  431. *
  432. * Checks whether the given sniffed (overheard) nc_packet has hit its buffering
  433. * timeout. If so, the packet is no longer kept and the entry deleted from the
  434. * queue. Has to be called with the appropriate locks.
  435. *
  436. * Returns false as soon as the entry in the fifo queue has not been timed out
  437. * yet and true otherwise.
  438. */
  439. static bool batadv_nc_sniffed_purge(struct batadv_priv *bat_priv,
  440. struct batadv_nc_path *nc_path,
  441. struct batadv_nc_packet *nc_packet)
  442. {
  443. unsigned long timeout = bat_priv->nc.max_buffer_time;
  444. bool res = false;
  445. /* Packets are added to tail, so the remaining packets did not time
  446. * out and we can stop processing the current queue
  447. */
  448. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE &&
  449. !batadv_has_timed_out(nc_packet->timestamp, timeout))
  450. goto out;
  451. /* purge nc packet */
  452. list_del(&nc_packet->list);
  453. batadv_nc_packet_free(nc_packet);
  454. res = true;
  455. out:
  456. return res;
  457. }
  458. /**
  459. * batadv_nc_fwd_flush - Checks the timestamp of the given nc packet.
  460. * @bat_priv: the bat priv with all the soft interface information
  461. * @nc_path: the nc path the packet belongs to
  462. * @nc_packet: the nc packet to be checked
  463. *
  464. * Checks whether the given nc packet has hit its forward timeout. If so, the
  465. * packet is no longer delayed, immediately sent and the entry deleted from the
  466. * queue. Has to be called with the appropriate locks.
  467. *
  468. * Returns false as soon as the entry in the fifo queue has not been timed out
  469. * yet and true otherwise.
  470. */
  471. static bool batadv_nc_fwd_flush(struct batadv_priv *bat_priv,
  472. struct batadv_nc_path *nc_path,
  473. struct batadv_nc_packet *nc_packet)
  474. {
  475. unsigned long timeout = bat_priv->nc.max_fwd_delay;
  476. /* Packets are added to tail, so the remaining packets did not time
  477. * out and we can stop processing the current queue
  478. */
  479. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE &&
  480. !batadv_has_timed_out(nc_packet->timestamp, timeout))
  481. return false;
  482. /* Send packet */
  483. batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
  484. batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
  485. nc_packet->skb->len + ETH_HLEN);
  486. list_del(&nc_packet->list);
  487. batadv_nc_send_packet(nc_packet);
  488. return true;
  489. }
  490. /**
  491. * batadv_nc_process_nc_paths - traverse given nc packet pool and free timed out
  492. * nc packets
  493. * @bat_priv: the bat priv with all the soft interface information
  494. * @hash: to be processed hash table
  495. * @process_fn: Function called to process given nc packet. Should return true
  496. * to encourage this function to proceed with the next packet.
  497. * Otherwise the rest of the current queue is skipped.
  498. */
  499. static void
  500. batadv_nc_process_nc_paths(struct batadv_priv *bat_priv,
  501. struct batadv_hashtable *hash,
  502. bool (*process_fn)(struct batadv_priv *,
  503. struct batadv_nc_path *,
  504. struct batadv_nc_packet *))
  505. {
  506. struct hlist_head *head;
  507. struct batadv_nc_packet *nc_packet, *nc_packet_tmp;
  508. struct batadv_nc_path *nc_path;
  509. bool ret;
  510. int i;
  511. if (!hash)
  512. return;
  513. /* Loop hash table bins */
  514. for (i = 0; i < hash->size; i++) {
  515. head = &hash->table[i];
  516. /* Loop coding paths */
  517. rcu_read_lock();
  518. hlist_for_each_entry_rcu(nc_path, head, hash_entry) {
  519. /* Loop packets */
  520. spin_lock_bh(&nc_path->packet_list_lock);
  521. list_for_each_entry_safe(nc_packet, nc_packet_tmp,
  522. &nc_path->packet_list, list) {
  523. ret = process_fn(bat_priv, nc_path, nc_packet);
  524. if (!ret)
  525. break;
  526. }
  527. spin_unlock_bh(&nc_path->packet_list_lock);
  528. }
  529. rcu_read_unlock();
  530. }
  531. }
  532. /**
  533. * batadv_nc_worker - periodic task for house keeping related to network coding
  534. * @work: kernel work struct
  535. */
  536. static void batadv_nc_worker(struct work_struct *work)
  537. {
  538. struct delayed_work *delayed_work;
  539. struct batadv_priv_nc *priv_nc;
  540. struct batadv_priv *bat_priv;
  541. unsigned long timeout;
  542. delayed_work = container_of(work, struct delayed_work, work);
  543. priv_nc = container_of(delayed_work, struct batadv_priv_nc, work);
  544. bat_priv = container_of(priv_nc, struct batadv_priv, nc);
  545. batadv_nc_purge_orig_hash(bat_priv);
  546. batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash,
  547. batadv_nc_to_purge_nc_path_coding);
  548. batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash,
  549. batadv_nc_to_purge_nc_path_decoding);
  550. timeout = bat_priv->nc.max_fwd_delay;
  551. if (batadv_has_timed_out(bat_priv->nc.timestamp_fwd_flush, timeout)) {
  552. batadv_nc_process_nc_paths(bat_priv, bat_priv->nc.coding_hash,
  553. batadv_nc_fwd_flush);
  554. bat_priv->nc.timestamp_fwd_flush = jiffies;
  555. }
  556. if (batadv_has_timed_out(bat_priv->nc.timestamp_sniffed_purge,
  557. bat_priv->nc.max_buffer_time)) {
  558. batadv_nc_process_nc_paths(bat_priv, bat_priv->nc.decoding_hash,
  559. batadv_nc_sniffed_purge);
  560. bat_priv->nc.timestamp_sniffed_purge = jiffies;
  561. }
  562. /* Schedule a new check */
  563. batadv_nc_start_timer(bat_priv);
  564. }
  565. /**
  566. * batadv_can_nc_with_orig - checks whether the given orig node is suitable for
  567. * coding or not
  568. * @bat_priv: the bat priv with all the soft interface information
  569. * @orig_node: neighboring orig node which may be used as nc candidate
  570. * @ogm_packet: incoming ogm packet also used for the checks
  571. *
  572. * Returns true if:
  573. * 1) The OGM must have the most recent sequence number.
  574. * 2) The TTL must be decremented by one and only one.
  575. * 3) The OGM must be received from the first hop from orig_node.
  576. * 4) The TQ value of the OGM must be above bat_priv->nc.min_tq.
  577. */
  578. static bool batadv_can_nc_with_orig(struct batadv_priv *bat_priv,
  579. struct batadv_orig_node *orig_node,
  580. struct batadv_ogm_packet *ogm_packet)
  581. {
  582. if (orig_node->last_real_seqno != ntohl(ogm_packet->seqno))
  583. return false;
  584. if (orig_node->last_ttl != ogm_packet->header.ttl + 1)
  585. return false;
  586. if (!batadv_compare_eth(ogm_packet->orig, ogm_packet->prev_sender))
  587. return false;
  588. if (ogm_packet->tq < bat_priv->nc.min_tq)
  589. return false;
  590. return true;
  591. }
  592. /**
  593. * batadv_nc_find_nc_node - search for an existing nc node and return it
  594. * @orig_node: orig node originating the ogm packet
  595. * @orig_neigh_node: neighboring orig node from which we received the ogm packet
  596. * (can be equal to orig_node)
  597. * @in_coding: traverse incoming or outgoing network coding list
  598. *
  599. * Returns the nc_node if found, NULL otherwise.
  600. */
  601. static struct batadv_nc_node
  602. *batadv_nc_find_nc_node(struct batadv_orig_node *orig_node,
  603. struct batadv_orig_node *orig_neigh_node,
  604. bool in_coding)
  605. {
  606. struct batadv_nc_node *nc_node, *nc_node_out = NULL;
  607. struct list_head *list;
  608. if (in_coding)
  609. list = &orig_neigh_node->in_coding_list;
  610. else
  611. list = &orig_neigh_node->out_coding_list;
  612. /* Traverse list of nc_nodes to orig_node */
  613. rcu_read_lock();
  614. list_for_each_entry_rcu(nc_node, list, list) {
  615. if (!batadv_compare_eth(nc_node->addr, orig_node->orig))
  616. continue;
  617. if (!atomic_inc_not_zero(&nc_node->refcount))
  618. continue;
  619. /* Found a match */
  620. nc_node_out = nc_node;
  621. break;
  622. }
  623. rcu_read_unlock();
  624. return nc_node_out;
  625. }
  626. /**
  627. * batadv_nc_get_nc_node - retrieves an nc node or creates the entry if it was
  628. * not found
  629. * @bat_priv: the bat priv with all the soft interface information
  630. * @orig_node: orig node originating the ogm packet
  631. * @orig_neigh_node: neighboring orig node from which we received the ogm packet
  632. * (can be equal to orig_node)
  633. * @in_coding: traverse incoming or outgoing network coding list
  634. *
  635. * Returns the nc_node if found or created, NULL in case of an error.
  636. */
  637. static struct batadv_nc_node
  638. *batadv_nc_get_nc_node(struct batadv_priv *bat_priv,
  639. struct batadv_orig_node *orig_node,
  640. struct batadv_orig_node *orig_neigh_node,
  641. bool in_coding)
  642. {
  643. struct batadv_nc_node *nc_node;
  644. spinlock_t *lock; /* Used to lock list selected by "int in_coding" */
  645. struct list_head *list;
  646. /* Check if nc_node is already added */
  647. nc_node = batadv_nc_find_nc_node(orig_node, orig_neigh_node, in_coding);
  648. /* Node found */
  649. if (nc_node)
  650. return nc_node;
  651. nc_node = kzalloc(sizeof(*nc_node), GFP_ATOMIC);
  652. if (!nc_node)
  653. return NULL;
  654. if (!atomic_inc_not_zero(&orig_neigh_node->refcount))
  655. goto free;
  656. /* Initialize nc_node */
  657. INIT_LIST_HEAD(&nc_node->list);
  658. memcpy(nc_node->addr, orig_node->orig, ETH_ALEN);
  659. nc_node->orig_node = orig_neigh_node;
  660. atomic_set(&nc_node->refcount, 2);
  661. /* Select ingoing or outgoing coding node */
  662. if (in_coding) {
  663. lock = &orig_neigh_node->in_coding_list_lock;
  664. list = &orig_neigh_node->in_coding_list;
  665. } else {
  666. lock = &orig_neigh_node->out_coding_list_lock;
  667. list = &orig_neigh_node->out_coding_list;
  668. }
  669. batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_node %pM -> %pM\n",
  670. nc_node->addr, nc_node->orig_node->orig);
  671. /* Add nc_node to orig_node */
  672. spin_lock_bh(lock);
  673. list_add_tail_rcu(&nc_node->list, list);
  674. spin_unlock_bh(lock);
  675. return nc_node;
  676. free:
  677. kfree(nc_node);
  678. return NULL;
  679. }
  680. /**
  681. * batadv_nc_update_nc_node - updates stored incoming and outgoing nc node structs
  682. * (best called on incoming OGMs)
  683. * @bat_priv: the bat priv with all the soft interface information
  684. * @orig_node: orig node originating the ogm packet
  685. * @orig_neigh_node: neighboring orig node from which we received the ogm packet
  686. * (can be equal to orig_node)
  687. * @ogm_packet: incoming ogm packet
  688. * @is_single_hop_neigh: orig_node is a single hop neighbor
  689. */
  690. void batadv_nc_update_nc_node(struct batadv_priv *bat_priv,
  691. struct batadv_orig_node *orig_node,
  692. struct batadv_orig_node *orig_neigh_node,
  693. struct batadv_ogm_packet *ogm_packet,
  694. int is_single_hop_neigh)
  695. {
  696. struct batadv_nc_node *in_nc_node = NULL, *out_nc_node = NULL;
  697. /* Check if network coding is enabled */
  698. if (!atomic_read(&bat_priv->network_coding))
  699. goto out;
  700. /* accept ogms from 'good' neighbors and single hop neighbors */
  701. if (!batadv_can_nc_with_orig(bat_priv, orig_node, ogm_packet) &&
  702. !is_single_hop_neigh)
  703. goto out;
  704. /* Add orig_node as in_nc_node on hop */
  705. in_nc_node = batadv_nc_get_nc_node(bat_priv, orig_node,
  706. orig_neigh_node, true);
  707. if (!in_nc_node)
  708. goto out;
  709. in_nc_node->last_seen = jiffies;
  710. /* Add hop as out_nc_node on orig_node */
  711. out_nc_node = batadv_nc_get_nc_node(bat_priv, orig_neigh_node,
  712. orig_node, false);
  713. if (!out_nc_node)
  714. goto out;
  715. out_nc_node->last_seen = jiffies;
  716. out:
  717. if (in_nc_node)
  718. batadv_nc_node_free_ref(in_nc_node);
  719. if (out_nc_node)
  720. batadv_nc_node_free_ref(out_nc_node);
  721. }
  722. /**
  723. * batadv_nc_get_path - get existing nc_path or allocate a new one
  724. * @bat_priv: the bat priv with all the soft interface information
  725. * @hash: hash table containing the nc path
  726. * @src: ethernet source address - first half of the nc path search key
  727. * @dst: ethernet destination address - second half of the nc path search key
  728. *
  729. * Returns pointer to nc_path if the path was found or created, returns NULL
  730. * on error.
  731. */
  732. static struct batadv_nc_path *batadv_nc_get_path(struct batadv_priv *bat_priv,
  733. struct batadv_hashtable *hash,
  734. uint8_t *src,
  735. uint8_t *dst)
  736. {
  737. int hash_added;
  738. struct batadv_nc_path *nc_path, nc_path_key;
  739. batadv_nc_hash_key_gen(&nc_path_key, src, dst);
  740. /* Search for existing nc_path */
  741. nc_path = batadv_nc_hash_find(hash, (void *)&nc_path_key);
  742. if (nc_path) {
  743. /* Set timestamp to delay removal of nc_path */
  744. nc_path->last_valid = jiffies;
  745. return nc_path;
  746. }
  747. /* No existing nc_path was found; create a new */
  748. nc_path = kzalloc(sizeof(*nc_path), GFP_ATOMIC);
  749. if (!nc_path)
  750. return NULL;
  751. /* Initialize nc_path */
  752. INIT_LIST_HEAD(&nc_path->packet_list);
  753. spin_lock_init(&nc_path->packet_list_lock);
  754. atomic_set(&nc_path->refcount, 2);
  755. nc_path->last_valid = jiffies;
  756. memcpy(nc_path->next_hop, dst, ETH_ALEN);
  757. memcpy(nc_path->prev_hop, src, ETH_ALEN);
  758. batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_path %pM -> %pM\n",
  759. nc_path->prev_hop,
  760. nc_path->next_hop);
  761. /* Add nc_path to hash table */
  762. hash_added = batadv_hash_add(hash, batadv_nc_hash_compare,
  763. batadv_nc_hash_choose, &nc_path_key,
  764. &nc_path->hash_entry);
  765. if (hash_added < 0) {
  766. kfree(nc_path);
  767. return NULL;
  768. }
  769. return nc_path;
  770. }
  771. /**
  772. * batadv_nc_random_weight_tq - scale the receivers TQ-value to avoid unfair
  773. * selection of a receiver with slightly lower TQ than the other
  774. * @tq: to be weighted tq value
  775. */
  776. static uint8_t batadv_nc_random_weight_tq(uint8_t tq)
  777. {
  778. uint8_t rand_val, rand_tq;
  779. get_random_bytes(&rand_val, sizeof(rand_val));
  780. /* randomize the estimated packet loss (max TQ - estimated TQ) */
  781. rand_tq = rand_val * (BATADV_TQ_MAX_VALUE - tq);
  782. /* normalize the randomized packet loss */
  783. rand_tq /= BATADV_TQ_MAX_VALUE;
  784. /* convert to (randomized) estimated tq again */
  785. return BATADV_TQ_MAX_VALUE - rand_tq;
  786. }
  787. /**
  788. * batadv_nc_memxor - XOR destination with source
  789. * @dst: byte array to XOR into
  790. * @src: byte array to XOR from
  791. * @len: length of destination array
  792. */
  793. static void batadv_nc_memxor(char *dst, const char *src, unsigned int len)
  794. {
  795. unsigned int i;
  796. for (i = 0; i < len; ++i)
  797. dst[i] ^= src[i];
  798. }
  799. /**
  800. * batadv_nc_code_packets - code a received unicast_packet with an nc packet
  801. * into a coded_packet and send it
  802. * @bat_priv: the bat priv with all the soft interface information
  803. * @skb: data skb to forward
  804. * @ethhdr: pointer to the ethernet header inside the skb
  805. * @nc_packet: structure containing the packet to the skb can be coded with
  806. * @neigh_node: next hop to forward packet to
  807. *
  808. * Returns true if both packets are consumed, false otherwise.
  809. */
  810. static bool batadv_nc_code_packets(struct batadv_priv *bat_priv,
  811. struct sk_buff *skb,
  812. struct ethhdr *ethhdr,
  813. struct batadv_nc_packet *nc_packet,
  814. struct batadv_neigh_node *neigh_node)
  815. {
  816. uint8_t tq_weighted_neigh, tq_weighted_coding;
  817. struct sk_buff *skb_dest, *skb_src;
  818. struct batadv_unicast_packet *packet1;
  819. struct batadv_unicast_packet *packet2;
  820. struct batadv_coded_packet *coded_packet;
  821. struct batadv_neigh_node *neigh_tmp, *router_neigh;
  822. struct batadv_neigh_node *router_coding = NULL;
  823. uint8_t *first_source, *first_dest, *second_source, *second_dest;
  824. __be32 packet_id1, packet_id2;
  825. size_t count;
  826. bool res = false;
  827. int coding_len;
  828. int unicast_size = sizeof(*packet1);
  829. int coded_size = sizeof(*coded_packet);
  830. int header_add = coded_size - unicast_size;
  831. router_neigh = batadv_orig_node_get_router(neigh_node->orig_node);
  832. if (!router_neigh)
  833. goto out;
  834. neigh_tmp = nc_packet->neigh_node;
  835. router_coding = batadv_orig_node_get_router(neigh_tmp->orig_node);
  836. if (!router_coding)
  837. goto out;
  838. tq_weighted_neigh = batadv_nc_random_weight_tq(router_neigh->tq_avg);
  839. tq_weighted_coding = batadv_nc_random_weight_tq(router_coding->tq_avg);
  840. /* Select one destination for the MAC-header dst-field based on
  841. * weighted TQ-values.
  842. */
  843. if (tq_weighted_neigh >= tq_weighted_coding) {
  844. /* Destination from nc_packet is selected for MAC-header */
  845. first_dest = nc_packet->nc_path->next_hop;
  846. first_source = nc_packet->nc_path->prev_hop;
  847. second_dest = neigh_node->addr;
  848. second_source = ethhdr->h_source;
  849. packet1 = (struct batadv_unicast_packet *)nc_packet->skb->data;
  850. packet2 = (struct batadv_unicast_packet *)skb->data;
  851. packet_id1 = nc_packet->packet_id;
  852. packet_id2 = batadv_skb_crc32(skb,
  853. skb->data + sizeof(*packet2));
  854. } else {
  855. /* Destination for skb is selected for MAC-header */
  856. first_dest = neigh_node->addr;
  857. first_source = ethhdr->h_source;
  858. second_dest = nc_packet->nc_path->next_hop;
  859. second_source = nc_packet->nc_path->prev_hop;
  860. packet1 = (struct batadv_unicast_packet *)skb->data;
  861. packet2 = (struct batadv_unicast_packet *)nc_packet->skb->data;
  862. packet_id1 = batadv_skb_crc32(skb,
  863. skb->data + sizeof(*packet1));
  864. packet_id2 = nc_packet->packet_id;
  865. }
  866. /* Instead of zero padding the smallest data buffer, we
  867. * code into the largest.
  868. */
  869. if (skb->len <= nc_packet->skb->len) {
  870. skb_dest = nc_packet->skb;
  871. skb_src = skb;
  872. } else {
  873. skb_dest = skb;
  874. skb_src = nc_packet->skb;
  875. }
  876. /* coding_len is used when decoding the packet shorter packet */
  877. coding_len = skb_src->len - unicast_size;
  878. if (skb_linearize(skb_dest) < 0 || skb_linearize(skb_src) < 0)
  879. goto out;
  880. skb_push(skb_dest, header_add);
  881. coded_packet = (struct batadv_coded_packet *)skb_dest->data;
  882. skb_reset_mac_header(skb_dest);
  883. coded_packet->header.packet_type = BATADV_CODED;
  884. coded_packet->header.version = BATADV_COMPAT_VERSION;
  885. coded_packet->header.ttl = packet1->header.ttl;
  886. /* Info about first unicast packet */
  887. memcpy(coded_packet->first_source, first_source, ETH_ALEN);
  888. memcpy(coded_packet->first_orig_dest, packet1->dest, ETH_ALEN);
  889. coded_packet->first_crc = packet_id1;
  890. coded_packet->first_ttvn = packet1->ttvn;
  891. /* Info about second unicast packet */
  892. memcpy(coded_packet->second_dest, second_dest, ETH_ALEN);
  893. memcpy(coded_packet->second_source, second_source, ETH_ALEN);
  894. memcpy(coded_packet->second_orig_dest, packet2->dest, ETH_ALEN);
  895. coded_packet->second_crc = packet_id2;
  896. coded_packet->second_ttl = packet2->header.ttl;
  897. coded_packet->second_ttvn = packet2->ttvn;
  898. coded_packet->coded_len = htons(coding_len);
  899. /* This is where the magic happens: Code skb_src into skb_dest */
  900. batadv_nc_memxor(skb_dest->data + coded_size,
  901. skb_src->data + unicast_size, coding_len);
  902. /* Update counters accordingly */
  903. if (BATADV_SKB_CB(skb_src)->decoded &&
  904. BATADV_SKB_CB(skb_dest)->decoded) {
  905. /* Both packets are recoded */
  906. count = skb_src->len + ETH_HLEN;
  907. count += skb_dest->len + ETH_HLEN;
  908. batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE, 2);
  909. batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES, count);
  910. } else if (!BATADV_SKB_CB(skb_src)->decoded &&
  911. !BATADV_SKB_CB(skb_dest)->decoded) {
  912. /* Both packets are newly coded */
  913. count = skb_src->len + ETH_HLEN;
  914. count += skb_dest->len + ETH_HLEN;
  915. batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE, 2);
  916. batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES, count);
  917. } else if (BATADV_SKB_CB(skb_src)->decoded &&
  918. !BATADV_SKB_CB(skb_dest)->decoded) {
  919. /* skb_src recoded and skb_dest is newly coded */
  920. batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE);
  921. batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES,
  922. skb_src->len + ETH_HLEN);
  923. batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE);
  924. batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES,
  925. skb_dest->len + ETH_HLEN);
  926. } else if (!BATADV_SKB_CB(skb_src)->decoded &&
  927. BATADV_SKB_CB(skb_dest)->decoded) {
  928. /* skb_src is newly coded and skb_dest is recoded */
  929. batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE);
  930. batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES,
  931. skb_src->len + ETH_HLEN);
  932. batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE);
  933. batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES,
  934. skb_dest->len + ETH_HLEN);
  935. }
  936. /* skb_src is now coded into skb_dest, so free it */
  937. kfree_skb(skb_src);
  938. /* avoid duplicate free of skb from nc_packet */
  939. nc_packet->skb = NULL;
  940. batadv_nc_packet_free(nc_packet);
  941. /* Send the coded packet and return true */
  942. batadv_send_skb_packet(skb_dest, neigh_node->if_incoming, first_dest);
  943. res = true;
  944. out:
  945. if (router_neigh)
  946. batadv_neigh_node_free_ref(router_neigh);
  947. if (router_coding)
  948. batadv_neigh_node_free_ref(router_coding);
  949. return res;
  950. }
  951. /**
  952. * batadv_nc_skb_coding_possible - true if a decoded skb is available at dst.
  953. * @skb: data skb to forward
  954. * @dst: destination mac address of the other skb to code with
  955. * @src: source mac address of skb
  956. *
  957. * Whenever we network code a packet we have to check whether we received it in
  958. * a network coded form. If so, we may not be able to use it for coding because
  959. * some neighbors may also have received (overheard) the packet in the network
  960. * coded form without being able to decode it. It is hard to know which of the
  961. * neighboring nodes was able to decode the packet, therefore we can only
  962. * re-code the packet if the source of the previous encoded packet is involved.
  963. * Since the source encoded the packet we can be certain it has all necessary
  964. * decode information.
  965. *
  966. * Returns true if coding of a decoded packet is allowed.
  967. */
  968. static bool batadv_nc_skb_coding_possible(struct sk_buff *skb,
  969. uint8_t *dst, uint8_t *src)
  970. {
  971. if (BATADV_SKB_CB(skb)->decoded && !batadv_compare_eth(dst, src))
  972. return false;
  973. else
  974. return true;
  975. }
  976. /**
  977. * batadv_nc_path_search - Find the coding path matching in_nc_node and
  978. * out_nc_node to retrieve a buffered packet that can be used for coding.
  979. * @bat_priv: the bat priv with all the soft interface information
  980. * @in_nc_node: pointer to skb next hop's neighbor nc node
  981. * @out_nc_node: pointer to skb source's neighbor nc node
  982. * @skb: data skb to forward
  983. * @eth_dst: next hop mac address of skb
  984. *
  985. * Returns true if coding of a decoded skb is allowed.
  986. */
  987. static struct batadv_nc_packet *
  988. batadv_nc_path_search(struct batadv_priv *bat_priv,
  989. struct batadv_nc_node *in_nc_node,
  990. struct batadv_nc_node *out_nc_node,
  991. struct sk_buff *skb,
  992. uint8_t *eth_dst)
  993. {
  994. struct batadv_nc_path *nc_path, nc_path_key;
  995. struct batadv_nc_packet *nc_packet_out = NULL;
  996. struct batadv_nc_packet *nc_packet, *nc_packet_tmp;
  997. struct batadv_hashtable *hash = bat_priv->nc.coding_hash;
  998. int idx;
  999. if (!hash)
  1000. return NULL;
  1001. /* Create almost path key */
  1002. batadv_nc_hash_key_gen(&nc_path_key, in_nc_node->addr,
  1003. out_nc_node->addr);
  1004. idx = batadv_nc_hash_choose(&nc_path_key, hash->size);
  1005. /* Check for coding opportunities in this nc_path */
  1006. rcu_read_lock();
  1007. hlist_for_each_entry_rcu(nc_path, &hash->table[idx], hash_entry) {
  1008. if (!batadv_compare_eth(nc_path->prev_hop, in_nc_node->addr))
  1009. continue;
  1010. if (!batadv_compare_eth(nc_path->next_hop, out_nc_node->addr))
  1011. continue;
  1012. spin_lock_bh(&nc_path->packet_list_lock);
  1013. if (list_empty(&nc_path->packet_list)) {
  1014. spin_unlock_bh(&nc_path->packet_list_lock);
  1015. continue;
  1016. }
  1017. list_for_each_entry_safe(nc_packet, nc_packet_tmp,
  1018. &nc_path->packet_list, list) {
  1019. if (!batadv_nc_skb_coding_possible(nc_packet->skb,
  1020. eth_dst,
  1021. in_nc_node->addr))
  1022. continue;
  1023. /* Coding opportunity is found! */
  1024. list_del(&nc_packet->list);
  1025. nc_packet_out = nc_packet;
  1026. break;
  1027. }
  1028. spin_unlock_bh(&nc_path->packet_list_lock);
  1029. break;
  1030. }
  1031. rcu_read_unlock();
  1032. return nc_packet_out;
  1033. }
  1034. /**
  1035. * batadv_nc_skb_src_search - Loops through the list of neighoring nodes of the
  1036. * skb's sender (may be equal to the originator).
  1037. * @bat_priv: the bat priv with all the soft interface information
  1038. * @skb: data skb to forward
  1039. * @eth_dst: next hop mac address of skb
  1040. * @eth_src: source mac address of skb
  1041. * @in_nc_node: pointer to skb next hop's neighbor nc node
  1042. *
  1043. * Returns an nc packet if a suitable coding packet was found, NULL otherwise.
  1044. */
  1045. static struct batadv_nc_packet *
  1046. batadv_nc_skb_src_search(struct batadv_priv *bat_priv,
  1047. struct sk_buff *skb,
  1048. uint8_t *eth_dst,
  1049. uint8_t *eth_src,
  1050. struct batadv_nc_node *in_nc_node)
  1051. {
  1052. struct batadv_orig_node *orig_node;
  1053. struct batadv_nc_node *out_nc_node;
  1054. struct batadv_nc_packet *nc_packet = NULL;
  1055. orig_node = batadv_orig_hash_find(bat_priv, eth_src);
  1056. if (!orig_node)
  1057. return NULL;
  1058. rcu_read_lock();
  1059. list_for_each_entry_rcu(out_nc_node,
  1060. &orig_node->out_coding_list, list) {
  1061. /* Check if the skb is decoded and if recoding is possible */
  1062. if (!batadv_nc_skb_coding_possible(skb,
  1063. out_nc_node->addr, eth_src))
  1064. continue;
  1065. /* Search for an opportunity in this nc_path */
  1066. nc_packet = batadv_nc_path_search(bat_priv, in_nc_node,
  1067. out_nc_node, skb, eth_dst);
  1068. if (nc_packet)
  1069. break;
  1070. }
  1071. rcu_read_unlock();
  1072. batadv_orig_node_free_ref(orig_node);
  1073. return nc_packet;
  1074. }
  1075. /**
  1076. * batadv_nc_skb_store_before_coding - set the ethernet src and dst of the
  1077. * unicast skb before it is stored for use in later decoding
  1078. * @bat_priv: the bat priv with all the soft interface information
  1079. * @skb: data skb to store
  1080. * @eth_dst_new: new destination mac address of skb
  1081. */
  1082. static void batadv_nc_skb_store_before_coding(struct batadv_priv *bat_priv,
  1083. struct sk_buff *skb,
  1084. uint8_t *eth_dst_new)
  1085. {
  1086. struct ethhdr *ethhdr;
  1087. /* Copy skb header to change the mac header */
  1088. skb = pskb_copy(skb, GFP_ATOMIC);
  1089. if (!skb)
  1090. return;
  1091. /* Set the mac header as if we actually sent the packet uncoded */
  1092. ethhdr = eth_hdr(skb);
  1093. memcpy(ethhdr->h_source, ethhdr->h_dest, ETH_ALEN);
  1094. memcpy(ethhdr->h_dest, eth_dst_new, ETH_ALEN);
  1095. /* Set data pointer to MAC header to mimic packets from our tx path */
  1096. skb_push(skb, ETH_HLEN);
  1097. /* Add the packet to the decoding packet pool */
  1098. batadv_nc_skb_store_for_decoding(bat_priv, skb);
  1099. /* batadv_nc_skb_store_for_decoding() clones the skb, so we must free
  1100. * our ref
  1101. */
  1102. kfree_skb(skb);
  1103. }
  1104. /**
  1105. * batadv_nc_skb_dst_search - Loops through list of neighboring nodes to dst.
  1106. * @skb: data skb to forward
  1107. * @neigh_node: next hop to forward packet to
  1108. * @ethhdr: pointer to the ethernet header inside the skb
  1109. *
  1110. * Loops through list of neighboring nodes the next hop has a good connection to
  1111. * (receives OGMs with a sufficient quality). We need to find a neighbor of our
  1112. * next hop that potentially sent a packet which our next hop also received
  1113. * (overheard) and has stored for later decoding.
  1114. *
  1115. * Returns true if the skb was consumed (encoded packet sent) or false otherwise
  1116. */
  1117. static bool batadv_nc_skb_dst_search(struct sk_buff *skb,
  1118. struct batadv_neigh_node *neigh_node,
  1119. struct ethhdr *ethhdr)
  1120. {
  1121. struct net_device *netdev = neigh_node->if_incoming->soft_iface;
  1122. struct batadv_priv *bat_priv = netdev_priv(netdev);
  1123. struct batadv_orig_node *orig_node = neigh_node->orig_node;
  1124. struct batadv_nc_node *nc_node;
  1125. struct batadv_nc_packet *nc_packet = NULL;
  1126. rcu_read_lock();
  1127. list_for_each_entry_rcu(nc_node, &orig_node->in_coding_list, list) {
  1128. /* Search for coding opportunity with this in_nc_node */
  1129. nc_packet = batadv_nc_skb_src_search(bat_priv, skb,
  1130. neigh_node->addr,
  1131. ethhdr->h_source, nc_node);
  1132. /* Opportunity was found, so stop searching */
  1133. if (nc_packet)
  1134. break;
  1135. }
  1136. rcu_read_unlock();
  1137. if (!nc_packet)
  1138. return false;
  1139. /* Save packets for later decoding */
  1140. batadv_nc_skb_store_before_coding(bat_priv, skb,
  1141. neigh_node->addr);
  1142. batadv_nc_skb_store_before_coding(bat_priv, nc_packet->skb,
  1143. nc_packet->neigh_node->addr);
  1144. /* Code and send packets */
  1145. if (batadv_nc_code_packets(bat_priv, skb, ethhdr, nc_packet,
  1146. neigh_node))
  1147. return true;
  1148. /* out of mem ? Coding failed - we have to free the buffered packet
  1149. * to avoid memleaks. The skb passed as argument will be dealt with
  1150. * by the calling function.
  1151. */
  1152. batadv_nc_send_packet(nc_packet);
  1153. return false;
  1154. }
  1155. /**
  1156. * batadv_nc_skb_add_to_path - buffer skb for later encoding / decoding
  1157. * @skb: skb to add to path
  1158. * @nc_path: path to add skb to
  1159. * @neigh_node: next hop to forward packet to
  1160. * @packet_id: checksum to identify packet
  1161. *
  1162. * Returns true if the packet was buffered or false in case of an error.
  1163. */
  1164. static bool batadv_nc_skb_add_to_path(struct sk_buff *skb,
  1165. struct batadv_nc_path *nc_path,
  1166. struct batadv_neigh_node *neigh_node,
  1167. __be32 packet_id)
  1168. {
  1169. struct batadv_nc_packet *nc_packet;
  1170. nc_packet = kzalloc(sizeof(*nc_packet), GFP_ATOMIC);
  1171. if (!nc_packet)
  1172. return false;
  1173. /* Initialize nc_packet */
  1174. nc_packet->timestamp = jiffies;
  1175. nc_packet->packet_id = packet_id;
  1176. nc_packet->skb = skb;
  1177. nc_packet->neigh_node = neigh_node;
  1178. nc_packet->nc_path = nc_path;
  1179. /* Add coding packet to list */
  1180. spin_lock_bh(&nc_path->packet_list_lock);
  1181. list_add_tail(&nc_packet->list, &nc_path->packet_list);
  1182. spin_unlock_bh(&nc_path->packet_list_lock);
  1183. return true;
  1184. }
  1185. /**
  1186. * batadv_nc_skb_forward - try to code a packet or add it to the coding packet
  1187. * buffer
  1188. * @skb: data skb to forward
  1189. * @neigh_node: next hop to forward packet to
  1190. *
  1191. * Returns true if the skb was consumed (encoded packet sent) or false otherwise
  1192. */
  1193. bool batadv_nc_skb_forward(struct sk_buff *skb,
  1194. struct batadv_neigh_node *neigh_node)
  1195. {
  1196. const struct net_device *netdev = neigh_node->if_incoming->soft_iface;
  1197. struct batadv_priv *bat_priv = netdev_priv(netdev);
  1198. struct batadv_unicast_packet *packet;
  1199. struct batadv_nc_path *nc_path;
  1200. struct ethhdr *ethhdr = eth_hdr(skb);
  1201. __be32 packet_id;
  1202. u8 *payload;
  1203. /* Check if network coding is enabled */
  1204. if (!atomic_read(&bat_priv->network_coding))
  1205. goto out;
  1206. /* We only handle unicast packets */
  1207. payload = skb_network_header(skb);
  1208. packet = (struct batadv_unicast_packet *)payload;
  1209. if (packet->header.packet_type != BATADV_UNICAST)
  1210. goto out;
  1211. /* Try to find a coding opportunity and send the skb if one is found */
  1212. if (batadv_nc_skb_dst_search(skb, neigh_node, ethhdr))
  1213. return true;
  1214. /* Find or create a nc_path for this src-dst pair */
  1215. nc_path = batadv_nc_get_path(bat_priv,
  1216. bat_priv->nc.coding_hash,
  1217. ethhdr->h_source,
  1218. neigh_node->addr);
  1219. if (!nc_path)
  1220. goto out;
  1221. /* Add skb to nc_path */
  1222. packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet));
  1223. if (!batadv_nc_skb_add_to_path(skb, nc_path, neigh_node, packet_id))
  1224. goto free_nc_path;
  1225. /* Packet is consumed */
  1226. return true;
  1227. free_nc_path:
  1228. batadv_nc_path_free_ref(nc_path);
  1229. out:
  1230. /* Packet is not consumed */
  1231. return false;
  1232. }
  1233. /**
  1234. * batadv_nc_skb_store_for_decoding - save a clone of the skb which can be used
  1235. * when decoding coded packets
  1236. * @bat_priv: the bat priv with all the soft interface information
  1237. * @skb: data skb to store
  1238. */
  1239. void batadv_nc_skb_store_for_decoding(struct batadv_priv *bat_priv,
  1240. struct sk_buff *skb)
  1241. {
  1242. struct batadv_unicast_packet *packet;
  1243. struct batadv_nc_path *nc_path;
  1244. struct ethhdr *ethhdr = eth_hdr(skb);
  1245. __be32 packet_id;
  1246. u8 *payload;
  1247. /* Check if network coding is enabled */
  1248. if (!atomic_read(&bat_priv->network_coding))
  1249. goto out;
  1250. /* Check for supported packet type */
  1251. payload = skb_network_header(skb);
  1252. packet = (struct batadv_unicast_packet *)payload;
  1253. if (packet->header.packet_type != BATADV_UNICAST)
  1254. goto out;
  1255. /* Find existing nc_path or create a new */
  1256. nc_path = batadv_nc_get_path(bat_priv,
  1257. bat_priv->nc.decoding_hash,
  1258. ethhdr->h_source,
  1259. ethhdr->h_dest);
  1260. if (!nc_path)
  1261. goto out;
  1262. /* Clone skb and adjust skb->data to point at batman header */
  1263. skb = skb_clone(skb, GFP_ATOMIC);
  1264. if (unlikely(!skb))
  1265. goto free_nc_path;
  1266. if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
  1267. goto free_skb;
  1268. if (unlikely(!skb_pull_rcsum(skb, ETH_HLEN)))
  1269. goto free_skb;
  1270. /* Add skb to nc_path */
  1271. packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet));
  1272. if (!batadv_nc_skb_add_to_path(skb, nc_path, NULL, packet_id))
  1273. goto free_skb;
  1274. batadv_inc_counter(bat_priv, BATADV_CNT_NC_BUFFER);
  1275. return;
  1276. free_skb:
  1277. kfree_skb(skb);
  1278. free_nc_path:
  1279. batadv_nc_path_free_ref(nc_path);
  1280. out:
  1281. return;
  1282. }
  1283. /**
  1284. * batadv_nc_skb_store_sniffed_unicast - check if a received unicast packet
  1285. * should be saved in the decoding buffer and, if so, store it there
  1286. * @bat_priv: the bat priv with all the soft interface information
  1287. * @skb: unicast skb to store
  1288. */
  1289. void batadv_nc_skb_store_sniffed_unicast(struct batadv_priv *bat_priv,
  1290. struct sk_buff *skb)
  1291. {
  1292. struct ethhdr *ethhdr = eth_hdr(skb);
  1293. if (batadv_is_my_mac(bat_priv, ethhdr->h_dest))
  1294. return;
  1295. /* Set data pointer to MAC header to mimic packets from our tx path */
  1296. skb_push(skb, ETH_HLEN);
  1297. batadv_nc_skb_store_for_decoding(bat_priv, skb);
  1298. }
  1299. /**
  1300. * batadv_nc_skb_decode_packet - decode given skb using the decode data stored
  1301. * in nc_packet
  1302. * @bat_priv: the bat priv with all the soft interface information
  1303. * @skb: unicast skb to decode
  1304. * @nc_packet: decode data needed to decode the skb
  1305. *
  1306. * Returns pointer to decoded unicast packet if the packet was decoded or NULL
  1307. * in case of an error.
  1308. */
  1309. static struct batadv_unicast_packet *
  1310. batadv_nc_skb_decode_packet(struct batadv_priv *bat_priv, struct sk_buff *skb,
  1311. struct batadv_nc_packet *nc_packet)
  1312. {
  1313. const int h_size = sizeof(struct batadv_unicast_packet);
  1314. const int h_diff = sizeof(struct batadv_coded_packet) - h_size;
  1315. struct batadv_unicast_packet *unicast_packet;
  1316. struct batadv_coded_packet coded_packet_tmp;
  1317. struct ethhdr *ethhdr, ethhdr_tmp;
  1318. uint8_t *orig_dest, ttl, ttvn;
  1319. unsigned int coding_len;
  1320. int err;
  1321. /* Save headers temporarily */
  1322. memcpy(&coded_packet_tmp, skb->data, sizeof(coded_packet_tmp));
  1323. memcpy(&ethhdr_tmp, skb_mac_header(skb), sizeof(ethhdr_tmp));
  1324. if (skb_cow(skb, 0) < 0)
  1325. return NULL;
  1326. if (unlikely(!skb_pull_rcsum(skb, h_diff)))
  1327. return NULL;
  1328. /* Data points to batman header, so set mac header 14 bytes before
  1329. * and network to data
  1330. */
  1331. skb_set_mac_header(skb, -ETH_HLEN);
  1332. skb_reset_network_header(skb);
  1333. /* Reconstruct original mac header */
  1334. ethhdr = eth_hdr(skb);
  1335. memcpy(ethhdr, &ethhdr_tmp, sizeof(*ethhdr));
  1336. /* Select the correct unicast header information based on the location
  1337. * of our mac address in the coded_packet header
  1338. */
  1339. if (batadv_is_my_mac(bat_priv, coded_packet_tmp.second_dest)) {
  1340. /* If we are the second destination the packet was overheard,
  1341. * so the Ethernet address must be copied to h_dest and
  1342. * pkt_type changed from PACKET_OTHERHOST to PACKET_HOST
  1343. */
  1344. memcpy(ethhdr->h_dest, coded_packet_tmp.second_dest, ETH_ALEN);
  1345. skb->pkt_type = PACKET_HOST;
  1346. orig_dest = coded_packet_tmp.second_orig_dest;
  1347. ttl = coded_packet_tmp.second_ttl;
  1348. ttvn = coded_packet_tmp.second_ttvn;
  1349. } else {
  1350. orig_dest = coded_packet_tmp.first_orig_dest;
  1351. ttl = coded_packet_tmp.header.ttl;
  1352. ttvn = coded_packet_tmp.first_ttvn;
  1353. }
  1354. coding_len = ntohs(coded_packet_tmp.coded_len);
  1355. if (coding_len > skb->len)
  1356. return NULL;
  1357. /* Here the magic is reversed:
  1358. * extract the missing packet from the received coded packet
  1359. */
  1360. batadv_nc_memxor(skb->data + h_size,
  1361. nc_packet->skb->data + h_size,
  1362. coding_len);
  1363. /* Resize decoded skb if decoded with larger packet */
  1364. if (nc_packet->skb->len > coding_len + h_size) {
  1365. err = pskb_trim_rcsum(skb, coding_len + h_size);
  1366. if (err)
  1367. return NULL;
  1368. }
  1369. /* Create decoded unicast packet */
  1370. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  1371. unicast_packet->header.packet_type = BATADV_UNICAST;
  1372. unicast_packet->header.version = BATADV_COMPAT_VERSION;
  1373. unicast_packet->header.ttl = ttl;
  1374. memcpy(unicast_packet->dest, orig_dest, ETH_ALEN);
  1375. unicast_packet->ttvn = ttvn;
  1376. batadv_nc_packet_free(nc_packet);
  1377. return unicast_packet;
  1378. }
  1379. /**
  1380. * batadv_nc_find_decoding_packet - search through buffered decoding data to
  1381. * find the data needed to decode the coded packet
  1382. * @bat_priv: the bat priv with all the soft interface information
  1383. * @ethhdr: pointer to the ethernet header inside the coded packet
  1384. * @coded: coded packet we try to find decode data for
  1385. *
  1386. * Returns pointer to nc packet if the needed data was found or NULL otherwise.
  1387. */
  1388. static struct batadv_nc_packet *
  1389. batadv_nc_find_decoding_packet(struct batadv_priv *bat_priv,
  1390. struct ethhdr *ethhdr,
  1391. struct batadv_coded_packet *coded)
  1392. {
  1393. struct batadv_hashtable *hash = bat_priv->nc.decoding_hash;
  1394. struct batadv_nc_packet *tmp_nc_packet, *nc_packet = NULL;
  1395. struct batadv_nc_path *nc_path, nc_path_key;
  1396. uint8_t *dest, *source;
  1397. __be32 packet_id;
  1398. int index;
  1399. if (!hash)
  1400. return NULL;
  1401. /* Select the correct packet id based on the location of our mac-addr */
  1402. dest = ethhdr->h_source;
  1403. if (!batadv_is_my_mac(bat_priv, coded->second_dest)) {
  1404. source = coded->second_source;
  1405. packet_id = coded->second_crc;
  1406. } else {
  1407. source = coded->first_source;
  1408. packet_id = coded->first_crc;
  1409. }
  1410. batadv_nc_hash_key_gen(&nc_path_key, source, dest);
  1411. index = batadv_nc_hash_choose(&nc_path_key, hash->size);
  1412. /* Search for matching coding path */
  1413. rcu_read_lock();
  1414. hlist_for_each_entry_rcu(nc_path, &hash->table[index], hash_entry) {
  1415. /* Find matching nc_packet */
  1416. spin_lock_bh(&nc_path->packet_list_lock);
  1417. list_for_each_entry(tmp_nc_packet,
  1418. &nc_path->packet_list, list) {
  1419. if (packet_id == tmp_nc_packet->packet_id) {
  1420. list_del(&tmp_nc_packet->list);
  1421. nc_packet = tmp_nc_packet;
  1422. break;
  1423. }
  1424. }
  1425. spin_unlock_bh(&nc_path->packet_list_lock);
  1426. if (nc_packet)
  1427. break;
  1428. }
  1429. rcu_read_unlock();
  1430. if (!nc_packet)
  1431. batadv_dbg(BATADV_DBG_NC, bat_priv,
  1432. "No decoding packet found for %u\n", packet_id);
  1433. return nc_packet;
  1434. }
  1435. /**
  1436. * batadv_nc_recv_coded_packet - try to decode coded packet and enqueue the
  1437. * resulting unicast packet
  1438. * @skb: incoming coded packet
  1439. * @recv_if: pointer to interface this packet was received on
  1440. */
  1441. static int batadv_nc_recv_coded_packet(struct sk_buff *skb,
  1442. struct batadv_hard_iface *recv_if)
  1443. {
  1444. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  1445. struct batadv_unicast_packet *unicast_packet;
  1446. struct batadv_coded_packet *coded_packet;
  1447. struct batadv_nc_packet *nc_packet;
  1448. struct ethhdr *ethhdr;
  1449. int hdr_size = sizeof(*coded_packet);
  1450. /* Check if network coding is enabled */
  1451. if (!atomic_read(&bat_priv->network_coding))
  1452. return NET_RX_DROP;
  1453. /* Make sure we can access (and remove) header */
  1454. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  1455. return NET_RX_DROP;
  1456. coded_packet = (struct batadv_coded_packet *)skb->data;
  1457. ethhdr = eth_hdr(skb);
  1458. /* Verify frame is destined for us */
  1459. if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest) &&
  1460. !batadv_is_my_mac(bat_priv, coded_packet->second_dest))
  1461. return NET_RX_DROP;
  1462. /* Update stat counter */
  1463. if (batadv_is_my_mac(bat_priv, coded_packet->second_dest))
  1464. batadv_inc_counter(bat_priv, BATADV_CNT_NC_SNIFFED);
  1465. nc_packet = batadv_nc_find_decoding_packet(bat_priv, ethhdr,
  1466. coded_packet);
  1467. if (!nc_packet) {
  1468. batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE_FAILED);
  1469. return NET_RX_DROP;
  1470. }
  1471. /* Make skb's linear, because decoding accesses the entire buffer */
  1472. if (skb_linearize(skb) < 0)
  1473. goto free_nc_packet;
  1474. if (skb_linearize(nc_packet->skb) < 0)
  1475. goto free_nc_packet;
  1476. /* Decode the packet */
  1477. unicast_packet = batadv_nc_skb_decode_packet(bat_priv, skb, nc_packet);
  1478. if (!unicast_packet) {
  1479. batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE_FAILED);
  1480. goto free_nc_packet;
  1481. }
  1482. /* Mark packet as decoded to do correct recoding when forwarding */
  1483. BATADV_SKB_CB(skb)->decoded = true;
  1484. batadv_inc_counter(bat_priv, BATADV_CNT_NC_DECODE);
  1485. batadv_add_counter(bat_priv, BATADV_CNT_NC_DECODE_BYTES,
  1486. skb->len + ETH_HLEN);
  1487. return batadv_recv_unicast_packet(skb, recv_if);
  1488. free_nc_packet:
  1489. batadv_nc_packet_free(nc_packet);
  1490. return NET_RX_DROP;
  1491. }
  1492. /**
  1493. * batadv_nc_mesh_free - clean up network coding memory
  1494. * @bat_priv: the bat priv with all the soft interface information
  1495. */
  1496. void batadv_nc_mesh_free(struct batadv_priv *bat_priv)
  1497. {
  1498. cancel_delayed_work_sync(&bat_priv->nc.work);
  1499. batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash, NULL);
  1500. batadv_hash_destroy(bat_priv->nc.coding_hash);
  1501. batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash, NULL);
  1502. batadv_hash_destroy(bat_priv->nc.decoding_hash);
  1503. }
  1504. /**
  1505. * batadv_nc_nodes_seq_print_text - print the nc node information
  1506. * @seq: seq file to print on
  1507. * @offset: not used
  1508. */
  1509. int batadv_nc_nodes_seq_print_text(struct seq_file *seq, void *offset)
  1510. {
  1511. struct net_device *net_dev = (struct net_device *)seq->private;
  1512. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  1513. struct batadv_hashtable *hash = bat_priv->orig_hash;
  1514. struct batadv_hard_iface *primary_if;
  1515. struct hlist_head *head;
  1516. struct batadv_orig_node *orig_node;
  1517. struct batadv_nc_node *nc_node;
  1518. int i;
  1519. primary_if = batadv_seq_print_text_primary_if_get(seq);
  1520. if (!primary_if)
  1521. goto out;
  1522. /* Traverse list of originators */
  1523. for (i = 0; i < hash->size; i++) {
  1524. head = &hash->table[i];
  1525. /* For each orig_node in this bin */
  1526. rcu_read_lock();
  1527. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  1528. /* no need to print the orig node if it does not have
  1529. * network coding neighbors
  1530. */
  1531. if (list_empty(&orig_node->in_coding_list) &&
  1532. list_empty(&orig_node->out_coding_list))
  1533. continue;
  1534. seq_printf(seq, "Node: %pM\n", orig_node->orig);
  1535. seq_puts(seq, " Ingoing: ");
  1536. /* For each in_nc_node to this orig_node */
  1537. list_for_each_entry_rcu(nc_node,
  1538. &orig_node->in_coding_list,
  1539. list)
  1540. seq_printf(seq, "%pM ",
  1541. nc_node->addr);
  1542. seq_puts(seq, "\n");
  1543. seq_puts(seq, " Outgoing: ");
  1544. /* For out_nc_node to this orig_node */
  1545. list_for_each_entry_rcu(nc_node,
  1546. &orig_node->out_coding_list,
  1547. list)
  1548. seq_printf(seq, "%pM ",
  1549. nc_node->addr);
  1550. seq_puts(seq, "\n\n");
  1551. }
  1552. rcu_read_unlock();
  1553. }
  1554. out:
  1555. if (primary_if)
  1556. batadv_hardif_free_ref(primary_if);
  1557. return 0;
  1558. }
  1559. /**
  1560. * batadv_nc_init_debugfs - create nc folder and related files in debugfs
  1561. * @bat_priv: the bat priv with all the soft interface information
  1562. */
  1563. int batadv_nc_init_debugfs(struct batadv_priv *bat_priv)
  1564. {
  1565. struct dentry *nc_dir, *file;
  1566. nc_dir = debugfs_create_dir("nc", bat_priv->debug_dir);
  1567. if (!nc_dir)
  1568. goto out;
  1569. file = debugfs_create_u8("min_tq", S_IRUGO | S_IWUSR, nc_dir,
  1570. &bat_priv->nc.min_tq);
  1571. if (!file)
  1572. goto out;
  1573. file = debugfs_create_u32("max_fwd_delay", S_IRUGO | S_IWUSR, nc_dir,
  1574. &bat_priv->nc.max_fwd_delay);
  1575. if (!file)
  1576. goto out;
  1577. file = debugfs_create_u32("max_buffer_time", S_IRUGO | S_IWUSR, nc_dir,
  1578. &bat_priv->nc.max_buffer_time);
  1579. if (!file)
  1580. goto out;
  1581. return 0;
  1582. out:
  1583. return -ENOMEM;
  1584. }