routing.c 33 KB

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