routing.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  1. /*
  2. * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner, Simon Wunderlich
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of version 2 of the GNU General Public
  8. * License as published by the Free Software Foundation.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  18. * 02110-1301, USA
  19. *
  20. */
  21. #include "main.h"
  22. #include "routing.h"
  23. #include "send.h"
  24. #include "soft-interface.h"
  25. #include "hard-interface.h"
  26. #include "icmp_socket.h"
  27. #include "translation-table.h"
  28. #include "originator.h"
  29. #include "vis.h"
  30. #include "unicast.h"
  31. void slide_own_bcast_window(struct hard_iface *hard_iface)
  32. {
  33. struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  34. struct hashtable_t *hash = bat_priv->orig_hash;
  35. struct hlist_node *node;
  36. struct hlist_head *head;
  37. struct orig_node *orig_node;
  38. unsigned long *word;
  39. uint32_t i;
  40. size_t word_index;
  41. for (i = 0; i < hash->size; i++) {
  42. head = &hash->table[i];
  43. rcu_read_lock();
  44. hlist_for_each_entry_rcu(orig_node, node, head, hash_entry) {
  45. spin_lock_bh(&orig_node->ogm_cnt_lock);
  46. word_index = hard_iface->if_num * NUM_WORDS;
  47. word = &(orig_node->bcast_own[word_index]);
  48. bit_get_packet(bat_priv, word, 1, 0);
  49. orig_node->bcast_own_sum[hard_iface->if_num] =
  50. bit_packet_count(word);
  51. spin_unlock_bh(&orig_node->ogm_cnt_lock);
  52. }
  53. rcu_read_unlock();
  54. }
  55. }
  56. static void _update_route(struct bat_priv *bat_priv,
  57. struct orig_node *orig_node,
  58. struct neigh_node *neigh_node)
  59. {
  60. struct neigh_node *curr_router;
  61. curr_router = orig_node_get_router(orig_node);
  62. /* route deleted */
  63. if ((curr_router) && (!neigh_node)) {
  64. bat_dbg(DBG_ROUTES, bat_priv, "Deleting route towards: %pM\n",
  65. orig_node->orig);
  66. tt_global_del_orig(bat_priv, orig_node,
  67. "Deleted route towards originator");
  68. /* route added */
  69. } else if ((!curr_router) && (neigh_node)) {
  70. bat_dbg(DBG_ROUTES, bat_priv,
  71. "Adding route towards: %pM (via %pM)\n",
  72. orig_node->orig, neigh_node->addr);
  73. /* route changed */
  74. } else if (neigh_node && curr_router) {
  75. bat_dbg(DBG_ROUTES, bat_priv,
  76. "Changing route towards: %pM "
  77. "(now via %pM - was via %pM)\n",
  78. orig_node->orig, neigh_node->addr,
  79. curr_router->addr);
  80. }
  81. if (curr_router)
  82. neigh_node_free_ref(curr_router);
  83. /* increase refcount of new best neighbor */
  84. if (neigh_node && !atomic_inc_not_zero(&neigh_node->refcount))
  85. neigh_node = NULL;
  86. spin_lock_bh(&orig_node->neigh_list_lock);
  87. rcu_assign_pointer(orig_node->router, neigh_node);
  88. spin_unlock_bh(&orig_node->neigh_list_lock);
  89. /* decrease refcount of previous best neighbor */
  90. if (curr_router)
  91. neigh_node_free_ref(curr_router);
  92. }
  93. void update_route(struct bat_priv *bat_priv, struct orig_node *orig_node,
  94. struct neigh_node *neigh_node)
  95. {
  96. struct neigh_node *router = NULL;
  97. if (!orig_node)
  98. goto out;
  99. router = orig_node_get_router(orig_node);
  100. if (router != neigh_node)
  101. _update_route(bat_priv, orig_node, neigh_node);
  102. out:
  103. if (router)
  104. neigh_node_free_ref(router);
  105. }
  106. /* caller must hold the neigh_list_lock */
  107. void bonding_candidate_del(struct orig_node *orig_node,
  108. struct neigh_node *neigh_node)
  109. {
  110. /* this neighbor is not part of our candidate list */
  111. if (list_empty(&neigh_node->bonding_list))
  112. goto out;
  113. list_del_rcu(&neigh_node->bonding_list);
  114. INIT_LIST_HEAD(&neigh_node->bonding_list);
  115. neigh_node_free_ref(neigh_node);
  116. atomic_dec(&orig_node->bond_candidates);
  117. out:
  118. return;
  119. }
  120. void bonding_candidate_add(struct orig_node *orig_node,
  121. struct neigh_node *neigh_node)
  122. {
  123. struct hlist_node *node;
  124. struct neigh_node *tmp_neigh_node, *router = NULL;
  125. uint8_t interference_candidate = 0;
  126. spin_lock_bh(&orig_node->neigh_list_lock);
  127. /* only consider if it has the same primary address ... */
  128. if (!compare_eth(orig_node->orig,
  129. neigh_node->orig_node->primary_addr))
  130. goto candidate_del;
  131. router = orig_node_get_router(orig_node);
  132. if (!router)
  133. goto candidate_del;
  134. /* ... and is good enough to be considered */
  135. if (neigh_node->tq_avg < router->tq_avg - BONDING_TQ_THRESHOLD)
  136. goto candidate_del;
  137. /**
  138. * check if we have another candidate with the same mac address or
  139. * interface. If we do, we won't select this candidate because of
  140. * possible interference.
  141. */
  142. hlist_for_each_entry_rcu(tmp_neigh_node, node,
  143. &orig_node->neigh_list, list) {
  144. if (tmp_neigh_node == neigh_node)
  145. continue;
  146. /* we only care if the other candidate is even
  147. * considered as candidate. */
  148. if (list_empty(&tmp_neigh_node->bonding_list))
  149. continue;
  150. if ((neigh_node->if_incoming == tmp_neigh_node->if_incoming) ||
  151. (compare_eth(neigh_node->addr, tmp_neigh_node->addr))) {
  152. interference_candidate = 1;
  153. break;
  154. }
  155. }
  156. /* don't care further if it is an interference candidate */
  157. if (interference_candidate)
  158. goto candidate_del;
  159. /* this neighbor already is part of our candidate list */
  160. if (!list_empty(&neigh_node->bonding_list))
  161. goto out;
  162. if (!atomic_inc_not_zero(&neigh_node->refcount))
  163. goto out;
  164. list_add_rcu(&neigh_node->bonding_list, &orig_node->bond_list);
  165. atomic_inc(&orig_node->bond_candidates);
  166. goto out;
  167. candidate_del:
  168. bonding_candidate_del(orig_node, neigh_node);
  169. out:
  170. spin_unlock_bh(&orig_node->neigh_list_lock);
  171. if (router)
  172. neigh_node_free_ref(router);
  173. }
  174. /* copy primary address for bonding */
  175. void bonding_save_primary(const struct orig_node *orig_node,
  176. struct orig_node *orig_neigh_node,
  177. const struct batman_ogm_packet *batman_ogm_packet)
  178. {
  179. if (!(batman_ogm_packet->flags & PRIMARIES_FIRST_HOP))
  180. return;
  181. memcpy(orig_neigh_node->primary_addr, orig_node->orig, ETH_ALEN);
  182. }
  183. /* checks whether the host restarted and is in the protection time.
  184. * returns:
  185. * 0 if the packet is to be accepted
  186. * 1 if the packet is to be ignored.
  187. */
  188. int window_protected(struct bat_priv *bat_priv, int32_t seq_num_diff,
  189. unsigned long *last_reset)
  190. {
  191. if ((seq_num_diff <= -TQ_LOCAL_WINDOW_SIZE) ||
  192. (seq_num_diff >= EXPECTED_SEQNO_RANGE)) {
  193. if (has_timed_out(*last_reset, RESET_PROTECTION_MS)) {
  194. *last_reset = jiffies;
  195. bat_dbg(DBG_BATMAN, bat_priv,
  196. "old packet received, start protection\n");
  197. return 0;
  198. } else
  199. return 1;
  200. }
  201. return 0;
  202. }
  203. int recv_bat_ogm_packet(struct sk_buff *skb, struct hard_iface *hard_iface)
  204. {
  205. struct bat_priv *bat_priv = netdev_priv(hard_iface->soft_iface);
  206. struct ethhdr *ethhdr;
  207. /* drop packet if it has not necessary minimum size */
  208. if (unlikely(!pskb_may_pull(skb, BATMAN_OGM_LEN)))
  209. return NET_RX_DROP;
  210. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  211. /* packet with broadcast indication but unicast recipient */
  212. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  213. return NET_RX_DROP;
  214. /* packet with broadcast sender address */
  215. if (is_broadcast_ether_addr(ethhdr->h_source))
  216. return NET_RX_DROP;
  217. /* create a copy of the skb, if needed, to modify it. */
  218. if (skb_cow(skb, 0) < 0)
  219. return NET_RX_DROP;
  220. /* keep skb linear */
  221. if (skb_linearize(skb) < 0)
  222. return NET_RX_DROP;
  223. bat_priv->bat_algo_ops->bat_ogm_receive(hard_iface, skb);
  224. kfree_skb(skb);
  225. return NET_RX_SUCCESS;
  226. }
  227. static int recv_my_icmp_packet(struct bat_priv *bat_priv,
  228. struct sk_buff *skb, size_t icmp_len)
  229. {
  230. struct hard_iface *primary_if = NULL;
  231. struct orig_node *orig_node = NULL;
  232. struct neigh_node *router = NULL;
  233. struct icmp_packet_rr *icmp_packet;
  234. int ret = NET_RX_DROP;
  235. icmp_packet = (struct icmp_packet_rr *)skb->data;
  236. /* add data to device queue */
  237. if (icmp_packet->msg_type != ECHO_REQUEST) {
  238. bat_socket_receive_packet(icmp_packet, icmp_len);
  239. goto out;
  240. }
  241. primary_if = primary_if_get_selected(bat_priv);
  242. if (!primary_if)
  243. goto out;
  244. /* answer echo request (ping) */
  245. /* get routing information */
  246. orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
  247. if (!orig_node)
  248. goto out;
  249. router = orig_node_get_router(orig_node);
  250. if (!router)
  251. goto out;
  252. /* create a copy of the skb, if needed, to modify it. */
  253. if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
  254. goto out;
  255. icmp_packet = (struct icmp_packet_rr *)skb->data;
  256. memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
  257. memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  258. icmp_packet->msg_type = ECHO_REPLY;
  259. icmp_packet->header.ttl = TTL;
  260. send_skb_packet(skb, router->if_incoming, router->addr);
  261. ret = NET_RX_SUCCESS;
  262. out:
  263. if (primary_if)
  264. hardif_free_ref(primary_if);
  265. if (router)
  266. neigh_node_free_ref(router);
  267. if (orig_node)
  268. orig_node_free_ref(orig_node);
  269. return ret;
  270. }
  271. static int recv_icmp_ttl_exceeded(struct bat_priv *bat_priv,
  272. struct sk_buff *skb)
  273. {
  274. struct hard_iface *primary_if = NULL;
  275. struct orig_node *orig_node = NULL;
  276. struct neigh_node *router = NULL;
  277. struct icmp_packet *icmp_packet;
  278. int ret = NET_RX_DROP;
  279. icmp_packet = (struct icmp_packet *)skb->data;
  280. /* send TTL exceeded if packet is an echo request (traceroute) */
  281. if (icmp_packet->msg_type != ECHO_REQUEST) {
  282. pr_debug("Warning - can't forward icmp packet from %pM to "
  283. "%pM: ttl exceeded\n", icmp_packet->orig,
  284. icmp_packet->dst);
  285. goto out;
  286. }
  287. primary_if = primary_if_get_selected(bat_priv);
  288. if (!primary_if)
  289. goto out;
  290. /* get routing information */
  291. orig_node = orig_hash_find(bat_priv, icmp_packet->orig);
  292. if (!orig_node)
  293. goto out;
  294. router = orig_node_get_router(orig_node);
  295. if (!router)
  296. goto out;
  297. /* create a copy of the skb, if needed, to modify it. */
  298. if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
  299. goto out;
  300. icmp_packet = (struct icmp_packet *)skb->data;
  301. memcpy(icmp_packet->dst, icmp_packet->orig, ETH_ALEN);
  302. memcpy(icmp_packet->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  303. icmp_packet->msg_type = TTL_EXCEEDED;
  304. icmp_packet->header.ttl = TTL;
  305. send_skb_packet(skb, router->if_incoming, router->addr);
  306. ret = NET_RX_SUCCESS;
  307. out:
  308. if (primary_if)
  309. hardif_free_ref(primary_if);
  310. if (router)
  311. neigh_node_free_ref(router);
  312. if (orig_node)
  313. orig_node_free_ref(orig_node);
  314. return ret;
  315. }
  316. int recv_icmp_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  317. {
  318. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  319. struct icmp_packet_rr *icmp_packet;
  320. struct ethhdr *ethhdr;
  321. struct orig_node *orig_node = NULL;
  322. struct neigh_node *router = NULL;
  323. int hdr_size = sizeof(struct icmp_packet);
  324. int ret = NET_RX_DROP;
  325. /**
  326. * we truncate all incoming icmp packets if they don't match our size
  327. */
  328. if (skb->len >= sizeof(struct icmp_packet_rr))
  329. hdr_size = sizeof(struct 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 (!is_my_mac(ethhdr->h_dest))
  342. goto out;
  343. icmp_packet = (struct icmp_packet_rr *)skb->data;
  344. /* add record route information if not full */
  345. if ((hdr_size == sizeof(struct icmp_packet_rr)) &&
  346. (icmp_packet->rr_cur < BAT_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 (is_my_mac(icmp_packet->dst))
  353. return recv_my_icmp_packet(bat_priv, skb, hdr_size);
  354. /* TTL exceeded */
  355. if (icmp_packet->header.ttl < 2)
  356. return recv_icmp_ttl_exceeded(bat_priv, skb);
  357. /* get routing information */
  358. orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
  359. if (!orig_node)
  360. goto out;
  361. router = 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, sizeof(struct ethhdr)) < 0)
  366. goto out;
  367. icmp_packet = (struct icmp_packet_rr *)skb->data;
  368. /* decrement ttl */
  369. icmp_packet->header.ttl--;
  370. /* route it */
  371. send_skb_packet(skb, router->if_incoming, router->addr);
  372. ret = NET_RX_SUCCESS;
  373. out:
  374. if (router)
  375. neigh_node_free_ref(router);
  376. if (orig_node)
  377. 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. static struct neigh_node *find_bond_router(struct orig_node *primary_orig,
  386. const struct hard_iface *recv_if)
  387. {
  388. struct neigh_node *tmp_neigh_node;
  389. struct neigh_node *router = NULL, *first_candidate = NULL;
  390. rcu_read_lock();
  391. list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
  392. bonding_list) {
  393. if (!first_candidate)
  394. first_candidate = tmp_neigh_node;
  395. /* recv_if == NULL on the first node. */
  396. if (tmp_neigh_node->if_incoming == recv_if)
  397. continue;
  398. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  399. continue;
  400. router = tmp_neigh_node;
  401. break;
  402. }
  403. /* use the first candidate if nothing was found. */
  404. if (!router && first_candidate &&
  405. atomic_inc_not_zero(&first_candidate->refcount))
  406. router = first_candidate;
  407. if (!router)
  408. goto out;
  409. /* selected should point to the next element
  410. * after the current router */
  411. spin_lock_bh(&primary_orig->neigh_list_lock);
  412. /* this is a list_move(), which unfortunately
  413. * does not exist as rcu version */
  414. list_del_rcu(&primary_orig->bond_list);
  415. list_add_rcu(&primary_orig->bond_list,
  416. &router->bonding_list);
  417. spin_unlock_bh(&primary_orig->neigh_list_lock);
  418. out:
  419. rcu_read_unlock();
  420. return router;
  421. }
  422. /* Interface Alternating: Use the best of the
  423. * remaining candidates which are not using
  424. * this interface.
  425. *
  426. * Increases the returned router's refcount */
  427. static struct neigh_node *find_ifalter_router(struct orig_node *primary_orig,
  428. const struct hard_iface *recv_if)
  429. {
  430. struct neigh_node *tmp_neigh_node;
  431. struct neigh_node *router = NULL, *first_candidate = NULL;
  432. rcu_read_lock();
  433. list_for_each_entry_rcu(tmp_neigh_node, &primary_orig->bond_list,
  434. bonding_list) {
  435. if (!first_candidate)
  436. first_candidate = tmp_neigh_node;
  437. /* recv_if == NULL on the first node. */
  438. if (tmp_neigh_node->if_incoming == recv_if)
  439. continue;
  440. if (!atomic_inc_not_zero(&tmp_neigh_node->refcount))
  441. continue;
  442. /* if we don't have a router yet
  443. * or this one is better, choose it. */
  444. if ((!router) ||
  445. (tmp_neigh_node->tq_avg > router->tq_avg)) {
  446. /* decrement refcount of
  447. * previously selected router */
  448. if (router)
  449. neigh_node_free_ref(router);
  450. router = tmp_neigh_node;
  451. atomic_inc_not_zero(&router->refcount);
  452. }
  453. neigh_node_free_ref(tmp_neigh_node);
  454. }
  455. /* use the first candidate if nothing was found. */
  456. if (!router && first_candidate &&
  457. atomic_inc_not_zero(&first_candidate->refcount))
  458. router = first_candidate;
  459. rcu_read_unlock();
  460. return router;
  461. }
  462. int recv_tt_query(struct sk_buff *skb, struct hard_iface *recv_if)
  463. {
  464. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  465. struct tt_query_packet *tt_query;
  466. uint16_t tt_len;
  467. struct ethhdr *ethhdr;
  468. /* drop packet if it has not necessary minimum size */
  469. if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet))))
  470. goto out;
  471. /* I could need to modify it */
  472. if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0)
  473. goto out;
  474. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  475. /* packet with unicast indication but broadcast recipient */
  476. if (is_broadcast_ether_addr(ethhdr->h_dest))
  477. goto out;
  478. /* packet with broadcast sender address */
  479. if (is_broadcast_ether_addr(ethhdr->h_source))
  480. goto out;
  481. tt_query = (struct tt_query_packet *)skb->data;
  482. tt_query->tt_data = ntohs(tt_query->tt_data);
  483. switch (tt_query->flags & TT_QUERY_TYPE_MASK) {
  484. case TT_REQUEST:
  485. /* If we cannot provide an answer the tt_request is
  486. * forwarded */
  487. if (!send_tt_response(bat_priv, tt_query)) {
  488. bat_dbg(DBG_TT, bat_priv,
  489. "Routing TT_REQUEST to %pM [%c]\n",
  490. tt_query->dst,
  491. (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
  492. tt_query->tt_data = htons(tt_query->tt_data);
  493. return route_unicast_packet(skb, recv_if);
  494. }
  495. break;
  496. case TT_RESPONSE:
  497. if (is_my_mac(tt_query->dst)) {
  498. /* packet needs to be linearized to access the TT
  499. * changes */
  500. if (skb_linearize(skb) < 0)
  501. goto out;
  502. tt_len = tt_query->tt_data * sizeof(struct tt_change);
  503. /* Ensure we have all the claimed data */
  504. if (unlikely(skb_headlen(skb) <
  505. sizeof(struct tt_query_packet) + tt_len))
  506. goto out;
  507. handle_tt_response(bat_priv, tt_query);
  508. } else {
  509. bat_dbg(DBG_TT, bat_priv,
  510. "Routing TT_RESPONSE to %pM [%c]\n",
  511. tt_query->dst,
  512. (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
  513. tt_query->tt_data = htons(tt_query->tt_data);
  514. return route_unicast_packet(skb, recv_if);
  515. }
  516. break;
  517. }
  518. out:
  519. /* returning NET_RX_DROP will make the caller function kfree the skb */
  520. return NET_RX_DROP;
  521. }
  522. int recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
  523. {
  524. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  525. struct roam_adv_packet *roam_adv_packet;
  526. struct orig_node *orig_node;
  527. struct ethhdr *ethhdr;
  528. /* drop packet if it has not necessary minimum size */
  529. if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet))))
  530. goto out;
  531. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  532. /* packet with unicast indication but broadcast recipient */
  533. if (is_broadcast_ether_addr(ethhdr->h_dest))
  534. goto out;
  535. /* packet with broadcast sender address */
  536. if (is_broadcast_ether_addr(ethhdr->h_source))
  537. goto out;
  538. roam_adv_packet = (struct roam_adv_packet *)skb->data;
  539. if (!is_my_mac(roam_adv_packet->dst))
  540. return route_unicast_packet(skb, recv_if);
  541. orig_node = orig_hash_find(bat_priv, roam_adv_packet->src);
  542. if (!orig_node)
  543. goto out;
  544. bat_dbg(DBG_TT, bat_priv, "Received ROAMING_ADV from %pM "
  545. "(client %pM)\n", roam_adv_packet->src,
  546. roam_adv_packet->client);
  547. tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
  548. atomic_read(&orig_node->last_ttvn) + 1, true, false);
  549. /* Roaming phase starts: I have new information but the ttvn has not
  550. * been incremented yet. This flag will make me check all the incoming
  551. * packets for the correct destination. */
  552. bat_priv->tt_poss_change = true;
  553. orig_node_free_ref(orig_node);
  554. out:
  555. /* returning NET_RX_DROP will make the caller function kfree the skb */
  556. return NET_RX_DROP;
  557. }
  558. /* find a suitable router for this originator, and use
  559. * bonding if possible. increases the found neighbors
  560. * refcount.*/
  561. struct neigh_node *find_router(struct bat_priv *bat_priv,
  562. struct orig_node *orig_node,
  563. const struct hard_iface *recv_if)
  564. {
  565. struct orig_node *primary_orig_node;
  566. struct orig_node *router_orig;
  567. struct neigh_node *router;
  568. static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  569. int bonding_enabled;
  570. if (!orig_node)
  571. return NULL;
  572. router = orig_node_get_router(orig_node);
  573. if (!router)
  574. goto err;
  575. /* without bonding, the first node should
  576. * always choose the default router. */
  577. bonding_enabled = atomic_read(&bat_priv->bonding);
  578. rcu_read_lock();
  579. /* select default router to output */
  580. router_orig = router->orig_node;
  581. if (!router_orig)
  582. goto err_unlock;
  583. if ((!recv_if) && (!bonding_enabled))
  584. goto return_router;
  585. /* if we have something in the primary_addr, we can search
  586. * for a potential bonding candidate. */
  587. if (compare_eth(router_orig->primary_addr, zero_mac))
  588. goto return_router;
  589. /* find the orig_node which has the primary interface. might
  590. * even be the same as our router_orig in many cases */
  591. if (compare_eth(router_orig->primary_addr, router_orig->orig)) {
  592. primary_orig_node = router_orig;
  593. } else {
  594. primary_orig_node = orig_hash_find(bat_priv,
  595. router_orig->primary_addr);
  596. if (!primary_orig_node)
  597. goto return_router;
  598. orig_node_free_ref(primary_orig_node);
  599. }
  600. /* with less than 2 candidates, we can't do any
  601. * bonding and prefer the original router. */
  602. if (atomic_read(&primary_orig_node->bond_candidates) < 2)
  603. goto return_router;
  604. /* all nodes between should choose a candidate which
  605. * is is not on the interface where the packet came
  606. * in. */
  607. neigh_node_free_ref(router);
  608. if (bonding_enabled)
  609. router = find_bond_router(primary_orig_node, recv_if);
  610. else
  611. router = find_ifalter_router(primary_orig_node, recv_if);
  612. return_router:
  613. if (router && router->if_incoming->if_status != IF_ACTIVE)
  614. goto err_unlock;
  615. rcu_read_unlock();
  616. return router;
  617. err_unlock:
  618. rcu_read_unlock();
  619. err:
  620. if (router)
  621. neigh_node_free_ref(router);
  622. return NULL;
  623. }
  624. static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
  625. {
  626. struct ethhdr *ethhdr;
  627. /* drop packet if it has not necessary minimum size */
  628. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  629. return -1;
  630. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  631. /* packet with unicast indication but broadcast recipient */
  632. if (is_broadcast_ether_addr(ethhdr->h_dest))
  633. return -1;
  634. /* packet with broadcast sender address */
  635. if (is_broadcast_ether_addr(ethhdr->h_source))
  636. return -1;
  637. /* not for me */
  638. if (!is_my_mac(ethhdr->h_dest))
  639. return -1;
  640. return 0;
  641. }
  642. int route_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  643. {
  644. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  645. struct orig_node *orig_node = NULL;
  646. struct neigh_node *neigh_node = NULL;
  647. struct unicast_packet *unicast_packet;
  648. struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
  649. int ret = NET_RX_DROP;
  650. struct sk_buff *new_skb;
  651. unicast_packet = (struct unicast_packet *)skb->data;
  652. /* TTL exceeded */
  653. if (unicast_packet->header.ttl < 2) {
  654. pr_debug("Warning - can't forward unicast packet from %pM to "
  655. "%pM: ttl exceeded\n", ethhdr->h_source,
  656. unicast_packet->dest);
  657. goto out;
  658. }
  659. /* get routing information */
  660. orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
  661. if (!orig_node)
  662. goto out;
  663. /* find_router() increases neigh_nodes refcount if found. */
  664. neigh_node = find_router(bat_priv, orig_node, recv_if);
  665. if (!neigh_node)
  666. goto out;
  667. /* create a copy of the skb, if needed, to modify it. */
  668. if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
  669. goto out;
  670. unicast_packet = (struct unicast_packet *)skb->data;
  671. if (unicast_packet->header.packet_type == BAT_UNICAST &&
  672. atomic_read(&bat_priv->fragmentation) &&
  673. skb->len > neigh_node->if_incoming->net_dev->mtu) {
  674. ret = frag_send_skb(skb, bat_priv,
  675. neigh_node->if_incoming, neigh_node->addr);
  676. goto out;
  677. }
  678. if (unicast_packet->header.packet_type == BAT_UNICAST_FRAG &&
  679. frag_can_reassemble(skb, neigh_node->if_incoming->net_dev->mtu)) {
  680. ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
  681. if (ret == NET_RX_DROP)
  682. goto out;
  683. /* packet was buffered for late merge */
  684. if (!new_skb) {
  685. ret = NET_RX_SUCCESS;
  686. goto out;
  687. }
  688. skb = new_skb;
  689. unicast_packet = (struct unicast_packet *)skb->data;
  690. }
  691. /* decrement ttl */
  692. unicast_packet->header.ttl--;
  693. /* route it */
  694. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  695. ret = NET_RX_SUCCESS;
  696. out:
  697. if (neigh_node)
  698. neigh_node_free_ref(neigh_node);
  699. if (orig_node)
  700. orig_node_free_ref(orig_node);
  701. return ret;
  702. }
  703. static int check_unicast_ttvn(struct bat_priv *bat_priv,
  704. struct sk_buff *skb) {
  705. uint8_t curr_ttvn;
  706. struct orig_node *orig_node;
  707. struct ethhdr *ethhdr;
  708. struct hard_iface *primary_if;
  709. struct unicast_packet *unicast_packet;
  710. bool tt_poss_change;
  711. /* I could need to modify it */
  712. if (skb_cow(skb, sizeof(struct unicast_packet)) < 0)
  713. return 0;
  714. unicast_packet = (struct unicast_packet *)skb->data;
  715. if (is_my_mac(unicast_packet->dest)) {
  716. tt_poss_change = bat_priv->tt_poss_change;
  717. curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
  718. } else {
  719. orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
  720. if (!orig_node)
  721. return 0;
  722. curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  723. tt_poss_change = orig_node->tt_poss_change;
  724. orig_node_free_ref(orig_node);
  725. }
  726. /* Check whether I have to reroute the packet */
  727. if (seq_before(unicast_packet->ttvn, curr_ttvn) || tt_poss_change) {
  728. /* Linearize the skb before accessing it */
  729. if (skb_linearize(skb) < 0)
  730. return 0;
  731. ethhdr = (struct ethhdr *)(skb->data +
  732. sizeof(struct unicast_packet));
  733. orig_node = transtable_search(bat_priv, NULL, ethhdr->h_dest);
  734. if (!orig_node) {
  735. if (!is_my_client(bat_priv, ethhdr->h_dest))
  736. return 0;
  737. primary_if = primary_if_get_selected(bat_priv);
  738. if (!primary_if)
  739. return 0;
  740. memcpy(unicast_packet->dest,
  741. primary_if->net_dev->dev_addr, ETH_ALEN);
  742. hardif_free_ref(primary_if);
  743. } else {
  744. memcpy(unicast_packet->dest, orig_node->orig,
  745. ETH_ALEN);
  746. curr_ttvn = (uint8_t)
  747. atomic_read(&orig_node->last_ttvn);
  748. orig_node_free_ref(orig_node);
  749. }
  750. bat_dbg(DBG_ROUTES, bat_priv, "TTVN mismatch (old_ttvn %u "
  751. "new_ttvn %u)! Rerouting unicast packet (for %pM) to "
  752. "%pM\n", unicast_packet->ttvn, curr_ttvn,
  753. ethhdr->h_dest, unicast_packet->dest);
  754. unicast_packet->ttvn = curr_ttvn;
  755. }
  756. return 1;
  757. }
  758. int recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  759. {
  760. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  761. struct unicast_packet *unicast_packet;
  762. int hdr_size = sizeof(*unicast_packet);
  763. if (check_unicast_packet(skb, hdr_size) < 0)
  764. return NET_RX_DROP;
  765. if (!check_unicast_ttvn(bat_priv, skb))
  766. return NET_RX_DROP;
  767. unicast_packet = (struct unicast_packet *)skb->data;
  768. /* packet for me */
  769. if (is_my_mac(unicast_packet->dest)) {
  770. interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
  771. return NET_RX_SUCCESS;
  772. }
  773. return route_unicast_packet(skb, recv_if);
  774. }
  775. int recv_ucast_frag_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  776. {
  777. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  778. struct unicast_frag_packet *unicast_packet;
  779. int hdr_size = sizeof(*unicast_packet);
  780. struct sk_buff *new_skb = NULL;
  781. int ret;
  782. if (check_unicast_packet(skb, hdr_size) < 0)
  783. return NET_RX_DROP;
  784. if (!check_unicast_ttvn(bat_priv, skb))
  785. return NET_RX_DROP;
  786. unicast_packet = (struct unicast_frag_packet *)skb->data;
  787. /* packet for me */
  788. if (is_my_mac(unicast_packet->dest)) {
  789. ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
  790. if (ret == NET_RX_DROP)
  791. return NET_RX_DROP;
  792. /* packet was buffered for late merge */
  793. if (!new_skb)
  794. return NET_RX_SUCCESS;
  795. interface_rx(recv_if->soft_iface, new_skb, recv_if,
  796. sizeof(struct unicast_packet));
  797. return NET_RX_SUCCESS;
  798. }
  799. return route_unicast_packet(skb, recv_if);
  800. }
  801. int recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  802. {
  803. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  804. struct orig_node *orig_node = NULL;
  805. struct bcast_packet *bcast_packet;
  806. struct ethhdr *ethhdr;
  807. int hdr_size = sizeof(*bcast_packet);
  808. int ret = NET_RX_DROP;
  809. int32_t seq_diff;
  810. /* drop packet if it has not necessary minimum size */
  811. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  812. goto out;
  813. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  814. /* packet with broadcast indication but unicast recipient */
  815. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  816. goto out;
  817. /* packet with broadcast sender address */
  818. if (is_broadcast_ether_addr(ethhdr->h_source))
  819. goto out;
  820. /* ignore broadcasts sent by myself */
  821. if (is_my_mac(ethhdr->h_source))
  822. goto out;
  823. bcast_packet = (struct bcast_packet *)skb->data;
  824. /* ignore broadcasts originated by myself */
  825. if (is_my_mac(bcast_packet->orig))
  826. goto out;
  827. if (bcast_packet->header.ttl < 2)
  828. goto out;
  829. orig_node = orig_hash_find(bat_priv, bcast_packet->orig);
  830. if (!orig_node)
  831. goto out;
  832. spin_lock_bh(&orig_node->bcast_seqno_lock);
  833. /* check whether the packet is a duplicate */
  834. if (get_bit_status(orig_node->bcast_bits, orig_node->last_bcast_seqno,
  835. ntohl(bcast_packet->seqno)))
  836. goto spin_unlock;
  837. seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
  838. /* check whether the packet is old and the host just restarted. */
  839. if (window_protected(bat_priv, seq_diff,
  840. &orig_node->bcast_seqno_reset))
  841. goto spin_unlock;
  842. /* mark broadcast in flood history, update window position
  843. * if required. */
  844. if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
  845. orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
  846. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  847. /* rebroadcast packet */
  848. add_bcast_packet_to_list(bat_priv, skb, 1);
  849. /* broadcast for me */
  850. interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
  851. ret = NET_RX_SUCCESS;
  852. goto out;
  853. spin_unlock:
  854. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  855. out:
  856. if (orig_node)
  857. orig_node_free_ref(orig_node);
  858. return ret;
  859. }
  860. int recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  861. {
  862. struct vis_packet *vis_packet;
  863. struct ethhdr *ethhdr;
  864. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  865. int hdr_size = sizeof(*vis_packet);
  866. /* keep skb linear */
  867. if (skb_linearize(skb) < 0)
  868. return NET_RX_DROP;
  869. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  870. return NET_RX_DROP;
  871. vis_packet = (struct vis_packet *)skb->data;
  872. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  873. /* not for me */
  874. if (!is_my_mac(ethhdr->h_dest))
  875. return NET_RX_DROP;
  876. /* ignore own packets */
  877. if (is_my_mac(vis_packet->vis_orig))
  878. return NET_RX_DROP;
  879. if (is_my_mac(vis_packet->sender_orig))
  880. return NET_RX_DROP;
  881. switch (vis_packet->vis_type) {
  882. case VIS_TYPE_SERVER_SYNC:
  883. receive_server_sync_packet(bat_priv, vis_packet,
  884. skb_headlen(skb));
  885. break;
  886. case VIS_TYPE_CLIENT_UPDATE:
  887. receive_client_update_packet(bat_priv, vis_packet,
  888. skb_headlen(skb));
  889. break;
  890. default: /* ignore unknown packet */
  891. break;
  892. }
  893. /* We take a copy of the data in the packet, so we should
  894. always free the skbuf. */
  895. return NET_RX_DROP;
  896. }