bat_iv_ogm.c 42 KB

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