routing.c 33 KB

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