bat_iv_ogm.c 36 KB

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