network-coding.c 58 KB

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