routing.c 32 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  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. static int batadv_check_unicast_packet(struct sk_buff *skb, int hdr_size)
  471. {
  472. struct ethhdr *ethhdr;
  473. /* drop packet if it has not necessary minimum size */
  474. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  475. return -1;
  476. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  477. /* packet with unicast indication but broadcast recipient */
  478. if (is_broadcast_ether_addr(ethhdr->h_dest))
  479. return -1;
  480. /* packet with broadcast sender address */
  481. if (is_broadcast_ether_addr(ethhdr->h_source))
  482. return -1;
  483. /* not for me */
  484. if (!batadv_is_my_mac(ethhdr->h_dest))
  485. return -1;
  486. return 0;
  487. }
  488. int batadv_recv_tt_query(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
  489. {
  490. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  491. struct batadv_tt_query_packet *tt_query;
  492. uint16_t tt_size;
  493. int hdr_size = sizeof(*tt_query);
  494. char tt_flag;
  495. size_t packet_size;
  496. if (batadv_check_unicast_packet(skb, hdr_size) < 0)
  497. return NET_RX_DROP;
  498. /* I could need to modify it */
  499. if (skb_cow(skb, sizeof(struct batadv_tt_query_packet)) < 0)
  500. goto out;
  501. tt_query = (struct batadv_tt_query_packet *)skb->data;
  502. switch (tt_query->flags & BATADV_TT_QUERY_TYPE_MASK) {
  503. case BATADV_TT_REQUEST:
  504. batadv_inc_counter(bat_priv, BATADV_CNT_TT_REQUEST_RX);
  505. /* If we cannot provide an answer the tt_request is
  506. * forwarded
  507. */
  508. if (!batadv_send_tt_response(bat_priv, tt_query)) {
  509. if (tt_query->flags & BATADV_TT_FULL_TABLE)
  510. tt_flag = 'F';
  511. else
  512. tt_flag = '.';
  513. batadv_dbg(BATADV_DBG_TT, bat_priv,
  514. "Routing TT_REQUEST to %pM [%c]\n",
  515. tt_query->dst,
  516. tt_flag);
  517. return batadv_route_unicast_packet(skb, recv_if);
  518. }
  519. break;
  520. case BATADV_TT_RESPONSE:
  521. batadv_inc_counter(bat_priv, BATADV_CNT_TT_RESPONSE_RX);
  522. if (batadv_is_my_mac(tt_query->dst)) {
  523. /* packet needs to be linearized to access the TT
  524. * changes
  525. */
  526. if (skb_linearize(skb) < 0)
  527. goto out;
  528. /* skb_linearize() possibly changed skb->data */
  529. tt_query = (struct batadv_tt_query_packet *)skb->data;
  530. tt_size = batadv_tt_len(ntohs(tt_query->tt_data));
  531. /* Ensure we have all the claimed data */
  532. packet_size = sizeof(struct batadv_tt_query_packet);
  533. packet_size += tt_size;
  534. if (unlikely(skb_headlen(skb) < packet_size))
  535. goto out;
  536. batadv_handle_tt_response(bat_priv, tt_query);
  537. } else {
  538. if (tt_query->flags & BATADV_TT_FULL_TABLE)
  539. tt_flag = 'F';
  540. else
  541. tt_flag = '.';
  542. batadv_dbg(BATADV_DBG_TT, bat_priv,
  543. "Routing TT_RESPONSE to %pM [%c]\n",
  544. tt_query->dst,
  545. tt_flag);
  546. return batadv_route_unicast_packet(skb, recv_if);
  547. }
  548. break;
  549. }
  550. out:
  551. /* returning NET_RX_DROP will make the caller function kfree the skb */
  552. return NET_RX_DROP;
  553. }
  554. int batadv_recv_roam_adv(struct sk_buff *skb, struct batadv_hard_iface *recv_if)
  555. {
  556. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  557. struct batadv_roam_adv_packet *roam_adv_packet;
  558. struct batadv_orig_node *orig_node;
  559. struct ethhdr *ethhdr;
  560. /* drop packet if it has not necessary minimum size */
  561. if (unlikely(!pskb_may_pull(skb,
  562. sizeof(struct batadv_roam_adv_packet))))
  563. goto out;
  564. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  565. /* packet with unicast indication but broadcast recipient */
  566. if (is_broadcast_ether_addr(ethhdr->h_dest))
  567. goto out;
  568. /* packet with broadcast sender address */
  569. if (is_broadcast_ether_addr(ethhdr->h_source))
  570. goto out;
  571. batadv_inc_counter(bat_priv, BATADV_CNT_TT_ROAM_ADV_RX);
  572. roam_adv_packet = (struct batadv_roam_adv_packet *)skb->data;
  573. if (!batadv_is_my_mac(roam_adv_packet->dst))
  574. return batadv_route_unicast_packet(skb, recv_if);
  575. /* check if it is a backbone gateway. we don't accept
  576. * roaming advertisement from it, as it has the same
  577. * entries as we have.
  578. */
  579. if (batadv_bla_is_backbone_gw_orig(bat_priv, roam_adv_packet->src))
  580. goto out;
  581. orig_node = batadv_orig_hash_find(bat_priv, roam_adv_packet->src);
  582. if (!orig_node)
  583. goto out;
  584. batadv_dbg(BATADV_DBG_TT, bat_priv,
  585. "Received ROAMING_ADV from %pM (client %pM)\n",
  586. roam_adv_packet->src, roam_adv_packet->client);
  587. batadv_tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
  588. BATADV_TT_CLIENT_ROAM,
  589. atomic_read(&orig_node->last_ttvn) + 1);
  590. /* Roaming phase starts: I have new information but the ttvn has not
  591. * been incremented yet. This flag will make me check all the incoming
  592. * packets for the correct destination.
  593. */
  594. bat_priv->tt.poss_change = true;
  595. batadv_orig_node_free_ref(orig_node);
  596. out:
  597. /* returning NET_RX_DROP will make the caller function kfree the skb */
  598. return NET_RX_DROP;
  599. }
  600. /* find a suitable router for this originator, and use
  601. * bonding if possible. increases the found neighbors
  602. * refcount.
  603. */
  604. struct batadv_neigh_node *
  605. batadv_find_router(struct batadv_priv *bat_priv,
  606. struct batadv_orig_node *orig_node,
  607. const struct batadv_hard_iface *recv_if)
  608. {
  609. struct batadv_orig_node *primary_orig_node;
  610. struct batadv_orig_node *router_orig;
  611. struct batadv_neigh_node *router;
  612. static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  613. int bonding_enabled;
  614. uint8_t *primary_addr;
  615. if (!orig_node)
  616. return NULL;
  617. router = batadv_orig_node_get_router(orig_node);
  618. if (!router)
  619. goto err;
  620. /* without bonding, the first node should
  621. * always choose the default router.
  622. */
  623. bonding_enabled = atomic_read(&bat_priv->bonding);
  624. rcu_read_lock();
  625. /* select default router to output */
  626. router_orig = router->orig_node;
  627. if (!router_orig)
  628. goto err_unlock;
  629. if ((!recv_if) && (!bonding_enabled))
  630. goto return_router;
  631. primary_addr = router_orig->primary_addr;
  632. /* if we have something in the primary_addr, we can search
  633. * for a potential bonding candidate.
  634. */
  635. if (batadv_compare_eth(primary_addr, zero_mac))
  636. goto return_router;
  637. /* find the orig_node which has the primary interface. might
  638. * even be the same as our router_orig in many cases
  639. */
  640. if (batadv_compare_eth(primary_addr, router_orig->orig)) {
  641. primary_orig_node = router_orig;
  642. } else {
  643. primary_orig_node = batadv_orig_hash_find(bat_priv,
  644. primary_addr);
  645. if (!primary_orig_node)
  646. goto return_router;
  647. batadv_orig_node_free_ref(primary_orig_node);
  648. }
  649. /* with less than 2 candidates, we can't do any
  650. * bonding and prefer the original router.
  651. */
  652. if (atomic_read(&primary_orig_node->bond_candidates) < 2)
  653. goto return_router;
  654. /* all nodes between should choose a candidate which
  655. * is is not on the interface where the packet came
  656. * in.
  657. */
  658. batadv_neigh_node_free_ref(router);
  659. if (bonding_enabled)
  660. router = batadv_find_bond_router(primary_orig_node, recv_if);
  661. else
  662. router = batadv_find_ifalter_router(primary_orig_node, recv_if);
  663. return_router:
  664. if (router && router->if_incoming->if_status != BATADV_IF_ACTIVE)
  665. goto err_unlock;
  666. rcu_read_unlock();
  667. return router;
  668. err_unlock:
  669. rcu_read_unlock();
  670. err:
  671. if (router)
  672. batadv_neigh_node_free_ref(router);
  673. return NULL;
  674. }
  675. static int batadv_route_unicast_packet(struct sk_buff *skb,
  676. struct batadv_hard_iface *recv_if)
  677. {
  678. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  679. struct batadv_orig_node *orig_node = NULL;
  680. struct batadv_neigh_node *neigh_node = NULL;
  681. struct batadv_unicast_packet *unicast_packet;
  682. struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
  683. int ret = NET_RX_DROP;
  684. struct sk_buff *new_skb;
  685. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  686. /* TTL exceeded */
  687. if (unicast_packet->header.ttl < 2) {
  688. pr_debug("Warning - can't forward unicast packet from %pM to %pM: ttl exceeded\n",
  689. ethhdr->h_source, unicast_packet->dest);
  690. goto out;
  691. }
  692. /* get routing information */
  693. orig_node = batadv_orig_hash_find(bat_priv, unicast_packet->dest);
  694. if (!orig_node)
  695. goto out;
  696. /* find_router() increases neigh_nodes refcount if found. */
  697. neigh_node = batadv_find_router(bat_priv, orig_node, recv_if);
  698. if (!neigh_node)
  699. goto out;
  700. /* create a copy of the skb, if needed, to modify it. */
  701. if (skb_cow(skb, ETH_HLEN) < 0)
  702. goto out;
  703. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  704. if (unicast_packet->header.packet_type == BATADV_UNICAST &&
  705. atomic_read(&bat_priv->fragmentation) &&
  706. skb->len > neigh_node->if_incoming->net_dev->mtu) {
  707. ret = batadv_frag_send_skb(skb, bat_priv,
  708. neigh_node->if_incoming,
  709. neigh_node->addr);
  710. goto out;
  711. }
  712. if (unicast_packet->header.packet_type == BATADV_UNICAST_FRAG &&
  713. batadv_frag_can_reassemble(skb,
  714. neigh_node->if_incoming->net_dev->mtu)) {
  715. ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
  716. if (ret == NET_RX_DROP)
  717. goto out;
  718. /* packet was buffered for late merge */
  719. if (!new_skb) {
  720. ret = NET_RX_SUCCESS;
  721. goto out;
  722. }
  723. skb = new_skb;
  724. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  725. }
  726. /* decrement ttl */
  727. unicast_packet->header.ttl--;
  728. /* Update stats counter */
  729. batadv_inc_counter(bat_priv, BATADV_CNT_FORWARD);
  730. batadv_add_counter(bat_priv, BATADV_CNT_FORWARD_BYTES,
  731. skb->len + ETH_HLEN);
  732. /* route it */
  733. batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  734. ret = NET_RX_SUCCESS;
  735. out:
  736. if (neigh_node)
  737. batadv_neigh_node_free_ref(neigh_node);
  738. if (orig_node)
  739. batadv_orig_node_free_ref(orig_node);
  740. return ret;
  741. }
  742. static int batadv_check_unicast_ttvn(struct batadv_priv *bat_priv,
  743. struct sk_buff *skb) {
  744. uint8_t curr_ttvn;
  745. struct batadv_orig_node *orig_node;
  746. struct ethhdr *ethhdr;
  747. struct batadv_hard_iface *primary_if;
  748. struct batadv_unicast_packet *unicast_packet;
  749. bool tt_poss_change;
  750. int is_old_ttvn;
  751. /* I could need to modify it */
  752. if (skb_cow(skb, sizeof(struct batadv_unicast_packet)) < 0)
  753. return 0;
  754. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  755. if (batadv_is_my_mac(unicast_packet->dest)) {
  756. tt_poss_change = bat_priv->tt.poss_change;
  757. curr_ttvn = (uint8_t)atomic_read(&bat_priv->tt.vn);
  758. } else {
  759. orig_node = batadv_orig_hash_find(bat_priv,
  760. unicast_packet->dest);
  761. if (!orig_node)
  762. return 0;
  763. curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  764. tt_poss_change = orig_node->tt_poss_change;
  765. batadv_orig_node_free_ref(orig_node);
  766. }
  767. /* Check whether I have to reroute the packet */
  768. is_old_ttvn = batadv_seq_before(unicast_packet->ttvn, curr_ttvn);
  769. if (is_old_ttvn || tt_poss_change) {
  770. /* check if there is enough data before accessing it */
  771. if (pskb_may_pull(skb, sizeof(struct batadv_unicast_packet) +
  772. ETH_HLEN) < 0)
  773. return 0;
  774. ethhdr = (struct ethhdr *)(skb->data + sizeof(*unicast_packet));
  775. /* we don't have an updated route for this client, so we should
  776. * not try to reroute the packet!!
  777. */
  778. if (batadv_tt_global_client_is_roaming(bat_priv,
  779. ethhdr->h_dest))
  780. return 1;
  781. orig_node = batadv_transtable_search(bat_priv, NULL,
  782. ethhdr->h_dest);
  783. if (!orig_node) {
  784. if (!batadv_is_my_client(bat_priv, ethhdr->h_dest))
  785. return 0;
  786. primary_if = batadv_primary_if_get_selected(bat_priv);
  787. if (!primary_if)
  788. return 0;
  789. memcpy(unicast_packet->dest,
  790. primary_if->net_dev->dev_addr, ETH_ALEN);
  791. batadv_hardif_free_ref(primary_if);
  792. } else {
  793. memcpy(unicast_packet->dest, orig_node->orig,
  794. ETH_ALEN);
  795. curr_ttvn = (uint8_t)
  796. atomic_read(&orig_node->last_ttvn);
  797. batadv_orig_node_free_ref(orig_node);
  798. }
  799. batadv_dbg(BATADV_DBG_ROUTES, bat_priv,
  800. "TTVN mismatch (old_ttvn %u new_ttvn %u)! Rerouting unicast packet (for %pM) to %pM\n",
  801. unicast_packet->ttvn, curr_ttvn, ethhdr->h_dest,
  802. unicast_packet->dest);
  803. unicast_packet->ttvn = curr_ttvn;
  804. }
  805. return 1;
  806. }
  807. int batadv_recv_unicast_packet(struct sk_buff *skb,
  808. struct batadv_hard_iface *recv_if)
  809. {
  810. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  811. struct batadv_unicast_packet *unicast_packet;
  812. int hdr_size = sizeof(*unicast_packet);
  813. if (batadv_check_unicast_packet(skb, hdr_size) < 0)
  814. return NET_RX_DROP;
  815. if (!batadv_check_unicast_ttvn(bat_priv, skb))
  816. return NET_RX_DROP;
  817. unicast_packet = (struct batadv_unicast_packet *)skb->data;
  818. /* packet for me */
  819. if (batadv_is_my_mac(unicast_packet->dest)) {
  820. batadv_interface_rx(recv_if->soft_iface, skb, recv_if,
  821. hdr_size);
  822. return NET_RX_SUCCESS;
  823. }
  824. return batadv_route_unicast_packet(skb, recv_if);
  825. }
  826. int batadv_recv_ucast_frag_packet(struct sk_buff *skb,
  827. struct batadv_hard_iface *recv_if)
  828. {
  829. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  830. struct batadv_unicast_frag_packet *unicast_packet;
  831. int hdr_size = sizeof(*unicast_packet);
  832. struct sk_buff *new_skb = NULL;
  833. int ret;
  834. if (batadv_check_unicast_packet(skb, hdr_size) < 0)
  835. return NET_RX_DROP;
  836. if (!batadv_check_unicast_ttvn(bat_priv, skb))
  837. return NET_RX_DROP;
  838. unicast_packet = (struct batadv_unicast_frag_packet *)skb->data;
  839. /* packet for me */
  840. if (batadv_is_my_mac(unicast_packet->dest)) {
  841. ret = batadv_frag_reassemble_skb(skb, bat_priv, &new_skb);
  842. if (ret == NET_RX_DROP)
  843. return NET_RX_DROP;
  844. /* packet was buffered for late merge */
  845. if (!new_skb)
  846. return NET_RX_SUCCESS;
  847. batadv_interface_rx(recv_if->soft_iface, new_skb, recv_if,
  848. sizeof(struct batadv_unicast_packet));
  849. return NET_RX_SUCCESS;
  850. }
  851. return batadv_route_unicast_packet(skb, recv_if);
  852. }
  853. int batadv_recv_bcast_packet(struct sk_buff *skb,
  854. struct batadv_hard_iface *recv_if)
  855. {
  856. struct batadv_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  857. struct batadv_orig_node *orig_node = NULL;
  858. struct batadv_bcast_packet *bcast_packet;
  859. struct ethhdr *ethhdr;
  860. int hdr_size = sizeof(*bcast_packet);
  861. int ret = NET_RX_DROP;
  862. int32_t seq_diff;
  863. /* drop packet if it has not necessary minimum size */
  864. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  865. goto out;
  866. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  867. /* packet with broadcast indication but unicast recipient */
  868. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  869. goto out;
  870. /* packet with broadcast sender address */
  871. if (is_broadcast_ether_addr(ethhdr->h_source))
  872. goto out;
  873. /* ignore broadcasts sent by myself */
  874. if (batadv_is_my_mac(ethhdr->h_source))
  875. goto out;
  876. bcast_packet = (struct batadv_bcast_packet *)skb->data;
  877. /* ignore broadcasts originated by myself */
  878. if (batadv_is_my_mac(bcast_packet->orig))
  879. goto out;
  880. if (bcast_packet->header.ttl < 2)
  881. goto out;
  882. orig_node = batadv_orig_hash_find(bat_priv, bcast_packet->orig);
  883. if (!orig_node)
  884. goto out;
  885. spin_lock_bh(&orig_node->bcast_seqno_lock);
  886. /* check whether the packet is a duplicate */
  887. if (batadv_test_bit(orig_node->bcast_bits, orig_node->last_bcast_seqno,
  888. ntohl(bcast_packet->seqno)))
  889. goto spin_unlock;
  890. seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
  891. /* check whether the packet is old and the host just restarted. */
  892. if (batadv_window_protected(bat_priv, seq_diff,
  893. &orig_node->bcast_seqno_reset))
  894. goto spin_unlock;
  895. /* mark broadcast in flood history, update window position
  896. * if required.
  897. */
  898. if (batadv_bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
  899. orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
  900. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  901. /* check whether this has been sent by another originator before */
  902. if (batadv_bla_check_bcast_duplist(bat_priv, bcast_packet, hdr_size))
  903. goto out;
  904. /* rebroadcast packet */
  905. batadv_add_bcast_packet_to_list(bat_priv, skb, 1);
  906. /* don't hand the broadcast up if it is from an originator
  907. * from the same backbone.
  908. */
  909. if (batadv_bla_is_backbone_gw(skb, orig_node, hdr_size))
  910. goto out;
  911. /* broadcast for me */
  912. batadv_interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
  913. ret = NET_RX_SUCCESS;
  914. goto out;
  915. spin_unlock:
  916. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  917. out:
  918. if (orig_node)
  919. batadv_orig_node_free_ref(orig_node);
  920. return ret;
  921. }
  922. int batadv_recv_vis_packet(struct sk_buff *skb,
  923. struct batadv_hard_iface *recv_if)
  924. {
  925. struct batadv_vis_packet *vis_packet;
  926. struct ethhdr *ethhdr;
  927. struct batadv_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 batadv_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. }