bat_iv_ogm.c 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425
  1. /* Copyright (C) 2007-2013 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, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA
  18. */
  19. #include "main.h"
  20. #include "translation-table.h"
  21. #include "originator.h"
  22. #include "routing.h"
  23. #include "gateway_common.h"
  24. #include "gateway_client.h"
  25. #include "hard-interface.h"
  26. #include "send.h"
  27. #include "bat_algo.h"
  28. #include "network-coding.h"
  29. /**
  30. * batadv_dup_status - duplicate status
  31. * @BATADV_NO_DUP: the packet is a duplicate
  32. * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
  33. * neighbor)
  34. * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
  35. * @BATADV_PROTECTED: originator is currently protected (after reboot)
  36. */
  37. enum batadv_dup_status {
  38. BATADV_NO_DUP = 0,
  39. BATADV_ORIG_DUP,
  40. BATADV_NEIGH_DUP,
  41. BATADV_PROTECTED,
  42. };
  43. /**
  44. * batadv_ring_buffer_set - update the ring buffer with the given value
  45. * @lq_recv: pointer to the ring buffer
  46. * @lq_index: index to store the value at
  47. * @value: value to store in the ring buffer
  48. */
  49. static void batadv_ring_buffer_set(uint8_t lq_recv[], uint8_t *lq_index,
  50. uint8_t value)
  51. {
  52. lq_recv[*lq_index] = value;
  53. *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
  54. }
  55. /**
  56. * batadv_ring_buffer_set - compute the average of all non-zero values stored
  57. * in the given ring buffer
  58. * @lq_recv: pointer to the ring buffer
  59. *
  60. * Returns computed average value.
  61. */
  62. static uint8_t batadv_ring_buffer_avg(const uint8_t lq_recv[])
  63. {
  64. const uint8_t *ptr;
  65. uint16_t count = 0, i = 0, sum = 0;
  66. ptr = lq_recv;
  67. while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
  68. if (*ptr != 0) {
  69. count++;
  70. sum += *ptr;
  71. }
  72. i++;
  73. ptr++;
  74. }
  75. if (count == 0)
  76. return 0;
  77. return (uint8_t)(sum / count);
  78. }
  79. static struct batadv_neigh_node *
  80. batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
  81. const uint8_t *neigh_addr,
  82. struct batadv_orig_node *orig_node,
  83. struct batadv_orig_node *orig_neigh)
  84. {
  85. struct batadv_neigh_node *neigh_node;
  86. neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr);
  87. if (!neigh_node)
  88. goto out;
  89. INIT_LIST_HEAD(&neigh_node->bonding_list);
  90. neigh_node->orig_node = orig_neigh;
  91. neigh_node->if_incoming = hard_iface;
  92. spin_lock_bh(&orig_node->neigh_list_lock);
  93. hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
  94. spin_unlock_bh(&orig_node->neigh_list_lock);
  95. out:
  96. return neigh_node;
  97. }
  98. static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
  99. {
  100. struct batadv_ogm_packet *batadv_ogm_packet;
  101. unsigned char *ogm_buff;
  102. uint32_t random_seqno;
  103. int res = -ENOMEM;
  104. /* randomize initial seqno to avoid collision */
  105. get_random_bytes(&random_seqno, sizeof(random_seqno));
  106. atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
  107. hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
  108. ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
  109. if (!ogm_buff)
  110. goto out;
  111. hard_iface->bat_iv.ogm_buff = ogm_buff;
  112. batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
  113. batadv_ogm_packet->header.packet_type = BATADV_IV_OGM;
  114. batadv_ogm_packet->header.version = BATADV_COMPAT_VERSION;
  115. batadv_ogm_packet->header.ttl = 2;
  116. batadv_ogm_packet->flags = BATADV_NO_FLAGS;
  117. batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
  118. batadv_ogm_packet->tt_num_changes = 0;
  119. batadv_ogm_packet->ttvn = 0;
  120. res = 0;
  121. out:
  122. return res;
  123. }
  124. static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
  125. {
  126. kfree(hard_iface->bat_iv.ogm_buff);
  127. hard_iface->bat_iv.ogm_buff = NULL;
  128. }
  129. static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
  130. {
  131. struct batadv_ogm_packet *batadv_ogm_packet;
  132. unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
  133. batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
  134. memcpy(batadv_ogm_packet->orig,
  135. hard_iface->net_dev->dev_addr, ETH_ALEN);
  136. memcpy(batadv_ogm_packet->prev_sender,
  137. hard_iface->net_dev->dev_addr, ETH_ALEN);
  138. }
  139. static void
  140. batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
  141. {
  142. struct batadv_ogm_packet *batadv_ogm_packet;
  143. unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
  144. batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
  145. batadv_ogm_packet->flags = BATADV_PRIMARIES_FIRST_HOP;
  146. batadv_ogm_packet->header.ttl = BATADV_TTL;
  147. }
  148. /* when do we schedule our own ogm to be sent */
  149. static unsigned long
  150. batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
  151. {
  152. unsigned int msecs;
  153. msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
  154. msecs += prandom_u32() % (2 * BATADV_JITTER);
  155. return jiffies + msecs_to_jiffies(msecs);
  156. }
  157. /* when do we schedule a ogm packet to be sent */
  158. static unsigned long batadv_iv_ogm_fwd_send_time(void)
  159. {
  160. return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
  161. }
  162. /* apply hop penalty for a normal link */
  163. static uint8_t batadv_hop_penalty(uint8_t tq,
  164. const struct batadv_priv *bat_priv)
  165. {
  166. int hop_penalty = atomic_read(&bat_priv->hop_penalty);
  167. int new_tq;
  168. new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
  169. new_tq /= BATADV_TQ_MAX_VALUE;
  170. return new_tq;
  171. }
  172. /* is there another aggregated packet here? */
  173. static int batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
  174. int tt_num_changes)
  175. {
  176. int next_buff_pos = 0;
  177. next_buff_pos += buff_pos + BATADV_OGM_HLEN;
  178. next_buff_pos += batadv_tt_len(tt_num_changes);
  179. return (next_buff_pos <= packet_len) &&
  180. (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
  181. }
  182. /* send a batman ogm to a given interface */
  183. static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
  184. struct batadv_hard_iface *hard_iface)
  185. {
  186. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  187. char *fwd_str;
  188. uint8_t packet_num;
  189. int16_t buff_pos;
  190. struct batadv_ogm_packet *batadv_ogm_packet;
  191. struct sk_buff *skb;
  192. uint8_t *packet_pos;
  193. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  194. return;
  195. packet_num = 0;
  196. buff_pos = 0;
  197. packet_pos = forw_packet->skb->data;
  198. batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
  199. /* adjust all flags and log packets */
  200. while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
  201. batadv_ogm_packet->tt_num_changes)) {
  202. /* we might have aggregated direct link packets with an
  203. * ordinary base packet
  204. */
  205. if (forw_packet->direct_link_flags & BIT(packet_num) &&
  206. forw_packet->if_incoming == hard_iface)
  207. batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
  208. else
  209. batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
  210. if (packet_num > 0 || !forw_packet->own)
  211. fwd_str = "Forwarding";
  212. else
  213. fwd_str = "Sending own";
  214. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  215. "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s, ttvn %d) on interface %s [%pM]\n",
  216. fwd_str, (packet_num > 0 ? "aggregated " : ""),
  217. batadv_ogm_packet->orig,
  218. ntohl(batadv_ogm_packet->seqno),
  219. batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl,
  220. (batadv_ogm_packet->flags & BATADV_DIRECTLINK ?
  221. "on" : "off"),
  222. batadv_ogm_packet->ttvn, hard_iface->net_dev->name,
  223. hard_iface->net_dev->dev_addr);
  224. buff_pos += BATADV_OGM_HLEN;
  225. buff_pos += batadv_tt_len(batadv_ogm_packet->tt_num_changes);
  226. packet_num++;
  227. packet_pos = forw_packet->skb->data + buff_pos;
  228. batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
  229. }
  230. /* create clone because function is called more than once */
  231. skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
  232. if (skb) {
  233. batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
  234. batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
  235. skb->len + ETH_HLEN);
  236. batadv_send_skb_packet(skb, hard_iface, batadv_broadcast_addr);
  237. }
  238. }
  239. /* send a batman ogm packet */
  240. static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
  241. {
  242. struct batadv_hard_iface *hard_iface;
  243. struct net_device *soft_iface;
  244. struct batadv_priv *bat_priv;
  245. struct batadv_hard_iface *primary_if = NULL;
  246. struct batadv_ogm_packet *batadv_ogm_packet;
  247. unsigned char directlink;
  248. uint8_t *packet_pos;
  249. packet_pos = forw_packet->skb->data;
  250. batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
  251. directlink = (batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0);
  252. if (!forw_packet->if_incoming) {
  253. pr_err("Error - can't forward packet: incoming iface not specified\n");
  254. goto out;
  255. }
  256. soft_iface = forw_packet->if_incoming->soft_iface;
  257. bat_priv = netdev_priv(soft_iface);
  258. if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
  259. goto out;
  260. primary_if = batadv_primary_if_get_selected(bat_priv);
  261. if (!primary_if)
  262. goto out;
  263. /* multihomed peer assumed
  264. * non-primary OGMs are only broadcasted on their interface
  265. */
  266. if ((directlink && (batadv_ogm_packet->header.ttl == 1)) ||
  267. (forw_packet->own && (forw_packet->if_incoming != primary_if))) {
  268. /* FIXME: what about aggregated packets ? */
  269. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  270. "%s packet (originator %pM, seqno %u, TTL %d) on interface %s [%pM]\n",
  271. (forw_packet->own ? "Sending own" : "Forwarding"),
  272. batadv_ogm_packet->orig,
  273. ntohl(batadv_ogm_packet->seqno),
  274. batadv_ogm_packet->header.ttl,
  275. forw_packet->if_incoming->net_dev->name,
  276. forw_packet->if_incoming->net_dev->dev_addr);
  277. /* skb is only used once and than forw_packet is free'd */
  278. batadv_send_skb_packet(forw_packet->skb,
  279. forw_packet->if_incoming,
  280. batadv_broadcast_addr);
  281. forw_packet->skb = NULL;
  282. goto out;
  283. }
  284. /* broadcast on every interface */
  285. rcu_read_lock();
  286. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  287. if (hard_iface->soft_iface != soft_iface)
  288. continue;
  289. batadv_iv_ogm_send_to_if(forw_packet, hard_iface);
  290. }
  291. rcu_read_unlock();
  292. out:
  293. if (primary_if)
  294. batadv_hardif_free_ref(primary_if);
  295. }
  296. /* return true if new_packet can be aggregated with forw_packet */
  297. static bool
  298. batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
  299. struct batadv_priv *bat_priv,
  300. int packet_len, unsigned long send_time,
  301. bool directlink,
  302. const struct batadv_hard_iface *if_incoming,
  303. const struct batadv_forw_packet *forw_packet)
  304. {
  305. struct batadv_ogm_packet *batadv_ogm_packet;
  306. int aggregated_bytes = forw_packet->packet_len + packet_len;
  307. struct batadv_hard_iface *primary_if = NULL;
  308. bool res = false;
  309. unsigned long aggregation_end_time;
  310. batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
  311. aggregation_end_time = send_time;
  312. aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
  313. /* we can aggregate the current packet to this aggregated packet
  314. * if:
  315. *
  316. * - the send time is within our MAX_AGGREGATION_MS time
  317. * - the resulting packet wont be bigger than
  318. * MAX_AGGREGATION_BYTES
  319. */
  320. if (time_before(send_time, forw_packet->send_time) &&
  321. time_after_eq(aggregation_end_time, forw_packet->send_time) &&
  322. (aggregated_bytes <= BATADV_MAX_AGGREGATION_BYTES)) {
  323. /* check aggregation compatibility
  324. * -> direct link packets are broadcasted on
  325. * their interface only
  326. * -> aggregate packet if the current packet is
  327. * a "global" packet as well as the base
  328. * packet
  329. */
  330. primary_if = batadv_primary_if_get_selected(bat_priv);
  331. if (!primary_if)
  332. goto out;
  333. /* packets without direct link flag and high TTL
  334. * are flooded through the net
  335. */
  336. if ((!directlink) &&
  337. (!(batadv_ogm_packet->flags & BATADV_DIRECTLINK)) &&
  338. (batadv_ogm_packet->header.ttl != 1) &&
  339. /* own packets originating non-primary
  340. * interfaces leave only that interface
  341. */
  342. ((!forw_packet->own) ||
  343. (forw_packet->if_incoming == primary_if))) {
  344. res = true;
  345. goto out;
  346. }
  347. /* if the incoming packet is sent via this one
  348. * interface only - we still can aggregate
  349. */
  350. if ((directlink) &&
  351. (new_bat_ogm_packet->header.ttl == 1) &&
  352. (forw_packet->if_incoming == if_incoming) &&
  353. /* packets from direct neighbors or
  354. * own secondary interface packets
  355. * (= secondary interface packets in general)
  356. */
  357. (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
  358. (forw_packet->own &&
  359. forw_packet->if_incoming != primary_if))) {
  360. res = true;
  361. goto out;
  362. }
  363. }
  364. out:
  365. if (primary_if)
  366. batadv_hardif_free_ref(primary_if);
  367. return res;
  368. }
  369. /* create a new aggregated packet and add this packet to it */
  370. static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
  371. int packet_len, unsigned long send_time,
  372. bool direct_link,
  373. struct batadv_hard_iface *if_incoming,
  374. int own_packet)
  375. {
  376. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  377. struct batadv_forw_packet *forw_packet_aggr;
  378. unsigned char *skb_buff;
  379. unsigned int skb_size;
  380. if (!atomic_inc_not_zero(&if_incoming->refcount))
  381. return;
  382. /* own packet should always be scheduled */
  383. if (!own_packet) {
  384. if (!batadv_atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
  385. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  386. "batman packet queue full\n");
  387. goto out;
  388. }
  389. }
  390. forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
  391. if (!forw_packet_aggr) {
  392. if (!own_packet)
  393. atomic_inc(&bat_priv->batman_queue_left);
  394. goto out;
  395. }
  396. if ((atomic_read(&bat_priv->aggregated_ogms)) &&
  397. (packet_len < BATADV_MAX_AGGREGATION_BYTES))
  398. skb_size = BATADV_MAX_AGGREGATION_BYTES;
  399. else
  400. skb_size = packet_len;
  401. skb_size += ETH_HLEN;
  402. forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
  403. if (!forw_packet_aggr->skb) {
  404. if (!own_packet)
  405. atomic_inc(&bat_priv->batman_queue_left);
  406. kfree(forw_packet_aggr);
  407. goto out;
  408. }
  409. forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
  410. skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
  411. skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
  412. forw_packet_aggr->packet_len = packet_len;
  413. memcpy(skb_buff, packet_buff, packet_len);
  414. forw_packet_aggr->own = own_packet;
  415. forw_packet_aggr->if_incoming = if_incoming;
  416. forw_packet_aggr->num_packets = 0;
  417. forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
  418. forw_packet_aggr->send_time = send_time;
  419. /* save packet direct link flag status */
  420. if (direct_link)
  421. forw_packet_aggr->direct_link_flags |= 1;
  422. /* add new packet to packet list */
  423. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  424. hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
  425. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  426. /* start timer for this packet */
  427. INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
  428. batadv_send_outstanding_bat_ogm_packet);
  429. queue_delayed_work(batadv_event_workqueue,
  430. &forw_packet_aggr->delayed_work,
  431. send_time - jiffies);
  432. return;
  433. out:
  434. batadv_hardif_free_ref(if_incoming);
  435. }
  436. /* aggregate a new packet into the existing ogm packet */
  437. static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
  438. const unsigned char *packet_buff,
  439. int packet_len, bool direct_link)
  440. {
  441. unsigned char *skb_buff;
  442. unsigned long new_direct_link_flag;
  443. skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
  444. memcpy(skb_buff, packet_buff, packet_len);
  445. forw_packet_aggr->packet_len += packet_len;
  446. forw_packet_aggr->num_packets++;
  447. /* save packet direct link flag status */
  448. if (direct_link) {
  449. new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
  450. forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
  451. }
  452. }
  453. static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
  454. unsigned char *packet_buff,
  455. int packet_len,
  456. struct batadv_hard_iface *if_incoming,
  457. int own_packet, unsigned long send_time)
  458. {
  459. /* _aggr -> pointer to the packet we want to aggregate with
  460. * _pos -> pointer to the position in the queue
  461. */
  462. struct batadv_forw_packet *forw_packet_aggr = NULL;
  463. struct batadv_forw_packet *forw_packet_pos = NULL;
  464. struct batadv_ogm_packet *batadv_ogm_packet;
  465. bool direct_link;
  466. unsigned long max_aggregation_jiffies;
  467. batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
  468. direct_link = batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0;
  469. max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
  470. /* find position for the packet in the forward queue */
  471. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  472. /* own packets are not to be aggregated */
  473. if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
  474. hlist_for_each_entry(forw_packet_pos,
  475. &bat_priv->forw_bat_list, list) {
  476. if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
  477. bat_priv, packet_len,
  478. send_time, direct_link,
  479. if_incoming,
  480. forw_packet_pos)) {
  481. forw_packet_aggr = forw_packet_pos;
  482. break;
  483. }
  484. }
  485. }
  486. /* nothing to aggregate with - either aggregation disabled or no
  487. * suitable aggregation packet found
  488. */
  489. if (!forw_packet_aggr) {
  490. /* the following section can run without the lock */
  491. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  492. /* if we could not aggregate this packet with one of the others
  493. * we hold it back for a while, so that it might be aggregated
  494. * later on
  495. */
  496. if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
  497. send_time += max_aggregation_jiffies;
  498. batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
  499. send_time, direct_link,
  500. if_incoming, own_packet);
  501. } else {
  502. batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
  503. packet_len, direct_link);
  504. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  505. }
  506. }
  507. static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
  508. const struct ethhdr *ethhdr,
  509. struct batadv_ogm_packet *batadv_ogm_packet,
  510. bool is_single_hop_neigh,
  511. bool is_from_best_next_hop,
  512. struct batadv_hard_iface *if_incoming)
  513. {
  514. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  515. uint8_t tt_num_changes;
  516. if (batadv_ogm_packet->header.ttl <= 1) {
  517. batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
  518. return;
  519. }
  520. if (!is_from_best_next_hop) {
  521. /* Mark the forwarded packet when it is not coming from our
  522. * best next hop. We still need to forward the packet for our
  523. * neighbor link quality detection to work in case the packet
  524. * originated from a single hop neighbor. Otherwise we can
  525. * simply drop the ogm.
  526. */
  527. if (is_single_hop_neigh)
  528. batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
  529. else
  530. return;
  531. }
  532. tt_num_changes = batadv_ogm_packet->tt_num_changes;
  533. batadv_ogm_packet->header.ttl--;
  534. memcpy(batadv_ogm_packet->prev_sender, ethhdr->h_source, ETH_ALEN);
  535. /* apply hop penalty */
  536. batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
  537. bat_priv);
  538. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  539. "Forwarding packet: tq: %i, ttl: %i\n",
  540. batadv_ogm_packet->tq, batadv_ogm_packet->header.ttl);
  541. /* switch of primaries first hop flag when forwarding */
  542. batadv_ogm_packet->flags &= ~BATADV_PRIMARIES_FIRST_HOP;
  543. if (is_single_hop_neigh)
  544. batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
  545. else
  546. batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
  547. batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
  548. BATADV_OGM_HLEN + batadv_tt_len(tt_num_changes),
  549. if_incoming, 0, batadv_iv_ogm_fwd_send_time());
  550. }
  551. /**
  552. * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
  553. * the given interface
  554. * @hard_iface: the interface for which the windows have to be shifted
  555. */
  556. static void
  557. batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
  558. {
  559. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  560. struct batadv_hashtable *hash = bat_priv->orig_hash;
  561. struct hlist_head *head;
  562. struct batadv_orig_node *orig_node;
  563. unsigned long *word;
  564. uint32_t i;
  565. size_t word_index;
  566. uint8_t *w;
  567. for (i = 0; i < hash->size; i++) {
  568. head = &hash->table[i];
  569. rcu_read_lock();
  570. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  571. spin_lock_bh(&orig_node->ogm_cnt_lock);
  572. word_index = hard_iface->if_num * BATADV_NUM_WORDS;
  573. word = &(orig_node->bcast_own[word_index]);
  574. batadv_bit_get_packet(bat_priv, word, 1, 0);
  575. w = &orig_node->bcast_own_sum[hard_iface->if_num];
  576. *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
  577. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  578. }
  579. rcu_read_unlock();
  580. }
  581. }
  582. static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
  583. {
  584. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  585. unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
  586. struct batadv_ogm_packet *batadv_ogm_packet;
  587. struct batadv_hard_iface *primary_if;
  588. int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
  589. int vis_server, tt_num_changes = 0;
  590. uint32_t seqno;
  591. uint8_t bandwidth;
  592. vis_server = atomic_read(&bat_priv->vis_mode);
  593. primary_if = batadv_primary_if_get_selected(bat_priv);
  594. if (hard_iface == primary_if)
  595. tt_num_changes = batadv_tt_append_diff(bat_priv, ogm_buff,
  596. ogm_buff_len,
  597. BATADV_OGM_HLEN);
  598. batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
  599. /* change sequence number to network order */
  600. seqno = (uint32_t)atomic_read(&hard_iface->bat_iv.ogm_seqno);
  601. batadv_ogm_packet->seqno = htonl(seqno);
  602. atomic_inc(&hard_iface->bat_iv.ogm_seqno);
  603. batadv_ogm_packet->ttvn = atomic_read(&bat_priv->tt.vn);
  604. batadv_ogm_packet->tt_crc = htons(bat_priv->tt.local_crc);
  605. if (tt_num_changes >= 0)
  606. batadv_ogm_packet->tt_num_changes = tt_num_changes;
  607. if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC)
  608. batadv_ogm_packet->flags |= BATADV_VIS_SERVER;
  609. else
  610. batadv_ogm_packet->flags &= ~BATADV_VIS_SERVER;
  611. if (hard_iface == primary_if &&
  612. atomic_read(&bat_priv->gw_mode) == BATADV_GW_MODE_SERVER) {
  613. bandwidth = (uint8_t)atomic_read(&bat_priv->gw_bandwidth);
  614. batadv_ogm_packet->gw_flags = bandwidth;
  615. } else {
  616. batadv_ogm_packet->gw_flags = BATADV_NO_FLAGS;
  617. }
  618. batadv_iv_ogm_slide_own_bcast_window(hard_iface);
  619. batadv_iv_ogm_queue_add(bat_priv, hard_iface->bat_iv.ogm_buff,
  620. hard_iface->bat_iv.ogm_buff_len, hard_iface, 1,
  621. batadv_iv_ogm_emit_send_time(bat_priv));
  622. if (primary_if)
  623. batadv_hardif_free_ref(primary_if);
  624. }
  625. static void
  626. batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
  627. struct batadv_orig_node *orig_node,
  628. const struct ethhdr *ethhdr,
  629. const struct batadv_ogm_packet *batadv_ogm_packet,
  630. struct batadv_hard_iface *if_incoming,
  631. const unsigned char *tt_buff,
  632. enum batadv_dup_status dup_status)
  633. {
  634. struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
  635. struct batadv_neigh_node *router = NULL;
  636. struct batadv_orig_node *orig_node_tmp;
  637. int if_num;
  638. uint8_t sum_orig, sum_neigh;
  639. uint8_t *neigh_addr;
  640. uint8_t tq_avg;
  641. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  642. "update_originator(): Searching and updating originator entry of received packet\n");
  643. rcu_read_lock();
  644. hlist_for_each_entry_rcu(tmp_neigh_node,
  645. &orig_node->neigh_list, list) {
  646. neigh_addr = tmp_neigh_node->addr;
  647. if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
  648. tmp_neigh_node->if_incoming == if_incoming &&
  649. atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
  650. if (WARN(neigh_node, "too many matching neigh_nodes"))
  651. batadv_neigh_node_free_ref(neigh_node);
  652. neigh_node = tmp_neigh_node;
  653. continue;
  654. }
  655. if (dup_status != BATADV_NO_DUP)
  656. continue;
  657. spin_lock_bh(&tmp_neigh_node->lq_update_lock);
  658. batadv_ring_buffer_set(tmp_neigh_node->tq_recv,
  659. &tmp_neigh_node->tq_index, 0);
  660. tq_avg = batadv_ring_buffer_avg(tmp_neigh_node->tq_recv);
  661. tmp_neigh_node->tq_avg = tq_avg;
  662. spin_unlock_bh(&tmp_neigh_node->lq_update_lock);
  663. }
  664. if (!neigh_node) {
  665. struct batadv_orig_node *orig_tmp;
  666. orig_tmp = batadv_get_orig_node(bat_priv, ethhdr->h_source);
  667. if (!orig_tmp)
  668. goto unlock;
  669. neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
  670. ethhdr->h_source,
  671. orig_node, orig_tmp);
  672. batadv_orig_node_free_ref(orig_tmp);
  673. if (!neigh_node)
  674. goto unlock;
  675. } else
  676. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  677. "Updating existing last-hop neighbor of originator\n");
  678. rcu_read_unlock();
  679. orig_node->flags = batadv_ogm_packet->flags;
  680. neigh_node->last_seen = jiffies;
  681. spin_lock_bh(&neigh_node->lq_update_lock);
  682. batadv_ring_buffer_set(neigh_node->tq_recv,
  683. &neigh_node->tq_index,
  684. batadv_ogm_packet->tq);
  685. neigh_node->tq_avg = batadv_ring_buffer_avg(neigh_node->tq_recv);
  686. spin_unlock_bh(&neigh_node->lq_update_lock);
  687. if (dup_status == BATADV_NO_DUP) {
  688. orig_node->last_ttl = batadv_ogm_packet->header.ttl;
  689. neigh_node->last_ttl = batadv_ogm_packet->header.ttl;
  690. }
  691. batadv_bonding_candidate_add(orig_node, neigh_node);
  692. /* if this neighbor already is our next hop there is nothing
  693. * to change
  694. */
  695. router = batadv_orig_node_get_router(orig_node);
  696. if (router == neigh_node)
  697. goto update_tt;
  698. /* if this neighbor does not offer a better TQ we won't consider it */
  699. if (router && (router->tq_avg > neigh_node->tq_avg))
  700. goto update_tt;
  701. /* if the TQ is the same and the link not more symmetric we
  702. * won't consider it either
  703. */
  704. if (router && (neigh_node->tq_avg == router->tq_avg)) {
  705. orig_node_tmp = router->orig_node;
  706. spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
  707. if_num = router->if_incoming->if_num;
  708. sum_orig = orig_node_tmp->bcast_own_sum[if_num];
  709. spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
  710. orig_node_tmp = neigh_node->orig_node;
  711. spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
  712. if_num = neigh_node->if_incoming->if_num;
  713. sum_neigh = orig_node_tmp->bcast_own_sum[if_num];
  714. spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
  715. if (sum_orig >= sum_neigh)
  716. goto update_tt;
  717. }
  718. batadv_update_route(bat_priv, orig_node, neigh_node);
  719. update_tt:
  720. /* I have to check for transtable changes only if the OGM has been
  721. * sent through a primary interface
  722. */
  723. if (((batadv_ogm_packet->orig != ethhdr->h_source) &&
  724. (batadv_ogm_packet->header.ttl > 2)) ||
  725. (batadv_ogm_packet->flags & BATADV_PRIMARIES_FIRST_HOP))
  726. batadv_tt_update_orig(bat_priv, orig_node, tt_buff,
  727. batadv_ogm_packet->tt_num_changes,
  728. batadv_ogm_packet->ttvn,
  729. ntohs(batadv_ogm_packet->tt_crc));
  730. if (orig_node->gw_flags != batadv_ogm_packet->gw_flags)
  731. batadv_gw_node_update(bat_priv, orig_node,
  732. batadv_ogm_packet->gw_flags);
  733. orig_node->gw_flags = batadv_ogm_packet->gw_flags;
  734. /* restart gateway selection if fast or late switching was enabled */
  735. if ((orig_node->gw_flags) &&
  736. (atomic_read(&bat_priv->gw_mode) == BATADV_GW_MODE_CLIENT) &&
  737. (atomic_read(&bat_priv->gw_sel_class) > 2))
  738. batadv_gw_check_election(bat_priv, orig_node);
  739. goto out;
  740. unlock:
  741. rcu_read_unlock();
  742. out:
  743. if (neigh_node)
  744. batadv_neigh_node_free_ref(neigh_node);
  745. if (router)
  746. batadv_neigh_node_free_ref(router);
  747. }
  748. static int batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
  749. struct batadv_orig_node *orig_neigh_node,
  750. struct batadv_ogm_packet *batadv_ogm_packet,
  751. struct batadv_hard_iface *if_incoming)
  752. {
  753. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  754. struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
  755. uint8_t total_count;
  756. uint8_t orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
  757. unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
  758. int tq_asym_penalty, inv_asym_penalty, ret = 0;
  759. unsigned int combined_tq;
  760. /* find corresponding one hop neighbor */
  761. rcu_read_lock();
  762. hlist_for_each_entry_rcu(tmp_neigh_node,
  763. &orig_neigh_node->neigh_list, list) {
  764. if (!batadv_compare_eth(tmp_neigh_node->addr,
  765. orig_neigh_node->orig))
  766. continue;
  767. if (tmp_neigh_node->if_incoming != if_incoming)
  768. continue;
  769. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  770. continue;
  771. neigh_node = tmp_neigh_node;
  772. break;
  773. }
  774. rcu_read_unlock();
  775. if (!neigh_node)
  776. neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
  777. orig_neigh_node->orig,
  778. orig_neigh_node,
  779. orig_neigh_node);
  780. if (!neigh_node)
  781. goto out;
  782. /* if orig_node is direct neighbor update neigh_node last_seen */
  783. if (orig_node == orig_neigh_node)
  784. neigh_node->last_seen = jiffies;
  785. orig_node->last_seen = jiffies;
  786. /* find packet count of corresponding one hop neighbor */
  787. spin_lock_bh(&orig_node->ogm_cnt_lock);
  788. orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
  789. neigh_rq_count = neigh_node->real_packet_count;
  790. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  791. /* pay attention to not get a value bigger than 100 % */
  792. if (orig_eq_count > neigh_rq_count)
  793. total_count = neigh_rq_count;
  794. else
  795. total_count = orig_eq_count;
  796. /* if we have too few packets (too less data) we set tq_own to zero
  797. * if we receive too few packets it is not considered bidirectional
  798. */
  799. if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
  800. neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
  801. tq_own = 0;
  802. else
  803. /* neigh_node->real_packet_count is never zero as we
  804. * only purge old information when getting new
  805. * information
  806. */
  807. tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
  808. /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
  809. * affect the nearly-symmetric links only a little, but
  810. * punishes asymmetric links more. This will give a value
  811. * between 0 and TQ_MAX_VALUE
  812. */
  813. neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
  814. neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
  815. neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
  816. BATADV_TQ_LOCAL_WINDOW_SIZE *
  817. BATADV_TQ_LOCAL_WINDOW_SIZE;
  818. inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
  819. inv_asym_penalty /= neigh_rq_max_cube;
  820. tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
  821. combined_tq = batadv_ogm_packet->tq * tq_own * tq_asym_penalty;
  822. combined_tq /= BATADV_TQ_MAX_VALUE * BATADV_TQ_MAX_VALUE;
  823. batadv_ogm_packet->tq = combined_tq;
  824. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  825. "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n",
  826. orig_node->orig, orig_neigh_node->orig, total_count,
  827. neigh_rq_count, tq_own,
  828. tq_asym_penalty, batadv_ogm_packet->tq);
  829. /* if link has the minimum required transmission quality
  830. * consider it bidirectional
  831. */
  832. if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
  833. ret = 1;
  834. out:
  835. if (neigh_node)
  836. batadv_neigh_node_free_ref(neigh_node);
  837. return ret;
  838. }
  839. /**
  840. * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
  841. * adjust the sequence number and find out whether it is a duplicate
  842. * @ethhdr: ethernet header of the packet
  843. * @batadv_ogm_packet: OGM packet to be considered
  844. * @if_incoming: interface on which the OGM packet was received
  845. *
  846. * Returns duplicate status as enum batadv_dup_status
  847. */
  848. static enum batadv_dup_status
  849. batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
  850. const struct batadv_ogm_packet *batadv_ogm_packet,
  851. const struct batadv_hard_iface *if_incoming)
  852. {
  853. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  854. struct batadv_orig_node *orig_node;
  855. struct batadv_neigh_node *tmp_neigh_node;
  856. int is_dup;
  857. int32_t seq_diff;
  858. int need_update = 0;
  859. int set_mark;
  860. enum batadv_dup_status ret = BATADV_NO_DUP;
  861. uint32_t seqno = ntohl(batadv_ogm_packet->seqno);
  862. uint8_t *neigh_addr;
  863. uint8_t packet_count;
  864. orig_node = batadv_get_orig_node(bat_priv, batadv_ogm_packet->orig);
  865. if (!orig_node)
  866. return BATADV_NO_DUP;
  867. spin_lock_bh(&orig_node->ogm_cnt_lock);
  868. seq_diff = seqno - orig_node->last_real_seqno;
  869. /* signalize caller that the packet is to be dropped. */
  870. if (!hlist_empty(&orig_node->neigh_list) &&
  871. batadv_window_protected(bat_priv, seq_diff,
  872. &orig_node->batman_seqno_reset)) {
  873. ret = BATADV_PROTECTED;
  874. goto out;
  875. }
  876. rcu_read_lock();
  877. hlist_for_each_entry_rcu(tmp_neigh_node,
  878. &orig_node->neigh_list, list) {
  879. neigh_addr = tmp_neigh_node->addr;
  880. is_dup = batadv_test_bit(tmp_neigh_node->real_bits,
  881. orig_node->last_real_seqno,
  882. seqno);
  883. if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
  884. tmp_neigh_node->if_incoming == if_incoming) {
  885. set_mark = 1;
  886. if (is_dup)
  887. ret = BATADV_NEIGH_DUP;
  888. } else {
  889. set_mark = 0;
  890. if (is_dup && (ret != BATADV_NEIGH_DUP))
  891. ret = BATADV_ORIG_DUP;
  892. }
  893. /* if the window moved, set the update flag. */
  894. need_update |= batadv_bit_get_packet(bat_priv,
  895. tmp_neigh_node->real_bits,
  896. seq_diff, set_mark);
  897. packet_count = bitmap_weight(tmp_neigh_node->real_bits,
  898. BATADV_TQ_LOCAL_WINDOW_SIZE);
  899. tmp_neigh_node->real_packet_count = packet_count;
  900. }
  901. rcu_read_unlock();
  902. if (need_update) {
  903. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  904. "updating last_seqno: old %u, new %u\n",
  905. orig_node->last_real_seqno, seqno);
  906. orig_node->last_real_seqno = seqno;
  907. }
  908. out:
  909. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  910. batadv_orig_node_free_ref(orig_node);
  911. return ret;
  912. }
  913. static void batadv_iv_ogm_process(const struct ethhdr *ethhdr,
  914. struct batadv_ogm_packet *batadv_ogm_packet,
  915. const unsigned char *tt_buff,
  916. struct batadv_hard_iface *if_incoming)
  917. {
  918. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  919. struct batadv_hard_iface *hard_iface;
  920. struct batadv_orig_node *orig_neigh_node, *orig_node;
  921. struct batadv_neigh_node *router = NULL, *router_router = NULL;
  922. struct batadv_neigh_node *orig_neigh_router = NULL;
  923. int has_directlink_flag;
  924. int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
  925. int is_bidirect;
  926. bool is_single_hop_neigh = false;
  927. bool is_from_best_next_hop = false;
  928. int sameseq, similar_ttl;
  929. enum batadv_dup_status dup_status;
  930. uint32_t if_incoming_seqno;
  931. uint8_t *prev_sender;
  932. /* Silently drop when the batman packet is actually not a
  933. * correct packet.
  934. *
  935. * This might happen if a packet is padded (e.g. Ethernet has a
  936. * minimum frame length of 64 byte) and the aggregation interprets
  937. * it as an additional length.
  938. *
  939. * TODO: A more sane solution would be to have a bit in the
  940. * batadv_ogm_packet to detect whether the packet is the last
  941. * packet in an aggregation. Here we expect that the padding
  942. * is always zero (or not 0x01)
  943. */
  944. if (batadv_ogm_packet->header.packet_type != BATADV_IV_OGM)
  945. return;
  946. /* could be changed by schedule_own_packet() */
  947. if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
  948. if (batadv_ogm_packet->flags & BATADV_DIRECTLINK)
  949. has_directlink_flag = 1;
  950. else
  951. has_directlink_flag = 0;
  952. if (batadv_compare_eth(ethhdr->h_source, batadv_ogm_packet->orig))
  953. is_single_hop_neigh = true;
  954. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  955. "Received BATMAN packet via NB: %pM, IF: %s [%pM] (from OG: %pM, via prev OG: %pM, seqno %u, ttvn %u, crc %#.4x, changes %u, tq %d, TTL %d, V %d, IDF %d)\n",
  956. ethhdr->h_source, if_incoming->net_dev->name,
  957. if_incoming->net_dev->dev_addr, batadv_ogm_packet->orig,
  958. batadv_ogm_packet->prev_sender,
  959. ntohl(batadv_ogm_packet->seqno), batadv_ogm_packet->ttvn,
  960. ntohs(batadv_ogm_packet->tt_crc),
  961. batadv_ogm_packet->tt_num_changes, batadv_ogm_packet->tq,
  962. batadv_ogm_packet->header.ttl,
  963. batadv_ogm_packet->header.version, has_directlink_flag);
  964. rcu_read_lock();
  965. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  966. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  967. continue;
  968. if (hard_iface->soft_iface != if_incoming->soft_iface)
  969. continue;
  970. if (batadv_compare_eth(ethhdr->h_source,
  971. hard_iface->net_dev->dev_addr))
  972. is_my_addr = 1;
  973. if (batadv_compare_eth(batadv_ogm_packet->orig,
  974. hard_iface->net_dev->dev_addr))
  975. is_my_orig = 1;
  976. if (batadv_compare_eth(batadv_ogm_packet->prev_sender,
  977. hard_iface->net_dev->dev_addr))
  978. is_my_oldorig = 1;
  979. }
  980. rcu_read_unlock();
  981. if (is_my_addr) {
  982. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  983. "Drop packet: received my own broadcast (sender: %pM)\n",
  984. ethhdr->h_source);
  985. return;
  986. }
  987. if (is_my_orig) {
  988. unsigned long *word;
  989. int offset;
  990. int32_t bit_pos;
  991. int16_t if_num;
  992. uint8_t *weight;
  993. orig_neigh_node = batadv_get_orig_node(bat_priv,
  994. ethhdr->h_source);
  995. if (!orig_neigh_node)
  996. return;
  997. /* neighbor has to indicate direct link and it has to
  998. * come via the corresponding interface
  999. * save packet seqno for bidirectional check
  1000. */
  1001. if (has_directlink_flag &&
  1002. batadv_compare_eth(if_incoming->net_dev->dev_addr,
  1003. batadv_ogm_packet->orig)) {
  1004. if_num = if_incoming->if_num;
  1005. offset = if_num * BATADV_NUM_WORDS;
  1006. spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
  1007. word = &(orig_neigh_node->bcast_own[offset]);
  1008. bit_pos = if_incoming_seqno - 2;
  1009. bit_pos -= ntohl(batadv_ogm_packet->seqno);
  1010. batadv_set_bit(word, bit_pos);
  1011. weight = &orig_neigh_node->bcast_own_sum[if_num];
  1012. *weight = bitmap_weight(word,
  1013. BATADV_TQ_LOCAL_WINDOW_SIZE);
  1014. spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
  1015. }
  1016. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1017. "Drop packet: originator packet from myself (via neighbor)\n");
  1018. batadv_orig_node_free_ref(orig_neigh_node);
  1019. return;
  1020. }
  1021. if (is_my_oldorig) {
  1022. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1023. "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
  1024. ethhdr->h_source);
  1025. return;
  1026. }
  1027. if (batadv_ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
  1028. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1029. "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
  1030. ethhdr->h_source);
  1031. return;
  1032. }
  1033. orig_node = batadv_get_orig_node(bat_priv, batadv_ogm_packet->orig);
  1034. if (!orig_node)
  1035. return;
  1036. dup_status = batadv_iv_ogm_update_seqnos(ethhdr, batadv_ogm_packet,
  1037. if_incoming);
  1038. if (dup_status == BATADV_PROTECTED) {
  1039. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1040. "Drop packet: packet within seqno protection time (sender: %pM)\n",
  1041. ethhdr->h_source);
  1042. goto out;
  1043. }
  1044. if (batadv_ogm_packet->tq == 0) {
  1045. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1046. "Drop packet: originator packet with tq equal 0\n");
  1047. goto out;
  1048. }
  1049. router = batadv_orig_node_get_router(orig_node);
  1050. if (router)
  1051. router_router = batadv_orig_node_get_router(router->orig_node);
  1052. if ((router && router->tq_avg != 0) &&
  1053. (batadv_compare_eth(router->addr, ethhdr->h_source)))
  1054. is_from_best_next_hop = true;
  1055. prev_sender = batadv_ogm_packet->prev_sender;
  1056. /* avoid temporary routing loops */
  1057. if (router && router_router &&
  1058. (batadv_compare_eth(router->addr, prev_sender)) &&
  1059. !(batadv_compare_eth(batadv_ogm_packet->orig, prev_sender)) &&
  1060. (batadv_compare_eth(router->addr, router_router->addr))) {
  1061. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1062. "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
  1063. ethhdr->h_source);
  1064. goto out;
  1065. }
  1066. /* if sender is a direct neighbor the sender mac equals
  1067. * originator mac
  1068. */
  1069. if (is_single_hop_neigh)
  1070. orig_neigh_node = orig_node;
  1071. else
  1072. orig_neigh_node = batadv_get_orig_node(bat_priv,
  1073. ethhdr->h_source);
  1074. if (!orig_neigh_node)
  1075. goto out;
  1076. /* Update nc_nodes of the originator */
  1077. batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
  1078. batadv_ogm_packet, is_single_hop_neigh);
  1079. orig_neigh_router = batadv_orig_node_get_router(orig_neigh_node);
  1080. /* drop packet if sender is not a direct neighbor and if we
  1081. * don't route towards it
  1082. */
  1083. if (!is_single_hop_neigh && (!orig_neigh_router)) {
  1084. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1085. "Drop packet: OGM via unknown neighbor!\n");
  1086. goto out_neigh;
  1087. }
  1088. is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
  1089. batadv_ogm_packet, if_incoming);
  1090. batadv_bonding_save_primary(orig_node, orig_neigh_node,
  1091. batadv_ogm_packet);
  1092. /* update ranking if it is not a duplicate or has the same
  1093. * seqno and similar ttl as the non-duplicate
  1094. */
  1095. sameseq = orig_node->last_real_seqno == ntohl(batadv_ogm_packet->seqno);
  1096. similar_ttl = orig_node->last_ttl - 3 <= batadv_ogm_packet->header.ttl;
  1097. if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
  1098. (sameseq && similar_ttl)))
  1099. batadv_iv_ogm_orig_update(bat_priv, orig_node, ethhdr,
  1100. batadv_ogm_packet, if_incoming,
  1101. tt_buff, dup_status);
  1102. /* is single hop (direct) neighbor */
  1103. if (is_single_hop_neigh) {
  1104. /* mark direct link on incoming interface */
  1105. batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet,
  1106. is_single_hop_neigh,
  1107. is_from_best_next_hop, if_incoming);
  1108. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1109. "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
  1110. goto out_neigh;
  1111. }
  1112. /* multihop originator */
  1113. if (!is_bidirect) {
  1114. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1115. "Drop packet: not received via bidirectional link\n");
  1116. goto out_neigh;
  1117. }
  1118. if (dup_status == BATADV_NEIGH_DUP) {
  1119. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1120. "Drop packet: duplicate packet received\n");
  1121. goto out_neigh;
  1122. }
  1123. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1124. "Forwarding packet: rebroadcast originator packet\n");
  1125. batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet,
  1126. is_single_hop_neigh, is_from_best_next_hop,
  1127. if_incoming);
  1128. out_neigh:
  1129. if ((orig_neigh_node) && (!is_single_hop_neigh))
  1130. batadv_orig_node_free_ref(orig_neigh_node);
  1131. out:
  1132. if (router)
  1133. batadv_neigh_node_free_ref(router);
  1134. if (router_router)
  1135. batadv_neigh_node_free_ref(router_router);
  1136. if (orig_neigh_router)
  1137. batadv_neigh_node_free_ref(orig_neigh_router);
  1138. batadv_orig_node_free_ref(orig_node);
  1139. }
  1140. static int batadv_iv_ogm_receive(struct sk_buff *skb,
  1141. struct batadv_hard_iface *if_incoming)
  1142. {
  1143. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  1144. struct batadv_ogm_packet *batadv_ogm_packet;
  1145. struct ethhdr *ethhdr;
  1146. int buff_pos = 0, packet_len;
  1147. unsigned char *tt_buff, *packet_buff;
  1148. bool ret;
  1149. uint8_t *packet_pos;
  1150. ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
  1151. if (!ret)
  1152. return NET_RX_DROP;
  1153. /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
  1154. * that does not have B.A.T.M.A.N. IV enabled ?
  1155. */
  1156. if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit)
  1157. return NET_RX_DROP;
  1158. batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
  1159. batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
  1160. skb->len + ETH_HLEN);
  1161. packet_len = skb_headlen(skb);
  1162. ethhdr = eth_hdr(skb);
  1163. packet_buff = skb->data;
  1164. batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
  1165. /* unpack the aggregated packets and process them one by one */
  1166. while (batadv_iv_ogm_aggr_packet(buff_pos, packet_len,
  1167. batadv_ogm_packet->tt_num_changes)) {
  1168. tt_buff = packet_buff + buff_pos + BATADV_OGM_HLEN;
  1169. batadv_iv_ogm_process(ethhdr, batadv_ogm_packet, tt_buff,
  1170. if_incoming);
  1171. buff_pos += BATADV_OGM_HLEN;
  1172. buff_pos += batadv_tt_len(batadv_ogm_packet->tt_num_changes);
  1173. packet_pos = packet_buff + buff_pos;
  1174. batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
  1175. }
  1176. kfree_skb(skb);
  1177. return NET_RX_SUCCESS;
  1178. }
  1179. static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
  1180. .name = "BATMAN_IV",
  1181. .bat_iface_enable = batadv_iv_ogm_iface_enable,
  1182. .bat_iface_disable = batadv_iv_ogm_iface_disable,
  1183. .bat_iface_update_mac = batadv_iv_ogm_iface_update_mac,
  1184. .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set,
  1185. .bat_ogm_schedule = batadv_iv_ogm_schedule,
  1186. .bat_ogm_emit = batadv_iv_ogm_emit,
  1187. };
  1188. int __init batadv_iv_init(void)
  1189. {
  1190. int ret;
  1191. /* batman originator packet */
  1192. ret = batadv_recv_handler_register(BATADV_IV_OGM,
  1193. batadv_iv_ogm_receive);
  1194. if (ret < 0)
  1195. goto out;
  1196. ret = batadv_algo_register(&batadv_batman_iv);
  1197. if (ret < 0)
  1198. goto handler_unregister;
  1199. goto out;
  1200. handler_unregister:
  1201. batadv_recv_handler_unregister(BATADV_IV_OGM);
  1202. out:
  1203. return ret;
  1204. }