icmp_socket.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  1. /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors:
  2. *
  3. * Marek Lindner
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  17. * 02110-1301, USA
  18. */
  19. #include "main.h"
  20. #include <linux/debugfs.h>
  21. #include <linux/slab.h>
  22. #include "icmp_socket.h"
  23. #include "send.h"
  24. #include "hash.h"
  25. #include "originator.h"
  26. #include "hard-interface.h"
  27. static struct socket_client *socket_client_hash[256];
  28. static void bat_socket_add_packet(struct socket_client *socket_client,
  29. struct icmp_packet_rr *icmp_packet,
  30. size_t icmp_len);
  31. void batadv_socket_init(void)
  32. {
  33. memset(socket_client_hash, 0, sizeof(socket_client_hash));
  34. }
  35. static int bat_socket_open(struct inode *inode, struct file *file)
  36. {
  37. unsigned int i;
  38. struct socket_client *socket_client;
  39. nonseekable_open(inode, file);
  40. socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
  41. if (!socket_client)
  42. return -ENOMEM;
  43. for (i = 0; i < ARRAY_SIZE(socket_client_hash); i++) {
  44. if (!socket_client_hash[i]) {
  45. socket_client_hash[i] = socket_client;
  46. break;
  47. }
  48. }
  49. if (i == ARRAY_SIZE(socket_client_hash)) {
  50. pr_err("Error - can't add another packet client: maximum number of clients reached\n");
  51. kfree(socket_client);
  52. return -EXFULL;
  53. }
  54. INIT_LIST_HEAD(&socket_client->queue_list);
  55. socket_client->queue_len = 0;
  56. socket_client->index = i;
  57. socket_client->bat_priv = inode->i_private;
  58. spin_lock_init(&socket_client->lock);
  59. init_waitqueue_head(&socket_client->queue_wait);
  60. file->private_data = socket_client;
  61. batadv_inc_module_count();
  62. return 0;
  63. }
  64. static int bat_socket_release(struct inode *inode, struct file *file)
  65. {
  66. struct socket_client *socket_client = file->private_data;
  67. struct socket_packet *socket_packet;
  68. struct list_head *list_pos, *list_pos_tmp;
  69. spin_lock_bh(&socket_client->lock);
  70. /* for all packets in the queue ... */
  71. list_for_each_safe(list_pos, list_pos_tmp, &socket_client->queue_list) {
  72. socket_packet = list_entry(list_pos,
  73. struct socket_packet, list);
  74. list_del(list_pos);
  75. kfree(socket_packet);
  76. }
  77. socket_client_hash[socket_client->index] = NULL;
  78. spin_unlock_bh(&socket_client->lock);
  79. kfree(socket_client);
  80. batadv_dec_module_count();
  81. return 0;
  82. }
  83. static ssize_t bat_socket_read(struct file *file, char __user *buf,
  84. size_t count, loff_t *ppos)
  85. {
  86. struct socket_client *socket_client = file->private_data;
  87. struct socket_packet *socket_packet;
  88. size_t packet_len;
  89. int error;
  90. if ((file->f_flags & O_NONBLOCK) && (socket_client->queue_len == 0))
  91. return -EAGAIN;
  92. if ((!buf) || (count < sizeof(struct icmp_packet)))
  93. return -EINVAL;
  94. if (!access_ok(VERIFY_WRITE, buf, count))
  95. return -EFAULT;
  96. error = wait_event_interruptible(socket_client->queue_wait,
  97. socket_client->queue_len);
  98. if (error)
  99. return error;
  100. spin_lock_bh(&socket_client->lock);
  101. socket_packet = list_first_entry(&socket_client->queue_list,
  102. struct socket_packet, list);
  103. list_del(&socket_packet->list);
  104. socket_client->queue_len--;
  105. spin_unlock_bh(&socket_client->lock);
  106. packet_len = min(count, socket_packet->icmp_len);
  107. error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
  108. kfree(socket_packet);
  109. if (error)
  110. return -EFAULT;
  111. return packet_len;
  112. }
  113. static ssize_t bat_socket_write(struct file *file, const char __user *buff,
  114. size_t len, loff_t *off)
  115. {
  116. struct socket_client *socket_client = file->private_data;
  117. struct bat_priv *bat_priv = socket_client->bat_priv;
  118. struct hard_iface *primary_if = NULL;
  119. struct sk_buff *skb;
  120. struct icmp_packet_rr *icmp_packet;
  121. struct orig_node *orig_node = NULL;
  122. struct neigh_node *neigh_node = NULL;
  123. size_t packet_len = sizeof(struct icmp_packet);
  124. if (len < sizeof(struct icmp_packet)) {
  125. bat_dbg(DBG_BATMAN, bat_priv,
  126. "Error - can't send packet from char device: invalid packet size\n");
  127. return -EINVAL;
  128. }
  129. primary_if = batadv_primary_if_get_selected(bat_priv);
  130. if (!primary_if) {
  131. len = -EFAULT;
  132. goto out;
  133. }
  134. if (len >= sizeof(struct icmp_packet_rr))
  135. packet_len = sizeof(struct icmp_packet_rr);
  136. skb = dev_alloc_skb(packet_len + ETH_HLEN);
  137. if (!skb) {
  138. len = -ENOMEM;
  139. goto out;
  140. }
  141. skb_reserve(skb, ETH_HLEN);
  142. icmp_packet = (struct icmp_packet_rr *)skb_put(skb, packet_len);
  143. if (copy_from_user(icmp_packet, buff, packet_len)) {
  144. len = -EFAULT;
  145. goto free_skb;
  146. }
  147. if (icmp_packet->header.packet_type != BAT_ICMP) {
  148. bat_dbg(DBG_BATMAN, bat_priv,
  149. "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
  150. len = -EINVAL;
  151. goto free_skb;
  152. }
  153. if (icmp_packet->msg_type != ECHO_REQUEST) {
  154. bat_dbg(DBG_BATMAN, bat_priv,
  155. "Error - can't send packet from char device: got bogus message type (expected: ECHO_REQUEST)\n");
  156. len = -EINVAL;
  157. goto free_skb;
  158. }
  159. icmp_packet->uid = socket_client->index;
  160. if (icmp_packet->header.version != COMPAT_VERSION) {
  161. icmp_packet->msg_type = PARAMETER_PROBLEM;
  162. icmp_packet->header.version = COMPAT_VERSION;
  163. bat_socket_add_packet(socket_client, icmp_packet, packet_len);
  164. goto free_skb;
  165. }
  166. if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
  167. goto dst_unreach;
  168. orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
  169. if (!orig_node)
  170. goto dst_unreach;
  171. neigh_node = batadv_orig_node_get_router(orig_node);
  172. if (!neigh_node)
  173. goto dst_unreach;
  174. if (!neigh_node->if_incoming)
  175. goto dst_unreach;
  176. if (neigh_node->if_incoming->if_status != IF_ACTIVE)
  177. goto dst_unreach;
  178. memcpy(icmp_packet->orig,
  179. primary_if->net_dev->dev_addr, ETH_ALEN);
  180. if (packet_len == sizeof(struct icmp_packet_rr))
  181. memcpy(icmp_packet->rr,
  182. neigh_node->if_incoming->net_dev->dev_addr, ETH_ALEN);
  183. batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  184. goto out;
  185. dst_unreach:
  186. icmp_packet->msg_type = DESTINATION_UNREACHABLE;
  187. bat_socket_add_packet(socket_client, icmp_packet, packet_len);
  188. free_skb:
  189. kfree_skb(skb);
  190. out:
  191. if (primary_if)
  192. batadv_hardif_free_ref(primary_if);
  193. if (neigh_node)
  194. batadv_neigh_node_free_ref(neigh_node);
  195. if (orig_node)
  196. batadv_orig_node_free_ref(orig_node);
  197. return len;
  198. }
  199. static unsigned int bat_socket_poll(struct file *file, poll_table *wait)
  200. {
  201. struct socket_client *socket_client = file->private_data;
  202. poll_wait(file, &socket_client->queue_wait, wait);
  203. if (socket_client->queue_len > 0)
  204. return POLLIN | POLLRDNORM;
  205. return 0;
  206. }
  207. static const struct file_operations fops = {
  208. .owner = THIS_MODULE,
  209. .open = bat_socket_open,
  210. .release = bat_socket_release,
  211. .read = bat_socket_read,
  212. .write = bat_socket_write,
  213. .poll = bat_socket_poll,
  214. .llseek = no_llseek,
  215. };
  216. int batadv_socket_setup(struct bat_priv *bat_priv)
  217. {
  218. struct dentry *d;
  219. if (!bat_priv->debug_dir)
  220. goto err;
  221. d = debugfs_create_file(ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
  222. bat_priv->debug_dir, bat_priv, &fops);
  223. if (!d)
  224. goto err;
  225. return 0;
  226. err:
  227. return -ENOMEM;
  228. }
  229. static void bat_socket_add_packet(struct socket_client *socket_client,
  230. struct icmp_packet_rr *icmp_packet,
  231. size_t icmp_len)
  232. {
  233. struct socket_packet *socket_packet;
  234. socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
  235. if (!socket_packet)
  236. return;
  237. INIT_LIST_HEAD(&socket_packet->list);
  238. memcpy(&socket_packet->icmp_packet, icmp_packet, icmp_len);
  239. socket_packet->icmp_len = icmp_len;
  240. spin_lock_bh(&socket_client->lock);
  241. /* while waiting for the lock the socket_client could have been
  242. * deleted
  243. */
  244. if (!socket_client_hash[icmp_packet->uid]) {
  245. spin_unlock_bh(&socket_client->lock);
  246. kfree(socket_packet);
  247. return;
  248. }
  249. list_add_tail(&socket_packet->list, &socket_client->queue_list);
  250. socket_client->queue_len++;
  251. if (socket_client->queue_len > 100) {
  252. socket_packet = list_first_entry(&socket_client->queue_list,
  253. struct socket_packet, list);
  254. list_del(&socket_packet->list);
  255. kfree(socket_packet);
  256. socket_client->queue_len--;
  257. }
  258. spin_unlock_bh(&socket_client->lock);
  259. wake_up(&socket_client->queue_wait);
  260. }
  261. void batadv_socket_receive_packet(struct icmp_packet_rr *icmp_packet,
  262. size_t icmp_len)
  263. {
  264. struct socket_client *hash = socket_client_hash[icmp_packet->uid];
  265. if (hash)
  266. bat_socket_add_packet(hash, icmp_packet, icmp_len);
  267. }