gateway_client.c 13 KB

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