gateway_client.c 14 KB

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