routing.c 31 KB

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