routing.c 31 KB

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