gateway_client.c 12 KB

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