bat_iv_ogm.c 36 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203
  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 %d, 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 %d, 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 (window_protected(bat_priv, seq_diff,
  720. &orig_node->batman_seqno_reset))
  721. goto out;
  722. rcu_read_lock();
  723. hlist_for_each_entry_rcu(tmp_neigh_node, node,
  724. &orig_node->neigh_list, list) {
  725. is_duplicate |= bat_test_bit(tmp_neigh_node->real_bits,
  726. orig_node->last_real_seqno,
  727. batman_ogm_packet->seqno);
  728. if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
  729. (tmp_neigh_node->if_incoming == if_incoming))
  730. set_mark = 1;
  731. else
  732. set_mark = 0;
  733. /* if the window moved, set the update flag. */
  734. need_update |= bit_get_packet(bat_priv,
  735. tmp_neigh_node->real_bits,
  736. seq_diff, set_mark);
  737. tmp_neigh_node->real_packet_count =
  738. bitmap_weight(tmp_neigh_node->real_bits,
  739. TQ_LOCAL_WINDOW_SIZE);
  740. }
  741. rcu_read_unlock();
  742. if (need_update) {
  743. bat_dbg(DBG_BATMAN, bat_priv,
  744. "updating last_seqno: old %d, new %d\n",
  745. orig_node->last_real_seqno, batman_ogm_packet->seqno);
  746. orig_node->last_real_seqno = batman_ogm_packet->seqno;
  747. }
  748. ret = is_duplicate;
  749. out:
  750. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  751. orig_node_free_ref(orig_node);
  752. return ret;
  753. }
  754. static void bat_iv_ogm_process(const struct ethhdr *ethhdr,
  755. struct batman_ogm_packet *batman_ogm_packet,
  756. const unsigned char *tt_buff,
  757. struct hard_iface *if_incoming)
  758. {
  759. struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  760. struct hard_iface *hard_iface;
  761. struct orig_node *orig_neigh_node, *orig_node;
  762. struct neigh_node *router = NULL, *router_router = NULL;
  763. struct neigh_node *orig_neigh_router = NULL;
  764. int has_directlink_flag;
  765. int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
  766. int is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
  767. int is_duplicate;
  768. uint32_t if_incoming_seqno;
  769. /* Silently drop when the batman packet is actually not a
  770. * correct packet.
  771. *
  772. * This might happen if a packet is padded (e.g. Ethernet has a
  773. * minimum frame length of 64 byte) and the aggregation interprets
  774. * it as an additional length.
  775. *
  776. * TODO: A more sane solution would be to have a bit in the
  777. * batman_ogm_packet to detect whether the packet is the last
  778. * packet in an aggregation. Here we expect that the padding
  779. * is always zero (or not 0x01)
  780. */
  781. if (batman_ogm_packet->header.packet_type != BAT_IV_OGM)
  782. return;
  783. /* could be changed by schedule_own_packet() */
  784. if_incoming_seqno = atomic_read(&if_incoming->seqno);
  785. has_directlink_flag = (batman_ogm_packet->flags & DIRECTLINK ? 1 : 0);
  786. is_single_hop_neigh = (compare_eth(ethhdr->h_source,
  787. batman_ogm_packet->orig) ? 1 : 0);
  788. bat_dbg(DBG_BATMAN, bat_priv,
  789. "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",
  790. ethhdr->h_source, if_incoming->net_dev->name,
  791. if_incoming->net_dev->dev_addr, batman_ogm_packet->orig,
  792. batman_ogm_packet->prev_sender, batman_ogm_packet->seqno,
  793. batman_ogm_packet->ttvn, batman_ogm_packet->tt_crc,
  794. batman_ogm_packet->tt_num_changes, batman_ogm_packet->tq,
  795. batman_ogm_packet->header.ttl,
  796. batman_ogm_packet->header.version, has_directlink_flag);
  797. rcu_read_lock();
  798. list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
  799. if (hard_iface->if_status != IF_ACTIVE)
  800. continue;
  801. if (hard_iface->soft_iface != if_incoming->soft_iface)
  802. continue;
  803. if (compare_eth(ethhdr->h_source,
  804. hard_iface->net_dev->dev_addr))
  805. is_my_addr = 1;
  806. if (compare_eth(batman_ogm_packet->orig,
  807. hard_iface->net_dev->dev_addr))
  808. is_my_orig = 1;
  809. if (compare_eth(batman_ogm_packet->prev_sender,
  810. hard_iface->net_dev->dev_addr))
  811. is_my_oldorig = 1;
  812. if (is_broadcast_ether_addr(ethhdr->h_source))
  813. is_broadcast = 1;
  814. }
  815. rcu_read_unlock();
  816. if (batman_ogm_packet->header.version != COMPAT_VERSION) {
  817. bat_dbg(DBG_BATMAN, bat_priv,
  818. "Drop packet: incompatible batman version (%i)\n",
  819. batman_ogm_packet->header.version);
  820. return;
  821. }
  822. if (is_my_addr) {
  823. bat_dbg(DBG_BATMAN, bat_priv,
  824. "Drop packet: received my own broadcast (sender: %pM)\n",
  825. ethhdr->h_source);
  826. return;
  827. }
  828. if (is_broadcast) {
  829. bat_dbg(DBG_BATMAN, bat_priv,
  830. "Drop packet: ignoring all packets with broadcast source addr (sender: %pM)\n",
  831. ethhdr->h_source);
  832. return;
  833. }
  834. if (is_my_orig) {
  835. unsigned long *word;
  836. int offset;
  837. orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
  838. if (!orig_neigh_node)
  839. return;
  840. /* neighbor has to indicate direct link and it has to
  841. * come via the corresponding interface */
  842. /* save packet seqno for bidirectional check */
  843. if (has_directlink_flag &&
  844. compare_eth(if_incoming->net_dev->dev_addr,
  845. batman_ogm_packet->orig)) {
  846. offset = if_incoming->if_num * NUM_WORDS;
  847. spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
  848. word = &(orig_neigh_node->bcast_own[offset]);
  849. bat_set_bit(word,
  850. if_incoming_seqno -
  851. batman_ogm_packet->seqno - 2);
  852. orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
  853. bitmap_weight(word, TQ_LOCAL_WINDOW_SIZE);
  854. spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
  855. }
  856. bat_dbg(DBG_BATMAN, bat_priv,
  857. "Drop packet: originator packet from myself (via neighbor)\n");
  858. orig_node_free_ref(orig_neigh_node);
  859. return;
  860. }
  861. if (is_my_oldorig) {
  862. bat_dbg(DBG_BATMAN, bat_priv,
  863. "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
  864. ethhdr->h_source);
  865. return;
  866. }
  867. orig_node = get_orig_node(bat_priv, batman_ogm_packet->orig);
  868. if (!orig_node)
  869. return;
  870. is_duplicate = bat_iv_ogm_update_seqnos(ethhdr, batman_ogm_packet,
  871. if_incoming);
  872. if (is_duplicate == -1) {
  873. bat_dbg(DBG_BATMAN, bat_priv,
  874. "Drop packet: packet within seqno protection time (sender: %pM)\n",
  875. ethhdr->h_source);
  876. goto out;
  877. }
  878. if (batman_ogm_packet->tq == 0) {
  879. bat_dbg(DBG_BATMAN, bat_priv,
  880. "Drop packet: originator packet with tq equal 0\n");
  881. goto out;
  882. }
  883. router = orig_node_get_router(orig_node);
  884. if (router)
  885. router_router = orig_node_get_router(router->orig_node);
  886. /* avoid temporary routing loops */
  887. if (router && router_router &&
  888. (compare_eth(router->addr, batman_ogm_packet->prev_sender)) &&
  889. !(compare_eth(batman_ogm_packet->orig,
  890. batman_ogm_packet->prev_sender)) &&
  891. (compare_eth(router->addr, router_router->addr))) {
  892. bat_dbg(DBG_BATMAN, bat_priv,
  893. "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
  894. ethhdr->h_source);
  895. goto out;
  896. }
  897. /* if sender is a direct neighbor the sender mac equals
  898. * originator mac */
  899. orig_neigh_node = (is_single_hop_neigh ?
  900. orig_node :
  901. get_orig_node(bat_priv, ethhdr->h_source));
  902. if (!orig_neigh_node)
  903. goto out;
  904. orig_neigh_router = orig_node_get_router(orig_neigh_node);
  905. /* drop packet if sender is not a direct neighbor and if we
  906. * don't route towards it */
  907. if (!is_single_hop_neigh && (!orig_neigh_router)) {
  908. bat_dbg(DBG_BATMAN, bat_priv,
  909. "Drop packet: OGM via unknown neighbor!\n");
  910. goto out_neigh;
  911. }
  912. is_bidirectional = bat_iv_ogm_calc_tq(orig_node, orig_neigh_node,
  913. batman_ogm_packet, if_incoming);
  914. bonding_save_primary(orig_node, orig_neigh_node, batman_ogm_packet);
  915. /* update ranking if it is not a duplicate or has the same
  916. * seqno and similar ttl as the non-duplicate */
  917. if (is_bidirectional &&
  918. (!is_duplicate ||
  919. ((orig_node->last_real_seqno == batman_ogm_packet->seqno) &&
  920. (orig_node->last_ttl - 3 <= batman_ogm_packet->header.ttl))))
  921. bat_iv_ogm_orig_update(bat_priv, orig_node, ethhdr,
  922. batman_ogm_packet, if_incoming,
  923. tt_buff, is_duplicate);
  924. /* is single hop (direct) neighbor */
  925. if (is_single_hop_neigh) {
  926. /* mark direct link on incoming interface */
  927. bat_iv_ogm_forward(orig_node, ethhdr, batman_ogm_packet,
  928. 1, if_incoming);
  929. bat_dbg(DBG_BATMAN, bat_priv,
  930. "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
  931. goto out_neigh;
  932. }
  933. /* multihop originator */
  934. if (!is_bidirectional) {
  935. bat_dbg(DBG_BATMAN, bat_priv,
  936. "Drop packet: not received via bidirectional link\n");
  937. goto out_neigh;
  938. }
  939. if (is_duplicate) {
  940. bat_dbg(DBG_BATMAN, bat_priv,
  941. "Drop packet: duplicate packet received\n");
  942. goto out_neigh;
  943. }
  944. bat_dbg(DBG_BATMAN, bat_priv,
  945. "Forwarding packet: rebroadcast originator packet\n");
  946. bat_iv_ogm_forward(orig_node, ethhdr, batman_ogm_packet,
  947. 0, if_incoming);
  948. out_neigh:
  949. if ((orig_neigh_node) && (!is_single_hop_neigh))
  950. orig_node_free_ref(orig_neigh_node);
  951. out:
  952. if (router)
  953. neigh_node_free_ref(router);
  954. if (router_router)
  955. neigh_node_free_ref(router_router);
  956. if (orig_neigh_router)
  957. neigh_node_free_ref(orig_neigh_router);
  958. orig_node_free_ref(orig_node);
  959. }
  960. static void bat_iv_ogm_receive(struct hard_iface *if_incoming,
  961. struct sk_buff *skb)
  962. {
  963. struct batman_ogm_packet *batman_ogm_packet;
  964. struct ethhdr *ethhdr;
  965. int buff_pos = 0, packet_len;
  966. unsigned char *tt_buff, *packet_buff;
  967. packet_len = skb_headlen(skb);
  968. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  969. packet_buff = skb->data;
  970. batman_ogm_packet = (struct batman_ogm_packet *)packet_buff;
  971. /* unpack the aggregated packets and process them one by one */
  972. do {
  973. /* network to host order for our 32bit seqno and the
  974. orig_interval */
  975. batman_ogm_packet->seqno = ntohl(batman_ogm_packet->seqno);
  976. batman_ogm_packet->tt_crc = ntohs(batman_ogm_packet->tt_crc);
  977. tt_buff = packet_buff + buff_pos + BATMAN_OGM_HLEN;
  978. bat_iv_ogm_process(ethhdr, batman_ogm_packet,
  979. tt_buff, if_incoming);
  980. buff_pos += BATMAN_OGM_HLEN +
  981. tt_len(batman_ogm_packet->tt_num_changes);
  982. batman_ogm_packet = (struct batman_ogm_packet *)
  983. (packet_buff + buff_pos);
  984. } while (bat_iv_ogm_aggr_packet(buff_pos, packet_len,
  985. batman_ogm_packet->tt_num_changes));
  986. }
  987. static struct bat_algo_ops batman_iv __read_mostly = {
  988. .name = "BATMAN IV",
  989. .bat_iface_enable = bat_iv_ogm_iface_enable,
  990. .bat_iface_disable = bat_iv_ogm_iface_disable,
  991. .bat_primary_iface_set = bat_iv_ogm_primary_iface_set,
  992. .bat_ogm_update_mac = bat_iv_ogm_update_mac,
  993. .bat_ogm_schedule = bat_iv_ogm_schedule,
  994. .bat_ogm_emit = bat_iv_ogm_emit,
  995. .bat_ogm_receive = bat_iv_ogm_receive,
  996. };
  997. int __init bat_iv_init(void)
  998. {
  999. return bat_algo_register(&batman_iv);
  1000. }