routing.c 33 KB

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