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/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_release - release nc_node from lists and queue for free after
  180. * rcu grace period
  181. * @nc_node: the nc node to free
  182. */
  183. static void batadv_nc_node_release(struct batadv_nc_node *nc_node)
  184. {
  185. batadv_orig_node_free_ref(nc_node->orig_node);
  186. kfree_rcu(nc_node, rcu);
  187. }
  188. /**
  189. * batadv_nc_node_free_ref - decrement the nc node refcounter and possibly
  190. * release it
  191. * @nc_node: the nc node to free
  192. */
  193. static void batadv_nc_node_free_ref(struct batadv_nc_node *nc_node)
  194. {
  195. if (atomic_dec_and_test(&nc_node->refcount))
  196. batadv_nc_node_release(nc_node);
  197. }
  198. /**
  199. * batadv_nc_path_free_ref - decrements the nc path refcounter and possibly
  200. * frees it
  201. * @nc_path: the nc node to free
  202. */
  203. static void batadv_nc_path_free_ref(struct batadv_nc_path *nc_path)
  204. {
  205. if (atomic_dec_and_test(&nc_path->refcount))
  206. kfree_rcu(nc_path, rcu);
  207. }
  208. /**
  209. * batadv_nc_packet_free - frees nc packet
  210. * @nc_packet: the nc packet to free
  211. */
  212. static void batadv_nc_packet_free(struct batadv_nc_packet *nc_packet)
  213. {
  214. if (nc_packet->skb)
  215. kfree_skb(nc_packet->skb);
  216. batadv_nc_path_free_ref(nc_packet->nc_path);
  217. kfree(nc_packet);
  218. }
  219. /**
  220. * batadv_nc_to_purge_nc_node - checks whether an nc node has to be purged
  221. * @bat_priv: the bat priv with all the soft interface information
  222. * @nc_node: the nc node to check
  223. *
  224. * Returns true if the entry has to be purged now, false otherwise
  225. */
  226. static bool batadv_nc_to_purge_nc_node(struct batadv_priv *bat_priv,
  227. struct batadv_nc_node *nc_node)
  228. {
  229. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  230. return true;
  231. return batadv_has_timed_out(nc_node->last_seen, BATADV_NC_NODE_TIMEOUT);
  232. }
  233. /**
  234. * batadv_nc_to_purge_nc_path_coding - checks whether an nc path has timed out
  235. * @bat_priv: the bat priv with all the soft interface information
  236. * @nc_path: the nc path to check
  237. *
  238. * Returns true if the entry has to be purged now, false otherwise
  239. */
  240. static bool batadv_nc_to_purge_nc_path_coding(struct batadv_priv *bat_priv,
  241. struct batadv_nc_path *nc_path)
  242. {
  243. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  244. return true;
  245. /* purge the path when no packets has been added for 10 times the
  246. * max_fwd_delay time
  247. */
  248. return batadv_has_timed_out(nc_path->last_valid,
  249. bat_priv->nc.max_fwd_delay * 10);
  250. }
  251. /**
  252. * batadv_nc_to_purge_nc_path_decoding - checks whether an nc path has timed out
  253. * @bat_priv: the bat priv with all the soft interface information
  254. * @nc_path: the nc path to check
  255. *
  256. * Returns true if the entry has to be purged now, false otherwise
  257. */
  258. static bool batadv_nc_to_purge_nc_path_decoding(struct batadv_priv *bat_priv,
  259. struct batadv_nc_path *nc_path)
  260. {
  261. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  262. return true;
  263. /* purge the path when no packets has been added for 10 times the
  264. * max_buffer time
  265. */
  266. return batadv_has_timed_out(nc_path->last_valid,
  267. bat_priv->nc.max_buffer_time * 10);
  268. }
  269. /**
  270. * batadv_nc_purge_orig_nc_nodes - go through list of nc nodes and purge stale
  271. * entries
  272. * @bat_priv: the bat priv with all the soft interface information
  273. * @list: list of nc nodes
  274. * @lock: nc node list lock
  275. * @to_purge: function in charge to decide whether an entry has to be purged or
  276. * not. This function takes the nc node as argument and has to return
  277. * a boolean value: true if the entry has to be deleted, false
  278. * otherwise
  279. */
  280. static void
  281. batadv_nc_purge_orig_nc_nodes(struct batadv_priv *bat_priv,
  282. struct list_head *list,
  283. spinlock_t *lock,
  284. bool (*to_purge)(struct batadv_priv *,
  285. struct batadv_nc_node *))
  286. {
  287. struct batadv_nc_node *nc_node, *nc_node_tmp;
  288. /* For each nc_node in list */
  289. spin_lock_bh(lock);
  290. list_for_each_entry_safe(nc_node, nc_node_tmp, list, list) {
  291. /* if an helper function has been passed as parameter,
  292. * ask it if the entry has to be purged or not
  293. */
  294. if (to_purge && !to_purge(bat_priv, nc_node))
  295. continue;
  296. batadv_dbg(BATADV_DBG_NC, bat_priv,
  297. "Removing nc_node %pM -> %pM\n",
  298. nc_node->addr, nc_node->orig_node->orig);
  299. list_del_rcu(&nc_node->list);
  300. batadv_nc_node_free_ref(nc_node);
  301. }
  302. spin_unlock_bh(lock);
  303. }
  304. /**
  305. * batadv_nc_purge_orig - purges all nc node data attached of the given
  306. * originator
  307. * @bat_priv: the bat priv with all the soft interface information
  308. * @orig_node: orig_node with the nc node entries to be purged
  309. * @to_purge: function in charge to decide whether an entry has to be purged or
  310. * not. This function takes the nc node as argument and has to return
  311. * a boolean value: true is the entry has to be deleted, false
  312. * otherwise
  313. */
  314. void batadv_nc_purge_orig(struct batadv_priv *bat_priv,
  315. struct batadv_orig_node *orig_node,
  316. bool (*to_purge)(struct batadv_priv *,
  317. struct batadv_nc_node *))
  318. {
  319. /* Check ingoing nc_node's of this orig_node */
  320. batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->in_coding_list,
  321. &orig_node->in_coding_list_lock,
  322. to_purge);
  323. /* Check outgoing nc_node's of this orig_node */
  324. batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->out_coding_list,
  325. &orig_node->out_coding_list_lock,
  326. to_purge);
  327. }
  328. /**
  329. * batadv_nc_purge_orig_hash - traverse entire originator hash to check if they
  330. * have timed out nc nodes
  331. * @bat_priv: the bat priv with all the soft interface information
  332. */
  333. static void batadv_nc_purge_orig_hash(struct batadv_priv *bat_priv)
  334. {
  335. struct batadv_hashtable *hash = bat_priv->orig_hash;
  336. struct hlist_head *head;
  337. struct batadv_orig_node *orig_node;
  338. u32 i;
  339. if (!hash)
  340. return;
  341. /* For each orig_node */
  342. for (i = 0; i < hash->size; i++) {
  343. head = &hash->table[i];
  344. rcu_read_lock();
  345. hlist_for_each_entry_rcu(orig_node, head, hash_entry)
  346. batadv_nc_purge_orig(bat_priv, orig_node,
  347. batadv_nc_to_purge_nc_node);
  348. rcu_read_unlock();
  349. }
  350. }
  351. /**
  352. * batadv_nc_purge_paths - traverse all nc paths part of the hash and remove
  353. * unused ones
  354. * @bat_priv: the bat priv with all the soft interface information
  355. * @hash: hash table containing the nc paths to check
  356. * @to_purge: function in charge to decide whether an entry has to be purged or
  357. * not. This function takes the nc node as argument and has to return
  358. * a boolean value: true is the entry has to be deleted, false
  359. * otherwise
  360. */
  361. static void batadv_nc_purge_paths(struct batadv_priv *bat_priv,
  362. struct batadv_hashtable *hash,
  363. bool (*to_purge)(struct batadv_priv *,
  364. struct batadv_nc_path *))
  365. {
  366. struct hlist_head *head;
  367. struct hlist_node *node_tmp;
  368. struct batadv_nc_path *nc_path;
  369. spinlock_t *lock; /* Protects lists in hash */
  370. u32 i;
  371. for (i = 0; i < hash->size; i++) {
  372. head = &hash->table[i];
  373. lock = &hash->list_locks[i];
  374. /* For each nc_path in this bin */
  375. spin_lock_bh(lock);
  376. hlist_for_each_entry_safe(nc_path, node_tmp, head, hash_entry) {
  377. /* if an helper function has been passed as parameter,
  378. * ask it if the entry has to be purged or not
  379. */
  380. if (to_purge && !to_purge(bat_priv, nc_path))
  381. continue;
  382. /* purging an non-empty nc_path should never happen, but
  383. * is observed under high CPU load. Delay the purging
  384. * until next iteration to allow the packet_list to be
  385. * emptied first.
  386. */
  387. if (!unlikely(list_empty(&nc_path->packet_list))) {
  388. net_ratelimited_function(printk,
  389. KERN_WARNING
  390. "Skipping free of non-empty nc_path (%pM -> %pM)!\n",
  391. nc_path->prev_hop,
  392. nc_path->next_hop);
  393. continue;
  394. }
  395. /* nc_path is unused, so remove it */
  396. batadv_dbg(BATADV_DBG_NC, bat_priv,
  397. "Remove nc_path %pM -> %pM\n",
  398. nc_path->prev_hop, nc_path->next_hop);
  399. hlist_del_rcu(&nc_path->hash_entry);
  400. batadv_nc_path_free_ref(nc_path);
  401. }
  402. spin_unlock_bh(lock);
  403. }
  404. }
  405. /**
  406. * batadv_nc_hash_key_gen - computes the nc_path hash key
  407. * @key: buffer to hold the final hash key
  408. * @src: source ethernet mac address going into the hash key
  409. * @dst: destination ethernet mac address going into the hash key
  410. */
  411. static void batadv_nc_hash_key_gen(struct batadv_nc_path *key, const char *src,
  412. const char *dst)
  413. {
  414. memcpy(key->prev_hop, src, sizeof(key->prev_hop));
  415. memcpy(key->next_hop, dst, sizeof(key->next_hop));
  416. }
  417. /**
  418. * batadv_nc_hash_choose - compute the hash value for an nc path
  419. * @data: data to hash
  420. * @size: size of the hash table
  421. *
  422. * Returns the selected index in the hash table for the given data.
  423. */
  424. static u32 batadv_nc_hash_choose(const void *data, u32 size)
  425. {
  426. const struct batadv_nc_path *nc_path = data;
  427. u32 hash = 0;
  428. hash = jhash(&nc_path->prev_hop, sizeof(nc_path->prev_hop), hash);
  429. hash = jhash(&nc_path->next_hop, sizeof(nc_path->next_hop), hash);
  430. return hash % size;
  431. }
  432. /**
  433. * batadv_nc_hash_compare - comparing function used in the network coding hash
  434. * tables
  435. * @node: node in the local table
  436. * @data2: second object to compare the node to
  437. *
  438. * Returns 1 if the two entry are the same, 0 otherwise
  439. */
  440. static int batadv_nc_hash_compare(const struct hlist_node *node,
  441. const void *data2)
  442. {
  443. const struct batadv_nc_path *nc_path1, *nc_path2;
  444. nc_path1 = container_of(node, struct batadv_nc_path, hash_entry);
  445. nc_path2 = data2;
  446. /* Return 1 if the two keys are identical */
  447. if (memcmp(nc_path1->prev_hop, nc_path2->prev_hop,
  448. sizeof(nc_path1->prev_hop)) != 0)
  449. return 0;
  450. if (memcmp(nc_path1->next_hop, nc_path2->next_hop,
  451. sizeof(nc_path1->next_hop)) != 0)
  452. return 0;
  453. return 1;
  454. }
  455. /**
  456. * batadv_nc_hash_find - search for an existing nc path and return it
  457. * @hash: hash table containing the nc path
  458. * @data: search key
  459. *
  460. * Returns the nc_path if found, NULL otherwise.
  461. */
  462. static struct batadv_nc_path *
  463. batadv_nc_hash_find(struct batadv_hashtable *hash,
  464. void *data)
  465. {
  466. struct hlist_head *head;
  467. struct batadv_nc_path *nc_path, *nc_path_tmp = NULL;
  468. int index;
  469. if (!hash)
  470. return NULL;
  471. index = batadv_nc_hash_choose(data, hash->size);
  472. head = &hash->table[index];
  473. rcu_read_lock();
  474. hlist_for_each_entry_rcu(nc_path, head, hash_entry) {
  475. if (!batadv_nc_hash_compare(&nc_path->hash_entry, data))
  476. continue;
  477. if (!atomic_inc_not_zero(&nc_path->refcount))
  478. continue;
  479. nc_path_tmp = nc_path;
  480. break;
  481. }
  482. rcu_read_unlock();
  483. return nc_path_tmp;
  484. }
  485. /**
  486. * batadv_nc_send_packet - send non-coded packet and free nc_packet struct
  487. * @nc_packet: the nc packet to send
  488. */
  489. static void batadv_nc_send_packet(struct batadv_nc_packet *nc_packet)
  490. {
  491. batadv_send_skb_packet(nc_packet->skb,
  492. nc_packet->neigh_node->if_incoming,
  493. nc_packet->nc_path->next_hop);
  494. nc_packet->skb = NULL;
  495. batadv_nc_packet_free(nc_packet);
  496. }
  497. /**
  498. * batadv_nc_sniffed_purge - Checks timestamp of given sniffed nc_packet.
  499. * @bat_priv: the bat priv with all the soft interface information
  500. * @nc_path: the nc path the packet belongs to
  501. * @nc_packet: the nc packet to be checked
  502. *
  503. * Checks whether the given sniffed (overheard) nc_packet has hit its buffering
  504. * timeout. If so, the packet is no longer kept and the entry deleted from the
  505. * queue. Has to be called with the appropriate locks.
  506. *
  507. * Returns false as soon as the entry in the fifo queue has not been timed out
  508. * yet and true otherwise.
  509. */
  510. static bool batadv_nc_sniffed_purge(struct batadv_priv *bat_priv,
  511. struct batadv_nc_path *nc_path,
  512. struct batadv_nc_packet *nc_packet)
  513. {
  514. unsigned long timeout = bat_priv->nc.max_buffer_time;
  515. bool res = false;
  516. lockdep_assert_held(&nc_path->packet_list_lock);
  517. /* Packets are added to tail, so the remaining packets did not time
  518. * out and we can stop processing the current queue
  519. */
  520. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE &&
  521. !batadv_has_timed_out(nc_packet->timestamp, timeout))
  522. goto out;
  523. /* purge nc packet */
  524. list_del(&nc_packet->list);
  525. batadv_nc_packet_free(nc_packet);
  526. res = true;
  527. out:
  528. return res;
  529. }
  530. /**
  531. * batadv_nc_fwd_flush - Checks the timestamp of the given nc packet.
  532. * @bat_priv: the bat priv with all the soft interface information
  533. * @nc_path: the nc path the packet belongs to
  534. * @nc_packet: the nc packet to be checked
  535. *
  536. * Checks whether the given nc packet has hit its forward timeout. If so, the
  537. * packet is no longer delayed, immediately sent and the entry deleted from the
  538. * queue. Has to be called with the appropriate locks.
  539. *
  540. * Returns false as soon as the entry in the fifo queue has not been timed out
  541. * yet and true otherwise.
  542. */
  543. static bool batadv_nc_fwd_flush(struct batadv_priv *bat_priv,
  544. struct batadv_nc_path *nc_path,
  545. struct batadv_nc_packet *nc_packet)
  546. {
  547. unsigned long timeout = bat_priv->nc.max_fwd_delay;
  548. lockdep_assert_held(&nc_path->packet_list_lock);
  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. u32 last_real_seqno;
  657. u8 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
  764. * structs (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;
  779. struct batadv_nc_node *out_nc_node = NULL;
  780. /* Check if network coding is enabled */
  781. if (!atomic_read(&bat_priv->network_coding))
  782. goto out;
  783. /* check if orig node is network coding enabled */
  784. if (!test_bit(BATADV_ORIG_CAPA_HAS_NC, &orig_node->capabilities))
  785. goto out;
  786. /* accept ogms from 'good' neighbors and single hop neighbors */
  787. if (!batadv_can_nc_with_orig(bat_priv, orig_node, ogm_packet) &&
  788. !is_single_hop_neigh)
  789. goto out;
  790. /* Add orig_node as in_nc_node on hop */
  791. in_nc_node = batadv_nc_get_nc_node(bat_priv, orig_node,
  792. orig_neigh_node, true);
  793. if (!in_nc_node)
  794. goto out;
  795. in_nc_node->last_seen = jiffies;
  796. /* Add hop as out_nc_node on orig_node */
  797. out_nc_node = batadv_nc_get_nc_node(bat_priv, orig_neigh_node,
  798. orig_node, false);
  799. if (!out_nc_node)
  800. goto out;
  801. out_nc_node->last_seen = jiffies;
  802. out:
  803. if (in_nc_node)
  804. batadv_nc_node_free_ref(in_nc_node);
  805. if (out_nc_node)
  806. batadv_nc_node_free_ref(out_nc_node);
  807. }
  808. /**
  809. * batadv_nc_get_path - get existing nc_path or allocate a new one
  810. * @bat_priv: the bat priv with all the soft interface information
  811. * @hash: hash table containing the nc path
  812. * @src: ethernet source address - first half of the nc path search key
  813. * @dst: ethernet destination address - second half of the nc path search key
  814. *
  815. * Returns pointer to nc_path if the path was found or created, returns NULL
  816. * on error.
  817. */
  818. static struct batadv_nc_path *batadv_nc_get_path(struct batadv_priv *bat_priv,
  819. struct batadv_hashtable *hash,
  820. u8 *src,
  821. u8 *dst)
  822. {
  823. int hash_added;
  824. struct batadv_nc_path *nc_path, nc_path_key;
  825. batadv_nc_hash_key_gen(&nc_path_key, src, dst);
  826. /* Search for existing nc_path */
  827. nc_path = batadv_nc_hash_find(hash, (void *)&nc_path_key);
  828. if (nc_path) {
  829. /* Set timestamp to delay removal of nc_path */
  830. nc_path->last_valid = jiffies;
  831. return nc_path;
  832. }
  833. /* No existing nc_path was found; create a new */
  834. nc_path = kzalloc(sizeof(*nc_path), GFP_ATOMIC);
  835. if (!nc_path)
  836. return NULL;
  837. /* Initialize nc_path */
  838. INIT_LIST_HEAD(&nc_path->packet_list);
  839. spin_lock_init(&nc_path->packet_list_lock);
  840. atomic_set(&nc_path->refcount, 2);
  841. nc_path->last_valid = jiffies;
  842. ether_addr_copy(nc_path->next_hop, dst);
  843. ether_addr_copy(nc_path->prev_hop, src);
  844. batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_path %pM -> %pM\n",
  845. nc_path->prev_hop,
  846. nc_path->next_hop);
  847. /* Add nc_path to hash table */
  848. hash_added = batadv_hash_add(hash, batadv_nc_hash_compare,
  849. batadv_nc_hash_choose, &nc_path_key,
  850. &nc_path->hash_entry);
  851. if (hash_added < 0) {
  852. kfree(nc_path);
  853. return NULL;
  854. }
  855. return nc_path;
  856. }
  857. /**
  858. * batadv_nc_random_weight_tq - scale the receivers TQ-value to avoid unfair
  859. * selection of a receiver with slightly lower TQ than the other
  860. * @tq: to be weighted tq value
  861. */
  862. static u8 batadv_nc_random_weight_tq(u8 tq)
  863. {
  864. u8 rand_val, rand_tq;
  865. get_random_bytes(&rand_val, sizeof(rand_val));
  866. /* randomize the estimated packet loss (max TQ - estimated TQ) */
  867. rand_tq = rand_val * (BATADV_TQ_MAX_VALUE - tq);
  868. /* normalize the randomized packet loss */
  869. rand_tq /= BATADV_TQ_MAX_VALUE;
  870. /* convert to (randomized) estimated tq again */
  871. return BATADV_TQ_MAX_VALUE - rand_tq;
  872. }
  873. /**
  874. * batadv_nc_memxor - XOR destination with source
  875. * @dst: byte array to XOR into
  876. * @src: byte array to XOR from
  877. * @len: length of destination array
  878. */
  879. static void batadv_nc_memxor(char *dst, const char *src, unsigned int len)
  880. {
  881. unsigned int i;
  882. for (i = 0; i < len; ++i)
  883. dst[i] ^= src[i];
  884. }
  885. /**
  886. * batadv_nc_code_packets - code a received unicast_packet with an nc packet
  887. * into a coded_packet and send it
  888. * @bat_priv: the bat priv with all the soft interface information
  889. * @skb: data skb to forward
  890. * @ethhdr: pointer to the ethernet header inside the skb
  891. * @nc_packet: structure containing the packet to the skb can be coded with
  892. * @neigh_node: next hop to forward packet to
  893. *
  894. * Returns true if both packets are consumed, false otherwise.
  895. */
  896. static bool batadv_nc_code_packets(struct batadv_priv *bat_priv,
  897. struct sk_buff *skb,
  898. struct ethhdr *ethhdr,
  899. struct batadv_nc_packet *nc_packet,
  900. struct batadv_neigh_node *neigh_node)
  901. {
  902. u8 tq_weighted_neigh, tq_weighted_coding, tq_tmp;
  903. struct sk_buff *skb_dest, *skb_src;
  904. struct batadv_unicast_packet *packet1;
  905. struct batadv_unicast_packet *packet2;
  906. struct batadv_coded_packet *coded_packet;
  907. struct batadv_neigh_node *neigh_tmp, *router_neigh;
  908. struct batadv_neigh_node *router_coding = NULL;
  909. struct batadv_neigh_ifinfo *router_neigh_ifinfo = NULL;
  910. struct batadv_neigh_ifinfo *router_coding_ifinfo = NULL;
  911. u8 *first_source, *first_dest, *second_source, *second_dest;
  912. __be32 packet_id1, packet_id2;
  913. size_t count;
  914. bool res = false;
  915. int coding_len;
  916. int unicast_size = sizeof(*packet1);
  917. int coded_size = sizeof(*coded_packet);
  918. int header_add = coded_size - unicast_size;
  919. /* TODO: do we need to consider the outgoing interface for
  920. * coded packets?
  921. */
  922. router_neigh = batadv_orig_router_get(neigh_node->orig_node,
  923. BATADV_IF_DEFAULT);
  924. if (!router_neigh)
  925. goto out;
  926. router_neigh_ifinfo = batadv_neigh_ifinfo_get(router_neigh,
  927. BATADV_IF_DEFAULT);
  928. if (!router_neigh_ifinfo)
  929. goto out;
  930. neigh_tmp = nc_packet->neigh_node;
  931. router_coding = batadv_orig_router_get(neigh_tmp->orig_node,
  932. BATADV_IF_DEFAULT);
  933. if (!router_coding)
  934. goto out;
  935. router_coding_ifinfo = batadv_neigh_ifinfo_get(router_coding,
  936. BATADV_IF_DEFAULT);
  937. if (!router_coding_ifinfo)
  938. goto out;
  939. tq_tmp = router_neigh_ifinfo->bat_iv.tq_avg;
  940. tq_weighted_neigh = batadv_nc_random_weight_tq(tq_tmp);
  941. tq_tmp = router_coding_ifinfo->bat_iv.tq_avg;
  942. tq_weighted_coding = batadv_nc_random_weight_tq(tq_tmp);
  943. /* Select one destination for the MAC-header dst-field based on
  944. * weighted TQ-values.
  945. */
  946. if (tq_weighted_neigh >= tq_weighted_coding) {
  947. /* Destination from nc_packet is selected for MAC-header */
  948. first_dest = nc_packet->nc_path->next_hop;
  949. first_source = nc_packet->nc_path->prev_hop;
  950. second_dest = neigh_node->addr;
  951. second_source = ethhdr->h_source;
  952. packet1 = (struct batadv_unicast_packet *)nc_packet->skb->data;
  953. packet2 = (struct batadv_unicast_packet *)skb->data;
  954. packet_id1 = nc_packet->packet_id;
  955. packet_id2 = batadv_skb_crc32(skb,
  956. skb->data + sizeof(*packet2));
  957. } else {
  958. /* Destination for skb is selected for MAC-header */
  959. first_dest = neigh_node->addr;
  960. first_source = ethhdr->h_source;
  961. second_dest = nc_packet->nc_path->next_hop;
  962. second_source = nc_packet->nc_path->prev_hop;
  963. packet1 = (struct batadv_unicast_packet *)skb->data;
  964. packet2 = (struct batadv_unicast_packet *)nc_packet->skb->data;
  965. packet_id1 = batadv_skb_crc32(skb,
  966. skb->data + sizeof(*packet1));
  967. packet_id2 = nc_packet->packet_id;
  968. }
  969. /* Instead of zero padding the smallest data buffer, we
  970. * code into the largest.
  971. */
  972. if (skb->len <= nc_packet->skb->len) {
  973. skb_dest = nc_packet->skb;
  974. skb_src = skb;
  975. } else {
  976. skb_dest = skb;
  977. skb_src = nc_packet->skb;
  978. }
  979. /* coding_len is used when decoding the packet shorter packet */
  980. coding_len = skb_src->len - unicast_size;
  981. if (skb_linearize(skb_dest) < 0 || skb_linearize(skb_src) < 0)
  982. goto out;
  983. skb_push(skb_dest, header_add);
  984. coded_packet = (struct batadv_coded_packet *)skb_dest->data;
  985. skb_reset_mac_header(skb_dest);
  986. coded_packet->packet_type = BATADV_CODED;
  987. coded_packet->version = BATADV_COMPAT_VERSION;
  988. coded_packet->ttl = packet1->ttl;
  989. /* Info about first unicast packet */
  990. ether_addr_copy(coded_packet->first_source, first_source);
  991. ether_addr_copy(coded_packet->first_orig_dest, packet1->dest);
  992. coded_packet->first_crc = packet_id1;
  993. coded_packet->first_ttvn = packet1->ttvn;
  994. /* Info about second unicast packet */
  995. ether_addr_copy(coded_packet->second_dest, second_dest);
  996. ether_addr_copy(coded_packet->second_source, second_source);
  997. ether_addr_copy(coded_packet->second_orig_dest, packet2->dest);
  998. coded_packet->second_crc = packet_id2;
  999. coded_packet->second_ttl = packet2->ttl;
  1000. coded_packet->second_ttvn = packet2->ttvn;
  1001. coded_packet->coded_len = htons(coding_len);
  1002. /* This is where the magic happens: Code skb_src into skb_dest */
  1003. batadv_nc_memxor(skb_dest->data + coded_size,
  1004. skb_src->data + unicast_size, coding_len);
  1005. /* Update counters accordingly */
  1006. if (BATADV_SKB_CB(skb_src)->decoded &&
  1007. BATADV_SKB_CB(skb_dest)->decoded) {
  1008. /* Both packets are recoded */
  1009. count = skb_src->len + ETH_HLEN;
  1010. count += skb_dest->len + ETH_HLEN;
  1011. batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE, 2);
  1012. batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES, count);
  1013. } else if (!BATADV_SKB_CB(skb_src)->decoded &&
  1014. !BATADV_SKB_CB(skb_dest)->decoded) {
  1015. /* Both packets are newly coded */
  1016. count = skb_src->len + ETH_HLEN;
  1017. count += skb_dest->len + ETH_HLEN;
  1018. batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE, 2);
  1019. batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES, count);
  1020. } else if (BATADV_SKB_CB(skb_src)->decoded &&
  1021. !BATADV_SKB_CB(skb_dest)->decoded) {
  1022. /* skb_src recoded and skb_dest is newly coded */
  1023. batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE);
  1024. batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES,
  1025. skb_src->len + ETH_HLEN);
  1026. batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE);
  1027. batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES,
  1028. skb_dest->len + ETH_HLEN);
  1029. } else if (!BATADV_SKB_CB(skb_src)->decoded &&
  1030. BATADV_SKB_CB(skb_dest)->decoded) {
  1031. /* skb_src is newly coded and skb_dest is recoded */
  1032. batadv_inc_counter(bat_priv, BATADV_CNT_NC_CODE);
  1033. batadv_add_counter(bat_priv, BATADV_CNT_NC_CODE_BYTES,
  1034. skb_src->len + ETH_HLEN);
  1035. batadv_inc_counter(bat_priv, BATADV_CNT_NC_RECODE);
  1036. batadv_add_counter(bat_priv, BATADV_CNT_NC_RECODE_BYTES,
  1037. skb_dest->len + ETH_HLEN);
  1038. }
  1039. /* skb_src is now coded into skb_dest, so free it */
  1040. kfree_skb(skb_src);
  1041. /* avoid duplicate free of skb from nc_packet */
  1042. nc_packet->skb = NULL;
  1043. batadv_nc_packet_free(nc_packet);
  1044. /* Send the coded packet and return true */
  1045. batadv_send_skb_packet(skb_dest, neigh_node->if_incoming, first_dest);
  1046. res = true;
  1047. out:
  1048. if (router_neigh)
  1049. batadv_neigh_node_free_ref(router_neigh);
  1050. if (router_coding)
  1051. batadv_neigh_node_free_ref(router_coding);
  1052. if (router_neigh_ifinfo)
  1053. batadv_neigh_ifinfo_free_ref(router_neigh_ifinfo);
  1054. if (router_coding_ifinfo)
  1055. batadv_neigh_ifinfo_free_ref(router_coding_ifinfo);
  1056. return res;
  1057. }
  1058. /**
  1059. * batadv_nc_skb_coding_possible - true if a decoded skb is available at dst.
  1060. * @skb: data skb to forward
  1061. * @dst: destination mac address of the other skb to code with
  1062. * @src: source mac address of skb
  1063. *
  1064. * Whenever we network code a packet we have to check whether we received it in
  1065. * a network coded form. If so, we may not be able to use it for coding because
  1066. * some neighbors may also have received (overheard) the packet in the network
  1067. * coded form without being able to decode it. It is hard to know which of the
  1068. * neighboring nodes was able to decode the packet, therefore we can only
  1069. * re-code the packet if the source of the previous encoded packet is involved.
  1070. * Since the source encoded the packet we can be certain it has all necessary
  1071. * decode information.
  1072. *
  1073. * Returns true if coding of a decoded packet is allowed.
  1074. */
  1075. static bool batadv_nc_skb_coding_possible(struct sk_buff *skb, u8 *dst, u8 *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. u8 *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. u8 *eth_dst,
  1154. u8 *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. u8 *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. u8 *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. u8 *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. }