gateway_client.c 13 KB

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