network-coding.c 54 KB

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