bat_iv_ogm.c 40 KB

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