network-coding.c 59 KB

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