bat_iv_ogm.c 41 KB

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