routing.c 35 KB

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