icmp_socket.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /* Copyright (C) 2007-2013 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. if (!try_module_get(THIS_MODULE))
  40. return -EBUSY;
  41. nonseekable_open(inode, file);
  42. socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
  43. if (!socket_client) {
  44. module_put(THIS_MODULE);
  45. return -ENOMEM;
  46. }
  47. for (i = 0; i < ARRAY_SIZE(batadv_socket_client_hash); i++) {
  48. if (!batadv_socket_client_hash[i]) {
  49. batadv_socket_client_hash[i] = socket_client;
  50. break;
  51. }
  52. }
  53. if (i == ARRAY_SIZE(batadv_socket_client_hash)) {
  54. pr_err("Error - can't add another packet client: maximum number of clients reached\n");
  55. kfree(socket_client);
  56. module_put(THIS_MODULE);
  57. return -EXFULL;
  58. }
  59. INIT_LIST_HEAD(&socket_client->queue_list);
  60. socket_client->queue_len = 0;
  61. socket_client->index = i;
  62. socket_client->bat_priv = inode->i_private;
  63. spin_lock_init(&socket_client->lock);
  64. init_waitqueue_head(&socket_client->queue_wait);
  65. file->private_data = socket_client;
  66. return 0;
  67. }
  68. static int batadv_socket_release(struct inode *inode, struct file *file)
  69. {
  70. struct batadv_socket_client *socket_client = file->private_data;
  71. struct batadv_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 batadv_socket_packet, list);
  78. list_del(list_pos);
  79. kfree(socket_packet);
  80. }
  81. batadv_socket_client_hash[socket_client->index] = NULL;
  82. spin_unlock_bh(&socket_client->lock);
  83. kfree(socket_client);
  84. module_put(THIS_MODULE);
  85. return 0;
  86. }
  87. static ssize_t batadv_socket_read(struct file *file, char __user *buf,
  88. size_t count, loff_t *ppos)
  89. {
  90. struct batadv_socket_client *socket_client = file->private_data;
  91. struct batadv_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 batadv_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 batadv_socket_packet, list);
  107. list_del(&socket_packet->list);
  108. socket_client->queue_len--;
  109. spin_unlock_bh(&socket_client->lock);
  110. packet_len = min(count, socket_packet->icmp_len);
  111. error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
  112. kfree(socket_packet);
  113. if (error)
  114. return -EFAULT;
  115. return packet_len;
  116. }
  117. static ssize_t batadv_socket_write(struct file *file, const char __user *buff,
  118. size_t len, loff_t *off)
  119. {
  120. struct batadv_socket_client *socket_client = file->private_data;
  121. struct batadv_priv *bat_priv = socket_client->bat_priv;
  122. struct batadv_hard_iface *primary_if = NULL;
  123. struct sk_buff *skb;
  124. struct batadv_icmp_packet_rr *icmp_packet;
  125. struct batadv_orig_node *orig_node = NULL;
  126. struct batadv_neigh_node *neigh_node = NULL;
  127. size_t packet_len = sizeof(struct batadv_icmp_packet);
  128. if (len < sizeof(struct batadv_icmp_packet)) {
  129. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  130. "Error - can't send packet from char device: invalid packet size\n");
  131. return -EINVAL;
  132. }
  133. primary_if = batadv_primary_if_get_selected(bat_priv);
  134. if (!primary_if) {
  135. len = -EFAULT;
  136. goto out;
  137. }
  138. if (len >= sizeof(struct batadv_icmp_packet_rr))
  139. packet_len = sizeof(struct batadv_icmp_packet_rr);
  140. skb = dev_alloc_skb(packet_len + ETH_HLEN + NET_IP_ALIGN);
  141. if (!skb) {
  142. len = -ENOMEM;
  143. goto out;
  144. }
  145. skb_reserve(skb, ETH_HLEN + NET_IP_ALIGN);
  146. icmp_packet = (struct batadv_icmp_packet_rr *)skb_put(skb, packet_len);
  147. if (copy_from_user(icmp_packet, buff, packet_len)) {
  148. len = -EFAULT;
  149. goto free_skb;
  150. }
  151. if (icmp_packet->header.packet_type != BATADV_ICMP) {
  152. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  153. "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
  154. len = -EINVAL;
  155. goto free_skb;
  156. }
  157. if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
  158. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  159. "Error - can't send packet from char device: got bogus message type (expected: ECHO_REQUEST)\n");
  160. len = -EINVAL;
  161. goto free_skb;
  162. }
  163. icmp_packet->uid = socket_client->index;
  164. if (icmp_packet->header.version != BATADV_COMPAT_VERSION) {
  165. icmp_packet->msg_type = BATADV_PARAMETER_PROBLEM;
  166. icmp_packet->header.version = BATADV_COMPAT_VERSION;
  167. batadv_socket_add_packet(socket_client, icmp_packet,
  168. packet_len);
  169. goto free_skb;
  170. }
  171. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  172. goto dst_unreach;
  173. orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
  174. if (!orig_node)
  175. goto dst_unreach;
  176. neigh_node = batadv_orig_node_get_router(orig_node);
  177. if (!neigh_node)
  178. goto dst_unreach;
  179. if (!neigh_node->if_incoming)
  180. goto dst_unreach;
  181. if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
  182. goto dst_unreach;
  183. memcpy(icmp_packet->orig,
  184. primary_if->net_dev->dev_addr, ETH_ALEN);
  185. if (packet_len == sizeof(struct batadv_icmp_packet_rr))
  186. memcpy(icmp_packet->rr,
  187. neigh_node->if_incoming->net_dev->dev_addr, ETH_ALEN);
  188. batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  189. goto out;
  190. dst_unreach:
  191. icmp_packet->msg_type = BATADV_DESTINATION_UNREACHABLE;
  192. batadv_socket_add_packet(socket_client, icmp_packet, packet_len);
  193. free_skb:
  194. kfree_skb(skb);
  195. out:
  196. if (primary_if)
  197. batadv_hardif_free_ref(primary_if);
  198. if (neigh_node)
  199. batadv_neigh_node_free_ref(neigh_node);
  200. if (orig_node)
  201. batadv_orig_node_free_ref(orig_node);
  202. return len;
  203. }
  204. static unsigned int batadv_socket_poll(struct file *file, poll_table *wait)
  205. {
  206. struct batadv_socket_client *socket_client = file->private_data;
  207. poll_wait(file, &socket_client->queue_wait, wait);
  208. if (socket_client->queue_len > 0)
  209. return POLLIN | POLLRDNORM;
  210. return 0;
  211. }
  212. static const struct file_operations batadv_fops = {
  213. .owner = THIS_MODULE,
  214. .open = batadv_socket_open,
  215. .release = batadv_socket_release,
  216. .read = batadv_socket_read,
  217. .write = batadv_socket_write,
  218. .poll = batadv_socket_poll,
  219. .llseek = no_llseek,
  220. };
  221. int batadv_socket_setup(struct batadv_priv *bat_priv)
  222. {
  223. struct dentry *d;
  224. if (!bat_priv->debug_dir)
  225. goto err;
  226. d = debugfs_create_file(BATADV_ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
  227. bat_priv->debug_dir, bat_priv, &batadv_fops);
  228. if (!d)
  229. goto err;
  230. return 0;
  231. err:
  232. return -ENOMEM;
  233. }
  234. static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
  235. struct batadv_icmp_packet_rr *icmp_packet,
  236. size_t icmp_len)
  237. {
  238. struct batadv_socket_packet *socket_packet;
  239. socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
  240. if (!socket_packet)
  241. return;
  242. INIT_LIST_HEAD(&socket_packet->list);
  243. memcpy(&socket_packet->icmp_packet, icmp_packet, icmp_len);
  244. socket_packet->icmp_len = icmp_len;
  245. spin_lock_bh(&socket_client->lock);
  246. /* while waiting for the lock the socket_client could have been
  247. * deleted
  248. */
  249. if (!batadv_socket_client_hash[icmp_packet->uid]) {
  250. spin_unlock_bh(&socket_client->lock);
  251. kfree(socket_packet);
  252. return;
  253. }
  254. list_add_tail(&socket_packet->list, &socket_client->queue_list);
  255. socket_client->queue_len++;
  256. if (socket_client->queue_len > 100) {
  257. socket_packet = list_first_entry(&socket_client->queue_list,
  258. struct batadv_socket_packet,
  259. 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 batadv_socket_receive_packet(struct batadv_icmp_packet_rr *icmp_packet,
  268. size_t icmp_len)
  269. {
  270. struct batadv_socket_client *hash;
  271. hash = batadv_socket_client_hash[icmp_packet->uid];
  272. if (hash)
  273. batadv_socket_add_packet(hash, icmp_packet, icmp_len);
  274. }