bat_iv_ogm.c 60 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979
  1. /* Copyright (C) 2007-2014 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, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "main.h"
  18. #include "translation-table.h"
  19. #include "originator.h"
  20. #include "routing.h"
  21. #include "gateway_common.h"
  22. #include "gateway_client.h"
  23. #include "hard-interface.h"
  24. #include "send.h"
  25. #include "bat_algo.h"
  26. #include "network-coding.h"
  27. /**
  28. * enum batadv_dup_status - duplicate status
  29. * @BATADV_NO_DUP: the packet is a duplicate
  30. * @BATADV_ORIG_DUP: OGM is a duplicate in the originator (but not for the
  31. * neighbor)
  32. * @BATADV_NEIGH_DUP: OGM is a duplicate for the neighbor
  33. * @BATADV_PROTECTED: originator is currently protected (after reboot)
  34. */
  35. enum batadv_dup_status {
  36. BATADV_NO_DUP = 0,
  37. BATADV_ORIG_DUP,
  38. BATADV_NEIGH_DUP,
  39. BATADV_PROTECTED,
  40. };
  41. /**
  42. * batadv_ring_buffer_set - update the ring buffer with the given value
  43. * @lq_recv: pointer to the ring buffer
  44. * @lq_index: index to store the value at
  45. * @value: value to store in the ring buffer
  46. */
  47. static void batadv_ring_buffer_set(uint8_t lq_recv[], uint8_t *lq_index,
  48. uint8_t value)
  49. {
  50. lq_recv[*lq_index] = value;
  51. *lq_index = (*lq_index + 1) % BATADV_TQ_GLOBAL_WINDOW_SIZE;
  52. }
  53. /**
  54. * batadv_ring_buffer_set - compute the average of all non-zero values stored
  55. * in the given ring buffer
  56. * @lq_recv: pointer to the ring buffer
  57. *
  58. * Returns computed average value.
  59. */
  60. static uint8_t batadv_ring_buffer_avg(const uint8_t lq_recv[])
  61. {
  62. const uint8_t *ptr;
  63. uint16_t count = 0, i = 0, sum = 0;
  64. ptr = lq_recv;
  65. while (i < BATADV_TQ_GLOBAL_WINDOW_SIZE) {
  66. if (*ptr != 0) {
  67. count++;
  68. sum += *ptr;
  69. }
  70. i++;
  71. ptr++;
  72. }
  73. if (count == 0)
  74. return 0;
  75. return (uint8_t)(sum / count);
  76. }
  77. /**
  78. * batadv_iv_ogm_orig_free - free the private resources allocated for this
  79. * orig_node
  80. * @orig_node: the orig_node for which the resources have to be free'd
  81. */
  82. static void batadv_iv_ogm_orig_free(struct batadv_orig_node *orig_node)
  83. {
  84. kfree(orig_node->bat_iv.bcast_own);
  85. kfree(orig_node->bat_iv.bcast_own_sum);
  86. }
  87. /**
  88. * batadv_iv_ogm_orig_add_if - change the private structures of the orig_node to
  89. * include the new hard-interface
  90. * @orig_node: the orig_node that has to be changed
  91. * @max_if_num: the current amount of interfaces
  92. *
  93. * Returns 0 on success, a negative error code otherwise.
  94. */
  95. static int batadv_iv_ogm_orig_add_if(struct batadv_orig_node *orig_node,
  96. int max_if_num)
  97. {
  98. void *data_ptr;
  99. size_t old_size;
  100. int ret = -ENOMEM;
  101. spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  102. old_size = (max_if_num - 1) * sizeof(unsigned long) * BATADV_NUM_WORDS;
  103. data_ptr = kmalloc_array(max_if_num,
  104. BATADV_NUM_WORDS * sizeof(unsigned long),
  105. GFP_ATOMIC);
  106. if (!data_ptr)
  107. goto unlock;
  108. memcpy(data_ptr, orig_node->bat_iv.bcast_own, old_size);
  109. kfree(orig_node->bat_iv.bcast_own);
  110. orig_node->bat_iv.bcast_own = data_ptr;
  111. data_ptr = kmalloc_array(max_if_num, sizeof(uint8_t), GFP_ATOMIC);
  112. if (!data_ptr) {
  113. kfree(orig_node->bat_iv.bcast_own);
  114. goto unlock;
  115. }
  116. memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
  117. (max_if_num - 1) * sizeof(uint8_t));
  118. kfree(orig_node->bat_iv.bcast_own_sum);
  119. orig_node->bat_iv.bcast_own_sum = data_ptr;
  120. ret = 0;
  121. unlock:
  122. spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  123. return ret;
  124. }
  125. /**
  126. * batadv_iv_ogm_orig_del_if - change the private structures of the orig_node to
  127. * exclude the removed interface
  128. * @orig_node: the orig_node that has to be changed
  129. * @max_if_num: the current amount of interfaces
  130. * @del_if_num: the index of the interface being removed
  131. *
  132. * Returns 0 on success, a negative error code otherwise.
  133. */
  134. static int batadv_iv_ogm_orig_del_if(struct batadv_orig_node *orig_node,
  135. int max_if_num, int del_if_num)
  136. {
  137. int chunk_size, ret = -ENOMEM, if_offset;
  138. void *data_ptr = NULL;
  139. spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  140. /* last interface was removed */
  141. if (max_if_num == 0)
  142. goto free_bcast_own;
  143. chunk_size = sizeof(unsigned long) * BATADV_NUM_WORDS;
  144. data_ptr = kmalloc_array(max_if_num, chunk_size, GFP_ATOMIC);
  145. if (!data_ptr)
  146. goto unlock;
  147. /* copy first part */
  148. memcpy(data_ptr, orig_node->bat_iv.bcast_own, del_if_num * chunk_size);
  149. /* copy second part */
  150. memcpy((char *)data_ptr + del_if_num * chunk_size,
  151. orig_node->bat_iv.bcast_own + ((del_if_num + 1) * chunk_size),
  152. (max_if_num - del_if_num) * chunk_size);
  153. free_bcast_own:
  154. kfree(orig_node->bat_iv.bcast_own);
  155. orig_node->bat_iv.bcast_own = data_ptr;
  156. if (max_if_num == 0)
  157. goto free_own_sum;
  158. data_ptr = kmalloc_array(max_if_num, sizeof(uint8_t), GFP_ATOMIC);
  159. if (!data_ptr) {
  160. kfree(orig_node->bat_iv.bcast_own);
  161. goto unlock;
  162. }
  163. memcpy(data_ptr, orig_node->bat_iv.bcast_own_sum,
  164. del_if_num * sizeof(uint8_t));
  165. if_offset = (del_if_num + 1) * sizeof(uint8_t);
  166. memcpy((char *)data_ptr + del_if_num * sizeof(uint8_t),
  167. orig_node->bat_iv.bcast_own_sum + if_offset,
  168. (max_if_num - del_if_num) * sizeof(uint8_t));
  169. free_own_sum:
  170. kfree(orig_node->bat_iv.bcast_own_sum);
  171. orig_node->bat_iv.bcast_own_sum = data_ptr;
  172. ret = 0;
  173. unlock:
  174. spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  175. return ret;
  176. }
  177. /**
  178. * batadv_iv_ogm_orig_get - retrieve or create (if does not exist) an originator
  179. * @bat_priv: the bat priv with all the soft interface information
  180. * @addr: mac address of the originator
  181. *
  182. * Returns the originator object corresponding to the passed mac address or NULL
  183. * on failure.
  184. * If the object does not exists it is created an initialised.
  185. */
  186. static struct batadv_orig_node *
  187. batadv_iv_ogm_orig_get(struct batadv_priv *bat_priv, const uint8_t *addr)
  188. {
  189. struct batadv_orig_node *orig_node;
  190. int size, hash_added;
  191. orig_node = batadv_orig_hash_find(bat_priv, addr);
  192. if (orig_node)
  193. return orig_node;
  194. orig_node = batadv_orig_node_new(bat_priv, addr);
  195. if (!orig_node)
  196. return NULL;
  197. spin_lock_init(&orig_node->bat_iv.ogm_cnt_lock);
  198. size = bat_priv->num_ifaces * sizeof(unsigned long) * BATADV_NUM_WORDS;
  199. orig_node->bat_iv.bcast_own = kzalloc(size, GFP_ATOMIC);
  200. if (!orig_node->bat_iv.bcast_own)
  201. goto free_orig_node;
  202. size = bat_priv->num_ifaces * sizeof(uint8_t);
  203. orig_node->bat_iv.bcast_own_sum = kzalloc(size, GFP_ATOMIC);
  204. if (!orig_node->bat_iv.bcast_own_sum)
  205. goto free_orig_node;
  206. hash_added = batadv_hash_add(bat_priv->orig_hash, batadv_compare_orig,
  207. batadv_choose_orig, orig_node,
  208. &orig_node->hash_entry);
  209. if (hash_added != 0)
  210. goto free_orig_node;
  211. return orig_node;
  212. free_orig_node:
  213. /* free twice, as batadv_orig_node_new sets refcount to 2 */
  214. batadv_orig_node_free_ref(orig_node);
  215. batadv_orig_node_free_ref(orig_node);
  216. return NULL;
  217. }
  218. static struct batadv_neigh_node *
  219. batadv_iv_ogm_neigh_new(struct batadv_hard_iface *hard_iface,
  220. const uint8_t *neigh_addr,
  221. struct batadv_orig_node *orig_node,
  222. struct batadv_orig_node *orig_neigh)
  223. {
  224. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  225. struct batadv_neigh_node *neigh_node, *tmp_neigh_node;
  226. neigh_node = batadv_neigh_node_new(hard_iface, neigh_addr, orig_node);
  227. if (!neigh_node)
  228. goto out;
  229. if (!atomic_inc_not_zero(&hard_iface->refcount)) {
  230. kfree(neigh_node);
  231. neigh_node = NULL;
  232. goto out;
  233. }
  234. neigh_node->orig_node = orig_neigh;
  235. neigh_node->if_incoming = hard_iface;
  236. spin_lock_bh(&orig_node->neigh_list_lock);
  237. tmp_neigh_node = batadv_neigh_node_get(orig_node, hard_iface,
  238. neigh_addr);
  239. if (!tmp_neigh_node) {
  240. hlist_add_head_rcu(&neigh_node->list, &orig_node->neigh_list);
  241. } else {
  242. kfree(neigh_node);
  243. batadv_hardif_free_ref(hard_iface);
  244. neigh_node = tmp_neigh_node;
  245. }
  246. spin_unlock_bh(&orig_node->neigh_list_lock);
  247. if (!tmp_neigh_node)
  248. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  249. "Creating new neighbor %pM for orig_node %pM on interface %s\n",
  250. neigh_addr, orig_node->orig,
  251. hard_iface->net_dev->name);
  252. out:
  253. return neigh_node;
  254. }
  255. static int batadv_iv_ogm_iface_enable(struct batadv_hard_iface *hard_iface)
  256. {
  257. struct batadv_ogm_packet *batadv_ogm_packet;
  258. unsigned char *ogm_buff;
  259. uint32_t random_seqno;
  260. int res = -ENOMEM;
  261. /* randomize initial seqno to avoid collision */
  262. get_random_bytes(&random_seqno, sizeof(random_seqno));
  263. atomic_set(&hard_iface->bat_iv.ogm_seqno, random_seqno);
  264. hard_iface->bat_iv.ogm_buff_len = BATADV_OGM_HLEN;
  265. ogm_buff = kmalloc(hard_iface->bat_iv.ogm_buff_len, GFP_ATOMIC);
  266. if (!ogm_buff)
  267. goto out;
  268. hard_iface->bat_iv.ogm_buff = ogm_buff;
  269. batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
  270. batadv_ogm_packet->packet_type = BATADV_IV_OGM;
  271. batadv_ogm_packet->version = BATADV_COMPAT_VERSION;
  272. batadv_ogm_packet->ttl = 2;
  273. batadv_ogm_packet->flags = BATADV_NO_FLAGS;
  274. batadv_ogm_packet->reserved = 0;
  275. batadv_ogm_packet->tq = BATADV_TQ_MAX_VALUE;
  276. res = 0;
  277. out:
  278. return res;
  279. }
  280. static void batadv_iv_ogm_iface_disable(struct batadv_hard_iface *hard_iface)
  281. {
  282. kfree(hard_iface->bat_iv.ogm_buff);
  283. hard_iface->bat_iv.ogm_buff = NULL;
  284. }
  285. static void batadv_iv_ogm_iface_update_mac(struct batadv_hard_iface *hard_iface)
  286. {
  287. struct batadv_ogm_packet *batadv_ogm_packet;
  288. unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
  289. batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
  290. ether_addr_copy(batadv_ogm_packet->orig,
  291. hard_iface->net_dev->dev_addr);
  292. ether_addr_copy(batadv_ogm_packet->prev_sender,
  293. hard_iface->net_dev->dev_addr);
  294. }
  295. static void
  296. batadv_iv_ogm_primary_iface_set(struct batadv_hard_iface *hard_iface)
  297. {
  298. struct batadv_ogm_packet *batadv_ogm_packet;
  299. unsigned char *ogm_buff = hard_iface->bat_iv.ogm_buff;
  300. batadv_ogm_packet = (struct batadv_ogm_packet *)ogm_buff;
  301. batadv_ogm_packet->flags = BATADV_PRIMARIES_FIRST_HOP;
  302. batadv_ogm_packet->ttl = BATADV_TTL;
  303. }
  304. /* when do we schedule our own ogm to be sent */
  305. static unsigned long
  306. batadv_iv_ogm_emit_send_time(const struct batadv_priv *bat_priv)
  307. {
  308. unsigned int msecs;
  309. msecs = atomic_read(&bat_priv->orig_interval) - BATADV_JITTER;
  310. msecs += prandom_u32() % (2 * BATADV_JITTER);
  311. return jiffies + msecs_to_jiffies(msecs);
  312. }
  313. /* when do we schedule a ogm packet to be sent */
  314. static unsigned long batadv_iv_ogm_fwd_send_time(void)
  315. {
  316. return jiffies + msecs_to_jiffies(prandom_u32() % (BATADV_JITTER / 2));
  317. }
  318. /* apply hop penalty for a normal link */
  319. static uint8_t batadv_hop_penalty(uint8_t tq,
  320. const struct batadv_priv *bat_priv)
  321. {
  322. int hop_penalty = atomic_read(&bat_priv->hop_penalty);
  323. int new_tq;
  324. new_tq = tq * (BATADV_TQ_MAX_VALUE - hop_penalty);
  325. new_tq /= BATADV_TQ_MAX_VALUE;
  326. return new_tq;
  327. }
  328. /* is there another aggregated packet here? */
  329. static int batadv_iv_ogm_aggr_packet(int buff_pos, int packet_len,
  330. __be16 tvlv_len)
  331. {
  332. int next_buff_pos = 0;
  333. next_buff_pos += buff_pos + BATADV_OGM_HLEN;
  334. next_buff_pos += ntohs(tvlv_len);
  335. return (next_buff_pos <= packet_len) &&
  336. (next_buff_pos <= BATADV_MAX_AGGREGATION_BYTES);
  337. }
  338. /* send a batman ogm to a given interface */
  339. static void batadv_iv_ogm_send_to_if(struct batadv_forw_packet *forw_packet,
  340. struct batadv_hard_iface *hard_iface)
  341. {
  342. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  343. char *fwd_str;
  344. uint8_t packet_num;
  345. int16_t buff_pos;
  346. struct batadv_ogm_packet *batadv_ogm_packet;
  347. struct sk_buff *skb;
  348. uint8_t *packet_pos;
  349. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  350. return;
  351. packet_num = 0;
  352. buff_pos = 0;
  353. packet_pos = forw_packet->skb->data;
  354. batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
  355. /* adjust all flags and log packets */
  356. while (batadv_iv_ogm_aggr_packet(buff_pos, forw_packet->packet_len,
  357. batadv_ogm_packet->tvlv_len)) {
  358. /* we might have aggregated direct link packets with an
  359. * ordinary base packet
  360. */
  361. if (forw_packet->direct_link_flags & BIT(packet_num) &&
  362. forw_packet->if_incoming == hard_iface)
  363. batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
  364. else
  365. batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
  366. if (packet_num > 0 || !forw_packet->own)
  367. fwd_str = "Forwarding";
  368. else
  369. fwd_str = "Sending own";
  370. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  371. "%s %spacket (originator %pM, seqno %u, TQ %d, TTL %d, IDF %s) on interface %s [%pM]\n",
  372. fwd_str, (packet_num > 0 ? "aggregated " : ""),
  373. batadv_ogm_packet->orig,
  374. ntohl(batadv_ogm_packet->seqno),
  375. batadv_ogm_packet->tq, batadv_ogm_packet->ttl,
  376. (batadv_ogm_packet->flags & BATADV_DIRECTLINK ?
  377. "on" : "off"),
  378. hard_iface->net_dev->name,
  379. hard_iface->net_dev->dev_addr);
  380. buff_pos += BATADV_OGM_HLEN;
  381. buff_pos += ntohs(batadv_ogm_packet->tvlv_len);
  382. packet_num++;
  383. packet_pos = forw_packet->skb->data + buff_pos;
  384. batadv_ogm_packet = (struct batadv_ogm_packet *)packet_pos;
  385. }
  386. /* create clone because function is called more than once */
  387. skb = skb_clone(forw_packet->skb, GFP_ATOMIC);
  388. if (skb) {
  389. batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_TX);
  390. batadv_add_counter(bat_priv, BATADV_CNT_MGMT_TX_BYTES,
  391. skb->len + ETH_HLEN);
  392. batadv_send_skb_packet(skb, hard_iface, batadv_broadcast_addr);
  393. }
  394. }
  395. /* send a batman ogm packet */
  396. static void batadv_iv_ogm_emit(struct batadv_forw_packet *forw_packet)
  397. {
  398. struct net_device *soft_iface;
  399. struct batadv_priv *bat_priv;
  400. struct batadv_hard_iface *primary_if = NULL;
  401. if (!forw_packet->if_incoming) {
  402. pr_err("Error - can't forward packet: incoming iface not specified\n");
  403. goto out;
  404. }
  405. soft_iface = forw_packet->if_incoming->soft_iface;
  406. bat_priv = netdev_priv(soft_iface);
  407. if (WARN_ON(!forw_packet->if_outgoing))
  408. goto out;
  409. if (WARN_ON(forw_packet->if_outgoing->soft_iface != soft_iface))
  410. goto out;
  411. if (forw_packet->if_incoming->if_status != BATADV_IF_ACTIVE)
  412. goto out;
  413. primary_if = batadv_primary_if_get_selected(bat_priv);
  414. if (!primary_if)
  415. goto out;
  416. /* only for one specific outgoing interface */
  417. batadv_iv_ogm_send_to_if(forw_packet, forw_packet->if_outgoing);
  418. out:
  419. if (primary_if)
  420. batadv_hardif_free_ref(primary_if);
  421. }
  422. /**
  423. * batadv_iv_ogm_can_aggregate - find out if an OGM can be aggregated on an
  424. * existing forward packet
  425. * @new_bat_ogm_packet: OGM packet to be aggregated
  426. * @bat_priv: the bat priv with all the soft interface information
  427. * @packet_len: (total) length of the OGM
  428. * @send_time: timestamp (jiffies) when the packet is to be sent
  429. * @directlink: true if this is a direct link packet
  430. * @if_incoming: interface where the packet was received
  431. * @if_outgoing: interface for which the retransmission should be considered
  432. * @forw_packet: the forwarded packet which should be checked
  433. *
  434. * Returns true if new_packet can be aggregated with forw_packet
  435. */
  436. static bool
  437. batadv_iv_ogm_can_aggregate(const struct batadv_ogm_packet *new_bat_ogm_packet,
  438. struct batadv_priv *bat_priv,
  439. int packet_len, unsigned long send_time,
  440. bool directlink,
  441. const struct batadv_hard_iface *if_incoming,
  442. const struct batadv_hard_iface *if_outgoing,
  443. const struct batadv_forw_packet *forw_packet)
  444. {
  445. struct batadv_ogm_packet *batadv_ogm_packet;
  446. int aggregated_bytes = forw_packet->packet_len + packet_len;
  447. struct batadv_hard_iface *primary_if = NULL;
  448. bool res = false;
  449. unsigned long aggregation_end_time;
  450. batadv_ogm_packet = (struct batadv_ogm_packet *)forw_packet->skb->data;
  451. aggregation_end_time = send_time;
  452. aggregation_end_time += msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
  453. /* we can aggregate the current packet to this aggregated packet
  454. * if:
  455. *
  456. * - the send time is within our MAX_AGGREGATION_MS time
  457. * - the resulting packet wont be bigger than
  458. * MAX_AGGREGATION_BYTES
  459. */
  460. if (time_before(send_time, forw_packet->send_time) &&
  461. time_after_eq(aggregation_end_time, forw_packet->send_time) &&
  462. (aggregated_bytes <= BATADV_MAX_AGGREGATION_BYTES)) {
  463. /* check aggregation compatibility
  464. * -> direct link packets are broadcasted on
  465. * their interface only
  466. * -> aggregate packet if the current packet is
  467. * a "global" packet as well as the base
  468. * packet
  469. */
  470. primary_if = batadv_primary_if_get_selected(bat_priv);
  471. if (!primary_if)
  472. goto out;
  473. /* packet is not leaving on the same interface. */
  474. if (forw_packet->if_outgoing != if_outgoing)
  475. goto out;
  476. /* packets without direct link flag and high TTL
  477. * are flooded through the net
  478. */
  479. if ((!directlink) &&
  480. (!(batadv_ogm_packet->flags & BATADV_DIRECTLINK)) &&
  481. (batadv_ogm_packet->ttl != 1) &&
  482. /* own packets originating non-primary
  483. * interfaces leave only that interface
  484. */
  485. ((!forw_packet->own) ||
  486. (forw_packet->if_incoming == primary_if))) {
  487. res = true;
  488. goto out;
  489. }
  490. /* if the incoming packet is sent via this one
  491. * interface only - we still can aggregate
  492. */
  493. if ((directlink) &&
  494. (new_bat_ogm_packet->ttl == 1) &&
  495. (forw_packet->if_incoming == if_incoming) &&
  496. /* packets from direct neighbors or
  497. * own secondary interface packets
  498. * (= secondary interface packets in general)
  499. */
  500. (batadv_ogm_packet->flags & BATADV_DIRECTLINK ||
  501. (forw_packet->own &&
  502. forw_packet->if_incoming != primary_if))) {
  503. res = true;
  504. goto out;
  505. }
  506. }
  507. out:
  508. if (primary_if)
  509. batadv_hardif_free_ref(primary_if);
  510. return res;
  511. }
  512. /**
  513. * batadv_iv_ogm_aggregate_new - create a new aggregated packet and add this
  514. * packet to it.
  515. * @packet_buff: pointer to the OGM
  516. * @packet_len: (total) length of the OGM
  517. * @send_time: timestamp (jiffies) when the packet is to be sent
  518. * @direct_link: whether this OGM has direct link status
  519. * @if_incoming: interface where the packet was received
  520. * @if_outgoing: interface for which the retransmission should be considered
  521. * @own_packet: true if it is a self-generated ogm
  522. */
  523. static void batadv_iv_ogm_aggregate_new(const unsigned char *packet_buff,
  524. int packet_len, unsigned long send_time,
  525. bool direct_link,
  526. struct batadv_hard_iface *if_incoming,
  527. struct batadv_hard_iface *if_outgoing,
  528. int own_packet)
  529. {
  530. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  531. struct batadv_forw_packet *forw_packet_aggr;
  532. unsigned char *skb_buff;
  533. unsigned int skb_size;
  534. if (!atomic_inc_not_zero(&if_incoming->refcount))
  535. return;
  536. if (!atomic_inc_not_zero(&if_outgoing->refcount))
  537. goto out_free_incoming;
  538. /* own packet should always be scheduled */
  539. if (!own_packet) {
  540. if (!batadv_atomic_dec_not_zero(&bat_priv->batman_queue_left)) {
  541. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  542. "batman packet queue full\n");
  543. goto out;
  544. }
  545. }
  546. forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
  547. if (!forw_packet_aggr) {
  548. if (!own_packet)
  549. atomic_inc(&bat_priv->batman_queue_left);
  550. goto out;
  551. }
  552. if ((atomic_read(&bat_priv->aggregated_ogms)) &&
  553. (packet_len < BATADV_MAX_AGGREGATION_BYTES))
  554. skb_size = BATADV_MAX_AGGREGATION_BYTES;
  555. else
  556. skb_size = packet_len;
  557. skb_size += ETH_HLEN;
  558. forw_packet_aggr->skb = netdev_alloc_skb_ip_align(NULL, skb_size);
  559. if (!forw_packet_aggr->skb) {
  560. if (!own_packet)
  561. atomic_inc(&bat_priv->batman_queue_left);
  562. kfree(forw_packet_aggr);
  563. goto out;
  564. }
  565. forw_packet_aggr->skb->priority = TC_PRIO_CONTROL;
  566. skb_reserve(forw_packet_aggr->skb, ETH_HLEN);
  567. skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
  568. forw_packet_aggr->packet_len = packet_len;
  569. memcpy(skb_buff, packet_buff, packet_len);
  570. forw_packet_aggr->own = own_packet;
  571. forw_packet_aggr->if_incoming = if_incoming;
  572. forw_packet_aggr->if_outgoing = if_outgoing;
  573. forw_packet_aggr->num_packets = 0;
  574. forw_packet_aggr->direct_link_flags = BATADV_NO_FLAGS;
  575. forw_packet_aggr->send_time = send_time;
  576. /* save packet direct link flag status */
  577. if (direct_link)
  578. forw_packet_aggr->direct_link_flags |= 1;
  579. /* add new packet to packet list */
  580. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  581. hlist_add_head(&forw_packet_aggr->list, &bat_priv->forw_bat_list);
  582. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  583. /* start timer for this packet */
  584. INIT_DELAYED_WORK(&forw_packet_aggr->delayed_work,
  585. batadv_send_outstanding_bat_ogm_packet);
  586. queue_delayed_work(batadv_event_workqueue,
  587. &forw_packet_aggr->delayed_work,
  588. send_time - jiffies);
  589. return;
  590. out:
  591. batadv_hardif_free_ref(if_outgoing);
  592. out_free_incoming:
  593. batadv_hardif_free_ref(if_incoming);
  594. }
  595. /* aggregate a new packet into the existing ogm packet */
  596. static void batadv_iv_ogm_aggregate(struct batadv_forw_packet *forw_packet_aggr,
  597. const unsigned char *packet_buff,
  598. int packet_len, bool direct_link)
  599. {
  600. unsigned char *skb_buff;
  601. unsigned long new_direct_link_flag;
  602. skb_buff = skb_put(forw_packet_aggr->skb, packet_len);
  603. memcpy(skb_buff, packet_buff, packet_len);
  604. forw_packet_aggr->packet_len += packet_len;
  605. forw_packet_aggr->num_packets++;
  606. /* save packet direct link flag status */
  607. if (direct_link) {
  608. new_direct_link_flag = BIT(forw_packet_aggr->num_packets);
  609. forw_packet_aggr->direct_link_flags |= new_direct_link_flag;
  610. }
  611. }
  612. /**
  613. * batadv_iv_ogm_queue_add - queue up an OGM for transmission
  614. * @bat_priv: the bat priv with all the soft interface information
  615. * @packet_buff: pointer to the OGM
  616. * @packet_len: (total) length of the OGM
  617. * @if_incoming: interface where the packet was received
  618. * @if_outgoing: interface for which the retransmission should be considered
  619. * @own_packet: true if it is a self-generated ogm
  620. * @send_time: timestamp (jiffies) when the packet is to be sent
  621. */
  622. static void batadv_iv_ogm_queue_add(struct batadv_priv *bat_priv,
  623. unsigned char *packet_buff,
  624. int packet_len,
  625. struct batadv_hard_iface *if_incoming,
  626. struct batadv_hard_iface *if_outgoing,
  627. int own_packet, unsigned long send_time)
  628. {
  629. /* _aggr -> pointer to the packet we want to aggregate with
  630. * _pos -> pointer to the position in the queue
  631. */
  632. struct batadv_forw_packet *forw_packet_aggr = NULL;
  633. struct batadv_forw_packet *forw_packet_pos = NULL;
  634. struct batadv_ogm_packet *batadv_ogm_packet;
  635. bool direct_link;
  636. unsigned long max_aggregation_jiffies;
  637. batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
  638. direct_link = batadv_ogm_packet->flags & BATADV_DIRECTLINK ? 1 : 0;
  639. max_aggregation_jiffies = msecs_to_jiffies(BATADV_MAX_AGGREGATION_MS);
  640. /* find position for the packet in the forward queue */
  641. spin_lock_bh(&bat_priv->forw_bat_list_lock);
  642. /* own packets are not to be aggregated */
  643. if ((atomic_read(&bat_priv->aggregated_ogms)) && (!own_packet)) {
  644. hlist_for_each_entry(forw_packet_pos,
  645. &bat_priv->forw_bat_list, list) {
  646. if (batadv_iv_ogm_can_aggregate(batadv_ogm_packet,
  647. bat_priv, packet_len,
  648. send_time, direct_link,
  649. if_incoming,
  650. if_outgoing,
  651. forw_packet_pos)) {
  652. forw_packet_aggr = forw_packet_pos;
  653. break;
  654. }
  655. }
  656. }
  657. /* nothing to aggregate with - either aggregation disabled or no
  658. * suitable aggregation packet found
  659. */
  660. if (!forw_packet_aggr) {
  661. /* the following section can run without the lock */
  662. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  663. /* if we could not aggregate this packet with one of the others
  664. * we hold it back for a while, so that it might be aggregated
  665. * later on
  666. */
  667. if (!own_packet && atomic_read(&bat_priv->aggregated_ogms))
  668. send_time += max_aggregation_jiffies;
  669. batadv_iv_ogm_aggregate_new(packet_buff, packet_len,
  670. send_time, direct_link,
  671. if_incoming, if_outgoing,
  672. own_packet);
  673. } else {
  674. batadv_iv_ogm_aggregate(forw_packet_aggr, packet_buff,
  675. packet_len, direct_link);
  676. spin_unlock_bh(&bat_priv->forw_bat_list_lock);
  677. }
  678. }
  679. static void batadv_iv_ogm_forward(struct batadv_orig_node *orig_node,
  680. const struct ethhdr *ethhdr,
  681. struct batadv_ogm_packet *batadv_ogm_packet,
  682. bool is_single_hop_neigh,
  683. bool is_from_best_next_hop,
  684. struct batadv_hard_iface *if_incoming,
  685. struct batadv_hard_iface *if_outgoing)
  686. {
  687. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  688. uint16_t tvlv_len;
  689. if (batadv_ogm_packet->ttl <= 1) {
  690. batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "ttl exceeded\n");
  691. return;
  692. }
  693. if (!is_from_best_next_hop) {
  694. /* Mark the forwarded packet when it is not coming from our
  695. * best next hop. We still need to forward the packet for our
  696. * neighbor link quality detection to work in case the packet
  697. * originated from a single hop neighbor. Otherwise we can
  698. * simply drop the ogm.
  699. */
  700. if (is_single_hop_neigh)
  701. batadv_ogm_packet->flags |= BATADV_NOT_BEST_NEXT_HOP;
  702. else
  703. return;
  704. }
  705. tvlv_len = ntohs(batadv_ogm_packet->tvlv_len);
  706. batadv_ogm_packet->ttl--;
  707. ether_addr_copy(batadv_ogm_packet->prev_sender, ethhdr->h_source);
  708. /* apply hop penalty */
  709. batadv_ogm_packet->tq = batadv_hop_penalty(batadv_ogm_packet->tq,
  710. bat_priv);
  711. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  712. "Forwarding packet: tq: %i, ttl: %i\n",
  713. batadv_ogm_packet->tq, batadv_ogm_packet->ttl);
  714. /* switch of primaries first hop flag when forwarding */
  715. batadv_ogm_packet->flags &= ~BATADV_PRIMARIES_FIRST_HOP;
  716. if (is_single_hop_neigh)
  717. batadv_ogm_packet->flags |= BATADV_DIRECTLINK;
  718. else
  719. batadv_ogm_packet->flags &= ~BATADV_DIRECTLINK;
  720. batadv_iv_ogm_queue_add(bat_priv, (unsigned char *)batadv_ogm_packet,
  721. BATADV_OGM_HLEN + tvlv_len,
  722. if_incoming, if_outgoing, 0,
  723. batadv_iv_ogm_fwd_send_time());
  724. }
  725. /**
  726. * batadv_iv_ogm_slide_own_bcast_window - bitshift own OGM broadcast windows for
  727. * the given interface
  728. * @hard_iface: the interface for which the windows have to be shifted
  729. */
  730. static void
  731. batadv_iv_ogm_slide_own_bcast_window(struct batadv_hard_iface *hard_iface)
  732. {
  733. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  734. struct batadv_hashtable *hash = bat_priv->orig_hash;
  735. struct hlist_head *head;
  736. struct batadv_orig_node *orig_node;
  737. unsigned long *word;
  738. uint32_t i;
  739. size_t word_index;
  740. uint8_t *w;
  741. int if_num;
  742. for (i = 0; i < hash->size; i++) {
  743. head = &hash->table[i];
  744. rcu_read_lock();
  745. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  746. spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  747. word_index = hard_iface->if_num * BATADV_NUM_WORDS;
  748. word = &orig_node->bat_iv.bcast_own[word_index];
  749. batadv_bit_get_packet(bat_priv, word, 1, 0);
  750. if_num = hard_iface->if_num;
  751. w = &orig_node->bat_iv.bcast_own_sum[if_num];
  752. *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
  753. spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  754. }
  755. rcu_read_unlock();
  756. }
  757. }
  758. static void batadv_iv_ogm_schedule(struct batadv_hard_iface *hard_iface)
  759. {
  760. struct batadv_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  761. unsigned char **ogm_buff = &hard_iface->bat_iv.ogm_buff;
  762. struct batadv_ogm_packet *batadv_ogm_packet;
  763. struct batadv_hard_iface *primary_if, *tmp_hard_iface;
  764. int *ogm_buff_len = &hard_iface->bat_iv.ogm_buff_len;
  765. uint32_t seqno;
  766. uint16_t tvlv_len = 0;
  767. unsigned long send_time;
  768. primary_if = batadv_primary_if_get_selected(bat_priv);
  769. if (hard_iface == primary_if) {
  770. /* tt changes have to be committed before the tvlv data is
  771. * appended as it may alter the tt tvlv container
  772. */
  773. batadv_tt_local_commit_changes(bat_priv);
  774. tvlv_len = batadv_tvlv_container_ogm_append(bat_priv, ogm_buff,
  775. ogm_buff_len,
  776. BATADV_OGM_HLEN);
  777. }
  778. batadv_ogm_packet = (struct batadv_ogm_packet *)(*ogm_buff);
  779. batadv_ogm_packet->tvlv_len = htons(tvlv_len);
  780. /* change sequence number to network order */
  781. seqno = (uint32_t)atomic_read(&hard_iface->bat_iv.ogm_seqno);
  782. batadv_ogm_packet->seqno = htonl(seqno);
  783. atomic_inc(&hard_iface->bat_iv.ogm_seqno);
  784. batadv_iv_ogm_slide_own_bcast_window(hard_iface);
  785. send_time = batadv_iv_ogm_emit_send_time(bat_priv);
  786. if (hard_iface != primary_if) {
  787. /* OGMs from secondary interfaces are only scheduled on their
  788. * respective interfaces.
  789. */
  790. batadv_iv_ogm_queue_add(bat_priv, *ogm_buff, *ogm_buff_len,
  791. hard_iface, hard_iface, 1, send_time);
  792. goto out;
  793. }
  794. /* OGMs from primary interfaces are scheduled on all
  795. * interfaces.
  796. */
  797. rcu_read_lock();
  798. list_for_each_entry_rcu(tmp_hard_iface, &batadv_hardif_list, list) {
  799. if (tmp_hard_iface->soft_iface != hard_iface->soft_iface)
  800. continue;
  801. batadv_iv_ogm_queue_add(bat_priv, *ogm_buff,
  802. *ogm_buff_len, hard_iface,
  803. tmp_hard_iface, 1, send_time);
  804. }
  805. rcu_read_unlock();
  806. out:
  807. if (primary_if)
  808. batadv_hardif_free_ref(primary_if);
  809. }
  810. /**
  811. * batadv_iv_ogm_orig_update - use OGM to update corresponding data in an
  812. * originator
  813. * @bat_priv: the bat priv with all the soft interface information
  814. * @orig_node: the orig node who originally emitted the ogm packet
  815. * @orig_ifinfo: ifinfo for the outgoing interface of the orig_node
  816. * @ethhdr: Ethernet header of the OGM
  817. * @batadv_ogm_packet: the ogm packet
  818. * @if_incoming: interface where the packet was received
  819. * @if_outgoing: interface for which the retransmission should be considered
  820. * @dup_status: the duplicate status of this ogm packet.
  821. */
  822. static void
  823. batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
  824. struct batadv_orig_node *orig_node,
  825. struct batadv_orig_ifinfo *orig_ifinfo,
  826. const struct ethhdr *ethhdr,
  827. const struct batadv_ogm_packet *batadv_ogm_packet,
  828. struct batadv_hard_iface *if_incoming,
  829. struct batadv_hard_iface *if_outgoing,
  830. enum batadv_dup_status dup_status)
  831. {
  832. struct batadv_neigh_ifinfo *neigh_ifinfo = NULL;
  833. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  834. struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
  835. struct batadv_neigh_node *router = NULL;
  836. struct batadv_orig_node *orig_node_tmp;
  837. int if_num;
  838. uint8_t sum_orig, sum_neigh;
  839. uint8_t *neigh_addr;
  840. uint8_t tq_avg;
  841. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  842. "update_originator(): Searching and updating originator entry of received packet\n");
  843. rcu_read_lock();
  844. hlist_for_each_entry_rcu(tmp_neigh_node,
  845. &orig_node->neigh_list, list) {
  846. neigh_addr = tmp_neigh_node->addr;
  847. if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
  848. tmp_neigh_node->if_incoming == if_incoming &&
  849. atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
  850. if (WARN(neigh_node, "too many matching neigh_nodes"))
  851. batadv_neigh_node_free_ref(neigh_node);
  852. neigh_node = tmp_neigh_node;
  853. continue;
  854. }
  855. if (dup_status != BATADV_NO_DUP)
  856. continue;
  857. /* only update the entry for this outgoing interface */
  858. neigh_ifinfo = batadv_neigh_ifinfo_get(tmp_neigh_node,
  859. if_outgoing);
  860. if (!neigh_ifinfo)
  861. continue;
  862. spin_lock_bh(&tmp_neigh_node->ifinfo_lock);
  863. batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
  864. &neigh_ifinfo->bat_iv.tq_index, 0);
  865. tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
  866. neigh_ifinfo->bat_iv.tq_avg = tq_avg;
  867. spin_unlock_bh(&tmp_neigh_node->ifinfo_lock);
  868. batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
  869. neigh_ifinfo = NULL;
  870. }
  871. if (!neigh_node) {
  872. struct batadv_orig_node *orig_tmp;
  873. orig_tmp = batadv_iv_ogm_orig_get(bat_priv, ethhdr->h_source);
  874. if (!orig_tmp)
  875. goto unlock;
  876. neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
  877. ethhdr->h_source,
  878. orig_node, orig_tmp);
  879. batadv_orig_node_free_ref(orig_tmp);
  880. if (!neigh_node)
  881. goto unlock;
  882. } else
  883. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  884. "Updating existing last-hop neighbor of originator\n");
  885. rcu_read_unlock();
  886. neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
  887. if (!neigh_ifinfo)
  888. goto out;
  889. neigh_node->last_seen = jiffies;
  890. spin_lock_bh(&neigh_node->ifinfo_lock);
  891. batadv_ring_buffer_set(neigh_ifinfo->bat_iv.tq_recv,
  892. &neigh_ifinfo->bat_iv.tq_index,
  893. batadv_ogm_packet->tq);
  894. tq_avg = batadv_ring_buffer_avg(neigh_ifinfo->bat_iv.tq_recv);
  895. neigh_ifinfo->bat_iv.tq_avg = tq_avg;
  896. spin_unlock_bh(&neigh_node->ifinfo_lock);
  897. if (dup_status == BATADV_NO_DUP) {
  898. orig_ifinfo->last_ttl = batadv_ogm_packet->ttl;
  899. neigh_ifinfo->last_ttl = batadv_ogm_packet->ttl;
  900. }
  901. /* if this neighbor already is our next hop there is nothing
  902. * to change
  903. */
  904. router = batadv_orig_router_get(orig_node, if_outgoing);
  905. if (router == neigh_node)
  906. goto out;
  907. if (router) {
  908. router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
  909. if (!router_ifinfo)
  910. goto out;
  911. /* if this neighbor does not offer a better TQ we won't
  912. * consider it
  913. */
  914. if (router_ifinfo->bat_iv.tq_avg > neigh_ifinfo->bat_iv.tq_avg)
  915. goto out;
  916. }
  917. /* if the TQ is the same and the link not more symmetric we
  918. * won't consider it either
  919. */
  920. if (router_ifinfo &&
  921. (neigh_ifinfo->bat_iv.tq_avg == router_ifinfo->bat_iv.tq_avg)) {
  922. orig_node_tmp = router->orig_node;
  923. spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
  924. if_num = router->if_incoming->if_num;
  925. sum_orig = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
  926. spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
  927. orig_node_tmp = neigh_node->orig_node;
  928. spin_lock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
  929. if_num = neigh_node->if_incoming->if_num;
  930. sum_neigh = orig_node_tmp->bat_iv.bcast_own_sum[if_num];
  931. spin_unlock_bh(&orig_node_tmp->bat_iv.ogm_cnt_lock);
  932. if (sum_orig >= sum_neigh)
  933. goto out;
  934. }
  935. batadv_update_route(bat_priv, orig_node, if_outgoing, neigh_node);
  936. goto out;
  937. unlock:
  938. rcu_read_unlock();
  939. out:
  940. if (neigh_node)
  941. batadv_neigh_node_free_ref(neigh_node);
  942. if (router)
  943. batadv_neigh_node_free_ref(router);
  944. if (neigh_ifinfo)
  945. batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
  946. if (router_ifinfo)
  947. batadv_neigh_ifinfo_free_ref(router_ifinfo);
  948. }
  949. /**
  950. * batadv_iv_ogm_calc_tq - calculate tq for current received ogm packet
  951. * @orig_node: the orig node who originally emitted the ogm packet
  952. * @orig_neigh_node: the orig node struct of the neighbor who sent the packet
  953. * @batadv_ogm_packet: the ogm packet
  954. * @if_incoming: interface where the packet was received
  955. * @if_outgoing: interface for which the retransmission should be considered
  956. *
  957. * Returns 1 if the link can be considered bidirectional, 0 otherwise
  958. */
  959. static int batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
  960. struct batadv_orig_node *orig_neigh_node,
  961. struct batadv_ogm_packet *batadv_ogm_packet,
  962. struct batadv_hard_iface *if_incoming,
  963. struct batadv_hard_iface *if_outgoing)
  964. {
  965. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  966. struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
  967. struct batadv_neigh_ifinfo *neigh_ifinfo;
  968. uint8_t total_count;
  969. uint8_t orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
  970. unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
  971. int tq_asym_penalty, inv_asym_penalty, if_num, ret = 0;
  972. unsigned int combined_tq;
  973. int tq_iface_penalty;
  974. /* find corresponding one hop neighbor */
  975. rcu_read_lock();
  976. hlist_for_each_entry_rcu(tmp_neigh_node,
  977. &orig_neigh_node->neigh_list, list) {
  978. if (!batadv_compare_eth(tmp_neigh_node->addr,
  979. orig_neigh_node->orig))
  980. continue;
  981. if (tmp_neigh_node->if_incoming != if_incoming)
  982. continue;
  983. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  984. continue;
  985. neigh_node = tmp_neigh_node;
  986. break;
  987. }
  988. rcu_read_unlock();
  989. if (!neigh_node)
  990. neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
  991. orig_neigh_node->orig,
  992. orig_neigh_node,
  993. orig_neigh_node);
  994. if (!neigh_node)
  995. goto out;
  996. /* if orig_node is direct neighbor update neigh_node last_seen */
  997. if (orig_node == orig_neigh_node)
  998. neigh_node->last_seen = jiffies;
  999. orig_node->last_seen = jiffies;
  1000. /* find packet count of corresponding one hop neighbor */
  1001. spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  1002. if_num = if_incoming->if_num;
  1003. orig_eq_count = orig_neigh_node->bat_iv.bcast_own_sum[if_num];
  1004. neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node, if_outgoing);
  1005. if (neigh_ifinfo) {
  1006. neigh_rq_count = neigh_ifinfo->bat_iv.real_packet_count;
  1007. batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
  1008. } else {
  1009. neigh_rq_count = 0;
  1010. }
  1011. spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  1012. /* pay attention to not get a value bigger than 100 % */
  1013. if (orig_eq_count > neigh_rq_count)
  1014. total_count = neigh_rq_count;
  1015. else
  1016. total_count = orig_eq_count;
  1017. /* if we have too few packets (too less data) we set tq_own to zero
  1018. * if we receive too few packets it is not considered bidirectional
  1019. */
  1020. if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
  1021. neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
  1022. tq_own = 0;
  1023. else
  1024. /* neigh_node->real_packet_count is never zero as we
  1025. * only purge old information when getting new
  1026. * information
  1027. */
  1028. tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
  1029. /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
  1030. * affect the nearly-symmetric links only a little, but
  1031. * punishes asymmetric links more. This will give a value
  1032. * between 0 and TQ_MAX_VALUE
  1033. */
  1034. neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
  1035. neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
  1036. neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
  1037. BATADV_TQ_LOCAL_WINDOW_SIZE *
  1038. BATADV_TQ_LOCAL_WINDOW_SIZE;
  1039. inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
  1040. inv_asym_penalty /= neigh_rq_max_cube;
  1041. tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
  1042. /* penalize if the OGM is forwarded on the same interface. WiFi
  1043. * interfaces and other half duplex devices suffer from throughput
  1044. * drops as they can't send and receive at the same time.
  1045. */
  1046. tq_iface_penalty = BATADV_TQ_MAX_VALUE;
  1047. if (if_outgoing && (if_incoming == if_outgoing) &&
  1048. batadv_is_wifi_netdev(if_outgoing->net_dev))
  1049. tq_iface_penalty = batadv_hop_penalty(BATADV_TQ_MAX_VALUE,
  1050. bat_priv);
  1051. combined_tq = batadv_ogm_packet->tq *
  1052. tq_own *
  1053. tq_asym_penalty *
  1054. tq_iface_penalty;
  1055. combined_tq /= BATADV_TQ_MAX_VALUE *
  1056. BATADV_TQ_MAX_VALUE *
  1057. BATADV_TQ_MAX_VALUE;
  1058. batadv_ogm_packet->tq = combined_tq;
  1059. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1060. "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, iface_penalty: %3i, total tq: %3i, if_incoming = %s, if_outgoing = %s\n",
  1061. orig_node->orig, orig_neigh_node->orig, total_count,
  1062. neigh_rq_count, tq_own, tq_asym_penalty, tq_iface_penalty,
  1063. batadv_ogm_packet->tq, if_incoming->net_dev->name,
  1064. if_outgoing ? if_outgoing->net_dev->name : "DEFAULT");
  1065. /* if link has the minimum required transmission quality
  1066. * consider it bidirectional
  1067. */
  1068. if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
  1069. ret = 1;
  1070. out:
  1071. if (neigh_node)
  1072. batadv_neigh_node_free_ref(neigh_node);
  1073. return ret;
  1074. }
  1075. /**
  1076. * batadv_iv_ogm_update_seqnos - process a batman packet for all interfaces,
  1077. * adjust the sequence number and find out whether it is a duplicate
  1078. * @ethhdr: ethernet header of the packet
  1079. * @batadv_ogm_packet: OGM packet to be considered
  1080. * @if_incoming: interface on which the OGM packet was received
  1081. * @if_outgoing: interface for which the retransmission should be considered
  1082. *
  1083. * Returns duplicate status as enum batadv_dup_status
  1084. */
  1085. static enum batadv_dup_status
  1086. batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
  1087. const struct batadv_ogm_packet *batadv_ogm_packet,
  1088. const struct batadv_hard_iface *if_incoming,
  1089. struct batadv_hard_iface *if_outgoing)
  1090. {
  1091. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  1092. struct batadv_orig_node *orig_node;
  1093. struct batadv_orig_ifinfo *orig_ifinfo = NULL;
  1094. struct batadv_neigh_node *neigh_node;
  1095. struct batadv_neigh_ifinfo *neigh_ifinfo;
  1096. int is_dup;
  1097. int32_t seq_diff;
  1098. int need_update = 0;
  1099. int set_mark;
  1100. enum batadv_dup_status ret = BATADV_NO_DUP;
  1101. uint32_t seqno = ntohl(batadv_ogm_packet->seqno);
  1102. uint8_t *neigh_addr;
  1103. uint8_t packet_count;
  1104. unsigned long *bitmap;
  1105. orig_node = batadv_iv_ogm_orig_get(bat_priv, batadv_ogm_packet->orig);
  1106. if (!orig_node)
  1107. return BATADV_NO_DUP;
  1108. orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
  1109. if (WARN_ON(!orig_ifinfo)) {
  1110. batadv_orig_node_free_ref(orig_node);
  1111. return 0;
  1112. }
  1113. spin_lock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  1114. seq_diff = seqno - orig_ifinfo->last_real_seqno;
  1115. /* signalize caller that the packet is to be dropped. */
  1116. if (!hlist_empty(&orig_node->neigh_list) &&
  1117. batadv_window_protected(bat_priv, seq_diff,
  1118. &orig_ifinfo->batman_seqno_reset)) {
  1119. ret = BATADV_PROTECTED;
  1120. goto out;
  1121. }
  1122. rcu_read_lock();
  1123. hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
  1124. neigh_ifinfo = batadv_neigh_ifinfo_new(neigh_node,
  1125. if_outgoing);
  1126. if (!neigh_ifinfo)
  1127. continue;
  1128. neigh_addr = neigh_node->addr;
  1129. is_dup = batadv_test_bit(neigh_ifinfo->bat_iv.real_bits,
  1130. orig_ifinfo->last_real_seqno,
  1131. seqno);
  1132. if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
  1133. neigh_node->if_incoming == if_incoming) {
  1134. set_mark = 1;
  1135. if (is_dup)
  1136. ret = BATADV_NEIGH_DUP;
  1137. } else {
  1138. set_mark = 0;
  1139. if (is_dup && (ret != BATADV_NEIGH_DUP))
  1140. ret = BATADV_ORIG_DUP;
  1141. }
  1142. /* if the window moved, set the update flag. */
  1143. bitmap = neigh_ifinfo->bat_iv.real_bits;
  1144. need_update |= batadv_bit_get_packet(bat_priv, bitmap,
  1145. seq_diff, set_mark);
  1146. packet_count = bitmap_weight(bitmap,
  1147. BATADV_TQ_LOCAL_WINDOW_SIZE);
  1148. neigh_ifinfo->bat_iv.real_packet_count = packet_count;
  1149. batadv_neigh_ifinfo_free_ref(neigh_ifinfo);
  1150. }
  1151. rcu_read_unlock();
  1152. if (need_update) {
  1153. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1154. "%s updating last_seqno: old %u, new %u\n",
  1155. if_outgoing ? if_outgoing->net_dev->name : "DEFAULT",
  1156. orig_ifinfo->last_real_seqno, seqno);
  1157. orig_ifinfo->last_real_seqno = seqno;
  1158. }
  1159. out:
  1160. spin_unlock_bh(&orig_node->bat_iv.ogm_cnt_lock);
  1161. batadv_orig_node_free_ref(orig_node);
  1162. if (orig_ifinfo)
  1163. batadv_orig_ifinfo_free_ref(orig_ifinfo);
  1164. return ret;
  1165. }
  1166. /**
  1167. * batadv_iv_ogm_process_per_outif - process a batman iv OGM for an outgoing if
  1168. * @skb: the skb containing the OGM
  1169. * @ogm_offset: offset from skb->data to start of ogm header
  1170. * @orig_node: the (cached) orig node for the originator of this OGM
  1171. * @if_incoming: the interface where this packet was received
  1172. * @if_outgoing: the interface for which the packet should be considered
  1173. */
  1174. static void
  1175. batadv_iv_ogm_process_per_outif(const struct sk_buff *skb, int ogm_offset,
  1176. struct batadv_orig_node *orig_node,
  1177. struct batadv_hard_iface *if_incoming,
  1178. struct batadv_hard_iface *if_outgoing)
  1179. {
  1180. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  1181. struct batadv_neigh_node *router = NULL, *router_router = NULL;
  1182. struct batadv_orig_node *orig_neigh_node;
  1183. struct batadv_orig_ifinfo *orig_ifinfo;
  1184. struct batadv_neigh_node *orig_neigh_router = NULL;
  1185. struct batadv_neigh_ifinfo *router_ifinfo = NULL;
  1186. struct batadv_ogm_packet *ogm_packet;
  1187. enum batadv_dup_status dup_status;
  1188. bool is_from_best_next_hop = false;
  1189. bool is_single_hop_neigh = false;
  1190. bool sameseq, similar_ttl;
  1191. struct sk_buff *skb_priv;
  1192. struct ethhdr *ethhdr;
  1193. uint8_t *prev_sender;
  1194. int is_bidirect;
  1195. /* create a private copy of the skb, as some functions change tq value
  1196. * and/or flags.
  1197. */
  1198. skb_priv = skb_copy(skb, GFP_ATOMIC);
  1199. if (!skb_priv)
  1200. return;
  1201. ethhdr = eth_hdr(skb_priv);
  1202. ogm_packet = (struct batadv_ogm_packet *)(skb_priv->data + ogm_offset);
  1203. dup_status = batadv_iv_ogm_update_seqnos(ethhdr, ogm_packet,
  1204. if_incoming, if_outgoing);
  1205. if (batadv_compare_eth(ethhdr->h_source, ogm_packet->orig))
  1206. is_single_hop_neigh = true;
  1207. if (dup_status == BATADV_PROTECTED) {
  1208. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1209. "Drop packet: packet within seqno protection time (sender: %pM)\n",
  1210. ethhdr->h_source);
  1211. goto out;
  1212. }
  1213. if (ogm_packet->tq == 0) {
  1214. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1215. "Drop packet: originator packet with tq equal 0\n");
  1216. goto out;
  1217. }
  1218. router = batadv_orig_router_get(orig_node, if_outgoing);
  1219. if (router) {
  1220. router_router = batadv_orig_router_get(router->orig_node,
  1221. if_outgoing);
  1222. router_ifinfo = batadv_neigh_ifinfo_get(router, if_outgoing);
  1223. }
  1224. if ((router_ifinfo && router_ifinfo->bat_iv.tq_avg != 0) &&
  1225. (batadv_compare_eth(router->addr, ethhdr->h_source)))
  1226. is_from_best_next_hop = true;
  1227. prev_sender = ogm_packet->prev_sender;
  1228. /* avoid temporary routing loops */
  1229. if (router && router_router &&
  1230. (batadv_compare_eth(router->addr, prev_sender)) &&
  1231. !(batadv_compare_eth(ogm_packet->orig, prev_sender)) &&
  1232. (batadv_compare_eth(router->addr, router_router->addr))) {
  1233. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1234. "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
  1235. ethhdr->h_source);
  1236. goto out;
  1237. }
  1238. if (if_outgoing == BATADV_IF_DEFAULT)
  1239. batadv_tvlv_ogm_receive(bat_priv, ogm_packet, orig_node);
  1240. /* if sender is a direct neighbor the sender mac equals
  1241. * originator mac
  1242. */
  1243. if (is_single_hop_neigh)
  1244. orig_neigh_node = orig_node;
  1245. else
  1246. orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
  1247. ethhdr->h_source);
  1248. if (!orig_neigh_node)
  1249. goto out;
  1250. /* Update nc_nodes of the originator */
  1251. batadv_nc_update_nc_node(bat_priv, orig_node, orig_neigh_node,
  1252. ogm_packet, is_single_hop_neigh);
  1253. orig_neigh_router = batadv_orig_router_get(orig_neigh_node,
  1254. if_outgoing);
  1255. /* drop packet if sender is not a direct neighbor and if we
  1256. * don't route towards it
  1257. */
  1258. if (!is_single_hop_neigh && (!orig_neigh_router)) {
  1259. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1260. "Drop packet: OGM via unknown neighbor!\n");
  1261. goto out_neigh;
  1262. }
  1263. is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
  1264. ogm_packet, if_incoming,
  1265. if_outgoing);
  1266. /* update ranking if it is not a duplicate or has the same
  1267. * seqno and similar ttl as the non-duplicate
  1268. */
  1269. orig_ifinfo = batadv_orig_ifinfo_new(orig_node, if_outgoing);
  1270. if (!orig_ifinfo)
  1271. goto out_neigh;
  1272. sameseq = orig_ifinfo->last_real_seqno == ntohl(ogm_packet->seqno);
  1273. similar_ttl = (orig_ifinfo->last_ttl - 3) <= ogm_packet->ttl;
  1274. if (is_bidirect && ((dup_status == BATADV_NO_DUP) ||
  1275. (sameseq && similar_ttl))) {
  1276. batadv_iv_ogm_orig_update(bat_priv, orig_node,
  1277. orig_ifinfo, ethhdr,
  1278. ogm_packet, if_incoming,
  1279. if_outgoing, dup_status);
  1280. }
  1281. batadv_orig_ifinfo_free_ref(orig_ifinfo);
  1282. /* only forward for specific interface, not for the default one. */
  1283. if (if_outgoing == BATADV_IF_DEFAULT)
  1284. goto out_neigh;
  1285. /* is single hop (direct) neighbor */
  1286. if (is_single_hop_neigh) {
  1287. /* OGMs from secondary interfaces should only scheduled once
  1288. * per interface where it has been received, not multiple times
  1289. */
  1290. if ((ogm_packet->ttl <= 2) &&
  1291. (if_incoming != if_outgoing)) {
  1292. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1293. "Drop packet: OGM from secondary interface and wrong outgoing interface\n");
  1294. goto out_neigh;
  1295. }
  1296. /* mark direct link on incoming interface */
  1297. batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
  1298. is_single_hop_neigh,
  1299. is_from_best_next_hop, if_incoming,
  1300. if_outgoing);
  1301. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1302. "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
  1303. goto out_neigh;
  1304. }
  1305. /* multihop originator */
  1306. if (!is_bidirect) {
  1307. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1308. "Drop packet: not received via bidirectional link\n");
  1309. goto out_neigh;
  1310. }
  1311. if (dup_status == BATADV_NEIGH_DUP) {
  1312. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1313. "Drop packet: duplicate packet received\n");
  1314. goto out_neigh;
  1315. }
  1316. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1317. "Forwarding packet: rebroadcast originator packet\n");
  1318. batadv_iv_ogm_forward(orig_node, ethhdr, ogm_packet,
  1319. is_single_hop_neigh, is_from_best_next_hop,
  1320. if_incoming, if_outgoing);
  1321. out_neigh:
  1322. if ((orig_neigh_node) && (!is_single_hop_neigh))
  1323. batadv_orig_node_free_ref(orig_neigh_node);
  1324. out:
  1325. if (router_ifinfo)
  1326. batadv_neigh_ifinfo_free_ref(router_ifinfo);
  1327. if (router)
  1328. batadv_neigh_node_free_ref(router);
  1329. if (router_router)
  1330. batadv_neigh_node_free_ref(router_router);
  1331. if (orig_neigh_router)
  1332. batadv_neigh_node_free_ref(orig_neigh_router);
  1333. kfree_skb(skb_priv);
  1334. }
  1335. /**
  1336. * batadv_iv_ogm_process - process an incoming batman iv OGM
  1337. * @skb: the skb containing the OGM
  1338. * @ogm_offset: offset to the OGM which should be processed (for aggregates)
  1339. * @if_incoming: the interface where this packet was receved
  1340. */
  1341. static void batadv_iv_ogm_process(const struct sk_buff *skb, int ogm_offset,
  1342. struct batadv_hard_iface *if_incoming)
  1343. {
  1344. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  1345. struct batadv_orig_node *orig_neigh_node, *orig_node;
  1346. struct batadv_hard_iface *hard_iface;
  1347. struct batadv_ogm_packet *ogm_packet;
  1348. uint32_t if_incoming_seqno;
  1349. bool has_directlink_flag;
  1350. struct ethhdr *ethhdr;
  1351. bool is_my_oldorig = false;
  1352. bool is_my_addr = false;
  1353. bool is_my_orig = false;
  1354. ogm_packet = (struct batadv_ogm_packet *)(skb->data + ogm_offset);
  1355. ethhdr = eth_hdr(skb);
  1356. /* Silently drop when the batman packet is actually not a
  1357. * correct packet.
  1358. *
  1359. * This might happen if a packet is padded (e.g. Ethernet has a
  1360. * minimum frame length of 64 byte) and the aggregation interprets
  1361. * it as an additional length.
  1362. *
  1363. * TODO: A more sane solution would be to have a bit in the
  1364. * batadv_ogm_packet to detect whether the packet is the last
  1365. * packet in an aggregation. Here we expect that the padding
  1366. * is always zero (or not 0x01)
  1367. */
  1368. if (ogm_packet->packet_type != BATADV_IV_OGM)
  1369. return;
  1370. /* could be changed by schedule_own_packet() */
  1371. if_incoming_seqno = atomic_read(&if_incoming->bat_iv.ogm_seqno);
  1372. if (ogm_packet->flags & BATADV_DIRECTLINK)
  1373. has_directlink_flag = true;
  1374. else
  1375. has_directlink_flag = false;
  1376. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1377. "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",
  1378. ethhdr->h_source, if_incoming->net_dev->name,
  1379. if_incoming->net_dev->dev_addr, ogm_packet->orig,
  1380. ogm_packet->prev_sender, ntohl(ogm_packet->seqno),
  1381. ogm_packet->tq, ogm_packet->ttl,
  1382. ogm_packet->version, has_directlink_flag);
  1383. rcu_read_lock();
  1384. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  1385. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  1386. continue;
  1387. if (hard_iface->soft_iface != if_incoming->soft_iface)
  1388. continue;
  1389. if (batadv_compare_eth(ethhdr->h_source,
  1390. hard_iface->net_dev->dev_addr))
  1391. is_my_addr = true;
  1392. if (batadv_compare_eth(ogm_packet->orig,
  1393. hard_iface->net_dev->dev_addr))
  1394. is_my_orig = true;
  1395. if (batadv_compare_eth(ogm_packet->prev_sender,
  1396. hard_iface->net_dev->dev_addr))
  1397. is_my_oldorig = true;
  1398. }
  1399. rcu_read_unlock();
  1400. if (is_my_addr) {
  1401. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1402. "Drop packet: received my own broadcast (sender: %pM)\n",
  1403. ethhdr->h_source);
  1404. return;
  1405. }
  1406. if (is_my_orig) {
  1407. unsigned long *word;
  1408. int offset;
  1409. int32_t bit_pos;
  1410. int16_t if_num;
  1411. uint8_t *weight;
  1412. orig_neigh_node = batadv_iv_ogm_orig_get(bat_priv,
  1413. ethhdr->h_source);
  1414. if (!orig_neigh_node)
  1415. return;
  1416. /* neighbor has to indicate direct link and it has to
  1417. * come via the corresponding interface
  1418. * save packet seqno for bidirectional check
  1419. */
  1420. if (has_directlink_flag &&
  1421. batadv_compare_eth(if_incoming->net_dev->dev_addr,
  1422. ogm_packet->orig)) {
  1423. if_num = if_incoming->if_num;
  1424. offset = if_num * BATADV_NUM_WORDS;
  1425. spin_lock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
  1426. word = &orig_neigh_node->bat_iv.bcast_own[offset];
  1427. bit_pos = if_incoming_seqno - 2;
  1428. bit_pos -= ntohl(ogm_packet->seqno);
  1429. batadv_set_bit(word, bit_pos);
  1430. weight = &orig_neigh_node->bat_iv.bcast_own_sum[if_num];
  1431. *weight = bitmap_weight(word,
  1432. BATADV_TQ_LOCAL_WINDOW_SIZE);
  1433. spin_unlock_bh(&orig_neigh_node->bat_iv.ogm_cnt_lock);
  1434. }
  1435. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1436. "Drop packet: originator packet from myself (via neighbor)\n");
  1437. batadv_orig_node_free_ref(orig_neigh_node);
  1438. return;
  1439. }
  1440. if (is_my_oldorig) {
  1441. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1442. "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
  1443. ethhdr->h_source);
  1444. return;
  1445. }
  1446. if (ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
  1447. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1448. "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
  1449. ethhdr->h_source);
  1450. return;
  1451. }
  1452. orig_node = batadv_iv_ogm_orig_get(bat_priv, ogm_packet->orig);
  1453. if (!orig_node)
  1454. return;
  1455. batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
  1456. if_incoming, BATADV_IF_DEFAULT);
  1457. rcu_read_lock();
  1458. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  1459. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  1460. continue;
  1461. if (hard_iface->soft_iface != bat_priv->soft_iface)
  1462. continue;
  1463. batadv_iv_ogm_process_per_outif(skb, ogm_offset, orig_node,
  1464. if_incoming, hard_iface);
  1465. }
  1466. rcu_read_unlock();
  1467. batadv_orig_node_free_ref(orig_node);
  1468. }
  1469. static int batadv_iv_ogm_receive(struct sk_buff *skb,
  1470. struct batadv_hard_iface *if_incoming)
  1471. {
  1472. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  1473. struct batadv_ogm_packet *ogm_packet;
  1474. uint8_t *packet_pos;
  1475. int ogm_offset;
  1476. bool ret;
  1477. ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
  1478. if (!ret)
  1479. return NET_RX_DROP;
  1480. /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
  1481. * that does not have B.A.T.M.A.N. IV enabled ?
  1482. */
  1483. if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit)
  1484. return NET_RX_DROP;
  1485. batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
  1486. batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
  1487. skb->len + ETH_HLEN);
  1488. ogm_offset = 0;
  1489. ogm_packet = (struct batadv_ogm_packet *)skb->data;
  1490. /* unpack the aggregated packets and process them one by one */
  1491. while (batadv_iv_ogm_aggr_packet(ogm_offset, skb_headlen(skb),
  1492. ogm_packet->tvlv_len)) {
  1493. batadv_iv_ogm_process(skb, ogm_offset, if_incoming);
  1494. ogm_offset += BATADV_OGM_HLEN;
  1495. ogm_offset += ntohs(ogm_packet->tvlv_len);
  1496. packet_pos = skb->data + ogm_offset;
  1497. ogm_packet = (struct batadv_ogm_packet *)packet_pos;
  1498. }
  1499. kfree_skb(skb);
  1500. return NET_RX_SUCCESS;
  1501. }
  1502. /**
  1503. * batadv_iv_ogm_orig_print_neigh - print neighbors for the originator table
  1504. * @orig_node: the orig_node for which the neighbors are printed
  1505. * @if_outgoing: outgoing interface for these entries
  1506. * @seq: debugfs table seq_file struct
  1507. *
  1508. * Must be called while holding an rcu lock.
  1509. */
  1510. static void
  1511. batadv_iv_ogm_orig_print_neigh(struct batadv_orig_node *orig_node,
  1512. struct batadv_hard_iface *if_outgoing,
  1513. struct seq_file *seq)
  1514. {
  1515. struct batadv_neigh_node *neigh_node;
  1516. struct batadv_neigh_ifinfo *n_ifinfo;
  1517. hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
  1518. n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
  1519. if (!n_ifinfo)
  1520. continue;
  1521. seq_printf(seq, " %pM (%3i)",
  1522. neigh_node->addr,
  1523. n_ifinfo->bat_iv.tq_avg);
  1524. batadv_neigh_ifinfo_free_ref(n_ifinfo);
  1525. }
  1526. }
  1527. /**
  1528. * batadv_iv_ogm_orig_print - print the originator table
  1529. * @bat_priv: the bat priv with all the soft interface information
  1530. * @seq: debugfs table seq_file struct
  1531. * @if_outgoing: the outgoing interface for which this should be printed
  1532. */
  1533. static void batadv_iv_ogm_orig_print(struct batadv_priv *bat_priv,
  1534. struct seq_file *seq,
  1535. struct batadv_hard_iface *if_outgoing)
  1536. {
  1537. struct batadv_neigh_node *neigh_node;
  1538. struct batadv_hashtable *hash = bat_priv->orig_hash;
  1539. int last_seen_msecs, last_seen_secs;
  1540. struct batadv_orig_node *orig_node;
  1541. struct batadv_neigh_ifinfo *n_ifinfo;
  1542. unsigned long last_seen_jiffies;
  1543. struct hlist_head *head;
  1544. int batman_count = 0;
  1545. uint32_t i;
  1546. seq_printf(seq, " %-15s %s (%s/%i) %17s [%10s]: %20s ...\n",
  1547. "Originator", "last-seen", "#", BATADV_TQ_MAX_VALUE,
  1548. "Nexthop", "outgoingIF", "Potential nexthops");
  1549. for (i = 0; i < hash->size; i++) {
  1550. head = &hash->table[i];
  1551. rcu_read_lock();
  1552. hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
  1553. neigh_node = batadv_orig_router_get(orig_node,
  1554. if_outgoing);
  1555. if (!neigh_node)
  1556. continue;
  1557. n_ifinfo = batadv_neigh_ifinfo_get(neigh_node,
  1558. if_outgoing);
  1559. if (!n_ifinfo)
  1560. goto next;
  1561. if (n_ifinfo->bat_iv.tq_avg == 0)
  1562. goto next;
  1563. last_seen_jiffies = jiffies - orig_node->last_seen;
  1564. last_seen_msecs = jiffies_to_msecs(last_seen_jiffies);
  1565. last_seen_secs = last_seen_msecs / 1000;
  1566. last_seen_msecs = last_seen_msecs % 1000;
  1567. seq_printf(seq, "%pM %4i.%03is (%3i) %pM [%10s]:",
  1568. orig_node->orig, last_seen_secs,
  1569. last_seen_msecs, n_ifinfo->bat_iv.tq_avg,
  1570. neigh_node->addr,
  1571. neigh_node->if_incoming->net_dev->name);
  1572. batadv_iv_ogm_orig_print_neigh(orig_node, if_outgoing,
  1573. seq);
  1574. seq_puts(seq, "\n");
  1575. batman_count++;
  1576. next:
  1577. batadv_neigh_node_free_ref(neigh_node);
  1578. if (n_ifinfo)
  1579. batadv_neigh_ifinfo_free_ref(n_ifinfo);
  1580. }
  1581. rcu_read_unlock();
  1582. }
  1583. if (batman_count == 0)
  1584. seq_puts(seq, "No batman nodes in range ...\n");
  1585. }
  1586. /**
  1587. * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
  1588. * @neigh1: the first neighbor object of the comparison
  1589. * @if_outgoing1: outgoing interface for the first neighbor
  1590. * @neigh2: the second neighbor object of the comparison
  1591. * @if_outgoing2: outgoing interface for the second neighbor
  1592. *
  1593. * Returns a value less, equal to or greater than 0 if the metric via neigh1 is
  1594. * lower, the same as or higher than the metric via neigh2
  1595. */
  1596. static int batadv_iv_ogm_neigh_cmp(struct batadv_neigh_node *neigh1,
  1597. struct batadv_hard_iface *if_outgoing1,
  1598. struct batadv_neigh_node *neigh2,
  1599. struct batadv_hard_iface *if_outgoing2)
  1600. {
  1601. struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
  1602. uint8_t tq1, tq2;
  1603. int diff;
  1604. neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
  1605. neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
  1606. if (!neigh1_ifinfo || !neigh2_ifinfo) {
  1607. diff = 0;
  1608. goto out;
  1609. }
  1610. tq1 = neigh1_ifinfo->bat_iv.tq_avg;
  1611. tq2 = neigh2_ifinfo->bat_iv.tq_avg;
  1612. diff = tq1 - tq2;
  1613. out:
  1614. if (neigh1_ifinfo)
  1615. batadv_neigh_ifinfo_free_ref(neigh1_ifinfo);
  1616. if (neigh2_ifinfo)
  1617. batadv_neigh_ifinfo_free_ref(neigh2_ifinfo);
  1618. return diff;
  1619. }
  1620. /**
  1621. * batadv_iv_ogm_neigh_is_eob - check if neigh1 is equally good or better than
  1622. * neigh2 from the metric prospective
  1623. * @neigh1: the first neighbor object of the comparison
  1624. * @if_outgoing1: outgoing interface for the first neighbor
  1625. * @neigh2: the second neighbor object of the comparison
  1626. * @if_outgoing2: outgoing interface for the second neighbor
  1627. *
  1628. * Returns true if the metric via neigh1 is equally good or better than
  1629. * the metric via neigh2, false otherwise.
  1630. */
  1631. static bool
  1632. batadv_iv_ogm_neigh_is_eob(struct batadv_neigh_node *neigh1,
  1633. struct batadv_hard_iface *if_outgoing1,
  1634. struct batadv_neigh_node *neigh2,
  1635. struct batadv_hard_iface *if_outgoing2)
  1636. {
  1637. struct batadv_neigh_ifinfo *neigh1_ifinfo, *neigh2_ifinfo;
  1638. uint8_t tq1, tq2;
  1639. bool ret;
  1640. neigh1_ifinfo = batadv_neigh_ifinfo_get(neigh1, if_outgoing1);
  1641. neigh2_ifinfo = batadv_neigh_ifinfo_get(neigh2, if_outgoing2);
  1642. /* we can't say that the metric is better */
  1643. if (!neigh1_ifinfo || !neigh2_ifinfo) {
  1644. ret = false;
  1645. goto out;
  1646. }
  1647. tq1 = neigh1_ifinfo->bat_iv.tq_avg;
  1648. tq2 = neigh2_ifinfo->bat_iv.tq_avg;
  1649. ret = (tq1 - tq2) > -BATADV_TQ_SIMILARITY_THRESHOLD;
  1650. out:
  1651. if (neigh1_ifinfo)
  1652. batadv_neigh_ifinfo_free_ref(neigh1_ifinfo);
  1653. if (neigh2_ifinfo)
  1654. batadv_neigh_ifinfo_free_ref(neigh2_ifinfo);
  1655. return ret;
  1656. }
  1657. static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
  1658. .name = "BATMAN_IV",
  1659. .bat_iface_enable = batadv_iv_ogm_iface_enable,
  1660. .bat_iface_disable = batadv_iv_ogm_iface_disable,
  1661. .bat_iface_update_mac = batadv_iv_ogm_iface_update_mac,
  1662. .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set,
  1663. .bat_ogm_schedule = batadv_iv_ogm_schedule,
  1664. .bat_ogm_emit = batadv_iv_ogm_emit,
  1665. .bat_neigh_cmp = batadv_iv_ogm_neigh_cmp,
  1666. .bat_neigh_is_equiv_or_better = batadv_iv_ogm_neigh_is_eob,
  1667. .bat_orig_print = batadv_iv_ogm_orig_print,
  1668. .bat_orig_free = batadv_iv_ogm_orig_free,
  1669. .bat_orig_add_if = batadv_iv_ogm_orig_add_if,
  1670. .bat_orig_del_if = batadv_iv_ogm_orig_del_if,
  1671. };
  1672. int __init batadv_iv_init(void)
  1673. {
  1674. int ret;
  1675. /* batman originator packet */
  1676. ret = batadv_recv_handler_register(BATADV_IV_OGM,
  1677. batadv_iv_ogm_receive);
  1678. if (ret < 0)
  1679. goto out;
  1680. ret = batadv_algo_register(&batadv_batman_iv);
  1681. if (ret < 0)
  1682. goto handler_unregister;
  1683. goto out;
  1684. handler_unregister:
  1685. batadv_recv_handler_unregister(BATADV_IV_OGM);
  1686. out:
  1687. return ret;
  1688. }