bat_iv_ogm.c 38 KB

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