network-coding.c 47 KB

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