icmp_socket.c 8.6 KB

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