gateway_client.c 13 KB

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