bat_iv_ogm.c 59 KB

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