bat_iv_ogm.c 40 KB

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