routing.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126
  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. int 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. struct ethhdr *ethhdr;
  469. /* drop packet if it has not necessary minimum size */
  470. if (unlikely(!pskb_may_pull(skb, sizeof(struct tt_query_packet))))
  471. goto out;
  472. /* I could need to modify it */
  473. if (skb_cow(skb, sizeof(struct tt_query_packet)) < 0)
  474. goto out;
  475. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  476. /* packet with unicast indication but broadcast recipient */
  477. if (is_broadcast_ether_addr(ethhdr->h_dest))
  478. goto out;
  479. /* packet with broadcast sender address */
  480. if (is_broadcast_ether_addr(ethhdr->h_source))
  481. goto out;
  482. tt_query = (struct tt_query_packet *)skb->data;
  483. tt_query->tt_data = ntohs(tt_query->tt_data);
  484. switch (tt_query->flags & TT_QUERY_TYPE_MASK) {
  485. case TT_REQUEST:
  486. /* If we cannot provide an answer the tt_request is
  487. * forwarded */
  488. if (!send_tt_response(bat_priv, tt_query)) {
  489. bat_dbg(DBG_TT, bat_priv,
  490. "Routing TT_REQUEST to %pM [%c]\n",
  491. tt_query->dst,
  492. (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
  493. tt_query->tt_data = htons(tt_query->tt_data);
  494. return route_unicast_packet(skb, recv_if);
  495. }
  496. break;
  497. case TT_RESPONSE:
  498. /* packet needs to be linearized to access the TT changes */
  499. if (skb_linearize(skb) < 0)
  500. goto out;
  501. if (is_my_mac(tt_query->dst))
  502. handle_tt_response(bat_priv, tt_query);
  503. else {
  504. bat_dbg(DBG_TT, bat_priv,
  505. "Routing TT_RESPONSE to %pM [%c]\n",
  506. tt_query->dst,
  507. (tt_query->flags & TT_FULL_TABLE ? 'F' : '.'));
  508. tt_query->tt_data = htons(tt_query->tt_data);
  509. return route_unicast_packet(skb, recv_if);
  510. }
  511. break;
  512. }
  513. out:
  514. /* returning NET_RX_DROP will make the caller function kfree the skb */
  515. return NET_RX_DROP;
  516. }
  517. int recv_roam_adv(struct sk_buff *skb, struct hard_iface *recv_if)
  518. {
  519. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  520. struct roam_adv_packet *roam_adv_packet;
  521. struct orig_node *orig_node;
  522. struct ethhdr *ethhdr;
  523. /* drop packet if it has not necessary minimum size */
  524. if (unlikely(!pskb_may_pull(skb, sizeof(struct roam_adv_packet))))
  525. goto out;
  526. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  527. /* packet with unicast indication but broadcast recipient */
  528. if (is_broadcast_ether_addr(ethhdr->h_dest))
  529. goto out;
  530. /* packet with broadcast sender address */
  531. if (is_broadcast_ether_addr(ethhdr->h_source))
  532. goto out;
  533. roam_adv_packet = (struct roam_adv_packet *)skb->data;
  534. if (!is_my_mac(roam_adv_packet->dst))
  535. return route_unicast_packet(skb, recv_if);
  536. orig_node = orig_hash_find(bat_priv, roam_adv_packet->src);
  537. if (!orig_node)
  538. goto out;
  539. bat_dbg(DBG_TT, bat_priv, "Received ROAMING_ADV from %pM "
  540. "(client %pM)\n", roam_adv_packet->src,
  541. roam_adv_packet->client);
  542. tt_global_add(bat_priv, orig_node, roam_adv_packet->client,
  543. atomic_read(&orig_node->last_ttvn) + 1, true, false);
  544. /* Roaming phase starts: I have new information but the ttvn has not
  545. * been incremented yet. This flag will make me check all the incoming
  546. * packets for the correct destination. */
  547. bat_priv->tt_poss_change = true;
  548. orig_node_free_ref(orig_node);
  549. out:
  550. /* returning NET_RX_DROP will make the caller function kfree the skb */
  551. return NET_RX_DROP;
  552. }
  553. /* find a suitable router for this originator, and use
  554. * bonding if possible. increases the found neighbors
  555. * refcount.*/
  556. struct neigh_node *find_router(struct bat_priv *bat_priv,
  557. struct orig_node *orig_node,
  558. const struct hard_iface *recv_if)
  559. {
  560. struct orig_node *primary_orig_node;
  561. struct orig_node *router_orig;
  562. struct neigh_node *router;
  563. static uint8_t zero_mac[ETH_ALEN] = {0, 0, 0, 0, 0, 0};
  564. int bonding_enabled;
  565. if (!orig_node)
  566. return NULL;
  567. router = orig_node_get_router(orig_node);
  568. if (!router)
  569. goto err;
  570. /* without bonding, the first node should
  571. * always choose the default router. */
  572. bonding_enabled = atomic_read(&bat_priv->bonding);
  573. rcu_read_lock();
  574. /* select default router to output */
  575. router_orig = router->orig_node;
  576. if (!router_orig)
  577. goto err_unlock;
  578. if ((!recv_if) && (!bonding_enabled))
  579. goto return_router;
  580. /* if we have something in the primary_addr, we can search
  581. * for a potential bonding candidate. */
  582. if (compare_eth(router_orig->primary_addr, zero_mac))
  583. goto return_router;
  584. /* find the orig_node which has the primary interface. might
  585. * even be the same as our router_orig in many cases */
  586. if (compare_eth(router_orig->primary_addr, router_orig->orig)) {
  587. primary_orig_node = router_orig;
  588. } else {
  589. primary_orig_node = orig_hash_find(bat_priv,
  590. router_orig->primary_addr);
  591. if (!primary_orig_node)
  592. goto return_router;
  593. orig_node_free_ref(primary_orig_node);
  594. }
  595. /* with less than 2 candidates, we can't do any
  596. * bonding and prefer the original router. */
  597. if (atomic_read(&primary_orig_node->bond_candidates) < 2)
  598. goto return_router;
  599. /* all nodes between should choose a candidate which
  600. * is is not on the interface where the packet came
  601. * in. */
  602. neigh_node_free_ref(router);
  603. if (bonding_enabled)
  604. router = find_bond_router(primary_orig_node, recv_if);
  605. else
  606. router = find_ifalter_router(primary_orig_node, recv_if);
  607. return_router:
  608. if (router && router->if_incoming->if_status != IF_ACTIVE)
  609. goto err_unlock;
  610. rcu_read_unlock();
  611. return router;
  612. err_unlock:
  613. rcu_read_unlock();
  614. err:
  615. if (router)
  616. neigh_node_free_ref(router);
  617. return NULL;
  618. }
  619. static int check_unicast_packet(struct sk_buff *skb, int hdr_size)
  620. {
  621. struct ethhdr *ethhdr;
  622. /* drop packet if it has not necessary minimum size */
  623. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  624. return -1;
  625. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  626. /* packet with unicast indication but broadcast recipient */
  627. if (is_broadcast_ether_addr(ethhdr->h_dest))
  628. return -1;
  629. /* packet with broadcast sender address */
  630. if (is_broadcast_ether_addr(ethhdr->h_source))
  631. return -1;
  632. /* not for me */
  633. if (!is_my_mac(ethhdr->h_dest))
  634. return -1;
  635. return 0;
  636. }
  637. int route_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  638. {
  639. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  640. struct orig_node *orig_node = NULL;
  641. struct neigh_node *neigh_node = NULL;
  642. struct unicast_packet *unicast_packet;
  643. struct ethhdr *ethhdr = (struct ethhdr *)skb_mac_header(skb);
  644. int ret = NET_RX_DROP;
  645. struct sk_buff *new_skb;
  646. unicast_packet = (struct unicast_packet *)skb->data;
  647. /* TTL exceeded */
  648. if (unicast_packet->ttl < 2) {
  649. pr_debug("Warning - can't forward unicast packet from %pM to "
  650. "%pM: ttl exceeded\n", ethhdr->h_source,
  651. unicast_packet->dest);
  652. goto out;
  653. }
  654. /* get routing information */
  655. orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
  656. if (!orig_node)
  657. goto out;
  658. /* find_router() increases neigh_nodes refcount if found. */
  659. neigh_node = find_router(bat_priv, orig_node, recv_if);
  660. if (!neigh_node)
  661. goto out;
  662. /* create a copy of the skb, if needed, to modify it. */
  663. if (skb_cow(skb, sizeof(struct ethhdr)) < 0)
  664. goto out;
  665. unicast_packet = (struct unicast_packet *)skb->data;
  666. if (unicast_packet->packet_type == BAT_UNICAST &&
  667. atomic_read(&bat_priv->fragmentation) &&
  668. skb->len > neigh_node->if_incoming->net_dev->mtu) {
  669. ret = frag_send_skb(skb, bat_priv,
  670. neigh_node->if_incoming, neigh_node->addr);
  671. goto out;
  672. }
  673. if (unicast_packet->packet_type == BAT_UNICAST_FRAG &&
  674. frag_can_reassemble(skb, neigh_node->if_incoming->net_dev->mtu)) {
  675. ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
  676. if (ret == NET_RX_DROP)
  677. goto out;
  678. /* packet was buffered for late merge */
  679. if (!new_skb) {
  680. ret = NET_RX_SUCCESS;
  681. goto out;
  682. }
  683. skb = new_skb;
  684. unicast_packet = (struct unicast_packet *)skb->data;
  685. }
  686. /* decrement ttl */
  687. unicast_packet->ttl--;
  688. /* route it */
  689. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  690. ret = NET_RX_SUCCESS;
  691. out:
  692. if (neigh_node)
  693. neigh_node_free_ref(neigh_node);
  694. if (orig_node)
  695. orig_node_free_ref(orig_node);
  696. return ret;
  697. }
  698. static int check_unicast_ttvn(struct bat_priv *bat_priv,
  699. struct sk_buff *skb) {
  700. uint8_t curr_ttvn;
  701. struct orig_node *orig_node;
  702. struct ethhdr *ethhdr;
  703. struct hard_iface *primary_if;
  704. struct unicast_packet *unicast_packet;
  705. bool tt_poss_change;
  706. /* I could need to modify it */
  707. if (skb_cow(skb, sizeof(struct unicast_packet)) < 0)
  708. return 0;
  709. unicast_packet = (struct unicast_packet *)skb->data;
  710. if (is_my_mac(unicast_packet->dest)) {
  711. tt_poss_change = bat_priv->tt_poss_change;
  712. curr_ttvn = (uint8_t)atomic_read(&bat_priv->ttvn);
  713. } else {
  714. orig_node = orig_hash_find(bat_priv, unicast_packet->dest);
  715. if (!orig_node)
  716. return 0;
  717. curr_ttvn = (uint8_t)atomic_read(&orig_node->last_ttvn);
  718. tt_poss_change = orig_node->tt_poss_change;
  719. orig_node_free_ref(orig_node);
  720. }
  721. /* Check whether I have to reroute the packet */
  722. if (seq_before(unicast_packet->ttvn, curr_ttvn) || tt_poss_change) {
  723. /* Linearize the skb before accessing it */
  724. if (skb_linearize(skb) < 0)
  725. return 0;
  726. ethhdr = (struct ethhdr *)(skb->data +
  727. sizeof(struct unicast_packet));
  728. orig_node = transtable_search(bat_priv, NULL, ethhdr->h_dest);
  729. if (!orig_node) {
  730. if (!is_my_client(bat_priv, ethhdr->h_dest))
  731. return 0;
  732. primary_if = primary_if_get_selected(bat_priv);
  733. if (!primary_if)
  734. return 0;
  735. memcpy(unicast_packet->dest,
  736. primary_if->net_dev->dev_addr, ETH_ALEN);
  737. hardif_free_ref(primary_if);
  738. } else {
  739. memcpy(unicast_packet->dest, orig_node->orig,
  740. ETH_ALEN);
  741. curr_ttvn = (uint8_t)
  742. atomic_read(&orig_node->last_ttvn);
  743. orig_node_free_ref(orig_node);
  744. }
  745. bat_dbg(DBG_ROUTES, bat_priv, "TTVN mismatch (old_ttvn %u "
  746. "new_ttvn %u)! Rerouting unicast packet (for %pM) to "
  747. "%pM\n", unicast_packet->ttvn, curr_ttvn,
  748. ethhdr->h_dest, unicast_packet->dest);
  749. unicast_packet->ttvn = curr_ttvn;
  750. }
  751. return 1;
  752. }
  753. int recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  754. {
  755. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  756. struct unicast_packet *unicast_packet;
  757. int hdr_size = sizeof(*unicast_packet);
  758. if (check_unicast_packet(skb, hdr_size) < 0)
  759. return NET_RX_DROP;
  760. if (!check_unicast_ttvn(bat_priv, skb))
  761. return NET_RX_DROP;
  762. unicast_packet = (struct unicast_packet *)skb->data;
  763. /* packet for me */
  764. if (is_my_mac(unicast_packet->dest)) {
  765. interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
  766. return NET_RX_SUCCESS;
  767. }
  768. return route_unicast_packet(skb, recv_if);
  769. }
  770. int recv_ucast_frag_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  771. {
  772. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  773. struct unicast_frag_packet *unicast_packet;
  774. int hdr_size = sizeof(*unicast_packet);
  775. struct sk_buff *new_skb = NULL;
  776. int ret;
  777. if (check_unicast_packet(skb, hdr_size) < 0)
  778. return NET_RX_DROP;
  779. if (!check_unicast_ttvn(bat_priv, skb))
  780. return NET_RX_DROP;
  781. unicast_packet = (struct unicast_frag_packet *)skb->data;
  782. /* packet for me */
  783. if (is_my_mac(unicast_packet->dest)) {
  784. ret = frag_reassemble_skb(skb, bat_priv, &new_skb);
  785. if (ret == NET_RX_DROP)
  786. return NET_RX_DROP;
  787. /* packet was buffered for late merge */
  788. if (!new_skb)
  789. return NET_RX_SUCCESS;
  790. interface_rx(recv_if->soft_iface, new_skb, recv_if,
  791. sizeof(struct unicast_packet));
  792. return NET_RX_SUCCESS;
  793. }
  794. return route_unicast_packet(skb, recv_if);
  795. }
  796. int recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  797. {
  798. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  799. struct orig_node *orig_node = NULL;
  800. struct bcast_packet *bcast_packet;
  801. struct ethhdr *ethhdr;
  802. int hdr_size = sizeof(*bcast_packet);
  803. int ret = NET_RX_DROP;
  804. int32_t seq_diff;
  805. /* drop packet if it has not necessary minimum size */
  806. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  807. goto out;
  808. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  809. /* packet with broadcast indication but unicast recipient */
  810. if (!is_broadcast_ether_addr(ethhdr->h_dest))
  811. goto out;
  812. /* packet with broadcast sender address */
  813. if (is_broadcast_ether_addr(ethhdr->h_source))
  814. goto out;
  815. /* ignore broadcasts sent by myself */
  816. if (is_my_mac(ethhdr->h_source))
  817. goto out;
  818. bcast_packet = (struct bcast_packet *)skb->data;
  819. /* ignore broadcasts originated by myself */
  820. if (is_my_mac(bcast_packet->orig))
  821. goto out;
  822. if (bcast_packet->ttl < 2)
  823. goto out;
  824. orig_node = orig_hash_find(bat_priv, bcast_packet->orig);
  825. if (!orig_node)
  826. goto out;
  827. spin_lock_bh(&orig_node->bcast_seqno_lock);
  828. /* check whether the packet is a duplicate */
  829. if (get_bit_status(orig_node->bcast_bits, orig_node->last_bcast_seqno,
  830. ntohl(bcast_packet->seqno)))
  831. goto spin_unlock;
  832. seq_diff = ntohl(bcast_packet->seqno) - orig_node->last_bcast_seqno;
  833. /* check whether the packet is old and the host just restarted. */
  834. if (window_protected(bat_priv, seq_diff,
  835. &orig_node->bcast_seqno_reset))
  836. goto spin_unlock;
  837. /* mark broadcast in flood history, update window position
  838. * if required. */
  839. if (bit_get_packet(bat_priv, orig_node->bcast_bits, seq_diff, 1))
  840. orig_node->last_bcast_seqno = ntohl(bcast_packet->seqno);
  841. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  842. /* rebroadcast packet */
  843. add_bcast_packet_to_list(bat_priv, skb, 1);
  844. /* broadcast for me */
  845. interface_rx(recv_if->soft_iface, skb, recv_if, hdr_size);
  846. ret = NET_RX_SUCCESS;
  847. goto out;
  848. spin_unlock:
  849. spin_unlock_bh(&orig_node->bcast_seqno_lock);
  850. out:
  851. if (orig_node)
  852. orig_node_free_ref(orig_node);
  853. return ret;
  854. }
  855. int recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
  856. {
  857. struct vis_packet *vis_packet;
  858. struct ethhdr *ethhdr;
  859. struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
  860. int hdr_size = sizeof(*vis_packet);
  861. /* keep skb linear */
  862. if (skb_linearize(skb) < 0)
  863. return NET_RX_DROP;
  864. if (unlikely(!pskb_may_pull(skb, hdr_size)))
  865. return NET_RX_DROP;
  866. vis_packet = (struct vis_packet *)skb->data;
  867. ethhdr = (struct ethhdr *)skb_mac_header(skb);
  868. /* not for me */
  869. if (!is_my_mac(ethhdr->h_dest))
  870. return NET_RX_DROP;
  871. /* ignore own packets */
  872. if (is_my_mac(vis_packet->vis_orig))
  873. return NET_RX_DROP;
  874. if (is_my_mac(vis_packet->sender_orig))
  875. return NET_RX_DROP;
  876. switch (vis_packet->vis_type) {
  877. case VIS_TYPE_SERVER_SYNC:
  878. receive_server_sync_packet(bat_priv, vis_packet,
  879. skb_headlen(skb));
  880. break;
  881. case VIS_TYPE_CLIENT_UPDATE:
  882. receive_client_update_packet(bat_priv, vis_packet,
  883. skb_headlen(skb));
  884. break;
  885. default: /* ignore unknown packet */
  886. break;
  887. }
  888. /* We take a copy of the data in the packet, so we should
  889. always free the skbuf. */
  890. return NET_RX_DROP;
  891. }