routing.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  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 ((icmp_packet->msg_type == BATADV_ECHO_REPLY ||
  307. icmp_packet->msg_type == BATADV_ECHO_REQUEST) &&
  308. (hdr_size == sizeof(struct batadv_icmp_packet_rr)) &&
  309. (icmp_packet->rr_cur < BATADV_RR_LEN)) {
  310. memcpy(&(icmp_packet->rr[icmp_packet->rr_cur]),
  311. ethhdr->h_dest, ETH_ALEN);
  312. icmp_packet->rr_cur++;
  313. }
  314. /* packet for me */
  315. if (batadv_is_my_mac(bat_priv, icmp_packet->dst))
  316. return batadv_recv_my_icmp_packet(bat_priv, skb, hdr_size);
  317. /* TTL exceeded */
  318. if (icmp_packet->header.ttl < 2)
  319. return batadv_recv_icmp_ttl_exceeded(bat_priv, skb);
  320. /* get routing information */
  321. orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
  322. if (!orig_node)
  323. goto out;
  324. /* create a copy of the skb, if needed, to modify it. */
  325. if (skb_cow(skb, ETH_HLEN) < 0)
  326. goto out;
  327. icmp_packet = (struct batadv_icmp_packet_rr *)skb->data;
  328. /* decrement ttl */
  329. icmp_packet->header.ttl--;
  330. /* route it */
  331. if (batadv_send_skb_to_orig(skb, orig_node, recv_if) != NET_XMIT_DROP)
  332. ret = NET_RX_SUCCESS;
  333. out:
  334. if (orig_node)
  335. batadv_orig_node_free_ref(orig_node);
  336. return ret;
  337. }
  338. /* In the bonding case, send the packets in a round
  339. * robin fashion over the remaining interfaces.
  340. *
  341. * This method rotates the bonding list and increases the
  342. * returned router's refcount.
  343. */
  344. static struct batadv_neigh_node *
  345. batadv_find_bond_router(struct batadv_orig_node *primary_orig,
  346. const struct batadv_hard_iface *recv_if)
  347. {
  348. struct batadv_neigh_node *tmp_neigh_node;
  349. struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
  350. rcu_read_lock();
  351. list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
  352. bonding_list) {
  353. if (!first_candidate)
  354. first_candidate = tmp_neigh_node;
  355. /* recv_if == NULL on the first node. */
  356. if (tmp_neigh_node->if_incoming == recv_if)
  357. continue;
  358. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  359. continue;
  360. router = tmp_neigh_node;
  361. break;
  362. }
  363. /* use the first candidate if nothing was found. */
  364. if (!router && first_candidate &&
  365. atomic_inc_not_zero(&first_candidate->refcount))
  366. router = first_candidate;
  367. if (!router)
  368. goto out;
  369. /* selected should point to the next element
  370. * after the current router
  371. */
  372. spin_lock_bh(&primary_orig->neigh_list_lock);
  373. /* this is a list_move(), which unfortunately
  374. * does not exist as rcu version
  375. */
  376. list_del_rcu(&primary_orig->bond_list);
  377. list_add_rcu(&primary_orig->bond_list,
  378. &router->bonding_list);
  379. spin_unlock_bh(&primary_orig->neigh_list_lock);
  380. out:
  381. rcu_read_unlock();
  382. return router;
  383. }
  384. /* Interface Alternating: Use the best of the
  385. * remaining candidates which are not using
  386. * this interface.
  387. *
  388. * Increases the returned router's refcount
  389. */
  390. static struct batadv_neigh_node *
  391. batadv_find_ifalter_router(struct batadv_orig_node *primary_orig,
  392. const struct batadv_hard_iface *recv_if)
  393. {
  394. struct batadv_neigh_node *tmp_neigh_node;
  395. struct batadv_neigh_node *router = NULL, *first_candidate = NULL;
  396. rcu_read_lock();
  397. list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
  398. bonding_list) {
  399. if (!first_candidate)
  400. first_candidate = tmp_neigh_node;
  401. /* recv_if == NULL on the first node. */
  402. if (tmp_neigh_node->if_incoming == recv_if)
  403. continue;
  404. if (router && tmp_neigh_node->tq_avg <= router->tq_avg)
  405. continue;
  406. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  407. continue;
  408. /* decrement refcount of previously selected router */
  409. if (router)
  410. batadv_neigh_node_free_ref(router);
  411. /* we found a better router (or at least one valid router) */
  412. router = tmp_neigh_node;
  413. }
  414. /* use the first candidate if nothing was found. */
  415. if (!router && first_candidate &&
  416. atomic_inc_not_zero(&first_candidate->refcount))
  417. router = first_candidate;
  418. rcu_read_unlock();
  419. return router;
  420. }
  421. /**
  422. * batadv_check_unicast_packet - Check for malformed unicast packets
  423. * @bat_priv: the bat priv with all the soft interface information
  424. * @skb: packet to check
  425. * @hdr_size: size of header to pull
  426. *
  427. * Check for short header and bad addresses in given packet. Returns negative
  428. * value when check fails and 0 otherwise. The negative value depends on the
  429. * reason: -ENODATA for bad header, -EBADR for broadcast destination or source,
  430. * and -EREMOTE for non-local (other host) destination.
  431. */
  432. static int batadv_check_unicast_packet(struct batadv_priv *bat_priv,
  433. struct sk_buff *skb, int hdr_size)
  434. {
  435. struct ethhdr *ethhdr;
  436. /* drop packet if it has not necessary minimum size */
  437. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  438. return -ENODATA;
  439. ethhdr = eth_hdr(skb);
  440. /* packet with unicast indication but broadcast recipient */
  441. if (is_broadcast_ether_addr(ethhdr->h_dest))
  442. return -EBADR;
  443. /* packet with broadcast sender address */
  444. if (is_broadcast_ether_addr(ethhdr->h_source))
  445. return -EBADR;
  446. /* not for me */
  447. if (!batadv_is_my_mac(bat_priv, ethhdr->h_dest))
  448. return -EREMOTE;
  449. return 0;
  450. }
  451. /* find a suitable router for this originator, and use
  452. * bonding if possible. increases the found neighbors
  453. * refcount.
  454. */
  455. struct batadv_neigh_node *
  456. batadv_find_router(struct batadv_priv *bat_priv,
  457. struct batadv_orig_node *orig_node,
  458. const struct batadv_hard_iface *recv_if)
  459. {
  460. struct batadv_orig_node *primary_orig_node;
  461. struct batadv_orig_node *router_orig;
  462. struct batadv_neigh_node *router;
  463. static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  464. int bonding_enabled;
  465. uint8_t *primary_addr;
  466. if (!orig_node)
  467. return NULL;
  468. router = batadv_orig_node_get_router(orig_node);
  469. if (!router)
  470. goto err;
  471. /* without bonding, the first node should
  472. * always choose the default router.
  473. */
  474. bonding_enabled = atomic_read(&bat_priv->bonding);
  475. rcu_read_lock();
  476. /* select default router to output */
  477. router_orig = router->orig_node;
  478. if (!router_orig)
  479. goto err_unlock;
  480. if ((!recv_if) && (!bonding_enabled))
  481. goto return_router;
  482. primary_addr = router_orig->primary_addr;
  483. /* if we have something in the primary_addr, we can search
  484. * for a potential bonding candidate.
  485. */
  486. if (batadv_compare_eth(primary_addr, zero_mac))
  487. goto return_router;
  488. /* find the orig_node which has the primary interface. might
  489. * even be the same as our router_orig in many cases
  490. */
  491. if (batadv_compare_eth(primary_addr, router_orig->orig)) {
  492. primary_orig_node = router_orig;
  493. } else {
  494. primary_orig_node = batadv_orig_hash_find(bat_priv,
  495. primary_addr);
  496. if (!primary_orig_node)
  497. goto return_router;
  498. batadv_orig_node_free_ref(primary_orig_node);
  499. }
  500. /* with less than 2 candidates, we can't do any
  501. * bonding and prefer the original router.
  502. */
  503. if (atomic_read(&primary_orig_node->bond_candidates) < 2)
  504. goto return_router;
  505. /* all nodes between should choose a candidate which
  506. * is is not on the interface where the packet came
  507. * in.
  508. */
  509. batadv_neigh_node_free_ref(router);
  510. if (bonding_enabled)
  511. router = batadv_find_bond_router(primary_orig_node, recv_if);
  512. else
  513. router = batadv_find_ifalter_router(primary_orig_node, recv_if);
  514. return_router:
  515. if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE)
  516. goto err_unlock;
  517. rcu_read_unlock();
  518. return router;
  519. err_unlock:
  520. rcu_read_unlock();
  521. err:
  522. if (router)
  523. batadv_neigh_node_free_ref(router);
  524. return NULL;
  525. }
  526. static int batadv_route_unicast_packet(struct sk_buff *skb,
  527. struct batadv_hard_iface *recv_if)
  528. {
  529. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  530. struct batadv_orig_node *orig_node = NULL;
  531. struct batadv_neigh_node *neigh_node = NULL;
  532. struct batadv_unicast_packet *unicast_packet;
  533. struct ethhdr *ethhdr = eth_hdr(skb);
  534. int res, hdr_len, ret = NET_RX_DROP;
  535. struct sk_buff *new_skb;
  536. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  537. /* TTL exceeded */
  538. if (unicast_packet->header.ttl < 2) {
  539. pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
  540. ethhdr->h_source, unicast_packet->dest);
  541. goto out;
  542. }
  543. /* get routing information */
  544. orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
  545. if (!orig_node)
  546. goto out;
  547. /* find_router() increases neigh_nodes refcount if found. */
  548. neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
  549. if (!neigh_node)
  550. goto out;
  551. /* create a copy of the skb, if needed, to modify it. */
  552. if (skb_cow(skb, ETH_HLEN) < 0)
  553. goto out;
  554. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  555. if (unicast_packet->header.packet_type == BATADV_UNICAST &&
  556. atomic_read(&bat_priv->fragmentation) &&
  557. skb->len > neigh_node->if_incoming->net_dev->mtu) {
  558. ret = batadv_frag_send_skb(skb, bat_priv,
  559. neigh_node->if_incoming,
  560. neigh_node->addr);
  561. goto out;
  562. }
  563. if (unicast_packet->header.packet_type == BATADV_UNICAST_FRAG &&
  564. batadv_frag_can_reassemble(skb,
  565. neigh_node->if_incoming->net_dev->mtu)) {
  566. ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
  567. if (ret == NET_RX_DROP)
  568. goto out;
  569. /* packet was buffered for late merge */
  570. if (!new_skb) {
  571. ret = NET_RX_SUCCESS;
  572. goto out;
  573. }
  574. skb = new_skb;
  575. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  576. }
  577. /* decrement ttl */
  578. unicast_packet->header.ttl--;
  579. switch (unicast_packet->header.packet_type) {
  580. case BATADV_UNICAST_4ADDR:
  581. hdr_len = sizeof(struct batadv_unicast_4addr_packet);
  582. break;
  583. case BATADV_UNICAST:
  584. hdr_len = sizeof(struct batadv_unicast_packet);
  585. break;
  586. default:
  587. /* other packet types not supported - yet */
  588. hdr_len = -1;
  589. break;
  590. }
  591. if (hdr_len > 0)
  592. batadv_skb_set_priority(skb, hdr_len);
  593. res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
  594. /* translate transmit result into receive result */
  595. if (res == NET_XMIT_SUCCESS) {
  596. /* skb was transmitted and consumed */
  597. batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
  598. batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
  599. skb->len + ETH_HLEN);
  600. ret = NET_RX_SUCCESS;
  601. } else if (res == NET_XMIT_POLICED) {
  602. /* skb was buffered and consumed */
  603. ret = NET_RX_SUCCESS;
  604. }
  605. out:
  606. if (neigh_node)
  607. batadv_neigh_node_free_ref(neigh_node);
  608. if (orig_node)
  609. batadv_orig_node_free_ref(orig_node);
  610. return ret;
  611. }
  612. /**
  613. * batadv_reroute_unicast_packet - update the unicast header for re-routing
  614. * @bat_priv: the bat priv with all the soft interface information
  615. * @unicast_packet: the unicast header to be updated
  616. * @dst_addr: the payload destination
  617. *
  618. * Search the translation table for dst_addr and update the unicast header with
  619. * the new corresponding information (originator address where the destination
  620. * client currently is and its known TTVN)
  621. *
  622. * Returns true if the packet header has been updated, false otherwise
  623. */
  624. static bool
  625. batadv_reroute_unicast_packet(struct batadv_priv *bat_priv,
  626. struct batadv_unicast_packet *unicast_packet,
  627. uint8_t *dst_addr)
  628. {
  629. struct batadv_orig_node *orig_node = NULL;
  630. struct batadv_hard_iface *primary_if = NULL;
  631. bool ret = false;
  632. uint8_t *orig_addr, orig_ttvn;
  633. if (batadv_is_my_client(bat_priv, dst_addr)) {
  634. primary_if = batadv_primary_if_get_selected(bat_priv);
  635. if (!primary_if)
  636. goto out;
  637. orig_addr = primary_if->net_dev->dev_addr;
  638. orig_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
  639. } else {
  640. orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr);
  641. if (!orig_node)
  642. goto out;
  643. if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
  644. goto out;
  645. orig_addr = orig_node->orig;
  646. orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  647. }
  648. /* update the packet header */
  649. memcpy(unicast_packet->dest, orig_addr, ETH_ALEN);
  650. unicast_packet->ttvn = orig_ttvn;
  651. ret = true;
  652. out:
  653. if (primary_if)
  654. batadv_hardif_free_ref(primary_if);
  655. if (orig_node)
  656. batadv_orig_node_free_ref(orig_node);
  657. return ret;
  658. }
  659. static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
  660. struct sk_buff *skb, int hdr_len) {
  661. uint8_t curr_ttvn, old_ttvn;
  662. struct batadv_orig_node *orig_node;
  663. struct ethhdr *ethhdr;
  664. struct batadv_hard_iface *primary_if;
  665. struct batadv_unicast_packet *unicast_packet;
  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) < 0)
  669. return 0;
  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 0;
  673. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  674. ethhdr = (struct ethhdr *)(skb->data + hdr_len);
  675. /* check if the destination client was served by this node and it is now
  676. * roaming. In this case, it means that the node has got a ROAM_ADV
  677. * message and that it knows the new destination in the mesh to re-route
  678. * the packet to
  679. */
  680. if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest)) {
  681. if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
  682. ethhdr->h_dest))
  683. net_ratelimited_function(batadv_dbg, BATADV_DBG_TT,
  684. bat_priv,
  685. "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
  686. unicast_packet->dest,
  687. ethhdr->h_dest);
  688. /* at this point the mesh destination should have been
  689. * substituted with the originator address found in the global
  690. * table. If not, let the packet go untouched anyway because
  691. * there is nothing the node can do
  692. */
  693. return 1;
  694. }
  695. /* retrieve the TTVN known by this node for the packet destination. This
  696. * value is used later to check if the node which sent (or re-routed
  697. * last time) the packet had an updated information or not
  698. */
  699. curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
  700. if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
  701. orig_node = batadv_orig_hash_find(bat_priv,
  702. unicast_packet->dest);
  703. /* if it is not possible to find the orig_node representing the
  704. * destination, the packet can immediately be dropped as it will
  705. * not be possible to deliver it
  706. */
  707. if (!orig_node)
  708. return 0;
  709. curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  710. batadv_orig_node_free_ref(orig_node);
  711. }
  712. /* check if the TTVN contained in the packet is fresher than what the
  713. * node knows
  714. */
  715. is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
  716. if (!is_old_ttvn)
  717. return 1;
  718. old_ttvn = unicast_packet->ttvn;
  719. /* the packet was forged based on outdated network information. Its
  720. * destination can possibly be updated and forwarded towards the new
  721. * target host
  722. */
  723. if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
  724. ethhdr->h_dest)) {
  725. net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, bat_priv,
  726. "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
  727. unicast_packet->dest, ethhdr->h_dest,
  728. old_ttvn, curr_ttvn);
  729. return 1;
  730. }
  731. /* the packet has not been re-routed: either the destination is
  732. * currently served by this node or there is no destination at all and
  733. * it is possible to drop the packet
  734. */
  735. if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
  736. return 0;
  737. /* update the header in order to let the packet be delivered to this
  738. * node's soft interface
  739. */
  740. primary_if = batadv_primary_if_get_selected(bat_priv);
  741. if (!primary_if)
  742. return 0;
  743. memcpy(unicast_packet->dest, primary_if->net_dev->dev_addr, ETH_ALEN);
  744. batadv_hardif_free_ref(primary_if);
  745. unicast_packet->ttvn = curr_ttvn;
  746. return 1;
  747. }
  748. /**
  749. * batadv_recv_unhandled_unicast_packet - receive and process packets which
  750. * are in the unicast number space but not yet known to the implementation
  751. * @skb: unicast tvlv packet to process
  752. * @recv_if: pointer to interface this packet was received on
  753. *
  754. * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
  755. * otherwise.
  756. */
  757. int batadv_recv_unhandled_unicast_packet(struct sk_buff *skb,
  758. struct batadv_hard_iface *recv_if)
  759. {
  760. struct batadv_unicast_packet *unicast_packet;
  761. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  762. int check, hdr_size = sizeof(*unicast_packet);
  763. check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
  764. if (check < 0)
  765. return NET_RX_DROP;
  766. /* we don't know about this type, drop it. */
  767. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  768. if (batadv_is_my_mac(bat_priv, unicast_packet->dest))
  769. return NET_RX_DROP;
  770. return batadv_route_unicast_packet(skb, recv_if);
  771. }
  772. int batadv_recv_unicast_packet(struct sk_buff *skb,
  773. struct batadv_hard_iface *recv_if)
  774. {
  775. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  776. struct batadv_unicast_packet *unicast_packet;
  777. struct batadv_unicast_4addr_packet *unicast_4addr_packet;
  778. uint8_t *orig_addr;
  779. struct batadv_orig_node *orig_node = NULL;
  780. int check, hdr_size = sizeof(*unicast_packet);
  781. bool is4addr;
  782. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  783. unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
  784. is4addr = unicast_packet->header.packet_type == BATADV_UNICAST_4ADDR;
  785. /* the caller function should have already pulled 2 bytes */
  786. if (is4addr)
  787. hdr_size = sizeof(*unicast_4addr_packet);
  788. /* function returns -EREMOTE for promiscuous packets */
  789. check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
  790. /* Even though the packet is not for us, we might save it to use for
  791. * decoding a later received coded packet
  792. */
  793. if (check == -EREMOTE)
  794. batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
  795. if (check < 0)
  796. return NET_RX_DROP;
  797. if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
  798. return NET_RX_DROP;
  799. /* packet for me */
  800. if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
  801. if (is4addr) {
  802. batadv_dat_inc_counter(bat_priv,
  803. unicast_4addr_packet->subtype);
  804. orig_addr = unicast_4addr_packet->src;
  805. orig_node = batadv_orig_hash_find(bat_priv, orig_addr);
  806. }
  807. if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
  808. hdr_size))
  809. goto rx_success;
  810. if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
  811. hdr_size))
  812. goto rx_success;
  813. batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
  814. orig_node);
  815. rx_success:
  816. if (orig_node)
  817. batadv_orig_node_free_ref(orig_node);
  818. return NET_RX_SUCCESS;
  819. }
  820. return batadv_route_unicast_packet(skb, recv_if);
  821. }
  822. int batadv_recv_ucast_frag_packet(struct sk_buff *skb,
  823. struct batadv_hard_iface *recv_if)
  824. {
  825. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  826. struct batadv_unicast_frag_packet *unicast_packet;
  827. int hdr_size = sizeof(*unicast_packet);
  828. struct sk_buff *new_skb = NULL;
  829. int ret;
  830. if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
  831. return NET_RX_DROP;
  832. if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
  833. return NET_RX_DROP;
  834. unicast_packet = (struct batadv_unicast_frag_packet *)skb->data;
  835. /* packet for me */
  836. if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
  837. ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
  838. if (ret == NET_RX_DROP)
  839. return NET_RX_DROP;
  840. /* packet was buffered for late merge */
  841. if (!new_skb)
  842. return NET_RX_SUCCESS;
  843. if (batadv_dat_snoop_incoming_arp_request(bat_priv, new_skb,
  844. hdr_size))
  845. goto rx_success;
  846. if (batadv_dat_snoop_incoming_arp_reply(bat_priv, new_skb,
  847. hdr_size))
  848. goto rx_success;
  849. batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
  850. sizeof(struct batadv_unicast_packet), NULL);
  851. rx_success:
  852. return NET_RX_SUCCESS;
  853. }
  854. return batadv_route_unicast_packet(skb, recv_if);
  855. }
  856. /**
  857. * batadv_recv_unicast_tvlv - receive and process unicast tvlv packets
  858. * @skb: unicast tvlv packet to process
  859. * @recv_if: pointer to interface this packet was received on
  860. * @dst_addr: the payload destination
  861. *
  862. * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
  863. * otherwise.
  864. */
  865. int batadv_recv_unicast_tvlv(struct sk_buff *skb,
  866. struct batadv_hard_iface *recv_if)
  867. {
  868. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  869. struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
  870. unsigned char *tvlv_buff;
  871. uint16_t tvlv_buff_len;
  872. int hdr_size = sizeof(*unicast_tvlv_packet);
  873. int ret = NET_RX_DROP;
  874. if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
  875. return NET_RX_DROP;
  876. /* the header is likely to be modified while forwarding */
  877. if (skb_cow(skb, hdr_size) < 0)
  878. return NET_RX_DROP;
  879. /* packet needs to be linearized to access the tvlv content */
  880. if (skb_linearize(skb) < 0)
  881. return NET_RX_DROP;
  882. unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)skb->data;
  883. tvlv_buff = (unsigned char *)(skb->data + hdr_size);
  884. tvlv_buff_len = ntohs(unicast_tvlv_packet->tvlv_len);
  885. if (tvlv_buff_len > skb->len - hdr_size)
  886. return NET_RX_DROP;
  887. ret = batadv_tvlv_containers_process(bat_priv, false, NULL,
  888. unicast_tvlv_packet->src,
  889. unicast_tvlv_packet->dst,
  890. tvlv_buff, tvlv_buff_len);
  891. if (ret != NET_RX_SUCCESS)
  892. ret = batadv_route_unicast_packet(skb, recv_if);
  893. return ret;
  894. }
  895. int batadv_recv_bcast_packet(struct sk_buff *skb,
  896. struct batadv_hard_iface *recv_if)
  897. {
  898. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  899. struct batadv_orig_node *orig_node = NULL;
  900. struct batadv_bcast_packet *bcast_packet;
  901. struct ethhdr *ethhdr;
  902. int hdr_size = sizeof(*bcast_packet);
  903. int ret = NET_RX_DROP;
  904. int32_t seq_diff;
  905. /* drop packet if it has not necessary minimum size */
  906. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  907. goto out;
  908. ethhdr = eth_hdr(skb);
  909. /* packet with broadcast indication but unicast recipient */
  910. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  911. goto out;
  912. /* packet with broadcast sender address */
  913. if (is_broadcast_ether_addr(ethhdr->h_source))
  914. goto out;
  915. /* ignore broadcasts sent by myself */
  916. if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
  917. goto out;
  918. bcast_packet = (struct batadv_bcast_packet *)skb->data;
  919. /* ignore broadcasts originated by myself */
  920. if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
  921. goto out;
  922. if (bcast_packet->header.ttl < 2)
  923. goto out;
  924. orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
  925. if (!orig_node)
  926. goto out;
  927. spin_lock_bh(&orig_node->bcast_seqno_lock);
  928. /* check whether the packet is a duplicate */
  929. if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
  930. ntohl(bcast_packet->seqno)))
  931. goto spin_unlock;
  932. seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
  933. /* check whether the packet is old and the host just restarted. */
  934. if (batadv_window_protected(bat_priv, seq_diff,
  935. &orig_node->bcast_seqno_reset))
  936. goto spin_unlock;
  937. /* mark broadcast in flood history, update window position
  938. * if required.
  939. */
  940. if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
  941. orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
  942. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  943. /* check whether this has been sent by another originator before */
  944. if (batadv_bla_check_bcast_duplist(bat_priv, skb))
  945. goto out;
  946. batadv_skb_set_priority(skb, sizeof(struct batadv_bcast_packet));
  947. /* rebroadcast packet */
  948. batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
  949. /* don't hand the broadcast up if it is from an originator
  950. * from the same backbone.
  951. */
  952. if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
  953. goto out;
  954. if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
  955. goto rx_success;
  956. if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
  957. goto rx_success;
  958. /* broadcast for me */
  959. batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
  960. orig_node);
  961. rx_success:
  962. ret = NET_RX_SUCCESS;
  963. goto out;
  964. spin_unlock:
  965. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  966. out:
  967. if (orig_node)
  968. batadv_orig_node_free_ref(orig_node);
  969. return ret;
  970. }