bat_iv_ogm.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325
  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. vis_server = atomic_read(&bat_priv->vis_mode);
  501. primary_if = batadv_primary_if_get_selected(bat_priv);
  502. if (hard_iface == primary_if)
  503. tt_num_changes = batadv_tt_append_diff(bat_priv,
  504. &hard_iface->packet_buff,
  505. &hard_iface->packet_len,
  506. BATADV_OGM_HLEN);
  507. batadv_ogm_packet = (struct batadv_ogm_packet *)hard_iface->packet_buff;
  508. /* change sequence number to network order */
  509. batadv_ogm_packet->seqno =
  510. htonl((uint32_t)atomic_read(&hard_iface->seqno));
  511. atomic_inc(&hard_iface->seqno);
  512. batadv_ogm_packet->ttvn = atomic_read(&bat_priv->tt.vn);
  513. batadv_ogm_packet->tt_crc = htons(bat_priv->tt.local_crc);
  514. if (tt_num_changes >= 0)
  515. batadv_ogm_packet->tt_num_changes = tt_num_changes;
  516. if (vis_server == BATADV_VIS_TYPE_SERVER_SYNC)
  517. batadv_ogm_packet->flags |= BATADV_VIS_SERVER;
  518. else
  519. batadv_ogm_packet->flags &= ~BATADV_VIS_SERVER;
  520. if ((hard_iface == primary_if) &&
  521. (atomic_read(&bat_priv->gw_mode) == BATADV_GW_MODE_SERVER))
  522. batadv_ogm_packet->gw_flags =
  523. (uint8_t)atomic_read(&bat_priv->gw_bandwidth);
  524. else
  525. batadv_ogm_packet->gw_flags = BATADV_NO_FLAGS;
  526. batadv_slide_own_bcast_window(hard_iface);
  527. batadv_iv_ogm_queue_add(bat_priv, hard_iface->packet_buff,
  528. hard_iface->packet_len, hard_iface, 1,
  529. batadv_iv_ogm_emit_send_time(bat_priv));
  530. if (primary_if)
  531. batadv_hardif_free_ref(primary_if);
  532. }
  533. static void
  534. batadv_iv_ogm_orig_update(struct batadv_priv *bat_priv,
  535. struct batadv_orig_node *orig_node,
  536. const struct ethhdr *ethhdr,
  537. const struct batadv_ogm_packet *batadv_ogm_packet,
  538. struct batadv_hard_iface *if_incoming,
  539. const unsigned char *tt_buff,
  540. int is_duplicate)
  541. {
  542. struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
  543. struct batadv_neigh_node *router = NULL;
  544. struct batadv_orig_node *orig_node_tmp;
  545. struct hlist_node *node;
  546. uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
  547. uint8_t *neigh_addr;
  548. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  549. "update_originator(): Searching and updating originator entry of received packet\n");
  550. rcu_read_lock();
  551. hlist_for_each_entry_rcu(tmp_neigh_node, node,
  552. &orig_node->neigh_list, list) {
  553. neigh_addr = tmp_neigh_node->addr;
  554. if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
  555. tmp_neigh_node->if_incoming == if_incoming &&
  556. atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
  557. if (neigh_node)
  558. batadv_neigh_node_free_ref(neigh_node);
  559. neigh_node = tmp_neigh_node;
  560. continue;
  561. }
  562. if (is_duplicate)
  563. continue;
  564. spin_lock_bh(&tmp_neigh_node->lq_update_lock);
  565. batadv_ring_buffer_set(tmp_neigh_node->tq_recv,
  566. &tmp_neigh_node->tq_index, 0);
  567. tmp_neigh_node->tq_avg =
  568. batadv_ring_buffer_avg(tmp_neigh_node->tq_recv);
  569. spin_unlock_bh(&tmp_neigh_node->lq_update_lock);
  570. }
  571. if (!neigh_node) {
  572. struct batadv_orig_node *orig_tmp;
  573. orig_tmp = batadv_get_orig_node(bat_priv, ethhdr->h_source);
  574. if (!orig_tmp)
  575. goto unlock;
  576. neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
  577. ethhdr->h_source,
  578. orig_node, orig_tmp,
  579. batadv_ogm_packet->seqno);
  580. batadv_orig_node_free_ref(orig_tmp);
  581. if (!neigh_node)
  582. goto unlock;
  583. } else
  584. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  585. "Updating existing last-hop neighbor of originator\n");
  586. rcu_read_unlock();
  587. orig_node->flags = batadv_ogm_packet->flags;
  588. neigh_node->last_seen = jiffies;
  589. spin_lock_bh(&neigh_node->lq_update_lock);
  590. batadv_ring_buffer_set(neigh_node->tq_recv,
  591. &neigh_node->tq_index,
  592. batadv_ogm_packet->tq);
  593. neigh_node->tq_avg = batadv_ring_buffer_avg(neigh_node->tq_recv);
  594. spin_unlock_bh(&neigh_node->lq_update_lock);
  595. if (!is_duplicate) {
  596. orig_node->last_ttl = batadv_ogm_packet->header.ttl;
  597. neigh_node->last_ttl = batadv_ogm_packet->header.ttl;
  598. }
  599. batadv_bonding_candidate_add(orig_node, neigh_node);
  600. /* if this neighbor already is our next hop there is nothing
  601. * to change
  602. */
  603. router = batadv_orig_node_get_router(orig_node);
  604. if (router == neigh_node)
  605. goto update_tt;
  606. /* if this neighbor does not offer a better TQ we won't consider it */
  607. if (router && (router->tq_avg > neigh_node->tq_avg))
  608. goto update_tt;
  609. /* if the TQ is the same and the link not more symmetric we
  610. * won't consider it either
  611. */
  612. if (router && (neigh_node->tq_avg == router->tq_avg)) {
  613. orig_node_tmp = router->orig_node;
  614. spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
  615. bcast_own_sum_orig =
  616. orig_node_tmp->bcast_own_sum[if_incoming->if_num];
  617. spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
  618. orig_node_tmp = neigh_node->orig_node;
  619. spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
  620. bcast_own_sum_neigh =
  621. orig_node_tmp->bcast_own_sum[if_incoming->if_num];
  622. spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
  623. if (bcast_own_sum_orig >= bcast_own_sum_neigh)
  624. goto update_tt;
  625. }
  626. batadv_update_route(bat_priv, orig_node, neigh_node);
  627. update_tt:
  628. /* I have to check for transtable changes only if the OGM has been
  629. * sent through a primary interface
  630. */
  631. if (((batadv_ogm_packet->orig != ethhdr->h_source) &&
  632. (batadv_ogm_packet->header.ttl > 2)) ||
  633. (batadv_ogm_packet->flags & BATADV_PRIMARIES_FIRST_HOP))
  634. batadv_tt_update_orig(bat_priv, orig_node, tt_buff,
  635. batadv_ogm_packet->tt_num_changes,
  636. batadv_ogm_packet->ttvn,
  637. ntohs(batadv_ogm_packet->tt_crc));
  638. if (orig_node->gw_flags != batadv_ogm_packet->gw_flags)
  639. batadv_gw_node_update(bat_priv, orig_node,
  640. batadv_ogm_packet->gw_flags);
  641. orig_node->gw_flags = batadv_ogm_packet->gw_flags;
  642. /* restart gateway selection if fast or late switching was enabled */
  643. if ((orig_node->gw_flags) &&
  644. (atomic_read(&bat_priv->gw_mode) == BATADV_GW_MODE_CLIENT) &&
  645. (atomic_read(&bat_priv->gw_sel_class) > 2))
  646. batadv_gw_check_election(bat_priv, orig_node);
  647. goto out;
  648. unlock:
  649. rcu_read_unlock();
  650. out:
  651. if (neigh_node)
  652. batadv_neigh_node_free_ref(neigh_node);
  653. if (router)
  654. batadv_neigh_node_free_ref(router);
  655. }
  656. static int batadv_iv_ogm_calc_tq(struct batadv_orig_node *orig_node,
  657. struct batadv_orig_node *orig_neigh_node,
  658. struct batadv_ogm_packet *batadv_ogm_packet,
  659. struct batadv_hard_iface *if_incoming)
  660. {
  661. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  662. struct batadv_neigh_node *neigh_node = NULL, *tmp_neigh_node;
  663. struct hlist_node *node;
  664. uint8_t total_count;
  665. uint8_t orig_eq_count, neigh_rq_count, neigh_rq_inv, tq_own;
  666. unsigned int neigh_rq_inv_cube, neigh_rq_max_cube;
  667. int tq_asym_penalty, inv_asym_penalty, ret = 0;
  668. unsigned int combined_tq;
  669. /* find corresponding one hop neighbor */
  670. rcu_read_lock();
  671. hlist_for_each_entry_rcu(tmp_neigh_node, node,
  672. &orig_neigh_node->neigh_list, list) {
  673. if (!batadv_compare_eth(tmp_neigh_node->addr,
  674. orig_neigh_node->orig))
  675. continue;
  676. if (tmp_neigh_node->if_incoming != if_incoming)
  677. continue;
  678. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  679. continue;
  680. neigh_node = tmp_neigh_node;
  681. break;
  682. }
  683. rcu_read_unlock();
  684. if (!neigh_node)
  685. neigh_node = batadv_iv_ogm_neigh_new(if_incoming,
  686. orig_neigh_node->orig,
  687. orig_neigh_node,
  688. orig_neigh_node,
  689. batadv_ogm_packet->seqno);
  690. if (!neigh_node)
  691. goto out;
  692. /* if orig_node is direct neighbor update neigh_node last_seen */
  693. if (orig_node == orig_neigh_node)
  694. neigh_node->last_seen = jiffies;
  695. orig_node->last_seen = jiffies;
  696. /* find packet count of corresponding one hop neighbor */
  697. spin_lock_bh(&orig_node->ogm_cnt_lock);
  698. orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
  699. neigh_rq_count = neigh_node->real_packet_count;
  700. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  701. /* pay attention to not get a value bigger than 100 % */
  702. total_count = (orig_eq_count > neigh_rq_count ?
  703. neigh_rq_count : orig_eq_count);
  704. /* if we have too few packets (too less data) we set tq_own to zero
  705. * if we receive too few packets it is not considered bidirectional
  706. */
  707. if (total_count < BATADV_TQ_LOCAL_BIDRECT_SEND_MINIMUM ||
  708. neigh_rq_count < BATADV_TQ_LOCAL_BIDRECT_RECV_MINIMUM)
  709. tq_own = 0;
  710. else
  711. /* neigh_node->real_packet_count is never zero as we
  712. * only purge old information when getting new
  713. * information
  714. */
  715. tq_own = (BATADV_TQ_MAX_VALUE * total_count) / neigh_rq_count;
  716. /* 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
  717. * affect the nearly-symmetric links only a little, but
  718. * punishes asymmetric links more. This will give a value
  719. * between 0 and TQ_MAX_VALUE
  720. */
  721. neigh_rq_inv = BATADV_TQ_LOCAL_WINDOW_SIZE - neigh_rq_count;
  722. neigh_rq_inv_cube = neigh_rq_inv * neigh_rq_inv * neigh_rq_inv;
  723. neigh_rq_max_cube = BATADV_TQ_LOCAL_WINDOW_SIZE *
  724. BATADV_TQ_LOCAL_WINDOW_SIZE *
  725. BATADV_TQ_LOCAL_WINDOW_SIZE;
  726. inv_asym_penalty = BATADV_TQ_MAX_VALUE * neigh_rq_inv_cube;
  727. inv_asym_penalty /= neigh_rq_max_cube;
  728. tq_asym_penalty = BATADV_TQ_MAX_VALUE - inv_asym_penalty;
  729. combined_tq = batadv_ogm_packet->tq * tq_own * tq_asym_penalty;
  730. combined_tq /= BATADV_TQ_MAX_VALUE * BATADV_TQ_MAX_VALUE;
  731. batadv_ogm_packet->tq = combined_tq;
  732. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  733. "bidirectional: orig = %-15pM neigh = %-15pM => own_bcast = %2i, real recv = %2i, local tq: %3i, asym_penalty: %3i, total tq: %3i\n",
  734. orig_node->orig, orig_neigh_node->orig, total_count,
  735. neigh_rq_count, tq_own,
  736. tq_asym_penalty, batadv_ogm_packet->tq);
  737. /* if link has the minimum required transmission quality
  738. * consider it bidirectional
  739. */
  740. if (batadv_ogm_packet->tq >= BATADV_TQ_TOTAL_BIDRECT_LIMIT)
  741. ret = 1;
  742. out:
  743. if (neigh_node)
  744. batadv_neigh_node_free_ref(neigh_node);
  745. return ret;
  746. }
  747. /* processes a batman packet for all interfaces, adjusts the sequence number and
  748. * finds out whether it is a duplicate.
  749. * returns:
  750. * 1 the packet is a duplicate
  751. * 0 the packet has not yet been received
  752. * -1 the packet is old and has been received while the seqno window
  753. * was protected. Caller should drop it.
  754. */
  755. static int
  756. batadv_iv_ogm_update_seqnos(const struct ethhdr *ethhdr,
  757. const struct batadv_ogm_packet *batadv_ogm_packet,
  758. const struct batadv_hard_iface *if_incoming)
  759. {
  760. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  761. struct batadv_orig_node *orig_node;
  762. struct batadv_neigh_node *tmp_neigh_node;
  763. struct hlist_node *node;
  764. int is_duplicate = 0;
  765. int32_t seq_diff;
  766. int need_update = 0;
  767. int set_mark, ret = -1;
  768. uint32_t seqno = ntohl(batadv_ogm_packet->seqno);
  769. uint8_t *neigh_addr;
  770. orig_node = batadv_get_orig_node(bat_priv, batadv_ogm_packet->orig);
  771. if (!orig_node)
  772. return 0;
  773. spin_lock_bh(&orig_node->ogm_cnt_lock);
  774. seq_diff = seqno - orig_node->last_real_seqno;
  775. /* signalize caller that the packet is to be dropped. */
  776. if (!hlist_empty(&orig_node->neigh_list) &&
  777. batadv_window_protected(bat_priv, seq_diff,
  778. &orig_node->batman_seqno_reset))
  779. goto out;
  780. rcu_read_lock();
  781. hlist_for_each_entry_rcu(tmp_neigh_node, node,
  782. &orig_node->neigh_list, list) {
  783. is_duplicate |= batadv_test_bit(tmp_neigh_node->real_bits,
  784. orig_node->last_real_seqno,
  785. seqno);
  786. neigh_addr = tmp_neigh_node->addr;
  787. if (batadv_compare_eth(neigh_addr, ethhdr->h_source) &&
  788. tmp_neigh_node->if_incoming == if_incoming)
  789. set_mark = 1;
  790. else
  791. set_mark = 0;
  792. /* if the window moved, set the update flag. */
  793. need_update |= batadv_bit_get_packet(bat_priv,
  794. tmp_neigh_node->real_bits,
  795. seq_diff, set_mark);
  796. tmp_neigh_node->real_packet_count =
  797. bitmap_weight(tmp_neigh_node->real_bits,
  798. BATADV_TQ_LOCAL_WINDOW_SIZE);
  799. }
  800. rcu_read_unlock();
  801. if (need_update) {
  802. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  803. "updating last_seqno: old %u, new %u\n",
  804. orig_node->last_real_seqno, seqno);
  805. orig_node->last_real_seqno = seqno;
  806. }
  807. ret = is_duplicate;
  808. out:
  809. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  810. batadv_orig_node_free_ref(orig_node);
  811. return ret;
  812. }
  813. static void batadv_iv_ogm_process(const struct ethhdr *ethhdr,
  814. struct batadv_ogm_packet *batadv_ogm_packet,
  815. const unsigned char *tt_buff,
  816. struct batadv_hard_iface *if_incoming)
  817. {
  818. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  819. struct batadv_hard_iface *hard_iface;
  820. struct batadv_orig_node *orig_neigh_node, *orig_node;
  821. struct batadv_neigh_node *router = NULL, *router_router = NULL;
  822. struct batadv_neigh_node *orig_neigh_router = NULL;
  823. int has_directlink_flag;
  824. int is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
  825. int is_broadcast = 0, is_bidirect;
  826. bool is_single_hop_neigh = false;
  827. bool is_from_best_next_hop = false;
  828. int is_duplicate, sameseq, simlar_ttl;
  829. uint32_t if_incoming_seqno;
  830. uint8_t *prev_sender;
  831. /* Silently drop when the batman packet is actually not a
  832. * correct packet.
  833. *
  834. * This might happen if a packet is padded (e.g. Ethernet has a
  835. * minimum frame length of 64 byte) and the aggregation interprets
  836. * it as an additional length.
  837. *
  838. * TODO: A more sane solution would be to have a bit in the
  839. * batadv_ogm_packet to detect whether the packet is the last
  840. * packet in an aggregation. Here we expect that the padding
  841. * is always zero (or not 0x01)
  842. */
  843. if (batadv_ogm_packet->header.packet_type != BATADV_IV_OGM)
  844. return;
  845. /* could be changed by schedule_own_packet() */
  846. if_incoming_seqno = atomic_read(&if_incoming->seqno);
  847. if (batadv_ogm_packet->flags & BATADV_DIRECTLINK)
  848. has_directlink_flag = 1;
  849. else
  850. has_directlink_flag = 0;
  851. if (batadv_compare_eth(ethhdr->h_source, batadv_ogm_packet->orig))
  852. is_single_hop_neigh = true;
  853. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  854. "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",
  855. ethhdr->h_source, if_incoming->net_dev->name,
  856. if_incoming->net_dev->dev_addr, batadv_ogm_packet->orig,
  857. batadv_ogm_packet->prev_sender,
  858. ntohl(batadv_ogm_packet->seqno), batadv_ogm_packet->ttvn,
  859. ntohs(batadv_ogm_packet->tt_crc),
  860. batadv_ogm_packet->tt_num_changes, batadv_ogm_packet->tq,
  861. batadv_ogm_packet->header.ttl,
  862. batadv_ogm_packet->header.version, has_directlink_flag);
  863. rcu_read_lock();
  864. list_for_each_entry_rcu(hard_iface, &batadv_hardif_list, list) {
  865. if (hard_iface->if_status != BATADV_IF_ACTIVE)
  866. continue;
  867. if (hard_iface->soft_iface != if_incoming->soft_iface)
  868. continue;
  869. if (batadv_compare_eth(ethhdr->h_source,
  870. hard_iface->net_dev->dev_addr))
  871. is_my_addr = 1;
  872. if (batadv_compare_eth(batadv_ogm_packet->orig,
  873. hard_iface->net_dev->dev_addr))
  874. is_my_orig = 1;
  875. if (batadv_compare_eth(batadv_ogm_packet->prev_sender,
  876. hard_iface->net_dev->dev_addr))
  877. is_my_oldorig = 1;
  878. if (is_broadcast_ether_addr(ethhdr->h_source))
  879. is_broadcast = 1;
  880. }
  881. rcu_read_unlock();
  882. if (batadv_ogm_packet->header.version != BATADV_COMPAT_VERSION) {
  883. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  884. "Drop packet: incompatible batman version (%i)\n",
  885. batadv_ogm_packet->header.version);
  886. return;
  887. }
  888. if (is_my_addr) {
  889. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  890. "Drop packet: received my own broadcast (sender: %pM)\n",
  891. ethhdr->h_source);
  892. return;
  893. }
  894. if (is_broadcast) {
  895. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  896. "Drop packet: ignoring all packets with broadcast source addr (sender: %pM)\n",
  897. ethhdr->h_source);
  898. return;
  899. }
  900. if (is_my_orig) {
  901. unsigned long *word;
  902. int offset;
  903. int32_t bit_pos;
  904. int16_t if_num;
  905. uint8_t *weight;
  906. orig_neigh_node = batadv_get_orig_node(bat_priv,
  907. ethhdr->h_source);
  908. if (!orig_neigh_node)
  909. return;
  910. /* neighbor has to indicate direct link and it has to
  911. * come via the corresponding interface
  912. * save packet seqno for bidirectional check
  913. */
  914. if (has_directlink_flag &&
  915. batadv_compare_eth(if_incoming->net_dev->dev_addr,
  916. batadv_ogm_packet->orig)) {
  917. if_num = if_incoming->if_num;
  918. offset = if_num * BATADV_NUM_WORDS;
  919. spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
  920. word = &(orig_neigh_node->bcast_own[offset]);
  921. bit_pos = if_incoming_seqno - 2;
  922. bit_pos -= ntohl(batadv_ogm_packet->seqno);
  923. batadv_set_bit(word, bit_pos);
  924. weight = &orig_neigh_node->bcast_own_sum[if_num];
  925. *weight = bitmap_weight(word,
  926. BATADV_TQ_LOCAL_WINDOW_SIZE);
  927. spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
  928. }
  929. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  930. "Drop packet: originator packet from myself (via neighbor)\n");
  931. batadv_orig_node_free_ref(orig_neigh_node);
  932. return;
  933. }
  934. if (is_my_oldorig) {
  935. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  936. "Drop packet: ignoring all rebroadcast echos (sender: %pM)\n",
  937. ethhdr->h_source);
  938. return;
  939. }
  940. if (batadv_ogm_packet->flags & BATADV_NOT_BEST_NEXT_HOP) {
  941. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  942. "Drop packet: ignoring all packets not forwarded from the best next hop (sender: %pM)\n",
  943. ethhdr->h_source);
  944. return;
  945. }
  946. orig_node = batadv_get_orig_node(bat_priv, batadv_ogm_packet->orig);
  947. if (!orig_node)
  948. return;
  949. is_duplicate = batadv_iv_ogm_update_seqnos(ethhdr, batadv_ogm_packet,
  950. if_incoming);
  951. if (is_duplicate == -1) {
  952. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  953. "Drop packet: packet within seqno protection time (sender: %pM)\n",
  954. ethhdr->h_source);
  955. goto out;
  956. }
  957. if (batadv_ogm_packet->tq == 0) {
  958. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  959. "Drop packet: originator packet with tq equal 0\n");
  960. goto out;
  961. }
  962. router = batadv_orig_node_get_router(orig_node);
  963. if (router)
  964. router_router = batadv_orig_node_get_router(router->orig_node);
  965. if ((router && router->tq_avg != 0) &&
  966. (batadv_compare_eth(router->addr, ethhdr->h_source)))
  967. is_from_best_next_hop = true;
  968. prev_sender = batadv_ogm_packet->prev_sender;
  969. /* avoid temporary routing loops */
  970. if (router && router_router &&
  971. (batadv_compare_eth(router->addr, prev_sender)) &&
  972. !(batadv_compare_eth(batadv_ogm_packet->orig, prev_sender)) &&
  973. (batadv_compare_eth(router->addr, router_router->addr))) {
  974. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  975. "Drop packet: ignoring all rebroadcast packets that may make me loop (sender: %pM)\n",
  976. ethhdr->h_source);
  977. goto out;
  978. }
  979. /* if sender is a direct neighbor the sender mac equals
  980. * originator mac
  981. */
  982. orig_neigh_node = (is_single_hop_neigh ?
  983. orig_node :
  984. batadv_get_orig_node(bat_priv, ethhdr->h_source));
  985. if (!orig_neigh_node)
  986. goto out;
  987. orig_neigh_router = batadv_orig_node_get_router(orig_neigh_node);
  988. /* drop packet if sender is not a direct neighbor and if we
  989. * don't route towards it
  990. */
  991. if (!is_single_hop_neigh && (!orig_neigh_router)) {
  992. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  993. "Drop packet: OGM via unknown neighbor!\n");
  994. goto out_neigh;
  995. }
  996. is_bidirect = batadv_iv_ogm_calc_tq(orig_node, orig_neigh_node,
  997. batadv_ogm_packet, if_incoming);
  998. batadv_bonding_save_primary(orig_node, orig_neigh_node,
  999. batadv_ogm_packet);
  1000. /* update ranking if it is not a duplicate or has the same
  1001. * seqno and similar ttl as the non-duplicate
  1002. */
  1003. sameseq = orig_node->last_real_seqno == ntohl(batadv_ogm_packet->seqno);
  1004. simlar_ttl = orig_node->last_ttl - 3 <= batadv_ogm_packet->header.ttl;
  1005. if (is_bidirect && (!is_duplicate || (sameseq && simlar_ttl)))
  1006. batadv_iv_ogm_orig_update(bat_priv, orig_node, ethhdr,
  1007. batadv_ogm_packet, if_incoming,
  1008. tt_buff, is_duplicate);
  1009. /* is single hop (direct) neighbor */
  1010. if (is_single_hop_neigh) {
  1011. /* mark direct link on incoming interface */
  1012. batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet,
  1013. is_single_hop_neigh,
  1014. is_from_best_next_hop, if_incoming);
  1015. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1016. "Forwarding packet: rebroadcast neighbor packet with direct link flag\n");
  1017. goto out_neigh;
  1018. }
  1019. /* multihop originator */
  1020. if (!is_bidirect) {
  1021. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1022. "Drop packet: not received via bidirectional link\n");
  1023. goto out_neigh;
  1024. }
  1025. if (is_duplicate) {
  1026. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1027. "Drop packet: duplicate packet received\n");
  1028. goto out_neigh;
  1029. }
  1030. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  1031. "Forwarding packet: rebroadcast originator packet\n");
  1032. batadv_iv_ogm_forward(orig_node, ethhdr, batadv_ogm_packet,
  1033. is_single_hop_neigh, is_from_best_next_hop,
  1034. if_incoming);
  1035. out_neigh:
  1036. if ((orig_neigh_node) && (!is_single_hop_neigh))
  1037. batadv_orig_node_free_ref(orig_neigh_node);
  1038. out:
  1039. if (router)
  1040. batadv_neigh_node_free_ref(router);
  1041. if (router_router)
  1042. batadv_neigh_node_free_ref(router_router);
  1043. if (orig_neigh_router)
  1044. batadv_neigh_node_free_ref(orig_neigh_router);
  1045. batadv_orig_node_free_ref(orig_node);
  1046. }
  1047. static int batadv_iv_ogm_receive(struct sk_buff *skb,
  1048. struct batadv_hard_iface *if_incoming)
  1049. {
  1050. struct batadv_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  1051. struct batadv_ogm_packet *batadv_ogm_packet;
  1052. struct ethhdr *ethhdr;
  1053. int buff_pos = 0, packet_len;
  1054. unsigned char *tt_buff, *packet_buff;
  1055. bool ret;
  1056. ret = batadv_check_management_packet(skb, if_incoming, BATADV_OGM_HLEN);
  1057. if (!ret)
  1058. return NET_RX_DROP;
  1059. /* did we receive a B.A.T.M.A.N. IV OGM packet on an interface
  1060. * that does not have B.A.T.M.A.N. IV enabled ?
  1061. */
  1062. if (bat_priv->bat_algo_ops->bat_ogm_emit != batadv_iv_ogm_emit)
  1063. return NET_RX_DROP;
  1064. batadv_inc_counter(bat_priv, BATADV_CNT_MGMT_RX);
  1065. batadv_add_counter(bat_priv, BATADV_CNT_MGMT_RX_BYTES,
  1066. skb->len + ETH_HLEN);
  1067. packet_len = skb_headlen(skb);
  1068. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  1069. packet_buff = skb->data;
  1070. batadv_ogm_packet = (struct batadv_ogm_packet *)packet_buff;
  1071. /* unpack the aggregated packets and process them one by one */
  1072. do {
  1073. tt_buff = packet_buff + buff_pos + BATADV_OGM_HLEN;
  1074. batadv_iv_ogm_process(ethhdr, batadv_ogm_packet, tt_buff,
  1075. if_incoming);
  1076. buff_pos += BATADV_OGM_HLEN;
  1077. buff_pos += batadv_tt_len(batadv_ogm_packet->tt_num_changes);
  1078. batadv_ogm_packet = (struct batadv_ogm_packet *)
  1079. (packet_buff + buff_pos);
  1080. } while (batadv_iv_ogm_aggr_packet(buff_pos, packet_len,
  1081. batadv_ogm_packet->tt_num_changes));
  1082. kfree_skb(skb);
  1083. return NET_RX_SUCCESS;
  1084. }
  1085. static struct batadv_algo_ops batadv_batman_iv __read_mostly = {
  1086. .name = "BATMAN_IV",
  1087. .bat_iface_enable = batadv_iv_ogm_iface_enable,
  1088. .bat_iface_disable = batadv_iv_ogm_iface_disable,
  1089. .bat_iface_update_mac = batadv_iv_ogm_iface_update_mac,
  1090. .bat_primary_iface_set = batadv_iv_ogm_primary_iface_set,
  1091. .bat_ogm_schedule = batadv_iv_ogm_schedule,
  1092. .bat_ogm_emit = batadv_iv_ogm_emit,
  1093. };
  1094. int __init batadv_iv_init(void)
  1095. {
  1096. int ret;
  1097. /* batman originator packet */
  1098. ret = batadv_recv_handler_register(BATADV_IV_OGM,
  1099. batadv_iv_ogm_receive);
  1100. if (ret < 0)
  1101. goto out;
  1102. ret = batadv_algo_register(&batadv_batman_iv);
  1103. if (ret < 0)
  1104. goto handler_unregister;
  1105. goto out;
  1106. handler_unregister:
  1107. batadv_recv_handler_unregister(BATADV_IV_OGM);
  1108. out:
  1109. return ret;
  1110. }