bat_iv_ogm.c 35 KB

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