bat_iv_ogm.c 40 KB

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