routing.c 33 KB

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