routing.c 29 KB

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