routing.c 33 KB

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