network-coding.c 57 KB

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