routing.c 46 KB

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