bat_iv_ogm.c 41 KB

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