bat_iv_ogm.c 38 KB

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