bat_iv_ogm.c 59 KB

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