bat_iv_ogm.c 41 KB

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