bat_iv_ogm.c 41 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339
  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. static struct batadv_neigh_node *
  30. batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
  31. const uint8_t *neigh_addr,
  32. struct batadv_orig_node *orig_node,
  33. struct batadv_orig_node *orig_neigh, __be32 seqno)
  34. {
  35. struct batadv_neigh_node *neigh_node;
  36. neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr,
  37. ntohl(seqno));
  38. if (!neigh_node)
  39. goto out;
  40. INIT_LIST_HEAD(&neigh_node->bonding_list);
  41. neigh_node->orig_node = orig_neigh;
  42. neigh_node->if_incoming = hard_iface;
  43. spin_lock_bh(&orig_node->neigh_list_lock);
  44. hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
  45. spin_unlock_bh(&orig_node->neigh_list_lock);
  46. out:
  47. return neigh_node;
  48. }
  49. static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
  50. {
  51. struct batadv_ogm_packet *batadv_ogm_packet;
  52. unsigned char *ogm_buff;
  53. uint32_t random_seqno;
  54. int res = -ENOMEM;
  55. /* randomize initial seqno to avoid collision */
  56. get_random_bytes(&random_seqno, sizeof(random_seqno));
  57. atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
  58. hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
  59. ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
  60. if (!ogm_buff)
  61. goto out;
  62. hard_iface->bat_iv.ogm_buff = ogm_buff;
  63. batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
  64. batadv_ogm_packet->header.packet_type = BATADV_IV_OGM;
  65. batadv_ogm_packet->header.version = BATADV_COMPAT_VERSION;
  66. batadv_ogm_packet->header.ttl = 2;
  67. batadv_ogm_packet->flags = BATADV_NO_FLAGS;
  68. batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
  69. batadv_ogm_packet->tt_num_changes = 0;
  70. batadv_ogm_packet->ttvn = 0;
  71. res = 0;
  72. out:
  73. return res;
  74. }
  75. static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
  76. {
  77. kfree(hard_iface->bat_iv.ogm_buff);
  78. hard_iface->bat_iv.ogm_buff = NULL;
  79. }
  80. static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
  81. {
  82. struct batadv_ogm_packet *batadv_ogm_packet;
  83. unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
  84. batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
  85. memcpy(batadv_ogm_packet->orig,
  86. hard_iface->net_dev->dev_addr, ETH_ALEN);
  87. memcpy(batadv_ogm_packet->prev_sender,
  88. hard_iface->net_dev->dev_addr, ETH_ALEN);
  89. }
  90. static void
  91. batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
  92. {
  93. struct batadv_ogm_packet *batadv_ogm_packet;
  94. unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
  95. batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
  96. batadv_ogm_packet->flags = BATADV_PRIMARIES_FIRST_HOP;
  97. batadv_ogm_packet->header.ttl = BATADV_TTL;
  98. }
  99. /* when do we schedule our own ogm to be sent */
  100. static unsigned long
  101. batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
  102. {
  103. unsigned int msecs;
  104. msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
  105. msecs += 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 (neigh_node)
  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_ogm_packet->seqno);
  593. batadv_orig_node_free_ref(orig_tmp);
  594. if (!neigh_node)
  595. goto unlock;
  596. } else
  597. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  598. "Updating existing last-hop neighbor of originator\n");
  599. rcu_read_unlock();
  600. orig_node->flags = batadv_ogm_packet->flags;
  601. neigh_node->last_seen = jiffies;
  602. spin_lock_bh(&neigh_node->lq_update_lock);
  603. batadv_ring_buffer_set(neigh_node->tq_recv,
  604. &neigh_node->tq_index,
  605. batadv_ogm_packet->tq);
  606. neigh_node->tq_avg = batadv_ring_buffer_avg(neigh_node->tq_recv);
  607. spin_unlock_bh(&neigh_node->lq_update_lock);
  608. if (!is_duplicate) {
  609. orig_node->last_ttl = batadv_ogm_packet->header.ttl;
  610. neigh_node->last_ttl = batadv_ogm_packet->header.ttl;
  611. }
  612. batadv_bonding_candidate_add(orig_node, neigh_node);
  613. /* if this neighbor already is our next hop there is nothing
  614. * to change
  615. */
  616. router = batadv_orig_node_get_router(orig_node);
  617. if (router == neigh_node)
  618. goto update_tt;
  619. /* if this neighbor does not offer a better TQ we won't consider it */
  620. if (router && (router->tq_avg > neigh_node->tq_avg))
  621. goto update_tt;
  622. /* if the TQ is the same and the link not more symmetric we
  623. * won't consider it either
  624. */
  625. if (router && (neigh_node->tq_avg == router->tq_avg)) {
  626. orig_node_tmp = router->orig_node;
  627. spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
  628. if_num = router->if_incoming->if_num;
  629. sum_orig = orig_node_tmp->bcast_own_sum[if_num];
  630. spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
  631. orig_node_tmp = neigh_node->orig_node;
  632. spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
  633. if_num = neigh_node->if_incoming->if_num;
  634. sum_neigh = orig_node_tmp->bcast_own_sum[if_num];
  635. spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
  636. if (sum_orig >= sum_neigh)
  637. goto update_tt;
  638. }
  639. batadv_update_route(bat_priv, orig_node, neigh_node);
  640. update_tt:
  641. /* I have to check for transtable changes only if the OGM has been
  642. * sent through a primary interface
  643. */
  644. if (((batadv_ogm_packet->orig != ethhdr->h_source) &&
  645. (batadv_ogm_packet->header.ttl > 2)) ||
  646. (batadv_ogm_packet->flags & BATADV_PRIMARIES_FIRST_HOP))
  647. batadv_tt_update_orig(bat_priv, orig_node, tt_buff,
  648. batadv_ogm_packet->tt_num_changes,
  649. batadv_ogm_packet->ttvn,
  650. ntohs(batadv_ogm_packet->tt_crc));
  651. if (orig_node->gw_flags != batadv_ogm_packet->gw_flags)
  652. batadv_gw_node_update(bat_priv, orig_node,
  653. batadv_ogm_packet->gw_flags);
  654. orig_node->gw_flags = batadv_ogm_packet->gw_flags;
  655. /* restart gateway selection if fast or late switching was enabled */
  656. if ((orig_node->gw_flags) &&
  657. (atomic_read(&bat_priv->gw_mode) == BATADV_GW_MODE_CLIENT) &&
  658. (atomic_read(&bat_priv->gw_sel_class) > 2))
  659. batadv_gw_check_election(bat_priv, orig_node);
  660. goto out;
  661. unlock:
  662. rcu_read_unlock();
  663. out:
  664. if (neigh_node)
  665. batadv_neigh_node_free_ref(neigh_node);
  666. if (router)
  667. batadv_neigh_node_free_ref(router);
  668. }
  669. static int batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
  670. struct batadv_orig_node *orig_neigh_node,
  671. struct batadv_ogm_packet *batadv_ogm_packet,
  672. struct batadv_hard_iface *if_incoming)
  673. {
  674. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  675. struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
  676. uint8_t total_count;
  677. uint8_t orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
  678. unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
  679. int tq_asym_penalty, inv_asym_penalty, ret = 0;
  680. unsigned int combined_tq;
  681. /* find corresponding one hop neighbor */
  682. rcu_read_lock();
  683. hlist_for_each_entry_rcu(tmp_neigh_node,
  684. &orig_neigh_node->neigh_list, list) {
  685. if (!batadv_compare_eth(tmp_neigh_node->addr,
  686. orig_neigh_node->orig))
  687. continue;
  688. if (tmp_neigh_node->if_incoming != if_incoming)
  689. continue;
  690. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  691. continue;
  692. neigh_node = tmp_neigh_node;
  693. break;
  694. }
  695. rcu_read_unlock();
  696. if (!neigh_node)
  697. neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
  698. orig_neigh_node->orig,
  699. orig_neigh_node,
  700. orig_neigh_node,
  701. batadv_ogm_packet->seqno);
  702. if (!neigh_node)
  703. goto out;
  704. /* if orig_node is direct neighbor update neigh_node last_seen */
  705. if (orig_node == orig_neigh_node)
  706. neigh_node->last_seen = jiffies;
  707. orig_node->last_seen = jiffies;
  708. /* find packet count of corresponding one hop neighbor */
  709. spin_lock_bh(&orig_node->ogm_cnt_lock);
  710. orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
  711. neigh_rq_count = neigh_node->real_packet_count;
  712. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  713. /* pay attention to not get a value bigger than 100 % */
  714. if (orig_eq_count > neigh_rq_count)
  715. total_count = neigh_rq_count;
  716. else
  717. total_count = orig_eq_count;
  718. /* if we have too few packets (too less data) we set tq_own to zero
  719. * if we receive too few packets it is not considered bidirectional
  720. */
  721. if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
  722. neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
  723. tq_own = 0;
  724. else
  725. /* neigh_node->real_packet_count is never zero as we
  726. * only purge old information when getting new
  727. * information
  728. */
  729. tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
  730. /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
  731. * affect the nearly-symmetric links only a little, but
  732. * punishes asymmetric links more. This will give a value
  733. * between 0 and TQ_MAX_VALUE
  734. */
  735. neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
  736. neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
  737. neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
  738. BATADV_TQ_LOCAL_WINDOW_SIZE *
  739. BATADV_TQ_LOCAL_WINDOW_SIZE;
  740. inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
  741. inv_asym_penalty /= neigh_rq_max_cube;
  742. tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
  743. combined_tq = batadv_ogm_packet->tq * tq_own * tq_asym_penalty;
  744. combined_tq /= BATADV_TQ_MAX_VALUE * BATADV_TQ_MAX_VALUE;
  745. batadv_ogm_packet->tq = combined_tq;
  746. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  747. "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n",
  748. orig_node->orig, orig_neigh_node->orig, total_count,
  749. neigh_rq_count, tq_own,
  750. tq_asym_penalty, batadv_ogm_packet->tq);
  751. /* if link has the minimum required transmission quality
  752. * consider it bidirectional
  753. */
  754. if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
  755. ret = 1;
  756. out:
  757. if (neigh_node)
  758. batadv_neigh_node_free_ref(neigh_node);
  759. return ret;
  760. }
  761. /* processes a batman packet for all interfaces, adjusts the sequence number and
  762. * finds out whether it is a duplicate.
  763. * returns:
  764. * 1 the packet is a duplicate
  765. * 0 the packet has not yet been received
  766. * -1 the packet is old and has been received while the seqno window
  767. * was protected. Caller should drop it.
  768. */
  769. static int
  770. batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
  771. const struct batadv_ogm_packet *batadv_ogm_packet,
  772. const struct batadv_hard_iface *if_incoming)
  773. {
  774. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  775. struct batadv_orig_node *orig_node;
  776. struct batadv_neigh_node *tmp_neigh_node;
  777. int is_duplicate = 0;
  778. int32_t seq_diff;
  779. int need_update = 0;
  780. int set_mark, ret = -1;
  781. uint32_t seqno = ntohl(batadv_ogm_packet->seqno);
  782. uint8_t *neigh_addr;
  783. uint8_t packet_count;
  784. orig_node = batadv_get_orig_node(bat_priv, batadv_ogm_packet->orig);
  785. if (!orig_node)
  786. return 0;
  787. spin_lock_bh(&orig_node->ogm_cnt_lock);
  788. seq_diff = seqno - orig_node->last_real_seqno;
  789. /* signalize caller that the packet is to be dropped. */
  790. if (!hlist_empty(&orig_node->neigh_list) &&
  791. batadv_window_protected(bat_priv, seq_diff,
  792. &orig_node->batman_seqno_reset))
  793. goto out;
  794. rcu_read_lock();
  795. hlist_for_each_entry_rcu(tmp_neigh_node,
  796. &orig_node->neigh_list, list) {
  797. is_duplicate |= batadv_test_bit(tmp_neigh_node->real_bits,
  798. orig_node->last_real_seqno,
  799. seqno);
  800. neigh_addr = tmp_neigh_node->addr;
  801. if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
  802. tmp_neigh_node->if_incoming == if_incoming)
  803. set_mark = 1;
  804. else
  805. set_mark = 0;
  806. /* if the window moved, set the update flag. */
  807. need_update |= batadv_bit_get_packet(bat_priv,
  808. tmp_neigh_node->real_bits,
  809. seq_diff, set_mark);
  810. packet_count = bitmap_weight(tmp_neigh_node->real_bits,
  811. BATADV_TQ_LOCAL_WINDOW_SIZE);
  812. tmp_neigh_node->real_packet_count = packet_count;
  813. }
  814. rcu_read_unlock();
  815. if (need_update) {
  816. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  817. "updating last_seqno: old %u, new %u\n",
  818. orig_node->last_real_seqno, seqno);
  819. orig_node->last_real_seqno = seqno;
  820. }
  821. ret = is_duplicate;
  822. out:
  823. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  824. batadv_orig_node_free_ref(orig_node);
  825. return ret;
  826. }
  827. static void batadv_iv_ogm_process(const struct ethhdr *ethhdr,
  828. struct batadv_ogm_packet *batadv_ogm_packet,
  829. const unsigned char *tt_buff,
  830. struct batadv_hard_iface *if_incoming)
  831. {
  832. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  833. struct batadv_hard_iface *hard_iface;
  834. struct batadv_orig_node *orig_neigh_node, *orig_node;
  835. struct batadv_neigh_node *router = NULL, *router_router = NULL;
  836. struct batadv_neigh_node *orig_neigh_router = NULL;
  837. int has_directlink_flag;
  838. int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
  839. int is_broadcast = 0, is_bidirect;
  840. bool is_single_hop_neigh = false;
  841. bool is_from_best_next_hop = false;
  842. int is_duplicate, sameseq, simlar_ttl;
  843. uint32_t if_incoming_seqno;
  844. uint8_t *prev_sender;
  845. /* Silently drop when the batman packet is actually not a
  846. * correct packet.
  847. *
  848. * This might happen if a packet is padded (e.g. Ethernet has a
  849. * minimum frame length of 64 byte) and the aggregation interprets
  850. * it as an additional length.
  851. *
  852. * TODO: A more sane solution would be to have a bit in the
  853. * batadv_ogm_packet to detect whether the packet is the last
  854. * packet in an aggregation. Here we expect that the padding
  855. * is always zero (or not 0x01)
  856. */
  857. if (batadv_ogm_packet->header.packet_type != BATADV_IV_OGM)
  858. return;
  859. /* could be changed by schedule_own_packet() */
  860. if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
  861. if (batadv_ogm_packet->flags & BATADV_DIRECTLINK)
  862. has_directlink_flag = 1;
  863. else
  864. has_directlink_flag = 0;
  865. if (batadv_compare_eth(ethhdr->h_source, batadv_ogm_packet->orig))
  866. is_single_hop_neigh = true;
  867. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  868. "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",
  869. ethhdr->h_source, if_incoming->net_dev->name,
  870. if_incoming->net_dev->dev_addr, batadv_ogm_packet->orig,
  871. batadv_ogm_packet->prev_sender,
  872. ntohl(batadv_ogm_packet->seqno), batadv_ogm_packet->ttvn,
  873. ntohs(batadv_ogm_packet->tt_crc),
  874. batadv_ogm_packet->tt_num_changes, batadv_ogm_packet->tq,
  875. batadv_ogm_packet->header.ttl,
  876. batadv_ogm_packet->header.version, has_directlink_flag);
  877. rcu_read_lock();
  878. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  879. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  880. continue;
  881. if (hard_iface->soft_iface != if_incoming->soft_iface)
  882. continue;
  883. if (batadv_compare_eth(ethhdr->h_source,
  884. hard_iface->net_dev->dev_addr))
  885. is_my_addr = 1;
  886. if (batadv_compare_eth(batadv_ogm_packet->orig,
  887. hard_iface->net_dev->dev_addr))
  888. is_my_orig = 1;
  889. if (batadv_compare_eth(batadv_ogm_packet->prev_sender,
  890. hard_iface->net_dev->dev_addr))
  891. is_my_oldorig = 1;
  892. if (is_broadcast_ether_addr(ethhdr->h_source))
  893. is_broadcast = 1;
  894. }
  895. rcu_read_unlock();
  896. if (batadv_ogm_packet->header.version != BATADV_COMPAT_VERSION) {
  897. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  898. "Drop packet: incompatible batman version (%i)\n",
  899. batadv_ogm_packet->header.version);
  900. return;
  901. }
  902. if (is_my_addr) {
  903. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  904. "Drop packet: received my own broadcast (sender: %pM)\n",
  905. ethhdr->h_source);
  906. return;
  907. }
  908. if (is_broadcast) {
  909. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  910. "Drop packet: ignoring all packets with broadcast source addr (sender: %pM)\n",
  911. ethhdr->h_source);
  912. return;
  913. }
  914. if (is_my_orig) {
  915. unsigned long *word;
  916. int offset;
  917. int32_t bit_pos;
  918. int16_t if_num;
  919. uint8_t *weight;
  920. orig_neigh_node = batadv_get_orig_node(bat_priv,
  921. ethhdr->h_source);
  922. if (!orig_neigh_node)
  923. return;
  924. /* neighbor has to indicate direct link and it has to
  925. * come via the corresponding interface
  926. * save packet seqno for bidirectional check
  927. */
  928. if (has_directlink_flag &&
  929. batadv_compare_eth(if_incoming->net_dev->dev_addr,
  930. batadv_ogm_packet->orig)) {
  931. if_num = if_incoming->if_num;
  932. offset = if_num * BATADV_NUM_WORDS;
  933. spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
  934. word = &(orig_neigh_node->bcast_own[offset]);
  935. bit_pos = if_incoming_seqno - 2;
  936. bit_pos -= ntohl(batadv_ogm_packet->seqno);
  937. batadv_set_bit(word, bit_pos);
  938. weight = &orig_neigh_node->bcast_own_sum[if_num];
  939. *weight = bitmap_weight(word,
  940. BATADV_TQ_LOCAL_WINDOW_SIZE);
  941. spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
  942. }
  943. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  944. "Drop packet: originator packet from myself (via neighbor)\n");
  945. batadv_orig_node_free_ref(orig_neigh_node);
  946. return;
  947. }
  948. if (is_my_oldorig) {
  949. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  950. "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
  951. ethhdr->h_source);
  952. return;
  953. }
  954. if (batadv_ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
  955. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  956. "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
  957. ethhdr->h_source);
  958. return;
  959. }
  960. orig_node = batadv_get_orig_node(bat_priv, batadv_ogm_packet->orig);
  961. if (!orig_node)
  962. return;
  963. is_duplicate = batadv_iv_ogm_update_seqnos(ethhdr, batadv_ogm_packet,
  964. if_incoming);
  965. if (is_duplicate == -1) {
  966. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  967. "Drop packet: packet within seqno protection time (sender: %pM)\n",
  968. ethhdr->h_source);
  969. goto out;
  970. }
  971. if (batadv_ogm_packet->tq == 0) {
  972. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  973. "Drop packet: originator packet with tq equal 0\n");
  974. goto out;
  975. }
  976. router = batadv_orig_node_get_router(orig_node);
  977. if (router)
  978. router_router = batadv_orig_node_get_router(router->orig_node);
  979. if ((router && router->tq_avg != 0) &&
  980. (batadv_compare_eth(router->addr, ethhdr->h_source)))
  981. is_from_best_next_hop = true;
  982. prev_sender = batadv_ogm_packet->prev_sender;
  983. /* avoid temporary routing loops */
  984. if (router && router_router &&
  985. (batadv_compare_eth(router->addr, prev_sender)) &&
  986. !(batadv_compare_eth(batadv_ogm_packet->orig, prev_sender)) &&
  987. (batadv_compare_eth(router->addr, router_router->addr))) {
  988. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  989. "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
  990. ethhdr->h_source);
  991. goto out;
  992. }
  993. /* if sender is a direct neighbor the sender mac equals
  994. * originator mac
  995. */
  996. if (is_single_hop_neigh)
  997. orig_neigh_node = orig_node;
  998. else
  999. orig_neigh_node = batadv_get_orig_node(bat_priv,
  1000. ethhdr->h_source);
  1001. if (!orig_neigh_node)
  1002. goto out;
  1003. orig_neigh_router = batadv_orig_node_get_router(orig_neigh_node);
  1004. /* drop packet if sender is not a direct neighbor and if we
  1005. * don't route towards it
  1006. */
  1007. if (!is_single_hop_neigh && (!orig_neigh_router)) {
  1008. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1009. "Drop packet: OGM via unknown neighbor!\n");
  1010. goto out_neigh;
  1011. }
  1012. is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
  1013. batadv_ogm_packet, if_incoming);
  1014. batadv_bonding_save_primary(orig_node, orig_neigh_node,
  1015. batadv_ogm_packet);
  1016. /* update ranking if it is not a duplicate or has the same
  1017. * seqno and similar ttl as the non-duplicate
  1018. */
  1019. sameseq = orig_node->last_real_seqno == ntohl(batadv_ogm_packet->seqno);
  1020. simlar_ttl = orig_node->last_ttl - 3 <= batadv_ogm_packet->header.ttl;
  1021. if (is_bidirect && (!is_duplicate || (sameseq && simlar_ttl)))
  1022. batadv_iv_ogm_orig_update(bat_priv, orig_node, ethhdr,
  1023. batadv_ogm_packet, if_incoming,
  1024. tt_buff, is_duplicate);
  1025. /* is single hop (direct) neighbor */
  1026. if (is_single_hop_neigh) {
  1027. /* mark direct link on incoming interface */
  1028. batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet,
  1029. is_single_hop_neigh,
  1030. is_from_best_next_hop, if_incoming);
  1031. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1032. "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
  1033. goto out_neigh;
  1034. }
  1035. /* multihop originator */
  1036. if (!is_bidirect) {
  1037. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1038. "Drop packet: not received via bidirectional link\n");
  1039. goto out_neigh;
  1040. }
  1041. if (is_duplicate) {
  1042. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1043. "Drop packet: duplicate packet received\n");
  1044. goto out_neigh;
  1045. }
  1046. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1047. "Forwarding packet: rebroadcast originator packet\n");
  1048. batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet,
  1049. is_single_hop_neigh, is_from_best_next_hop,
  1050. if_incoming);
  1051. out_neigh:
  1052. if ((orig_neigh_node) && (!is_single_hop_neigh))
  1053. batadv_orig_node_free_ref(orig_neigh_node);
  1054. out:
  1055. if (router)
  1056. batadv_neigh_node_free_ref(router);
  1057. if (router_router)
  1058. batadv_neigh_node_free_ref(router_router);
  1059. if (orig_neigh_router)
  1060. batadv_neigh_node_free_ref(orig_neigh_router);
  1061. batadv_orig_node_free_ref(orig_node);
  1062. }
  1063. static int batadv_iv_ogm_receive(struct sk_buff *skb,
  1064. struct batadv_hard_iface *if_incoming)
  1065. {
  1066. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  1067. struct batadv_ogm_packet *batadv_ogm_packet;
  1068. struct ethhdr *ethhdr;
  1069. int buff_pos = 0, packet_len;
  1070. unsigned char *tt_buff, *packet_buff;
  1071. bool ret;
  1072. uint8_t *packet_pos;
  1073. ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
  1074. if (!ret)
  1075. return NET_RX_DROP;
  1076. /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
  1077. * that does not have B.A.T.M.A.N. IV enabled ?
  1078. */
  1079. if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit)
  1080. return NET_RX_DROP;
  1081. batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
  1082. batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
  1083. skb->len + ETH_HLEN);
  1084. packet_len = skb_headlen(skb);
  1085. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  1086. packet_buff = skb->data;
  1087. batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
  1088. /* unpack the aggregated packets and process them one by one */
  1089. do {
  1090. tt_buff = packet_buff + buff_pos + BATADV_OGM_HLEN;
  1091. batadv_iv_ogm_process(ethhdr, batadv_ogm_packet, tt_buff,
  1092. if_incoming);
  1093. buff_pos += BATADV_OGM_HLEN;
  1094. buff_pos += batadv_tt_len(batadv_ogm_packet->tt_num_changes);
  1095. packet_pos = packet_buff + buff_pos;
  1096. batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
  1097. } while (batadv_iv_ogm_aggr_packet(buff_pos, packet_len,
  1098. batadv_ogm_packet->tt_num_changes));
  1099. kfree_skb(skb);
  1100. return NET_RX_SUCCESS;
  1101. }
  1102. static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
  1103. .name = "BATMAN_IV",
  1104. .bat_iface_enable = batadv_iv_ogm_iface_enable,
  1105. .bat_iface_disable = batadv_iv_ogm_iface_disable,
  1106. .bat_iface_update_mac = batadv_iv_ogm_iface_update_mac,
  1107. .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set,
  1108. .bat_ogm_schedule = batadv_iv_ogm_schedule,
  1109. .bat_ogm_emit = batadv_iv_ogm_emit,
  1110. };
  1111. int __init batadv_iv_init(void)
  1112. {
  1113. int ret;
  1114. /* batman originator packet */
  1115. ret = batadv_recv_handler_register(BATADV_IV_OGM,
  1116. batadv_iv_ogm_receive);
  1117. if (ret < 0)
  1118. goto out;
  1119. ret = batadv_algo_register(&batadv_batman_iv);
  1120. if (ret < 0)
  1121. goto handler_unregister;
  1122. goto out;
  1123. handler_unregister:
  1124. batadv_recv_handler_unregister(BATADV_IV_OGM);
  1125. out:
  1126. return ret;
  1127. }