bat_iv_ogm.c 50 KB

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