routing.c 33 KB

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