routing.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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 "bridge_loop_avoidance.h"
  28. #include "distributed-arp-table.h"
  29. #include "network-coding.h"
  30. #include "fragmentation.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_unicast_packet *unicast_packet;
  532. struct ethhdr *ethhdr = eth_hdr(skb);
  533. int res, hdr_len, ret = NET_RX_DROP;
  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. /* create a copy of the skb, if needed, to modify it. */
  546. if (skb_cow(skb, ETH_HLEN) < 0)
  547. goto out;
  548. /* decrement ttl */
  549. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  550. unicast_packet->header.ttl--;
  551. switch (unicast_packet->header.packet_type) {
  552. case BATADV_UNICAST_4ADDR:
  553. hdr_len = sizeof(struct batadv_unicast_4addr_packet);
  554. break;
  555. case BATADV_UNICAST:
  556. hdr_len = sizeof(struct batadv_unicast_packet);
  557. break;
  558. default:
  559. /* other packet types not supported - yet */
  560. hdr_len = -1;
  561. break;
  562. }
  563. if (hdr_len > 0)
  564. batadv_skb_set_priority(skb, hdr_len);
  565. res = batadv_send_skb_to_orig(skb, orig_node, recv_if);
  566. /* translate transmit result into receive result */
  567. if (res == NET_XMIT_SUCCESS) {
  568. /* skb was transmitted and consumed */
  569. batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
  570. batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
  571. skb->len + ETH_HLEN);
  572. ret = NET_RX_SUCCESS;
  573. } else if (res == NET_XMIT_POLICED) {
  574. /* skb was buffered and consumed */
  575. ret = NET_RX_SUCCESS;
  576. }
  577. out:
  578. if (orig_node)
  579. batadv_orig_node_free_ref(orig_node);
  580. return ret;
  581. }
  582. /**
  583. * batadv_reroute_unicast_packet - update the unicast header for re-routing
  584. * @bat_priv: the bat priv with all the soft interface information
  585. * @unicast_packet: the unicast header to be updated
  586. * @dst_addr: the payload destination
  587. *
  588. * Search the translation table for dst_addr and update the unicast header with
  589. * the new corresponding information (originator address where the destination
  590. * client currently is and its known TTVN)
  591. *
  592. * Returns true if the packet header has been updated, false otherwise
  593. */
  594. static bool
  595. batadv_reroute_unicast_packet(struct batadv_priv *bat_priv,
  596. struct batadv_unicast_packet *unicast_packet,
  597. uint8_t *dst_addr)
  598. {
  599. struct batadv_orig_node *orig_node = NULL;
  600. struct batadv_hard_iface *primary_if = NULL;
  601. bool ret = false;
  602. uint8_t *orig_addr, orig_ttvn;
  603. if (batadv_is_my_client(bat_priv, dst_addr)) {
  604. primary_if = batadv_primary_if_get_selected(bat_priv);
  605. if (!primary_if)
  606. goto out;
  607. orig_addr = primary_if->net_dev->dev_addr;
  608. orig_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
  609. } else {
  610. orig_node = batadv_transtable_search(bat_priv, NULL, dst_addr);
  611. if (!orig_node)
  612. goto out;
  613. if (batadv_compare_eth(orig_node->orig, unicast_packet->dest))
  614. goto out;
  615. orig_addr = orig_node->orig;
  616. orig_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  617. }
  618. /* update the packet header */
  619. memcpy(unicast_packet->dest, orig_addr, ETH_ALEN);
  620. unicast_packet->ttvn = orig_ttvn;
  621. ret = true;
  622. out:
  623. if (primary_if)
  624. batadv_hardif_free_ref(primary_if);
  625. if (orig_node)
  626. batadv_orig_node_free_ref(orig_node);
  627. return ret;
  628. }
  629. static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
  630. struct sk_buff *skb, int hdr_len) {
  631. uint8_t curr_ttvn, old_ttvn;
  632. struct batadv_orig_node *orig_node;
  633. struct ethhdr *ethhdr;
  634. struct batadv_hard_iface *primary_if;
  635. struct batadv_unicast_packet *unicast_packet;
  636. int is_old_ttvn;
  637. /* check if there is enough data before accessing it */
  638. if (pskb_may_pull(skb, hdr_len + ETH_HLEN) < 0)
  639. return 0;
  640. /* create a copy of the skb (in case of for re-routing) to modify it. */
  641. if (skb_cow(skb, sizeof(*unicast_packet)) < 0)
  642. return 0;
  643. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  644. ethhdr = (struct ethhdr *)(skb->data + hdr_len);
  645. /* check if the destination client was served by this node and it is now
  646. * roaming. In this case, it means that the node has got a ROAM_ADV
  647. * message and that it knows the new destination in the mesh to re-route
  648. * the packet to
  649. */
  650. if (batadv_tt_local_client_is_roaming(bat_priv, ethhdr->h_dest)) {
  651. if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
  652. ethhdr->h_dest))
  653. net_ratelimited_function(batadv_dbg, BATADV_DBG_TT,
  654. bat_priv,
  655. "Rerouting unicast packet to %pM (dst=%pM): Local Roaming\n",
  656. unicast_packet->dest,
  657. ethhdr->h_dest);
  658. /* at this point the mesh destination should have been
  659. * substituted with the originator address found in the global
  660. * table. If not, let the packet go untouched anyway because
  661. * there is nothing the node can do
  662. */
  663. return 1;
  664. }
  665. /* retrieve the TTVN known by this node for the packet destination. This
  666. * value is used later to check if the node which sent (or re-routed
  667. * last time) the packet had an updated information or not
  668. */
  669. curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
  670. if (!batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
  671. orig_node = batadv_orig_hash_find(bat_priv,
  672. unicast_packet->dest);
  673. /* if it is not possible to find the orig_node representing the
  674. * destination, the packet can immediately be dropped as it will
  675. * not be possible to deliver it
  676. */
  677. if (!orig_node)
  678. return 0;
  679. curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  680. batadv_orig_node_free_ref(orig_node);
  681. }
  682. /* check if the TTVN contained in the packet is fresher than what the
  683. * node knows
  684. */
  685. is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
  686. if (!is_old_ttvn)
  687. return 1;
  688. old_ttvn = unicast_packet->ttvn;
  689. /* the packet was forged based on outdated network information. Its
  690. * destination can possibly be updated and forwarded towards the new
  691. * target host
  692. */
  693. if (batadv_reroute_unicast_packet(bat_priv, unicast_packet,
  694. ethhdr->h_dest)) {
  695. net_ratelimited_function(batadv_dbg, BATADV_DBG_TT, bat_priv,
  696. "Rerouting unicast packet to %pM (dst=%pM): TTVN mismatch old_ttvn=%u new_ttvn=%u\n",
  697. unicast_packet->dest, ethhdr->h_dest,
  698. old_ttvn, curr_ttvn);
  699. return 1;
  700. }
  701. /* the packet has not been re-routed: either the destination is
  702. * currently served by this node or there is no destination at all and
  703. * it is possible to drop the packet
  704. */
  705. if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
  706. return 0;
  707. /* update the header in order to let the packet be delivered to this
  708. * node's soft interface
  709. */
  710. primary_if = batadv_primary_if_get_selected(bat_priv);
  711. if (!primary_if)
  712. return 0;
  713. memcpy(unicast_packet->dest, primary_if->net_dev->dev_addr, ETH_ALEN);
  714. batadv_hardif_free_ref(primary_if);
  715. unicast_packet->ttvn = curr_ttvn;
  716. return 1;
  717. }
  718. /**
  719. * batadv_recv_unhandled_unicast_packet - receive and process packets which
  720. * are in the unicast number space but not yet known to the implementation
  721. * @skb: unicast tvlv packet to process
  722. * @recv_if: pointer to interface this packet was received on
  723. *
  724. * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
  725. * otherwise.
  726. */
  727. int batadv_recv_unhandled_unicast_packet(struct sk_buff *skb,
  728. struct batadv_hard_iface *recv_if)
  729. {
  730. struct batadv_unicast_packet *unicast_packet;
  731. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  732. int check, hdr_size = sizeof(*unicast_packet);
  733. check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
  734. if (check < 0)
  735. return NET_RX_DROP;
  736. /* we don't know about this type, drop it. */
  737. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  738. if (batadv_is_my_mac(bat_priv, unicast_packet->dest))
  739. return NET_RX_DROP;
  740. return batadv_route_unicast_packet(skb, recv_if);
  741. }
  742. int batadv_recv_unicast_packet(struct sk_buff *skb,
  743. struct batadv_hard_iface *recv_if)
  744. {
  745. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  746. struct batadv_unicast_packet *unicast_packet;
  747. struct batadv_unicast_4addr_packet *unicast_4addr_packet;
  748. uint8_t *orig_addr;
  749. struct batadv_orig_node *orig_node = NULL;
  750. int check, hdr_size = sizeof(*unicast_packet);
  751. bool is4addr;
  752. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  753. unicast_4addr_packet = (struct batadv_unicast_4addr_packet *)skb->data;
  754. is4addr = unicast_packet->header.packet_type == BATADV_UNICAST_4ADDR;
  755. /* the caller function should have already pulled 2 bytes */
  756. if (is4addr)
  757. hdr_size = sizeof(*unicast_4addr_packet);
  758. /* function returns -EREMOTE for promiscuous packets */
  759. check = batadv_check_unicast_packet(bat_priv, skb, hdr_size);
  760. /* Even though the packet is not for us, we might save it to use for
  761. * decoding a later received coded packet
  762. */
  763. if (check == -EREMOTE)
  764. batadv_nc_skb_store_sniffed_unicast(bat_priv, skb);
  765. if (check < 0)
  766. return NET_RX_DROP;
  767. if (!batadv_check_unicast_ttvn(bat_priv, skb, hdr_size))
  768. return NET_RX_DROP;
  769. /* packet for me */
  770. if (batadv_is_my_mac(bat_priv, unicast_packet->dest)) {
  771. if (is4addr) {
  772. batadv_dat_inc_counter(bat_priv,
  773. unicast_4addr_packet->subtype);
  774. orig_addr = unicast_4addr_packet->src;
  775. orig_node = batadv_orig_hash_find(bat_priv, orig_addr);
  776. }
  777. if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb,
  778. hdr_size))
  779. goto rx_success;
  780. if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb,
  781. hdr_size))
  782. goto rx_success;
  783. batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
  784. orig_node);
  785. rx_success:
  786. if (orig_node)
  787. batadv_orig_node_free_ref(orig_node);
  788. return NET_RX_SUCCESS;
  789. }
  790. return batadv_route_unicast_packet(skb, recv_if);
  791. }
  792. /**
  793. * batadv_recv_unicast_tvlv - receive and process unicast tvlv packets
  794. * @skb: unicast tvlv packet to process
  795. * @recv_if: pointer to interface this packet was received on
  796. * @dst_addr: the payload destination
  797. *
  798. * Returns NET_RX_SUCCESS if the packet has been consumed or NET_RX_DROP
  799. * otherwise.
  800. */
  801. int batadv_recv_unicast_tvlv(struct sk_buff *skb,
  802. struct batadv_hard_iface *recv_if)
  803. {
  804. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  805. struct batadv_unicast_tvlv_packet *unicast_tvlv_packet;
  806. unsigned char *tvlv_buff;
  807. uint16_t tvlv_buff_len;
  808. int hdr_size = sizeof(*unicast_tvlv_packet);
  809. int ret = NET_RX_DROP;
  810. if (batadv_check_unicast_packet(bat_priv, skb, hdr_size) < 0)
  811. return NET_RX_DROP;
  812. /* the header is likely to be modified while forwarding */
  813. if (skb_cow(skb, hdr_size) < 0)
  814. return NET_RX_DROP;
  815. /* packet needs to be linearized to access the tvlv content */
  816. if (skb_linearize(skb) < 0)
  817. return NET_RX_DROP;
  818. unicast_tvlv_packet = (struct batadv_unicast_tvlv_packet *)skb->data;
  819. tvlv_buff = (unsigned char *)(skb->data + hdr_size);
  820. tvlv_buff_len = ntohs(unicast_tvlv_packet->tvlv_len);
  821. if (tvlv_buff_len > skb->len - hdr_size)
  822. return NET_RX_DROP;
  823. ret = batadv_tvlv_containers_process(bat_priv, false, NULL,
  824. unicast_tvlv_packet->src,
  825. unicast_tvlv_packet->dst,
  826. tvlv_buff, tvlv_buff_len);
  827. if (ret != NET_RX_SUCCESS)
  828. ret = batadv_route_unicast_packet(skb, recv_if);
  829. return ret;
  830. }
  831. /**
  832. * batadv_recv_frag_packet - process received fragment
  833. * @skb: the received fragment
  834. * @recv_if: interface that the skb is received on
  835. *
  836. * This function does one of the three following things: 1) Forward fragment, if
  837. * the assembled packet will exceed our MTU; 2) Buffer fragment, if we till
  838. * lack further fragments; 3) Merge fragments, if we have all needed parts.
  839. *
  840. * Return NET_RX_DROP if the skb is not consumed, NET_RX_SUCCESS otherwise.
  841. */
  842. int batadv_recv_frag_packet(struct sk_buff *skb,
  843. struct batadv_hard_iface *recv_if)
  844. {
  845. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  846. struct batadv_orig_node *orig_node_src = NULL;
  847. struct batadv_frag_packet *frag_packet;
  848. int ret = NET_RX_DROP;
  849. if (batadv_check_unicast_packet(bat_priv, skb,
  850. sizeof(*frag_packet)) < 0)
  851. goto out;
  852. frag_packet = (struct batadv_frag_packet *)skb->data;
  853. orig_node_src = batadv_orig_hash_find(bat_priv, frag_packet->orig);
  854. if (!orig_node_src)
  855. goto out;
  856. /* Route the fragment if it is not for us and too big to be merged. */
  857. if (!batadv_is_my_mac(bat_priv, frag_packet->dest) &&
  858. batadv_frag_skb_fwd(skb, recv_if, orig_node_src)) {
  859. ret = NET_RX_SUCCESS;
  860. goto out;
  861. }
  862. batadv_inc_counter(bat_priv, BATADV_CNT_FRAG_RX);
  863. batadv_add_counter(bat_priv, BATADV_CNT_FRAG_RX_BYTES, skb->len);
  864. /* Add fragment to buffer and merge if possible. */
  865. if (!batadv_frag_skb_buffer(&skb, orig_node_src))
  866. goto out;
  867. /* Deliver merged packet to the appropriate handler, if it was
  868. * merged
  869. */
  870. if (skb)
  871. batadv_batman_skb_recv(skb, recv_if->net_dev,
  872. &recv_if->batman_adv_ptype, NULL);
  873. ret = NET_RX_SUCCESS;
  874. out:
  875. if (orig_node_src)
  876. batadv_orig_node_free_ref(orig_node_src);
  877. return ret;
  878. }
  879. int batadv_recv_bcast_packet(struct sk_buff *skb,
  880. struct batadv_hard_iface *recv_if)
  881. {
  882. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  883. struct batadv_orig_node *orig_node = NULL;
  884. struct batadv_bcast_packet *bcast_packet;
  885. struct ethhdr *ethhdr;
  886. int hdr_size = sizeof(*bcast_packet);
  887. int ret = NET_RX_DROP;
  888. int32_t seq_diff;
  889. /* drop packet if it has not necessary minimum size */
  890. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  891. goto out;
  892. ethhdr = eth_hdr(skb);
  893. /* packet with broadcast indication but unicast recipient */
  894. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  895. goto out;
  896. /* packet with broadcast sender address */
  897. if (is_broadcast_ether_addr(ethhdr->h_source))
  898. goto out;
  899. /* ignore broadcasts sent by myself */
  900. if (batadv_is_my_mac(bat_priv, ethhdr->h_source))
  901. goto out;
  902. bcast_packet = (struct batadv_bcast_packet *)skb->data;
  903. /* ignore broadcasts originated by myself */
  904. if (batadv_is_my_mac(bat_priv, bcast_packet->orig))
  905. goto out;
  906. if (bcast_packet->header.ttl < 2)
  907. goto out;
  908. orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
  909. if (!orig_node)
  910. goto out;
  911. spin_lock_bh(&orig_node->bcast_seqno_lock);
  912. /* check whether the packet is a duplicate */
  913. if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
  914. ntohl(bcast_packet->seqno)))
  915. goto spin_unlock;
  916. seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
  917. /* check whether the packet is old and the host just restarted. */
  918. if (batadv_window_protected(bat_priv, seq_diff,
  919. &orig_node->bcast_seqno_reset))
  920. goto spin_unlock;
  921. /* mark broadcast in flood history, update window position
  922. * if required.
  923. */
  924. if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
  925. orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
  926. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  927. /* check whether this has been sent by another originator before */
  928. if (batadv_bla_check_bcast_duplist(bat_priv, skb))
  929. goto out;
  930. batadv_skb_set_priority(skb, sizeof(struct batadv_bcast_packet));
  931. /* rebroadcast packet */
  932. batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
  933. /* don't hand the broadcast up if it is from an originator
  934. * from the same backbone.
  935. */
  936. if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
  937. goto out;
  938. if (batadv_dat_snoop_incoming_arp_request(bat_priv, skb, hdr_size))
  939. goto rx_success;
  940. if (batadv_dat_snoop_incoming_arp_reply(bat_priv, skb, hdr_size))
  941. goto rx_success;
  942. /* broadcast for me */
  943. batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size,
  944. orig_node);
  945. rx_success:
  946. ret = NET_RX_SUCCESS;
  947. goto out;
  948. spin_unlock:
  949. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  950. out:
  951. if (orig_node)
  952. batadv_orig_node_free_ref(orig_node);
  953. return ret;
  954. }