routing.c 32 KB

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