icmp_socket.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  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 batadv_socket_client *batadv_socket_client_hash[256];
  28. static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
  29. struct batadv_icmp_packet_rr *icmp_packet,
  30. size_t icmp_len);
  31. void batadv_socket_init(void)
  32. {
  33. memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash));
  34. }
  35. static int batadv_socket_open(struct inode *inode, struct file *file)
  36. {
  37. unsigned int i;
  38. struct batadv_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(batadv_socket_client_hash); i++) {
  44. if (!batadv_socket_client_hash[i]) {
  45. batadv_socket_client_hash[i] = socket_client;
  46. break;
  47. }
  48. }
  49. if (i == ARRAY_SIZE(batadv_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 batadv_socket_release(struct inode *inode, struct file *file)
  65. {
  66. struct batadv_socket_client *socket_client = file->private_data;
  67. struct batadv_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 batadv_socket_packet, list);
  74. list_del(list_pos);
  75. kfree(socket_packet);
  76. }
  77. batadv_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 batadv_socket_read(struct file *file, char __user *buf,
  84. size_t count, loff_t *ppos)
  85. {
  86. struct batadv_socket_client *socket_client = file->private_data;
  87. struct batadv_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 batadv_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 batadv_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 batadv_socket_write(struct file *file, const char __user *buff,
  114. size_t len, loff_t *off)
  115. {
  116. struct batadv_socket_client *socket_client = file->private_data;
  117. struct batadv_priv *bat_priv = socket_client->bat_priv;
  118. struct batadv_hard_iface *primary_if = NULL;
  119. struct sk_buff *skb;
  120. struct batadv_icmp_packet_rr *icmp_packet;
  121. struct batadv_orig_node *orig_node = NULL;
  122. struct batadv_neigh_node *neigh_node = NULL;
  123. size_t packet_len = sizeof(struct batadv_icmp_packet);
  124. if (len < sizeof(struct batadv_icmp_packet)) {
  125. batadv_dbg(BATADV_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 batadv_icmp_packet_rr))
  135. packet_len = sizeof(struct batadv_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 batadv_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 != BATADV_ICMP) {
  148. batadv_dbg(BATADV_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 != BATADV_ECHO_REQUEST) {
  154. batadv_dbg(BATADV_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 != BATADV_COMPAT_VERSION) {
  161. icmp_packet->msg_type = BATADV_PARAMETER_PROBLEM;
  162. icmp_packet->header.version = BATADV_COMPAT_VERSION;
  163. batadv_socket_add_packet(socket_client, icmp_packet,
  164. packet_len);
  165. goto free_skb;
  166. }
  167. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  168. goto dst_unreach;
  169. orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
  170. if (!orig_node)
  171. goto dst_unreach;
  172. neigh_node = batadv_orig_node_get_router(orig_node);
  173. if (!neigh_node)
  174. goto dst_unreach;
  175. if (!neigh_node->if_incoming)
  176. goto dst_unreach;
  177. if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
  178. goto dst_unreach;
  179. memcpy(icmp_packet->orig,
  180. primary_if->net_dev->dev_addr, ETH_ALEN);
  181. if (packet_len == sizeof(struct batadv_icmp_packet_rr))
  182. memcpy(icmp_packet->rr,
  183. neigh_node->if_incoming->net_dev->dev_addr, ETH_ALEN);
  184. batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  185. goto out;
  186. dst_unreach:
  187. icmp_packet->msg_type = BATADV_DESTINATION_UNREACHABLE;
  188. batadv_socket_add_packet(socket_client, icmp_packet, packet_len);
  189. free_skb:
  190. kfree_skb(skb);
  191. out:
  192. if (primary_if)
  193. batadv_hardif_free_ref(primary_if);
  194. if (neigh_node)
  195. batadv_neigh_node_free_ref(neigh_node);
  196. if (orig_node)
  197. batadv_orig_node_free_ref(orig_node);
  198. return len;
  199. }
  200. static unsigned int batadv_socket_poll(struct file *file, poll_table *wait)
  201. {
  202. struct batadv_socket_client *socket_client = file->private_data;
  203. poll_wait(file, &socket_client->queue_wait, wait);
  204. if (socket_client->queue_len > 0)
  205. return POLLIN | POLLRDNORM;
  206. return 0;
  207. }
  208. static const struct file_operations batadv_fops = {
  209. .owner = THIS_MODULE,
  210. .open = batadv_socket_open,
  211. .release = batadv_socket_release,
  212. .read = batadv_socket_read,
  213. .write = batadv_socket_write,
  214. .poll = batadv_socket_poll,
  215. .llseek = no_llseek,
  216. };
  217. int batadv_socket_setup(struct batadv_priv *bat_priv)
  218. {
  219. struct dentry *d;
  220. if (!bat_priv->debug_dir)
  221. goto err;
  222. d = debugfs_create_file(BATADV_ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
  223. bat_priv->debug_dir, bat_priv, &batadv_fops);
  224. if (!d)
  225. goto err;
  226. return 0;
  227. err:
  228. return -ENOMEM;
  229. }
  230. static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
  231. struct batadv_icmp_packet_rr *icmp_packet,
  232. size_t icmp_len)
  233. {
  234. struct batadv_socket_packet *socket_packet;
  235. socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
  236. if (!socket_packet)
  237. return;
  238. INIT_LIST_HEAD(&socket_packet->list);
  239. memcpy(&socket_packet->icmp_packet, icmp_packet, icmp_len);
  240. socket_packet->icmp_len = icmp_len;
  241. spin_lock_bh(&socket_client->lock);
  242. /* while waiting for the lock the socket_client could have been
  243. * deleted
  244. */
  245. if (!batadv_socket_client_hash[icmp_packet->uid]) {
  246. spin_unlock_bh(&socket_client->lock);
  247. kfree(socket_packet);
  248. return;
  249. }
  250. list_add_tail(&socket_packet->list, &socket_client->queue_list);
  251. socket_client->queue_len++;
  252. if (socket_client->queue_len > 100) {
  253. socket_packet = list_first_entry(&socket_client->queue_list,
  254. struct batadv_socket_packet,
  255. list);
  256. list_del(&socket_packet->list);
  257. kfree(socket_packet);
  258. socket_client->queue_len--;
  259. }
  260. spin_unlock_bh(&socket_client->lock);
  261. wake_up(&socket_client->queue_wait);
  262. }
  263. void batadv_socket_receive_packet(struct batadv_icmp_packet_rr *icmp_packet,
  264. size_t icmp_len)
  265. {
  266. struct batadv_socket_client *hash;
  267. hash = batadv_socket_client_hash[icmp_packet->uid];
  268. if (hash)
  269. batadv_socket_add_packet(hash, icmp_packet, icmp_len);
  270. }