bat_iv_ogm.c 42 KB

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