bat_iv_ogm.c 41 KB

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