bat_iv_ogm.c 36 KB

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