network-coding.c 57 KB

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