routing.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  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 batman_if *batman_if)
  37. {
  38. struct bat_priv *bat_priv = netdev_priv(batman_if->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 = batman_if->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[batman_if->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 batman_if *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 batman_if *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 batman_if *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 batman_if *if_incoming)
  497. {
  498. struct bat_priv *bat_priv = netdev_priv(if_incoming->soft_iface);
  499. struct batman_if *batman_if;
  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(batman_if, &if_list, list) {
  536. if (batman_if->if_status != IF_ACTIVE)
  537. continue;
  538. if (batman_if->soft_iface != if_incoming->soft_iface)
  539. continue;
  540. if (compare_eth(ethhdr->h_source,
  541. batman_if->net_dev->dev_addr))
  542. is_my_addr = 1;
  543. if (compare_eth(batman_packet->orig,
  544. batman_if->net_dev->dev_addr))
  545. is_my_orig = 1;
  546. if (compare_eth(batman_packet->prev_sender,
  547. batman_if->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 batman_if *batman_if)
  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. batman_if);
  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 batman_if *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 batman_if *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 batman_if *recv_if,
  1028. int hdr_size)
  1029. {
  1030. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  1031. struct orig_node *orig_node = NULL;
  1032. struct neigh_node *neigh_node = NULL;
  1033. struct unicast_packet *unicast_packet;
  1034. struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
  1035. int ret = NET_RX_DROP;
  1036. struct sk_buff *new_skb;
  1037. unicast_packet = (struct unicast_packet *)skb->data;
  1038. /* TTL exceeded */
  1039. if (unicast_packet->ttl < 2) {
  1040. pr_debug("Warning - can't forward unicast packet from %pM to "
  1041. "%pM: ttl exceeded\n", ethhdr->h_source,
  1042. unicast_packet->dest);
  1043. goto out;
  1044. }
  1045. /* get routing information */
  1046. rcu_read_lock();
  1047. orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
  1048. if (!orig_node)
  1049. goto unlock;
  1050. rcu_read_unlock();
  1051. /* find_router() increases neigh_nodes refcount if found. */
  1052. neigh_node = find_router(bat_priv, orig_node, recv_if);
  1053. if (!neigh_node)
  1054. goto out;
  1055. /* create a copy of the skb, if needed, to modify it. */
  1056. if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
  1057. goto out;
  1058. unicast_packet = (struct unicast_packet *)skb->data;
  1059. if (unicast_packet->packet_type == BAT_UNICAST &&
  1060. atomic_read(&bat_priv->fragmentation) &&
  1061. skb->len > neigh_node->if_incoming->net_dev->mtu) {
  1062. ret = frag_send_skb(skb, bat_priv,
  1063. neigh_node->if_incoming, neigh_node->addr);
  1064. goto out;
  1065. }
  1066. if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
  1067. frag_can_reassemble(skb, neigh_node->if_incoming->net_dev->mtu)) {
  1068. ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
  1069. if (ret == NET_RX_DROP)
  1070. goto out;
  1071. /* packet was buffered for late merge */
  1072. if (!new_skb) {
  1073. ret = NET_RX_SUCCESS;
  1074. goto out;
  1075. }
  1076. skb = new_skb;
  1077. unicast_packet = (struct unicast_packet *)skb->data;
  1078. }
  1079. /* decrement ttl */
  1080. unicast_packet->ttl--;
  1081. /* route it */
  1082. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  1083. ret = NET_RX_SUCCESS;
  1084. goto out;
  1085. unlock:
  1086. rcu_read_unlock();
  1087. out:
  1088. if (neigh_node)
  1089. neigh_node_free_ref(neigh_node);
  1090. if (orig_node)
  1091. orig_node_free_ref(orig_node);
  1092. return ret;
  1093. }
  1094. int recv_unicast_packet(struct sk_buff *skb, struct batman_if *recv_if)
  1095. {
  1096. struct unicast_packet *unicast_packet;
  1097. int hdr_size = sizeof(struct unicast_packet);
  1098. if (check_unicast_packet(skb, hdr_size) < 0)
  1099. return NET_RX_DROP;
  1100. unicast_packet = (struct unicast_packet *)skb->data;
  1101. /* packet for me */
  1102. if (is_my_mac(unicast_packet->dest)) {
  1103. interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
  1104. return NET_RX_SUCCESS;
  1105. }
  1106. return route_unicast_packet(skb, recv_if, hdr_size);
  1107. }
  1108. int recv_ucast_frag_packet(struct sk_buff *skb, struct batman_if *recv_if)
  1109. {
  1110. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  1111. struct unicast_frag_packet *unicast_packet;
  1112. int hdr_size = sizeof(struct unicast_frag_packet);
  1113. struct sk_buff *new_skb = NULL;
  1114. int ret;
  1115. if (check_unicast_packet(skb, hdr_size) < 0)
  1116. return NET_RX_DROP;
  1117. unicast_packet = (struct unicast_frag_packet *)skb->data;
  1118. /* packet for me */
  1119. if (is_my_mac(unicast_packet->dest)) {
  1120. ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
  1121. if (ret == NET_RX_DROP)
  1122. return NET_RX_DROP;
  1123. /* packet was buffered for late merge */
  1124. if (!new_skb)
  1125. return NET_RX_SUCCESS;
  1126. interface_rx(recv_if->soft_iface, new_skb, recv_if,
  1127. sizeof(struct unicast_packet));
  1128. return NET_RX_SUCCESS;
  1129. }
  1130. return route_unicast_packet(skb, recv_if, hdr_size);
  1131. }
  1132. int recv_bcast_packet(struct sk_buff *skb, struct batman_if *recv_if)
  1133. {
  1134. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  1135. struct orig_node *orig_node = NULL;
  1136. struct bcast_packet *bcast_packet;
  1137. struct ethhdr *ethhdr;
  1138. int hdr_size = sizeof(struct bcast_packet);
  1139. int ret = NET_RX_DROP;
  1140. int32_t seq_diff;
  1141. /* drop packet if it has not necessary minimum size */
  1142. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  1143. goto out;
  1144. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  1145. /* packet with broadcast indication but unicast recipient */
  1146. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  1147. goto out;
  1148. /* packet with broadcast sender address */
  1149. if (is_broadcast_ether_addr(ethhdr->h_source))
  1150. goto out;
  1151. /* ignore broadcasts sent by myself */
  1152. if (is_my_mac(ethhdr->h_source))
  1153. goto out;
  1154. bcast_packet = (struct bcast_packet *)skb->data;
  1155. /* ignore broadcasts originated by myself */
  1156. if (is_my_mac(bcast_packet->orig))
  1157. goto out;
  1158. if (bcast_packet->ttl < 2)
  1159. goto out;
  1160. rcu_read_lock();
  1161. orig_node = orig_hash_find(bat_priv, bcast_packet->orig);
  1162. if (!orig_node)
  1163. goto rcu_unlock;
  1164. rcu_read_unlock();
  1165. spin_lock_bh(&orig_node->bcast_seqno_lock);
  1166. /* check whether the packet is a duplicate */
  1167. if (get_bit_status(orig_node->bcast_bits, orig_node->last_bcast_seqno,
  1168. ntohl(bcast_packet->seqno)))
  1169. goto spin_unlock;
  1170. seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
  1171. /* check whether the packet is old and the host just restarted. */
  1172. if (window_protected(bat_priv, seq_diff,
  1173. &orig_node->bcast_seqno_reset))
  1174. goto spin_unlock;
  1175. /* mark broadcast in flood history, update window position
  1176. * if required. */
  1177. if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
  1178. orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
  1179. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  1180. /* rebroadcast packet */
  1181. add_bcast_packet_to_list(bat_priv, skb);
  1182. /* broadcast for me */
  1183. interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
  1184. ret = NET_RX_SUCCESS;
  1185. goto out;
  1186. rcu_unlock:
  1187. rcu_read_unlock();
  1188. goto out;
  1189. spin_unlock:
  1190. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  1191. out:
  1192. if (orig_node)
  1193. orig_node_free_ref(orig_node);
  1194. return ret;
  1195. }
  1196. int recv_vis_packet(struct sk_buff *skb, struct batman_if *recv_if)
  1197. {
  1198. struct vis_packet *vis_packet;
  1199. struct ethhdr *ethhdr;
  1200. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  1201. int hdr_size = sizeof(struct vis_packet);
  1202. /* keep skb linear */
  1203. if (skb_linearize(skb) < 0)
  1204. return NET_RX_DROP;
  1205. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  1206. return NET_RX_DROP;
  1207. vis_packet = (struct vis_packet *)skb->data;
  1208. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  1209. /* not for me */
  1210. if (!is_my_mac(ethhdr->h_dest))
  1211. return NET_RX_DROP;
  1212. /* ignore own packets */
  1213. if (is_my_mac(vis_packet->vis_orig))
  1214. return NET_RX_DROP;
  1215. if (is_my_mac(vis_packet->sender_orig))
  1216. return NET_RX_DROP;
  1217. switch (vis_packet->vis_type) {
  1218. case VIS_TYPE_SERVER_SYNC:
  1219. receive_server_sync_packet(bat_priv, vis_packet,
  1220. skb_headlen(skb));
  1221. break;
  1222. case VIS_TYPE_CLIENT_UPDATE:
  1223. receive_client_update_packet(bat_priv, vis_packet,
  1224. skb_headlen(skb));
  1225. break;
  1226. default: /* ignore unknown packet */
  1227. break;
  1228. }
  1229. /* We take a copy of the data in the packet, so we should
  1230. always free the skbuf. */
  1231. return NET_RX_DROP;
  1232. }