routing.c 31 KB

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