bat_iv_ogm.c 38 KB

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