network-coding.c 58 KB

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