gateway_client.c 13 KB

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