routing.c 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197
  1. /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
  2. *
  3. * Marek Lindner, Simon Wunderlich
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA
  18. */
  19. #include "main.h"
  20. #include "routing.h"
  21. #include "send.h"
  22. #include "soft-interface.h"
  23. #include "hard-interface.h"
  24. #include "icmp_socket.h"
  25. #include "translation-table.h"
  26. #include "originator.h"
  27. #include "vis.h"
  28. #include "unicast.h"
  29. #include "bridge_loop_avoidance.h"
  30. static int batadv_route_unicast_packet(struct sk_buff *skb,
  31. struct hard_iface *recv_if);
  32. void batadv_slide_own_bcast_window(struct hard_iface *hard_iface)
  33. {
  34. struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  35. struct hashtable_t *hash = bat_priv->orig_hash;
  36. struct hlist_node *node;
  37. struct hlist_head *head;
  38. struct orig_node *orig_node;
  39. unsigned long *word;
  40. uint32_t i;
  41. size_t word_index;
  42. uint8_t *w;
  43. for (i = 0; i < hash->size; i++) {
  44. head = &hash->table[i];
  45. rcu_read_lock();
  46. hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
  47. spin_lock_bh(&orig_node->ogm_cnt_lock);
  48. word_index = hard_iface->if_num * BATADV_NUM_WORDS;
  49. word = &(orig_node->bcast_own[word_index]);
  50. batadv_bit_get_packet(bat_priv, word, 1, 0);
  51. w = &orig_node->bcast_own_sum[hard_iface->if_num];
  52. *w = bitmap_weight(word, BATADV_TQ_LOCAL_WINDOW_SIZE);
  53. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  54. }
  55. rcu_read_unlock();
  56. }
  57. }
  58. static void _batadv_update_route(struct bat_priv *bat_priv,
  59. struct orig_node *orig_node,
  60. struct neigh_node *neigh_node)
  61. {
  62. struct neigh_node *curr_router;
  63. curr_router = batadv_orig_node_get_router(orig_node);
  64. /* route deleted */
  65. if ((curr_router) && (!neigh_node)) {
  66. batadv_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
  67. orig_node->orig);
  68. batadv_tt_global_del_orig(bat_priv, orig_node,
  69. "Deleted route towards originator");
  70. /* route added */
  71. } else if ((!curr_router) && (neigh_node)) {
  72. batadv_dbg(DBG_ROUTES, bat_priv,
  73. "Adding route towards: %pM (via %pM)\n",
  74. orig_node->orig, neigh_node->addr);
  75. /* route changed */
  76. } else if (neigh_node && curr_router) {
  77. batadv_dbg(DBG_ROUTES, bat_priv,
  78. "Changing route towards: %pM (now via %pM - was via %pM)\n",
  79. orig_node->orig, neigh_node->addr,
  80. curr_router->addr);
  81. }
  82. if (curr_router)
  83. batadv_neigh_node_free_ref(curr_router);
  84. /* increase refcount of new best neighbor */
  85. if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
  86. neigh_node = NULL;
  87. spin_lock_bh(&orig_node->neigh_list_lock);
  88. rcu_assign_pointer(orig_node->router, neigh_node);
  89. spin_unlock_bh(&orig_node->neigh_list_lock);
  90. /* decrease refcount of previous best neighbor */
  91. if (curr_router)
  92. batadv_neigh_node_free_ref(curr_router);
  93. }
  94. void batadv_update_route(struct bat_priv *bat_priv, struct orig_node *orig_node,
  95. struct neigh_node *neigh_node)
  96. {
  97. struct neigh_node *router = NULL;
  98. if (!orig_node)
  99. goto out;
  100. router = batadv_orig_node_get_router(orig_node);
  101. if (router != neigh_node)
  102. _batadv_update_route(bat_priv, orig_node, neigh_node);
  103. out:
  104. if (router)
  105. batadv_neigh_node_free_ref(router);
  106. }
  107. /* caller must hold the neigh_list_lock */
  108. void batadv_bonding_candidate_del(struct orig_node *orig_node,
  109. struct neigh_node *neigh_node)
  110. {
  111. /* this neighbor is not part of our candidate list */
  112. if (list_empty(&neigh_node->bonding_list))
  113. goto out;
  114. list_del_rcu(&neigh_node->bonding_list);
  115. INIT_LIST_HEAD(&neigh_node->bonding_list);
  116. batadv_neigh_node_free_ref(neigh_node);
  117. atomic_dec(&orig_node->bond_candidates);
  118. out:
  119. return;
  120. }
  121. void batadv_bonding_candidate_add(struct orig_node *orig_node,
  122. struct neigh_node *neigh_node)
  123. {
  124. struct hlist_node *node;
  125. struct neigh_node *tmp_neigh_node, *router = NULL;
  126. uint8_t interference_candidate = 0;
  127. spin_lock_bh(&orig_node->neigh_list_lock);
  128. /* only consider if it has the same primary address ... */
  129. if (!batadv_compare_eth(orig_node->orig,
  130. neigh_node->orig_node->primary_addr))
  131. goto candidate_del;
  132. router = batadv_orig_node_get_router(orig_node);
  133. if (!router)
  134. goto candidate_del;
  135. /* ... and is good enough to be considered */
  136. if (neigh_node->tq_avg < router->tq_avg - BATADV_BONDING_TQ_THRESHOLD)
  137. goto candidate_del;
  138. /* check if we have another candidate with the same mac address or
  139. * interface. If we do, we won't select this candidate because of
  140. * possible interference.
  141. */
  142. hlist_for_each_entry_rcu(tmp_neigh_node, node,
  143. &orig_node->neigh_list, list) {
  144. if (tmp_neigh_node == neigh_node)
  145. continue;
  146. /* we only care if the other candidate is even
  147. * considered as candidate.
  148. */
  149. if (list_empty(&tmp_neigh_node->bonding_list))
  150. continue;
  151. if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
  152. (batadv_compare_eth(neigh_node->addr,
  153. tmp_neigh_node->addr))) {
  154. interference_candidate = 1;
  155. break;
  156. }
  157. }
  158. /* don't care further if it is an interference candidate */
  159. if (interference_candidate)
  160. goto candidate_del;
  161. /* this neighbor already is part of our candidate list */
  162. if (!list_empty(&neigh_node->bonding_list))
  163. goto out;
  164. if (!atomic_inc_not_zero(&neigh_node->refcount))
  165. goto out;
  166. list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
  167. atomic_inc(&orig_node->bond_candidates);
  168. goto out;
  169. candidate_del:
  170. batadv_bonding_candidate_del(orig_node, neigh_node);
  171. out:
  172. spin_unlock_bh(&orig_node->neigh_list_lock);
  173. if (router)
  174. batadv_neigh_node_free_ref(router);
  175. }
  176. /* copy primary address for bonding */
  177. void
  178. batadv_bonding_save_primary(const struct orig_node *orig_node,
  179. struct orig_node *orig_neigh_node,
  180. const struct batman_ogm_packet *batman_ogm_packet)
  181. {
  182. if (!(batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
  183. return;
  184. memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
  185. }
  186. /* checks whether the host restarted and is in the protection time.
  187. * returns:
  188. * 0 if the packet is to be accepted
  189. * 1 if the packet is to be ignored.
  190. */
  191. int batadv_window_protected(struct bat_priv *bat_priv, int32_t seq_num_diff,
  192. unsigned long *last_reset)
  193. {
  194. if (seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE ||
  195. seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
  196. if (!batadv_has_timed_out(*last_reset,
  197. BATADV_RESET_PROTECTION_MS))
  198. return 1;
  199. *last_reset = jiffies;
  200. batadv_dbg(DBG_BATMAN, bat_priv,
  201. "old packet received, start protection\n");
  202. }
  203. return 0;
  204. }
  205. bool batadv_check_management_packet(struct sk_buff *skb,
  206. struct hard_iface *hard_iface,
  207. int header_len)
  208. {
  209. struct ethhdr *ethhdr;
  210. /* drop packet if it has not necessary minimum size */
  211. if (unlikely(!pskb_may_pull(skb, header_len)))
  212. return false;
  213. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  214. /* packet with broadcast indication but unicast recipient */
  215. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  216. return false;
  217. /* packet with broadcast sender address */
  218. if (is_broadcast_ether_addr(ethhdr->h_source))
  219. return false;
  220. /* create a copy of the skb, if needed, to modify it. */
  221. if (skb_cow(skb, 0) < 0)
  222. return false;
  223. /* keep skb linear */
  224. if (skb_linearize(skb) < 0)
  225. return false;
  226. return true;
  227. }
  228. static int batadv_recv_my_icmp_packet(struct bat_priv *bat_priv,
  229. struct sk_buff *skb, size_t icmp_len)
  230. {
  231. struct hard_iface *primary_if = NULL;
  232. struct orig_node *orig_node = NULL;
  233. struct neigh_node *router = NULL;
  234. struct icmp_packet_rr *icmp_packet;
  235. int ret = NET_RX_DROP;
  236. icmp_packet = (struct icmp_packet_rr *)skb->data;
  237. /* add data to device queue */
  238. if (icmp_packet->msg_type != ECHO_REQUEST) {
  239. batadv_socket_receive_packet(icmp_packet, icmp_len);
  240. goto out;
  241. }
  242. primary_if = batadv_primary_if_get_selected(bat_priv);
  243. if (!primary_if)
  244. goto out;
  245. /* answer echo request (ping) */
  246. /* get routing information */
  247. orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
  248. if (!orig_node)
  249. goto out;
  250. router = batadv_orig_node_get_router(orig_node);
  251. if (!router)
  252. goto out;
  253. /* create a copy of the skb, if needed, to modify it. */
  254. if (skb_cow(skb, ETH_HLEN) < 0)
  255. goto out;
  256. icmp_packet = (struct icmp_packet_rr *)skb->data;
  257. memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
  258. memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  259. icmp_packet->msg_type = ECHO_REPLY;
  260. icmp_packet->header.ttl = BATADV_TTL;
  261. batadv_send_skb_packet(skb, router->if_incoming, router->addr);
  262. ret = NET_RX_SUCCESS;
  263. out:
  264. if (primary_if)
  265. batadv_hardif_free_ref(primary_if);
  266. if (router)
  267. batadv_neigh_node_free_ref(router);
  268. if (orig_node)
  269. batadv_orig_node_free_ref(orig_node);
  270. return ret;
  271. }
  272. static int batadv_recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
  273. struct sk_buff *skb)
  274. {
  275. struct hard_iface *primary_if = NULL;
  276. struct orig_node *orig_node = NULL;
  277. struct neigh_node *router = NULL;
  278. struct icmp_packet *icmp_packet;
  279. int ret = NET_RX_DROP;
  280. icmp_packet = (struct icmp_packet *)skb->data;
  281. /* send TTL exceeded if packet is an echo request (traceroute) */
  282. if (icmp_packet->msg_type != ECHO_REQUEST) {
  283. pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
  284. icmp_packet->orig, icmp_packet->dst);
  285. goto out;
  286. }
  287. primary_if = batadv_primary_if_get_selected(bat_priv);
  288. if (!primary_if)
  289. goto out;
  290. /* get routing information */
  291. orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
  292. if (!orig_node)
  293. goto out;
  294. router = batadv_orig_node_get_router(orig_node);
  295. if (!router)
  296. goto out;
  297. /* create a copy of the skb, if needed, to modify it. */
  298. if (skb_cow(skb, ETH_HLEN) < 0)
  299. goto out;
  300. icmp_packet = (struct icmp_packet *)skb->data;
  301. memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
  302. memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  303. icmp_packet->msg_type = TTL_EXCEEDED;
  304. icmp_packet->header.ttl = BATADV_TTL;
  305. batadv_send_skb_packet(skb, router->if_incoming, router->addr);
  306. ret = NET_RX_SUCCESS;
  307. out:
  308. if (primary_if)
  309. batadv_hardif_free_ref(primary_if);
  310. if (router)
  311. batadv_neigh_node_free_ref(router);
  312. if (orig_node)
  313. batadv_orig_node_free_ref(orig_node);
  314. return ret;
  315. }
  316. int batadv_recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  317. {
  318. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  319. struct icmp_packet_rr *icmp_packet;
  320. struct ethhdr *ethhdr;
  321. struct orig_node *orig_node = NULL;
  322. struct neigh_node *router = NULL;
  323. int hdr_size = sizeof(struct icmp_packet);
  324. int ret = NET_RX_DROP;
  325. /* we truncate all incoming icmp packets if they don't match our size */
  326. if (skb->len >= sizeof(struct icmp_packet_rr))
  327. hdr_size = sizeof(struct icmp_packet_rr);
  328. /* drop packet if it has not necessary minimum size */
  329. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  330. goto out;
  331. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  332. /* packet with unicast indication but broadcast recipient */
  333. if (is_broadcast_ether_addr(ethhdr->h_dest))
  334. goto out;
  335. /* packet with broadcast sender address */
  336. if (is_broadcast_ether_addr(ethhdr->h_source))
  337. goto out;
  338. /* not for me */
  339. if (!batadv_is_my_mac(ethhdr->h_dest))
  340. goto out;
  341. icmp_packet = (struct icmp_packet_rr *)skb->data;
  342. /* add record route information if not full */
  343. if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
  344. (icmp_packet->rr_cur < BATADV_RR_LEN)) {
  345. memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
  346. ethhdr->h_dest, ETH_ALEN);
  347. icmp_packet->rr_cur++;
  348. }
  349. /* packet for me */
  350. if (batadv_is_my_mac(icmp_packet->dst))
  351. return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size);
  352. /* TTL exceeded */
  353. if (icmp_packet->header.ttl < 2)
  354. return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
  355. /* get routing information */
  356. orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
  357. if (!orig_node)
  358. goto out;
  359. router = batadv_orig_node_get_router(orig_node);
  360. if (!router)
  361. goto out;
  362. /* create a copy of the skb, if needed, to modify it. */
  363. if (skb_cow(skb, ETH_HLEN) < 0)
  364. goto out;
  365. icmp_packet = (struct icmp_packet_rr *)skb->data;
  366. /* decrement ttl */
  367. icmp_packet->header.ttl--;
  368. /* route it */
  369. batadv_send_skb_packet(skb, router->if_incoming, router->addr);
  370. ret = NET_RX_SUCCESS;
  371. out:
  372. if (router)
  373. batadv_neigh_node_free_ref(router);
  374. if (orig_node)
  375. batadv_orig_node_free_ref(orig_node);
  376. return ret;
  377. }
  378. /* In the bonding case, send the packets in a round
  379. * robin fashion over the remaining interfaces.
  380. *
  381. * This method rotates the bonding list and increases the
  382. * returned router's refcount.
  383. */
  384. static struct neigh_node *
  385. batadv_find_bond_router(struct orig_node *primary_orig,
  386. const struct hard_iface *recv_if)
  387. {
  388. struct neigh_node *tmp_neigh_node;
  389. struct neigh_node *router = NULL, *first_candidate = NULL;
  390. rcu_read_lock();
  391. list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
  392. bonding_list) {
  393. if (!first_candidate)
  394. first_candidate = tmp_neigh_node;
  395. /* recv_if == NULL on the first node. */
  396. if (tmp_neigh_node->if_incoming == recv_if)
  397. continue;
  398. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  399. continue;
  400. router = tmp_neigh_node;
  401. break;
  402. }
  403. /* use the first candidate if nothing was found. */
  404. if (!router && first_candidate &&
  405. atomic_inc_not_zero(&first_candidate->refcount))
  406. router = first_candidate;
  407. if (!router)
  408. goto out;
  409. /* selected should point to the next element
  410. * after the current router
  411. */
  412. spin_lock_bh(&primary_orig->neigh_list_lock);
  413. /* this is a list_move(), which unfortunately
  414. * does not exist as rcu version
  415. */
  416. list_del_rcu(&primary_orig->bond_list);
  417. list_add_rcu(&primary_orig->bond_list,
  418. &router->bonding_list);
  419. spin_unlock_bh(&primary_orig->neigh_list_lock);
  420. out:
  421. rcu_read_unlock();
  422. return router;
  423. }
  424. /* Interface Alternating: Use the best of the
  425. * remaining candidates which are not using
  426. * this interface.
  427. *
  428. * Increases the returned router's refcount
  429. */
  430. static struct neigh_node *
  431. batadv_find_ifalter_router(struct orig_node *primary_orig,
  432. const struct hard_iface *recv_if)
  433. {
  434. struct neigh_node *tmp_neigh_node;
  435. struct neigh_node *router = NULL, *first_candidate = NULL;
  436. rcu_read_lock();
  437. list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
  438. bonding_list) {
  439. if (!first_candidate)
  440. first_candidate = tmp_neigh_node;
  441. /* recv_if == NULL on the first node. */
  442. if (tmp_neigh_node->if_incoming == recv_if)
  443. continue;
  444. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  445. continue;
  446. /* if we don't have a router yet
  447. * or this one is better, choose it.
  448. */
  449. if ((!router) ||
  450. (tmp_neigh_node->tq_avg > router->tq_avg)) {
  451. /* decrement refcount of
  452. * previously selected router
  453. */
  454. if (router)
  455. batadv_neigh_node_free_ref(router);
  456. router = tmp_neigh_node;
  457. atomic_inc_not_zero(&router->refcount);
  458. }
  459. batadv_neigh_node_free_ref(tmp_neigh_node);
  460. }
  461. /* use the first candidate if nothing was found. */
  462. if (!router && first_candidate &&
  463. atomic_inc_not_zero(&first_candidate->refcount))
  464. router = first_candidate;
  465. rcu_read_unlock();
  466. return router;
  467. }
  468. int batadv_recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if)
  469. {
  470. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  471. struct tt_query_packet *tt_query;
  472. uint16_t tt_size;
  473. struct ethhdr *ethhdr;
  474. char tt_flag;
  475. /* drop packet if it has not necessary minimum size */
  476. if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet))))
  477. goto out;
  478. /* I could need to modify it */
  479. if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0)
  480. goto out;
  481. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  482. /* packet with unicast indication but broadcast recipient */
  483. if (is_broadcast_ether_addr(ethhdr->h_dest))
  484. goto out;
  485. /* packet with broadcast sender address */
  486. if (is_broadcast_ether_addr(ethhdr->h_source))
  487. goto out;
  488. tt_query = (struct tt_query_packet *)skb->data;
  489. switch (tt_query->flags & BATADV_TT_QUERY_TYPE_MASK) {
  490. case TT_REQUEST:
  491. batadv_inc_counter(bat_priv, BAT_CNT_TT_REQUEST_RX);
  492. /* If we cannot provide an answer the tt_request is
  493. * forwarded
  494. */
  495. if (!batadv_send_tt_response(bat_priv, tt_query)) {
  496. tt_flag = tt_query->flags & TT_FULL_TABLE ? 'F' : '.';
  497. batadv_dbg(DBG_TT, bat_priv,
  498. "Routing TT_REQUEST to %pM [%c]\n",
  499. tt_query->dst,
  500. tt_flag);
  501. return batadv_route_unicast_packet(skb, recv_if);
  502. }
  503. break;
  504. case TT_RESPONSE:
  505. batadv_inc_counter(bat_priv, BAT_CNT_TT_RESPONSE_RX);
  506. if (batadv_is_my_mac(tt_query->dst)) {
  507. /* packet needs to be linearized to access the TT
  508. * changes
  509. */
  510. if (skb_linearize(skb) < 0)
  511. goto out;
  512. /* skb_linearize() possibly changed skb->data */
  513. tt_query = (struct tt_query_packet *)skb->data;
  514. tt_size = batadv_tt_len(ntohs(tt_query->tt_data));
  515. /* Ensure we have all the claimed data */
  516. if (unlikely(skb_headlen(skb) <
  517. sizeof(struct tt_query_packet) + tt_size))
  518. goto out;
  519. batadv_handle_tt_response(bat_priv, tt_query);
  520. } else {
  521. tt_flag = tt_query->flags & TT_FULL_TABLE ? 'F' : '.';
  522. batadv_dbg(DBG_TT, bat_priv,
  523. "Routing TT_RESPONSE to %pM [%c]\n",
  524. tt_query->dst,
  525. tt_flag);
  526. return batadv_route_unicast_packet(skb, recv_if);
  527. }
  528. break;
  529. }
  530. out:
  531. /* returning NET_RX_DROP will make the caller function kfree the skb */
  532. return NET_RX_DROP;
  533. }
  534. int batadv_recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
  535. {
  536. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  537. struct roam_adv_packet *roam_adv_packet;
  538. struct orig_node *orig_node;
  539. struct ethhdr *ethhdr;
  540. /* drop packet if it has not necessary minimum size */
  541. if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet))))
  542. goto out;
  543. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  544. /* packet with unicast indication but broadcast recipient */
  545. if (is_broadcast_ether_addr(ethhdr->h_dest))
  546. goto out;
  547. /* packet with broadcast sender address */
  548. if (is_broadcast_ether_addr(ethhdr->h_source))
  549. goto out;
  550. batadv_inc_counter(bat_priv, BAT_CNT_TT_ROAM_ADV_RX);
  551. roam_adv_packet = (struct roam_adv_packet *)skb->data;
  552. if (!batadv_is_my_mac(roam_adv_packet->dst))
  553. return batadv_route_unicast_packet(skb, recv_if);
  554. /* check if it is a backbone gateway. we don't accept
  555. * roaming advertisement from it, as it has the same
  556. * entries as we have.
  557. */
  558. if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src))
  559. goto out;
  560. orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src);
  561. if (!orig_node)
  562. goto out;
  563. batadv_dbg(DBG_TT, bat_priv,
  564. "Received ROAMING_ADV from %pM (client %pM)\n",
  565. roam_adv_packet->src, roam_adv_packet->client);
  566. batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
  567. TT_CLIENT_ROAM,
  568. atomic_read(&orig_node->last_ttvn) + 1);
  569. /* Roaming phase starts: I have new information but the ttvn has not
  570. * been incremented yet. This flag will make me check all the incoming
  571. * packets for the correct destination.
  572. */
  573. bat_priv->tt_poss_change = true;
  574. batadv_orig_node_free_ref(orig_node);
  575. out:
  576. /* returning NET_RX_DROP will make the caller function kfree the skb */
  577. return NET_RX_DROP;
  578. }
  579. /* find a suitable router for this originator, and use
  580. * bonding if possible. increases the found neighbors
  581. * refcount.
  582. */
  583. struct neigh_node *batadv_find_router(struct bat_priv *bat_priv,
  584. struct orig_node *orig_node,
  585. const struct hard_iface *recv_if)
  586. {
  587. struct orig_node *primary_orig_node;
  588. struct orig_node *router_orig;
  589. struct neigh_node *router;
  590. static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  591. int bonding_enabled;
  592. uint8_t *primary_addr;
  593. if (!orig_node)
  594. return NULL;
  595. router = batadv_orig_node_get_router(orig_node);
  596. if (!router)
  597. goto err;
  598. /* without bonding, the first node should
  599. * always choose the default router.
  600. */
  601. bonding_enabled = atomic_read(&bat_priv->bonding);
  602. rcu_read_lock();
  603. /* select default router to output */
  604. router_orig = router->orig_node;
  605. if (!router_orig)
  606. goto err_unlock;
  607. if ((!recv_if) && (!bonding_enabled))
  608. goto return_router;
  609. primary_addr = router_orig->primary_addr;
  610. /* if we have something in the primary_addr, we can search
  611. * for a potential bonding candidate.
  612. */
  613. if (batadv_compare_eth(primary_addr, zero_mac))
  614. goto return_router;
  615. /* find the orig_node which has the primary interface. might
  616. * even be the same as our router_orig in many cases
  617. */
  618. if (batadv_compare_eth(primary_addr, router_orig->orig)) {
  619. primary_orig_node = router_orig;
  620. } else {
  621. primary_orig_node = batadv_orig_hash_find(bat_priv,
  622. primary_addr);
  623. if (!primary_orig_node)
  624. goto return_router;
  625. batadv_orig_node_free_ref(primary_orig_node);
  626. }
  627. /* with less than 2 candidates, we can't do any
  628. * bonding and prefer the original router.
  629. */
  630. if (atomic_read(&primary_orig_node->bond_candidates) < 2)
  631. goto return_router;
  632. /* all nodes between should choose a candidate which
  633. * is is not on the interface where the packet came
  634. * in.
  635. */
  636. batadv_neigh_node_free_ref(router);
  637. if (bonding_enabled)
  638. router = batadv_find_bond_router(primary_orig_node, recv_if);
  639. else
  640. router = batadv_find_ifalter_router(primary_orig_node, recv_if);
  641. return_router:
  642. if (router && router->if_incoming->if_status != IF_ACTIVE)
  643. goto err_unlock;
  644. rcu_read_unlock();
  645. return router;
  646. err_unlock:
  647. rcu_read_unlock();
  648. err:
  649. if (router)
  650. batadv_neigh_node_free_ref(router);
  651. return NULL;
  652. }
  653. static int batadv_check_unicast_packet(struct sk_buff *skb, int hdr_size)
  654. {
  655. struct ethhdr *ethhdr;
  656. /* drop packet if it has not necessary minimum size */
  657. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  658. return -1;
  659. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  660. /* packet with unicast indication but broadcast recipient */
  661. if (is_broadcast_ether_addr(ethhdr->h_dest))
  662. return -1;
  663. /* packet with broadcast sender address */
  664. if (is_broadcast_ether_addr(ethhdr->h_source))
  665. return -1;
  666. /* not for me */
  667. if (!batadv_is_my_mac(ethhdr->h_dest))
  668. return -1;
  669. return 0;
  670. }
  671. static int batadv_route_unicast_packet(struct sk_buff *skb,
  672. struct hard_iface *recv_if)
  673. {
  674. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  675. struct orig_node *orig_node = NULL;
  676. struct neigh_node *neigh_node = NULL;
  677. struct unicast_packet *unicast_packet;
  678. struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
  679. int ret = NET_RX_DROP;
  680. struct sk_buff *new_skb;
  681. unicast_packet = (struct unicast_packet *)skb->data;
  682. /* TTL exceeded */
  683. if (unicast_packet->header.ttl < 2) {
  684. pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
  685. ethhdr->h_source, unicast_packet->dest);
  686. goto out;
  687. }
  688. /* get routing information */
  689. orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
  690. if (!orig_node)
  691. goto out;
  692. /* find_router() increases neigh_nodes refcount if found. */
  693. neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
  694. if (!neigh_node)
  695. goto out;
  696. /* create a copy of the skb, if needed, to modify it. */
  697. if (skb_cow(skb, ETH_HLEN) < 0)
  698. goto out;
  699. unicast_packet = (struct unicast_packet *)skb->data;
  700. if (unicast_packet->header.packet_type == BAT_UNICAST &&
  701. atomic_read(&bat_priv->fragmentation) &&
  702. skb->len > neigh_node->if_incoming->net_dev->mtu) {
  703. ret = batadv_frag_send_skb(skb, bat_priv,
  704. neigh_node->if_incoming,
  705. neigh_node->addr);
  706. goto out;
  707. }
  708. if (unicast_packet->header.packet_type == BAT_UNICAST_FRAG &&
  709. batadv_frag_can_reassemble(skb,
  710. neigh_node->if_incoming->net_dev->mtu)) {
  711. ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
  712. if (ret == NET_RX_DROP)
  713. goto out;
  714. /* packet was buffered for late merge */
  715. if (!new_skb) {
  716. ret = NET_RX_SUCCESS;
  717. goto out;
  718. }
  719. skb = new_skb;
  720. unicast_packet = (struct unicast_packet *)skb->data;
  721. }
  722. /* decrement ttl */
  723. unicast_packet->header.ttl--;
  724. /* Update stats counter */
  725. batadv_inc_counter(bat_priv, BAT_CNT_FORWARD);
  726. batadv_add_counter(bat_priv, BAT_CNT_FORWARD_BYTES,
  727. skb->len + ETH_HLEN);
  728. /* route it */
  729. batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  730. ret = NET_RX_SUCCESS;
  731. out:
  732. if (neigh_node)
  733. batadv_neigh_node_free_ref(neigh_node);
  734. if (orig_node)
  735. batadv_orig_node_free_ref(orig_node);
  736. return ret;
  737. }
  738. static int batadv_check_unicast_ttvn(struct bat_priv *bat_priv,
  739. struct sk_buff *skb) {
  740. uint8_t curr_ttvn;
  741. struct orig_node *orig_node;
  742. struct ethhdr *ethhdr;
  743. struct hard_iface *primary_if;
  744. struct unicast_packet *unicast_packet;
  745. bool tt_poss_change;
  746. int is_old_ttvn;
  747. /* I could need to modify it */
  748. if (skb_cow(skb, sizeof(struct unicast_packet)) < 0)
  749. return 0;
  750. unicast_packet = (struct unicast_packet *)skb->data;
  751. if (batadv_is_my_mac(unicast_packet->dest)) {
  752. tt_poss_change = bat_priv->tt_poss_change;
  753. curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
  754. } else {
  755. orig_node = batadv_orig_hash_find(bat_priv,
  756. unicast_packet->dest);
  757. if (!orig_node)
  758. return 0;
  759. curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  760. tt_poss_change = orig_node->tt_poss_change;
  761. batadv_orig_node_free_ref(orig_node);
  762. }
  763. /* Check whether I have to reroute the packet */
  764. is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
  765. if (is_old_ttvn || tt_poss_change) {
  766. /* check if there is enough data before accessing it */
  767. if (pskb_may_pull(skb, sizeof(struct unicast_packet) +
  768. ETH_HLEN) < 0)
  769. return 0;
  770. ethhdr = (struct ethhdr *)(skb->data +
  771. sizeof(struct unicast_packet));
  772. /* we don't have an updated route for this client, so we should
  773. * not try to reroute the packet!!
  774. */
  775. if (batadv_tt_global_client_is_roaming(bat_priv,
  776. ethhdr->h_dest))
  777. return 1;
  778. orig_node = batadv_transtable_search(bat_priv, NULL,
  779. ethhdr->h_dest);
  780. if (!orig_node) {
  781. if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
  782. return 0;
  783. primary_if = batadv_primary_if_get_selected(bat_priv);
  784. if (!primary_if)
  785. return 0;
  786. memcpy(unicast_packet->dest,
  787. primary_if->net_dev->dev_addr, ETH_ALEN);
  788. batadv_hardif_free_ref(primary_if);
  789. } else {
  790. memcpy(unicast_packet->dest, orig_node->orig,
  791. ETH_ALEN);
  792. curr_ttvn = (uint8_t)
  793. atomic_read(&orig_node->last_ttvn);
  794. batadv_orig_node_free_ref(orig_node);
  795. }
  796. batadv_dbg(DBG_ROUTES, bat_priv,
  797. "TTVN mismatch (old_ttvn %u new_ttvn %u)! Rerouting unicast packet (for %pM) to %pM\n",
  798. unicast_packet->ttvn, curr_ttvn, ethhdr->h_dest,
  799. unicast_packet->dest);
  800. unicast_packet->ttvn = curr_ttvn;
  801. }
  802. return 1;
  803. }
  804. int batadv_recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  805. {
  806. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  807. struct unicast_packet *unicast_packet;
  808. int hdr_size = sizeof(*unicast_packet);
  809. if (batadv_check_unicast_packet(skb, hdr_size) < 0)
  810. return NET_RX_DROP;
  811. if (!batadv_check_unicast_ttvn(bat_priv, skb))
  812. return NET_RX_DROP;
  813. unicast_packet = (struct unicast_packet *)skb->data;
  814. /* packet for me */
  815. if (batadv_is_my_mac(unicast_packet->dest)) {
  816. batadv_interface_rx(recv_if->soft_iface, skb, recv_if,
  817. hdr_size);
  818. return NET_RX_SUCCESS;
  819. }
  820. return batadv_route_unicast_packet(skb, recv_if);
  821. }
  822. int batadv_recv_ucast_frag_packet(struct sk_buff *skb,
  823. struct hard_iface *recv_if)
  824. {
  825. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  826. struct unicast_frag_packet *unicast_packet;
  827. int hdr_size = sizeof(*unicast_packet);
  828. struct sk_buff *new_skb = NULL;
  829. int ret;
  830. if (batadv_check_unicast_packet(skb, hdr_size) < 0)
  831. return NET_RX_DROP;
  832. if (!batadv_check_unicast_ttvn(bat_priv, skb))
  833. return NET_RX_DROP;
  834. unicast_packet = (struct unicast_frag_packet *)skb->data;
  835. /* packet for me */
  836. if (batadv_is_my_mac(unicast_packet->dest)) {
  837. ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
  838. if (ret == NET_RX_DROP)
  839. return NET_RX_DROP;
  840. /* packet was buffered for late merge */
  841. if (!new_skb)
  842. return NET_RX_SUCCESS;
  843. batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
  844. sizeof(struct unicast_packet));
  845. return NET_RX_SUCCESS;
  846. }
  847. return batadv_route_unicast_packet(skb, recv_if);
  848. }
  849. int batadv_recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  850. {
  851. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  852. struct orig_node *orig_node = NULL;
  853. struct bcast_packet *bcast_packet;
  854. struct ethhdr *ethhdr;
  855. int hdr_size = sizeof(*bcast_packet);
  856. int ret = NET_RX_DROP;
  857. int32_t seq_diff;
  858. /* drop packet if it has not necessary minimum size */
  859. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  860. goto out;
  861. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  862. /* packet with broadcast indication but unicast recipient */
  863. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  864. goto out;
  865. /* packet with broadcast sender address */
  866. if (is_broadcast_ether_addr(ethhdr->h_source))
  867. goto out;
  868. /* ignore broadcasts sent by myself */
  869. if (batadv_is_my_mac(ethhdr->h_source))
  870. goto out;
  871. bcast_packet = (struct bcast_packet *)skb->data;
  872. /* ignore broadcasts originated by myself */
  873. if (batadv_is_my_mac(bcast_packet->orig))
  874. goto out;
  875. if (bcast_packet->header.ttl < 2)
  876. goto out;
  877. orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
  878. if (!orig_node)
  879. goto out;
  880. spin_lock_bh(&orig_node->bcast_seqno_lock);
  881. /* check whether the packet is a duplicate */
  882. if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
  883. ntohl(bcast_packet->seqno)))
  884. goto spin_unlock;
  885. seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
  886. /* check whether the packet is old and the host just restarted. */
  887. if (batadv_window_protected(bat_priv, seq_diff,
  888. &orig_node->bcast_seqno_reset))
  889. goto spin_unlock;
  890. /* mark broadcast in flood history, update window position
  891. * if required.
  892. */
  893. if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
  894. orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
  895. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  896. /* check whether this has been sent by another originator before */
  897. if (batadv_bla_check_bcast_duplist(bat_priv, bcast_packet, hdr_size))
  898. goto out;
  899. /* rebroadcast packet */
  900. batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
  901. /* don't hand the broadcast up if it is from an originator
  902. * from the same backbone.
  903. */
  904. if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
  905. goto out;
  906. /* broadcast for me */
  907. batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
  908. ret = NET_RX_SUCCESS;
  909. goto out;
  910. spin_unlock:
  911. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  912. out:
  913. if (orig_node)
  914. batadv_orig_node_free_ref(orig_node);
  915. return ret;
  916. }
  917. int batadv_recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  918. {
  919. struct vis_packet *vis_packet;
  920. struct ethhdr *ethhdr;
  921. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  922. int hdr_size = sizeof(*vis_packet);
  923. /* keep skb linear */
  924. if (skb_linearize(skb) < 0)
  925. return NET_RX_DROP;
  926. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  927. return NET_RX_DROP;
  928. vis_packet = (struct vis_packet *)skb->data;
  929. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  930. /* not for me */
  931. if (!batadv_is_my_mac(ethhdr->h_dest))
  932. return NET_RX_DROP;
  933. /* ignore own packets */
  934. if (batadv_is_my_mac(vis_packet->vis_orig))
  935. return NET_RX_DROP;
  936. if (batadv_is_my_mac(vis_packet->sender_orig))
  937. return NET_RX_DROP;
  938. switch (vis_packet->vis_type) {
  939. case VIS_TYPE_SERVER_SYNC:
  940. batadv_receive_server_sync_packet(bat_priv, vis_packet,
  941. skb_headlen(skb));
  942. break;
  943. case VIS_TYPE_CLIENT_UPDATE:
  944. batadv_receive_client_update_packet(bat_priv, vis_packet,
  945. skb_headlen(skb));
  946. break;
  947. default: /* ignore unknown packet */
  948. break;
  949. }
  950. /* We take a copy of the data in the packet, so we should
  951. * always free the skbuf.
  952. */
  953. return NET_RX_DROP;
  954. }