network-coding.c 59 KB

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