gateway_client.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562
  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 = bat_priv->curr_gw;
  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 = kmalloc(sizeof(struct gw_node), GFP_ATOMIC);
  229. if (!gw_node)
  230. return;
  231. memset(gw_node, 0, sizeof(struct gw_node));
  232. INIT_HLIST_NODE(&gw_node->list);
  233. gw_node->orig_node = orig_node;
  234. atomic_set(&gw_node->refcount, 1);
  235. spin_lock_bh(&bat_priv->gw_list_lock);
  236. hlist_add_head_rcu(&gw_node->list, &bat_priv->gw_list);
  237. spin_unlock_bh(&bat_priv->gw_list_lock);
  238. gw_bandwidth_to_kbit(new_gwflags, &down, &up);
  239. bat_dbg(DBG_BATMAN, bat_priv,
  240. "Found new gateway %pM -> gw_class: %i - %i%s/%i%s\n",
  241. orig_node->orig, new_gwflags,
  242. (down > 2048 ? down / 1024 : down),
  243. (down > 2048 ? "MBit" : "KBit"),
  244. (up > 2048 ? up / 1024 : up),
  245. (up > 2048 ? "MBit" : "KBit"));
  246. }
  247. void gw_node_update(struct bat_priv *bat_priv,
  248. struct orig_node *orig_node, uint8_t new_gwflags)
  249. {
  250. struct hlist_node *node;
  251. struct gw_node *gw_node, *curr_gw;
  252. /**
  253. * Note: We don't need a NULL check here, since curr_gw never gets
  254. * dereferenced. If curr_gw is NULL we also should not exit as we may
  255. * have this gateway in our list (duplication check!) even though we
  256. * have no currently selected gateway.
  257. */
  258. curr_gw = gw_get_selected_gw_node(bat_priv);
  259. rcu_read_lock();
  260. hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
  261. if (gw_node->orig_node != orig_node)
  262. continue;
  263. bat_dbg(DBG_BATMAN, bat_priv,
  264. "Gateway class of originator %pM changed from "
  265. "%i to %i\n",
  266. orig_node->orig, gw_node->orig_node->gw_flags,
  267. new_gwflags);
  268. gw_node->deleted = 0;
  269. if (new_gwflags == 0) {
  270. gw_node->deleted = jiffies;
  271. bat_dbg(DBG_BATMAN, bat_priv,
  272. "Gateway %pM removed from gateway list\n",
  273. orig_node->orig);
  274. if (gw_node == curr_gw)
  275. goto deselect;
  276. }
  277. goto unlock;
  278. }
  279. if (new_gwflags == 0)
  280. goto unlock;
  281. gw_node_add(bat_priv, orig_node, new_gwflags);
  282. goto unlock;
  283. deselect:
  284. gw_deselect(bat_priv);
  285. unlock:
  286. rcu_read_unlock();
  287. if (curr_gw)
  288. gw_node_free_ref(curr_gw);
  289. }
  290. void gw_node_delete(struct bat_priv *bat_priv, struct orig_node *orig_node)
  291. {
  292. return gw_node_update(bat_priv, orig_node, 0);
  293. }
  294. void gw_node_purge(struct bat_priv *bat_priv)
  295. {
  296. struct gw_node *gw_node, *curr_gw;
  297. struct hlist_node *node, *node_tmp;
  298. unsigned long timeout = 2 * PURGE_TIMEOUT * HZ;
  299. char do_deselect = 0;
  300. curr_gw = gw_get_selected_gw_node(bat_priv);
  301. spin_lock_bh(&bat_priv->gw_list_lock);
  302. hlist_for_each_entry_safe(gw_node, node, node_tmp,
  303. &bat_priv->gw_list, list) {
  304. if (((!gw_node->deleted) ||
  305. (time_before(jiffies, gw_node->deleted + timeout))) &&
  306. atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE)
  307. continue;
  308. if (curr_gw == gw_node)
  309. do_deselect = 1;
  310. hlist_del_rcu(&gw_node->list);
  311. gw_node_free_ref(gw_node);
  312. }
  313. spin_unlock_bh(&bat_priv->gw_list_lock);
  314. /* gw_deselect() needs to acquire the gw_list_lock */
  315. if (do_deselect)
  316. gw_deselect(bat_priv);
  317. if (curr_gw)
  318. gw_node_free_ref(curr_gw);
  319. }
  320. /**
  321. * fails if orig_node has no router
  322. */
  323. static int _write_buffer_text(struct bat_priv *bat_priv,
  324. struct seq_file *seq, struct gw_node *gw_node)
  325. {
  326. struct gw_node *curr_gw;
  327. struct neigh_node *router;
  328. int down, up, ret = -1;
  329. gw_bandwidth_to_kbit(gw_node->orig_node->gw_flags, &down, &up);
  330. router = orig_node_get_router(gw_node->orig_node);
  331. if (!router)
  332. goto out;
  333. curr_gw = gw_get_selected_gw_node(bat_priv);
  334. ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %3i - %i%s/%i%s\n",
  335. (curr_gw == gw_node ? "=>" : " "),
  336. gw_node->orig_node->orig,
  337. router->tq_avg, router->addr,
  338. router->if_incoming->net_dev->name,
  339. gw_node->orig_node->gw_flags,
  340. (down > 2048 ? down / 1024 : down),
  341. (down > 2048 ? "MBit" : "KBit"),
  342. (up > 2048 ? up / 1024 : up),
  343. (up > 2048 ? "MBit" : "KBit"));
  344. neigh_node_free_ref(router);
  345. if (curr_gw)
  346. gw_node_free_ref(curr_gw);
  347. out:
  348. return ret;
  349. }
  350. int gw_client_seq_print_text(struct seq_file *seq, void *offset)
  351. {
  352. struct net_device *net_dev = (struct net_device *)seq->private;
  353. struct bat_priv *bat_priv = netdev_priv(net_dev);
  354. struct hard_iface *primary_if;
  355. struct gw_node *gw_node;
  356. struct hlist_node *node;
  357. int gw_count = 0, ret = 0;
  358. primary_if = primary_if_get_selected(bat_priv);
  359. if (!primary_if) {
  360. ret = seq_printf(seq, "BATMAN mesh %s disabled - please "
  361. "specify interfaces to enable it\n",
  362. net_dev->name);
  363. goto out;
  364. }
  365. if (primary_if->if_status != IF_ACTIVE) {
  366. ret = seq_printf(seq, "BATMAN mesh %s disabled - "
  367. "primary interface not active\n",
  368. net_dev->name);
  369. goto out;
  370. }
  371. seq_printf(seq, " %-12s (%s/%i) %17s [%10s]: gw_class ... "
  372. "[B.A.T.M.A.N. adv %s%s, MainIF/MAC: %s/%pM (%s)]\n",
  373. "Gateway", "#", TQ_MAX_VALUE, "Nexthop",
  374. "outgoingIF", SOURCE_VERSION, REVISION_VERSION_STR,
  375. primary_if->net_dev->name,
  376. primary_if->net_dev->dev_addr, net_dev->name);
  377. rcu_read_lock();
  378. hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
  379. if (gw_node->deleted)
  380. continue;
  381. /* fails if orig_node has no router */
  382. if (_write_buffer_text(bat_priv, seq, gw_node) < 0)
  383. continue;
  384. gw_count++;
  385. }
  386. rcu_read_unlock();
  387. if (gw_count == 0)
  388. seq_printf(seq, "No gateways in range ...\n");
  389. out:
  390. if (primary_if)
  391. hardif_free_ref(primary_if);
  392. return ret;
  393. }
  394. int gw_is_target(struct bat_priv *bat_priv, struct sk_buff *skb)
  395. {
  396. struct ethhdr *ethhdr;
  397. struct iphdr *iphdr;
  398. struct ipv6hdr *ipv6hdr;
  399. struct udphdr *udphdr;
  400. struct gw_node *curr_gw;
  401. unsigned int header_len = 0;
  402. if (atomic_read(&bat_priv->gw_mode) == GW_MODE_OFF)
  403. return 0;
  404. /* check for ethernet header */
  405. if (!pskb_may_pull(skb, header_len + ETH_HLEN))
  406. return 0;
  407. ethhdr = (struct ethhdr *)skb->data;
  408. header_len += ETH_HLEN;
  409. /* check for initial vlan header */
  410. if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
  411. if (!pskb_may_pull(skb, header_len + VLAN_HLEN))
  412. return 0;
  413. ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN);
  414. header_len += VLAN_HLEN;
  415. }
  416. /* check for ip header */
  417. switch (ntohs(ethhdr->h_proto)) {
  418. case ETH_P_IP:
  419. if (!pskb_may_pull(skb, header_len + sizeof(struct iphdr)))
  420. return 0;
  421. iphdr = (struct iphdr *)(skb->data + header_len);
  422. header_len += iphdr->ihl * 4;
  423. /* check for udp header */
  424. if (iphdr->protocol != IPPROTO_UDP)
  425. return 0;
  426. break;
  427. case ETH_P_IPV6:
  428. if (!pskb_may_pull(skb, header_len + sizeof(struct ipv6hdr)))
  429. return 0;
  430. ipv6hdr = (struct ipv6hdr *)(skb->data + header_len);
  431. header_len += sizeof(struct ipv6hdr);
  432. /* check for udp header */
  433. if (ipv6hdr->nexthdr != IPPROTO_UDP)
  434. return 0;
  435. break;
  436. default:
  437. return 0;
  438. }
  439. if (!pskb_may_pull(skb, header_len + sizeof(struct udphdr)))
  440. return 0;
  441. udphdr = (struct udphdr *)(skb->data + header_len);
  442. header_len += sizeof(struct udphdr);
  443. /* check for bootp port */
  444. if ((ntohs(ethhdr->h_proto) == ETH_P_IP) &&
  445. (ntohs(udphdr->dest) != 67))
  446. return 0;
  447. if ((ntohs(ethhdr->h_proto) == ETH_P_IPV6) &&
  448. (ntohs(udphdr->dest) != 547))
  449. return 0;
  450. if (atomic_read(&bat_priv->gw_mode) == GW_MODE_SERVER)
  451. return -1;
  452. curr_gw = gw_get_selected_gw_node(bat_priv);
  453. if (!curr_gw)
  454. return 0;
  455. if (curr_gw)
  456. gw_node_free_ref(curr_gw);
  457. return 1;
  458. }