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_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. curr_gw = gw_get_selected_gw_node(bat_priv);
  259. if (!curr_gw)
  260. goto out;
  261. rcu_read_lock();
  262. hlist_for_each_entry_rcu(gw_node, node, &bat_priv->gw_list, list) {
  263. if (gw_node->orig_node != orig_node)
  264. continue;
  265. bat_dbg(DBG_BATMAN, bat_priv,
  266. "Gateway class of originator %pM changed from "
  267. "%i to %i\n",
  268. orig_node->orig, gw_node->orig_node->gw_flags,
  269. new_gwflags);
  270. gw_node->deleted = 0;
  271. if (new_gwflags == 0) {
  272. gw_node->deleted = jiffies;
  273. bat_dbg(DBG_BATMAN, bat_priv,
  274. "Gateway %pM removed from gateway list\n",
  275. orig_node->orig);
  276. if (gw_node == curr_gw)
  277. goto deselect;
  278. }
  279. goto unlock;
  280. }
  281. if (new_gwflags == 0)
  282. goto unlock;
  283. gw_node_add(bat_priv, orig_node, new_gwflags);
  284. goto unlock;
  285. deselect:
  286. gw_deselect(bat_priv);
  287. unlock:
  288. rcu_read_unlock();
  289. out:
  290. if (curr_gw)
  291. gw_node_free_ref(curr_gw);
  292. }
  293. void gw_node_delete(struct bat_priv *bat_priv, struct orig_node *orig_node)
  294. {
  295. return gw_node_update(bat_priv, orig_node, 0);
  296. }
  297. void gw_node_purge(struct bat_priv *bat_priv)
  298. {
  299. struct gw_node *gw_node, *curr_gw;
  300. struct hlist_node *node, *node_tmp;
  301. unsigned long timeout = 2 * PURGE_TIMEOUT * HZ;
  302. char do_deselect = 0;
  303. curr_gw = gw_get_selected_gw_node(bat_priv);
  304. spin_lock_bh(&bat_priv->gw_list_lock);
  305. hlist_for_each_entry_safe(gw_node, node, node_tmp,
  306. &bat_priv->gw_list, list) {
  307. if (((!gw_node->deleted) ||
  308. (time_before(jiffies, gw_node->deleted + timeout))) &&
  309. atomic_read(&bat_priv->mesh_state) == MESH_ACTIVE)
  310. continue;
  311. if (curr_gw == gw_node)
  312. do_deselect = 1;
  313. hlist_del_rcu(&gw_node->list);
  314. gw_node_free_ref(gw_node);
  315. }
  316. spin_unlock_bh(&bat_priv->gw_list_lock);
  317. /* gw_deselect() needs to acquire the gw_list_lock */
  318. if (do_deselect)
  319. gw_deselect(bat_priv);
  320. if (curr_gw)
  321. gw_node_free_ref(curr_gw);
  322. }
  323. /**
  324. * fails if orig_node has no router
  325. */
  326. static int _write_buffer_text(struct bat_priv *bat_priv,
  327. struct seq_file *seq, struct gw_node *gw_node)
  328. {
  329. struct gw_node *curr_gw;
  330. struct neigh_node *router;
  331. int down, up, ret = -1;
  332. gw_bandwidth_to_kbit(gw_node->orig_node->gw_flags, &down, &up);
  333. router = orig_node_get_router(gw_node->orig_node);
  334. if (!router)
  335. goto out;
  336. curr_gw = gw_get_selected_gw_node(bat_priv);
  337. ret = seq_printf(seq, "%s %pM (%3i) %pM [%10s]: %3i - %i%s/%i%s\n",
  338. (curr_gw == gw_node ? "=>" : " "),
  339. gw_node->orig_node->orig,
  340. router->tq_avg, router->addr,
  341. router->if_incoming->net_dev->name,
  342. gw_node->orig_node->gw_flags,
  343. (down > 2048 ? down / 1024 : down),
  344. (down > 2048 ? "MBit" : "KBit"),
  345. (up > 2048 ? up / 1024 : up),
  346. (up > 2048 ? "MBit" : "KBit"));
  347. neigh_node_free_ref(router);
  348. if (curr_gw)
  349. gw_node_free_ref(curr_gw);
  350. out:
  351. return ret;
  352. }
  353. int gw_client_seq_print_text(struct seq_file *seq, void *offset)
  354. {
  355. struct net_device *net_dev = (struct net_device *)seq->private;
  356. struct bat_priv *bat_priv = netdev_priv(net_dev);
  357. struct gw_node *gw_node;
  358. struct hlist_node *node;
  359. int gw_count = 0;
  360. if (!bat_priv->primary_if) {
  361. return seq_printf(seq, "BATMAN mesh %s disabled - please "
  362. "specify interfaces to enable it\n",
  363. net_dev->name);
  364. }
  365. if (bat_priv->primary_if->if_status != IF_ACTIVE) {
  366. return seq_printf(seq, "BATMAN mesh %s disabled - "
  367. "primary interface not active\n",
  368. net_dev->name);
  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. bat_priv->primary_if->net_dev->name,
  375. bat_priv->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. return 0;
  389. }
  390. int gw_is_target(struct bat_priv *bat_priv, struct sk_buff *skb)
  391. {
  392. struct ethhdr *ethhdr;
  393. struct iphdr *iphdr;
  394. struct ipv6hdr *ipv6hdr;
  395. struct udphdr *udphdr;
  396. struct gw_node *curr_gw;
  397. unsigned int header_len = 0;
  398. if (atomic_read(&bat_priv->gw_mode) == GW_MODE_OFF)
  399. return 0;
  400. /* check for ethernet header */
  401. if (!pskb_may_pull(skb, header_len + ETH_HLEN))
  402. return 0;
  403. ethhdr = (struct ethhdr *)skb->data;
  404. header_len += ETH_HLEN;
  405. /* check for initial vlan header */
  406. if (ntohs(ethhdr->h_proto) == ETH_P_8021Q) {
  407. if (!pskb_may_pull(skb, header_len + VLAN_HLEN))
  408. return 0;
  409. ethhdr = (struct ethhdr *)(skb->data + VLAN_HLEN);
  410. header_len += VLAN_HLEN;
  411. }
  412. /* check for ip header */
  413. switch (ntohs(ethhdr->h_proto)) {
  414. case ETH_P_IP:
  415. if (!pskb_may_pull(skb, header_len + sizeof(struct iphdr)))
  416. return 0;
  417. iphdr = (struct iphdr *)(skb->data + header_len);
  418. header_len += iphdr->ihl * 4;
  419. /* check for udp header */
  420. if (iphdr->protocol != IPPROTO_UDP)
  421. return 0;
  422. break;
  423. case ETH_P_IPV6:
  424. if (!pskb_may_pull(skb, header_len + sizeof(struct ipv6hdr)))
  425. return 0;
  426. ipv6hdr = (struct ipv6hdr *)(skb->data + header_len);
  427. header_len += sizeof(struct ipv6hdr);
  428. /* check for udp header */
  429. if (ipv6hdr->nexthdr != IPPROTO_UDP)
  430. return 0;
  431. break;
  432. default:
  433. return 0;
  434. }
  435. if (!pskb_may_pull(skb, header_len + sizeof(struct udphdr)))
  436. return 0;
  437. udphdr = (struct udphdr *)(skb->data + header_len);
  438. header_len += sizeof(struct udphdr);
  439. /* check for bootp port */
  440. if ((ntohs(ethhdr->h_proto) == ETH_P_IP) &&
  441. (ntohs(udphdr->dest) != 67))
  442. return 0;
  443. if ((ntohs(ethhdr->h_proto) == ETH_P_IPV6) &&
  444. (ntohs(udphdr->dest) != 547))
  445. return 0;
  446. if (atomic_read(&bat_priv->gw_mode) == GW_MODE_SERVER)
  447. return -1;
  448. curr_gw = gw_get_selected_gw_node(bat_priv);
  449. if (!curr_gw)
  450. return 0;
  451. if (curr_gw)
  452. gw_node_free_ref(curr_gw);
  453. return 1;
  454. }