bat_iv_ogm.c 41 KB

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