bat_iv_ogm.c 40 KB

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