bat_iv_ogm.c 36 KB

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