network-coding.c 56 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878
  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) B.A.T.M.A.N. contributors:
  3. *
  4. * Martin Hundebøll, Jeppe Ledet-Pedersen
  5. */
  6. #include "network-coding.h"
  7. #include "main.h"
  8. #include <linux/atomic.h>
  9. #include <linux/bitops.h>
  10. #include <linux/byteorder/generic.h>
  11. #include <linux/compiler.h>
  12. #include <linux/container_of.h>
  13. #include <linux/errno.h>
  14. #include <linux/etherdevice.h>
  15. #include <linux/gfp.h>
  16. #include <linux/if_ether.h>
  17. #include <linux/if_packet.h>
  18. #include <linux/init.h>
  19. #include <linux/jhash.h>
  20. #include <linux/jiffies.h>
  21. #include <linux/kref.h>
  22. #include <linux/list.h>
  23. #include <linux/lockdep.h>
  24. #include <linux/net.h>
  25. #include <linux/netdevice.h>
  26. #include <linux/printk.h>
  27. #include <linux/random.h>
  28. #include <linux/rculist.h>
  29. #include <linux/rcupdate.h>
  30. #include <linux/skbuff.h>
  31. #include <linux/slab.h>
  32. #include <linux/spinlock.h>
  33. #include <linux/stddef.h>
  34. #include <linux/string.h>
  35. #include <linux/workqueue.h>
  36. #include <uapi/linux/batadv_packet.h>
  37. #include "hash.h"
  38. #include "log.h"
  39. #include "originator.h"
  40. #include "routing.h"
  41. #include "send.h"
  42. #include "tvlv.h"
  43. static struct lock_class_key batadv_nc_coding_hash_lock_class_key;
  44. static struct lock_class_key batadv_nc_decoding_hash_lock_class_key;
  45. static void batadv_nc_worker(struct work_struct *work);
  46. static int batadv_nc_recv_coded_packet(struct sk_buff *skb,
  47. struct batadv_hard_iface *recv_if);
  48. /**
  49. * batadv_nc_init() - one-time initialization for network coding
  50. *
  51. * Return: 0 on success or negative error number in case of failure
  52. */
  53. int __init batadv_nc_init(void)
  54. {
  55. /* Register our packet type */
  56. return batadv_recv_handler_register(BATADV_CODED,
  57. batadv_nc_recv_coded_packet);
  58. }
  59. /**
  60. * batadv_nc_start_timer() - initialise the nc periodic worker
  61. * @bat_priv: the bat priv with all the soft interface information
  62. */
  63. static void batadv_nc_start_timer(struct batadv_priv *bat_priv)
  64. {
  65. queue_delayed_work(batadv_event_workqueue, &bat_priv->nc.work,
  66. msecs_to_jiffies(10));
  67. }
  68. /**
  69. * batadv_nc_tvlv_container_update() - update the network coding tvlv container
  70. * after network coding setting change
  71. * @bat_priv: the bat priv with all the soft interface information
  72. */
  73. static void batadv_nc_tvlv_container_update(struct batadv_priv *bat_priv)
  74. {
  75. char nc_mode;
  76. nc_mode = atomic_read(&bat_priv->network_coding);
  77. switch (nc_mode) {
  78. case 0:
  79. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_NC, 1);
  80. break;
  81. case 1:
  82. batadv_tvlv_container_register(bat_priv, BATADV_TVLV_NC, 1,
  83. NULL, 0);
  84. break;
  85. }
  86. }
  87. /**
  88. * batadv_nc_status_update() - update the network coding tvlv container after
  89. * network coding setting change
  90. * @net_dev: the soft interface net device
  91. */
  92. void batadv_nc_status_update(struct net_device *net_dev)
  93. {
  94. struct batadv_priv *bat_priv = netdev_priv(net_dev);
  95. batadv_nc_tvlv_container_update(bat_priv);
  96. }
  97. /**
  98. * batadv_nc_tvlv_ogm_handler_v1() - process incoming nc tvlv container
  99. * @bat_priv: the bat priv with all the soft interface information
  100. * @orig: the orig_node of the ogm
  101. * @flags: flags indicating the tvlv state (see batadv_tvlv_handler_flags)
  102. * @tvlv_value: tvlv buffer containing the gateway data
  103. * @tvlv_value_len: tvlv buffer length
  104. */
  105. static void batadv_nc_tvlv_ogm_handler_v1(struct batadv_priv *bat_priv,
  106. struct batadv_orig_node *orig,
  107. u8 flags,
  108. void *tvlv_value, u16 tvlv_value_len)
  109. {
  110. if (flags & BATADV_TVLV_HANDLER_OGM_CIFNOTFND)
  111. clear_bit(BATADV_ORIG_CAPA_HAS_NC, &orig->capabilities);
  112. else
  113. set_bit(BATADV_ORIG_CAPA_HAS_NC, &orig->capabilities);
  114. }
  115. /**
  116. * batadv_nc_mesh_init() - initialise coding hash table and start housekeeping
  117. * @bat_priv: the bat priv with all the soft interface information
  118. *
  119. * Return: 0 on success or negative error number in case of failure
  120. */
  121. int batadv_nc_mesh_init(struct batadv_priv *bat_priv)
  122. {
  123. bat_priv->nc.timestamp_fwd_flush = jiffies;
  124. bat_priv->nc.timestamp_sniffed_purge = jiffies;
  125. if (bat_priv->nc.coding_hash || bat_priv->nc.decoding_hash)
  126. return 0;
  127. bat_priv->nc.coding_hash = batadv_hash_new(128);
  128. if (!bat_priv->nc.coding_hash)
  129. goto err;
  130. batadv_hash_set_lock_class(bat_priv->nc.coding_hash,
  131. &batadv_nc_coding_hash_lock_class_key);
  132. bat_priv->nc.decoding_hash = batadv_hash_new(128);
  133. if (!bat_priv->nc.decoding_hash) {
  134. batadv_hash_destroy(bat_priv->nc.coding_hash);
  135. goto err;
  136. }
  137. batadv_hash_set_lock_class(bat_priv->nc.decoding_hash,
  138. &batadv_nc_decoding_hash_lock_class_key);
  139. INIT_DELAYED_WORK(&bat_priv->nc.work, batadv_nc_worker);
  140. batadv_nc_start_timer(bat_priv);
  141. batadv_tvlv_handler_register(bat_priv, batadv_nc_tvlv_ogm_handler_v1,
  142. NULL, NULL, BATADV_TVLV_NC, 1,
  143. BATADV_TVLV_HANDLER_OGM_CIFNOTFND);
  144. batadv_nc_tvlv_container_update(bat_priv);
  145. return 0;
  146. err:
  147. return -ENOMEM;
  148. }
  149. /**
  150. * batadv_nc_init_bat_priv() - initialise the nc specific bat_priv variables
  151. * @bat_priv: the bat priv with all the soft interface information
  152. */
  153. void batadv_nc_init_bat_priv(struct batadv_priv *bat_priv)
  154. {
  155. atomic_set(&bat_priv->network_coding, 0);
  156. bat_priv->nc.min_tq = 200;
  157. bat_priv->nc.max_fwd_delay = 10;
  158. bat_priv->nc.max_buffer_time = 200;
  159. }
  160. /**
  161. * batadv_nc_init_orig() - initialise the nc fields of an orig_node
  162. * @orig_node: the orig_node which is going to be initialised
  163. */
  164. void batadv_nc_init_orig(struct batadv_orig_node *orig_node)
  165. {
  166. INIT_LIST_HEAD(&orig_node->in_coding_list);
  167. INIT_LIST_HEAD(&orig_node->out_coding_list);
  168. spin_lock_init(&orig_node->in_coding_list_lock);
  169. spin_lock_init(&orig_node->out_coding_list_lock);
  170. }
  171. /**
  172. * batadv_nc_node_release() - release nc_node from lists and queue for free
  173. * after rcu grace period
  174. * @ref: kref pointer of the nc_node
  175. */
  176. static void batadv_nc_node_release(struct kref *ref)
  177. {
  178. struct batadv_nc_node *nc_node;
  179. nc_node = container_of(ref, struct batadv_nc_node, refcount);
  180. batadv_orig_node_put(nc_node->orig_node);
  181. kfree_rcu(nc_node, rcu);
  182. }
  183. /**
  184. * batadv_nc_node_put() - decrement the nc_node refcounter and possibly
  185. * release it
  186. * @nc_node: nc_node to be free'd
  187. */
  188. static void batadv_nc_node_put(struct batadv_nc_node *nc_node)
  189. {
  190. if (!nc_node)
  191. return;
  192. kref_put(&nc_node->refcount, batadv_nc_node_release);
  193. }
  194. /**
  195. * batadv_nc_path_release() - release nc_path from lists and queue for free
  196. * after rcu grace period
  197. * @ref: kref pointer of the nc_path
  198. */
  199. static void batadv_nc_path_release(struct kref *ref)
  200. {
  201. struct batadv_nc_path *nc_path;
  202. nc_path = container_of(ref, struct batadv_nc_path, refcount);
  203. kfree_rcu(nc_path, rcu);
  204. }
  205. /**
  206. * batadv_nc_path_put() - decrement the nc_path refcounter and possibly
  207. * release it
  208. * @nc_path: nc_path to be free'd
  209. */
  210. static void batadv_nc_path_put(struct batadv_nc_path *nc_path)
  211. {
  212. if (!nc_path)
  213. return;
  214. kref_put(&nc_path->refcount, batadv_nc_path_release);
  215. }
  216. /**
  217. * batadv_nc_packet_free() - frees nc packet
  218. * @nc_packet: the nc packet to free
  219. * @dropped: whether the packet is freed because is dropped
  220. */
  221. static void batadv_nc_packet_free(struct batadv_nc_packet *nc_packet,
  222. bool dropped)
  223. {
  224. if (dropped)
  225. kfree_skb(nc_packet->skb);
  226. else
  227. consume_skb(nc_packet->skb);
  228. batadv_nc_path_put(nc_packet->nc_path);
  229. kfree(nc_packet);
  230. }
  231. /**
  232. * batadv_nc_to_purge_nc_node() - checks whether an nc node has to be purged
  233. * @bat_priv: the bat priv with all the soft interface information
  234. * @nc_node: the nc node to check
  235. *
  236. * Return: true if the entry has to be purged now, false otherwise
  237. */
  238. static bool batadv_nc_to_purge_nc_node(struct batadv_priv *bat_priv,
  239. struct batadv_nc_node *nc_node)
  240. {
  241. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  242. return true;
  243. return batadv_has_timed_out(nc_node->last_seen, BATADV_NC_NODE_TIMEOUT);
  244. }
  245. /**
  246. * batadv_nc_to_purge_nc_path_coding() - checks whether an nc path has timed out
  247. * @bat_priv: the bat priv with all the soft interface information
  248. * @nc_path: the nc path to check
  249. *
  250. * Return: true if the entry has to be purged now, false otherwise
  251. */
  252. static bool batadv_nc_to_purge_nc_path_coding(struct batadv_priv *bat_priv,
  253. struct batadv_nc_path *nc_path)
  254. {
  255. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  256. return true;
  257. /* purge the path when no packets has been added for 10 times the
  258. * max_fwd_delay time
  259. */
  260. return batadv_has_timed_out(nc_path->last_valid,
  261. bat_priv->nc.max_fwd_delay * 10);
  262. }
  263. /**
  264. * batadv_nc_to_purge_nc_path_decoding() - checks whether an nc path has timed
  265. * out
  266. * @bat_priv: the bat priv with all the soft interface information
  267. * @nc_path: the nc path to check
  268. *
  269. * Return: true if the entry has to be purged now, false otherwise
  270. */
  271. static bool batadv_nc_to_purge_nc_path_decoding(struct batadv_priv *bat_priv,
  272. struct batadv_nc_path *nc_path)
  273. {
  274. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  275. return true;
  276. /* purge the path when no packets has been added for 10 times the
  277. * max_buffer time
  278. */
  279. return batadv_has_timed_out(nc_path->last_valid,
  280. bat_priv->nc.max_buffer_time * 10);
  281. }
  282. /**
  283. * batadv_nc_purge_orig_nc_nodes() - go through list of nc nodes and purge stale
  284. * entries
  285. * @bat_priv: the bat priv with all the soft interface information
  286. * @list: list of nc nodes
  287. * @lock: nc node list lock
  288. * @to_purge: function in charge to decide whether an entry has to be purged or
  289. * not. This function takes the nc node as argument and has to return
  290. * a boolean value: true if the entry has to be deleted, false
  291. * otherwise
  292. */
  293. static void
  294. batadv_nc_purge_orig_nc_nodes(struct batadv_priv *bat_priv,
  295. struct list_head *list,
  296. spinlock_t *lock,
  297. bool (*to_purge)(struct batadv_priv *,
  298. struct batadv_nc_node *))
  299. {
  300. struct batadv_nc_node *nc_node, *nc_node_tmp;
  301. /* For each nc_node in list */
  302. spin_lock_bh(lock);
  303. list_for_each_entry_safe(nc_node, nc_node_tmp, list, list) {
  304. /* if an helper function has been passed as parameter,
  305. * ask it if the entry has to be purged or not
  306. */
  307. if (to_purge && !to_purge(bat_priv, nc_node))
  308. continue;
  309. batadv_dbg(BATADV_DBG_NC, bat_priv,
  310. "Removing nc_node %pM -> %pM\n",
  311. nc_node->addr, nc_node->orig_node->orig);
  312. list_del_rcu(&nc_node->list);
  313. batadv_nc_node_put(nc_node);
  314. }
  315. spin_unlock_bh(lock);
  316. }
  317. /**
  318. * batadv_nc_purge_orig() - purges all nc node data attached of the given
  319. * originator
  320. * @bat_priv: the bat priv with all the soft interface information
  321. * @orig_node: orig_node with the nc node entries to be purged
  322. * @to_purge: function in charge to decide whether an entry has to be purged or
  323. * not. This function takes the nc node as argument and has to return
  324. * a boolean value: true is the entry has to be deleted, false
  325. * otherwise
  326. */
  327. void batadv_nc_purge_orig(struct batadv_priv *bat_priv,
  328. struct batadv_orig_node *orig_node,
  329. bool (*to_purge)(struct batadv_priv *,
  330. struct batadv_nc_node *))
  331. {
  332. /* Check ingoing nc_node's of this orig_node */
  333. batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->in_coding_list,
  334. &orig_node->in_coding_list_lock,
  335. to_purge);
  336. /* Check outgoing nc_node's of this orig_node */
  337. batadv_nc_purge_orig_nc_nodes(bat_priv, &orig_node->out_coding_list,
  338. &orig_node->out_coding_list_lock,
  339. to_purge);
  340. }
  341. /**
  342. * batadv_nc_purge_orig_hash() - traverse entire originator hash to check if
  343. * they have timed out nc nodes
  344. * @bat_priv: the bat priv with all the soft interface information
  345. */
  346. static void batadv_nc_purge_orig_hash(struct batadv_priv *bat_priv)
  347. {
  348. struct batadv_hashtable *hash = bat_priv->orig_hash;
  349. struct hlist_head *head;
  350. struct batadv_orig_node *orig_node;
  351. u32 i;
  352. if (!hash)
  353. return;
  354. /* For each orig_node */
  355. for (i = 0; i < hash->size; i++) {
  356. head = &hash->table[i];
  357. rcu_read_lock();
  358. hlist_for_each_entry_rcu(orig_node, head, hash_entry)
  359. batadv_nc_purge_orig(bat_priv, orig_node,
  360. batadv_nc_to_purge_nc_node);
  361. rcu_read_unlock();
  362. }
  363. }
  364. /**
  365. * batadv_nc_purge_paths() - traverse all nc paths part of the hash and remove
  366. * unused ones
  367. * @bat_priv: the bat priv with all the soft interface information
  368. * @hash: hash table containing the nc paths to check
  369. * @to_purge: function in charge to decide whether an entry has to be purged or
  370. * not. This function takes the nc node as argument and has to return
  371. * a boolean value: true is the entry has to be deleted, false
  372. * otherwise
  373. */
  374. static void batadv_nc_purge_paths(struct batadv_priv *bat_priv,
  375. struct batadv_hashtable *hash,
  376. bool (*to_purge)(struct batadv_priv *,
  377. struct batadv_nc_path *))
  378. {
  379. struct hlist_head *head;
  380. struct hlist_node *node_tmp;
  381. struct batadv_nc_path *nc_path;
  382. spinlock_t *lock; /* Protects lists in hash */
  383. u32 i;
  384. for (i = 0; i < hash->size; i++) {
  385. head = &hash->table[i];
  386. lock = &hash->list_locks[i];
  387. /* For each nc_path in this bin */
  388. spin_lock_bh(lock);
  389. hlist_for_each_entry_safe(nc_path, node_tmp, head, hash_entry) {
  390. /* if an helper function has been passed as parameter,
  391. * ask it if the entry has to be purged or not
  392. */
  393. if (to_purge && !to_purge(bat_priv, nc_path))
  394. continue;
  395. /* purging an non-empty nc_path should never happen, but
  396. * is observed under high CPU load. Delay the purging
  397. * until next iteration to allow the packet_list to be
  398. * emptied first.
  399. */
  400. if (!unlikely(list_empty(&nc_path->packet_list))) {
  401. net_ratelimited_function(printk,
  402. KERN_WARNING
  403. "Skipping free of non-empty nc_path (%pM -> %pM)!\n",
  404. nc_path->prev_hop,
  405. nc_path->next_hop);
  406. continue;
  407. }
  408. /* nc_path is unused, so remove it */
  409. batadv_dbg(BATADV_DBG_NC, bat_priv,
  410. "Remove nc_path %pM -> %pM\n",
  411. nc_path->prev_hop, nc_path->next_hop);
  412. hlist_del_rcu(&nc_path->hash_entry);
  413. batadv_nc_path_put(nc_path);
  414. }
  415. spin_unlock_bh(lock);
  416. }
  417. }
  418. /**
  419. * batadv_nc_hash_key_gen() - computes the nc_path hash key
  420. * @key: buffer to hold the final hash key
  421. * @src: source ethernet mac address going into the hash key
  422. * @dst: destination ethernet mac address going into the hash key
  423. */
  424. static void batadv_nc_hash_key_gen(struct batadv_nc_path *key, const char *src,
  425. const char *dst)
  426. {
  427. memcpy(key->prev_hop, src, sizeof(key->prev_hop));
  428. memcpy(key->next_hop, dst, sizeof(key->next_hop));
  429. }
  430. /**
  431. * batadv_nc_hash_choose() - compute the hash value for an nc path
  432. * @data: data to hash
  433. * @size: size of the hash table
  434. *
  435. * Return: the selected index in the hash table for the given data.
  436. */
  437. static u32 batadv_nc_hash_choose(const void *data, u32 size)
  438. {
  439. const struct batadv_nc_path *nc_path = data;
  440. u32 hash = 0;
  441. hash = jhash(&nc_path->prev_hop, sizeof(nc_path->prev_hop), hash);
  442. hash = jhash(&nc_path->next_hop, sizeof(nc_path->next_hop), hash);
  443. return hash % size;
  444. }
  445. /**
  446. * batadv_nc_hash_compare() - comparing function used in the network coding hash
  447. * tables
  448. * @node: node in the local table
  449. * @data2: second object to compare the node to
  450. *
  451. * Return: true if the two entry are the same, false otherwise
  452. */
  453. static bool batadv_nc_hash_compare(const struct hlist_node *node,
  454. const void *data2)
  455. {
  456. const struct batadv_nc_path *nc_path1, *nc_path2;
  457. nc_path1 = container_of(node, struct batadv_nc_path, hash_entry);
  458. nc_path2 = data2;
  459. /* Return 1 if the two keys are identical */
  460. if (!batadv_compare_eth(nc_path1->prev_hop, nc_path2->prev_hop))
  461. return false;
  462. if (!batadv_compare_eth(nc_path1->next_hop, nc_path2->next_hop))
  463. return false;
  464. return true;
  465. }
  466. /**
  467. * batadv_nc_hash_find() - search for an existing nc path and return it
  468. * @hash: hash table containing the nc path
  469. * @data: search key
  470. *
  471. * Return: the nc_path if found, NULL otherwise.
  472. */
  473. static struct batadv_nc_path *
  474. batadv_nc_hash_find(struct batadv_hashtable *hash,
  475. void *data)
  476. {
  477. struct hlist_head *head;
  478. struct batadv_nc_path *nc_path, *nc_path_tmp = NULL;
  479. int index;
  480. if (!hash)
  481. return NULL;
  482. index = batadv_nc_hash_choose(data, hash->size);
  483. head = &hash->table[index];
  484. rcu_read_lock();
  485. hlist_for_each_entry_rcu(nc_path, head, hash_entry) {
  486. if (!batadv_nc_hash_compare(&nc_path->hash_entry, data))
  487. continue;
  488. if (!kref_get_unless_zero(&nc_path->refcount))
  489. continue;
  490. nc_path_tmp = nc_path;
  491. break;
  492. }
  493. rcu_read_unlock();
  494. return nc_path_tmp;
  495. }
  496. /**
  497. * batadv_nc_send_packet() - send non-coded packet and free nc_packet struct
  498. * @nc_packet: the nc packet to send
  499. */
  500. static void batadv_nc_send_packet(struct batadv_nc_packet *nc_packet)
  501. {
  502. batadv_send_unicast_skb(nc_packet->skb, nc_packet->neigh_node);
  503. nc_packet->skb = NULL;
  504. batadv_nc_packet_free(nc_packet, false);
  505. }
  506. /**
  507. * batadv_nc_sniffed_purge() - Checks timestamp of given sniffed nc_packet.
  508. * @bat_priv: the bat priv with all the soft interface information
  509. * @nc_path: the nc path the packet belongs to
  510. * @nc_packet: the nc packet to be checked
  511. *
  512. * Checks whether the given sniffed (overheard) nc_packet has hit its buffering
  513. * timeout. If so, the packet is no longer kept and the entry deleted from the
  514. * queue. Has to be called with the appropriate locks.
  515. *
  516. * Return: false as soon as the entry in the fifo queue has not been timed out
  517. * yet and true otherwise.
  518. */
  519. static bool batadv_nc_sniffed_purge(struct batadv_priv *bat_priv,
  520. struct batadv_nc_path *nc_path,
  521. struct batadv_nc_packet *nc_packet)
  522. {
  523. unsigned long timeout = bat_priv->nc.max_buffer_time;
  524. bool res = false;
  525. lockdep_assert_held(&nc_path->packet_list_lock);
  526. /* Packets are added to tail, so the remaining packets did not time
  527. * out and we can stop processing the current queue
  528. */
  529. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE &&
  530. !batadv_has_timed_out(nc_packet->timestamp, timeout))
  531. goto out;
  532. /* purge nc packet */
  533. list_del(&nc_packet->list);
  534. batadv_nc_packet_free(nc_packet, true);
  535. res = true;
  536. out:
  537. return res;
  538. }
  539. /**
  540. * batadv_nc_fwd_flush() - Checks the timestamp of the given nc packet.
  541. * @bat_priv: the bat priv with all the soft interface information
  542. * @nc_path: the nc path the packet belongs to
  543. * @nc_packet: the nc packet to be checked
  544. *
  545. * Checks whether the given nc packet has hit its forward timeout. If so, the
  546. * packet is no longer delayed, immediately sent and the entry deleted from the
  547. * queue. Has to be called with the appropriate locks.
  548. *
  549. * Return: false as soon as the entry in the fifo queue has not been timed out
  550. * yet and true otherwise.
  551. */
  552. static bool batadv_nc_fwd_flush(struct batadv_priv *bat_priv,
  553. struct batadv_nc_path *nc_path,
  554. struct batadv_nc_packet *nc_packet)
  555. {
  556. unsigned long timeout = bat_priv->nc.max_fwd_delay;
  557. lockdep_assert_held(&nc_path->packet_list_lock);
  558. /* Packets are added to tail, so the remaining packets did not time
  559. * out and we can stop processing the current queue
  560. */
  561. if (atomic_read(&bat_priv->mesh_state) == BATADV_MESH_ACTIVE &&
  562. !batadv_has_timed_out(nc_packet->timestamp, timeout))
  563. return false;
  564. /* Send packet */
  565. batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
  566. batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
  567. nc_packet->skb->len + ETH_HLEN);
  568. list_del(&nc_packet->list);
  569. batadv_nc_send_packet(nc_packet);
  570. return true;
  571. }
  572. /**
  573. * batadv_nc_process_nc_paths() - traverse given nc packet pool and free timed
  574. * out nc packets
  575. * @bat_priv: the bat priv with all the soft interface information
  576. * @hash: to be processed hash table
  577. * @process_fn: Function called to process given nc packet. Should return true
  578. * to encourage this function to proceed with the next packet.
  579. * Otherwise the rest of the current queue is skipped.
  580. */
  581. static void
  582. batadv_nc_process_nc_paths(struct batadv_priv *bat_priv,
  583. struct batadv_hashtable *hash,
  584. bool (*process_fn)(struct batadv_priv *,
  585. struct batadv_nc_path *,
  586. struct batadv_nc_packet *))
  587. {
  588. struct hlist_head *head;
  589. struct batadv_nc_packet *nc_packet, *nc_packet_tmp;
  590. struct batadv_nc_path *nc_path;
  591. bool ret;
  592. int i;
  593. if (!hash)
  594. return;
  595. /* Loop hash table bins */
  596. for (i = 0; i < hash->size; i++) {
  597. head = &hash->table[i];
  598. /* Loop coding paths */
  599. rcu_read_lock();
  600. hlist_for_each_entry_rcu(nc_path, head, hash_entry) {
  601. /* Loop packets */
  602. spin_lock_bh(&nc_path->packet_list_lock);
  603. list_for_each_entry_safe(nc_packet, nc_packet_tmp,
  604. &nc_path->packet_list, list) {
  605. ret = process_fn(bat_priv, nc_path, nc_packet);
  606. if (!ret)
  607. break;
  608. }
  609. spin_unlock_bh(&nc_path->packet_list_lock);
  610. }
  611. rcu_read_unlock();
  612. }
  613. }
  614. /**
  615. * batadv_nc_worker() - periodic task for housekeeping related to network
  616. * coding
  617. * @work: kernel work struct
  618. */
  619. static void batadv_nc_worker(struct work_struct *work)
  620. {
  621. struct delayed_work *delayed_work;
  622. struct batadv_priv_nc *priv_nc;
  623. struct batadv_priv *bat_priv;
  624. unsigned long timeout;
  625. delayed_work = to_delayed_work(work);
  626. priv_nc = container_of(delayed_work, struct batadv_priv_nc, work);
  627. bat_priv = container_of(priv_nc, struct batadv_priv, nc);
  628. batadv_nc_purge_orig_hash(bat_priv);
  629. batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash,
  630. batadv_nc_to_purge_nc_path_coding);
  631. batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash,
  632. batadv_nc_to_purge_nc_path_decoding);
  633. timeout = bat_priv->nc.max_fwd_delay;
  634. if (batadv_has_timed_out(bat_priv->nc.timestamp_fwd_flush, timeout)) {
  635. batadv_nc_process_nc_paths(bat_priv, bat_priv->nc.coding_hash,
  636. batadv_nc_fwd_flush);
  637. bat_priv->nc.timestamp_fwd_flush = jiffies;
  638. }
  639. if (batadv_has_timed_out(bat_priv->nc.timestamp_sniffed_purge,
  640. bat_priv->nc.max_buffer_time)) {
  641. batadv_nc_process_nc_paths(bat_priv, bat_priv->nc.decoding_hash,
  642. batadv_nc_sniffed_purge);
  643. bat_priv->nc.timestamp_sniffed_purge = jiffies;
  644. }
  645. /* Schedule a new check */
  646. batadv_nc_start_timer(bat_priv);
  647. }
  648. /**
  649. * batadv_can_nc_with_orig() - checks whether the given orig node is suitable
  650. * for coding or not
  651. * @bat_priv: the bat priv with all the soft interface information
  652. * @orig_node: neighboring orig node which may be used as nc candidate
  653. * @ogm_packet: incoming ogm packet also used for the checks
  654. *
  655. * Return: true if:
  656. * 1) The OGM must have the most recent sequence number.
  657. * 2) The TTL must be decremented by one and only one.
  658. * 3) The OGM must be received from the first hop from orig_node.
  659. * 4) The TQ value of the OGM must be above bat_priv->nc.min_tq.
  660. */
  661. static bool batadv_can_nc_with_orig(struct batadv_priv *bat_priv,
  662. struct batadv_orig_node *orig_node,
  663. struct batadv_ogm_packet *ogm_packet)
  664. {
  665. struct batadv_orig_ifinfo *orig_ifinfo;
  666. u32 last_real_seqno;
  667. u8 last_ttl;
  668. orig_ifinfo = batadv_orig_ifinfo_get(orig_node, BATADV_IF_DEFAULT);
  669. if (!orig_ifinfo)
  670. return false;
  671. last_ttl = orig_ifinfo->last_ttl;
  672. last_real_seqno = orig_ifinfo->last_real_seqno;
  673. batadv_orig_ifinfo_put(orig_ifinfo);
  674. if (last_real_seqno != ntohl(ogm_packet->seqno))
  675. return false;
  676. if (last_ttl != ogm_packet->ttl + 1)
  677. return false;
  678. if (!batadv_compare_eth(ogm_packet->orig, ogm_packet->prev_sender))
  679. return false;
  680. if (ogm_packet->tq < bat_priv->nc.min_tq)
  681. return false;
  682. return true;
  683. }
  684. /**
  685. * batadv_nc_find_nc_node() - search for an existing nc node and return it
  686. * @orig_node: orig node originating the ogm packet
  687. * @orig_neigh_node: neighboring orig node from which we received the ogm packet
  688. * (can be equal to orig_node)
  689. * @in_coding: traverse incoming or outgoing network coding list
  690. *
  691. * Return: the nc_node if found, NULL otherwise.
  692. */
  693. static struct batadv_nc_node *
  694. batadv_nc_find_nc_node(struct batadv_orig_node *orig_node,
  695. struct batadv_orig_node *orig_neigh_node,
  696. bool in_coding)
  697. {
  698. struct batadv_nc_node *nc_node, *nc_node_out = NULL;
  699. struct list_head *list;
  700. if (in_coding)
  701. list = &orig_neigh_node->in_coding_list;
  702. else
  703. list = &orig_neigh_node->out_coding_list;
  704. /* Traverse list of nc_nodes to orig_node */
  705. rcu_read_lock();
  706. list_for_each_entry_rcu(nc_node, list, list) {
  707. if (!batadv_compare_eth(nc_node->addr, orig_node->orig))
  708. continue;
  709. if (!kref_get_unless_zero(&nc_node->refcount))
  710. continue;
  711. /* Found a match */
  712. nc_node_out = nc_node;
  713. break;
  714. }
  715. rcu_read_unlock();
  716. return nc_node_out;
  717. }
  718. /**
  719. * batadv_nc_get_nc_node() - retrieves an nc node or creates the entry if it was
  720. * not found
  721. * @bat_priv: the bat priv with all the soft interface information
  722. * @orig_node: orig node originating the ogm packet
  723. * @orig_neigh_node: neighboring orig node from which we received the ogm packet
  724. * (can be equal to orig_node)
  725. * @in_coding: traverse incoming or outgoing network coding list
  726. *
  727. * Return: the nc_node if found or created, NULL in case of an error.
  728. */
  729. static struct batadv_nc_node *
  730. batadv_nc_get_nc_node(struct batadv_priv *bat_priv,
  731. struct batadv_orig_node *orig_node,
  732. struct batadv_orig_node *orig_neigh_node,
  733. bool in_coding)
  734. {
  735. struct batadv_nc_node *nc_node;
  736. spinlock_t *lock; /* Used to lock list selected by "int in_coding" */
  737. struct list_head *list;
  738. /* Select ingoing or outgoing coding node */
  739. if (in_coding) {
  740. lock = &orig_neigh_node->in_coding_list_lock;
  741. list = &orig_neigh_node->in_coding_list;
  742. } else {
  743. lock = &orig_neigh_node->out_coding_list_lock;
  744. list = &orig_neigh_node->out_coding_list;
  745. }
  746. spin_lock_bh(lock);
  747. /* Check if nc_node is already added */
  748. nc_node = batadv_nc_find_nc_node(orig_node, orig_neigh_node, in_coding);
  749. /* Node found */
  750. if (nc_node)
  751. goto unlock;
  752. nc_node = kzalloc(sizeof(*nc_node), GFP_ATOMIC);
  753. if (!nc_node)
  754. goto unlock;
  755. /* Initialize nc_node */
  756. INIT_LIST_HEAD(&nc_node->list);
  757. kref_init(&nc_node->refcount);
  758. ether_addr_copy(nc_node->addr, orig_node->orig);
  759. kref_get(&orig_neigh_node->refcount);
  760. nc_node->orig_node = orig_neigh_node;
  761. batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_node %pM -> %pM\n",
  762. nc_node->addr, nc_node->orig_node->orig);
  763. /* Add nc_node to orig_node */
  764. kref_get(&nc_node->refcount);
  765. list_add_tail_rcu(&nc_node->list, list);
  766. unlock:
  767. spin_unlock_bh(lock);
  768. return nc_node;
  769. }
  770. /**
  771. * batadv_nc_update_nc_node() - updates stored incoming and outgoing nc node
  772. * structs (best called on incoming OGMs)
  773. * @bat_priv: the bat priv with all the soft interface information
  774. * @orig_node: orig node originating the ogm packet
  775. * @orig_neigh_node: neighboring orig node from which we received the ogm packet
  776. * (can be equal to orig_node)
  777. * @ogm_packet: incoming ogm packet
  778. * @is_single_hop_neigh: orig_node is a single hop neighbor
  779. */
  780. void batadv_nc_update_nc_node(struct batadv_priv *bat_priv,
  781. struct batadv_orig_node *orig_node,
  782. struct batadv_orig_node *orig_neigh_node,
  783. struct batadv_ogm_packet *ogm_packet,
  784. int is_single_hop_neigh)
  785. {
  786. struct batadv_nc_node *in_nc_node = NULL;
  787. struct batadv_nc_node *out_nc_node = NULL;
  788. /* Check if network coding is enabled */
  789. if (!atomic_read(&bat_priv->network_coding))
  790. goto out;
  791. /* check if orig node is network coding enabled */
  792. if (!test_bit(BATADV_ORIG_CAPA_HAS_NC, &orig_node->capabilities))
  793. goto out;
  794. /* accept ogms from 'good' neighbors and single hop neighbors */
  795. if (!batadv_can_nc_with_orig(bat_priv, orig_node, ogm_packet) &&
  796. !is_single_hop_neigh)
  797. goto out;
  798. /* Add orig_node as in_nc_node on hop */
  799. in_nc_node = batadv_nc_get_nc_node(bat_priv, orig_node,
  800. orig_neigh_node, true);
  801. if (!in_nc_node)
  802. goto out;
  803. in_nc_node->last_seen = jiffies;
  804. /* Add hop as out_nc_node on orig_node */
  805. out_nc_node = batadv_nc_get_nc_node(bat_priv, orig_neigh_node,
  806. orig_node, false);
  807. if (!out_nc_node)
  808. goto out;
  809. out_nc_node->last_seen = jiffies;
  810. out:
  811. batadv_nc_node_put(in_nc_node);
  812. batadv_nc_node_put(out_nc_node);
  813. }
  814. /**
  815. * batadv_nc_get_path() - get existing nc_path or allocate a new one
  816. * @bat_priv: the bat priv with all the soft interface information
  817. * @hash: hash table containing the nc path
  818. * @src: ethernet source address - first half of the nc path search key
  819. * @dst: ethernet destination address - second half of the nc path search key
  820. *
  821. * Return: pointer to nc_path if the path was found or created, returns NULL
  822. * on error.
  823. */
  824. static struct batadv_nc_path *batadv_nc_get_path(struct batadv_priv *bat_priv,
  825. struct batadv_hashtable *hash,
  826. u8 *src,
  827. u8 *dst)
  828. {
  829. int hash_added;
  830. struct batadv_nc_path *nc_path, nc_path_key;
  831. batadv_nc_hash_key_gen(&nc_path_key, src, dst);
  832. /* Search for existing nc_path */
  833. nc_path = batadv_nc_hash_find(hash, (void *)&nc_path_key);
  834. if (nc_path) {
  835. /* Set timestamp to delay removal of nc_path */
  836. nc_path->last_valid = jiffies;
  837. return nc_path;
  838. }
  839. /* No existing nc_path was found; create a new */
  840. nc_path = kzalloc(sizeof(*nc_path), GFP_ATOMIC);
  841. if (!nc_path)
  842. return NULL;
  843. /* Initialize nc_path */
  844. INIT_LIST_HEAD(&nc_path->packet_list);
  845. spin_lock_init(&nc_path->packet_list_lock);
  846. kref_init(&nc_path->refcount);
  847. nc_path->last_valid = jiffies;
  848. ether_addr_copy(nc_path->next_hop, dst);
  849. ether_addr_copy(nc_path->prev_hop, src);
  850. batadv_dbg(BATADV_DBG_NC, bat_priv, "Adding nc_path %pM -> %pM\n",
  851. nc_path->prev_hop,
  852. nc_path->next_hop);
  853. /* Add nc_path to hash table */
  854. kref_get(&nc_path->refcount);
  855. hash_added = batadv_hash_add(hash, batadv_nc_hash_compare,
  856. batadv_nc_hash_choose, &nc_path_key,
  857. &nc_path->hash_entry);
  858. if (hash_added < 0) {
  859. kfree(nc_path);
  860. return NULL;
  861. }
  862. return nc_path;
  863. }
  864. /**
  865. * batadv_nc_random_weight_tq() - scale the receivers TQ-value to avoid unfair
  866. * selection of a receiver with slightly lower TQ than the other
  867. * @tq: to be weighted tq value
  868. *
  869. * Return: scaled tq value
  870. */
  871. static u8 batadv_nc_random_weight_tq(u8 tq)
  872. {
  873. /* randomize the estimated packet loss (max TQ - estimated TQ) */
  874. u8 rand_tq = get_random_u32_below(BATADV_TQ_MAX_VALUE + 1 - tq);
  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, *first_dest;
  913. struct batadv_neigh_node *router_coding = NULL, *second_dest;
  914. struct batadv_neigh_ifinfo *router_neigh_ifinfo = NULL;
  915. struct batadv_neigh_ifinfo *router_coding_ifinfo = NULL;
  916. u8 *first_source, *second_source;
  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->neigh_node;
  954. first_source = nc_packet->nc_path->prev_hop;
  955. second_dest = neigh_node;
  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;
  965. first_source = ethhdr->h_source;
  966. second_dest = nc_packet->neigh_node;
  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->addr);
  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. consume_skb(skb_src);
  1046. /* avoid duplicate free of skb from nc_packet */
  1047. nc_packet->skb = NULL;
  1048. batadv_nc_packet_free(nc_packet, false);
  1049. /* Send the coded packet and return true */
  1050. batadv_send_unicast_skb(skb_dest, first_dest);
  1051. res = true;
  1052. out:
  1053. batadv_neigh_node_put(router_neigh);
  1054. batadv_neigh_node_put(router_coding);
  1055. batadv_neigh_ifinfo_put(router_neigh_ifinfo);
  1056. batadv_neigh_ifinfo_put(router_coding_ifinfo);
  1057. return res;
  1058. }
  1059. /**
  1060. * batadv_nc_skb_coding_possible() - true if a decoded skb is available at dst.
  1061. * @skb: data skb to forward
  1062. * @dst: destination mac address of the other skb to code with
  1063. * @src: source mac address of skb
  1064. *
  1065. * Whenever we network code a packet we have to check whether we received it in
  1066. * a network coded form. If so, we may not be able to use it for coding because
  1067. * some neighbors may also have received (overheard) the packet in the network
  1068. * coded form without being able to decode it. It is hard to know which of the
  1069. * neighboring nodes was able to decode the packet, therefore we can only
  1070. * re-code the packet if the source of the previous encoded packet is involved.
  1071. * Since the source encoded the packet we can be certain it has all necessary
  1072. * decode information.
  1073. *
  1074. * Return: true if coding of a decoded packet is allowed.
  1075. */
  1076. static bool batadv_nc_skb_coding_possible(struct sk_buff *skb, u8 *dst, u8 *src)
  1077. {
  1078. if (BATADV_SKB_CB(skb)->decoded && !batadv_compare_eth(dst, src))
  1079. return false;
  1080. return true;
  1081. }
  1082. /**
  1083. * batadv_nc_path_search() - Find the coding path matching in_nc_node and
  1084. * out_nc_node to retrieve a buffered packet that can be used for coding.
  1085. * @bat_priv: the bat priv with all the soft interface information
  1086. * @in_nc_node: pointer to skb next hop's neighbor nc node
  1087. * @out_nc_node: pointer to skb source's neighbor nc node
  1088. * @skb: data skb to forward
  1089. * @eth_dst: next hop mac address of skb
  1090. *
  1091. * Return: true if coding of a decoded skb is allowed.
  1092. */
  1093. static struct batadv_nc_packet *
  1094. batadv_nc_path_search(struct batadv_priv *bat_priv,
  1095. struct batadv_nc_node *in_nc_node,
  1096. struct batadv_nc_node *out_nc_node,
  1097. struct sk_buff *skb,
  1098. u8 *eth_dst)
  1099. {
  1100. struct batadv_nc_path *nc_path, nc_path_key;
  1101. struct batadv_nc_packet *nc_packet_out = NULL;
  1102. struct batadv_nc_packet *nc_packet, *nc_packet_tmp;
  1103. struct batadv_hashtable *hash = bat_priv->nc.coding_hash;
  1104. int idx;
  1105. if (!hash)
  1106. return NULL;
  1107. /* Create almost path key */
  1108. batadv_nc_hash_key_gen(&nc_path_key, in_nc_node->addr,
  1109. out_nc_node->addr);
  1110. idx = batadv_nc_hash_choose(&nc_path_key, hash->size);
  1111. /* Check for coding opportunities in this nc_path */
  1112. rcu_read_lock();
  1113. hlist_for_each_entry_rcu(nc_path, &hash->table[idx], hash_entry) {
  1114. if (!batadv_compare_eth(nc_path->prev_hop, in_nc_node->addr))
  1115. continue;
  1116. if (!batadv_compare_eth(nc_path->next_hop, out_nc_node->addr))
  1117. continue;
  1118. spin_lock_bh(&nc_path->packet_list_lock);
  1119. if (list_empty(&nc_path->packet_list)) {
  1120. spin_unlock_bh(&nc_path->packet_list_lock);
  1121. continue;
  1122. }
  1123. list_for_each_entry_safe(nc_packet, nc_packet_tmp,
  1124. &nc_path->packet_list, list) {
  1125. if (!batadv_nc_skb_coding_possible(nc_packet->skb,
  1126. eth_dst,
  1127. in_nc_node->addr))
  1128. continue;
  1129. /* Coding opportunity is found! */
  1130. list_del(&nc_packet->list);
  1131. nc_packet_out = nc_packet;
  1132. break;
  1133. }
  1134. spin_unlock_bh(&nc_path->packet_list_lock);
  1135. break;
  1136. }
  1137. rcu_read_unlock();
  1138. return nc_packet_out;
  1139. }
  1140. /**
  1141. * batadv_nc_skb_src_search() - Loops through the list of neighboring nodes of
  1142. * the skb's sender (may be equal to the originator).
  1143. * @bat_priv: the bat priv with all the soft interface information
  1144. * @skb: data skb to forward
  1145. * @eth_dst: next hop mac address of skb
  1146. * @eth_src: source mac address of skb
  1147. * @in_nc_node: pointer to skb next hop's neighbor nc node
  1148. *
  1149. * Return: an nc packet if a suitable coding packet was found, NULL otherwise.
  1150. */
  1151. static struct batadv_nc_packet *
  1152. batadv_nc_skb_src_search(struct batadv_priv *bat_priv,
  1153. struct sk_buff *skb,
  1154. u8 *eth_dst,
  1155. u8 *eth_src,
  1156. struct batadv_nc_node *in_nc_node)
  1157. {
  1158. struct batadv_orig_node *orig_node;
  1159. struct batadv_nc_node *out_nc_node;
  1160. struct batadv_nc_packet *nc_packet = NULL;
  1161. orig_node = batadv_orig_hash_find(bat_priv, eth_src);
  1162. if (!orig_node)
  1163. return NULL;
  1164. rcu_read_lock();
  1165. list_for_each_entry_rcu(out_nc_node,
  1166. &orig_node->out_coding_list, list) {
  1167. /* Check if the skb is decoded and if recoding is possible */
  1168. if (!batadv_nc_skb_coding_possible(skb,
  1169. out_nc_node->addr, eth_src))
  1170. continue;
  1171. /* Search for an opportunity in this nc_path */
  1172. nc_packet = batadv_nc_path_search(bat_priv, in_nc_node,
  1173. out_nc_node, skb, eth_dst);
  1174. if (nc_packet)
  1175. break;
  1176. }
  1177. rcu_read_unlock();
  1178. batadv_orig_node_put(orig_node);
  1179. return nc_packet;
  1180. }
  1181. /**
  1182. * batadv_nc_skb_store_before_coding() - set the ethernet src and dst of the
  1183. * unicast skb before it is stored for use in later decoding
  1184. * @bat_priv: the bat priv with all the soft interface information
  1185. * @skb: data skb to store
  1186. * @eth_dst_new: new destination mac address of skb
  1187. */
  1188. static void batadv_nc_skb_store_before_coding(struct batadv_priv *bat_priv,
  1189. struct sk_buff *skb,
  1190. u8 *eth_dst_new)
  1191. {
  1192. struct ethhdr *ethhdr;
  1193. /* Copy skb header to change the mac header */
  1194. skb = pskb_copy_for_clone(skb, GFP_ATOMIC);
  1195. if (!skb)
  1196. return;
  1197. /* Set the mac header as if we actually sent the packet uncoded */
  1198. ethhdr = eth_hdr(skb);
  1199. ether_addr_copy(ethhdr->h_source, ethhdr->h_dest);
  1200. ether_addr_copy(ethhdr->h_dest, eth_dst_new);
  1201. /* Set data pointer to MAC header to mimic packets from our tx path */
  1202. skb_push(skb, ETH_HLEN);
  1203. /* Add the packet to the decoding packet pool */
  1204. batadv_nc_skb_store_for_decoding(bat_priv, skb);
  1205. /* batadv_nc_skb_store_for_decoding() clones the skb, so we must free
  1206. * our ref
  1207. */
  1208. consume_skb(skb);
  1209. }
  1210. /**
  1211. * batadv_nc_skb_dst_search() - Loops through list of neighboring nodes to dst.
  1212. * @skb: data skb to forward
  1213. * @neigh_node: next hop to forward packet to
  1214. * @ethhdr: pointer to the ethernet header inside the skb
  1215. *
  1216. * Loops through the list of neighboring nodes the next hop has a good
  1217. * connection to (receives OGMs with a sufficient quality). We need to find a
  1218. * neighbor of our next hop that potentially sent a packet which our next hop
  1219. * also received (overheard) and has stored for later decoding.
  1220. *
  1221. * Return: true if the skb was consumed (encoded packet sent) or false otherwise
  1222. */
  1223. static bool batadv_nc_skb_dst_search(struct sk_buff *skb,
  1224. struct batadv_neigh_node *neigh_node,
  1225. struct ethhdr *ethhdr)
  1226. {
  1227. struct net_device *netdev = neigh_node->if_incoming->soft_iface;
  1228. struct batadv_priv *bat_priv = netdev_priv(netdev);
  1229. struct batadv_orig_node *orig_node = neigh_node->orig_node;
  1230. struct batadv_nc_node *nc_node;
  1231. struct batadv_nc_packet *nc_packet = NULL;
  1232. rcu_read_lock();
  1233. list_for_each_entry_rcu(nc_node, &orig_node->in_coding_list, list) {
  1234. /* Search for coding opportunity with this in_nc_node */
  1235. nc_packet = batadv_nc_skb_src_search(bat_priv, skb,
  1236. neigh_node->addr,
  1237. ethhdr->h_source, nc_node);
  1238. /* Opportunity was found, so stop searching */
  1239. if (nc_packet)
  1240. break;
  1241. }
  1242. rcu_read_unlock();
  1243. if (!nc_packet)
  1244. return false;
  1245. /* Save packets for later decoding */
  1246. batadv_nc_skb_store_before_coding(bat_priv, skb,
  1247. neigh_node->addr);
  1248. batadv_nc_skb_store_before_coding(bat_priv, nc_packet->skb,
  1249. nc_packet->neigh_node->addr);
  1250. /* Code and send packets */
  1251. if (batadv_nc_code_packets(bat_priv, skb, ethhdr, nc_packet,
  1252. neigh_node))
  1253. return true;
  1254. /* out of mem ? Coding failed - we have to free the buffered packet
  1255. * to avoid memleaks. The skb passed as argument will be dealt with
  1256. * by the calling function.
  1257. */
  1258. batadv_nc_send_packet(nc_packet);
  1259. return false;
  1260. }
  1261. /**
  1262. * batadv_nc_skb_add_to_path() - buffer skb for later encoding / decoding
  1263. * @skb: skb to add to path
  1264. * @nc_path: path to add skb to
  1265. * @neigh_node: next hop to forward packet to
  1266. * @packet_id: checksum to identify packet
  1267. *
  1268. * Return: true if the packet was buffered or false in case of an error.
  1269. */
  1270. static bool batadv_nc_skb_add_to_path(struct sk_buff *skb,
  1271. struct batadv_nc_path *nc_path,
  1272. struct batadv_neigh_node *neigh_node,
  1273. __be32 packet_id)
  1274. {
  1275. struct batadv_nc_packet *nc_packet;
  1276. nc_packet = kzalloc(sizeof(*nc_packet), GFP_ATOMIC);
  1277. if (!nc_packet)
  1278. return false;
  1279. /* Initialize nc_packet */
  1280. nc_packet->timestamp = jiffies;
  1281. nc_packet->packet_id = packet_id;
  1282. nc_packet->skb = skb;
  1283. nc_packet->neigh_node = neigh_node;
  1284. nc_packet->nc_path = nc_path;
  1285. /* Add coding packet to list */
  1286. spin_lock_bh(&nc_path->packet_list_lock);
  1287. list_add_tail(&nc_packet->list, &nc_path->packet_list);
  1288. spin_unlock_bh(&nc_path->packet_list_lock);
  1289. return true;
  1290. }
  1291. /**
  1292. * batadv_nc_skb_forward() - try to code a packet or add it to the coding packet
  1293. * buffer
  1294. * @skb: data skb to forward
  1295. * @neigh_node: next hop to forward packet to
  1296. *
  1297. * Return: true if the skb was consumed (encoded packet sent) or false otherwise
  1298. */
  1299. bool batadv_nc_skb_forward(struct sk_buff *skb,
  1300. struct batadv_neigh_node *neigh_node)
  1301. {
  1302. const struct net_device *netdev = neigh_node->if_incoming->soft_iface;
  1303. struct batadv_priv *bat_priv = netdev_priv(netdev);
  1304. struct batadv_unicast_packet *packet;
  1305. struct batadv_nc_path *nc_path;
  1306. struct ethhdr *ethhdr = eth_hdr(skb);
  1307. __be32 packet_id;
  1308. u8 *payload;
  1309. /* Check if network coding is enabled */
  1310. if (!atomic_read(&bat_priv->network_coding))
  1311. goto out;
  1312. /* We only handle unicast packets */
  1313. payload = skb_network_header(skb);
  1314. packet = (struct batadv_unicast_packet *)payload;
  1315. if (packet->packet_type != BATADV_UNICAST)
  1316. goto out;
  1317. /* Try to find a coding opportunity and send the skb if one is found */
  1318. if (batadv_nc_skb_dst_search(skb, neigh_node, ethhdr))
  1319. return true;
  1320. /* Find or create a nc_path for this src-dst pair */
  1321. nc_path = batadv_nc_get_path(bat_priv,
  1322. bat_priv->nc.coding_hash,
  1323. ethhdr->h_source,
  1324. neigh_node->addr);
  1325. if (!nc_path)
  1326. goto out;
  1327. /* Add skb to nc_path */
  1328. packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet));
  1329. if (!batadv_nc_skb_add_to_path(skb, nc_path, neigh_node, packet_id))
  1330. goto free_nc_path;
  1331. /* Packet is consumed */
  1332. return true;
  1333. free_nc_path:
  1334. batadv_nc_path_put(nc_path);
  1335. out:
  1336. /* Packet is not consumed */
  1337. return false;
  1338. }
  1339. /**
  1340. * batadv_nc_skb_store_for_decoding() - save a clone of the skb which can be
  1341. * used when decoding coded packets
  1342. * @bat_priv: the bat priv with all the soft interface information
  1343. * @skb: data skb to store
  1344. */
  1345. void batadv_nc_skb_store_for_decoding(struct batadv_priv *bat_priv,
  1346. struct sk_buff *skb)
  1347. {
  1348. struct batadv_unicast_packet *packet;
  1349. struct batadv_nc_path *nc_path;
  1350. struct ethhdr *ethhdr = eth_hdr(skb);
  1351. __be32 packet_id;
  1352. u8 *payload;
  1353. /* Check if network coding is enabled */
  1354. if (!atomic_read(&bat_priv->network_coding))
  1355. goto out;
  1356. /* Check for supported packet type */
  1357. payload = skb_network_header(skb);
  1358. packet = (struct batadv_unicast_packet *)payload;
  1359. if (packet->packet_type != BATADV_UNICAST)
  1360. goto out;
  1361. /* Find existing nc_path or create a new */
  1362. nc_path = batadv_nc_get_path(bat_priv,
  1363. bat_priv->nc.decoding_hash,
  1364. ethhdr->h_source,
  1365. ethhdr->h_dest);
  1366. if (!nc_path)
  1367. goto out;
  1368. /* Clone skb and adjust skb->data to point at batman header */
  1369. skb = skb_clone(skb, GFP_ATOMIC);
  1370. if (unlikely(!skb))
  1371. goto free_nc_path;
  1372. if (unlikely(!pskb_may_pull(skb, ETH_HLEN)))
  1373. goto free_skb;
  1374. if (unlikely(!skb_pull_rcsum(skb, ETH_HLEN)))
  1375. goto free_skb;
  1376. /* Add skb to nc_path */
  1377. packet_id = batadv_skb_crc32(skb, payload + sizeof(*packet));
  1378. if (!batadv_nc_skb_add_to_path(skb, nc_path, NULL, packet_id))
  1379. goto free_skb;
  1380. batadv_inc_counter(bat_priv, BATADV_CNT_NC_BUFFER);
  1381. return;
  1382. free_skb:
  1383. kfree_skb(skb);
  1384. free_nc_path:
  1385. batadv_nc_path_put(nc_path);
  1386. out:
  1387. return;
  1388. }
  1389. /**
  1390. * batadv_nc_skb_store_sniffed_unicast() - check if a received unicast packet
  1391. * should be saved in the decoding buffer and, if so, store it there
  1392. * @bat_priv: the bat priv with all the soft interface information
  1393. * @skb: unicast skb to store
  1394. */
  1395. void batadv_nc_skb_store_sniffed_unicast(struct batadv_priv *bat_priv,
  1396. struct sk_buff *skb)
  1397. {
  1398. struct ethhdr *ethhdr = eth_hdr(skb);
  1399. if (batadv_is_my_mac(bat_priv, ethhdr->h_dest))
  1400. return;
  1401. /* Set data pointer to MAC header to mimic packets from our tx path */
  1402. skb_push(skb, ETH_HLEN);
  1403. batadv_nc_skb_store_for_decoding(bat_priv, skb);
  1404. }
  1405. /**
  1406. * batadv_nc_skb_decode_packet() - decode given skb using the decode data stored
  1407. * in nc_packet
  1408. * @bat_priv: the bat priv with all the soft interface information
  1409. * @skb: unicast skb to decode
  1410. * @nc_packet: decode data needed to decode the skb
  1411. *
  1412. * Return: pointer to decoded unicast packet if the packet was decoded or NULL
  1413. * in case of an error.
  1414. */
  1415. static struct batadv_unicast_packet *
  1416. batadv_nc_skb_decode_packet(struct batadv_priv *bat_priv, struct sk_buff *skb,
  1417. struct batadv_nc_packet *nc_packet)
  1418. {
  1419. const int h_size = sizeof(struct batadv_unicast_packet);
  1420. const int h_diff = sizeof(struct batadv_coded_packet) - h_size;
  1421. struct batadv_unicast_packet *unicast_packet;
  1422. struct batadv_coded_packet coded_packet_tmp;
  1423. struct ethhdr *ethhdr, ethhdr_tmp;
  1424. u8 *orig_dest, ttl, ttvn;
  1425. unsigned int coding_len;
  1426. int err;
  1427. /* Save headers temporarily */
  1428. memcpy(&coded_packet_tmp, skb->data, sizeof(coded_packet_tmp));
  1429. memcpy(&ethhdr_tmp, skb_mac_header(skb), sizeof(ethhdr_tmp));
  1430. if (skb_cow(skb, 0) < 0)
  1431. return NULL;
  1432. if (unlikely(!skb_pull_rcsum(skb, h_diff)))
  1433. return NULL;
  1434. /* Data points to batman header, so set mac header 14 bytes before
  1435. * and network to data
  1436. */
  1437. skb_set_mac_header(skb, -ETH_HLEN);
  1438. skb_reset_network_header(skb);
  1439. /* Reconstruct original mac header */
  1440. ethhdr = eth_hdr(skb);
  1441. *ethhdr = ethhdr_tmp;
  1442. /* Select the correct unicast header information based on the location
  1443. * of our mac address in the coded_packet header
  1444. */
  1445. if (batadv_is_my_mac(bat_priv, coded_packet_tmp.second_dest)) {
  1446. /* If we are the second destination the packet was overheard,
  1447. * so the Ethernet address must be copied to h_dest and
  1448. * pkt_type changed from PACKET_OTHERHOST to PACKET_HOST
  1449. */
  1450. ether_addr_copy(ethhdr->h_dest, coded_packet_tmp.second_dest);
  1451. skb->pkt_type = PACKET_HOST;
  1452. orig_dest = coded_packet_tmp.second_orig_dest;
  1453. ttl = coded_packet_tmp.second_ttl;
  1454. ttvn = coded_packet_tmp.second_ttvn;
  1455. } else {
  1456. orig_dest = coded_packet_tmp.first_orig_dest;
  1457. ttl = coded_packet_tmp.ttl;
  1458. ttvn = coded_packet_tmp.first_ttvn;
  1459. }
  1460. coding_len = ntohs(coded_packet_tmp.coded_len);
  1461. /* ensure dst buffer is large enough (payload only) */
  1462. if (coding_len + h_size > skb->len)
  1463. return NULL;
  1464. /* ensure src buffer is large enough (payload only) */
  1465. if (coding_len + h_size > nc_packet->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, false);
  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. goto free_skb;
  1566. /* Make sure we can access (and remove) header */
  1567. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  1568. goto free_skb;
  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. goto free_skb;
  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. goto free_skb;
  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, true);
  1603. free_skb:
  1604. kfree_skb(skb);
  1605. return NET_RX_DROP;
  1606. }
  1607. /**
  1608. * batadv_nc_mesh_free() - clean up network coding memory
  1609. * @bat_priv: the bat priv with all the soft interface information
  1610. */
  1611. void batadv_nc_mesh_free(struct batadv_priv *bat_priv)
  1612. {
  1613. batadv_tvlv_container_unregister(bat_priv, BATADV_TVLV_NC, 1);
  1614. batadv_tvlv_handler_unregister(bat_priv, BATADV_TVLV_NC, 1);
  1615. cancel_delayed_work_sync(&bat_priv->nc.work);
  1616. batadv_nc_purge_paths(bat_priv, bat_priv->nc.coding_hash, NULL);
  1617. batadv_hash_destroy(bat_priv->nc.coding_hash);
  1618. batadv_nc_purge_paths(bat_priv, bat_priv->nc.decoding_hash, NULL);
  1619. batadv_hash_destroy(bat_priv->nc.decoding_hash);
  1620. }