/net/batman-adv/routing.c

http://github.com/mirrors/linux · C · 1288 lines · 762 code · 216 blank · 310 comment · 158 complexity · 64bcbc8747b8668c4d5301774fe8821e MD5 · raw file

  1. // SPDX-License-Identifier: GPL-2.0
  2. /* Copyright (C) 2007-2020 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner, Simon Wunderlich
  5. */
  6. #include "routing.h"
  7. #include "main.h"
  8. #include <linux/atomic.h>
  9. #include <linux/byteorder/generic.h>
  10. #include <linux/compiler.h>
  11. #include <linux/errno.h>
  12. #include <linux/etherdevice.h>
  13. #include <linux/if_ether.h>
  14. #include <linux/jiffies.h>
  15. #include <linux/kref.h>
  16. #include <linux/netdevice.h>
  17. #include <linux/printk.h>
  18. #include <linux/rculist.h>
  19. #include <linux/rcupdate.h>
  20. #include <linux/skbuff.h>
  21. #include <linux/spinlock.h>
  22. #include <linux/stddef.h>
  23. #include <uapi/linux/batadv_packet.h>
  24. #include "bitarray.h"
  25. #include "bridge_loop_avoidance.h"
  26. #include "distributed-arp-table.h"
  27. #include "fragmentation.h"
  28. #include "hard-interface.h"
  29. #include "icmp_socket.h"
  30. #include "log.h"
  31. #include "network-coding.h"
  32. #include "originator.h"
  33. #include "send.h"
  34. #include "soft-interface.h"
  35. #include "tp_meter.h"
  36. #include "translation-table.h"
  37. #include "tvlv.h"
  38. static int batadv_route_unicast_packet(struct sk_buff *skb,
  39. struct batadv_hard_iface *recv_if);
  40. /**
  41. * _batadv_update_route() - set the router for this originator
  42. * @bat_priv: the bat priv with all the soft interface information
  43. * @orig_node: orig node which is to be configured
  44. * @recv_if: the receive interface for which this route is set
  45. * @neigh_node: neighbor which should be the next router
  46. *
  47. * This function does not perform any error checks
  48. */
  49. static void _batadv_update_route(struct batadv_priv *bat_priv,
  50. struct batadv_orig_node *orig_node,
  51. struct batadv_hard_iface *recv_if,
  52. struct batadv_neigh_node *neigh_node)
  53. {
  54. struct batadv_orig_ifinfo *orig_ifinfo;
  55. struct batadv_neigh_node *curr_router;
  56. orig_ifinfo = batadv_orig_ifinfo_get(orig_node, recv_if);
  57. if (!orig_ifinfo)
  58. return;
  59. spin_lock_bh(&orig_node->neigh_list_lock);
  60. /* curr_router used earlier may not be the current orig_ifinfo->router
  61. * anymore because it was dereferenced outside of the neigh_list_lock
  62. * protected region. After the new best neighbor has replace the current
  63. * best neighbor the reference counter needs to decrease. Consequently,
  64. * the code needs to ensure the curr_router variable contains a pointer
  65. * to the replaced best neighbor.
  66. */
  67. curr_router = rcu_dereference_protected(orig_ifinfo->router, true);
  68. /* increase refcount of new best neighbor */
  69. if (neigh_node)
  70. kref_get(&neigh_node->refcount);
  71. rcu_assign_pointer(orig_ifinfo->router, neigh_node);
  72. spin_unlock_bh(&orig_node->neigh_list_lock);
  73. batadv_orig_ifinfo_put(orig_ifinfo);
  74. /* route deleted */
  75. if (curr_router && !neigh_node) {
  76. batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
  77. "Deleting route towards: %pM\n", orig_node->orig);
  78. batadv_tt_global_del_orig(bat_priv, orig_node, -1,
  79. "Deleted route towards originator");
  80. /* route added */
  81. } else if (!curr_router && neigh_node) {
  82. batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
  83. "Adding route towards: %pM (via %pM)\n",
  84. orig_node->orig, neigh_node->addr);
  85. /* route changed */
  86. } else if (neigh_node && curr_router) {
  87. batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
  88. "Changing route towards: %pM (now via %pM - was via %pM)\n",
  89. orig_node->orig, neigh_node->addr,
  90. curr_router->addr);
  91. }
  92. /* decrease refcount of previous best neighbor */
  93. if (curr_router)
  94. batadv_neigh_node_put(curr_router);
  95. }
  96. /**
  97. * batadv_update_route() - set the router for this originator
  98. * @bat_priv: the bat priv with all the soft interface information
  99. * @orig_node: orig node which is to be configured
  100. * @recv_if: the receive interface for which this route is set
  101. * @neigh_node: neighbor which should be the next router
  102. */
  103. void batadv_update_route(struct batadv_priv *bat_priv,
  104. struct batadv_orig_node *orig_node,
  105. struct batadv_hard_iface *recv_if,
  106. struct batadv_neigh_node *neigh_node)
  107. {
  108. struct batadv_neigh_node *router = NULL;
  109. if (!orig_node)
  110. goto out;
  111. router = batadv_orig_router_get(orig_node, recv_if);
  112. if (router != neigh_node)
  113. _batadv_update_route(bat_priv, orig_node, recv_if, neigh_node);
  114. out:
  115. if (router)
  116. batadv_neigh_node_put(router);
  117. }
  118. /**
  119. * batadv_window_protected() - checks whether the host restarted and is in the
  120. * protection time.
  121. * @bat_priv: the bat priv with all the soft interface information
  122. * @seq_num_diff: difference between the current/received sequence number and
  123. * the last sequence number
  124. * @seq_old_max_diff: maximum age of sequence number not considered as restart
  125. * @last_reset: jiffies timestamp of the last reset, will be updated when reset
  126. * is detected
  127. * @protection_started: is set to true if the protection window was started,
  128. * doesn't change otherwise.
  129. *
  130. * Return:
  131. * false if the packet is to be accepted.
  132. * true if the packet is to be ignored.
  133. */
  134. bool batadv_window_protected(struct batadv_priv *bat_priv, s32 seq_num_diff,
  135. s32 seq_old_max_diff, unsigned long *last_reset,
  136. bool *protection_started)
  137. {
  138. if (seq_num_diff <= -seq_old_max_diff ||
  139. seq_num_diff >= BATADV_EXPECTED_SEQNO_RANGE) {
  140. if (!batadv_has_timed_out(*last_reset,
  141. BATADV_RESET_PROTECTION_MS))
  142. return true;
  143. *last_reset = jiffies;
  144. if (protection_started)
  145. *protection_started = true;
  146. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  147. "old packet received, start protection\n");
  148. }
  149. return false;
  150. }
  151. /**
  152. * batadv_check_management_packet() - Check preconditions for management packets
  153. * @skb: incoming packet buffer
  154. * @hard_iface: incoming hard interface
  155. * @header_len: minimal header length of packet type
  156. *
  157. * Return: true when management preconditions are met, false otherwise
  158. */
  159. bool batadv_check_management_packet(struct sk_buff *skb,
  160. struct batadv_hard_iface *hard_iface,
  161. int header_len)
  162. {
  163. struct ethhdr *ethhdr;
  164. /* drop packet if it has not necessary minimum size */
  165. if (unlikely(!pskb_may_pull(skb, header_len)))
  166. return false;
  167. ethhdr = eth_hdr(skb);
  168. /* packet with broadcast indication but unicast recipient */
  169. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  170. return false;
  171. /* packet with invalid sender address */
  172. if (!is_valid_ether_addr(ethhdr->h_source))
  173. return false;
  174. /* create a copy of the skb, if needed, to modify it. */
  175. if (skb_cow(skb, 0) < 0)
  176. return false;
  177. /* keep skb linear */
  178. if (skb_linearize(skb) < 0)
  179. return false;
  180. return true;
  181. }
  182. /**
  183. * batadv_recv_my_icmp_packet() - receive an icmp packet locally
  184. * @bat_priv: the bat priv with all the soft interface information
  185. * @skb: icmp packet to process
  186. *
  187. * Return: NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
  188. * otherwise.
  189. */
  190. static int batadv_recv_my_icmp_packet(struct batadv_priv *bat_priv,
  191. struct sk_buff *skb)
  192. {
  193. struct batadv_hard_iface *primary_if = NULL;
  194. struct batadv_orig_node *orig_node = NULL;
  195. struct batadv_icmp_header *icmph;
  196. int res, ret = NET_RX_DROP;
  197. icmph = (struct batadv_icmp_header *)skb->data;
  198. switch (icmph->msg_type) {
  199. case BATADV_ECHO_REPLY:
  200. case BATADV_DESTINATION_UNREACHABLE:
  201. case BATADV_TTL_EXCEEDED:
  202. /* receive the packet */
  203. if (skb_linearize(skb) < 0)
  204. break;
  205. batadv_socket_receive_packet(icmph, skb->len);
  206. break;
  207. case BATADV_ECHO_REQUEST:
  208. /* answer echo request (ping) */
  209. primary_if = batadv_primary_if_get_selected(bat_priv);
  210. if (!primary_if)
  211. goto out;
  212. /* get routing information */
  213. orig_node = batadv_orig_hash_find(bat_priv, icmph->orig);
  214. if (!orig_node)
  215. goto out;
  216. /* create a copy of the skb, if needed, to modify it. */
  217. if (skb_cow(skb, ETH_HLEN) < 0)
  218. goto out;
  219. icmph = (struct batadv_icmp_header *)skb->data;
  220. ether_addr_copy(icmph->dst, icmph->orig);
  221. ether_addr_copy(icmph->orig, primary_if->net_dev->dev_addr);
  222. icmph->msg_type = BATADV_ECHO_REPLY;
  223. icmph->ttl = BATADV_TTL;
  224. res = batadv_send_skb_to_orig(skb, orig_node, NULL);
  225. if (res == NET_XMIT_SUCCESS)
  226. ret = NET_RX_SUCCESS;
  227. /* skb was consumed */
  228. skb = NULL;
  229. break;
  230. case BATADV_TP:
  231. if (!pskb_may_pull(skb, sizeof(struct batadv_icmp_tp_packet)))
  232. goto out;
  233. batadv_tp_meter_recv(bat_priv, skb);
  234. ret = NET_RX_SUCCESS;
  235. /* skb was consumed */
  236. skb = NULL;
  237. goto out;
  238. default:
  239. /* drop unknown type */
  240. goto out;
  241. }
  242. out:
  243. if (primary_if)
  244. batadv_hardif_put(primary_if);
  245. if (orig_node)
  246. batadv_orig_node_put(orig_node);
  247. kfree_skb(skb);
  248. return ret;
  249. }
  250. static int batadv_recv_icmp_ttl_exceeded(struct batadv_priv *bat_priv,
  251. struct sk_buff *skb)
  252. {
  253. struct batadv_hard_iface *primary_if = NULL;
  254. struct batadv_orig_node *orig_node = NULL;
  255. struct batadv_icmp_packet *icmp_packet;
  256. int res, ret = NET_RX_DROP;
  257. icmp_packet = (struct batadv_icmp_packet *)skb->data;
  258. /* send TTL exceeded if packet is an echo request (traceroute) */
  259. if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
  260. pr_debug("Warning - can't forward icmp packet from %pM to %pM: ttl exceeded\n",
  261. icmp_packet->orig, icmp_packet->dst);
  262. goto out;
  263. }
  264. primary_if = batadv_primary_if_get_selected(bat_priv);
  265. if (!primary_if)
  266. goto out;
  267. /* get routing information */
  268. orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->orig);
  269. if (!orig_node)
  270. goto out;
  271. /* create a copy of the skb, if needed, to modify it. */
  272. if (skb_cow(skb, ETH_HLEN) < 0)
  273. goto out;
  274. icmp_packet = (struct batadv_icmp_packet *)skb->data;
  275. ether_addr_copy(icmp_packet->dst, icmp_packet->orig);
  276. ether_addr_copy(icmp_packet->orig, primary_if->net_dev->dev_addr);
  277. icmp_packet->msg_type = BATADV_TTL_EXCEEDED;
  278. icmp_packet->ttl = BATADV_TTL;
  279. res = batadv_send_skb_to_orig(skb, orig_node, NULL);
  280. if (res == NET_RX_SUCCESS)
  281. ret = NET_XMIT_SUCCESS;
  282. /* skb was consumed */
  283. skb = NULL;
  284. out:
  285. if (primary_if)
  286. batadv_hardif_put(primary_if);
  287. if (orig_node)
  288. batadv_orig_node_put(orig_node);
  289. kfree_skb(skb);
  290. return ret;
  291. }
  292. /**
  293. * batadv_recv_icmp_packet() - Process incoming icmp packet
  294. * @skb: incoming packet buffer
  295. * @recv_if: incoming hard interface
  296. *
  297. * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
  298. */
  299. int batadv_recv_icmp_packet(struct sk_buff *skb,
  300. struct batadv_hard_iface *recv_if)
  301. {
  302. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  303. struct batadv_icmp_header *icmph;
  304. struct batadv_icmp_packet_rr *icmp_packet_rr;
  305. struct ethhdr *ethhdr;
  306. struct batadv_orig_node *orig_node = NULL;
  307. int hdr_size = sizeof(struct batadv_icmp_header);
  308. int res, ret = NET_RX_DROP;
  309. /* drop packet if it has not necessary minimum size */
  310. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  311. goto free_skb;
  312. ethhdr = eth_hdr(skb);
  313. /* packet with unicast indication but non-unicast recipient */
  314. if (!is_valid_ether_addr(ethhdr->h_dest))
  315. goto free_skb;
  316. /* packet with broadcast/multicast sender address */
  317. if (is_multicast_ether_addr(ethhdr->h_source))
  318. goto free_skb;
  319. /* not for me */
  320. if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
  321. goto free_skb;
  322. icmph = (struct batadv_icmp_header *)skb->data;
  323. /* add record route information if not full */
  324. if ((icmph->msg_type == BATADV_ECHO_REPLY ||
  325. icmph->msg_type == BATADV_ECHO_REQUEST) &&
  326. skb->len >= sizeof(struct batadv_icmp_packet_rr)) {
  327. if (skb_linearize(skb) < 0)
  328. goto free_skb;
  329. /* create a copy of the skb, if needed, to modify it. */
  330. if (skb_cow(skb, ETH_HLEN) < 0)
  331. goto free_skb;
  332. ethhdr = eth_hdr(skb);
  333. icmph = (struct batadv_icmp_header *)skb->data;
  334. icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmph;
  335. if (icmp_packet_rr->rr_cur >= BATADV_RR_LEN)
  336. goto free_skb;
  337. ether_addr_copy(icmp_packet_rr->rr[icmp_packet_rr->rr_cur],
  338. ethhdr->h_dest);
  339. icmp_packet_rr->rr_cur++;
  340. }
  341. /* packet for me */
  342. if (batadv_is_my_mac(bat_priv, icmph->dst))
  343. return batadv_recv_my_icmp_packet(bat_priv, skb);
  344. /* TTL exceeded */
  345. if (icmph->ttl < 2)
  346. return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
  347. /* get routing information */
  348. orig_node = batadv_orig_hash_find(bat_priv, icmph->dst);
  349. if (!orig_node)
  350. goto free_skb;
  351. /* create a copy of the skb, if needed, to modify it. */
  352. if (skb_cow(skb, ETH_HLEN) < 0)
  353. goto put_orig_node;
  354. icmph = (struct batadv_icmp_header *)skb->data;
  355. /* decrement ttl */
  356. icmph->ttl--;
  357. /* route it */
  358. res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
  359. if (res == NET_XMIT_SUCCESS)
  360. ret = NET_RX_SUCCESS;
  361. /* skb was consumed */
  362. skb = NULL;
  363. put_orig_node:
  364. if (orig_node)
  365. batadv_orig_node_put(orig_node);
  366. free_skb:
  367. kfree_skb(skb);
  368. return ret;
  369. }
  370. /**
  371. * batadv_check_unicast_packet() - Check for malformed unicast packets
  372. * @bat_priv: the bat priv with all the soft interface information
  373. * @skb: packet to check
  374. * @hdr_size: size of header to pull
  375. *
  376. * Check for short header and bad addresses in given packet.
  377. *
  378. * Return: negative value when check fails and 0 otherwise. The negative value
  379. * depends on the reason: -ENODATA for bad header, -EBADR for broadcast
  380. * destination or source, and -EREMOTE for non-local (other host) destination.
  381. */
  382. static int batadv_check_unicast_packet(struct batadv_priv *bat_priv,
  383. struct sk_buff *skb, int hdr_size)
  384. {
  385. struct ethhdr *ethhdr;
  386. /* drop packet if it has not necessary minimum size */
  387. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  388. return -ENODATA;
  389. ethhdr = eth_hdr(skb);
  390. /* packet with unicast indication but non-unicast recipient */
  391. if (!is_valid_ether_addr(ethhdr->h_dest))
  392. return -EBADR;
  393. /* packet with broadcast/multicast sender address */
  394. if (is_multicast_ether_addr(ethhdr->h_source))
  395. return -EBADR;
  396. /* not for me */
  397. if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
  398. return -EREMOTE;
  399. return 0;
  400. }
  401. /**
  402. * batadv_last_bonding_get() - Get last_bonding_candidate of orig_node
  403. * @orig_node: originator node whose last bonding candidate should be retrieved
  404. *
  405. * Return: last bonding candidate of router or NULL if not found
  406. *
  407. * The object is returned with refcounter increased by 1.
  408. */
  409. static struct batadv_orig_ifinfo *
  410. batadv_last_bonding_get(struct batadv_orig_node *orig_node)
  411. {
  412. struct batadv_orig_ifinfo *last_bonding_candidate;
  413. spin_lock_bh(&orig_node->neigh_list_lock);
  414. last_bonding_candidate = orig_node->last_bonding_candidate;
  415. if (last_bonding_candidate)
  416. kref_get(&last_bonding_candidate->refcount);
  417. spin_unlock_bh(&orig_node->neigh_list_lock);
  418. return last_bonding_candidate;
  419. }
  420. /**
  421. * batadv_last_bonding_replace() - Replace last_bonding_candidate of orig_node
  422. * @orig_node: originator node whose bonding candidates should be replaced
  423. * @new_candidate: new bonding candidate or NULL
  424. */
  425. static void
  426. batadv_last_bonding_replace(struct batadv_orig_node *orig_node,
  427. struct batadv_orig_ifinfo *new_candidate)
  428. {
  429. struct batadv_orig_ifinfo *old_candidate;
  430. spin_lock_bh(&orig_node->neigh_list_lock);
  431. old_candidate = orig_node->last_bonding_candidate;
  432. if (new_candidate)
  433. kref_get(&new_candidate->refcount);
  434. orig_node->last_bonding_candidate = new_candidate;
  435. spin_unlock_bh(&orig_node->neigh_list_lock);
  436. if (old_candidate)
  437. batadv_orig_ifinfo_put(old_candidate);
  438. }
  439. /**
  440. * batadv_find_router() - find a suitable router for this originator
  441. * @bat_priv: the bat priv with all the soft interface information
  442. * @orig_node: the destination node
  443. * @recv_if: pointer to interface this packet was received on
  444. *
  445. * Return: the router which should be used for this orig_node on
  446. * this interface, or NULL if not available.
  447. */
  448. struct batadv_neigh_node *
  449. batadv_find_router(struct batadv_priv *bat_priv,
  450. struct batadv_orig_node *orig_node,
  451. struct batadv_hard_iface *recv_if)
  452. {
  453. struct batadv_algo_ops *bao = bat_priv->algo_ops;
  454. struct batadv_neigh_node *first_candidate_router = NULL;
  455. struct batadv_neigh_node *next_candidate_router = NULL;
  456. struct batadv_neigh_node *router, *cand_router = NULL;
  457. struct batadv_neigh_node *last_cand_router = NULL;
  458. struct batadv_orig_ifinfo *cand, *first_candidate = NULL;
  459. struct batadv_orig_ifinfo *next_candidate = NULL;
  460. struct batadv_orig_ifinfo *last_candidate;
  461. bool last_candidate_found = false;
  462. if (!orig_node)
  463. return NULL;
  464. router = batadv_orig_router_get(orig_node, recv_if);
  465. if (!router)
  466. return router;
  467. /* only consider bonding for recv_if == BATADV_IF_DEFAULT (first hop)
  468. * and if activated.
  469. */
  470. if (!(recv_if == BATADV_IF_DEFAULT && atomic_read(&bat_priv->bonding)))
  471. return router;
  472. /* bonding: loop through the list of possible routers found
  473. * for the various outgoing interfaces and find a candidate after
  474. * the last chosen bonding candidate (next_candidate). If no such
  475. * router is found, use the first candidate found (the previously
  476. * chosen bonding candidate might have been the last one in the list).
  477. * If this can't be found either, return the previously chosen
  478. * router - obviously there are no other candidates.
  479. */
  480. rcu_read_lock();
  481. last_candidate = batadv_last_bonding_get(orig_node);
  482. if (last_candidate)
  483. last_cand_router = rcu_dereference(last_candidate->router);
  484. hlist_for_each_entry_rcu(cand, &orig_node->ifinfo_list, list) {
  485. /* acquire some structures and references ... */
  486. if (!kref_get_unless_zero(&cand->refcount))
  487. continue;
  488. cand_router = rcu_dereference(cand->router);
  489. if (!cand_router)
  490. goto next;
  491. if (!kref_get_unless_zero(&cand_router->refcount)) {
  492. cand_router = NULL;
  493. goto next;
  494. }
  495. /* alternative candidate should be good enough to be
  496. * considered
  497. */
  498. if (!bao->neigh.is_similar_or_better(cand_router,
  499. cand->if_outgoing, router,
  500. recv_if))
  501. goto next;
  502. /* don't use the same router twice */
  503. if (last_cand_router == cand_router)
  504. goto next;
  505. /* mark the first possible candidate */
  506. if (!first_candidate) {
  507. kref_get(&cand_router->refcount);
  508. kref_get(&cand->refcount);
  509. first_candidate = cand;
  510. first_candidate_router = cand_router;
  511. }
  512. /* check if the loop has already passed the previously selected
  513. * candidate ... this function should select the next candidate
  514. * AFTER the previously used bonding candidate.
  515. */
  516. if (!last_candidate || last_candidate_found) {
  517. next_candidate = cand;
  518. next_candidate_router = cand_router;
  519. break;
  520. }
  521. if (last_candidate == cand)
  522. last_candidate_found = true;
  523. next:
  524. /* free references */
  525. if (cand_router) {
  526. batadv_neigh_node_put(cand_router);
  527. cand_router = NULL;
  528. }
  529. batadv_orig_ifinfo_put(cand);
  530. }
  531. rcu_read_unlock();
  532. /* After finding candidates, handle the three cases:
  533. * 1) there is a next candidate, use that
  534. * 2) there is no next candidate, use the first of the list
  535. * 3) there is no candidate at all, return the default router
  536. */
  537. if (next_candidate) {
  538. batadv_neigh_node_put(router);
  539. kref_get(&next_candidate_router->refcount);
  540. router = next_candidate_router;
  541. batadv_last_bonding_replace(orig_node, next_candidate);
  542. } else if (first_candidate) {
  543. batadv_neigh_node_put(router);
  544. kref_get(&first_candidate_router->refcount);
  545. router = first_candidate_router;
  546. batadv_last_bonding_replace(orig_node, first_candidate);
  547. } else {
  548. batadv_last_bonding_replace(orig_node, NULL);
  549. }
  550. /* cleanup of candidates */
  551. if (first_candidate) {
  552. batadv_neigh_node_put(first_candidate_router);
  553. batadv_orig_ifinfo_put(first_candidate);
  554. }
  555. if (next_candidate) {
  556. batadv_neigh_node_put(next_candidate_router);
  557. batadv_orig_ifinfo_put(next_candidate);
  558. }
  559. if (last_candidate)
  560. batadv_orig_ifinfo_put(last_candidate);
  561. return router;
  562. }
  563. static int batadv_route_unicast_packet(struct sk_buff *skb,
  564. struct batadv_hard_iface *recv_if)
  565. {
  566. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  567. struct batadv_orig_node *orig_node = NULL;
  568. struct batadv_unicast_packet *unicast_packet;
  569. struct ethhdr *ethhdr = eth_hdr(skb);
  570. int res, hdr_len, ret = NET_RX_DROP;
  571. unsigned int len;
  572. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  573. /* TTL exceeded */
  574. if (unicast_packet->ttl < 2) {
  575. pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
  576. ethhdr->h_source, unicast_packet->dest);
  577. goto free_skb;
  578. }
  579. /* get routing information */
  580. orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
  581. if (!orig_node)
  582. goto free_skb;
  583. /* create a copy of the skb, if needed, to modify it. */
  584. if (skb_cow(skb, ETH_HLEN) < 0)
  585. goto put_orig_node;
  586. /* decrement ttl */
  587. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  588. unicast_packet->ttl--;
  589. switch (unicast_packet->packet_type) {
  590. case BATADV_UNICAST_4ADDR:
  591. hdr_len = sizeof(struct batadv_unicast_4addr_packet);
  592. break;
  593. case BATADV_UNICAST:
  594. hdr_len = sizeof(struct batadv_unicast_packet);
  595. break;
  596. default:
  597. /* other packet types not supported - yet */
  598. hdr_len = -1;
  599. break;
  600. }
  601. if (hdr_len > 0)
  602. batadv_skb_set_priority(skb, hdr_len);
  603. len = skb->len;
  604. res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
  605. /* translate transmit result into receive result */
  606. if (res == NET_XMIT_SUCCESS) {
  607. ret = NET_RX_SUCCESS;
  608. /* skb was transmitted and consumed */
  609. batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
  610. batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
  611. len + ETH_HLEN);
  612. }
  613. /* skb was consumed */
  614. skb = NULL;
  615. put_orig_node:
  616. batadv_orig_node_put(orig_node);
  617. free_skb:
  618. kfree_skb(skb);
  619. return ret;
  620. }
  621. /**
  622. * batadv_reroute_unicast_packet() - update the unicast header for re-routing
  623. * @bat_priv: the bat priv with all the soft interface information
  624. * @skb: unicast packet to process
  625. * @unicast_packet: the unicast header to be updated
  626. * @dst_addr: the payload destination
  627. * @vid: VLAN identifier
  628. *
  629. * Search the translation table for dst_addr and update the unicast header with
  630. * the new corresponding information (originator address where the destination
  631. * client currently is and its known TTVN)
  632. *
  633. * Return: true if the packet header has been updated, false otherwise
  634. */
  635. static bool
  636. batadv_reroute_unicast_packet(struct batadv_priv *bat_priv, struct sk_buff *skb,
  637. struct batadv_unicast_packet *unicast_packet,
  638. u8 *dst_addr, unsigned short vid)
  639. {
  640. struct batadv_orig_node *orig_node = NULL;
  641. struct batadv_hard_iface *primary_if = NULL;
  642. bool ret = false;
  643. u8 *orig_addr, orig_ttvn;
  644. if (batadv_is_my_client(bat_priv, dst_addr, vid)) {
  645. primary_if = batadv_primary_if_get_selected(bat_priv);
  646. if (!primary_if)
  647. goto out;
  648. orig_addr = primary_if->net_dev->dev_addr;
  649. orig_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
  650. } else {
  651. orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr,
  652. vid);
  653. if (!orig_node)
  654. goto out;
  655. if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
  656. goto out;
  657. orig_addr = orig_node->orig;
  658. orig_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
  659. }
  660. /* update the packet header */
  661. skb_postpull_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
  662. ether_addr_copy(unicast_packet->dest, orig_addr);
  663. unicast_packet->ttvn = orig_ttvn;
  664. skb_postpush_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
  665. ret = true;
  666. out:
  667. if (primary_if)
  668. batadv_hardif_put(primary_if);
  669. if (orig_node)
  670. batadv_orig_node_put(orig_node);
  671. return ret;
  672. }
  673. static bool batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
  674. struct sk_buff *skb, int hdr_len)
  675. {
  676. struct batadv_unicast_packet *unicast_packet;
  677. struct batadv_hard_iface *primary_if;
  678. struct batadv_orig_node *orig_node;
  679. u8 curr_ttvn, old_ttvn;
  680. struct ethhdr *ethhdr;
  681. unsigned short vid;
  682. int is_old_ttvn;
  683. /* check if there is enough data before accessing it */
  684. if (!pskb_may_pull(skb, hdr_len + ETH_HLEN))
  685. return false;
  686. /* create a copy of the skb (in case of for re-routing) to modify it. */
  687. if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
  688. return false;
  689. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  690. vid = batadv_get_vid(skb, hdr_len);
  691. ethhdr = (struct ethhdr *)(skb->data + hdr_len);
  692. /* check if the destination client was served by this node and it is now
  693. * roaming. In this case, it means that the node has got a ROAM_ADV
  694. * message and that it knows the new destination in the mesh to re-route
  695. * the packet to
  696. */
  697. if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest, vid)) {
  698. if (batadv_reroute_unicast_packet(bat_priv, skb, unicast_packet,
  699. ethhdr->h_dest, vid))
  700. batadv_dbg_ratelimited(BATADV_DBG_TT,
  701. bat_priv,
  702. "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
  703. unicast_packet->dest,
  704. ethhdr->h_dest);
  705. /* at this point the mesh destination should have been
  706. * substituted with the originator address found in the global
  707. * table. If not, let the packet go untouched anyway because
  708. * there is nothing the node can do
  709. */
  710. return true;
  711. }
  712. /* retrieve the TTVN known by this node for the packet destination. This
  713. * value is used later to check if the node which sent (or re-routed
  714. * last time) the packet had an updated information or not
  715. */
  716. curr_ttvn = (u8)atomic_read(&bat_priv->tt.vn);
  717. if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
  718. orig_node = batadv_orig_hash_find(bat_priv,
  719. unicast_packet->dest);
  720. /* if it is not possible to find the orig_node representing the
  721. * destination, the packet can immediately be dropped as it will
  722. * not be possible to deliver it
  723. */
  724. if (!orig_node)
  725. return false;
  726. curr_ttvn = (u8)atomic_read(&orig_node->last_ttvn);
  727. batadv_orig_node_put(orig_node);
  728. }
  729. /* check if the TTVN contained in the packet is fresher than what the
  730. * node knows
  731. */
  732. is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
  733. if (!is_old_ttvn)
  734. return true;
  735. old_ttvn = unicast_packet->ttvn;
  736. /* the packet was forged based on outdated network information. Its
  737. * destination can possibly be updated and forwarded towards the new
  738. * target host
  739. */
  740. if (batadv_reroute_unicast_packet(bat_priv, skb, unicast_packet,
  741. ethhdr->h_dest, vid)) {
  742. batadv_dbg_ratelimited(BATADV_DBG_TT, bat_priv,
  743. "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
  744. unicast_packet->dest, ethhdr->h_dest,
  745. old_ttvn, curr_ttvn);
  746. return true;
  747. }
  748. /* the packet has not been re-routed: either the destination is
  749. * currently served by this node or there is no destination at all and
  750. * it is possible to drop the packet
  751. */
  752. if (!batadv_is_my_client(bat_priv, ethhdr->h_dest, vid))
  753. return false;
  754. /* update the header in order to let the packet be delivered to this
  755. * node's soft interface
  756. */
  757. primary_if = batadv_primary_if_get_selected(bat_priv);
  758. if (!primary_if)
  759. return false;
  760. /* update the packet header */
  761. skb_postpull_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
  762. ether_addr_copy(unicast_packet->dest, primary_if->net_dev->dev_addr);
  763. unicast_packet->ttvn = curr_ttvn;
  764. skb_postpush_rcsum(skb, unicast_packet, sizeof(*unicast_packet));
  765. batadv_hardif_put(primary_if);
  766. return true;
  767. }
  768. /**
  769. * batadv_recv_unhandled_unicast_packet() - receive and process packets which
  770. * are in the unicast number space but not yet known to the implementation
  771. * @skb: unicast tvlv packet to process
  772. * @recv_if: pointer to interface this packet was received on
  773. *
  774. * Return: NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
  775. * otherwise.
  776. */
  777. int batadv_recv_unhandled_unicast_packet(struct sk_buff *skb,
  778. struct batadv_hard_iface *recv_if)
  779. {
  780. struct batadv_unicast_packet *unicast_packet;
  781. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  782. int check, hdr_size = sizeof(*unicast_packet);
  783. check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
  784. if (check < 0)
  785. goto free_skb;
  786. /* we don't know about this type, drop it. */
  787. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  788. if (batadv_is_my_mac(bat_priv, unicast_packet->dest))
  789. goto free_skb;
  790. return batadv_route_unicast_packet(skb, recv_if);
  791. free_skb:
  792. kfree_skb(skb);
  793. return NET_RX_DROP;
  794. }
  795. /**
  796. * batadv_recv_unicast_packet() - Process incoming unicast packet
  797. * @skb: incoming packet buffer
  798. * @recv_if: incoming hard interface
  799. *
  800. * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
  801. */
  802. int batadv_recv_unicast_packet(struct sk_buff *skb,
  803. struct batadv_hard_iface *recv_if)
  804. {
  805. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  806. struct batadv_unicast_packet *unicast_packet;
  807. struct batadv_unicast_4addr_packet *unicast_4addr_packet;
  808. u8 *orig_addr, *orig_addr_gw;
  809. struct batadv_orig_node *orig_node = NULL, *orig_node_gw = NULL;
  810. int check, hdr_size = sizeof(*unicast_packet);
  811. enum batadv_subtype subtype;
  812. int ret = NET_RX_DROP;
  813. bool is4addr, is_gw;
  814. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  815. is4addr = unicast_packet->packet_type == BATADV_UNICAST_4ADDR;
  816. /* the caller function should have already pulled 2 bytes */
  817. if (is4addr)
  818. hdr_size = sizeof(*unicast_4addr_packet);
  819. /* function returns -EREMOTE for promiscuous packets */
  820. check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
  821. /* Even though the packet is not for us, we might save it to use for
  822. * decoding a later received coded packet
  823. */
  824. if (check == -EREMOTE)
  825. batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
  826. if (check < 0)
  827. goto free_skb;
  828. if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
  829. goto free_skb;
  830. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  831. /* packet for me */
  832. if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
  833. /* If this is a unicast packet from another backgone gw,
  834. * drop it.
  835. */
  836. orig_addr_gw = eth_hdr(skb)->h_source;
  837. orig_node_gw = batadv_orig_hash_find(bat_priv, orig_addr_gw);
  838. if (orig_node_gw) {
  839. is_gw = batadv_bla_is_backbone_gw(skb, orig_node_gw,
  840. hdr_size);
  841. batadv_orig_node_put(orig_node_gw);
  842. if (is_gw) {
  843. batadv_dbg(BATADV_DBG_BLA, bat_priv,
  844. "%s(): Dropped unicast pkt received from another backbone gw %pM.\n",
  845. __func__, orig_addr_gw);
  846. goto free_skb;
  847. }
  848. }
  849. if (is4addr) {
  850. unicast_4addr_packet =
  851. (struct batadv_unicast_4addr_packet *)skb->data;
  852. subtype = unicast_4addr_packet->subtype;
  853. batadv_dat_inc_counter(bat_priv, subtype);
  854. /* Only payload data should be considered for speedy
  855. * join. For example, DAT also uses unicast 4addr
  856. * types, but those packets should not be considered
  857. * for speedy join, since the clients do not actually
  858. * reside at the sending originator.
  859. */
  860. if (subtype == BATADV_P_DATA) {
  861. orig_addr = unicast_4addr_packet->src;
  862. orig_node = batadv_orig_hash_find(bat_priv,
  863. orig_addr);
  864. }
  865. }
  866. if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
  867. hdr_size))
  868. goto rx_success;
  869. if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
  870. hdr_size))
  871. goto rx_success;
  872. batadv_dat_snoop_incoming_dhcp_ack(bat_priv, skb, hdr_size);
  873. batadv_interface_rx(recv_if->soft_iface, skb, hdr_size,
  874. orig_node);
  875. rx_success:
  876. if (orig_node)
  877. batadv_orig_node_put(orig_node);
  878. return NET_RX_SUCCESS;
  879. }
  880. ret = batadv_route_unicast_packet(skb, recv_if);
  881. /* skb was consumed */
  882. skb = NULL;
  883. free_skb:
  884. kfree_skb(skb);
  885. return ret;
  886. }
  887. /**
  888. * batadv_recv_unicast_tvlv() - receive and process unicast tvlv packets
  889. * @skb: unicast tvlv packet to process
  890. * @recv_if: pointer to interface this packet was received on
  891. *
  892. * Return: NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
  893. * otherwise.
  894. */
  895. int batadv_recv_unicast_tvlv(struct sk_buff *skb,
  896. struct batadv_hard_iface *recv_if)
  897. {
  898. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  899. struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
  900. unsigned char *tvlv_buff;
  901. u16 tvlv_buff_len;
  902. int hdr_size = sizeof(*unicast_tvlv_packet);
  903. int ret = NET_RX_DROP;
  904. if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
  905. goto free_skb;
  906. /* the header is likely to be modified while forwarding */
  907. if (skb_cow(skb, hdr_size) < 0)
  908. goto free_skb;
  909. /* packet needs to be linearized to access the tvlv content */
  910. if (skb_linearize(skb) < 0)
  911. goto free_skb;
  912. unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)skb->data;
  913. tvlv_buff = (unsigned char *)(skb->data + hdr_size);
  914. tvlv_buff_len = ntohs(unicast_tvlv_packet->tvlv_len);
  915. if (tvlv_buff_len > skb->len - hdr_size)
  916. goto free_skb;
  917. ret = batadv_tvlv_containers_process(bat_priv, false, NULL,
  918. unicast_tvlv_packet->src,
  919. unicast_tvlv_packet->dst,
  920. tvlv_buff, tvlv_buff_len);
  921. if (ret != NET_RX_SUCCESS) {
  922. ret = batadv_route_unicast_packet(skb, recv_if);
  923. /* skb was consumed */
  924. skb = NULL;
  925. }
  926. free_skb:
  927. kfree_skb(skb);
  928. return ret;
  929. }
  930. /**
  931. * batadv_recv_frag_packet() - process received fragment
  932. * @skb: the received fragment
  933. * @recv_if: interface that the skb is received on
  934. *
  935. * This function does one of the three following things: 1) Forward fragment, if
  936. * the assembled packet will exceed our MTU; 2) Buffer fragment, if we till
  937. * lack further fragments; 3) Merge fragments, if we have all needed parts.
  938. *
  939. * Return: NET_RX_DROP if the skb is not consumed, NET_RX_SUCCESS otherwise.
  940. */
  941. int batadv_recv_frag_packet(struct sk_buff *skb,
  942. struct batadv_hard_iface *recv_if)
  943. {
  944. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  945. struct batadv_orig_node *orig_node_src = NULL;
  946. struct batadv_frag_packet *frag_packet;
  947. int ret = NET_RX_DROP;
  948. if (batadv_check_unicast_packet(bat_priv, skb,
  949. sizeof(*frag_packet)) < 0)
  950. goto free_skb;
  951. frag_packet = (struct batadv_frag_packet *)skb->data;
  952. orig_node_src = batadv_orig_hash_find(bat_priv, frag_packet->orig);
  953. if (!orig_node_src)
  954. goto free_skb;
  955. skb->priority = frag_packet->priority + 256;
  956. /* Route the fragment if it is not for us and too big to be merged. */
  957. if (!batadv_is_my_mac(bat_priv, frag_packet->dest) &&
  958. batadv_frag_skb_fwd(skb, recv_if, orig_node_src)) {
  959. /* skb was consumed */
  960. skb = NULL;
  961. ret = NET_RX_SUCCESS;
  962. goto put_orig_node;
  963. }
  964. batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_RX);
  965. batadv_add_counter(bat_priv, BATADV_CNT_FRAG_RX_BYTES, skb->len);
  966. /* Add fragment to buffer and merge if possible. */
  967. if (!batadv_frag_skb_buffer(&skb, orig_node_src))
  968. goto put_orig_node;
  969. /* Deliver merged packet to the appropriate handler, if it was
  970. * merged
  971. */
  972. if (skb) {
  973. batadv_batman_skb_recv(skb, recv_if->net_dev,
  974. &recv_if->batman_adv_ptype, NULL);
  975. /* skb was consumed */
  976. skb = NULL;
  977. }
  978. ret = NET_RX_SUCCESS;
  979. put_orig_node:
  980. batadv_orig_node_put(orig_node_src);
  981. free_skb:
  982. kfree_skb(skb);
  983. return ret;
  984. }
  985. /**
  986. * batadv_recv_bcast_packet() - Process incoming broadcast packet
  987. * @skb: incoming packet buffer
  988. * @recv_if: incoming hard interface
  989. *
  990. * Return: NET_RX_SUCCESS on success or NET_RX_DROP in case of failure
  991. */
  992. int batadv_recv_bcast_packet(struct sk_buff *skb,
  993. struct batadv_hard_iface *recv_if)
  994. {
  995. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  996. struct batadv_orig_node *orig_node = NULL;
  997. struct batadv_bcast_packet *bcast_packet;
  998. struct ethhdr *ethhdr;
  999. int hdr_size = sizeof(*bcast_packet);
  1000. int ret = NET_RX_DROP;
  1001. s32 seq_diff;
  1002. u32 seqno;
  1003. /* drop packet if it has not necessary minimum size */
  1004. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  1005. goto free_skb;
  1006. ethhdr = eth_hdr(skb);
  1007. /* packet with broadcast indication but unicast recipient */
  1008. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  1009. goto free_skb;
  1010. /* packet with broadcast/multicast sender address */
  1011. if (is_multicast_ether_addr(ethhdr->h_source))
  1012. goto free_skb;
  1013. /* ignore broadcasts sent by myself */
  1014. if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
  1015. goto free_skb;
  1016. bcast_packet = (struct batadv_bcast_packet *)skb->data;
  1017. /* ignore broadcasts originated by myself */
  1018. if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
  1019. goto free_skb;
  1020. if (bcast_packet->ttl < 2)
  1021. goto free_skb;
  1022. orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
  1023. if (!orig_node)
  1024. goto free_skb;
  1025. spin_lock_bh(&orig_node->bcast_seqno_lock);
  1026. seqno = ntohl(bcast_packet->seqno);
  1027. /* check whether the packet is a duplicate */
  1028. if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
  1029. seqno))
  1030. goto spin_unlock;
  1031. seq_diff = seqno - orig_node->last_bcast_seqno;
  1032. /* check whether the packet is old and the host just restarted. */
  1033. if (batadv_window_protected(bat_priv, seq_diff,
  1034. BATADV_BCAST_MAX_AGE,
  1035. &orig_node->bcast_seqno_reset, NULL))
  1036. goto spin_unlock;
  1037. /* mark broadcast in flood history, update window position
  1038. * if required.
  1039. */
  1040. if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
  1041. orig_node->last_bcast_seqno = seqno;
  1042. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  1043. /* check whether this has been sent by another originator before */
  1044. if (batadv_bla_check_bcast_duplist(bat_priv, skb))
  1045. goto free_skb;
  1046. batadv_skb_set_priority(skb, sizeof(struct batadv_bcast_packet));
  1047. /* rebroadcast packet */
  1048. batadv_add_bcast_packet_to_list(bat_priv, skb, 1, false);
  1049. /* don't hand the broadcast up if it is from an originator
  1050. * from the same backbone.
  1051. */
  1052. if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
  1053. goto free_skb;
  1054. if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
  1055. goto rx_success;
  1056. if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
  1057. goto rx_success;
  1058. batadv_dat_snoop_incoming_dhcp_ack(bat_priv, skb, hdr_size);
  1059. /* broadcast for me */
  1060. batadv_interface_rx(recv_if->soft_iface, skb, hdr_size, orig_node);
  1061. rx_success:
  1062. ret = NET_RX_SUCCESS;
  1063. goto out;
  1064. spin_unlock:
  1065. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  1066. free_skb:
  1067. kfree_skb(skb);
  1068. out:
  1069. if (orig_node)
  1070. batadv_orig_node_put(orig_node);
  1071. return ret;
  1072. }