routing.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. /* Copyright (C) 2007-2013 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 "bridge_loop_avoidance.h"
  28. #include "distributed-arp-table.h"
  29. #include "network-coding.h"
  30. #include "fragmentation.h"
  31. #include <linux/if_vlan.h>
  32. static int batadv_route_unicast_packet(struct sk_buff *skb,
  33. struct batadv_hard_iface *recv_if);
  34. static void _batadv_update_route(struct batadv_priv *bat_priv,
  35. struct batadv_orig_node *orig_node,
  36. struct batadv_neigh_node *neigh_node)
  37. {
  38. struct batadv_neigh_node *curr_router;
  39. curr_router = batadv_orig_node_get_router(orig_node);
  40. /* route deleted */
  41. if ((curr_router) && (!neigh_node)) {
  42. batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
  43. "Deleting route towards: %pM\n", orig_node->orig);
  44. batadv_tt_global_del_orig(bat_priv, orig_node, -1,
  45. "Deleted route towards originator");
  46. /* route added */
  47. } else if ((!curr_router) && (neigh_node)) {
  48. batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
  49. "Adding route towards: %pM (via %pM)\n",
  50. orig_node->orig, neigh_node->addr);
  51. /* route changed */
  52. } else if (neigh_node && curr_router) {
  53. batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
  54. "Changing route towards: %pM (now via %pM - was via %pM)\n",
  55. orig_node->orig, neigh_node->addr,
  56. curr_router->addr);
  57. }
  58. if (curr_router)
  59. batadv_neigh_node_free_ref(curr_router);
  60. /* increase refcount of new best neighbor */
  61. if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
  62. neigh_node = NULL;
  63. spin_lock_bh(&orig_node->neigh_list_lock);
  64. rcu_assign_pointer(orig_node->router, neigh_node);
  65. spin_unlock_bh(&orig_node->neigh_list_lock);
  66. /* decrease refcount of previous best neighbor */
  67. if (curr_router)
  68. batadv_neigh_node_free_ref(curr_router);
  69. }
  70. void batadv_update_route(struct batadv_priv *bat_priv,
  71. struct batadv_orig_node *orig_node,
  72. struct batadv_neigh_node *neigh_node)
  73. {
  74. struct batadv_neigh_node *router = NULL;
  75. if (!orig_node)
  76. goto out;
  77. router = batadv_orig_node_get_router(orig_node);
  78. if (router != neigh_node)
  79. _batadv_update_route(bat_priv, orig_node, neigh_node);
  80. out:
  81. if (router)
  82. batadv_neigh_node_free_ref(router);
  83. }
  84. /* caller must hold the neigh_list_lock */
  85. void batadv_bonding_candidate_del(struct batadv_orig_node *orig_node,
  86. struct batadv_neigh_node *neigh_node)
  87. {
  88. /* this neighbor is not part of our candidate list */
  89. if (list_empty(&neigh_node->bonding_list))
  90. goto out;
  91. list_del_rcu(&neigh_node->bonding_list);
  92. INIT_LIST_HEAD(&neigh_node->bonding_list);
  93. batadv_neigh_node_free_ref(neigh_node);
  94. atomic_dec(&orig_node->bond_candidates);
  95. out:
  96. return;
  97. }
  98. void batadv_bonding_candidate_add(struct batadv_orig_node *orig_node,
  99. struct batadv_neigh_node *neigh_node)
  100. {
  101. struct batadv_neigh_node *tmp_neigh_node, *router = NULL;
  102. uint8_t interference_candidate = 0, tq;
  103. spin_lock_bh(&orig_node->neigh_list_lock);
  104. /* only consider if it has the same primary address ... */
  105. if (!batadv_compare_eth(orig_node->orig,
  106. neigh_node->orig_node->primary_addr))
  107. goto candidate_del;
  108. router = batadv_orig_node_get_router(orig_node);
  109. if (!router)
  110. goto candidate_del;
  111. /* ... and is good enough to be considered */
  112. tq = router->bat_iv.tq_avg - BATADV_BONDING_TQ_THRESHOLD;
  113. if (neigh_node->bat_iv.tq_avg < tq)
  114. goto candidate_del;
  115. /* check if we have another candidate with the same mac address or
  116. * interface. If we do, we won't select this candidate because of
  117. * possible interference.
  118. */
  119. hlist_for_each_entry_rcu(tmp_neigh_node,
  120. &orig_node->neigh_list, list) {
  121. if (tmp_neigh_node == neigh_node)
  122. continue;
  123. /* we only care if the other candidate is even
  124. * considered as candidate.
  125. */
  126. if (list_empty(&tmp_neigh_node->bonding_list))
  127. continue;
  128. if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
  129. (batadv_compare_eth(neigh_node->addr,
  130. tmp_neigh_node->addr))) {
  131. interference_candidate = 1;
  132. break;
  133. }
  134. }
  135. /* don't care further if it is an interference candidate */
  136. if (interference_candidate)
  137. goto candidate_del;
  138. /* this neighbor already is part of our candidate list */
  139. if (!list_empty(&neigh_node->bonding_list))
  140. goto out;
  141. if (!atomic_inc_not_zero(&neigh_node->refcount))
  142. goto out;
  143. list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
  144. atomic_inc(&orig_node->bond_candidates);
  145. goto out;
  146. candidate_del:
  147. batadv_bonding_candidate_del(orig_node, neigh_node);
  148. out:
  149. spin_unlock_bh(&orig_node->neigh_list_lock);
  150. if (router)
  151. batadv_neigh_node_free_ref(router);
  152. }
  153. /* copy primary address for bonding */
  154. void
  155. batadv_bonding_save_primary(const struct batadv_orig_node *orig_node,
  156. struct batadv_orig_node *orig_neigh_node,
  157. const struct batadv_ogm_packet *batman_ogm_packet)
  158. {
  159. if (!(batman_ogm_packet->flags & BATADV_PRIMARIES_FIRST_HOP))
  160. return;
  161. memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
  162. }
  163. /* checks whether the host restarted and is in the protection time.
  164. * returns:
  165. * 0 if the packet is to be accepted
  166. * 1 if the packet is to be ignored.
  167. */
  168. int batadv_window_protected(struct batadv_priv *bat_priv, int32_t seq_num_diff,
  169. unsigned long *last_reset)
  170. {
  171. if (seq_num_diff <= -BATADV_TQ_LOCAL_WINDOW_SIZE ||
  172. seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
  173. if (!batadv_has_timed_out(*last_reset,
  174. BATADV_RESET_PROTECTION_MS))
  175. return 1;
  176. *last_reset = jiffies;
  177. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  178. "old packet received, start protection\n");
  179. }
  180. return 0;
  181. }
  182. bool batadv_check_management_packet(struct sk_buff *skb,
  183. struct batadv_hard_iface *hard_iface,
  184. int header_len)
  185. {
  186. struct ethhdr *ethhdr;
  187. /* drop packet if it has not necessary minimum size */
  188. if (unlikely(!pskb_may_pull(skb, header_len)))
  189. return false;
  190. ethhdr = eth_hdr(skb);
  191. /* packet with broadcast indication but unicast recipient */
  192. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  193. return false;
  194. /* packet with broadcast sender address */
  195. if (is_broadcast_ether_addr(ethhdr->h_source))
  196. return false;
  197. /* create a copy of the skb, if needed, to modify it. */
  198. if (skb_cow(skb, 0) < 0)
  199. return false;
  200. /* keep skb linear */
  201. if (skb_linearize(skb) < 0)
  202. return false;
  203. return true;
  204. }
  205. static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
  206. struct sk_buff *skb, size_t icmp_len)
  207. {
  208. struct batadv_hard_iface *primary_if = NULL;
  209. struct batadv_orig_node *orig_node = NULL;
  210. struct batadv_icmp_packet_rr *icmp_packet;
  211. int ret = NET_RX_DROP;
  212. icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
  213. /* add data to device queue */
  214. if (icmp_packet->icmph.msg_type != BATADV_ECHO_REQUEST) {
  215. batadv_socket_receive_packet(icmp_packet, icmp_len);
  216. goto out;
  217. }
  218. primary_if = batadv_primary_if_get_selected(bat_priv);
  219. if (!primary_if)
  220. goto out;
  221. /* answer echo request (ping) */
  222. /* get routing information */
  223. orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->icmph.orig);
  224. if (!orig_node)
  225. goto out;
  226. /* create a copy of the skb, if needed, to modify it. */
  227. if (skb_cow(skb, ETH_HLEN) < 0)
  228. goto out;
  229. icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
  230. memcpy(icmp_packet->icmph.dst, icmp_packet->icmph.orig, ETH_ALEN);
  231. memcpy(icmp_packet->icmph.orig, primary_if->net_dev->dev_addr,
  232. ETH_ALEN);
  233. icmp_packet->icmph.msg_type = BATADV_ECHO_REPLY;
  234. icmp_packet->icmph.header.ttl = BATADV_TTL;
  235. if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
  236. ret = NET_RX_SUCCESS;
  237. out:
  238. if (primary_if)
  239. batadv_hardif_free_ref(primary_if);
  240. if (orig_node)
  241. batadv_orig_node_free_ref(orig_node);
  242. return ret;
  243. }
  244. static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
  245. struct sk_buff *skb)
  246. {
  247. struct batadv_hard_iface *primary_if = NULL;
  248. struct batadv_orig_node *orig_node = NULL;
  249. struct batadv_icmp_packet *icmp_packet;
  250. int ret = NET_RX_DROP;
  251. icmp_packet = (struct batadv_icmp_packet *)skb->data;
  252. /* send TTL exceeded if packet is an echo request (traceroute) */
  253. if (icmp_packet->icmph.msg_type != BATADV_ECHO_REQUEST) {
  254. pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
  255. icmp_packet->icmph.orig, icmp_packet->icmph.dst);
  256. goto out;
  257. }
  258. primary_if = batadv_primary_if_get_selected(bat_priv);
  259. if (!primary_if)
  260. goto out;
  261. /* get routing information */
  262. orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->icmph.orig);
  263. if (!orig_node)
  264. goto out;
  265. /* create a copy of the skb, if needed, to modify it. */
  266. if (skb_cow(skb, ETH_HLEN) < 0)
  267. goto out;
  268. icmp_packet = (struct batadv_icmp_packet *)skb->data;
  269. memcpy(icmp_packet->icmph.dst, icmp_packet->icmph.orig, ETH_ALEN);
  270. memcpy(icmp_packet->icmph.orig, primary_if->net_dev->dev_addr,
  271. ETH_ALEN);
  272. icmp_packet->icmph.msg_type = BATADV_TTL_EXCEEDED;
  273. icmp_packet->icmph.header.ttl = BATADV_TTL;
  274. if (batadv_send_skb_to_orig(skb, orig_node, NULL) != NET_XMIT_DROP)
  275. ret = NET_RX_SUCCESS;
  276. out:
  277. if (primary_if)
  278. batadv_hardif_free_ref(primary_if);
  279. if (orig_node)
  280. batadv_orig_node_free_ref(orig_node);
  281. return ret;
  282. }
  283. int batadv_recv_icmp_packet(struct sk_buff *skb,
  284. struct batadv_hard_iface *recv_if)
  285. {
  286. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  287. struct batadv_icmp_packet_rr *icmp_packet;
  288. struct ethhdr *ethhdr;
  289. struct batadv_orig_node *orig_node = NULL;
  290. int hdr_size = sizeof(struct batadv_icmp_packet);
  291. int ret = NET_RX_DROP;
  292. /* we truncate all incoming icmp packets if they don't match our size */
  293. if (skb->len >= sizeof(struct batadv_icmp_packet_rr))
  294. hdr_size = sizeof(struct batadv_icmp_packet_rr);
  295. /* drop packet if it has not necessary minimum size */
  296. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  297. goto out;
  298. ethhdr = eth_hdr(skb);
  299. /* packet with unicast indication but broadcast recipient */
  300. if (is_broadcast_ether_addr(ethhdr->h_dest))
  301. goto out;
  302. /* packet with broadcast sender address */
  303. if (is_broadcast_ether_addr(ethhdr->h_source))
  304. goto out;
  305. /* not for me */
  306. if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
  307. goto out;
  308. icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
  309. /* add record route information if not full */
  310. if ((icmp_packet->icmph.msg_type == BATADV_ECHO_REPLY ||
  311. icmp_packet->icmph.msg_type == BATADV_ECHO_REQUEST) &&
  312. (hdr_size == sizeof(struct batadv_icmp_packet_rr)) &&
  313. (icmp_packet->rr_cur < BATADV_RR_LEN)) {
  314. memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
  315. ethhdr->h_dest, ETH_ALEN);
  316. icmp_packet->rr_cur++;
  317. }
  318. /* packet for me */
  319. if (batadv_is_my_mac(bat_priv, icmp_packet->icmph.dst))
  320. return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size);
  321. /* TTL exceeded */
  322. if (icmp_packet->icmph.header.ttl < 2)
  323. return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
  324. /* get routing information */
  325. orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->icmph.dst);
  326. if (!orig_node)
  327. goto out;
  328. /* create a copy of the skb, if needed, to modify it. */
  329. if (skb_cow(skb, ETH_HLEN) < 0)
  330. goto out;
  331. icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
  332. /* decrement ttl */
  333. icmp_packet->icmph.header.ttl--;
  334. /* route it */
  335. if (batadv_send_skb_to_orig(skb, orig_node, recv_if) != NET_XMIT_DROP)
  336. ret = NET_RX_SUCCESS;
  337. out:
  338. if (orig_node)
  339. batadv_orig_node_free_ref(orig_node);
  340. return ret;
  341. }
  342. /* In the bonding case, send the packets in a round
  343. * robin fashion over the remaining interfaces.
  344. *
  345. * This method rotates the bonding list and increases the
  346. * returned router's refcount.
  347. */
  348. static struct batadv_neigh_node *
  349. batadv_find_bond_router(struct batadv_orig_node *primary_orig,
  350. const struct batadv_hard_iface *recv_if)
  351. {
  352. struct batadv_neigh_node *tmp_neigh_node;
  353. struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
  354. rcu_read_lock();
  355. list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
  356. bonding_list) {
  357. if (!first_candidate)
  358. first_candidate = tmp_neigh_node;
  359. /* recv_if == NULL on the first node. */
  360. if (tmp_neigh_node->if_incoming == recv_if)
  361. continue;
  362. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  363. continue;
  364. router = tmp_neigh_node;
  365. break;
  366. }
  367. /* use the first candidate if nothing was found. */
  368. if (!router && first_candidate &&
  369. atomic_inc_not_zero(&first_candidate->refcount))
  370. router = first_candidate;
  371. if (!router)
  372. goto out;
  373. /* selected should point to the next element
  374. * after the current router
  375. */
  376. spin_lock_bh(&primary_orig->neigh_list_lock);
  377. /* this is a list_move(), which unfortunately
  378. * does not exist as rcu version
  379. */
  380. list_del_rcu(&primary_orig->bond_list);
  381. list_add_rcu(&primary_orig->bond_list,
  382. &router->bonding_list);
  383. spin_unlock_bh(&primary_orig->neigh_list_lock);
  384. out:
  385. rcu_read_unlock();
  386. return router;
  387. }
  388. /* Interface Alternating: Use the best of the
  389. * remaining candidates which are not using
  390. * this interface.
  391. *
  392. * Increases the returned router's refcount
  393. */
  394. static struct batadv_neigh_node *
  395. batadv_find_ifalter_router(struct batadv_orig_node *primary_orig,
  396. const struct batadv_hard_iface *recv_if)
  397. {
  398. struct batadv_neigh_node *tmp_neigh_node;
  399. struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
  400. rcu_read_lock();
  401. list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
  402. bonding_list) {
  403. if (!first_candidate)
  404. first_candidate = tmp_neigh_node;
  405. /* recv_if == NULL on the first node. */
  406. if (tmp_neigh_node->if_incoming == recv_if)
  407. continue;
  408. if (router &&
  409. tmp_neigh_node->bat_iv.tq_avg <= router->bat_iv.tq_avg)
  410. continue;
  411. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  412. continue;
  413. /* decrement refcount of previously selected router */
  414. if (router)
  415. batadv_neigh_node_free_ref(router);
  416. /* we found a better router (or at least one valid router) */
  417. router = tmp_neigh_node;
  418. }
  419. /* use the first candidate if nothing was found. */
  420. if (!router && first_candidate &&
  421. atomic_inc_not_zero(&first_candidate->refcount))
  422. router = first_candidate;
  423. rcu_read_unlock();
  424. return router;
  425. }
  426. /**
  427. * batadv_check_unicast_packet - Check for malformed unicast packets
  428. * @bat_priv: the bat priv with all the soft interface information
  429. * @skb: packet to check
  430. * @hdr_size: size of header to pull
  431. *
  432. * Check for short header and bad addresses in given packet. Returns negative
  433. * value when check fails and 0 otherwise. The negative value depends on the
  434. * reason: -ENODATA for bad header, -EBADR for broadcast destination or source,
  435. * and -EREMOTE for non-local (other host) destination.
  436. */
  437. static int batadv_check_unicast_packet(struct batadv_priv *bat_priv,
  438. struct sk_buff *skb, int hdr_size)
  439. {
  440. struct ethhdr *ethhdr;
  441. /* drop packet if it has not necessary minimum size */
  442. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  443. return -ENODATA;
  444. ethhdr = eth_hdr(skb);
  445. /* packet with unicast indication but broadcast recipient */
  446. if (is_broadcast_ether_addr(ethhdr->h_dest))
  447. return -EBADR;
  448. /* packet with broadcast sender address */
  449. if (is_broadcast_ether_addr(ethhdr->h_source))
  450. return -EBADR;
  451. /* not for me */
  452. if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
  453. return -EREMOTE;
  454. return 0;
  455. }
  456. /* find a suitable router for this originator, and use
  457. * bonding if possible. increases the found neighbors
  458. * refcount.
  459. */
  460. struct batadv_neigh_node *
  461. batadv_find_router(struct batadv_priv *bat_priv,
  462. struct batadv_orig_node *orig_node,
  463. const struct batadv_hard_iface *recv_if)
  464. {
  465. struct batadv_orig_node *primary_orig_node;
  466. struct batadv_orig_node *router_orig;
  467. struct batadv_neigh_node *router;
  468. static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  469. int bonding_enabled;
  470. uint8_t *primary_addr;
  471. if (!orig_node)
  472. return NULL;
  473. router = batadv_orig_node_get_router(orig_node);
  474. if (!router)
  475. goto err;
  476. /* without bonding, the first node should
  477. * always choose the default router.
  478. */
  479. bonding_enabled = atomic_read(&bat_priv->bonding);
  480. rcu_read_lock();
  481. /* select default router to output */
  482. router_orig = router->orig_node;
  483. if (!router_orig)
  484. goto err_unlock;
  485. if ((!recv_if) && (!bonding_enabled))
  486. goto return_router;
  487. primary_addr = router_orig->primary_addr;
  488. /* if we have something in the primary_addr, we can search
  489. * for a potential bonding candidate.
  490. */
  491. if (batadv_compare_eth(primary_addr, zero_mac))
  492. goto return_router;
  493. /* find the orig_node which has the primary interface. might
  494. * even be the same as our router_orig in many cases
  495. */
  496. if (batadv_compare_eth(primary_addr, router_orig->orig)) {
  497. primary_orig_node = router_orig;
  498. } else {
  499. primary_orig_node = batadv_orig_hash_find(bat_priv,
  500. primary_addr);
  501. if (!primary_orig_node)
  502. goto return_router;
  503. batadv_orig_node_free_ref(primary_orig_node);
  504. }
  505. /* with less than 2 candidates, we can't do any
  506. * bonding and prefer the original router.
  507. */
  508. if (atomic_read(&primary_orig_node->bond_candidates) < 2)
  509. goto return_router;
  510. /* all nodes between should choose a candidate which
  511. * is is not on the interface where the packet came
  512. * in.
  513. */
  514. batadv_neigh_node_free_ref(router);
  515. if (bonding_enabled)
  516. router = batadv_find_bond_router(primary_orig_node, recv_if);
  517. else
  518. router = batadv_find_ifalter_router(primary_orig_node, recv_if);
  519. return_router:
  520. if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE)
  521. goto err_unlock;
  522. rcu_read_unlock();
  523. return router;
  524. err_unlock:
  525. rcu_read_unlock();
  526. err:
  527. if (router)
  528. batadv_neigh_node_free_ref(router);
  529. return NULL;
  530. }
  531. static int batadv_route_unicast_packet(struct sk_buff *skb,
  532. struct batadv_hard_iface *recv_if)
  533. {
  534. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  535. struct batadv_orig_node *orig_node = NULL;
  536. struct batadv_unicast_packet *unicast_packet;
  537. struct ethhdr *ethhdr = eth_hdr(skb);
  538. int res, hdr_len, ret = NET_RX_DROP;
  539. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  540. /* TTL exceeded */
  541. if (unicast_packet->header.ttl < 2) {
  542. pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
  543. ethhdr->h_source, unicast_packet->dest);
  544. goto out;
  545. }
  546. /* get routing information */
  547. orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
  548. if (!orig_node)
  549. goto out;
  550. /* create a copy of the skb, if needed, to modify it. */
  551. if (skb_cow(skb, ETH_HLEN) < 0)
  552. goto out;
  553. /* decrement ttl */
  554. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  555. unicast_packet->header.ttl--;
  556. switch (unicast_packet->header.packet_type) {
  557. case BATADV_UNICAST_4ADDR:
  558. hdr_len = sizeof(struct batadv_unicast_4addr_packet);
  559. break;
  560. case BATADV_UNICAST:
  561. hdr_len = sizeof(struct batadv_unicast_packet);
  562. break;
  563. default:
  564. /* other packet types not supported - yet */
  565. hdr_len = -1;
  566. break;
  567. }
  568. if (hdr_len > 0)
  569. batadv_skb_set_priority(skb, hdr_len);
  570. res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
  571. /* translate transmit result into receive result */
  572. if (res == NET_XMIT_SUCCESS) {
  573. /* skb was transmitted and consumed */
  574. batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
  575. batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
  576. skb->len + ETH_HLEN);
  577. ret = NET_RX_SUCCESS;
  578. } else if (res == NET_XMIT_POLICED) {
  579. /* skb was buffered and consumed */
  580. ret = NET_RX_SUCCESS;
  581. }
  582. out:
  583. if (orig_node)
  584. batadv_orig_node_free_ref(orig_node);
  585. return ret;
  586. }
  587. /**
  588. * batadv_reroute_unicast_packet - update the unicast header for re-routing
  589. * @bat_priv: the bat priv with all the soft interface information
  590. * @unicast_packet: the unicast header to be updated
  591. * @dst_addr: the payload destination
  592. * @vid: VLAN identifier
  593. *
  594. * Search the translation table for dst_addr and update the unicast header with
  595. * the new corresponding information (originator address where the destination
  596. * client currently is and its known TTVN)
  597. *
  598. * Returns true if the packet header has been updated, false otherwise
  599. */
  600. static bool
  601. batadv_reroute_unicast_packet(struct batadv_priv *bat_priv,
  602. struct batadv_unicast_packet *unicast_packet,
  603. uint8_t *dst_addr, unsigned short vid)
  604. {
  605. struct batadv_orig_node *orig_node = NULL;
  606. struct batadv_hard_iface *primary_if = NULL;
  607. bool ret = false;
  608. uint8_t *orig_addr, orig_ttvn;
  609. if (batadv_is_my_client(bat_priv, dst_addr, vid)) {
  610. primary_if = batadv_primary_if_get_selected(bat_priv);
  611. if (!primary_if)
  612. goto out;
  613. orig_addr = primary_if->net_dev->dev_addr;
  614. orig_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
  615. } else {
  616. orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr,
  617. vid);
  618. if (!orig_node)
  619. goto out;
  620. if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
  621. goto out;
  622. orig_addr = orig_node->orig;
  623. orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  624. }
  625. /* update the packet header */
  626. memcpy(unicast_packet->dest, orig_addr, ETH_ALEN);
  627. unicast_packet->ttvn = orig_ttvn;
  628. ret = true;
  629. out:
  630. if (primary_if)
  631. batadv_hardif_free_ref(primary_if);
  632. if (orig_node)
  633. batadv_orig_node_free_ref(orig_node);
  634. return ret;
  635. }
  636. static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
  637. struct sk_buff *skb, int hdr_len) {
  638. struct batadv_unicast_packet *unicast_packet;
  639. struct batadv_hard_iface *primary_if;
  640. struct batadv_orig_node *orig_node;
  641. uint8_t curr_ttvn, old_ttvn;
  642. struct ethhdr *ethhdr;
  643. unsigned short vid;
  644. int is_old_ttvn;
  645. /* check if there is enough data before accessing it */
  646. if (pskb_may_pull(skb, hdr_len + ETH_HLEN) < 0)
  647. return 0;
  648. /* create a copy of the skb (in case of for re-routing) to modify it. */
  649. if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
  650. return 0;
  651. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  652. vid = batadv_get_vid(skb, hdr_len);
  653. ethhdr = (struct ethhdr *)(skb->data + hdr_len);
  654. /* check if the destination client was served by this node and it is now
  655. * roaming. In this case, it means that the node has got a ROAM_ADV
  656. * message and that it knows the new destination in the mesh to re-route
  657. * the packet to
  658. */
  659. if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest, vid)) {
  660. if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
  661. ethhdr->h_dest, vid))
  662. net_ratelimited_function(batadv_dbg, BATADV_DBG_TT,
  663. bat_priv,
  664. "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
  665. unicast_packet->dest,
  666. ethhdr->h_dest);
  667. /* at this point the mesh destination should have been
  668. * substituted with the originator address found in the global
  669. * table. If not, let the packet go untouched anyway because
  670. * there is nothing the node can do
  671. */
  672. return 1;
  673. }
  674. /* retrieve the TTVN known by this node for the packet destination. This
  675. * value is used later to check if the node which sent (or re-routed
  676. * last time) the packet had an updated information or not
  677. */
  678. curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
  679. if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
  680. orig_node = batadv_orig_hash_find(bat_priv,
  681. unicast_packet->dest);
  682. /* if it is not possible to find the orig_node representing the
  683. * destination, the packet can immediately be dropped as it will
  684. * not be possible to deliver it
  685. */
  686. if (!orig_node)
  687. return 0;
  688. curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  689. batadv_orig_node_free_ref(orig_node);
  690. }
  691. /* check if the TTVN contained in the packet is fresher than what the
  692. * node knows
  693. */
  694. is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
  695. if (!is_old_ttvn)
  696. return 1;
  697. old_ttvn = unicast_packet->ttvn;
  698. /* the packet was forged based on outdated network information. Its
  699. * destination can possibly be updated and forwarded towards the new
  700. * target host
  701. */
  702. if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
  703. ethhdr->h_dest, vid)) {
  704. net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, bat_priv,
  705. "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
  706. unicast_packet->dest, ethhdr->h_dest,
  707. old_ttvn, curr_ttvn);
  708. return 1;
  709. }
  710. /* the packet has not been re-routed: either the destination is
  711. * currently served by this node or there is no destination at all and
  712. * it is possible to drop the packet
  713. */
  714. if (!batadv_is_my_client(bat_priv, ethhdr->h_dest, vid))
  715. return 0;
  716. /* update the header in order to let the packet be delivered to this
  717. * node's soft interface
  718. */
  719. primary_if = batadv_primary_if_get_selected(bat_priv);
  720. if (!primary_if)
  721. return 0;
  722. memcpy(unicast_packet->dest, primary_if->net_dev->dev_addr, ETH_ALEN);
  723. batadv_hardif_free_ref(primary_if);
  724. unicast_packet->ttvn = curr_ttvn;
  725. return 1;
  726. }
  727. /**
  728. * batadv_recv_unhandled_unicast_packet - receive and process packets which
  729. * are in the unicast number space but not yet known to the implementation
  730. * @skb: unicast tvlv packet to process
  731. * @recv_if: pointer to interface this packet was received on
  732. *
  733. * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
  734. * otherwise.
  735. */
  736. int batadv_recv_unhandled_unicast_packet(struct sk_buff *skb,
  737. struct batadv_hard_iface *recv_if)
  738. {
  739. struct batadv_unicast_packet *unicast_packet;
  740. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  741. int check, hdr_size = sizeof(*unicast_packet);
  742. check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
  743. if (check < 0)
  744. return NET_RX_DROP;
  745. /* we don't know about this type, drop it. */
  746. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  747. if (batadv_is_my_mac(bat_priv, unicast_packet->dest))
  748. return NET_RX_DROP;
  749. return batadv_route_unicast_packet(skb, recv_if);
  750. }
  751. int batadv_recv_unicast_packet(struct sk_buff *skb,
  752. struct batadv_hard_iface *recv_if)
  753. {
  754. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  755. struct batadv_unicast_packet *unicast_packet;
  756. struct batadv_unicast_4addr_packet *unicast_4addr_packet;
  757. uint8_t *orig_addr;
  758. struct batadv_orig_node *orig_node = NULL;
  759. int check, hdr_size = sizeof(*unicast_packet);
  760. bool is4addr;
  761. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  762. unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
  763. is4addr = unicast_packet->header.packet_type == BATADV_UNICAST_4ADDR;
  764. /* the caller function should have already pulled 2 bytes */
  765. if (is4addr)
  766. hdr_size = sizeof(*unicast_4addr_packet);
  767. /* function returns -EREMOTE for promiscuous packets */
  768. check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
  769. /* Even though the packet is not for us, we might save it to use for
  770. * decoding a later received coded packet
  771. */
  772. if (check == -EREMOTE)
  773. batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
  774. if (check < 0)
  775. return NET_RX_DROP;
  776. if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
  777. return NET_RX_DROP;
  778. /* packet for me */
  779. if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
  780. if (is4addr) {
  781. batadv_dat_inc_counter(bat_priv,
  782. unicast_4addr_packet->subtype);
  783. orig_addr = unicast_4addr_packet->src;
  784. orig_node = batadv_orig_hash_find(bat_priv, orig_addr);
  785. }
  786. if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
  787. hdr_size))
  788. goto rx_success;
  789. if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
  790. hdr_size))
  791. goto rx_success;
  792. batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
  793. orig_node);
  794. rx_success:
  795. if (orig_node)
  796. batadv_orig_node_free_ref(orig_node);
  797. return NET_RX_SUCCESS;
  798. }
  799. return batadv_route_unicast_packet(skb, recv_if);
  800. }
  801. /**
  802. * batadv_recv_unicast_tvlv - receive and process unicast tvlv packets
  803. * @skb: unicast tvlv packet to process
  804. * @recv_if: pointer to interface this packet was received on
  805. * @dst_addr: the payload destination
  806. *
  807. * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
  808. * otherwise.
  809. */
  810. int batadv_recv_unicast_tvlv(struct sk_buff *skb,
  811. struct batadv_hard_iface *recv_if)
  812. {
  813. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  814. struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
  815. unsigned char *tvlv_buff;
  816. uint16_t tvlv_buff_len;
  817. int hdr_size = sizeof(*unicast_tvlv_packet);
  818. int ret = NET_RX_DROP;
  819. if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
  820. return NET_RX_DROP;
  821. /* the header is likely to be modified while forwarding */
  822. if (skb_cow(skb, hdr_size) < 0)
  823. return NET_RX_DROP;
  824. /* packet needs to be linearized to access the tvlv content */
  825. if (skb_linearize(skb) < 0)
  826. return NET_RX_DROP;
  827. unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)skb->data;
  828. tvlv_buff = (unsigned char *)(skb->data + hdr_size);
  829. tvlv_buff_len = ntohs(unicast_tvlv_packet->tvlv_len);
  830. if (tvlv_buff_len > skb->len - hdr_size)
  831. return NET_RX_DROP;
  832. ret = batadv_tvlv_containers_process(bat_priv, false, NULL,
  833. unicast_tvlv_packet->src,
  834. unicast_tvlv_packet->dst,
  835. tvlv_buff, tvlv_buff_len);
  836. if (ret != NET_RX_SUCCESS)
  837. ret = batadv_route_unicast_packet(skb, recv_if);
  838. return ret;
  839. }
  840. /**
  841. * batadv_recv_frag_packet - process received fragment
  842. * @skb: the received fragment
  843. * @recv_if: interface that the skb is received on
  844. *
  845. * This function does one of the three following things: 1) Forward fragment, if
  846. * the assembled packet will exceed our MTU; 2) Buffer fragment, if we till
  847. * lack further fragments; 3) Merge fragments, if we have all needed parts.
  848. *
  849. * Return NET_RX_DROP if the skb is not consumed, NET_RX_SUCCESS otherwise.
  850. */
  851. int batadv_recv_frag_packet(struct sk_buff *skb,
  852. struct batadv_hard_iface *recv_if)
  853. {
  854. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  855. struct batadv_orig_node *orig_node_src = NULL;
  856. struct batadv_frag_packet *frag_packet;
  857. int ret = NET_RX_DROP;
  858. if (batadv_check_unicast_packet(bat_priv, skb,
  859. sizeof(*frag_packet)) < 0)
  860. goto out;
  861. frag_packet = (struct batadv_frag_packet *)skb->data;
  862. orig_node_src = batadv_orig_hash_find(bat_priv, frag_packet->orig);
  863. if (!orig_node_src)
  864. goto out;
  865. /* Route the fragment if it is not for us and too big to be merged. */
  866. if (!batadv_is_my_mac(bat_priv, frag_packet->dest) &&
  867. batadv_frag_skb_fwd(skb, recv_if, orig_node_src)) {
  868. ret = NET_RX_SUCCESS;
  869. goto out;
  870. }
  871. batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_RX);
  872. batadv_add_counter(bat_priv, BATADV_CNT_FRAG_RX_BYTES, skb->len);
  873. /* Add fragment to buffer and merge if possible. */
  874. if (!batadv_frag_skb_buffer(&skb, orig_node_src))
  875. goto out;
  876. /* Deliver merged packet to the appropriate handler, if it was
  877. * merged
  878. */
  879. if (skb)
  880. batadv_batman_skb_recv(skb, recv_if->net_dev,
  881. &recv_if->batman_adv_ptype, NULL);
  882. ret = NET_RX_SUCCESS;
  883. out:
  884. if (orig_node_src)
  885. batadv_orig_node_free_ref(orig_node_src);
  886. return ret;
  887. }
  888. int batadv_recv_bcast_packet(struct sk_buff *skb,
  889. struct batadv_hard_iface *recv_if)
  890. {
  891. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  892. struct batadv_orig_node *orig_node = NULL;
  893. struct batadv_bcast_packet *bcast_packet;
  894. struct ethhdr *ethhdr;
  895. int hdr_size = sizeof(*bcast_packet);
  896. int ret = NET_RX_DROP;
  897. int32_t seq_diff;
  898. /* drop packet if it has not necessary minimum size */
  899. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  900. goto out;
  901. ethhdr = eth_hdr(skb);
  902. /* packet with broadcast indication but unicast recipient */
  903. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  904. goto out;
  905. /* packet with broadcast sender address */
  906. if (is_broadcast_ether_addr(ethhdr->h_source))
  907. goto out;
  908. /* ignore broadcasts sent by myself */
  909. if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
  910. goto out;
  911. bcast_packet = (struct batadv_bcast_packet *)skb->data;
  912. /* ignore broadcasts originated by myself */
  913. if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
  914. goto out;
  915. if (bcast_packet->header.ttl < 2)
  916. goto out;
  917. orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
  918. if (!orig_node)
  919. goto out;
  920. spin_lock_bh(&orig_node->bcast_seqno_lock);
  921. /* check whether the packet is a duplicate */
  922. if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
  923. ntohl(bcast_packet->seqno)))
  924. goto spin_unlock;
  925. seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
  926. /* check whether the packet is old and the host just restarted. */
  927. if (batadv_window_protected(bat_priv, seq_diff,
  928. &orig_node->bcast_seqno_reset))
  929. goto spin_unlock;
  930. /* mark broadcast in flood history, update window position
  931. * if required.
  932. */
  933. if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
  934. orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
  935. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  936. /* check whether this has been sent by another originator before */
  937. if (batadv_bla_check_bcast_duplist(bat_priv, skb))
  938. goto out;
  939. batadv_skb_set_priority(skb, sizeof(struct batadv_bcast_packet));
  940. /* rebroadcast packet */
  941. batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
  942. /* don't hand the broadcast up if it is from an originator
  943. * from the same backbone.
  944. */
  945. if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
  946. goto out;
  947. if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
  948. goto rx_success;
  949. if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
  950. goto rx_success;
  951. /* broadcast for me */
  952. batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
  953. orig_node);
  954. rx_success:
  955. ret = NET_RX_SUCCESS;
  956. goto out;
  957. spin_unlock:
  958. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  959. out:
  960. if (orig_node)
  961. batadv_orig_node_free_ref(orig_node);
  962. return ret;
  963. }