gateway_client.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. /*
  2. * Copyright (C) 2009-2011 B.A.T.M.A.N. contributors:
  3. *
  4. * Marek Lindner
  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 "gateway_client.h"
  23. #include "gateway_common.h"
  24. #include "hard-interface.h"
  25. #include "originator.h"
  26. #include <linux/ip.h>
  27. #include <linux/ipv6.h>
  28. #include <linux/udp.h>
  29. #include <linux/if_vlan.h>
  30. static void gw_node_free_ref(struct gw_node *gw_node)
  31. {
  32. if (atomic_dec_and_test(&gw_node->refcount))
  33. kfree_rcu(gw_node, rcu);
  34. }
  35. static struct gw_node *gw_get_selected_gw_node(struct bat_priv *bat_priv)
  36. {
  37. struct gw_node *gw_node;
  38. rcu_read_lock();
  39. gw_node = rcu_dereference(bat_priv->curr_gw);
  40. if (!gw_node)
  41. goto out;
  42. if (!atomic_inc_not_zero(&gw_node->refcount))
  43. gw_node = NULL;
  44. out:
  45. rcu_read_unlock();
  46. return gw_node;
  47. }
  48. struct orig_node *gw_get_selected_orig(struct bat_priv *bat_priv)
  49. {
  50. struct gw_node *gw_node;
  51. struct orig_node *orig_node = NULL;
  52. gw_node = gw_get_selected_gw_node(bat_priv);
  53. if (!gw_node)
  54. goto out;
  55. rcu_read_lock();
  56. orig_node = gw_node->orig_node;
  57. if (!orig_node)
  58. goto unlock;
  59. if (!atomic_inc_not_zero(&orig_node->refcount))
  60. orig_node = NULL;
  61. unlock:
  62. rcu_read_unlock();
  63. out:
  64. if (gw_node)
  65. gw_node_free_ref(gw_node);
  66. return orig_node;
  67. }
  68. static void gw_select(struct bat_priv *bat_priv, struct gw_node *new_gw_node)
  69. {
  70. struct gw_node *curr_gw_node;
  71. spin_lock_bh(&bat_priv->gw_list_lock);
  72. if (new_gw_node && !atomic_inc_not_zero(&new_gw_node->refcount))
  73. new_gw_node = NULL;
  74. curr_gw_node = rcu_dereference_protected(bat_priv->curr_gw, 1);
  75. rcu_assign_pointer(bat_priv->curr_gw, new_gw_node);
  76. if (curr_gw_node)
  77. gw_node_free_ref(curr_gw_node);
  78. spin_unlock_bh(&bat_priv->gw_list_lock);
  79. }
  80. void gw_deselect(struct bat_priv *bat_priv)
  81. {
  82. gw_select(bat_priv, NULL);
  83. }
  84. void gw_election(struct bat_priv *bat_priv)
  85. {
  86. struct hlist_node *node;
  87. struct gw_node *gw_node, *curr_gw = NULL, *curr_gw_tmp = NULL;
  88. struct neigh_node *router;
  89. uint8_t max_tq = 0;
  90. uint32_t max_gw_factor = 0, tmp_gw_factor = 0;
  91. int down, up;
  92. /**
  93. * The batman daemon checks here if we already passed a full originator
  94. * cycle in order to make sure we don't choose the first gateway we
  95. * hear about. This check is based on the daemon's uptime which we
  96. * don't have.
  97. **/
  98. if (atomic_read(&bat_priv->gw_mode) != GW_MODE_CLIENT)
  99. return;
  100. curr_gw = gw_get_selected_gw_node(bat_priv);
  101. if (curr_gw)
  102. goto out;
  103. rcu_read_lock();
  104. if (hlist_empty(&bat_priv->gw_list)) {
  105. bat_dbg(DBG_BATMAN, bat_priv,
  106. "Removing selected gateway - "
  107. "no gateway in range\n");
  108. gw_deselect(bat_priv);
  109. goto unlock;
  110. }
  111. hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
  112. if (gw_node->deleted)
  113. continue;
  114. router = orig_node_get_router(gw_node->orig_node);
  115. if (!router)
  116. continue;
  117. switch (atomic_read(&bat_priv->gw_sel_class)) {
  118. case 1: /* fast connection */
  119. gw_bandwidth_to_kbit(gw_node->orig_node->gw_flags,
  120. &down, &up);
  121. tmp_gw_factor = (router->tq_avg * router->tq_avg *
  122. down * 100 * 100) /
  123. (TQ_LOCAL_WINDOW_SIZE *
  124. TQ_LOCAL_WINDOW_SIZE * 64);
  125. if ((tmp_gw_factor > max_gw_factor) ||
  126. ((tmp_gw_factor == max_gw_factor) &&
  127. (router->tq_avg > max_tq)))
  128. curr_gw_tmp = gw_node;
  129. break;
  130. default: /**
  131. * 2: stable connection (use best statistic)
  132. * 3: fast-switch (use best statistic but change as
  133. * soon as a better gateway appears)
  134. * XX: late-switch (use best statistic but change as
  135. * soon as a better gateway appears which has
  136. * $routing_class more tq points)
  137. **/
  138. if (router->tq_avg > max_tq)
  139. curr_gw_tmp = gw_node;
  140. break;
  141. }
  142. if (router->tq_avg > max_tq)
  143. max_tq = router->tq_avg;
  144. if (tmp_gw_factor > max_gw_factor)
  145. max_gw_factor = tmp_gw_factor;
  146. neigh_node_free_ref(router);
  147. }
  148. if (curr_gw != curr_gw_tmp) {
  149. router = orig_node_get_router(curr_gw_tmp->orig_node);
  150. if (!router)
  151. goto unlock;
  152. if ((curr_gw) && (!curr_gw_tmp))
  153. bat_dbg(DBG_BATMAN, bat_priv,
  154. "Removing selected gateway - "
  155. "no gateway in range\n");
  156. else if ((!curr_gw) && (curr_gw_tmp))
  157. bat_dbg(DBG_BATMAN, bat_priv,
  158. "Adding route to gateway %pM "
  159. "(gw_flags: %i, tq: %i)\n",
  160. curr_gw_tmp->orig_node->orig,
  161. curr_gw_tmp->orig_node->gw_flags,
  162. router->tq_avg);
  163. else
  164. bat_dbg(DBG_BATMAN, bat_priv,
  165. "Changing route to gateway %pM "
  166. "(gw_flags: %i, tq: %i)\n",
  167. curr_gw_tmp->orig_node->orig,
  168. curr_gw_tmp->orig_node->gw_flags,
  169. router->tq_avg);
  170. neigh_node_free_ref(router);
  171. gw_select(bat_priv, curr_gw_tmp);
  172. }
  173. unlock:
  174. rcu_read_unlock();
  175. out:
  176. if (curr_gw)
  177. gw_node_free_ref(curr_gw);
  178. }
  179. void gw_check_election(struct bat_priv *bat_priv, struct orig_node *orig_node)
  180. {
  181. struct orig_node *curr_gw_orig;
  182. struct neigh_node *router_gw = NULL, *router_orig = NULL;
  183. uint8_t gw_tq_avg, orig_tq_avg;
  184. curr_gw_orig = gw_get_selected_orig(bat_priv);
  185. if (!curr_gw_orig)
  186. goto deselect;
  187. router_gw = orig_node_get_router(curr_gw_orig);
  188. if (!router_gw)
  189. goto deselect;
  190. /* this node already is the gateway */
  191. if (curr_gw_orig == orig_node)
  192. goto out;
  193. router_orig = orig_node_get_router(orig_node);
  194. if (!router_orig)
  195. goto out;
  196. gw_tq_avg = router_gw->tq_avg;
  197. orig_tq_avg = router_orig->tq_avg;
  198. /* the TQ value has to be better */
  199. if (orig_tq_avg < gw_tq_avg)
  200. goto out;
  201. /**
  202. * if the routing class is greater than 3 the value tells us how much
  203. * greater the TQ value of the new gateway must be
  204. **/
  205. if ((atomic_read(&bat_priv->gw_sel_class) > 3) &&
  206. (orig_tq_avg - gw_tq_avg < atomic_read(&bat_priv->gw_sel_class)))
  207. goto out;
  208. bat_dbg(DBG_BATMAN, bat_priv,
  209. "Restarting gateway selection: better gateway found (tq curr: "
  210. "%i, tq new: %i)\n",
  211. gw_tq_avg, orig_tq_avg);
  212. deselect:
  213. gw_deselect(bat_priv);
  214. out:
  215. if (curr_gw_orig)
  216. orig_node_free_ref(curr_gw_orig);
  217. if (router_gw)
  218. neigh_node_free_ref(router_gw);
  219. if (router_orig)
  220. neigh_node_free_ref(router_orig);
  221. return;
  222. }
  223. static void gw_node_add(struct bat_priv *bat_priv,
  224. struct orig_node *orig_node, uint8_t new_gwflags)
  225. {
  226. struct gw_node *gw_node;
  227. int down, up;
  228. gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
  229. if (!gw_node)
  230. return;
  231. INIT_HLIST_NODE(&gw_node->list);
  232. gw_node->orig_node = orig_node;
  233. atomic_set(&gw_node->refcount, 1);
  234. spin_lock_bh(&bat_priv->gw_list_lock);
  235. hlist_add_head_rcu(&gw_node->list, &bat_priv->gw_list);
  236. spin_unlock_bh(&bat_priv->gw_list_lock);
  237. gw_bandwidth_to_kbit(new_gwflags, &down, &up);
  238. bat_dbg(DBG_BATMAN, bat_priv,
  239. "Found new gateway %pM -> gw_class: %i - %i%s/%i%s\n",
  240. orig_node->orig, new_gwflags,
  241. (down > 2048 ? down / 1024 : down),
  242. (down > 2048 ? "MBit" : "KBit"),
  243. (up > 2048 ? up / 1024 : up),
  244. (up > 2048 ? "MBit" : "KBit"));
  245. }
  246. void gw_node_update(struct bat_priv *bat_priv,
  247. struct orig_node *orig_node, uint8_t new_gwflags)
  248. {
  249. struct hlist_node *node;
  250. struct gw_node *gw_node, *curr_gw;
  251. /**
  252. * Note: We don't need a NULL check here, since curr_gw never gets
  253. * dereferenced. If curr_gw is NULL we also should not exit as we may
  254. * have this gateway in our list (duplication check!) even though we
  255. * have no currently selected gateway.
  256. */
  257. curr_gw = gw_get_selected_gw_node(bat_priv);
  258. rcu_read_lock();
  259. hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
  260. if (gw_node->orig_node != orig_node)
  261. continue;
  262. bat_dbg(DBG_BATMAN, bat_priv,
  263. "Gateway class of originator %pM changed from "
  264. "%i to %i\n",
  265. orig_node->orig, gw_node->orig_node->gw_flags,
  266. new_gwflags);
  267. gw_node->deleted = 0;
  268. if (new_gwflags == NO_FLAGS) {
  269. gw_node->deleted = jiffies;
  270. bat_dbg(DBG_BATMAN, bat_priv,
  271. "Gateway %pM removed from gateway list\n",
  272. orig_node->orig);
  273. if (gw_node == curr_gw)
  274. goto deselect;
  275. }
  276. goto unlock;
  277. }
  278. if (new_gwflags == NO_FLAGS)
  279. goto unlock;
  280. gw_node_add(bat_priv, orig_node, new_gwflags);
  281. goto unlock;
  282. deselect:
  283. gw_deselect(bat_priv);
  284. unlock:
  285. rcu_read_unlock();
  286. if (curr_gw)
  287. gw_node_free_ref(curr_gw);
  288. }
  289. void gw_node_delete(struct bat_priv *bat_priv, struct orig_node *orig_node)
  290. {
  291. gw_node_update(bat_priv, orig_node, 0);
  292. }
  293. void gw_node_purge(struct bat_priv *bat_priv)
  294. {
  295. struct gw_node *gw_node, *curr_gw;
  296. struct hlist_node *node, *node_tmp;
  297. unsigned long timeout = 2 * PURGE_TIMEOUT * HZ;
  298. char do_deselect = 0;
  299. curr_gw = gw_get_selected_gw_node(bat_priv);
  300. spin_lock_bh(&bat_priv->gw_list_lock);
  301. hlist_for_each_entry_safe(gw_node, node, node_tmp,
  302. &bat_priv->gw_list, list) {
  303. if (((!gw_node->deleted) ||
  304. (time_before(jiffies, gw_node->deleted + timeout))) &&
  305. atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE)
  306. continue;
  307. if (curr_gw == gw_node)
  308. do_deselect = 1;
  309. hlist_del_rcu(&gw_node->list);
  310. gw_node_free_ref(gw_node);
  311. }
  312. spin_unlock_bh(&bat_priv->gw_list_lock);
  313. /* gw_deselect() needs to acquire the gw_list_lock */
  314. if (do_deselect)
  315. gw_deselect(bat_priv);
  316. if (curr_gw)
  317. gw_node_free_ref(curr_gw);
  318. }
  319. /**
  320. * fails if orig_node has no router
  321. */
  322. static int _write_buffer_text(struct bat_priv *bat_priv, struct seq_file *seq,
  323. const struct gw_node *gw_node)
  324. {
  325. struct gw_node *curr_gw;
  326. struct neigh_node *router;
  327. int down, up, ret = -1;
  328. gw_bandwidth_to_kbit(gw_node->orig_node->gw_flags, &down, &up);
  329. router = orig_node_get_router(gw_node->orig_node);
  330. if (!router)
  331. goto out;
  332. curr_gw = gw_get_selected_gw_node(bat_priv);
  333. ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %3i - %i%s/%i%s\n",
  334. (curr_gw == gw_node ? "=>" : " "),
  335. gw_node->orig_node->orig,
  336. router->tq_avg, router->addr,
  337. router->if_incoming->net_dev->name,
  338. gw_node->orig_node->gw_flags,
  339. (down > 2048 ? down / 1024 : down),
  340. (down > 2048 ? "MBit" : "KBit"),
  341. (up > 2048 ? up / 1024 : up),
  342. (up > 2048 ? "MBit" : "KBit"));
  343. neigh_node_free_ref(router);
  344. if (curr_gw)
  345. gw_node_free_ref(curr_gw);
  346. out:
  347. return ret;
  348. }
  349. int gw_client_seq_print_text(struct seq_file *seq, void *offset)
  350. {
  351. struct net_device *net_dev = (struct net_device *)seq->private;
  352. struct bat_priv *bat_priv = netdev_priv(net_dev);
  353. struct hard_iface *primary_if;
  354. struct gw_node *gw_node;
  355. struct hlist_node *node;
  356. int gw_count = 0, ret = 0;
  357. primary_if = primary_if_get_selected(bat_priv);
  358. if (!primary_if) {
  359. ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
  360. "specify interfaces to enable it\n",
  361. net_dev->name);
  362. goto out;
  363. }
  364. if (primary_if->if_status != IF_ACTIVE) {
  365. ret = seq_printf(seq, "BATMAN mesh %s disabled - "
  366. "primary interface not active\n",
  367. net_dev->name);
  368. goto out;
  369. }
  370. seq_printf(seq, " %-12s (%s/%i) %17s [%10s]: gw_class ... "
  371. "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
  372. "Gateway", "#", TQ_MAX_VALUE, "Nexthop",
  373. "outgoingIF", SOURCE_VERSION, REVISION_VERSION_STR,
  374. primary_if->net_dev->name,
  375. primary_if->net_dev->dev_addr, net_dev->name);
  376. rcu_read_lock();
  377. hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
  378. if (gw_node->deleted)
  379. continue;
  380. /* fails if orig_node has no router */
  381. if (_write_buffer_text(bat_priv, seq, gw_node) < 0)
  382. continue;
  383. gw_count++;
  384. }
  385. rcu_read_unlock();
  386. if (gw_count == 0)
  387. seq_printf(seq, "No gateways in range ...\n");
  388. out:
  389. if (primary_if)
  390. hardif_free_ref(primary_if);
  391. return ret;
  392. }
  393. int gw_is_target(struct bat_priv *bat_priv, struct sk_buff *skb)
  394. {
  395. struct ethhdr *ethhdr;
  396. struct iphdr *iphdr;
  397. struct ipv6hdr *ipv6hdr;
  398. struct udphdr *udphdr;
  399. struct gw_node *curr_gw;
  400. unsigned int header_len = 0;
  401. if (atomic_read(&bat_priv->gw_mode) == GW_MODE_OFF)
  402. return 0;
  403. /* check for ethernet header */
  404. if (!pskb_may_pull(skb, header_len + ETH_HLEN))
  405. return 0;
  406. ethhdr = (struct ethhdr *)skb->data;
  407. header_len += ETH_HLEN;
  408. /* check for initial vlan header */
  409. if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
  410. if (!pskb_may_pull(skb, header_len + VLAN_HLEN))
  411. return 0;
  412. ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN);
  413. header_len += VLAN_HLEN;
  414. }
  415. /* check for ip header */
  416. switch (ntohs(ethhdr->h_proto)) {
  417. case ETH_P_IP:
  418. if (!pskb_may_pull(skb, header_len + sizeof(*iphdr)))
  419. return 0;
  420. iphdr = (struct iphdr *)(skb->data + header_len);
  421. header_len += iphdr->ihl * 4;
  422. /* check for udp header */
  423. if (iphdr->protocol != IPPROTO_UDP)
  424. return 0;
  425. break;
  426. case ETH_P_IPV6:
  427. if (!pskb_may_pull(skb, header_len + sizeof(*ipv6hdr)))
  428. return 0;
  429. ipv6hdr = (struct ipv6hdr *)(skb->data + header_len);
  430. header_len += sizeof(*ipv6hdr);
  431. /* check for udp header */
  432. if (ipv6hdr->nexthdr != IPPROTO_UDP)
  433. return 0;
  434. break;
  435. default:
  436. return 0;
  437. }
  438. if (!pskb_may_pull(skb, header_len + sizeof(*udphdr)))
  439. return 0;
  440. udphdr = (struct udphdr *)(skb->data + header_len);
  441. header_len += sizeof(*udphdr);
  442. /* check for bootp port */
  443. if ((ntohs(ethhdr->h_proto) == ETH_P_IP) &&
  444. (ntohs(udphdr->dest) != 67))
  445. return 0;
  446. if ((ntohs(ethhdr->h_proto) == ETH_P_IPV6) &&
  447. (ntohs(udphdr->dest) != 547))
  448. return 0;
  449. if (atomic_read(&bat_priv->gw_mode) == GW_MODE_SERVER)
  450. return -1;
  451. curr_gw = gw_get_selected_gw_node(bat_priv);
  452. if (!curr_gw)
  453. return 0;
  454. if (curr_gw)
  455. gw_node_free_ref(curr_gw);
  456. return 1;
  457. }