routing.c 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085
  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. /* only consider bonding for recv_if == BATADV_IF_DEFAULT (first hop)
  368. * and if activated.
  369. */
  370. if (recv_if == BATADV_IF_DEFAULT || !atomic_read(&bat_priv->bonding) ||
  371. !router)
  372. return router;
  373. /* bonding: loop through the list of possible routers found
  374. * for the various outgoing interfaces and find a candidate after
  375. * the last chosen bonding candidate (next_candidate). If no such
  376. * router is found, use the first candidate found (the previously
  377. * chosen bonding candidate might have been the last one in the list).
  378. * If this can't be found either, return the previously choosen
  379. * router - obviously there are no other candidates.
  380. */
  381. rcu_read_lock();
  382. last_candidate = orig_node->last_bonding_candidate;
  383. if (last_candidate)
  384. last_cand_router = rcu_dereference(last_candidate->router);
  385. hlist_for_each_entry_rcu(cand, &orig_node->ifinfo_list, list) {
  386. /* acquire some structures and references ... */
  387. if (!atomic_inc_not_zero(&cand->refcount))
  388. continue;
  389. cand_router = rcu_dereference(cand->router);
  390. if (!cand_router)
  391. goto next;
  392. if (!atomic_inc_not_zero(&cand_router->refcount)) {
  393. cand_router = NULL;
  394. goto next;
  395. }
  396. /* alternative candidate should be good enough to be
  397. * considered
  398. */
  399. if (!bao->bat_neigh_is_equiv_or_better(cand_router,
  400. cand->if_outgoing,
  401. router, recv_if))
  402. goto next;
  403. /* don't use the same router twice */
  404. if (last_cand_router == cand_router)
  405. goto next;
  406. /* mark the first possible candidate */
  407. if (!first_candidate) {
  408. atomic_inc(&cand_router->refcount);
  409. atomic_inc(&cand->refcount);
  410. first_candidate = cand;
  411. first_candidate_router = cand_router;
  412. }
  413. /* check if the loop has already passed the previously selected
  414. * candidate ... this function should select the next candidate
  415. * AFTER the previously used bonding candidate.
  416. */
  417. if (!last_candidate || last_candidate_found) {
  418. next_candidate = cand;
  419. next_candidate_router = cand_router;
  420. break;
  421. }
  422. if (last_candidate == cand)
  423. last_candidate_found = true;
  424. next:
  425. /* free references */
  426. if (cand_router) {
  427. batadv_neigh_node_free_ref(cand_router);
  428. cand_router = NULL;
  429. }
  430. batadv_orig_ifinfo_free_ref(cand);
  431. }
  432. rcu_read_unlock();
  433. /* last_bonding_candidate is reset below, remove the old reference. */
  434. if (orig_node->last_bonding_candidate)
  435. batadv_orig_ifinfo_free_ref(orig_node->last_bonding_candidate);
  436. /* After finding candidates, handle the three cases:
  437. * 1) there is a next candidate, use that
  438. * 2) there is no next candidate, use the first of the list
  439. * 3) there is no candidate at all, return the default router
  440. */
  441. if (next_candidate) {
  442. batadv_neigh_node_free_ref(router);
  443. /* remove references to first candidate, we don't need it. */
  444. if (first_candidate) {
  445. batadv_neigh_node_free_ref(first_candidate_router);
  446. batadv_orig_ifinfo_free_ref(first_candidate);
  447. }
  448. router = next_candidate_router;
  449. orig_node->last_bonding_candidate = next_candidate;
  450. } else if (first_candidate) {
  451. batadv_neigh_node_free_ref(router);
  452. /* refcounting has already been done in the loop above. */
  453. router = first_candidate_router;
  454. orig_node->last_bonding_candidate = first_candidate;
  455. } else {
  456. orig_node->last_bonding_candidate = NULL;
  457. }
  458. return router;
  459. }
  460. static int batadv_route_unicast_packet(struct sk_buff *skb,
  461. struct batadv_hard_iface *recv_if)
  462. {
  463. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  464. struct batadv_orig_node *orig_node = NULL;
  465. struct batadv_unicast_packet *unicast_packet;
  466. struct ethhdr *ethhdr = eth_hdr(skb);
  467. int res, hdr_len, ret = NET_RX_DROP;
  468. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  469. /* TTL exceeded */
  470. if (unicast_packet->ttl < 2) {
  471. pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
  472. ethhdr->h_source, unicast_packet->dest);
  473. goto out;
  474. }
  475. /* get routing information */
  476. orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
  477. if (!orig_node)
  478. goto out;
  479. /* create a copy of the skb, if needed, to modify it. */
  480. if (skb_cow(skb, ETH_HLEN) < 0)
  481. goto out;
  482. /* decrement ttl */
  483. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  484. unicast_packet->ttl--;
  485. switch (unicast_packet->packet_type) {
  486. case BATADV_UNICAST_4ADDR:
  487. hdr_len = sizeof(struct batadv_unicast_4addr_packet);
  488. break;
  489. case BATADV_UNICAST:
  490. hdr_len = sizeof(struct batadv_unicast_packet);
  491. break;
  492. default:
  493. /* other packet types not supported - yet */
  494. hdr_len = -1;
  495. break;
  496. }
  497. if (hdr_len > 0)
  498. batadv_skb_set_priority(skb, hdr_len);
  499. res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
  500. /* translate transmit result into receive result */
  501. if (res == NET_XMIT_SUCCESS) {
  502. /* skb was transmitted and consumed */
  503. batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
  504. batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
  505. skb->len + ETH_HLEN);
  506. ret = NET_RX_SUCCESS;
  507. } else if (res == NET_XMIT_POLICED) {
  508. /* skb was buffered and consumed */
  509. ret = NET_RX_SUCCESS;
  510. }
  511. out:
  512. if (orig_node)
  513. batadv_orig_node_free_ref(orig_node);
  514. return ret;
  515. }
  516. /**
  517. * batadv_reroute_unicast_packet - update the unicast header for re-routing
  518. * @bat_priv: the bat priv with all the soft interface information
  519. * @unicast_packet: the unicast header to be updated
  520. * @dst_addr: the payload destination
  521. * @vid: VLAN identifier
  522. *
  523. * Search the translation table for dst_addr and update the unicast header with
  524. * the new corresponding information (originator address where the destination
  525. * client currently is and its known TTVN)
  526. *
  527. * Returns true if the packet header has been updated, false otherwise
  528. */
  529. static bool
  530. batadv_reroute_unicast_packet(struct batadv_priv *bat_priv,
  531. struct batadv_unicast_packet *unicast_packet,
  532. uint8_t *dst_addr, unsigned short vid)
  533. {
  534. struct batadv_orig_node *orig_node = NULL;
  535. struct batadv_hard_iface *primary_if = NULL;
  536. bool ret = false;
  537. uint8_t *orig_addr, orig_ttvn;
  538. if (batadv_is_my_client(bat_priv, dst_addr, vid)) {
  539. primary_if = batadv_primary_if_get_selected(bat_priv);
  540. if (!primary_if)
  541. goto out;
  542. orig_addr = primary_if->net_dev->dev_addr;
  543. orig_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
  544. } else {
  545. orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr,
  546. vid);
  547. if (!orig_node)
  548. goto out;
  549. if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
  550. goto out;
  551. orig_addr = orig_node->orig;
  552. orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  553. }
  554. /* update the packet header */
  555. ether_addr_copy(unicast_packet->dest, orig_addr);
  556. unicast_packet->ttvn = orig_ttvn;
  557. ret = true;
  558. out:
  559. if (primary_if)
  560. batadv_hardif_free_ref(primary_if);
  561. if (orig_node)
  562. batadv_orig_node_free_ref(orig_node);
  563. return ret;
  564. }
  565. static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
  566. struct sk_buff *skb, int hdr_len) {
  567. struct batadv_unicast_packet *unicast_packet;
  568. struct batadv_hard_iface *primary_if;
  569. struct batadv_orig_node *orig_node;
  570. uint8_t curr_ttvn, old_ttvn;
  571. struct ethhdr *ethhdr;
  572. unsigned short vid;
  573. int is_old_ttvn;
  574. /* check if there is enough data before accessing it */
  575. if (!pskb_may_pull(skb, hdr_len + ETH_HLEN))
  576. return 0;
  577. /* create a copy of the skb (in case of for re-routing) to modify it. */
  578. if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
  579. return 0;
  580. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  581. vid = batadv_get_vid(skb, hdr_len);
  582. ethhdr = (struct ethhdr *)(skb->data + hdr_len);
  583. /* check if the destination client was served by this node and it is now
  584. * roaming. In this case, it means that the node has got a ROAM_ADV
  585. * message and that it knows the new destination in the mesh to re-route
  586. * the packet to
  587. */
  588. if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest, vid)) {
  589. if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
  590. ethhdr->h_dest, vid))
  591. batadv_dbg_ratelimited(BATADV_DBG_TT,
  592. bat_priv,
  593. "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
  594. unicast_packet->dest,
  595. ethhdr->h_dest);
  596. /* at this point the mesh destination should have been
  597. * substituted with the originator address found in the global
  598. * table. If not, let the packet go untouched anyway because
  599. * there is nothing the node can do
  600. */
  601. return 1;
  602. }
  603. /* retrieve the TTVN known by this node for the packet destination. This
  604. * value is used later to check if the node which sent (or re-routed
  605. * last time) the packet had an updated information or not
  606. */
  607. curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
  608. if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
  609. orig_node = batadv_orig_hash_find(bat_priv,
  610. unicast_packet->dest);
  611. /* if it is not possible to find the orig_node representing the
  612. * destination, the packet can immediately be dropped as it will
  613. * not be possible to deliver it
  614. */
  615. if (!orig_node)
  616. return 0;
  617. curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  618. batadv_orig_node_free_ref(orig_node);
  619. }
  620. /* check if the TTVN contained in the packet is fresher than what the
  621. * node knows
  622. */
  623. is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
  624. if (!is_old_ttvn)
  625. return 1;
  626. old_ttvn = unicast_packet->ttvn;
  627. /* the packet was forged based on outdated network information. Its
  628. * destination can possibly be updated and forwarded towards the new
  629. * target host
  630. */
  631. if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
  632. ethhdr->h_dest, vid)) {
  633. batadv_dbg_ratelimited(BATADV_DBG_TT, bat_priv,
  634. "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
  635. unicast_packet->dest, ethhdr->h_dest,
  636. old_ttvn, curr_ttvn);
  637. return 1;
  638. }
  639. /* the packet has not been re-routed: either the destination is
  640. * currently served by this node or there is no destination at all and
  641. * it is possible to drop the packet
  642. */
  643. if (!batadv_is_my_client(bat_priv, ethhdr->h_dest, vid))
  644. return 0;
  645. /* update the header in order to let the packet be delivered to this
  646. * node's soft interface
  647. */
  648. primary_if = batadv_primary_if_get_selected(bat_priv);
  649. if (!primary_if)
  650. return 0;
  651. ether_addr_copy(unicast_packet->dest, primary_if->net_dev->dev_addr);
  652. batadv_hardif_free_ref(primary_if);
  653. unicast_packet->ttvn = curr_ttvn;
  654. return 1;
  655. }
  656. /**
  657. * batadv_recv_unhandled_unicast_packet - receive and process packets which
  658. * are in the unicast number space but not yet known to the implementation
  659. * @skb: unicast tvlv packet to process
  660. * @recv_if: pointer to interface this packet was received on
  661. *
  662. * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
  663. * otherwise.
  664. */
  665. int batadv_recv_unhandled_unicast_packet(struct sk_buff *skb,
  666. struct batadv_hard_iface *recv_if)
  667. {
  668. struct batadv_unicast_packet *unicast_packet;
  669. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  670. int check, hdr_size = sizeof(*unicast_packet);
  671. check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
  672. if (check < 0)
  673. return NET_RX_DROP;
  674. /* we don't know about this type, drop it. */
  675. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  676. if (batadv_is_my_mac(bat_priv, unicast_packet->dest))
  677. return NET_RX_DROP;
  678. return batadv_route_unicast_packet(skb, recv_if);
  679. }
  680. int batadv_recv_unicast_packet(struct sk_buff *skb,
  681. struct batadv_hard_iface *recv_if)
  682. {
  683. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  684. struct batadv_unicast_packet *unicast_packet;
  685. struct batadv_unicast_4addr_packet *unicast_4addr_packet;
  686. uint8_t *orig_addr;
  687. struct batadv_orig_node *orig_node = NULL;
  688. int check, hdr_size = sizeof(*unicast_packet);
  689. bool is4addr;
  690. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  691. unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
  692. is4addr = unicast_packet->packet_type == BATADV_UNICAST_4ADDR;
  693. /* the caller function should have already pulled 2 bytes */
  694. if (is4addr)
  695. hdr_size = sizeof(*unicast_4addr_packet);
  696. /* function returns -EREMOTE for promiscuous packets */
  697. check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
  698. /* Even though the packet is not for us, we might save it to use for
  699. * decoding a later received coded packet
  700. */
  701. if (check == -EREMOTE)
  702. batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
  703. if (check < 0)
  704. return NET_RX_DROP;
  705. if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
  706. return NET_RX_DROP;
  707. /* packet for me */
  708. if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
  709. if (is4addr) {
  710. batadv_dat_inc_counter(bat_priv,
  711. unicast_4addr_packet->subtype);
  712. orig_addr = unicast_4addr_packet->src;
  713. orig_node = batadv_orig_hash_find(bat_priv, orig_addr);
  714. }
  715. if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
  716. hdr_size))
  717. goto rx_success;
  718. if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
  719. hdr_size))
  720. goto rx_success;
  721. batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
  722. orig_node);
  723. rx_success:
  724. if (orig_node)
  725. batadv_orig_node_free_ref(orig_node);
  726. return NET_RX_SUCCESS;
  727. }
  728. return batadv_route_unicast_packet(skb, recv_if);
  729. }
  730. /**
  731. * batadv_recv_unicast_tvlv - receive and process unicast tvlv packets
  732. * @skb: unicast tvlv packet to process
  733. * @recv_if: pointer to interface this packet was received on
  734. * @dst_addr: the payload destination
  735. *
  736. * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
  737. * otherwise.
  738. */
  739. int batadv_recv_unicast_tvlv(struct sk_buff *skb,
  740. struct batadv_hard_iface *recv_if)
  741. {
  742. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  743. struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
  744. unsigned char *tvlv_buff;
  745. uint16_t tvlv_buff_len;
  746. int hdr_size = sizeof(*unicast_tvlv_packet);
  747. int ret = NET_RX_DROP;
  748. if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
  749. return NET_RX_DROP;
  750. /* the header is likely to be modified while forwarding */
  751. if (skb_cow(skb, hdr_size) < 0)
  752. return NET_RX_DROP;
  753. /* packet needs to be linearized to access the tvlv content */
  754. if (skb_linearize(skb) < 0)
  755. return NET_RX_DROP;
  756. unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)skb->data;
  757. tvlv_buff = (unsigned char *)(skb->data + hdr_size);
  758. tvlv_buff_len = ntohs(unicast_tvlv_packet->tvlv_len);
  759. if (tvlv_buff_len > skb->len - hdr_size)
  760. return NET_RX_DROP;
  761. ret = batadv_tvlv_containers_process(bat_priv, false, NULL,
  762. unicast_tvlv_packet->src,
  763. unicast_tvlv_packet->dst,
  764. tvlv_buff, tvlv_buff_len);
  765. if (ret != NET_RX_SUCCESS)
  766. ret = batadv_route_unicast_packet(skb, recv_if);
  767. else
  768. consume_skb(skb);
  769. return ret;
  770. }
  771. /**
  772. * batadv_recv_frag_packet - process received fragment
  773. * @skb: the received fragment
  774. * @recv_if: interface that the skb is received on
  775. *
  776. * This function does one of the three following things: 1) Forward fragment, if
  777. * the assembled packet will exceed our MTU; 2) Buffer fragment, if we till
  778. * lack further fragments; 3) Merge fragments, if we have all needed parts.
  779. *
  780. * Return NET_RX_DROP if the skb is not consumed, NET_RX_SUCCESS otherwise.
  781. */
  782. int batadv_recv_frag_packet(struct sk_buff *skb,
  783. struct batadv_hard_iface *recv_if)
  784. {
  785. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  786. struct batadv_orig_node *orig_node_src = NULL;
  787. struct batadv_frag_packet *frag_packet;
  788. int ret = NET_RX_DROP;
  789. if (batadv_check_unicast_packet(bat_priv, skb,
  790. sizeof(*frag_packet)) < 0)
  791. goto out;
  792. frag_packet = (struct batadv_frag_packet *)skb->data;
  793. orig_node_src = batadv_orig_hash_find(bat_priv, frag_packet->orig);
  794. if (!orig_node_src)
  795. goto out;
  796. /* Route the fragment if it is not for us and too big to be merged. */
  797. if (!batadv_is_my_mac(bat_priv, frag_packet->dest) &&
  798. batadv_frag_skb_fwd(skb, recv_if, orig_node_src)) {
  799. ret = NET_RX_SUCCESS;
  800. goto out;
  801. }
  802. batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_RX);
  803. batadv_add_counter(bat_priv, BATADV_CNT_FRAG_RX_BYTES, skb->len);
  804. /* Add fragment to buffer and merge if possible. */
  805. if (!batadv_frag_skb_buffer(&skb, orig_node_src))
  806. goto out;
  807. /* Deliver merged packet to the appropriate handler, if it was
  808. * merged
  809. */
  810. if (skb)
  811. batadv_batman_skb_recv(skb, recv_if->net_dev,
  812. &recv_if->batman_adv_ptype, NULL);
  813. ret = NET_RX_SUCCESS;
  814. out:
  815. if (orig_node_src)
  816. batadv_orig_node_free_ref(orig_node_src);
  817. return ret;
  818. }
  819. int batadv_recv_bcast_packet(struct sk_buff *skb,
  820. struct batadv_hard_iface *recv_if)
  821. {
  822. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  823. struct batadv_orig_node *orig_node = NULL;
  824. struct batadv_bcast_packet *bcast_packet;
  825. struct ethhdr *ethhdr;
  826. int hdr_size = sizeof(*bcast_packet);
  827. int ret = NET_RX_DROP;
  828. int32_t seq_diff;
  829. uint32_t seqno;
  830. /* drop packet if it has not necessary minimum size */
  831. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  832. goto out;
  833. ethhdr = eth_hdr(skb);
  834. /* packet with broadcast indication but unicast recipient */
  835. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  836. goto out;
  837. /* packet with broadcast sender address */
  838. if (is_broadcast_ether_addr(ethhdr->h_source))
  839. goto out;
  840. /* ignore broadcasts sent by myself */
  841. if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
  842. goto out;
  843. bcast_packet = (struct batadv_bcast_packet *)skb->data;
  844. /* ignore broadcasts originated by myself */
  845. if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
  846. goto out;
  847. if (bcast_packet->ttl < 2)
  848. goto out;
  849. orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
  850. if (!orig_node)
  851. goto out;
  852. spin_lock_bh(&orig_node->bcast_seqno_lock);
  853. seqno = ntohl(bcast_packet->seqno);
  854. /* check whether the packet is a duplicate */
  855. if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
  856. seqno))
  857. goto spin_unlock;
  858. seq_diff = seqno - orig_node->last_bcast_seqno;
  859. /* check whether the packet is old and the host just restarted. */
  860. if (batadv_window_protected(bat_priv, seq_diff,
  861. &orig_node->bcast_seqno_reset))
  862. goto spin_unlock;
  863. /* mark broadcast in flood history, update window position
  864. * if required.
  865. */
  866. if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
  867. orig_node->last_bcast_seqno = seqno;
  868. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  869. /* check whether this has been sent by another originator before */
  870. if (batadv_bla_check_bcast_duplist(bat_priv, skb))
  871. goto out;
  872. batadv_skb_set_priority(skb, sizeof(struct batadv_bcast_packet));
  873. /* rebroadcast packet */
  874. batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
  875. /* don't hand the broadcast up if it is from an originator
  876. * from the same backbone.
  877. */
  878. if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
  879. goto out;
  880. if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
  881. goto rx_success;
  882. if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
  883. goto rx_success;
  884. /* broadcast for me */
  885. batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
  886. orig_node);
  887. rx_success:
  888. ret = NET_RX_SUCCESS;
  889. goto out;
  890. spin_unlock:
  891. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  892. out:
  893. if (orig_node)
  894. batadv_orig_node_free_ref(orig_node);
  895. return ret;
  896. }