network-coding.c 58 KB

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