bat_iv_ogm.c 41 KB

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