bat_iv_ogm.c 38 KB

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