bat_iv_ogm.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010
  1. /* Copyright (C) 2007-2015 B.A.T.M.A.N. contributors:
  2. *
  3. * Marek Lindner, Simon Wunderlich
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "bat_algo.h"
  18. #include "main.h"
  19. #include <linux/atomic.h>
  20. #include <linux/bitmap.h>
  21. #include <linux/bitops.h>
  22. #include <linux/bug.h>
  23. #include <linux/byteorder/generic.h>
  24. #include <linux/cache.h>
  25. #include <linux/errno.h>
  26. #include <linux/etherdevice.h>
  27. #include <linux/fs.h>
  28. #include <linux/if_ether.h>
  29. #include <linux/init.h>
  30. #include <linux/jiffies.h>
  31. #include <linux/list.h>
  32. #include <linux/netdevice.h>
  33. #include <linux/pkt_sched.h>
  34. #include <linux/printk.h>
  35. #include <linux/random.h>
  36. #include <linux/rculist.h>
  37. #include <linux/rcupdate.h>
  38. #include <linux/seq_file.h>
  39. #include <linux/skbuff.h>
  40. #include <linux/slab.h>
  41. #include <linux/spinlock.h>
  42. #include <linux/stddef.h>
  43. #include <linux/string.h>
  44. #include <linux/types.h>
  45. #include <linux/workqueue.h>
  46. #include "bitarray.h"
  47. #include "hard-interface.h"
  48. #include "hash.h"
  49. #include "network-coding.h"
  50. #include "originator.h"
  51. #include "packet.h"
  52. #include "routing.h"
  53. #include "send.h"
  54. #include "translation-table.h"
  55. /**
  56. * enum batadv_dup_status - duplicate status
  57. * @BATADV_NO_DUP: the packet is no duplicate
  58. * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
  59. * neighbor)
  60. * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
  61. * @BATADV_PROTECTED: originator is currently protected (after reboot)
  62. */
  63. enum batadv_dup_status {
  64. BATADV_NO_DUP = 0,
  65. BATADV_ORIG_DUP,
  66. BATADV_NEIGH_DUP,
  67. BATADV_PROTECTED,
  68. };
  69. /**
  70. * batadv_ring_buffer_set - update the ring buffer with the given value
  71. * @lq_recv: pointer to the ring buffer
  72. * @lq_index: index to store the value at
  73. * @value: value to store in the ring buffer
  74. */
  75. static void batadv_ring_buffer_set(uint8_t lq_recv[], uint8_t *lq_index,
  76. uint8_t value)
  77. {
  78. lq_recv[*lq_index] = value;
  79. *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
  80. }
  81. /**
  82. * batadv_ring_buffer_avg - compute the average of all non-zero values stored
  83. * in the given ring buffer
  84. * @lq_recv: pointer to the ring buffer
  85. *
  86. * Returns computed average value.
  87. */
  88. static uint8_t batadv_ring_buffer_avg(const uint8_t lq_recv[])
  89. {
  90. const uint8_t *ptr;
  91. uint16_t count = 0;
  92. uint16_t i = 0;
  93. uint16_t sum = 0;
  94. ptr = lq_recv;
  95. while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
  96. if (*ptr != 0) {
  97. count++;
  98. sum += *ptr;
  99. }
  100. i++;
  101. ptr++;
  102. }
  103. if (count == 0)
  104. return 0;
  105. return (uint8_t)(sum / count);
  106. }
  107. /**
  108. * batadv_iv_ogm_orig_free - free the private resources allocated for this
  109. * orig_node
  110. * @orig_node: the orig_node for which the resources have to be free'd
  111. */
  112. static void batadv_iv_ogm_orig_free(struct batadv_orig_node *orig_node)
  113. {
  114. kfree(orig_node->bat_iv.bcast_own);
  115. kfree(orig_node->bat_iv.bcast_own_sum);
  116. }
  117. /**
  118. * batadv_iv_ogm_orig_add_if - change the private structures of the orig_node to
  119. * include the new hard-interface
  120. * @orig_node: the orig_node that has to be changed
  121. * @max_if_num: the current amount of interfaces
  122. *
  123. * Returns 0 on success, a negative error code otherwise.
  124. */
  125. static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node,
  126. int max_if_num)
  127. {
  128. void *data_ptr;
  129. size_t old_size;
  130. int ret = -ENOMEM;
  131. spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  132. old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
  133. data_ptr = kmalloc_array(max_if_num,
  134. BATADV_NUM_WORDS * sizeof(unsigned long),
  135. GFP_ATOMIC);
  136. if (!data_ptr)
  137. goto unlock;
  138. memcpy(data_ptr, orig_node->bat_iv.bcast_own, old_size);
  139. kfree(orig_node->bat_iv.bcast_own);
  140. orig_node->bat_iv.bcast_own = data_ptr;
  141. data_ptr = kmalloc_array(max_if_num, sizeof(uint8_t), GFP_ATOMIC);
  142. if (!data_ptr) {
  143. kfree(orig_node->bat_iv.bcast_own);
  144. goto unlock;
  145. }
  146. memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
  147. (max_if_num - 1) * sizeof(uint8_t));
  148. kfree(orig_node->bat_iv.bcast_own_sum);
  149. orig_node->bat_iv.bcast_own_sum = data_ptr;
  150. ret = 0;
  151. unlock:
  152. spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  153. return ret;
  154. }
  155. /**
  156. * batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to
  157. * exclude the removed interface
  158. * @orig_node: the orig_node that has to be changed
  159. * @max_if_num: the current amount of interfaces
  160. * @del_if_num: the index of the interface being removed
  161. *
  162. * Returns 0 on success, a negative error code otherwise.
  163. */
  164. static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node,
  165. int max_if_num, int del_if_num)
  166. {
  167. int chunk_size, ret = -ENOMEM, if_offset;
  168. void *data_ptr = NULL;
  169. spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  170. /* last interface was removed */
  171. if (max_if_num == 0)
  172. goto free_bcast_own;
  173. chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
  174. data_ptr = kmalloc_array(max_if_num, chunk_size, GFP_ATOMIC);
  175. if (!data_ptr)
  176. goto unlock;
  177. /* copy first part */
  178. memcpy(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size);
  179. /* copy second part */
  180. memcpy((char *)data_ptr + del_if_num * chunk_size,
  181. orig_node->bat_iv.bcast_own + ((del_if_num + 1) * chunk_size),
  182. (max_if_num - del_if_num) * chunk_size);
  183. free_bcast_own:
  184. kfree(orig_node->bat_iv.bcast_own);
  185. orig_node->bat_iv.bcast_own = data_ptr;
  186. if (max_if_num == 0)
  187. goto free_own_sum;
  188. data_ptr = kmalloc_array(max_if_num, sizeof(uint8_t), GFP_ATOMIC);
  189. if (!data_ptr) {
  190. kfree(orig_node->bat_iv.bcast_own);
  191. goto unlock;
  192. }
  193. memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
  194. del_if_num * sizeof(uint8_t));
  195. if_offset = (del_if_num + 1) * sizeof(uint8_t);
  196. memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t),
  197. orig_node->bat_iv.bcast_own_sum + if_offset,
  198. (max_if_num - del_if_num) * sizeof(uint8_t));
  199. free_own_sum:
  200. kfree(orig_node->bat_iv.bcast_own_sum);
  201. orig_node->bat_iv.bcast_own_sum = data_ptr;
  202. ret = 0;
  203. unlock:
  204. spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  205. return ret;
  206. }
  207. /**
  208. * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator
  209. * @bat_priv: the bat priv with all the soft interface information
  210. * @addr: mac address of the originator
  211. *
  212. * Returns the originator object corresponding to the passed mac address or NULL
  213. * on failure.
  214. * If the object does not exists it is created an initialised.
  215. */
  216. static struct batadv_orig_node *
  217. batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const uint8_t *addr)
  218. {
  219. struct batadv_orig_node *orig_node;
  220. int size, hash_added;
  221. orig_node = batadv_orig_hash_find(bat_priv, addr);
  222. if (orig_node)
  223. return orig_node;
  224. orig_node = batadv_orig_node_new(bat_priv, addr);
  225. if (!orig_node)
  226. return NULL;
  227. spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
  228. size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
  229. orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC);
  230. if (!orig_node->bat_iv.bcast_own)
  231. goto free_orig_node;
  232. size = bat_priv->num_ifaces * sizeof(uint8_t);
  233. orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC);
  234. if (!orig_node->bat_iv.bcast_own_sum)
  235. goto free_orig_node;
  236. hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
  237. batadv_choose_orig, orig_node,
  238. &orig_node->hash_entry);
  239. if (hash_added != 0)
  240. goto free_orig_node;
  241. return orig_node;
  242. free_orig_node:
  243. /* free twice, as batadv_orig_node_new sets refcount to 2 */
  244. batadv_orig_node_free_ref(orig_node);
  245. batadv_orig_node_free_ref(orig_node);
  246. return NULL;
  247. }
  248. static struct batadv_neigh_node *
  249. batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
  250. const uint8_t *neigh_addr,
  251. struct batadv_orig_node *orig_node,
  252. struct batadv_orig_node *orig_neigh)
  253. {
  254. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  255. struct batadv_neigh_node *neigh_node, *tmp_neigh_node;
  256. neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr, orig_node);
  257. if (!neigh_node)
  258. goto out;
  259. if (!atomic_inc_not_zero(&hard_iface->refcount)) {
  260. kfree(neigh_node);
  261. neigh_node = NULL;
  262. goto out;
  263. }
  264. neigh_node->orig_node = orig_neigh;
  265. neigh_node->if_incoming = hard_iface;
  266. spin_lock_bh(&orig_node->neigh_list_lock);
  267. tmp_neigh_node = batadv_neigh_node_get(orig_node, hard_iface,
  268. neigh_addr);
  269. if (!tmp_neigh_node) {
  270. hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
  271. } else {
  272. kfree(neigh_node);
  273. batadv_hardif_free_ref(hard_iface);
  274. neigh_node = tmp_neigh_node;
  275. }
  276. spin_unlock_bh(&orig_node->neigh_list_lock);
  277. if (!tmp_neigh_node)
  278. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  279. "Creating new neighbor %pM for orig_node %pM on interface %s\n",
  280. neigh_addr, orig_node->orig,
  281. hard_iface->net_dev->name);
  282. out:
  283. return neigh_node;
  284. }
  285. static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
  286. {
  287. struct batadv_ogm_packet *batadv_ogm_packet;
  288. unsigned char *ogm_buff;
  289. uint32_t random_seqno;
  290. /* randomize initial seqno to avoid collision */
  291. get_random_bytes(&random_seqno, sizeof(random_seqno));
  292. atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
  293. hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
  294. ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
  295. if (!ogm_buff)
  296. return -ENOMEM;
  297. hard_iface->bat_iv.ogm_buff = ogm_buff;
  298. batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
  299. batadv_ogm_packet->packet_type = BATADV_IV_OGM;
  300. batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
  301. batadv_ogm_packet->ttl = 2;
  302. batadv_ogm_packet->flags = BATADV_NO_FLAGS;
  303. batadv_ogm_packet->reserved = 0;
  304. batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
  305. return 0;
  306. }
  307. static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
  308. {
  309. kfree(hard_iface->bat_iv.ogm_buff);
  310. hard_iface->bat_iv.ogm_buff = NULL;
  311. }
  312. static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
  313. {
  314. struct batadv_ogm_packet *batadv_ogm_packet;
  315. unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
  316. batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
  317. ether_addr_copy(batadv_ogm_packet->orig,
  318. hard_iface->net_dev->dev_addr);
  319. ether_addr_copy(batadv_ogm_packet->prev_sender,
  320. hard_iface->net_dev->dev_addr);
  321. }
  322. static void
  323. batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
  324. {
  325. struct batadv_ogm_packet *batadv_ogm_packet;
  326. unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
  327. batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
  328. batadv_ogm_packet->flags = BATADV_PRIMARIES_FIRST_HOP;
  329. batadv_ogm_packet->ttl = BATADV_TTL;
  330. }
  331. /* when do we schedule our own ogm to be sent */
  332. static unsigned long
  333. batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
  334. {
  335. unsigned int msecs;
  336. msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
  337. msecs += prandom_u32() % (2 * BATADV_JITTER);
  338. return jiffies + msecs_to_jiffies(msecs);
  339. }
  340. /* when do we schedule a ogm packet to be sent */
  341. static unsigned long batadv_iv_ogm_fwd_send_time(void)
  342. {
  343. return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
  344. }
  345. /* apply hop penalty for a normal link */
  346. static uint8_t batadv_hop_penalty(uint8_t tq,
  347. const struct batadv_priv *bat_priv)
  348. {
  349. int hop_penalty = atomic_read(&bat_priv->hop_penalty);
  350. int new_tq;
  351. new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
  352. new_tq /= BATADV_TQ_MAX_VALUE;
  353. return new_tq;
  354. }
  355. /* is there another aggregated packet here? */
  356. static bool batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
  357. __be16 tvlv_len)
  358. {
  359. int next_buff_pos = 0;
  360. next_buff_pos += buff_pos + BATADV_OGM_HLEN;
  361. next_buff_pos += ntohs(tvlv_len);
  362. return (next_buff_pos <= packet_len) &&
  363. (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
  364. }
  365. /* send a batman ogm to a given interface */
  366. static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
  367. struct batadv_hard_iface *hard_iface)
  368. {
  369. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  370. const char *fwd_str;
  371. uint8_t packet_num;
  372. int16_t buff_pos;
  373. struct batadv_ogm_packet *batadv_ogm_packet;
  374. struct sk_buff *skb;
  375. uint8_t *packet_pos;
  376. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  377. return;
  378. packet_num = 0;
  379. buff_pos = 0;
  380. packet_pos = forw_packet->skb->data;
  381. batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
  382. /* adjust all flags and log packets */
  383. while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
  384. batadv_ogm_packet->tvlv_len)) {
  385. /* we might have aggregated direct link packets with an
  386. * ordinary base packet
  387. */
  388. if (forw_packet->direct_link_flags & BIT(packet_num) &&
  389. forw_packet->if_incoming == hard_iface)
  390. batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
  391. else
  392. batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
  393. if (packet_num > 0 || !forw_packet->own)
  394. fwd_str = "Forwarding";
  395. else
  396. fwd_str = "Sending own";
  397. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  398. "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
  399. fwd_str, (packet_num > 0 ? "aggregated " : ""),
  400. batadv_ogm_packet->orig,
  401. ntohl(batadv_ogm_packet->seqno),
  402. batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
  403. ((batadv_ogm_packet->flags & BATADV_DIRECTLINK) ?
  404. "on" : "off"),
  405. hard_iface->net_dev->name,
  406. hard_iface->net_dev->dev_addr);
  407. buff_pos += BATADV_OGM_HLEN;
  408. buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
  409. packet_num++;
  410. packet_pos = forw_packet->skb->data + buff_pos;
  411. batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
  412. }
  413. /* create clone because function is called more than once */
  414. skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
  415. if (skb) {
  416. batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
  417. batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
  418. skb->len + ETH_HLEN);
  419. batadv_send_skb_packet(skb, hard_iface, batadv_broadcast_addr);
  420. }
  421. }
  422. /* send a batman ogm packet */
  423. static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
  424. {
  425. struct net_device *soft_iface;
  426. struct batadv_priv *bat_priv;
  427. struct batadv_hard_iface *primary_if = NULL;
  428. if (!forw_packet->if_incoming) {
  429. pr_err("Error - can't forward packet: incoming iface not specified\n");
  430. goto out;
  431. }
  432. soft_iface = forw_packet->if_incoming->soft_iface;
  433. bat_priv = netdev_priv(soft_iface);
  434. if (WARN_ON(!forw_packet->if_outgoing))
  435. goto out;
  436. if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface))
  437. goto out;
  438. if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
  439. goto out;
  440. primary_if = batadv_primary_if_get_selected(bat_priv);
  441. if (!primary_if)
  442. goto out;
  443. /* only for one specific outgoing interface */
  444. batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
  445. out:
  446. if (primary_if)
  447. batadv_hardif_free_ref(primary_if);
  448. }
  449. /**
  450. * batadv_iv_ogm_can_aggregate - find out if an OGM can be aggregated on an
  451. * existing forward packet
  452. * @new_bat_ogm_packet: OGM packet to be aggregated
  453. * @bat_priv: the bat priv with all the soft interface information
  454. * @packet_len: (total) length of the OGM
  455. * @send_time: timestamp (jiffies) when the packet is to be sent
  456. * @directlink: true if this is a direct link packet
  457. * @if_incoming: interface where the packet was received
  458. * @if_outgoing: interface for which the retransmission should be considered
  459. * @forw_packet: the forwarded packet which should be checked
  460. *
  461. * Returns true if new_packet can be aggregated with forw_packet
  462. */
  463. static bool
  464. batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
  465. struct batadv_priv *bat_priv,
  466. int packet_len, unsigned long send_time,
  467. bool directlink,
  468. const struct batadv_hard_iface *if_incoming,
  469. const struct batadv_hard_iface *if_outgoing,
  470. const struct batadv_forw_packet *forw_packet)
  471. {
  472. struct batadv_ogm_packet *batadv_ogm_packet;
  473. int aggregated_bytes = forw_packet->packet_len + packet_len;
  474. struct batadv_hard_iface *primary_if = NULL;
  475. bool res = false;
  476. unsigned long aggregation_end_time;
  477. batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
  478. aggregation_end_time = send_time;
  479. aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
  480. /* we can aggregate the current packet to this aggregated packet
  481. * if:
  482. *
  483. * - the send time is within our MAX_AGGREGATION_MS time
  484. * - the resulting packet wont be bigger than
  485. * MAX_AGGREGATION_BYTES
  486. * otherwise aggregation is not possible
  487. */
  488. if (!time_before(send_time, forw_packet->send_time) ||
  489. !time_after_eq(aggregation_end_time, forw_packet->send_time))
  490. return false;
  491. if (aggregated_bytes > BATADV_MAX_AGGREGATION_BYTES)
  492. return false;
  493. /* packet is not leaving on the same interface. */
  494. if (forw_packet->if_outgoing != if_outgoing)
  495. return false;
  496. /* check aggregation compatibility
  497. * -> direct link packets are broadcasted on
  498. * their interface only
  499. * -> aggregate packet if the current packet is
  500. * a "global" packet as well as the base
  501. * packet
  502. */
  503. primary_if = batadv_primary_if_get_selected(bat_priv);
  504. if (!primary_if)
  505. return false;
  506. /* packets without direct link flag and high TTL
  507. * are flooded through the net
  508. */
  509. if (!directlink &&
  510. !(batadv_ogm_packet->flags & BATADV_DIRECTLINK) &&
  511. batadv_ogm_packet->ttl != 1 &&
  512. /* own packets originating non-primary
  513. * interfaces leave only that interface
  514. */
  515. (!forw_packet->own ||
  516. forw_packet->if_incoming == primary_if)) {
  517. res = true;
  518. goto out;
  519. }
  520. /* if the incoming packet is sent via this one
  521. * interface only - we still can aggregate
  522. */
  523. if (directlink &&
  524. new_bat_ogm_packet->ttl == 1 &&
  525. forw_packet->if_incoming == if_incoming &&
  526. /* packets from direct neighbors or
  527. * own secondary interface packets
  528. * (= secondary interface packets in general)
  529. */
  530. (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
  531. (forw_packet->own &&
  532. forw_packet->if_incoming != primary_if))) {
  533. res = true;
  534. goto out;
  535. }
  536. out:
  537. if (primary_if)
  538. batadv_hardif_free_ref(primary_if);
  539. return res;
  540. }
  541. /**
  542. * batadv_iv_ogm_aggregate_new - create a new aggregated packet and add this
  543. * packet to it.
  544. * @packet_buff: pointer to the OGM
  545. * @packet_len: (total) length of the OGM
  546. * @send_time: timestamp (jiffies) when the packet is to be sent
  547. * @direct_link: whether this OGM has direct link status
  548. * @if_incoming: interface where the packet was received
  549. * @if_outgoing: interface for which the retransmission should be considered
  550. * @own_packet: true if it is a self-generated ogm
  551. */
  552. static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
  553. int packet_len, unsigned long send_time,
  554. bool direct_link,
  555. struct batadv_hard_iface *if_incoming,
  556. struct batadv_hard_iface *if_outgoing,
  557. int own_packet)
  558. {
  559. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  560. struct batadv_forw_packet *forw_packet_aggr;
  561. unsigned char *skb_buff;
  562. unsigned int skb_size;
  563. if (!atomic_inc_not_zero(&if_incoming->refcount))
  564. return;
  565. if (!atomic_inc_not_zero(&if_outgoing->refcount))
  566. goto out_free_incoming;
  567. /* own packet should always be scheduled */
  568. if (!own_packet) {
  569. if (!batadv_atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
  570. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  571. "batman packet queue full\n");
  572. goto out_free_outgoing;
  573. }
  574. }
  575. forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
  576. if (!forw_packet_aggr)
  577. goto out_nomem;
  578. if (atomic_read(&bat_priv->aggregated_ogms) &&
  579. packet_len < BATADV_MAX_AGGREGATION_BYTES)
  580. skb_size = BATADV_MAX_AGGREGATION_BYTES;
  581. else
  582. skb_size = packet_len;
  583. skb_size += ETH_HLEN;
  584. forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
  585. if (!forw_packet_aggr->skb)
  586. goto out_free_forw_packet;
  587. forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
  588. skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
  589. skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
  590. forw_packet_aggr->packet_len = packet_len;
  591. memcpy(skb_buff, packet_buff, packet_len);
  592. forw_packet_aggr->own = own_packet;
  593. forw_packet_aggr->if_incoming = if_incoming;
  594. forw_packet_aggr->if_outgoing = if_outgoing;
  595. forw_packet_aggr->num_packets = 0;
  596. forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
  597. forw_packet_aggr->send_time = send_time;
  598. /* save packet direct link flag status */
  599. if (direct_link)
  600. forw_packet_aggr->direct_link_flags |= 1;
  601. /* add new packet to packet list */
  602. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  603. hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
  604. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  605. /* start timer for this packet */
  606. INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
  607. batadv_send_outstanding_bat_ogm_packet);
  608. queue_delayed_work(batadv_event_workqueue,
  609. &forw_packet_aggr->delayed_work,
  610. send_time - jiffies);
  611. return;
  612. out_free_forw_packet:
  613. kfree(forw_packet_aggr);
  614. out_nomem:
  615. if (!own_packet)
  616. atomic_inc(&bat_priv->batman_queue_left);
  617. out_free_outgoing:
  618. batadv_hardif_free_ref(if_outgoing);
  619. out_free_incoming:
  620. batadv_hardif_free_ref(if_incoming);
  621. }
  622. /* aggregate a new packet into the existing ogm packet */
  623. static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
  624. const unsigned char *packet_buff,
  625. int packet_len, bool direct_link)
  626. {
  627. unsigned char *skb_buff;
  628. unsigned long new_direct_link_flag;
  629. skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
  630. memcpy(skb_buff, packet_buff, packet_len);
  631. forw_packet_aggr->packet_len += packet_len;
  632. forw_packet_aggr->num_packets++;
  633. /* save packet direct link flag status */
  634. if (direct_link) {
  635. new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
  636. forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
  637. }
  638. }
  639. /**
  640. * batadv_iv_ogm_queue_add - queue up an OGM for transmission
  641. * @bat_priv: the bat priv with all the soft interface information
  642. * @packet_buff: pointer to the OGM
  643. * @packet_len: (total) length of the OGM
  644. * @if_incoming: interface where the packet was received
  645. * @if_outgoing: interface for which the retransmission should be considered
  646. * @own_packet: true if it is a self-generated ogm
  647. * @send_time: timestamp (jiffies) when the packet is to be sent
  648. */
  649. static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
  650. unsigned char *packet_buff,
  651. int packet_len,
  652. struct batadv_hard_iface *if_incoming,
  653. struct batadv_hard_iface *if_outgoing,
  654. int own_packet, unsigned long send_time)
  655. {
  656. /* _aggr -> pointer to the packet we want to aggregate with
  657. * _pos -> pointer to the position in the queue
  658. */
  659. struct batadv_forw_packet *forw_packet_aggr = NULL;
  660. struct batadv_forw_packet *forw_packet_pos = NULL;
  661. struct batadv_ogm_packet *batadv_ogm_packet;
  662. bool direct_link;
  663. unsigned long max_aggregation_jiffies;
  664. batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
  665. direct_link = !!(batadv_ogm_packet->flags & BATADV_DIRECTLINK);
  666. max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
  667. /* find position for the packet in the forward queue */
  668. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  669. /* own packets are not to be aggregated */
  670. if (atomic_read(&bat_priv->aggregated_ogms) && !own_packet) {
  671. hlist_for_each_entry(forw_packet_pos,
  672. &bat_priv->forw_bat_list, list) {
  673. if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
  674. bat_priv, packet_len,
  675. send_time, direct_link,
  676. if_incoming,
  677. if_outgoing,
  678. forw_packet_pos)) {
  679. forw_packet_aggr = forw_packet_pos;
  680. break;
  681. }
  682. }
  683. }
  684. /* nothing to aggregate with - either aggregation disabled or no
  685. * suitable aggregation packet found
  686. */
  687. if (!forw_packet_aggr) {
  688. /* the following section can run without the lock */
  689. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  690. /* if we could not aggregate this packet with one of the others
  691. * we hold it back for a while, so that it might be aggregated
  692. * later on
  693. */
  694. if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
  695. send_time += max_aggregation_jiffies;
  696. batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
  697. send_time, direct_link,
  698. if_incoming, if_outgoing,
  699. own_packet);
  700. } else {
  701. batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
  702. packet_len, direct_link);
  703. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  704. }
  705. }
  706. static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
  707. const struct ethhdr *ethhdr,
  708. struct batadv_ogm_packet *batadv_ogm_packet,
  709. bool is_single_hop_neigh,
  710. bool is_from_best_next_hop,
  711. struct batadv_hard_iface *if_incoming,
  712. struct batadv_hard_iface *if_outgoing)
  713. {
  714. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  715. uint16_t tvlv_len;
  716. if (batadv_ogm_packet->ttl <= 1) {
  717. batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
  718. return;
  719. }
  720. if (!is_from_best_next_hop) {
  721. /* Mark the forwarded packet when it is not coming from our
  722. * best next hop. We still need to forward the packet for our
  723. * neighbor link quality detection to work in case the packet
  724. * originated from a single hop neighbor. Otherwise we can
  725. * simply drop the ogm.
  726. */
  727. if (is_single_hop_neigh)
  728. batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
  729. else
  730. return;
  731. }
  732. tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
  733. batadv_ogm_packet->ttl--;
  734. ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
  735. /* apply hop penalty */
  736. batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
  737. bat_priv);
  738. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  739. "Forwarding packet: tq: %i, ttl: %i\n",
  740. batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
  741. /* switch of primaries first hop flag when forwarding */
  742. batadv_ogm_packet->flags &= ~BATADV_PRIMARIES_FIRST_HOP;
  743. if (is_single_hop_neigh)
  744. batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
  745. else
  746. batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
  747. batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
  748. BATADV_OGM_HLEN + tvlv_len,
  749. if_incoming, if_outgoing, 0,
  750. batadv_iv_ogm_fwd_send_time());
  751. }
  752. /**
  753. * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
  754. * the given interface
  755. * @hard_iface: the interface for which the windows have to be shifted
  756. */
  757. static void
  758. batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
  759. {
  760. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  761. struct batadv_hashtable *hash = bat_priv->orig_hash;
  762. struct hlist_head *head;
  763. struct batadv_orig_node *orig_node;
  764. unsigned long *word;
  765. uint32_t i;
  766. size_t word_index;
  767. uint8_t *w;
  768. int if_num;
  769. for (i = 0; i < hash->size; i++) {
  770. head = &hash->table[i];
  771. rcu_read_lock();
  772. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  773. spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  774. word_index = hard_iface->if_num * BATADV_NUM_WORDS;
  775. word = &orig_node->bat_iv.bcast_own[word_index];
  776. batadv_bit_get_packet(bat_priv, word, 1, 0);
  777. if_num = hard_iface->if_num;
  778. w = &orig_node->bat_iv.bcast_own_sum[if_num];
  779. *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
  780. spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  781. }
  782. rcu_read_unlock();
  783. }
  784. }
  785. static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
  786. {
  787. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  788. unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
  789. struct batadv_ogm_packet *batadv_ogm_packet;
  790. struct batadv_hard_iface *primary_if, *tmp_hard_iface;
  791. int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
  792. uint32_t seqno;
  793. uint16_t tvlv_len = 0;
  794. unsigned long send_time;
  795. primary_if = batadv_primary_if_get_selected(bat_priv);
  796. if (hard_iface == primary_if) {
  797. /* tt changes have to be committed before the tvlv data is
  798. * appended as it may alter the tt tvlv container
  799. */
  800. batadv_tt_local_commit_changes(bat_priv);
  801. tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
  802. ogm_buff_len,
  803. BATADV_OGM_HLEN);
  804. }
  805. batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
  806. batadv_ogm_packet->tvlv_len = htons(tvlv_len);
  807. /* change sequence number to network order */
  808. seqno = (uint32_t)atomic_read(&hard_iface->bat_iv.ogm_seqno);
  809. batadv_ogm_packet->seqno = htonl(seqno);
  810. atomic_inc(&hard_iface->bat_iv.ogm_seqno);
  811. batadv_iv_ogm_slide_own_bcast_window(hard_iface);
  812. send_time = batadv_iv_ogm_emit_send_time(bat_priv);
  813. if (hard_iface != primary_if) {
  814. /* OGMs from secondary interfaces are only scheduled on their
  815. * respective interfaces.
  816. */
  817. batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
  818. hard_iface, hard_iface, 1, send_time);
  819. goto out;
  820. }
  821. /* OGMs from primary interfaces are scheduled on all
  822. * interfaces.
  823. */
  824. rcu_read_lock();
  825. list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
  826. if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
  827. continue;
  828. batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
  829. *ogm_buff_len, hard_iface,
  830. tmp_hard_iface, 1, send_time);
  831. }
  832. rcu_read_unlock();
  833. out:
  834. if (primary_if)
  835. batadv_hardif_free_ref(primary_if);
  836. }
  837. /**
  838. * batadv_iv_ogm_orig_update - use OGM to update corresponding data in an
  839. * originator
  840. * @bat_priv: the bat priv with all the soft interface information
  841. * @orig_node: the orig node who originally emitted the ogm packet
  842. * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
  843. * @ethhdr: Ethernet header of the OGM
  844. * @batadv_ogm_packet: the ogm packet
  845. * @if_incoming: interface where the packet was received
  846. * @if_outgoing: interface for which the retransmission should be considered
  847. * @dup_status: the duplicate status of this ogm packet.
  848. */
  849. static void
  850. batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
  851. struct batadv_orig_node *orig_node,
  852. struct batadv_orig_ifinfo *orig_ifinfo,
  853. const struct ethhdr *ethhdr,
  854. const struct batadv_ogm_packet *batadv_ogm_packet,
  855. struct batadv_hard_iface *if_incoming,
  856. struct batadv_hard_iface *if_outgoing,
  857. enum batadv_dup_status dup_status)
  858. {
  859. struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
  860. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  861. struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
  862. struct batadv_neigh_node *router = NULL;
  863. struct batadv_orig_node *orig_node_tmp;
  864. int if_num;
  865. uint8_t sum_orig, sum_neigh;
  866. uint8_t *neigh_addr;
  867. uint8_t tq_avg;
  868. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  869. "update_originator(): Searching and updating originator entry of received packet\n");
  870. rcu_read_lock();
  871. hlist_for_each_entry_rcu(tmp_neigh_node,
  872. &orig_node->neigh_list, list) {
  873. neigh_addr = tmp_neigh_node->addr;
  874. if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
  875. tmp_neigh_node->if_incoming == if_incoming &&
  876. atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
  877. if (WARN(neigh_node, "too many matching neigh_nodes"))
  878. batadv_neigh_node_free_ref(neigh_node);
  879. neigh_node = tmp_neigh_node;
  880. continue;
  881. }
  882. if (dup_status != BATADV_NO_DUP)
  883. continue;
  884. /* only update the entry for this outgoing interface */
  885. neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
  886. if_outgoing);
  887. if (!neigh_ifinfo)
  888. continue;
  889. spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
  890. batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
  891. &neigh_ifinfo->bat_iv.tq_index, 0);
  892. tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
  893. neigh_ifinfo->bat_iv.tq_avg = tq_avg;
  894. spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
  895. batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
  896. neigh_ifinfo = NULL;
  897. }
  898. if (!neigh_node) {
  899. struct batadv_orig_node *orig_tmp;
  900. orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
  901. if (!orig_tmp)
  902. goto unlock;
  903. neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
  904. ethhdr->h_source,
  905. orig_node, orig_tmp);
  906. batadv_orig_node_free_ref(orig_tmp);
  907. if (!neigh_node)
  908. goto unlock;
  909. } else {
  910. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  911. "Updating existing last-hop neighbor of originator\n");
  912. }
  913. rcu_read_unlock();
  914. neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
  915. if (!neigh_ifinfo)
  916. goto out;
  917. neigh_node->last_seen = jiffies;
  918. spin_lock_bh(&neigh_node->ifinfo_lock);
  919. batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
  920. &neigh_ifinfo->bat_iv.tq_index,
  921. batadv_ogm_packet->tq);
  922. tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
  923. neigh_ifinfo->bat_iv.tq_avg = tq_avg;
  924. spin_unlock_bh(&neigh_node->ifinfo_lock);
  925. if (dup_status == BATADV_NO_DUP) {
  926. orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
  927. neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
  928. }
  929. /* if this neighbor already is our next hop there is nothing
  930. * to change
  931. */
  932. router = batadv_orig_router_get(orig_node, if_outgoing);
  933. if (router == neigh_node)
  934. goto out;
  935. if (router) {
  936. router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
  937. if (!router_ifinfo)
  938. goto out;
  939. /* if this neighbor does not offer a better TQ we won't
  940. * consider it
  941. */
  942. if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
  943. goto out;
  944. }
  945. /* if the TQ is the same and the link not more symmetric we
  946. * won't consider it either
  947. */
  948. if (router_ifinfo &&
  949. neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg) {
  950. orig_node_tmp = router->orig_node;
  951. spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
  952. if_num = router->if_incoming->if_num;
  953. sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
  954. spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
  955. orig_node_tmp = neigh_node->orig_node;
  956. spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
  957. if_num = neigh_node->if_incoming->if_num;
  958. sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
  959. spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
  960. if (sum_orig >= sum_neigh)
  961. goto out;
  962. }
  963. batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
  964. goto out;
  965. unlock:
  966. rcu_read_unlock();
  967. out:
  968. if (neigh_node)
  969. batadv_neigh_node_free_ref(neigh_node);
  970. if (router)
  971. batadv_neigh_node_free_ref(router);
  972. if (neigh_ifinfo)
  973. batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
  974. if (router_ifinfo)
  975. batadv_neigh_ifinfo_free_ref(router_ifinfo);
  976. }
  977. /**
  978. * batadv_iv_ogm_calc_tq - calculate tq for current received ogm packet
  979. * @orig_node: the orig node who originally emitted the ogm packet
  980. * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
  981. * @batadv_ogm_packet: the ogm packet
  982. * @if_incoming: interface where the packet was received
  983. * @if_outgoing: interface for which the retransmission should be considered
  984. *
  985. * Returns 1 if the link can be considered bidirectional, 0 otherwise
  986. */
  987. static int batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
  988. struct batadv_orig_node *orig_neigh_node,
  989. struct batadv_ogm_packet *batadv_ogm_packet,
  990. struct batadv_hard_iface *if_incoming,
  991. struct batadv_hard_iface *if_outgoing)
  992. {
  993. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  994. struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
  995. struct batadv_neigh_ifinfo *neigh_ifinfo;
  996. uint8_t total_count;
  997. uint8_t orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
  998. unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
  999. int tq_asym_penalty, inv_asym_penalty, if_num, ret = 0;
  1000. unsigned int combined_tq;
  1001. int tq_iface_penalty;
  1002. /* find corresponding one hop neighbor */
  1003. rcu_read_lock();
  1004. hlist_for_each_entry_rcu(tmp_neigh_node,
  1005. &orig_neigh_node->neigh_list, list) {
  1006. if (!batadv_compare_eth(tmp_neigh_node->addr,
  1007. orig_neigh_node->orig))
  1008. continue;
  1009. if (tmp_neigh_node->if_incoming != if_incoming)
  1010. continue;
  1011. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  1012. continue;
  1013. neigh_node = tmp_neigh_node;
  1014. break;
  1015. }
  1016. rcu_read_unlock();
  1017. if (!neigh_node)
  1018. neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
  1019. orig_neigh_node->orig,
  1020. orig_neigh_node,
  1021. orig_neigh_node);
  1022. if (!neigh_node)
  1023. goto out;
  1024. /* if orig_node is direct neighbor update neigh_node last_seen */
  1025. if (orig_node == orig_neigh_node)
  1026. neigh_node->last_seen = jiffies;
  1027. orig_node->last_seen = jiffies;
  1028. /* find packet count of corresponding one hop neighbor */
  1029. spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  1030. if_num = if_incoming->if_num;
  1031. orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num];
  1032. neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
  1033. if (neigh_ifinfo) {
  1034. neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
  1035. batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
  1036. } else {
  1037. neigh_rq_count = 0;
  1038. }
  1039. spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  1040. /* pay attention to not get a value bigger than 100 % */
  1041. if (orig_eq_count > neigh_rq_count)
  1042. total_count = neigh_rq_count;
  1043. else
  1044. total_count = orig_eq_count;
  1045. /* if we have too few packets (too less data) we set tq_own to zero
  1046. * if we receive too few packets it is not considered bidirectional
  1047. */
  1048. if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
  1049. neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
  1050. tq_own = 0;
  1051. else
  1052. /* neigh_node->real_packet_count is never zero as we
  1053. * only purge old information when getting new
  1054. * information
  1055. */
  1056. tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
  1057. /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
  1058. * affect the nearly-symmetric links only a little, but
  1059. * punishes asymmetric links more. This will give a value
  1060. * between 0 and TQ_MAX_VALUE
  1061. */
  1062. neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
  1063. neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
  1064. neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
  1065. BATADV_TQ_LOCAL_WINDOW_SIZE *
  1066. BATADV_TQ_LOCAL_WINDOW_SIZE;
  1067. inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
  1068. inv_asym_penalty /= neigh_rq_max_cube;
  1069. tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
  1070. /* penalize if the OGM is forwarded on the same interface. WiFi
  1071. * interfaces and other half duplex devices suffer from throughput
  1072. * drops as they can't send and receive at the same time.
  1073. */
  1074. tq_iface_penalty = BATADV_TQ_MAX_VALUE;
  1075. if (if_outgoing && (if_incoming == if_outgoing) &&
  1076. batadv_is_wifi_netdev(if_outgoing->net_dev))
  1077. tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE,
  1078. bat_priv);
  1079. combined_tq = batadv_ogm_packet->tq *
  1080. tq_own *
  1081. tq_asym_penalty *
  1082. tq_iface_penalty;
  1083. combined_tq /= BATADV_TQ_MAX_VALUE *
  1084. BATADV_TQ_MAX_VALUE *
  1085. BATADV_TQ_MAX_VALUE;
  1086. batadv_ogm_packet->tq = combined_tq;
  1087. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1088. "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, iface_penalty: %3i, total tq: %3i, if_incoming = %s, if_outgoing = %s\n",
  1089. orig_node->orig, orig_neigh_node->orig, total_count,
  1090. neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty,
  1091. batadv_ogm_packet->tq, if_incoming->net_dev->name,
  1092. if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
  1093. /* if link has the minimum required transmission quality
  1094. * consider it bidirectional
  1095. */
  1096. if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
  1097. ret = 1;
  1098. out:
  1099. if (neigh_node)
  1100. batadv_neigh_node_free_ref(neigh_node);
  1101. return ret;
  1102. }
  1103. /**
  1104. * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
  1105. * adjust the sequence number and find out whether it is a duplicate
  1106. * @ethhdr: ethernet header of the packet
  1107. * @batadv_ogm_packet: OGM packet to be considered
  1108. * @if_incoming: interface on which the OGM packet was received
  1109. * @if_outgoing: interface for which the retransmission should be considered
  1110. *
  1111. * Returns duplicate status as enum batadv_dup_status
  1112. */
  1113. static enum batadv_dup_status
  1114. batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
  1115. const struct batadv_ogm_packet *batadv_ogm_packet,
  1116. const struct batadv_hard_iface *if_incoming,
  1117. struct batadv_hard_iface *if_outgoing)
  1118. {
  1119. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  1120. struct batadv_orig_node *orig_node;
  1121. struct batadv_orig_ifinfo *orig_ifinfo = NULL;
  1122. struct batadv_neigh_node *neigh_node;
  1123. struct batadv_neigh_ifinfo *neigh_ifinfo;
  1124. int is_dup;
  1125. int32_t seq_diff;
  1126. int need_update = 0;
  1127. int set_mark;
  1128. enum batadv_dup_status ret = BATADV_NO_DUP;
  1129. uint32_t seqno = ntohl(batadv_ogm_packet->seqno);
  1130. uint8_t *neigh_addr;
  1131. uint8_t packet_count;
  1132. unsigned long *bitmap;
  1133. orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
  1134. if (!orig_node)
  1135. return BATADV_NO_DUP;
  1136. orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
  1137. if (WARN_ON(!orig_ifinfo)) {
  1138. batadv_orig_node_free_ref(orig_node);
  1139. return 0;
  1140. }
  1141. spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  1142. seq_diff = seqno - orig_ifinfo->last_real_seqno;
  1143. /* signalize caller that the packet is to be dropped. */
  1144. if (!hlist_empty(&orig_node->neigh_list) &&
  1145. batadv_window_protected(bat_priv, seq_diff,
  1146. &orig_ifinfo->batman_seqno_reset)) {
  1147. ret = BATADV_PROTECTED;
  1148. goto out;
  1149. }
  1150. rcu_read_lock();
  1151. hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
  1152. neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
  1153. if_outgoing);
  1154. if (!neigh_ifinfo)
  1155. continue;
  1156. neigh_addr = neigh_node->addr;
  1157. is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
  1158. orig_ifinfo->last_real_seqno,
  1159. seqno);
  1160. if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
  1161. neigh_node->if_incoming == if_incoming) {
  1162. set_mark = 1;
  1163. if (is_dup)
  1164. ret = BATADV_NEIGH_DUP;
  1165. } else {
  1166. set_mark = 0;
  1167. if (is_dup && (ret != BATADV_NEIGH_DUP))
  1168. ret = BATADV_ORIG_DUP;
  1169. }
  1170. /* if the window moved, set the update flag. */
  1171. bitmap = neigh_ifinfo->bat_iv.real_bits;
  1172. need_update |= batadv_bit_get_packet(bat_priv, bitmap,
  1173. seq_diff, set_mark);
  1174. packet_count = bitmap_weight(bitmap,
  1175. BATADV_TQ_LOCAL_WINDOW_SIZE);
  1176. neigh_ifinfo->bat_iv.real_packet_count = packet_count;
  1177. batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
  1178. }
  1179. rcu_read_unlock();
  1180. if (need_update) {
  1181. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1182. "%s updating last_seqno: old %u, new %u\n",
  1183. if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
  1184. orig_ifinfo->last_real_seqno, seqno);
  1185. orig_ifinfo->last_real_seqno = seqno;
  1186. }
  1187. out:
  1188. spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  1189. batadv_orig_node_free_ref(orig_node);
  1190. batadv_orig_ifinfo_free_ref(orig_ifinfo);
  1191. return ret;
  1192. }
  1193. /**
  1194. * batadv_iv_ogm_process_per_outif - process a batman iv OGM for an outgoing if
  1195. * @skb: the skb containing the OGM
  1196. * @ogm_offset: offset from skb->data to start of ogm header
  1197. * @orig_node: the (cached) orig node for the originator of this OGM
  1198. * @if_incoming: the interface where this packet was received
  1199. * @if_outgoing: the interface for which the packet should be considered
  1200. */
  1201. static void
  1202. batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
  1203. struct batadv_orig_node *orig_node,
  1204. struct batadv_hard_iface *if_incoming,
  1205. struct batadv_hard_iface *if_outgoing)
  1206. {
  1207. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  1208. struct batadv_neigh_node *router = NULL, *router_router = NULL;
  1209. struct batadv_orig_node *orig_neigh_node;
  1210. struct batadv_orig_ifinfo *orig_ifinfo;
  1211. struct batadv_neigh_node *orig_neigh_router = NULL;
  1212. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  1213. struct batadv_ogm_packet *ogm_packet;
  1214. enum batadv_dup_status dup_status;
  1215. bool is_from_best_next_hop = false;
  1216. bool is_single_hop_neigh = false;
  1217. bool sameseq, similar_ttl;
  1218. struct sk_buff *skb_priv;
  1219. struct ethhdr *ethhdr;
  1220. uint8_t *prev_sender;
  1221. int is_bidirect;
  1222. /* create a private copy of the skb, as some functions change tq value
  1223. * and/or flags.
  1224. */
  1225. skb_priv = skb_copy(skb, GFP_ATOMIC);
  1226. if (!skb_priv)
  1227. return;
  1228. ethhdr = eth_hdr(skb_priv);
  1229. ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
  1230. dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
  1231. if_incoming, if_outgoing);
  1232. if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
  1233. is_single_hop_neigh = true;
  1234. if (dup_status == BATADV_PROTECTED) {
  1235. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1236. "Drop packet: packet within seqno protection time (sender: %pM)\n",
  1237. ethhdr->h_source);
  1238. goto out;
  1239. }
  1240. if (ogm_packet->tq == 0) {
  1241. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1242. "Drop packet: originator packet with tq equal 0\n");
  1243. goto out;
  1244. }
  1245. router = batadv_orig_router_get(orig_node, if_outgoing);
  1246. if (router) {
  1247. router_router = batadv_orig_router_get(router->orig_node,
  1248. if_outgoing);
  1249. router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
  1250. }
  1251. if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
  1252. (batadv_compare_eth(router->addr, ethhdr->h_source)))
  1253. is_from_best_next_hop = true;
  1254. prev_sender = ogm_packet->prev_sender;
  1255. /* avoid temporary routing loops */
  1256. if (router && router_router &&
  1257. (batadv_compare_eth(router->addr, prev_sender)) &&
  1258. !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
  1259. (batadv_compare_eth(router->addr, router_router->addr))) {
  1260. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1261. "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
  1262. ethhdr->h_source);
  1263. goto out;
  1264. }
  1265. if (if_outgoing == BATADV_IF_DEFAULT)
  1266. batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
  1267. /* if sender is a direct neighbor the sender mac equals
  1268. * originator mac
  1269. */
  1270. if (is_single_hop_neigh)
  1271. orig_neigh_node = orig_node;
  1272. else
  1273. orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
  1274. ethhdr->h_source);
  1275. if (!orig_neigh_node)
  1276. goto out;
  1277. /* Update nc_nodes of the originator */
  1278. batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
  1279. ogm_packet, is_single_hop_neigh);
  1280. orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
  1281. if_outgoing);
  1282. /* drop packet if sender is not a direct neighbor and if we
  1283. * don't route towards it
  1284. */
  1285. if (!is_single_hop_neigh && (!orig_neigh_router)) {
  1286. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1287. "Drop packet: OGM via unknown neighbor!\n");
  1288. goto out_neigh;
  1289. }
  1290. is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
  1291. ogm_packet, if_incoming,
  1292. if_outgoing);
  1293. /* update ranking if it is not a duplicate or has the same
  1294. * seqno and similar ttl as the non-duplicate
  1295. */
  1296. orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
  1297. if (!orig_ifinfo)
  1298. goto out_neigh;
  1299. sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
  1300. similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
  1301. if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
  1302. (sameseq && similar_ttl))) {
  1303. batadv_iv_ogm_orig_update(bat_priv, orig_node,
  1304. orig_ifinfo, ethhdr,
  1305. ogm_packet, if_incoming,
  1306. if_outgoing, dup_status);
  1307. }
  1308. batadv_orig_ifinfo_free_ref(orig_ifinfo);
  1309. /* only forward for specific interface, not for the default one. */
  1310. if (if_outgoing == BATADV_IF_DEFAULT)
  1311. goto out_neigh;
  1312. /* is single hop (direct) neighbor */
  1313. if (is_single_hop_neigh) {
  1314. /* OGMs from secondary interfaces should only scheduled once
  1315. * per interface where it has been received, not multiple times
  1316. */
  1317. if ((ogm_packet->ttl <= 2) &&
  1318. (if_incoming != if_outgoing)) {
  1319. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1320. "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
  1321. goto out_neigh;
  1322. }
  1323. /* mark direct link on incoming interface */
  1324. batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
  1325. is_single_hop_neigh,
  1326. is_from_best_next_hop, if_incoming,
  1327. if_outgoing);
  1328. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1329. "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
  1330. goto out_neigh;
  1331. }
  1332. /* multihop originator */
  1333. if (!is_bidirect) {
  1334. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1335. "Drop packet: not received via bidirectional link\n");
  1336. goto out_neigh;
  1337. }
  1338. if (dup_status == BATADV_NEIGH_DUP) {
  1339. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1340. "Drop packet: duplicate packet received\n");
  1341. goto out_neigh;
  1342. }
  1343. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1344. "Forwarding packet: rebroadcast originator packet\n");
  1345. batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
  1346. is_single_hop_neigh, is_from_best_next_hop,
  1347. if_incoming, if_outgoing);
  1348. out_neigh:
  1349. if ((orig_neigh_node) && (!is_single_hop_neigh))
  1350. batadv_orig_node_free_ref(orig_neigh_node);
  1351. out:
  1352. if (router_ifinfo)
  1353. batadv_neigh_ifinfo_free_ref(router_ifinfo);
  1354. if (router)
  1355. batadv_neigh_node_free_ref(router);
  1356. if (router_router)
  1357. batadv_neigh_node_free_ref(router_router);
  1358. if (orig_neigh_router)
  1359. batadv_neigh_node_free_ref(orig_neigh_router);
  1360. kfree_skb(skb_priv);
  1361. }
  1362. /**
  1363. * batadv_iv_ogm_process - process an incoming batman iv OGM
  1364. * @skb: the skb containing the OGM
  1365. * @ogm_offset: offset to the OGM which should be processed (for aggregates)
  1366. * @if_incoming: the interface where this packet was receved
  1367. */
  1368. static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
  1369. struct batadv_hard_iface *if_incoming)
  1370. {
  1371. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  1372. struct batadv_orig_node *orig_neigh_node, *orig_node;
  1373. struct batadv_hard_iface *hard_iface;
  1374. struct batadv_ogm_packet *ogm_packet;
  1375. uint32_t if_incoming_seqno;
  1376. bool has_directlink_flag;
  1377. struct ethhdr *ethhdr;
  1378. bool is_my_oldorig = false;
  1379. bool is_my_addr = false;
  1380. bool is_my_orig = false;
  1381. ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
  1382. ethhdr = eth_hdr(skb);
  1383. /* Silently drop when the batman packet is actually not a
  1384. * correct packet.
  1385. *
  1386. * This might happen if a packet is padded (e.g. Ethernet has a
  1387. * minimum frame length of 64 byte) and the aggregation interprets
  1388. * it as an additional length.
  1389. *
  1390. * TODO: A more sane solution would be to have a bit in the
  1391. * batadv_ogm_packet to detect whether the packet is the last
  1392. * packet in an aggregation. Here we expect that the padding
  1393. * is always zero (or not 0x01)
  1394. */
  1395. if (ogm_packet->packet_type != BATADV_IV_OGM)
  1396. return;
  1397. /* could be changed by schedule_own_packet() */
  1398. if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
  1399. if (ogm_packet->flags & BATADV_DIRECTLINK)
  1400. has_directlink_flag = true;
  1401. else
  1402. has_directlink_flag = false;
  1403. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1404. "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, tq %d, TTL %d, V %d, IDF %d)\n",
  1405. ethhdr->h_source, if_incoming->net_dev->name,
  1406. if_incoming->net_dev->dev_addr, ogm_packet->orig,
  1407. ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
  1408. ogm_packet->tq, ogm_packet->ttl,
  1409. ogm_packet->version, has_directlink_flag);
  1410. rcu_read_lock();
  1411. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  1412. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  1413. continue;
  1414. if (hard_iface->soft_iface != if_incoming->soft_iface)
  1415. continue;
  1416. if (batadv_compare_eth(ethhdr->h_source,
  1417. hard_iface->net_dev->dev_addr))
  1418. is_my_addr = true;
  1419. if (batadv_compare_eth(ogm_packet->orig,
  1420. hard_iface->net_dev->dev_addr))
  1421. is_my_orig = true;
  1422. if (batadv_compare_eth(ogm_packet->prev_sender,
  1423. hard_iface->net_dev->dev_addr))
  1424. is_my_oldorig = true;
  1425. }
  1426. rcu_read_unlock();
  1427. if (is_my_addr) {
  1428. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1429. "Drop packet: received my own broadcast (sender: %pM)\n",
  1430. ethhdr->h_source);
  1431. return;
  1432. }
  1433. if (is_my_orig) {
  1434. unsigned long *word;
  1435. int offset;
  1436. int32_t bit_pos;
  1437. int16_t if_num;
  1438. uint8_t *weight;
  1439. orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
  1440. ethhdr->h_source);
  1441. if (!orig_neigh_node)
  1442. return;
  1443. /* neighbor has to indicate direct link and it has to
  1444. * come via the corresponding interface
  1445. * save packet seqno for bidirectional check
  1446. */
  1447. if (has_directlink_flag &&
  1448. batadv_compare_eth(if_incoming->net_dev->dev_addr,
  1449. ogm_packet->orig)) {
  1450. if_num = if_incoming->if_num;
  1451. offset = if_num * BATADV_NUM_WORDS;
  1452. spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
  1453. word = &orig_neigh_node->bat_iv.bcast_own[offset];
  1454. bit_pos = if_incoming_seqno - 2;
  1455. bit_pos -= ntohl(ogm_packet->seqno);
  1456. batadv_set_bit(word, bit_pos);
  1457. weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
  1458. *weight = bitmap_weight(word,
  1459. BATADV_TQ_LOCAL_WINDOW_SIZE);
  1460. spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
  1461. }
  1462. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1463. "Drop packet: originator packet from myself (via neighbor)\n");
  1464. batadv_orig_node_free_ref(orig_neigh_node);
  1465. return;
  1466. }
  1467. if (is_my_oldorig) {
  1468. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1469. "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
  1470. ethhdr->h_source);
  1471. return;
  1472. }
  1473. if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
  1474. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1475. "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
  1476. ethhdr->h_source);
  1477. return;
  1478. }
  1479. orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
  1480. if (!orig_node)
  1481. return;
  1482. batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
  1483. if_incoming, BATADV_IF_DEFAULT);
  1484. rcu_read_lock();
  1485. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  1486. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  1487. continue;
  1488. if (hard_iface->soft_iface != bat_priv->soft_iface)
  1489. continue;
  1490. batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
  1491. if_incoming, hard_iface);
  1492. }
  1493. rcu_read_unlock();
  1494. batadv_orig_node_free_ref(orig_node);
  1495. }
  1496. static int batadv_iv_ogm_receive(struct sk_buff *skb,
  1497. struct batadv_hard_iface *if_incoming)
  1498. {
  1499. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  1500. struct batadv_ogm_packet *ogm_packet;
  1501. uint8_t *packet_pos;
  1502. int ogm_offset;
  1503. bool ret;
  1504. ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
  1505. if (!ret)
  1506. return NET_RX_DROP;
  1507. /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
  1508. * that does not have B.A.T.M.A.N. IV enabled ?
  1509. */
  1510. if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit)
  1511. return NET_RX_DROP;
  1512. batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
  1513. batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
  1514. skb->len + ETH_HLEN);
  1515. ogm_offset = 0;
  1516. ogm_packet = (struct batadv_ogm_packet *)skb->data;
  1517. /* unpack the aggregated packets and process them one by one */
  1518. while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
  1519. ogm_packet->tvlv_len)) {
  1520. batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
  1521. ogm_offset += BATADV_OGM_HLEN;
  1522. ogm_offset += ntohs(ogm_packet->tvlv_len);
  1523. packet_pos = skb->data + ogm_offset;
  1524. ogm_packet = (struct batadv_ogm_packet *)packet_pos;
  1525. }
  1526. kfree_skb(skb);
  1527. return NET_RX_SUCCESS;
  1528. }
  1529. /**
  1530. * batadv_iv_ogm_orig_print_neigh - print neighbors for the originator table
  1531. * @orig_node: the orig_node for which the neighbors are printed
  1532. * @if_outgoing: outgoing interface for these entries
  1533. * @seq: debugfs table seq_file struct
  1534. *
  1535. * Must be called while holding an rcu lock.
  1536. */
  1537. static void
  1538. batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
  1539. struct batadv_hard_iface *if_outgoing,
  1540. struct seq_file *seq)
  1541. {
  1542. struct batadv_neigh_node *neigh_node;
  1543. struct batadv_neigh_ifinfo *n_ifinfo;
  1544. hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
  1545. n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
  1546. if (!n_ifinfo)
  1547. continue;
  1548. seq_printf(seq, " %pM (%3i)",
  1549. neigh_node->addr,
  1550. n_ifinfo->bat_iv.tq_avg);
  1551. batadv_neigh_ifinfo_free_ref(n_ifinfo);
  1552. }
  1553. }
  1554. /**
  1555. * batadv_iv_ogm_orig_print - print the originator table
  1556. * @bat_priv: the bat priv with all the soft interface information
  1557. * @seq: debugfs table seq_file struct
  1558. * @if_outgoing: the outgoing interface for which this should be printed
  1559. */
  1560. static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
  1561. struct seq_file *seq,
  1562. struct batadv_hard_iface *if_outgoing)
  1563. {
  1564. struct batadv_neigh_node *neigh_node;
  1565. struct batadv_hashtable *hash = bat_priv->orig_hash;
  1566. int last_seen_msecs, last_seen_secs;
  1567. struct batadv_orig_node *orig_node;
  1568. struct batadv_neigh_ifinfo *n_ifinfo;
  1569. unsigned long last_seen_jiffies;
  1570. struct hlist_head *head;
  1571. int batman_count = 0;
  1572. uint32_t i;
  1573. seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
  1574. "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE,
  1575. "Nexthop", "outgoingIF", "Potential nexthops");
  1576. for (i = 0; i < hash->size; i++) {
  1577. head = &hash->table[i];
  1578. rcu_read_lock();
  1579. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  1580. neigh_node = batadv_orig_router_get(orig_node,
  1581. if_outgoing);
  1582. if (!neigh_node)
  1583. continue;
  1584. n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
  1585. if_outgoing);
  1586. if (!n_ifinfo)
  1587. goto next;
  1588. if (n_ifinfo->bat_iv.tq_avg == 0)
  1589. goto next;
  1590. last_seen_jiffies = jiffies - orig_node->last_seen;
  1591. last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
  1592. last_seen_secs = last_seen_msecs / 1000;
  1593. last_seen_msecs = last_seen_msecs % 1000;
  1594. seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
  1595. orig_node->orig, last_seen_secs,
  1596. last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
  1597. neigh_node->addr,
  1598. neigh_node->if_incoming->net_dev->name);
  1599. batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing,
  1600. seq);
  1601. seq_puts(seq, "\n");
  1602. batman_count++;
  1603. next:
  1604. batadv_neigh_node_free_ref(neigh_node);
  1605. if (n_ifinfo)
  1606. batadv_neigh_ifinfo_free_ref(n_ifinfo);
  1607. }
  1608. rcu_read_unlock();
  1609. }
  1610. if (batman_count == 0)
  1611. seq_puts(seq, "No batman nodes in range ...\n");
  1612. }
  1613. /**
  1614. * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
  1615. * @neigh1: the first neighbor object of the comparison
  1616. * @if_outgoing1: outgoing interface for the first neighbor
  1617. * @neigh2: the second neighbor object of the comparison
  1618. * @if_outgoing2: outgoing interface for the second neighbor
  1619. *
  1620. * Returns a value less, equal to or greater than 0 if the metric via neigh1 is
  1621. * lower, the same as or higher than the metric via neigh2
  1622. */
  1623. static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
  1624. struct batadv_hard_iface *if_outgoing1,
  1625. struct batadv_neigh_node *neigh2,
  1626. struct batadv_hard_iface *if_outgoing2)
  1627. {
  1628. struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
  1629. uint8_t tq1, tq2;
  1630. int diff;
  1631. neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
  1632. neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
  1633. if (!neigh1_ifinfo || !neigh2_ifinfo) {
  1634. diff = 0;
  1635. goto out;
  1636. }
  1637. tq1 = neigh1_ifinfo->bat_iv.tq_avg;
  1638. tq2 = neigh2_ifinfo->bat_iv.tq_avg;
  1639. diff = tq1 - tq2;
  1640. out:
  1641. if (neigh1_ifinfo)
  1642. batadv_neigh_ifinfo_free_ref(neigh1_ifinfo);
  1643. if (neigh2_ifinfo)
  1644. batadv_neigh_ifinfo_free_ref(neigh2_ifinfo);
  1645. return diff;
  1646. }
  1647. /**
  1648. * batadv_iv_ogm_neigh_is_eob - check if neigh1 is equally good or better than
  1649. * neigh2 from the metric prospective
  1650. * @neigh1: the first neighbor object of the comparison
  1651. * @if_outgoing1: outgoing interface for the first neighbor
  1652. * @neigh2: the second neighbor object of the comparison
  1653. * @if_outgoing2: outgoing interface for the second neighbor
  1654. *
  1655. * Returns true if the metric via neigh1 is equally good or better than
  1656. * the metric via neigh2, false otherwise.
  1657. */
  1658. static bool
  1659. batadv_iv_ogm_neigh_is_eob(struct batadv_neigh_node *neigh1,
  1660. struct batadv_hard_iface *if_outgoing1,
  1661. struct batadv_neigh_node *neigh2,
  1662. struct batadv_hard_iface *if_outgoing2)
  1663. {
  1664. struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
  1665. uint8_t tq1, tq2;
  1666. bool ret;
  1667. neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
  1668. neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
  1669. /* we can't say that the metric is better */
  1670. if (!neigh1_ifinfo || !neigh2_ifinfo) {
  1671. ret = false;
  1672. goto out;
  1673. }
  1674. tq1 = neigh1_ifinfo->bat_iv.tq_avg;
  1675. tq2 = neigh2_ifinfo->bat_iv.tq_avg;
  1676. ret = (tq1 - tq2) > -BATADV_TQ_SIMILARITY_THRESHOLD;
  1677. out:
  1678. if (neigh1_ifinfo)
  1679. batadv_neigh_ifinfo_free_ref(neigh1_ifinfo);
  1680. if (neigh2_ifinfo)
  1681. batadv_neigh_ifinfo_free_ref(neigh2_ifinfo);
  1682. return ret;
  1683. }
  1684. static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
  1685. .name = "BATMAN_IV",
  1686. .bat_iface_enable = batadv_iv_ogm_iface_enable,
  1687. .bat_iface_disable = batadv_iv_ogm_iface_disable,
  1688. .bat_iface_update_mac = batadv_iv_ogm_iface_update_mac,
  1689. .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set,
  1690. .bat_ogm_schedule = batadv_iv_ogm_schedule,
  1691. .bat_ogm_emit = batadv_iv_ogm_emit,
  1692. .bat_neigh_cmp = batadv_iv_ogm_neigh_cmp,
  1693. .bat_neigh_is_equiv_or_better = batadv_iv_ogm_neigh_is_eob,
  1694. .bat_orig_print = batadv_iv_ogm_orig_print,
  1695. .bat_orig_free = batadv_iv_ogm_orig_free,
  1696. .bat_orig_add_if = batadv_iv_ogm_orig_add_if,
  1697. .bat_orig_del_if = batadv_iv_ogm_orig_del_if,
  1698. };
  1699. int __init batadv_iv_init(void)
  1700. {
  1701. int ret;
  1702. /* batman originator packet */
  1703. ret = batadv_recv_handler_register(BATADV_IV_OGM,
  1704. batadv_iv_ogm_receive);
  1705. if (ret < 0)
  1706. goto out;
  1707. ret = batadv_algo_register(&batadv_batman_iv);
  1708. if (ret < 0)
  1709. goto handler_unregister;
  1710. goto out;
  1711. handler_unregister:
  1712. batadv_recv_handler_unregister(BATADV_IV_OGM);
  1713. out:
  1714. return ret;
  1715. }