bat_iv_ogm.c 42 KB

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