routing.c 40 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535
  1. /*
  2. * Copyright (C) 2007-2011 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 "routing.h"
  23. #include "send.h"
  24. #include "hash.h"
  25. #include "soft-interface.h"
  26. #include "hard-interface.h"
  27. #include "icmp_socket.h"
  28. #include "translation-table.h"
  29. #include "originator.h"
  30. #include "ring_buffer.h"
  31. #include "vis.h"
  32. #include "aggregation.h"
  33. #include "gateway_common.h"
  34. #include "gateway_client.h"
  35. #include "unicast.h"
  36. void slide_own_bcast_window(struct hard_iface *hard_iface)
  37. {
  38. struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  39. struct hashtable_t *hash = bat_priv->orig_hash;
  40. struct hlist_node *node;
  41. struct hlist_head *head;
  42. struct orig_node *orig_node;
  43. unsigned long *word;
  44. int i;
  45. size_t word_index;
  46. for (i = 0; i < hash->size; i++) {
  47. head = &hash->table[i];
  48. rcu_read_lock();
  49. hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
  50. spin_lock_bh(&orig_node->ogm_cnt_lock);
  51. word_index = hard_iface->if_num * NUM_WORDS;
  52. word = &(orig_node->bcast_own[word_index]);
  53. bit_get_packet(bat_priv, word, 1, 0);
  54. orig_node->bcast_own_sum[hard_iface->if_num] =
  55. bit_packet_count(word);
  56. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  57. }
  58. rcu_read_unlock();
  59. }
  60. }
  61. static void update_HNA(struct bat_priv *bat_priv, struct orig_node *orig_node,
  62. unsigned char *hna_buff, int hna_buff_len)
  63. {
  64. if ((hna_buff_len != orig_node->hna_buff_len) ||
  65. ((hna_buff_len > 0) &&
  66. (orig_node->hna_buff_len > 0) &&
  67. (memcmp(orig_node->hna_buff, hna_buff, hna_buff_len) != 0))) {
  68. if (orig_node->hna_buff_len > 0)
  69. hna_global_del_orig(bat_priv, orig_node,
  70. "originator changed hna");
  71. if ((hna_buff_len > 0) && (hna_buff))
  72. hna_global_add_orig(bat_priv, orig_node,
  73. hna_buff, hna_buff_len);
  74. }
  75. }
  76. static void update_route(struct bat_priv *bat_priv,
  77. struct orig_node *orig_node,
  78. struct neigh_node *neigh_node,
  79. unsigned char *hna_buff, int hna_buff_len)
  80. {
  81. struct neigh_node *neigh_node_tmp;
  82. /* route deleted */
  83. if ((orig_node->router) && (!neigh_node)) {
  84. bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
  85. orig_node->orig);
  86. hna_global_del_orig(bat_priv, orig_node,
  87. "originator timed out");
  88. /* route added */
  89. } else if ((!orig_node->router) && (neigh_node)) {
  90. bat_dbg(DBG_ROUTES, bat_priv,
  91. "Adding route towards: %pM (via %pM)\n",
  92. orig_node->orig, neigh_node->addr);
  93. hna_global_add_orig(bat_priv, orig_node,
  94. hna_buff, hna_buff_len);
  95. /* route changed */
  96. } else {
  97. bat_dbg(DBG_ROUTES, bat_priv,
  98. "Changing route towards: %pM "
  99. "(now via %pM - was via %pM)\n",
  100. orig_node->orig, neigh_node->addr,
  101. orig_node->router->addr);
  102. }
  103. if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
  104. neigh_node = NULL;
  105. neigh_node_tmp = orig_node->router;
  106. orig_node->router = neigh_node;
  107. if (neigh_node_tmp)
  108. neigh_node_free_ref(neigh_node_tmp);
  109. }
  110. void update_routes(struct bat_priv *bat_priv, struct orig_node *orig_node,
  111. struct neigh_node *neigh_node, unsigned char *hna_buff,
  112. int hna_buff_len)
  113. {
  114. if (!orig_node)
  115. return;
  116. if (orig_node->router != neigh_node)
  117. update_route(bat_priv, orig_node, neigh_node,
  118. hna_buff, hna_buff_len);
  119. /* may be just HNA changed */
  120. else
  121. update_HNA(bat_priv, orig_node, hna_buff, hna_buff_len);
  122. }
  123. static int is_bidirectional_neigh(struct orig_node *orig_node,
  124. struct orig_node *orig_neigh_node,
  125. struct batman_packet *batman_packet,
  126. struct hard_iface *if_incoming)
  127. {
  128. struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  129. struct neigh_node *neigh_node = NULL, *tmp_neigh_node;
  130. struct hlist_node *node;
  131. unsigned char total_count;
  132. uint8_t orig_eq_count, neigh_rq_count, tq_own;
  133. int tq_asym_penalty, ret = 0;
  134. if (orig_node == orig_neigh_node) {
  135. rcu_read_lock();
  136. hlist_for_each_entry_rcu(tmp_neigh_node, node,
  137. &orig_node->neigh_list, list) {
  138. if (!compare_eth(tmp_neigh_node->addr,
  139. orig_neigh_node->orig))
  140. continue;
  141. if (tmp_neigh_node->if_incoming != if_incoming)
  142. continue;
  143. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  144. continue;
  145. neigh_node = tmp_neigh_node;
  146. }
  147. rcu_read_unlock();
  148. if (!neigh_node)
  149. neigh_node = create_neighbor(orig_node,
  150. orig_neigh_node,
  151. orig_neigh_node->orig,
  152. if_incoming);
  153. if (!neigh_node)
  154. goto out;
  155. neigh_node->last_valid = jiffies;
  156. } else {
  157. /* find packet count of corresponding one hop neighbor */
  158. rcu_read_lock();
  159. hlist_for_each_entry_rcu(tmp_neigh_node, node,
  160. &orig_neigh_node->neigh_list, list) {
  161. if (!compare_eth(tmp_neigh_node->addr,
  162. orig_neigh_node->orig))
  163. continue;
  164. if (tmp_neigh_node->if_incoming != if_incoming)
  165. continue;
  166. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  167. continue;
  168. neigh_node = tmp_neigh_node;
  169. }
  170. rcu_read_unlock();
  171. if (!neigh_node)
  172. neigh_node = create_neighbor(orig_neigh_node,
  173. orig_neigh_node,
  174. orig_neigh_node->orig,
  175. if_incoming);
  176. if (!neigh_node)
  177. goto out;
  178. }
  179. orig_node->last_valid = jiffies;
  180. spin_lock_bh(&orig_node->ogm_cnt_lock);
  181. orig_eq_count = orig_neigh_node->bcast_own_sum[if_incoming->if_num];
  182. neigh_rq_count = neigh_node->real_packet_count;
  183. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  184. /* pay attention to not get a value bigger than 100 % */
  185. total_count = (orig_eq_count > neigh_rq_count ?
  186. neigh_rq_count : orig_eq_count);
  187. /* if we have too few packets (too less data) we set tq_own to zero */
  188. /* if we receive too few packets it is not considered bidirectional */
  189. if ((total_count < TQ_LOCAL_BIDRECT_SEND_MINIMUM) ||
  190. (neigh_rq_count < TQ_LOCAL_BIDRECT_RECV_MINIMUM))
  191. tq_own = 0;
  192. else
  193. /* neigh_node->real_packet_count is never zero as we
  194. * only purge old information when getting new
  195. * information */
  196. tq_own = (TQ_MAX_VALUE * total_count) / neigh_rq_count;
  197. /*
  198. * 1 - ((1-x) ** 3), normalized to TQ_MAX_VALUE this does
  199. * affect the nearly-symmetric links only a little, but
  200. * punishes asymmetric links more. This will give a value
  201. * between 0 and TQ_MAX_VALUE
  202. */
  203. tq_asym_penalty = TQ_MAX_VALUE - (TQ_MAX_VALUE *
  204. (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
  205. (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count) *
  206. (TQ_LOCAL_WINDOW_SIZE - neigh_rq_count)) /
  207. (TQ_LOCAL_WINDOW_SIZE *
  208. TQ_LOCAL_WINDOW_SIZE *
  209. TQ_LOCAL_WINDOW_SIZE);
  210. batman_packet->tq = ((batman_packet->tq * tq_own * tq_asym_penalty) /
  211. (TQ_MAX_VALUE * TQ_MAX_VALUE));
  212. bat_dbg(DBG_BATMAN, bat_priv,
  213. "bidirectional: "
  214. "orig = %-15pM neigh = %-15pM => own_bcast = %2i, "
  215. "real recv = %2i, local tq: %3i, asym_penalty: %3i, "
  216. "total tq: %3i\n",
  217. orig_node->orig, orig_neigh_node->orig, total_count,
  218. neigh_rq_count, tq_own, tq_asym_penalty, batman_packet->tq);
  219. /* if link has the minimum required transmission quality
  220. * consider it bidirectional */
  221. if (batman_packet->tq >= TQ_TOTAL_BIDRECT_LIMIT)
  222. ret = 1;
  223. out:
  224. if (neigh_node)
  225. neigh_node_free_ref(neigh_node);
  226. return ret;
  227. }
  228. /* caller must hold the neigh_list_lock */
  229. void bonding_candidate_del(struct orig_node *orig_node,
  230. struct neigh_node *neigh_node)
  231. {
  232. /* this neighbor is not part of our candidate list */
  233. if (list_empty(&neigh_node->bonding_list))
  234. goto out;
  235. list_del_rcu(&neigh_node->bonding_list);
  236. INIT_LIST_HEAD(&neigh_node->bonding_list);
  237. neigh_node_free_ref(neigh_node);
  238. atomic_dec(&orig_node->bond_candidates);
  239. out:
  240. return;
  241. }
  242. static void bonding_candidate_add(struct orig_node *orig_node,
  243. struct neigh_node *neigh_node)
  244. {
  245. struct hlist_node *node;
  246. struct neigh_node *tmp_neigh_node;
  247. uint8_t best_tq, interference_candidate = 0;
  248. spin_lock_bh(&orig_node->neigh_list_lock);
  249. /* only consider if it has the same primary address ... */
  250. if (!compare_eth(orig_node->orig,
  251. neigh_node->orig_node->primary_addr))
  252. goto candidate_del;
  253. if (!orig_node->router)
  254. goto candidate_del;
  255. best_tq = orig_node->router->tq_avg;
  256. /* ... and is good enough to be considered */
  257. if (neigh_node->tq_avg < best_tq - BONDING_TQ_THRESHOLD)
  258. goto candidate_del;
  259. /**
  260. * check if we have another candidate with the same mac address or
  261. * interface. If we do, we won't select this candidate because of
  262. * possible interference.
  263. */
  264. hlist_for_each_entry_rcu(tmp_neigh_node, node,
  265. &orig_node->neigh_list, list) {
  266. if (tmp_neigh_node == neigh_node)
  267. continue;
  268. /* we only care if the other candidate is even
  269. * considered as candidate. */
  270. if (list_empty(&tmp_neigh_node->bonding_list))
  271. continue;
  272. if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
  273. (compare_eth(neigh_node->addr, tmp_neigh_node->addr))) {
  274. interference_candidate = 1;
  275. break;
  276. }
  277. }
  278. /* don't care further if it is an interference candidate */
  279. if (interference_candidate)
  280. goto candidate_del;
  281. /* this neighbor already is part of our candidate list */
  282. if (!list_empty(&neigh_node->bonding_list))
  283. goto out;
  284. if (!atomic_inc_not_zero(&neigh_node->refcount))
  285. goto out;
  286. list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
  287. atomic_inc(&orig_node->bond_candidates);
  288. goto out;
  289. candidate_del:
  290. bonding_candidate_del(orig_node, neigh_node);
  291. out:
  292. spin_unlock_bh(&orig_node->neigh_list_lock);
  293. return;
  294. }
  295. /* copy primary address for bonding */
  296. static void bonding_save_primary(struct orig_node *orig_node,
  297. struct orig_node *orig_neigh_node,
  298. struct batman_packet *batman_packet)
  299. {
  300. if (!(batman_packet->flags & PRIMARIES_FIRST_HOP))
  301. return;
  302. memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
  303. }
  304. static void update_orig(struct bat_priv *bat_priv,
  305. struct orig_node *orig_node,
  306. struct ethhdr *ethhdr,
  307. struct batman_packet *batman_packet,
  308. struct hard_iface *if_incoming,
  309. unsigned char *hna_buff, int hna_buff_len,
  310. char is_duplicate)
  311. {
  312. struct neigh_node *neigh_node = NULL, *tmp_neigh_node = NULL;
  313. struct orig_node *orig_node_tmp;
  314. struct hlist_node *node;
  315. int tmp_hna_buff_len;
  316. uint8_t bcast_own_sum_orig, bcast_own_sum_neigh;
  317. bat_dbg(DBG_BATMAN, bat_priv, "update_originator(): "
  318. "Searching and updating originator entry of received packet\n");
  319. rcu_read_lock();
  320. hlist_for_each_entry_rcu(tmp_neigh_node, node,
  321. &orig_node->neigh_list, list) {
  322. if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
  323. (tmp_neigh_node->if_incoming == if_incoming) &&
  324. atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
  325. if (neigh_node)
  326. neigh_node_free_ref(neigh_node);
  327. neigh_node = tmp_neigh_node;
  328. continue;
  329. }
  330. if (is_duplicate)
  331. continue;
  332. ring_buffer_set(tmp_neigh_node->tq_recv,
  333. &tmp_neigh_node->tq_index, 0);
  334. tmp_neigh_node->tq_avg =
  335. ring_buffer_avg(tmp_neigh_node->tq_recv);
  336. }
  337. if (!neigh_node) {
  338. struct orig_node *orig_tmp;
  339. orig_tmp = get_orig_node(bat_priv, ethhdr->h_source);
  340. if (!orig_tmp)
  341. goto unlock;
  342. neigh_node = create_neighbor(orig_node, orig_tmp,
  343. ethhdr->h_source, if_incoming);
  344. orig_node_free_ref(orig_tmp);
  345. if (!neigh_node)
  346. goto unlock;
  347. } else
  348. bat_dbg(DBG_BATMAN, bat_priv,
  349. "Updating existing last-hop neighbor of originator\n");
  350. rcu_read_unlock();
  351. orig_node->flags = batman_packet->flags;
  352. neigh_node->last_valid = jiffies;
  353. ring_buffer_set(neigh_node->tq_recv,
  354. &neigh_node->tq_index,
  355. batman_packet->tq);
  356. neigh_node->tq_avg = ring_buffer_avg(neigh_node->tq_recv);
  357. if (!is_duplicate) {
  358. orig_node->last_ttl = batman_packet->ttl;
  359. neigh_node->last_ttl = batman_packet->ttl;
  360. }
  361. bonding_candidate_add(orig_node, neigh_node);
  362. tmp_hna_buff_len = (hna_buff_len > batman_packet->num_hna * ETH_ALEN ?
  363. batman_packet->num_hna * ETH_ALEN : hna_buff_len);
  364. /* if this neighbor already is our next hop there is nothing
  365. * to change */
  366. if (orig_node->router == neigh_node)
  367. goto update_hna;
  368. /* if this neighbor does not offer a better TQ we won't consider it */
  369. if ((orig_node->router) &&
  370. (orig_node->router->tq_avg > neigh_node->tq_avg))
  371. goto update_hna;
  372. /* if the TQ is the same and the link not more symetric we
  373. * won't consider it either */
  374. if ((orig_node->router) &&
  375. (neigh_node->tq_avg == orig_node->router->tq_avg)) {
  376. orig_node_tmp = orig_node->router->orig_node;
  377. spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
  378. bcast_own_sum_orig =
  379. orig_node_tmp->bcast_own_sum[if_incoming->if_num];
  380. spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
  381. orig_node_tmp = neigh_node->orig_node;
  382. spin_lock_bh(&orig_node_tmp->ogm_cnt_lock);
  383. bcast_own_sum_neigh =
  384. orig_node_tmp->bcast_own_sum[if_incoming->if_num];
  385. spin_unlock_bh(&orig_node_tmp->ogm_cnt_lock);
  386. if (bcast_own_sum_orig >= bcast_own_sum_neigh)
  387. goto update_hna;
  388. }
  389. update_routes(bat_priv, orig_node, neigh_node,
  390. hna_buff, tmp_hna_buff_len);
  391. goto update_gw;
  392. update_hna:
  393. update_routes(bat_priv, orig_node, orig_node->router,
  394. hna_buff, tmp_hna_buff_len);
  395. update_gw:
  396. if (orig_node->gw_flags != batman_packet->gw_flags)
  397. gw_node_update(bat_priv, orig_node, batman_packet->gw_flags);
  398. orig_node->gw_flags = batman_packet->gw_flags;
  399. /* restart gateway selection if fast or late switching was enabled */
  400. if ((orig_node->gw_flags) &&
  401. (atomic_read(&bat_priv->gw_mode) == GW_MODE_CLIENT) &&
  402. (atomic_read(&bat_priv->gw_sel_class) > 2))
  403. gw_check_election(bat_priv, orig_node);
  404. goto out;
  405. unlock:
  406. rcu_read_unlock();
  407. out:
  408. if (neigh_node)
  409. neigh_node_free_ref(neigh_node);
  410. }
  411. /* checks whether the host restarted and is in the protection time.
  412. * returns:
  413. * 0 if the packet is to be accepted
  414. * 1 if the packet is to be ignored.
  415. */
  416. static int window_protected(struct bat_priv *bat_priv,
  417. int32_t seq_num_diff,
  418. unsigned long *last_reset)
  419. {
  420. if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE)
  421. || (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
  422. if (time_after(jiffies, *last_reset +
  423. msecs_to_jiffies(RESET_PROTECTION_MS))) {
  424. *last_reset = jiffies;
  425. bat_dbg(DBG_BATMAN, bat_priv,
  426. "old packet received, start protection\n");
  427. return 0;
  428. } else
  429. return 1;
  430. }
  431. return 0;
  432. }
  433. /* processes a batman packet for all interfaces, adjusts the sequence number and
  434. * finds out whether it is a duplicate.
  435. * returns:
  436. * 1 the packet is a duplicate
  437. * 0 the packet has not yet been received
  438. * -1 the packet is old and has been received while the seqno window
  439. * was protected. Caller should drop it.
  440. */
  441. static char count_real_packets(struct ethhdr *ethhdr,
  442. struct batman_packet *batman_packet,
  443. struct hard_iface *if_incoming)
  444. {
  445. struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  446. struct orig_node *orig_node;
  447. struct neigh_node *tmp_neigh_node;
  448. struct hlist_node *node;
  449. char is_duplicate = 0;
  450. int32_t seq_diff;
  451. int need_update = 0;
  452. int set_mark, ret = -1;
  453. orig_node = get_orig_node(bat_priv, batman_packet->orig);
  454. if (!orig_node)
  455. return 0;
  456. spin_lock_bh(&orig_node->ogm_cnt_lock);
  457. seq_diff = batman_packet->seqno - orig_node->last_real_seqno;
  458. /* signalize caller that the packet is to be dropped. */
  459. if (window_protected(bat_priv, seq_diff,
  460. &orig_node->batman_seqno_reset))
  461. goto out;
  462. rcu_read_lock();
  463. hlist_for_each_entry_rcu(tmp_neigh_node, node,
  464. &orig_node->neigh_list, list) {
  465. is_duplicate |= get_bit_status(tmp_neigh_node->real_bits,
  466. orig_node->last_real_seqno,
  467. batman_packet->seqno);
  468. if (compare_eth(tmp_neigh_node->addr, ethhdr->h_source) &&
  469. (tmp_neigh_node->if_incoming == if_incoming))
  470. set_mark = 1;
  471. else
  472. set_mark = 0;
  473. /* if the window moved, set the update flag. */
  474. need_update |= bit_get_packet(bat_priv,
  475. tmp_neigh_node->real_bits,
  476. seq_diff, set_mark);
  477. tmp_neigh_node->real_packet_count =
  478. bit_packet_count(tmp_neigh_node->real_bits);
  479. }
  480. rcu_read_unlock();
  481. if (need_update) {
  482. bat_dbg(DBG_BATMAN, bat_priv,
  483. "updating last_seqno: old %d, new %d\n",
  484. orig_node->last_real_seqno, batman_packet->seqno);
  485. orig_node->last_real_seqno = batman_packet->seqno;
  486. }
  487. ret = is_duplicate;
  488. out:
  489. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  490. orig_node_free_ref(orig_node);
  491. return ret;
  492. }
  493. void receive_bat_packet(struct ethhdr *ethhdr,
  494. struct batman_packet *batman_packet,
  495. unsigned char *hna_buff, int hna_buff_len,
  496. struct hard_iface *if_incoming)
  497. {
  498. struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  499. struct hard_iface *hard_iface;
  500. struct orig_node *orig_neigh_node, *orig_node;
  501. char has_directlink_flag;
  502. char is_my_addr = 0, is_my_orig = 0, is_my_oldorig = 0;
  503. char is_broadcast = 0, is_bidirectional, is_single_hop_neigh;
  504. char is_duplicate;
  505. uint32_t if_incoming_seqno;
  506. /* Silently drop when the batman packet is actually not a
  507. * correct packet.
  508. *
  509. * This might happen if a packet is padded (e.g. Ethernet has a
  510. * minimum frame length of 64 byte) and the aggregation interprets
  511. * it as an additional length.
  512. *
  513. * TODO: A more sane solution would be to have a bit in the
  514. * batman_packet to detect whether the packet is the last
  515. * packet in an aggregation. Here we expect that the padding
  516. * is always zero (or not 0x01)
  517. */
  518. if (batman_packet->packet_type != BAT_PACKET)
  519. return;
  520. /* could be changed by schedule_own_packet() */
  521. if_incoming_seqno = atomic_read(&if_incoming->seqno);
  522. has_directlink_flag = (batman_packet->flags & DIRECTLINK ? 1 : 0);
  523. is_single_hop_neigh = (compare_eth(ethhdr->h_source,
  524. batman_packet->orig) ? 1 : 0);
  525. bat_dbg(DBG_BATMAN, bat_priv,
  526. "Received BATMAN packet via NB: %pM, IF: %s [%pM] "
  527. "(from OG: %pM, via prev OG: %pM, seqno %d, tq %d, "
  528. "TTL %d, V %d, IDF %d)\n",
  529. ethhdr->h_source, if_incoming->net_dev->name,
  530. if_incoming->net_dev->dev_addr, batman_packet->orig,
  531. batman_packet->prev_sender, batman_packet->seqno,
  532. batman_packet->tq, batman_packet->ttl, batman_packet->version,
  533. has_directlink_flag);
  534. rcu_read_lock();
  535. list_for_each_entry_rcu(hard_iface, &hardif_list, list) {
  536. if (hard_iface->if_status != IF_ACTIVE)
  537. continue;
  538. if (hard_iface->soft_iface != if_incoming->soft_iface)
  539. continue;
  540. if (compare_eth(ethhdr->h_source,
  541. hard_iface->net_dev->dev_addr))
  542. is_my_addr = 1;
  543. if (compare_eth(batman_packet->orig,
  544. hard_iface->net_dev->dev_addr))
  545. is_my_orig = 1;
  546. if (compare_eth(batman_packet->prev_sender,
  547. hard_iface->net_dev->dev_addr))
  548. is_my_oldorig = 1;
  549. if (compare_eth(ethhdr->h_source, broadcast_addr))
  550. is_broadcast = 1;
  551. }
  552. rcu_read_unlock();
  553. if (batman_packet->version != COMPAT_VERSION) {
  554. bat_dbg(DBG_BATMAN, bat_priv,
  555. "Drop packet: incompatible batman version (%i)\n",
  556. batman_packet->version);
  557. return;
  558. }
  559. if (is_my_addr) {
  560. bat_dbg(DBG_BATMAN, bat_priv,
  561. "Drop packet: received my own broadcast (sender: %pM"
  562. ")\n",
  563. ethhdr->h_source);
  564. return;
  565. }
  566. if (is_broadcast) {
  567. bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
  568. "ignoring all packets with broadcast source addr (sender: %pM"
  569. ")\n", ethhdr->h_source);
  570. return;
  571. }
  572. if (is_my_orig) {
  573. unsigned long *word;
  574. int offset;
  575. orig_neigh_node = get_orig_node(bat_priv, ethhdr->h_source);
  576. if (!orig_neigh_node)
  577. return;
  578. /* neighbor has to indicate direct link and it has to
  579. * come via the corresponding interface */
  580. /* if received seqno equals last send seqno save new
  581. * seqno for bidirectional check */
  582. if (has_directlink_flag &&
  583. compare_eth(if_incoming->net_dev->dev_addr,
  584. batman_packet->orig) &&
  585. (batman_packet->seqno - if_incoming_seqno + 2 == 0)) {
  586. offset = if_incoming->if_num * NUM_WORDS;
  587. spin_lock_bh(&orig_neigh_node->ogm_cnt_lock);
  588. word = &(orig_neigh_node->bcast_own[offset]);
  589. bit_mark(word, 0);
  590. orig_neigh_node->bcast_own_sum[if_incoming->if_num] =
  591. bit_packet_count(word);
  592. spin_unlock_bh(&orig_neigh_node->ogm_cnt_lock);
  593. }
  594. bat_dbg(DBG_BATMAN, bat_priv, "Drop packet: "
  595. "originator packet from myself (via neighbor)\n");
  596. orig_node_free_ref(orig_neigh_node);
  597. return;
  598. }
  599. if (is_my_oldorig) {
  600. bat_dbg(DBG_BATMAN, bat_priv,
  601. "Drop packet: ignoring all rebroadcast echos (sender: "
  602. "%pM)\n", ethhdr->h_source);
  603. return;
  604. }
  605. orig_node = get_orig_node(bat_priv, batman_packet->orig);
  606. if (!orig_node)
  607. return;
  608. is_duplicate = count_real_packets(ethhdr, batman_packet, if_incoming);
  609. if (is_duplicate == -1) {
  610. bat_dbg(DBG_BATMAN, bat_priv,
  611. "Drop packet: packet within seqno protection time "
  612. "(sender: %pM)\n", ethhdr->h_source);
  613. goto out;
  614. }
  615. if (batman_packet->tq == 0) {
  616. bat_dbg(DBG_BATMAN, bat_priv,
  617. "Drop packet: originator packet with tq equal 0\n");
  618. goto out;
  619. }
  620. /* avoid temporary routing loops */
  621. if ((orig_node->router) &&
  622. (orig_node->router->orig_node->router) &&
  623. (compare_eth(orig_node->router->addr,
  624. batman_packet->prev_sender)) &&
  625. !(compare_eth(batman_packet->orig, batman_packet->prev_sender)) &&
  626. (compare_eth(orig_node->router->addr,
  627. orig_node->router->orig_node->router->addr))) {
  628. bat_dbg(DBG_BATMAN, bat_priv,
  629. "Drop packet: ignoring all rebroadcast packets that "
  630. "may make me loop (sender: %pM)\n", ethhdr->h_source);
  631. goto out;
  632. }
  633. /* if sender is a direct neighbor the sender mac equals
  634. * originator mac */
  635. orig_neigh_node = (is_single_hop_neigh ?
  636. orig_node :
  637. get_orig_node(bat_priv, ethhdr->h_source));
  638. if (!orig_neigh_node)
  639. goto out;
  640. /* drop packet if sender is not a direct neighbor and if we
  641. * don't route towards it */
  642. if (!is_single_hop_neigh && (!orig_neigh_node->router)) {
  643. bat_dbg(DBG_BATMAN, bat_priv,
  644. "Drop packet: OGM via unknown neighbor!\n");
  645. goto out_neigh;
  646. }
  647. is_bidirectional = is_bidirectional_neigh(orig_node, orig_neigh_node,
  648. batman_packet, if_incoming);
  649. bonding_save_primary(orig_node, orig_neigh_node, batman_packet);
  650. /* update ranking if it is not a duplicate or has the same
  651. * seqno and similar ttl as the non-duplicate */
  652. if (is_bidirectional &&
  653. (!is_duplicate ||
  654. ((orig_node->last_real_seqno == batman_packet->seqno) &&
  655. (orig_node->last_ttl - 3 <= batman_packet->ttl))))
  656. update_orig(bat_priv, orig_node, ethhdr, batman_packet,
  657. if_incoming, hna_buff, hna_buff_len, is_duplicate);
  658. /* is single hop (direct) neighbor */
  659. if (is_single_hop_neigh) {
  660. /* mark direct link on incoming interface */
  661. schedule_forward_packet(orig_node, ethhdr, batman_packet,
  662. 1, hna_buff_len, if_incoming);
  663. bat_dbg(DBG_BATMAN, bat_priv, "Forwarding packet: "
  664. "rebroadcast neighbor packet with direct link flag\n");
  665. goto out_neigh;
  666. }
  667. /* multihop originator */
  668. if (!is_bidirectional) {
  669. bat_dbg(DBG_BATMAN, bat_priv,
  670. "Drop packet: not received via bidirectional link\n");
  671. goto out_neigh;
  672. }
  673. if (is_duplicate) {
  674. bat_dbg(DBG_BATMAN, bat_priv,
  675. "Drop packet: duplicate packet received\n");
  676. goto out_neigh;
  677. }
  678. bat_dbg(DBG_BATMAN, bat_priv,
  679. "Forwarding packet: rebroadcast originator packet\n");
  680. schedule_forward_packet(orig_node, ethhdr, batman_packet,
  681. 0, hna_buff_len, if_incoming);
  682. out_neigh:
  683. if ((orig_neigh_node) && (!is_single_hop_neigh))
  684. orig_node_free_ref(orig_neigh_node);
  685. out:
  686. orig_node_free_ref(orig_node);
  687. }
  688. int recv_bat_packet(struct sk_buff *skb, struct hard_iface *hard_iface)
  689. {
  690. struct ethhdr *ethhdr;
  691. /* drop packet if it has not necessary minimum size */
  692. if (unlikely(!pskb_may_pull(skb, sizeof(struct batman_packet))))
  693. return NET_RX_DROP;
  694. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  695. /* packet with broadcast indication but unicast recipient */
  696. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  697. return NET_RX_DROP;
  698. /* packet with broadcast sender address */
  699. if (is_broadcast_ether_addr(ethhdr->h_source))
  700. return NET_RX_DROP;
  701. /* create a copy of the skb, if needed, to modify it. */
  702. if (skb_cow(skb, 0) < 0)
  703. return NET_RX_DROP;
  704. /* keep skb linear */
  705. if (skb_linearize(skb) < 0)
  706. return NET_RX_DROP;
  707. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  708. receive_aggr_bat_packet(ethhdr,
  709. skb->data,
  710. skb_headlen(skb),
  711. hard_iface);
  712. kfree_skb(skb);
  713. return NET_RX_SUCCESS;
  714. }
  715. static int recv_my_icmp_packet(struct bat_priv *bat_priv,
  716. struct sk_buff *skb, size_t icmp_len)
  717. {
  718. struct orig_node *orig_node = NULL;
  719. struct neigh_node *neigh_node = NULL;
  720. struct icmp_packet_rr *icmp_packet;
  721. int ret = NET_RX_DROP;
  722. icmp_packet = (struct icmp_packet_rr *)skb->data;
  723. /* add data to device queue */
  724. if (icmp_packet->msg_type != ECHO_REQUEST) {
  725. bat_socket_receive_packet(icmp_packet, icmp_len);
  726. goto out;
  727. }
  728. if (!bat_priv->primary_if)
  729. goto out;
  730. /* answer echo request (ping) */
  731. /* get routing information */
  732. rcu_read_lock();
  733. orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
  734. if (!orig_node)
  735. goto unlock;
  736. neigh_node = orig_node->router;
  737. if (!neigh_node)
  738. goto unlock;
  739. if (!atomic_inc_not_zero(&neigh_node->refcount)) {
  740. neigh_node = NULL;
  741. goto unlock;
  742. }
  743. rcu_read_unlock();
  744. /* create a copy of the skb, if needed, to modify it. */
  745. if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
  746. goto out;
  747. icmp_packet = (struct icmp_packet_rr *)skb->data;
  748. memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
  749. memcpy(icmp_packet->orig,
  750. bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
  751. icmp_packet->msg_type = ECHO_REPLY;
  752. icmp_packet->ttl = TTL;
  753. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  754. ret = NET_RX_SUCCESS;
  755. goto out;
  756. unlock:
  757. rcu_read_unlock();
  758. out:
  759. if (neigh_node)
  760. neigh_node_free_ref(neigh_node);
  761. if (orig_node)
  762. orig_node_free_ref(orig_node);
  763. return ret;
  764. }
  765. static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
  766. struct sk_buff *skb)
  767. {
  768. struct orig_node *orig_node = NULL;
  769. struct neigh_node *neigh_node = NULL;
  770. struct icmp_packet *icmp_packet;
  771. int ret = NET_RX_DROP;
  772. icmp_packet = (struct icmp_packet *)skb->data;
  773. /* send TTL exceeded if packet is an echo request (traceroute) */
  774. if (icmp_packet->msg_type != ECHO_REQUEST) {
  775. pr_debug("Warning - can't forward icmp packet from %pM to "
  776. "%pM: ttl exceeded\n", icmp_packet->orig,
  777. icmp_packet->dst);
  778. goto out;
  779. }
  780. if (!bat_priv->primary_if)
  781. goto out;
  782. /* get routing information */
  783. rcu_read_lock();
  784. orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
  785. if (!orig_node)
  786. goto unlock;
  787. neigh_node = orig_node->router;
  788. if (!neigh_node)
  789. goto unlock;
  790. if (!atomic_inc_not_zero(&neigh_node->refcount)) {
  791. neigh_node = NULL;
  792. goto unlock;
  793. }
  794. rcu_read_unlock();
  795. /* create a copy of the skb, if needed, to modify it. */
  796. if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
  797. goto out;
  798. icmp_packet = (struct icmp_packet *)skb->data;
  799. memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
  800. memcpy(icmp_packet->orig,
  801. bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
  802. icmp_packet->msg_type = TTL_EXCEEDED;
  803. icmp_packet->ttl = TTL;
  804. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  805. ret = NET_RX_SUCCESS;
  806. goto out;
  807. unlock:
  808. rcu_read_unlock();
  809. out:
  810. if (neigh_node)
  811. neigh_node_free_ref(neigh_node);
  812. if (orig_node)
  813. orig_node_free_ref(orig_node);
  814. return ret;
  815. }
  816. int recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  817. {
  818. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  819. struct icmp_packet_rr *icmp_packet;
  820. struct ethhdr *ethhdr;
  821. struct orig_node *orig_node = NULL;
  822. struct neigh_node *neigh_node = NULL;
  823. int hdr_size = sizeof(struct icmp_packet);
  824. int ret = NET_RX_DROP;
  825. /**
  826. * we truncate all incoming icmp packets if they don't match our size
  827. */
  828. if (skb->len >= sizeof(struct icmp_packet_rr))
  829. hdr_size = sizeof(struct icmp_packet_rr);
  830. /* drop packet if it has not necessary minimum size */
  831. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  832. goto out;
  833. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  834. /* packet with unicast indication but broadcast recipient */
  835. if (is_broadcast_ether_addr(ethhdr->h_dest))
  836. goto out;
  837. /* packet with broadcast sender address */
  838. if (is_broadcast_ether_addr(ethhdr->h_source))
  839. goto out;
  840. /* not for me */
  841. if (!is_my_mac(ethhdr->h_dest))
  842. goto out;
  843. icmp_packet = (struct icmp_packet_rr *)skb->data;
  844. /* add record route information if not full */
  845. if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
  846. (icmp_packet->rr_cur < BAT_RR_LEN)) {
  847. memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
  848. ethhdr->h_dest, ETH_ALEN);
  849. icmp_packet->rr_cur++;
  850. }
  851. /* packet for me */
  852. if (is_my_mac(icmp_packet->dst))
  853. return recv_my_icmp_packet(bat_priv, skb, hdr_size);
  854. /* TTL exceeded */
  855. if (icmp_packet->ttl < 2)
  856. return recv_icmp_ttl_exceeded(bat_priv, skb);
  857. /* get routing information */
  858. rcu_read_lock();
  859. orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
  860. if (!orig_node)
  861. goto unlock;
  862. neigh_node = orig_node->router;
  863. if (!neigh_node)
  864. goto unlock;
  865. if (!atomic_inc_not_zero(&neigh_node->refcount)) {
  866. neigh_node = NULL;
  867. goto unlock;
  868. }
  869. rcu_read_unlock();
  870. /* create a copy of the skb, if needed, to modify it. */
  871. if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
  872. goto out;
  873. icmp_packet = (struct icmp_packet_rr *)skb->data;
  874. /* decrement ttl */
  875. icmp_packet->ttl--;
  876. /* route it */
  877. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  878. ret = NET_RX_SUCCESS;
  879. goto out;
  880. unlock:
  881. rcu_read_unlock();
  882. out:
  883. if (neigh_node)
  884. neigh_node_free_ref(neigh_node);
  885. if (orig_node)
  886. orig_node_free_ref(orig_node);
  887. return ret;
  888. }
  889. /* find a suitable router for this originator, and use
  890. * bonding if possible. increases the found neighbors
  891. * refcount.*/
  892. struct neigh_node *find_router(struct bat_priv *bat_priv,
  893. struct orig_node *orig_node,
  894. struct hard_iface *recv_if)
  895. {
  896. struct orig_node *primary_orig_node;
  897. struct orig_node *router_orig;
  898. struct neigh_node *router, *first_candidate, *tmp_neigh_node;
  899. static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  900. int bonding_enabled;
  901. if (!orig_node)
  902. return NULL;
  903. if (!orig_node->router)
  904. return NULL;
  905. /* without bonding, the first node should
  906. * always choose the default router. */
  907. bonding_enabled = atomic_read(&bat_priv->bonding);
  908. rcu_read_lock();
  909. /* select default router to output */
  910. router = orig_node->router;
  911. router_orig = orig_node->router->orig_node;
  912. if (!router_orig || !atomic_inc_not_zero(&router->refcount)) {
  913. rcu_read_unlock();
  914. return NULL;
  915. }
  916. if ((!recv_if) && (!bonding_enabled))
  917. goto return_router;
  918. /* if we have something in the primary_addr, we can search
  919. * for a potential bonding candidate. */
  920. if (compare_eth(router_orig->primary_addr, zero_mac))
  921. goto return_router;
  922. /* find the orig_node which has the primary interface. might
  923. * even be the same as our router_orig in many cases */
  924. if (compare_eth(router_orig->primary_addr, router_orig->orig)) {
  925. primary_orig_node = router_orig;
  926. } else {
  927. primary_orig_node = orig_hash_find(bat_priv,
  928. router_orig->primary_addr);
  929. if (!primary_orig_node)
  930. goto return_router;
  931. orig_node_free_ref(primary_orig_node);
  932. }
  933. /* with less than 2 candidates, we can't do any
  934. * bonding and prefer the original router. */
  935. if (atomic_read(&primary_orig_node->bond_candidates) < 2)
  936. goto return_router;
  937. /* all nodes between should choose a candidate which
  938. * is is not on the interface where the packet came
  939. * in. */
  940. neigh_node_free_ref(router);
  941. first_candidate = NULL;
  942. router = NULL;
  943. if (bonding_enabled) {
  944. /* in the bonding case, send the packets in a round
  945. * robin fashion over the remaining interfaces. */
  946. list_for_each_entry_rcu(tmp_neigh_node,
  947. &primary_orig_node->bond_list, bonding_list) {
  948. if (!first_candidate)
  949. first_candidate = tmp_neigh_node;
  950. /* recv_if == NULL on the first node. */
  951. if (tmp_neigh_node->if_incoming != recv_if &&
  952. atomic_inc_not_zero(&tmp_neigh_node->refcount)) {
  953. router = tmp_neigh_node;
  954. break;
  955. }
  956. }
  957. /* use the first candidate if nothing was found. */
  958. if (!router && first_candidate &&
  959. atomic_inc_not_zero(&first_candidate->refcount))
  960. router = first_candidate;
  961. if (!router) {
  962. rcu_read_unlock();
  963. return NULL;
  964. }
  965. /* selected should point to the next element
  966. * after the current router */
  967. spin_lock_bh(&primary_orig_node->neigh_list_lock);
  968. /* this is a list_move(), which unfortunately
  969. * does not exist as rcu version */
  970. list_del_rcu(&primary_orig_node->bond_list);
  971. list_add_rcu(&primary_orig_node->bond_list,
  972. &router->bonding_list);
  973. spin_unlock_bh(&primary_orig_node->neigh_list_lock);
  974. } else {
  975. /* if bonding is disabled, use the best of the
  976. * remaining candidates which are not using
  977. * this interface. */
  978. list_for_each_entry_rcu(tmp_neigh_node,
  979. &primary_orig_node->bond_list, bonding_list) {
  980. if (!first_candidate)
  981. first_candidate = tmp_neigh_node;
  982. /* recv_if == NULL on the first node. */
  983. if (tmp_neigh_node->if_incoming == recv_if)
  984. continue;
  985. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  986. continue;
  987. /* if we don't have a router yet
  988. * or this one is better, choose it. */
  989. if ((!router) ||
  990. (tmp_neigh_node->tq_avg > router->tq_avg)) {
  991. /* decrement refcount of
  992. * previously selected router */
  993. if (router)
  994. neigh_node_free_ref(router);
  995. router = tmp_neigh_node;
  996. atomic_inc_not_zero(&router->refcount);
  997. }
  998. neigh_node_free_ref(tmp_neigh_node);
  999. }
  1000. /* use the first candidate if nothing was found. */
  1001. if (!router && first_candidate &&
  1002. atomic_inc_not_zero(&first_candidate->refcount))
  1003. router = first_candidate;
  1004. }
  1005. return_router:
  1006. rcu_read_unlock();
  1007. return router;
  1008. }
  1009. static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
  1010. {
  1011. struct ethhdr *ethhdr;
  1012. /* drop packet if it has not necessary minimum size */
  1013. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  1014. return -1;
  1015. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  1016. /* packet with unicast indication but broadcast recipient */
  1017. if (is_broadcast_ether_addr(ethhdr->h_dest))
  1018. return -1;
  1019. /* packet with broadcast sender address */
  1020. if (is_broadcast_ether_addr(ethhdr->h_source))
  1021. return -1;
  1022. /* not for me */
  1023. if (!is_my_mac(ethhdr->h_dest))
  1024. return -1;
  1025. return 0;
  1026. }
  1027. int route_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  1028. {
  1029. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  1030. struct orig_node *orig_node = NULL;
  1031. struct neigh_node *neigh_node = NULL;
  1032. struct unicast_packet *unicast_packet;
  1033. struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
  1034. int ret = NET_RX_DROP;
  1035. struct sk_buff *new_skb;
  1036. unicast_packet = (struct unicast_packet *)skb->data;
  1037. /* TTL exceeded */
  1038. if (unicast_packet->ttl < 2) {
  1039. pr_debug("Warning - can't forward unicast packet from %pM to "
  1040. "%pM: ttl exceeded\n", ethhdr->h_source,
  1041. unicast_packet->dest);
  1042. goto out;
  1043. }
  1044. /* get routing information */
  1045. rcu_read_lock();
  1046. orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
  1047. if (!orig_node)
  1048. goto unlock;
  1049. rcu_read_unlock();
  1050. /* find_router() increases neigh_nodes refcount if found. */
  1051. neigh_node = find_router(bat_priv, orig_node, recv_if);
  1052. if (!neigh_node)
  1053. goto out;
  1054. /* create a copy of the skb, if needed, to modify it. */
  1055. if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
  1056. goto out;
  1057. unicast_packet = (struct unicast_packet *)skb->data;
  1058. if (unicast_packet->packet_type == BAT_UNICAST &&
  1059. atomic_read(&bat_priv->fragmentation) &&
  1060. skb->len > neigh_node->if_incoming->net_dev->mtu) {
  1061. ret = frag_send_skb(skb, bat_priv,
  1062. neigh_node->if_incoming, neigh_node->addr);
  1063. goto out;
  1064. }
  1065. if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
  1066. frag_can_reassemble(skb, neigh_node->if_incoming->net_dev->mtu)) {
  1067. ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
  1068. if (ret == NET_RX_DROP)
  1069. goto out;
  1070. /* packet was buffered for late merge */
  1071. if (!new_skb) {
  1072. ret = NET_RX_SUCCESS;
  1073. goto out;
  1074. }
  1075. skb = new_skb;
  1076. unicast_packet = (struct unicast_packet *)skb->data;
  1077. }
  1078. /* decrement ttl */
  1079. unicast_packet->ttl--;
  1080. /* route it */
  1081. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  1082. ret = NET_RX_SUCCESS;
  1083. goto out;
  1084. unlock:
  1085. rcu_read_unlock();
  1086. out:
  1087. if (neigh_node)
  1088. neigh_node_free_ref(neigh_node);
  1089. if (orig_node)
  1090. orig_node_free_ref(orig_node);
  1091. return ret;
  1092. }
  1093. int recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  1094. {
  1095. struct unicast_packet *unicast_packet;
  1096. int hdr_size = sizeof(struct unicast_packet);
  1097. if (check_unicast_packet(skb, hdr_size) < 0)
  1098. return NET_RX_DROP;
  1099. unicast_packet = (struct unicast_packet *)skb->data;
  1100. /* packet for me */
  1101. if (is_my_mac(unicast_packet->dest)) {
  1102. interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
  1103. return NET_RX_SUCCESS;
  1104. }
  1105. return route_unicast_packet(skb, recv_if);
  1106. }
  1107. int recv_ucast_frag_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  1108. {
  1109. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  1110. struct unicast_frag_packet *unicast_packet;
  1111. int hdr_size = sizeof(struct unicast_frag_packet);
  1112. struct sk_buff *new_skb = NULL;
  1113. int ret;
  1114. if (check_unicast_packet(skb, hdr_size) < 0)
  1115. return NET_RX_DROP;
  1116. unicast_packet = (struct unicast_frag_packet *)skb->data;
  1117. /* packet for me */
  1118. if (is_my_mac(unicast_packet->dest)) {
  1119. ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
  1120. if (ret == NET_RX_DROP)
  1121. return NET_RX_DROP;
  1122. /* packet was buffered for late merge */
  1123. if (!new_skb)
  1124. return NET_RX_SUCCESS;
  1125. interface_rx(recv_if->soft_iface, new_skb, recv_if,
  1126. sizeof(struct unicast_packet));
  1127. return NET_RX_SUCCESS;
  1128. }
  1129. return route_unicast_packet(skb, recv_if);
  1130. }
  1131. int recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  1132. {
  1133. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  1134. struct orig_node *orig_node = NULL;
  1135. struct bcast_packet *bcast_packet;
  1136. struct ethhdr *ethhdr;
  1137. int hdr_size = sizeof(struct bcast_packet);
  1138. int ret = NET_RX_DROP;
  1139. int32_t seq_diff;
  1140. /* drop packet if it has not necessary minimum size */
  1141. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  1142. goto out;
  1143. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  1144. /* packet with broadcast indication but unicast recipient */
  1145. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  1146. goto out;
  1147. /* packet with broadcast sender address */
  1148. if (is_broadcast_ether_addr(ethhdr->h_source))
  1149. goto out;
  1150. /* ignore broadcasts sent by myself */
  1151. if (is_my_mac(ethhdr->h_source))
  1152. goto out;
  1153. bcast_packet = (struct bcast_packet *)skb->data;
  1154. /* ignore broadcasts originated by myself */
  1155. if (is_my_mac(bcast_packet->orig))
  1156. goto out;
  1157. if (bcast_packet->ttl < 2)
  1158. goto out;
  1159. rcu_read_lock();
  1160. orig_node = orig_hash_find(bat_priv, bcast_packet->orig);
  1161. if (!orig_node)
  1162. goto rcu_unlock;
  1163. rcu_read_unlock();
  1164. spin_lock_bh(&orig_node->bcast_seqno_lock);
  1165. /* check whether the packet is a duplicate */
  1166. if (get_bit_status(orig_node->bcast_bits, orig_node->last_bcast_seqno,
  1167. ntohl(bcast_packet->seqno)))
  1168. goto spin_unlock;
  1169. seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
  1170. /* check whether the packet is old and the host just restarted. */
  1171. if (window_protected(bat_priv, seq_diff,
  1172. &orig_node->bcast_seqno_reset))
  1173. goto spin_unlock;
  1174. /* mark broadcast in flood history, update window position
  1175. * if required. */
  1176. if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
  1177. orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
  1178. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  1179. /* rebroadcast packet */
  1180. add_bcast_packet_to_list(bat_priv, skb);
  1181. /* broadcast for me */
  1182. interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
  1183. ret = NET_RX_SUCCESS;
  1184. goto out;
  1185. rcu_unlock:
  1186. rcu_read_unlock();
  1187. goto out;
  1188. spin_unlock:
  1189. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  1190. out:
  1191. if (orig_node)
  1192. orig_node_free_ref(orig_node);
  1193. return ret;
  1194. }
  1195. int recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  1196. {
  1197. struct vis_packet *vis_packet;
  1198. struct ethhdr *ethhdr;
  1199. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  1200. int hdr_size = sizeof(struct vis_packet);
  1201. /* keep skb linear */
  1202. if (skb_linearize(skb) < 0)
  1203. return NET_RX_DROP;
  1204. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  1205. return NET_RX_DROP;
  1206. vis_packet = (struct vis_packet *)skb->data;
  1207. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  1208. /* not for me */
  1209. if (!is_my_mac(ethhdr->h_dest))
  1210. return NET_RX_DROP;
  1211. /* ignore own packets */
  1212. if (is_my_mac(vis_packet->vis_orig))
  1213. return NET_RX_DROP;
  1214. if (is_my_mac(vis_packet->sender_orig))
  1215. return NET_RX_DROP;
  1216. switch (vis_packet->vis_type) {
  1217. case VIS_TYPE_SERVER_SYNC:
  1218. receive_server_sync_packet(bat_priv, vis_packet,
  1219. skb_headlen(skb));
  1220. break;
  1221. case VIS_TYPE_CLIENT_UPDATE:
  1222. receive_client_update_packet(bat_priv, vis_packet,
  1223. skb_headlen(skb));
  1224. break;
  1225. default: /* ignore unknown packet */
  1226. break;
  1227. }
  1228. /* We take a copy of the data in the packet, so we should
  1229. always free the skbuf. */
  1230. return NET_RX_DROP;
  1231. }