bat_iv_ogm.c 41 KB

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