network-coding.c 58 KB

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