network-coding.c 59 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991
  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/stddef.h>
  46. #include <linux/string.h>
  47. #include <linux/workqueue.h>
  48. #include "hard-interface.h"
  49. #include "hash.h"
  50. #include "log.h"
  51. #include "originator.h"
  52. #include "packet.h"
  53. #include "routing.h"
  54. #include "send.h"
  55. #include "tvlv.h"
  56. static struct lock_class_key batadv_nc_coding_hash_lock_class_key;
  57. static struct lock_class_key batadv_nc_decoding_hash_lock_class_key;
  58. static void batadv_nc_worker(struct work_struct *work);
  59. static int batadv_nc_recv_coded_packet(struct sk_buff *skb,
  60. struct batadv_hard_iface *recv_if);
  61. /**
  62. * batadv_nc_init - one-time initialization for network coding
  63. *
  64. * Return: 0 on success or negative error number in case of failure
  65. */
  66. int __init batadv_nc_init(void)
  67. {
  68. int ret;
  69. /* Register our packet type */
  70. ret = batadv_recv_handler_register(BATADV_CODED,
  71. batadv_nc_recv_coded_packet);
  72. return ret;
  73. }
  74. /**
  75. * batadv_nc_start_timer - initialise the nc periodic worker
  76. * @bat_priv: the bat priv with all the soft interface information
  77. */
  78. static void batadv_nc_start_timer(struct batadv_priv *bat_priv)
  79. {
  80. queue_delayed_work(batadv_event_workqueue, &bat_priv->nc.work,
  81. msecs_to_jiffies(10));
  82. }
  83. /**
  84. * batadv_nc_tvlv_container_update - update the network coding tvlv container
  85. * after network coding setting change
  86. * @bat_priv: the bat priv with all the soft interface information
  87. */
  88. static void batadv_nc_tvlv_container_update(struct batadv_priv *bat_priv)
  89. {
  90. char nc_mode;
  91. nc_mode = atomic_read(&bat_priv->network_coding);
  92. switch (nc_mode) {
  93. case 0:
  94. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_NC, 1);
  95. break;
  96. case 1:
  97. batadv_tvlv_container_register(bat_priv, BATADV_TVLV_NC, 1,
  98. NULL, 0);
  99. break;
  100. }
  101. }
  102. /**
  103. * batadv_nc_status_update - update the network coding tvlv container after
  104. * network coding setting change
  105. * @net_dev: the soft interface net device
  106. */
  107. void batadv_nc_status_update(struct net_device *net_dev)
  108. {
  109. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  110. batadv_nc_tvlv_container_update(bat_priv);
  111. }
  112. /**
  113. * batadv_nc_tvlv_ogm_handler_v1 - process incoming nc tvlv container
  114. * @bat_priv: the bat priv with all the soft interface information
  115. * @orig: the orig_node of the ogm
  116. * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
  117. * @tvlv_value: tvlv buffer containing the gateway data
  118. * @tvlv_value_len: tvlv buffer length
  119. */
  120. static void batadv_nc_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
  121. struct batadv_orig_node *orig,
  122. u8 flags,
  123. void *tvlv_value, u16 tvlv_value_len)
  124. {
  125. if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
  126. clear_bit(BATADV_ORIG_CAPA_HAS_NC, &orig->capabilities);
  127. else
  128. set_bit(BATADV_ORIG_CAPA_HAS_NC, &orig->capabilities);
  129. }
  130. /**
  131. * batadv_nc_mesh_init - initialise coding hash table and start house keeping
  132. * @bat_priv: the bat priv with all the soft interface information
  133. *
  134. * Return: 0 on success or negative error number in case of failure
  135. */
  136. int batadv_nc_mesh_init(struct batadv_priv *bat_priv)
  137. {
  138. bat_priv->nc.timestamp_fwd_flush = jiffies;
  139. bat_priv->nc.timestamp_sniffed_purge = jiffies;
  140. if (bat_priv->nc.coding_hash || bat_priv->nc.decoding_hash)
  141. return 0;
  142. bat_priv->nc.coding_hash = batadv_hash_new(128);
  143. if (!bat_priv->nc.coding_hash)
  144. goto err;
  145. batadv_hash_set_lock_class(bat_priv->nc.coding_hash,
  146. &batadv_nc_coding_hash_lock_class_key);
  147. bat_priv->nc.decoding_hash = batadv_hash_new(128);
  148. if (!bat_priv->nc.decoding_hash)
  149. goto err;
  150. batadv_hash_set_lock_class(bat_priv->nc.decoding_hash,
  151. &batadv_nc_decoding_hash_lock_class_key);
  152. INIT_DELAYED_WORK(&bat_priv->nc.work, batadv_nc_worker);
  153. batadv_nc_start_timer(bat_priv);
  154. batadv_tvlv_handler_register(bat_priv, batadv_nc_tvlv_ogm_handler_v1,
  155. NULL, BATADV_TVLV_NC, 1,
  156. BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
  157. batadv_nc_tvlv_container_update(bat_priv);
  158. return 0;
  159. err:
  160. return -ENOMEM;
  161. }
  162. /**
  163. * batadv_nc_init_bat_priv - initialise the nc specific bat_priv variables
  164. * @bat_priv: the bat priv with all the soft interface information
  165. */
  166. void batadv_nc_init_bat_priv(struct batadv_priv *bat_priv)
  167. {
  168. atomic_set(&bat_priv->network_coding, 0);
  169. bat_priv->nc.min_tq = 200;
  170. bat_priv->nc.max_fwd_delay = 10;
  171. bat_priv->nc.max_buffer_time = 200;
  172. }
  173. /**
  174. * batadv_nc_init_orig - initialise the nc fields of an orig_node
  175. * @orig_node: the orig_node which is going to be initialised
  176. */
  177. void batadv_nc_init_orig(struct batadv_orig_node *orig_node)
  178. {
  179. INIT_LIST_HEAD(&orig_node->in_coding_list);
  180. INIT_LIST_HEAD(&orig_node->out_coding_list);
  181. spin_lock_init(&orig_node->in_coding_list_lock);
  182. spin_lock_init(&orig_node->out_coding_list_lock);
  183. }
  184. /**
  185. * batadv_nc_node_release - release nc_node from lists and queue for free after
  186. * rcu grace period
  187. * @ref: kref pointer of the nc_node
  188. */
  189. static void batadv_nc_node_release(struct kref *ref)
  190. {
  191. struct batadv_nc_node *nc_node;
  192. nc_node = container_of(ref, struct batadv_nc_node, refcount);
  193. batadv_orig_node_put(nc_node->orig_node);
  194. kfree_rcu(nc_node, rcu);
  195. }
  196. /**
  197. * batadv_nc_node_put - decrement the nc_node refcounter and possibly
  198. * release it
  199. * @nc_node: nc_node to be free'd
  200. */
  201. static void batadv_nc_node_put(struct batadv_nc_node *nc_node)
  202. {
  203. kref_put(&nc_node->refcount, batadv_nc_node_release);
  204. }
  205. /**
  206. * batadv_nc_path_release - release nc_path from lists and queue for free after
  207. * rcu grace period
  208. * @ref: kref pointer of the nc_path
  209. */
  210. static void batadv_nc_path_release(struct kref *ref)
  211. {
  212. struct batadv_nc_path *nc_path;
  213. nc_path = container_of(ref, struct batadv_nc_path, refcount);
  214. kfree_rcu(nc_path, rcu);
  215. }
  216. /**
  217. * batadv_nc_path_put - decrement the nc_path refcounter and possibly
  218. * release it
  219. * @nc_path: nc_path to be free'd
  220. */
  221. static void batadv_nc_path_put(struct batadv_nc_path *nc_path)
  222. {
  223. kref_put(&nc_path->refcount, batadv_nc_path_release);
  224. }
  225. /**
  226. * batadv_nc_packet_free - frees nc packet
  227. * @nc_packet: the nc packet to free
  228. * @dropped: whether the packet is freed because is is dropped
  229. */
  230. static void batadv_nc_packet_free(struct batadv_nc_packet *nc_packet,
  231. bool dropped)
  232. {
  233. if (dropped)
  234. kfree_skb(nc_packet->skb);
  235. else
  236. consume_skb(nc_packet->skb);
  237. batadv_nc_path_put(nc_packet->nc_path);
  238. kfree(nc_packet);
  239. }
  240. /**
  241. * batadv_nc_to_purge_nc_node - checks whether an nc node has to be purged
  242. * @bat_priv: the bat priv with all the soft interface information
  243. * @nc_node: the nc node to check
  244. *
  245. * Return: true if the entry has to be purged now, false otherwise
  246. */
  247. static bool batadv_nc_to_purge_nc_node(struct batadv_priv *bat_priv,
  248. struct batadv_nc_node *nc_node)
  249. {
  250. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  251. return true;
  252. return batadv_has_timed_out(nc_node->last_seen, BATADV_NC_NODE_TIMEOUT);
  253. }
  254. /**
  255. * batadv_nc_to_purge_nc_path_coding - checks whether an nc path has timed out
  256. * @bat_priv: the bat priv with all the soft interface information
  257. * @nc_path: the nc path to check
  258. *
  259. * Return: true if the entry has to be purged now, false otherwise
  260. */
  261. static bool batadv_nc_to_purge_nc_path_coding(struct batadv_priv *bat_priv,
  262. struct batadv_nc_path *nc_path)
  263. {
  264. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  265. return true;
  266. /* purge the path when no packets has been added for 10 times the
  267. * max_fwd_delay time
  268. */
  269. return batadv_has_timed_out(nc_path->last_valid,
  270. bat_priv->nc.max_fwd_delay * 10);
  271. }
  272. /**
  273. * batadv_nc_to_purge_nc_path_decoding - checks whether an nc path has timed out
  274. * @bat_priv: the bat priv with all the soft interface information
  275. * @nc_path: the nc path to check
  276. *
  277. * Return: true if the entry has to be purged now, false otherwise
  278. */
  279. static bool batadv_nc_to_purge_nc_path_decoding(struct batadv_priv *bat_priv,
  280. struct batadv_nc_path *nc_path)
  281. {
  282. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  283. return true;
  284. /* purge the path when no packets has been added for 10 times the
  285. * max_buffer time
  286. */
  287. return batadv_has_timed_out(nc_path->last_valid,
  288. bat_priv->nc.max_buffer_time * 10);
  289. }
  290. /**
  291. * batadv_nc_purge_orig_nc_nodes - go through list of nc nodes and purge stale
  292. * entries
  293. * @bat_priv: the bat priv with all the soft interface information
  294. * @list: list of nc nodes
  295. * @lock: nc node list lock
  296. * @to_purge: function in charge to decide whether an entry has to be purged or
  297. * not. This function takes the nc node as argument and has to return
  298. * a boolean value: true if the entry has to be deleted, false
  299. * otherwise
  300. */
  301. static void
  302. batadv_nc_purge_orig_nc_nodes(struct batadv_priv *bat_priv,
  303. struct list_head *list,
  304. spinlock_t *lock,
  305. bool (*to_purge)(struct batadv_priv *,
  306. struct batadv_nc_node *))
  307. {
  308. struct batadv_nc_node *nc_node, *nc_node_tmp;
  309. /* For each nc_node in list */
  310. spin_lock_bh(lock);
  311. list_for_each_entry_safe(nc_node, nc_node_tmp, list, list) {
  312. /* if an helper function has been passed as parameter,
  313. * ask it if the entry has to be purged or not
  314. */
  315. if (to_purge && !to_purge(bat_priv, nc_node))
  316. continue;
  317. batadv_dbg(BATADV_DBG_NC, bat_priv,
  318. "Removing nc_node %pM -> %pM\n",
  319. nc_node->addr, nc_node->orig_node->orig);
  320. list_del_rcu(&nc_node->list);
  321. batadv_nc_node_put(nc_node);
  322. }
  323. spin_unlock_bh(lock);
  324. }
  325. /**
  326. * batadv_nc_purge_orig - purges all nc node data attached of the given
  327. * originator
  328. * @bat_priv: the bat priv with all the soft interface information
  329. * @orig_node: orig_node with the nc node entries to be purged
  330. * @to_purge: function in charge to decide whether an entry has to be purged or
  331. * not. This function takes the nc node as argument and has to return
  332. * a boolean value: true is the entry has to be deleted, false
  333. * otherwise
  334. */
  335. void batadv_nc_purge_orig(struct batadv_priv *bat_priv,
  336. struct batadv_orig_node *orig_node,
  337. bool (*to_purge)(struct batadv_priv *,
  338. struct batadv_nc_node *))
  339. {
  340. /* Check ingoing nc_node's of this orig_node */
  341. batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->in_coding_list,
  342. &orig_node->in_coding_list_lock,
  343. to_purge);
  344. /* Check outgoing nc_node's of this orig_node */
  345. batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->out_coding_list,
  346. &orig_node->out_coding_list_lock,
  347. to_purge);
  348. }
  349. /**
  350. * batadv_nc_purge_orig_hash - traverse entire originator hash to check if they
  351. * have timed out nc nodes
  352. * @bat_priv: the bat priv with all the soft interface information
  353. */
  354. static void batadv_nc_purge_orig_hash(struct batadv_priv *bat_priv)
  355. {
  356. struct batadv_hashtable *hash = bat_priv->orig_hash;
  357. struct hlist_head *head;
  358. struct batadv_orig_node *orig_node;
  359. u32 i;
  360. if (!hash)
  361. return;
  362. /* For each orig_node */
  363. for (i = 0; i < hash->size; i++) {
  364. head = &hash->table[i];
  365. rcu_read_lock();
  366. hlist_for_each_entry_rcu(orig_node, head, hash_entry)
  367. batadv_nc_purge_orig(bat_priv, orig_node,
  368. batadv_nc_to_purge_nc_node);
  369. rcu_read_unlock();
  370. }
  371. }
  372. /**
  373. * batadv_nc_purge_paths - traverse all nc paths part of the hash and remove
  374. * unused ones
  375. * @bat_priv: the bat priv with all the soft interface information
  376. * @hash: hash table containing the nc paths to check
  377. * @to_purge: function in charge to decide whether an entry has to be purged or
  378. * not. This function takes the nc node as argument and has to return
  379. * a boolean value: true is the entry has to be deleted, false
  380. * otherwise
  381. */
  382. static void batadv_nc_purge_paths(struct batadv_priv *bat_priv,
  383. struct batadv_hashtable *hash,
  384. bool (*to_purge)(struct batadv_priv *,
  385. struct batadv_nc_path *))
  386. {
  387. struct hlist_head *head;
  388. struct hlist_node *node_tmp;
  389. struct batadv_nc_path *nc_path;
  390. spinlock_t *lock; /* Protects lists in hash */
  391. u32 i;
  392. for (i = 0; i < hash->size; i++) {
  393. head = &hash->table[i];
  394. lock = &hash->list_locks[i];
  395. /* For each nc_path in this bin */
  396. spin_lock_bh(lock);
  397. hlist_for_each_entry_safe(nc_path, node_tmp, head, hash_entry) {
  398. /* if an helper function has been passed as parameter,
  399. * ask it if the entry has to be purged or not
  400. */
  401. if (to_purge && !to_purge(bat_priv, nc_path))
  402. continue;
  403. /* purging an non-empty nc_path should never happen, but
  404. * is observed under high CPU load. Delay the purging
  405. * until next iteration to allow the packet_list to be
  406. * emptied first.
  407. */
  408. if (!unlikely(list_empty(&nc_path->packet_list))) {
  409. net_ratelimited_function(printk,
  410. KERN_WARNING
  411. "Skipping free of non-empty nc_path (%pM -> %pM)!\n",
  412. nc_path->prev_hop,
  413. nc_path->next_hop);
  414. continue;
  415. }
  416. /* nc_path is unused, so remove it */
  417. batadv_dbg(BATADV_DBG_NC, bat_priv,
  418. "Remove nc_path %pM -> %pM\n",
  419. nc_path->prev_hop, nc_path->next_hop);
  420. hlist_del_rcu(&nc_path->hash_entry);
  421. batadv_nc_path_put(nc_path);
  422. }
  423. spin_unlock_bh(lock);
  424. }
  425. }
  426. /**
  427. * batadv_nc_hash_key_gen - computes the nc_path hash key
  428. * @key: buffer to hold the final hash key
  429. * @src: source ethernet mac address going into the hash key
  430. * @dst: destination ethernet mac address going into the hash key
  431. */
  432. static void batadv_nc_hash_key_gen(struct batadv_nc_path *key, const char *src,
  433. const char *dst)
  434. {
  435. memcpy(key->prev_hop, src, sizeof(key->prev_hop));
  436. memcpy(key->next_hop, dst, sizeof(key->next_hop));
  437. }
  438. /**
  439. * batadv_nc_hash_choose - compute the hash value for an nc path
  440. * @data: data to hash
  441. * @size: size of the hash table
  442. *
  443. * Return: the selected index in the hash table for the given data.
  444. */
  445. static u32 batadv_nc_hash_choose(const void *data, u32 size)
  446. {
  447. const struct batadv_nc_path *nc_path = data;
  448. u32 hash = 0;
  449. hash = jhash(&nc_path->prev_hop, sizeof(nc_path->prev_hop), hash);
  450. hash = jhash(&nc_path->next_hop, sizeof(nc_path->next_hop), hash);
  451. return hash % size;
  452. }
  453. /**
  454. * batadv_nc_hash_compare - comparing function used in the network coding hash
  455. * tables
  456. * @node: node in the local table
  457. * @data2: second object to compare the node to
  458. *
  459. * Return: true if the two entry are the same, false otherwise
  460. */
  461. static bool batadv_nc_hash_compare(const struct hlist_node *node,
  462. const void *data2)
  463. {
  464. const struct batadv_nc_path *nc_path1, *nc_path2;
  465. nc_path1 = container_of(node, struct batadv_nc_path, hash_entry);
  466. nc_path2 = data2;
  467. /* Return 1 if the two keys are identical */
  468. if (!batadv_compare_eth(nc_path1->prev_hop, nc_path2->prev_hop))
  469. return false;
  470. if (!batadv_compare_eth(nc_path1->next_hop, nc_path2->next_hop))
  471. return false;
  472. return true;
  473. }
  474. /**
  475. * batadv_nc_hash_find - search for an existing nc path and return it
  476. * @hash: hash table containing the nc path
  477. * @data: search key
  478. *
  479. * Return: the nc_path if found, NULL otherwise.
  480. */
  481. static struct batadv_nc_path *
  482. batadv_nc_hash_find(struct batadv_hashtable *hash,
  483. void *data)
  484. {
  485. struct hlist_head *head;
  486. struct batadv_nc_path *nc_path, *nc_path_tmp = NULL;
  487. int index;
  488. if (!hash)
  489. return NULL;
  490. index = batadv_nc_hash_choose(data, hash->size);
  491. head = &hash->table[index];
  492. rcu_read_lock();
  493. hlist_for_each_entry_rcu(nc_path, head, hash_entry) {
  494. if (!batadv_nc_hash_compare(&nc_path->hash_entry, data))
  495. continue;
  496. if (!kref_get_unless_zero(&nc_path->refcount))
  497. continue;
  498. nc_path_tmp = nc_path;
  499. break;
  500. }
  501. rcu_read_unlock();
  502. return nc_path_tmp;
  503. }
  504. /**
  505. * batadv_nc_send_packet - send non-coded packet and free nc_packet struct
  506. * @nc_packet: the nc packet to send
  507. */
  508. static void batadv_nc_send_packet(struct batadv_nc_packet *nc_packet)
  509. {
  510. batadv_send_unicast_skb(nc_packet->skb, nc_packet->neigh_node);
  511. nc_packet->skb = NULL;
  512. batadv_nc_packet_free(nc_packet, false);
  513. }
  514. /**
  515. * batadv_nc_sniffed_purge - Checks timestamp of given sniffed nc_packet.
  516. * @bat_priv: the bat priv with all the soft interface information
  517. * @nc_path: the nc path the packet belongs to
  518. * @nc_packet: the nc packet to be checked
  519. *
  520. * Checks whether the given sniffed (overheard) nc_packet has hit its buffering
  521. * timeout. If so, the packet is no longer kept and the entry deleted from the
  522. * queue. Has to be called with the appropriate locks.
  523. *
  524. * Return: false as soon as the entry in the fifo queue has not been timed out
  525. * yet and true otherwise.
  526. */
  527. static bool batadv_nc_sniffed_purge(struct batadv_priv *bat_priv,
  528. struct batadv_nc_path *nc_path,
  529. struct batadv_nc_packet *nc_packet)
  530. {
  531. unsigned long timeout = bat_priv->nc.max_buffer_time;
  532. bool res = false;
  533. lockdep_assert_held(&nc_path->packet_list_lock);
  534. /* Packets are added to tail, so the remaining packets did not time
  535. * out and we can stop processing the current queue
  536. */
  537. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE &&
  538. !batadv_has_timed_out(nc_packet->timestamp, timeout))
  539. goto out;
  540. /* purge nc packet */
  541. list_del(&nc_packet->list);
  542. batadv_nc_packet_free(nc_packet, true);
  543. res = true;
  544. out:
  545. return res;
  546. }
  547. /**
  548. * batadv_nc_fwd_flush - Checks the timestamp of the given nc packet.
  549. * @bat_priv: the bat priv with all the soft interface information
  550. * @nc_path: the nc path the packet belongs to
  551. * @nc_packet: the nc packet to be checked
  552. *
  553. * Checks whether the given nc packet has hit its forward timeout. If so, the
  554. * packet is no longer delayed, immediately sent and the entry deleted from the
  555. * queue. Has to be called with the appropriate locks.
  556. *
  557. * Return: false as soon as the entry in the fifo queue has not been timed out
  558. * yet and true otherwise.
  559. */
  560. static bool batadv_nc_fwd_flush(struct batadv_priv *bat_priv,
  561. struct batadv_nc_path *nc_path,
  562. struct batadv_nc_packet *nc_packet)
  563. {
  564. unsigned long timeout = bat_priv->nc.max_fwd_delay;
  565. lockdep_assert_held(&nc_path->packet_list_lock);
  566. /* Packets are added to tail, so the remaining packets did not time
  567. * out and we can stop processing the current queue
  568. */
  569. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE &&
  570. !batadv_has_timed_out(nc_packet->timestamp, timeout))
  571. return false;
  572. /* Send packet */
  573. batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
  574. batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
  575. nc_packet->skb->len + ETH_HLEN);
  576. list_del(&nc_packet->list);
  577. batadv_nc_send_packet(nc_packet);
  578. return true;
  579. }
  580. /**
  581. * batadv_nc_process_nc_paths - traverse given nc packet pool and free timed out
  582. * nc packets
  583. * @bat_priv: the bat priv with all the soft interface information
  584. * @hash: to be processed hash table
  585. * @process_fn: Function called to process given nc packet. Should return true
  586. * to encourage this function to proceed with the next packet.
  587. * Otherwise the rest of the current queue is skipped.
  588. */
  589. static void
  590. batadv_nc_process_nc_paths(struct batadv_priv *bat_priv,
  591. struct batadv_hashtable *hash,
  592. bool (*process_fn)(struct batadv_priv *,
  593. struct batadv_nc_path *,
  594. struct batadv_nc_packet *))
  595. {
  596. struct hlist_head *head;
  597. struct batadv_nc_packet *nc_packet, *nc_packet_tmp;
  598. struct batadv_nc_path *nc_path;
  599. bool ret;
  600. int i;
  601. if (!hash)
  602. return;
  603. /* Loop hash table bins */
  604. for (i = 0; i < hash->size; i++) {
  605. head = &hash->table[i];
  606. /* Loop coding paths */
  607. rcu_read_lock();
  608. hlist_for_each_entry_rcu(nc_path, head, hash_entry) {
  609. /* Loop packets */
  610. spin_lock_bh(&nc_path->packet_list_lock);
  611. list_for_each_entry_safe(nc_packet, nc_packet_tmp,
  612. &nc_path->packet_list, list) {
  613. ret = process_fn(bat_priv, nc_path, nc_packet);
  614. if (!ret)
  615. break;
  616. }
  617. spin_unlock_bh(&nc_path->packet_list_lock);
  618. }
  619. rcu_read_unlock();
  620. }
  621. }
  622. /**
  623. * batadv_nc_worker - periodic task for house keeping related to network coding
  624. * @work: kernel work struct
  625. */
  626. static void batadv_nc_worker(struct work_struct *work)
  627. {
  628. struct delayed_work *delayed_work;
  629. struct batadv_priv_nc *priv_nc;
  630. struct batadv_priv *bat_priv;
  631. unsigned long timeout;
  632. delayed_work = to_delayed_work(work);
  633. priv_nc = container_of(delayed_work, struct batadv_priv_nc, work);
  634. bat_priv = container_of(priv_nc, struct batadv_priv, nc);
  635. batadv_nc_purge_orig_hash(bat_priv);
  636. batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash,
  637. batadv_nc_to_purge_nc_path_coding);
  638. batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash,
  639. batadv_nc_to_purge_nc_path_decoding);
  640. timeout = bat_priv->nc.max_fwd_delay;
  641. if (batadv_has_timed_out(bat_priv->nc.timestamp_fwd_flush, timeout)) {
  642. batadv_nc_process_nc_paths(bat_priv, bat_priv->nc.coding_hash,
  643. batadv_nc_fwd_flush);
  644. bat_priv->nc.timestamp_fwd_flush = jiffies;
  645. }
  646. if (batadv_has_timed_out(bat_priv->nc.timestamp_sniffed_purge,
  647. bat_priv->nc.max_buffer_time)) {
  648. batadv_nc_process_nc_paths(bat_priv, bat_priv->nc.decoding_hash,
  649. batadv_nc_sniffed_purge);
  650. bat_priv->nc.timestamp_sniffed_purge = jiffies;
  651. }
  652. /* Schedule a new check */
  653. batadv_nc_start_timer(bat_priv);
  654. }
  655. /**
  656. * batadv_can_nc_with_orig - checks whether the given orig node is suitable for
  657. * coding or not
  658. * @bat_priv: the bat priv with all the soft interface information
  659. * @orig_node: neighboring orig node which may be used as nc candidate
  660. * @ogm_packet: incoming ogm packet also used for the checks
  661. *
  662. * Return: true if:
  663. * 1) The OGM must have the most recent sequence number.
  664. * 2) The TTL must be decremented by one and only one.
  665. * 3) The OGM must be received from the first hop from orig_node.
  666. * 4) The TQ value of the OGM must be above bat_priv->nc.min_tq.
  667. */
  668. static bool batadv_can_nc_with_orig(struct batadv_priv *bat_priv,
  669. struct batadv_orig_node *orig_node,
  670. struct batadv_ogm_packet *ogm_packet)
  671. {
  672. struct batadv_orig_ifinfo *orig_ifinfo;
  673. u32 last_real_seqno;
  674. u8 last_ttl;
  675. orig_ifinfo = batadv_orig_ifinfo_get(orig_node, BATADV_IF_DEFAULT);
  676. if (!orig_ifinfo)
  677. return false;
  678. last_ttl = orig_ifinfo->last_ttl;
  679. last_real_seqno = orig_ifinfo->last_real_seqno;
  680. batadv_orig_ifinfo_put(orig_ifinfo);
  681. if (last_real_seqno != ntohl(ogm_packet->seqno))
  682. return false;
  683. if (last_ttl != ogm_packet->ttl + 1)
  684. return false;
  685. if (!batadv_compare_eth(ogm_packet->orig, ogm_packet->prev_sender))
  686. return false;
  687. if (ogm_packet->tq < bat_priv->nc.min_tq)
  688. return false;
  689. return true;
  690. }
  691. /**
  692. * batadv_nc_find_nc_node - search for an existing nc node and return it
  693. * @orig_node: orig node originating the ogm packet
  694. * @orig_neigh_node: neighboring orig node from which we received the ogm packet
  695. * (can be equal to orig_node)
  696. * @in_coding: traverse incoming or outgoing network coding list
  697. *
  698. * Return: the nc_node if found, NULL otherwise.
  699. */
  700. static struct batadv_nc_node *
  701. batadv_nc_find_nc_node(struct batadv_orig_node *orig_node,
  702. struct batadv_orig_node *orig_neigh_node,
  703. bool in_coding)
  704. {
  705. struct batadv_nc_node *nc_node, *nc_node_out = NULL;
  706. struct list_head *list;
  707. if (in_coding)
  708. list = &orig_neigh_node->in_coding_list;
  709. else
  710. list = &orig_neigh_node->out_coding_list;
  711. /* Traverse list of nc_nodes to orig_node */
  712. rcu_read_lock();
  713. list_for_each_entry_rcu(nc_node, list, list) {
  714. if (!batadv_compare_eth(nc_node->addr, orig_node->orig))
  715. continue;
  716. if (!kref_get_unless_zero(&nc_node->refcount))
  717. continue;
  718. /* Found a match */
  719. nc_node_out = nc_node;
  720. break;
  721. }
  722. rcu_read_unlock();
  723. return nc_node_out;
  724. }
  725. /**
  726. * batadv_nc_get_nc_node - retrieves an nc node or creates the entry if it was
  727. * not found
  728. * @bat_priv: the bat priv with all the soft interface information
  729. * @orig_node: orig node originating the ogm packet
  730. * @orig_neigh_node: neighboring orig node from which we received the ogm packet
  731. * (can be equal to orig_node)
  732. * @in_coding: traverse incoming or outgoing network coding list
  733. *
  734. * Return: the nc_node if found or created, NULL in case of an error.
  735. */
  736. static struct batadv_nc_node *
  737. batadv_nc_get_nc_node(struct batadv_priv *bat_priv,
  738. struct batadv_orig_node *orig_node,
  739. struct batadv_orig_node *orig_neigh_node,
  740. bool in_coding)
  741. {
  742. struct batadv_nc_node *nc_node;
  743. spinlock_t *lock; /* Used to lock list selected by "int in_coding" */
  744. struct list_head *list;
  745. /* Check if nc_node is already added */
  746. nc_node = batadv_nc_find_nc_node(orig_node, orig_neigh_node, in_coding);
  747. /* Node found */
  748. if (nc_node)
  749. return nc_node;
  750. nc_node = kzalloc(sizeof(*nc_node), GFP_ATOMIC);
  751. if (!nc_node)
  752. return NULL;
  753. /* Initialize nc_node */
  754. INIT_LIST_HEAD(&nc_node->list);
  755. kref_init(&nc_node->refcount);
  756. ether_addr_copy(nc_node->addr, orig_node->orig);
  757. kref_get(&orig_neigh_node->refcount);
  758. nc_node->orig_node = orig_neigh_node;
  759. /* Select ingoing or outgoing coding node */
  760. if (in_coding) {
  761. lock = &orig_neigh_node->in_coding_list_lock;
  762. list = &orig_neigh_node->in_coding_list;
  763. } else {
  764. lock = &orig_neigh_node->out_coding_list_lock;
  765. list = &orig_neigh_node->out_coding_list;
  766. }
  767. batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_node %pM -> %pM\n",
  768. nc_node->addr, nc_node->orig_node->orig);
  769. /* Add nc_node to orig_node */
  770. spin_lock_bh(lock);
  771. kref_get(&nc_node->refcount);
  772. list_add_tail_rcu(&nc_node->list, list);
  773. spin_unlock_bh(lock);
  774. return nc_node;
  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. nc_path->last_valid = jiffies;
  856. ether_addr_copy(nc_path->next_hop, dst);
  857. ether_addr_copy(nc_path->prev_hop, src);
  858. batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_path %pM -> %pM\n",
  859. nc_path->prev_hop,
  860. nc_path->next_hop);
  861. /* Add nc_path to hash table */
  862. kref_get(&nc_path->refcount);
  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. consume_skb(skb_src);
  1058. /* avoid duplicate free of skb from nc_packet */
  1059. nc_packet->skb = NULL;
  1060. batadv_nc_packet_free(nc_packet, false);
  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. consume_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, false);
  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. goto free_skb;
  1578. /* Make sure we can access (and remove) header */
  1579. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  1580. goto free_skb;
  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. goto free_skb;
  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. goto free_skb;
  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, true);
  1615. free_skb:
  1616. kfree_skb(skb);
  1617. return NET_RX_DROP;
  1618. }
  1619. /**
  1620. * batadv_nc_mesh_free - clean up network coding memory
  1621. * @bat_priv: the bat priv with all the soft interface information
  1622. */
  1623. void batadv_nc_mesh_free(struct batadv_priv *bat_priv)
  1624. {
  1625. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_NC, 1);
  1626. batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_NC, 1);
  1627. cancel_delayed_work_sync(&bat_priv->nc.work);
  1628. batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash, NULL);
  1629. batadv_hash_destroy(bat_priv->nc.coding_hash);
  1630. batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash, NULL);
  1631. batadv_hash_destroy(bat_priv->nc.decoding_hash);
  1632. }
  1633. #ifdef CONFIG_BATMAN_ADV_DEBUGFS
  1634. /**
  1635. * batadv_nc_nodes_seq_print_text - print the nc node information
  1636. * @seq: seq file to print on
  1637. * @offset: not used
  1638. *
  1639. * Return: always 0
  1640. */
  1641. int batadv_nc_nodes_seq_print_text(struct seq_file *seq, void *offset)
  1642. {
  1643. struct net_device *net_dev = (struct net_device *)seq->private;
  1644. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  1645. struct batadv_hashtable *hash = bat_priv->orig_hash;
  1646. struct batadv_hard_iface *primary_if;
  1647. struct hlist_head *head;
  1648. struct batadv_orig_node *orig_node;
  1649. struct batadv_nc_node *nc_node;
  1650. int i;
  1651. primary_if = batadv_seq_print_text_primary_if_get(seq);
  1652. if (!primary_if)
  1653. goto out;
  1654. /* Traverse list of originators */
  1655. for (i = 0; i < hash->size; i++) {
  1656. head = &hash->table[i];
  1657. /* For each orig_node in this bin */
  1658. rcu_read_lock();
  1659. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  1660. /* no need to print the orig node if it does not have
  1661. * network coding neighbors
  1662. */
  1663. if (list_empty(&orig_node->in_coding_list) &&
  1664. list_empty(&orig_node->out_coding_list))
  1665. continue;
  1666. seq_printf(seq, "Node: %pM\n", orig_node->orig);
  1667. seq_puts(seq, " Ingoing: ");
  1668. /* For each in_nc_node to this orig_node */
  1669. list_for_each_entry_rcu(nc_node,
  1670. &orig_node->in_coding_list,
  1671. list)
  1672. seq_printf(seq, "%pM ",
  1673. nc_node->addr);
  1674. seq_puts(seq, "\n");
  1675. seq_puts(seq, " Outgoing: ");
  1676. /* For out_nc_node to this orig_node */
  1677. list_for_each_entry_rcu(nc_node,
  1678. &orig_node->out_coding_list,
  1679. list)
  1680. seq_printf(seq, "%pM ",
  1681. nc_node->addr);
  1682. seq_puts(seq, "\n\n");
  1683. }
  1684. rcu_read_unlock();
  1685. }
  1686. out:
  1687. if (primary_if)
  1688. batadv_hardif_put(primary_if);
  1689. return 0;
  1690. }
  1691. /**
  1692. * batadv_nc_init_debugfs - create nc folder and related files in debugfs
  1693. * @bat_priv: the bat priv with all the soft interface information
  1694. *
  1695. * Return: 0 on success or negative error number in case of failure
  1696. */
  1697. int batadv_nc_init_debugfs(struct batadv_priv *bat_priv)
  1698. {
  1699. struct dentry *nc_dir, *file;
  1700. nc_dir = debugfs_create_dir("nc", bat_priv->debug_dir);
  1701. if (!nc_dir)
  1702. goto out;
  1703. file = debugfs_create_u8("min_tq", 0644, nc_dir, &bat_priv->nc.min_tq);
  1704. if (!file)
  1705. goto out;
  1706. file = debugfs_create_u32("max_fwd_delay", 0644, nc_dir,
  1707. &bat_priv->nc.max_fwd_delay);
  1708. if (!file)
  1709. goto out;
  1710. file = debugfs_create_u32("max_buffer_time", 0644, nc_dir,
  1711. &bat_priv->nc.max_buffer_time);
  1712. if (!file)
  1713. goto out;
  1714. return 0;
  1715. out:
  1716. return -ENOMEM;
  1717. }
  1718. #endif