bat_iv_ogm.c 39 KB

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