network-coding.c 57 KB

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