bat_iv_ogm.c 38 KB

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