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