bat_iv_ogm.c 36 KB

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