bat_iv_ogm.c 35 KB

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