network-coding.c 57 KB

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