routing.c 32 KB

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