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 sk_buff *skb;
  123. struct icmp_packet_rr *icmp_packet;
  124. struct orig_node *orig_node;
  125. struct batman_if *batman_if;
  126. size_t packet_len = sizeof(struct icmp_packet);
  127. uint8_t dstaddr[ETH_ALEN];
  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. if (!bat_priv->primary_if)
  135. return -EFAULT;
  136. if (len >= sizeof(struct icmp_packet_rr))
  137. packet_len = sizeof(struct icmp_packet_rr);
  138. skb = dev_alloc_skb(packet_len + sizeof(struct ethhdr));
  139. if (!skb)
  140. return -ENOMEM;
  141. skb_reserve(skb, sizeof(struct ethhdr));
  142. icmp_packet = (struct icmp_packet_rr *)skb_put(skb, packet_len);
  143. if (!access_ok(VERIFY_READ, buff, packet_len)) {
  144. len = -EFAULT;
  145. goto free_skb;
  146. }
  147. if (__copy_from_user(icmp_packet, buff, packet_len)) {
  148. len = -EFAULT;
  149. goto free_skb;
  150. }
  151. if (icmp_packet->packet_type != BAT_ICMP) {
  152. bat_dbg(DBG_BATMAN, bat_priv,
  153. "Error - can't send packet from char device: "
  154. "got bogus packet type (expected: BAT_ICMP)\n");
  155. len = -EINVAL;
  156. goto free_skb;
  157. }
  158. if (icmp_packet->msg_type != ECHO_REQUEST) {
  159. bat_dbg(DBG_BATMAN, bat_priv,
  160. "Error - can't send packet from char device: "
  161. "got bogus message type (expected: ECHO_REQUEST)\n");
  162. len = -EINVAL;
  163. goto free_skb;
  164. }
  165. icmp_packet->uid = socket_client->index;
  166. if (icmp_packet->version != COMPAT_VERSION) {
  167. icmp_packet->msg_type = PARAMETER_PROBLEM;
  168. icmp_packet->ttl = COMPAT_VERSION;
  169. bat_socket_add_packet(socket_client, icmp_packet, packet_len);
  170. goto free_skb;
  171. }
  172. if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
  173. goto dst_unreach;
  174. spin_lock_bh(&bat_priv->orig_hash_lock);
  175. rcu_read_lock();
  176. orig_node = ((struct orig_node *)hash_find(bat_priv->orig_hash,
  177. compare_orig, choose_orig,
  178. icmp_packet->dst));
  179. rcu_read_unlock();
  180. if (!orig_node)
  181. goto unlock;
  182. if (!orig_node->router)
  183. goto unlock;
  184. batman_if = orig_node->router->if_incoming;
  185. memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
  186. spin_unlock_bh(&bat_priv->orig_hash_lock);
  187. if (!batman_if)
  188. goto dst_unreach;
  189. if (batman_if->if_status != IF_ACTIVE)
  190. goto dst_unreach;
  191. memcpy(icmp_packet->orig,
  192. bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN);
  193. if (packet_len == sizeof(struct icmp_packet_rr))
  194. memcpy(icmp_packet->rr, batman_if->net_dev->dev_addr, ETH_ALEN);
  195. send_skb_packet(skb, batman_if, dstaddr);
  196. goto out;
  197. unlock:
  198. spin_unlock_bh(&bat_priv->orig_hash_lock);
  199. dst_unreach:
  200. icmp_packet->msg_type = DESTINATION_UNREACHABLE;
  201. bat_socket_add_packet(socket_client, icmp_packet, packet_len);
  202. free_skb:
  203. kfree_skb(skb);
  204. out:
  205. return len;
  206. }
  207. static unsigned int bat_socket_poll(struct file *file, poll_table *wait)
  208. {
  209. struct socket_client *socket_client = file->private_data;
  210. poll_wait(file, &socket_client->queue_wait, wait);
  211. if (socket_client->queue_len > 0)
  212. return POLLIN | POLLRDNORM;
  213. return 0;
  214. }
  215. static const struct file_operations fops = {
  216. .owner = THIS_MODULE,
  217. .open = bat_socket_open,
  218. .release = bat_socket_release,
  219. .read = bat_socket_read,
  220. .write = bat_socket_write,
  221. .poll = bat_socket_poll,
  222. .llseek = no_llseek,
  223. };
  224. int bat_socket_setup(struct bat_priv *bat_priv)
  225. {
  226. struct dentry *d;
  227. if (!bat_priv->debug_dir)
  228. goto err;
  229. d = debugfs_create_file(ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
  230. bat_priv->debug_dir, bat_priv, &fops);
  231. if (d)
  232. goto err;
  233. return 0;
  234. err:
  235. return 1;
  236. }
  237. static void bat_socket_add_packet(struct socket_client *socket_client,
  238. struct icmp_packet_rr *icmp_packet,
  239. size_t icmp_len)
  240. {
  241. struct socket_packet *socket_packet;
  242. socket_packet = kmalloc(sizeof(struct socket_packet), GFP_ATOMIC);
  243. if (!socket_packet)
  244. return;
  245. INIT_LIST_HEAD(&socket_packet->list);
  246. memcpy(&socket_packet->icmp_packet, icmp_packet, icmp_len);
  247. socket_packet->icmp_len = icmp_len;
  248. spin_lock_bh(&socket_client->lock);
  249. /* while waiting for the lock the socket_client could have been
  250. * deleted */
  251. if (!socket_client_hash[icmp_packet->uid]) {
  252. spin_unlock_bh(&socket_client->lock);
  253. kfree(socket_packet);
  254. return;
  255. }
  256. list_add_tail(&socket_packet->list, &socket_client->queue_list);
  257. socket_client->queue_len++;
  258. if (socket_client->queue_len > 100) {
  259. socket_packet = list_first_entry(&socket_client->queue_list,
  260. struct socket_packet, list);
  261. list_del(&socket_packet->list);
  262. kfree(socket_packet);
  263. socket_client->queue_len--;
  264. }
  265. spin_unlock_bh(&socket_client->lock);
  266. wake_up(&socket_client->queue_wait);
  267. }
  268. void bat_socket_receive_packet(struct icmp_packet_rr *icmp_packet,
  269. size_t icmp_len)
  270. {
  271. struct socket_client *hash = socket_client_hash[icmp_packet->uid];
  272. if (hash)
  273. bat_socket_add_packet(hash, icmp_packet, icmp_len);
  274. }