bat_iv_ogm.c 47 KB

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