network-coding.c 59 KB

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