icmp_socket.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Copyright (C) 2007-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 <linux/debugfs.h>
  23. #include <linux/slab.h>
  24. #include "icmp_socket.h"
  25. #include "send.h"
  26. #include "hash.h"
  27. #include "originator.h"
  28. #include "hard-interface.h"
  29. static struct socket_client *socket_client_hash[256];
  30. static void bat_socket_add_packet(struct socket_client *socket_client,
  31. struct icmp_packet_rr *icmp_packet,
  32. size_t icmp_len);
  33. void bat_socket_init(void)
  34. {
  35. memset(socket_client_hash, 0, sizeof(socket_client_hash));
  36. }
  37. static int bat_socket_open(struct inode *inode, struct file *file)
  38. {
  39. unsigned int i;
  40. struct socket_client *socket_client;
  41. nonseekable_open(inode, file);
  42. socket_client = kmalloc(sizeof(struct socket_client), GFP_KERNEL);
  43. if (!socket_client)
  44. return -ENOMEM;
  45. for (i = 0; i < ARRAY_SIZE(socket_client_hash); i++) {
  46. if (!socket_client_hash[i]) {
  47. socket_client_hash[i] = socket_client;
  48. break;
  49. }
  50. }
  51. if (i == ARRAY_SIZE(socket_client_hash)) {
  52. pr_err("Error - can't add another packet client: "
  53. "maximum number of clients reached\n");
  54. kfree(socket_client);
  55. return -EXFULL;
  56. }
  57. INIT_LIST_HEAD(&socket_client->queue_list);
  58. socket_client->queue_len = 0;
  59. socket_client->index = i;
  60. socket_client->bat_priv = inode->i_private;
  61. spin_lock_init(&socket_client->lock);
  62. init_waitqueue_head(&socket_client->queue_wait);
  63. file->private_data = socket_client;
  64. inc_module_count();
  65. return 0;
  66. }
  67. static int bat_socket_release(struct inode *inode, struct file *file)
  68. {
  69. struct socket_client *socket_client = file->private_data;
  70. struct socket_packet *socket_packet;
  71. struct list_head *list_pos, *list_pos_tmp;
  72. spin_lock_bh(&socket_client->lock);
  73. /* for all packets in the queue ... */
  74. list_for_each_safe(list_pos, list_pos_tmp, &socket_client->queue_list) {
  75. socket_packet = list_entry(list_pos,
  76. struct socket_packet, list);
  77. list_del(list_pos);
  78. kfree(socket_packet);
  79. }
  80. socket_client_hash[socket_client->index] = NULL;
  81. spin_unlock_bh(&socket_client->lock);
  82. kfree(socket_client);
  83. dec_module_count();
  84. return 0;
  85. }
  86. static ssize_t bat_socket_read(struct file *file, char __user *buf,
  87. size_t count, loff_t *ppos)
  88. {
  89. struct socket_client *socket_client = file->private_data;
  90. struct socket_packet *socket_packet;
  91. size_t packet_len;
  92. int error;
  93. if ((file->f_flags & O_NONBLOCK) && (socket_client->queue_len == 0))
  94. return -EAGAIN;
  95. if ((!buf) || (count < sizeof(struct icmp_packet)))
  96. return -EINVAL;
  97. if (!access_ok(VERIFY_WRITE, buf, count))
  98. return -EFAULT;
  99. error = wait_event_interruptible(socket_client->queue_wait,
  100. socket_client->queue_len);
  101. if (error)
  102. return error;
  103. spin_lock_bh(&socket_client->lock);
  104. socket_packet = list_first_entry(&socket_client->queue_list,
  105. struct socket_packet, list);
  106. list_del(&socket_packet->list);
  107. socket_client->queue_len--;
  108. spin_unlock_bh(&socket_client->lock);
  109. error = __copy_to_user(buf, &socket_packet->icmp_packet,
  110. socket_packet->icmp_len);
  111. packet_len = socket_packet->icmp_len;
  112. kfree(socket_packet);
  113. if (error)
  114. return -EFAULT;
  115. return packet_len;
  116. }
  117. static ssize_t bat_socket_write(struct file *file, const char __user *buff,
  118. size_t len, loff_t *off)
  119. {
  120. struct socket_client *socket_client = file->private_data;
  121. struct bat_priv *bat_priv = socket_client->bat_priv;
  122. struct hard_iface *primary_if = NULL;
  123. struct sk_buff *skb;
  124. struct icmp_packet_rr *icmp_packet;
  125. struct orig_node *orig_node = NULL;
  126. struct neigh_node *neigh_node = NULL;
  127. size_t packet_len = sizeof(struct icmp_packet);
  128. if (len < sizeof(struct icmp_packet)) {
  129. bat_dbg(DBG_BATMAN, bat_priv,
  130. "Error - can't send packet from char device: "
  131. "invalid packet size\n");
  132. return -EINVAL;
  133. }
  134. primary_if = primary_if_get_selected(bat_priv);
  135. if (!primary_if) {
  136. len = -EFAULT;
  137. goto out;
  138. }
  139. if (len >= sizeof(struct icmp_packet_rr))
  140. packet_len = sizeof(struct icmp_packet_rr);
  141. skb = dev_alloc_skb(packet_len + sizeof(struct ethhdr));
  142. if (!skb) {
  143. len = -ENOMEM;
  144. goto out;
  145. }
  146. skb_reserve(skb, sizeof(struct ethhdr));
  147. icmp_packet = (struct icmp_packet_rr *)skb_put(skb, packet_len);
  148. if (!access_ok(VERIFY_READ, buff, packet_len)) {
  149. len = -EFAULT;
  150. goto free_skb;
  151. }
  152. if (__copy_from_user(icmp_packet, buff, packet_len)) {
  153. len = -EFAULT;
  154. goto free_skb;
  155. }
  156. if (icmp_packet->packet_type != BAT_ICMP) {
  157. bat_dbg(DBG_BATMAN, bat_priv,
  158. "Error - can't send packet from char device: "
  159. "got bogus packet type (expected: BAT_ICMP)\n");
  160. len = -EINVAL;
  161. goto free_skb;
  162. }
  163. if (icmp_packet->msg_type != ECHO_REQUEST) {
  164. bat_dbg(DBG_BATMAN, bat_priv,
  165. "Error - can't send packet from char device: "
  166. "got bogus message type (expected: ECHO_REQUEST)\n");
  167. len = -EINVAL;
  168. goto free_skb;
  169. }
  170. icmp_packet->uid = socket_client->index;
  171. if (icmp_packet->version != COMPAT_VERSION) {
  172. icmp_packet->msg_type = PARAMETER_PROBLEM;
  173. icmp_packet->ttl = COMPAT_VERSION;
  174. bat_socket_add_packet(socket_client, icmp_packet, packet_len);
  175. goto free_skb;
  176. }
  177. if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
  178. goto dst_unreach;
  179. orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
  180. if (!orig_node)
  181. goto dst_unreach;
  182. neigh_node = orig_node_get_router(orig_node);
  183. if (!neigh_node)
  184. goto dst_unreach;
  185. if (!neigh_node->if_incoming)
  186. goto dst_unreach;
  187. if (neigh_node->if_incoming->if_status != IF_ACTIVE)
  188. goto dst_unreach;
  189. memcpy(icmp_packet->orig,
  190. primary_if->net_dev->dev_addr, ETH_ALEN);
  191. if (packet_len == sizeof(struct icmp_packet_rr))
  192. memcpy(icmp_packet->rr,
  193. neigh_node->if_incoming->net_dev->dev_addr, ETH_ALEN);
  194. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  195. goto out;
  196. dst_unreach:
  197. icmp_packet->msg_type = DESTINATION_UNREACHABLE;
  198. bat_socket_add_packet(socket_client, icmp_packet, packet_len);
  199. free_skb:
  200. kfree_skb(skb);
  201. out:
  202. if (primary_if)
  203. hardif_free_ref(primary_if);
  204. if (neigh_node)
  205. neigh_node_free_ref(neigh_node);
  206. if (orig_node)
  207. orig_node_free_ref(orig_node);
  208. return len;
  209. }
  210. static unsigned int bat_socket_poll(struct file *file, poll_table *wait)
  211. {
  212. struct socket_client *socket_client = file->private_data;
  213. poll_wait(file, &socket_client->queue_wait, wait);
  214. if (socket_client->queue_len > 0)
  215. return POLLIN | POLLRDNORM;
  216. return 0;
  217. }
  218. static const struct file_operations fops = {
  219. .owner = THIS_MODULE,
  220. .open = bat_socket_open,
  221. .release = bat_socket_release,
  222. .read = bat_socket_read,
  223. .write = bat_socket_write,
  224. .poll = bat_socket_poll,
  225. .llseek = no_llseek,
  226. };
  227. int bat_socket_setup(struct bat_priv *bat_priv)
  228. {
  229. struct dentry *d;
  230. if (!bat_priv->debug_dir)
  231. goto err;
  232. d = debugfs_create_file(ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
  233. bat_priv->debug_dir, bat_priv, &fops);
  234. if (d)
  235. goto err;
  236. return 0;
  237. err:
  238. return 1;
  239. }
  240. static void bat_socket_add_packet(struct socket_client *socket_client,
  241. struct icmp_packet_rr *icmp_packet,
  242. size_t icmp_len)
  243. {
  244. struct socket_packet *socket_packet;
  245. socket_packet = kmalloc(sizeof(struct socket_packet), GFP_ATOMIC);
  246. if (!socket_packet)
  247. return;
  248. INIT_LIST_HEAD(&socket_packet->list);
  249. memcpy(&socket_packet->icmp_packet, icmp_packet, icmp_len);
  250. socket_packet->icmp_len = icmp_len;
  251. spin_lock_bh(&socket_client->lock);
  252. /* while waiting for the lock the socket_client could have been
  253. * deleted */
  254. if (!socket_client_hash[icmp_packet->uid]) {
  255. spin_unlock_bh(&socket_client->lock);
  256. kfree(socket_packet);
  257. return;
  258. }
  259. list_add_tail(&socket_packet->list, &socket_client->queue_list);
  260. socket_client->queue_len++;
  261. if (socket_client->queue_len > 100) {
  262. socket_packet = list_first_entry(&socket_client->queue_list,
  263. struct socket_packet, list);
  264. list_del(&socket_packet->list);
  265. kfree(socket_packet);
  266. socket_client->queue_len--;
  267. }
  268. spin_unlock_bh(&socket_client->lock);
  269. wake_up(&socket_client->queue_wait);
  270. }
  271. void bat_socket_receive_packet(struct icmp_packet_rr *icmp_packet,
  272. size_t icmp_len)
  273. {
  274. struct socket_client *hash = socket_client_hash[icmp_packet->uid];
  275. if (hash)
  276. bat_socket_add_packet(hash, icmp_packet, icmp_len);
  277. }