network-coding.c 55 KB

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