icmp_socket.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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_header *icmph,
  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_rr;
  125. struct batadv_icmp_header *icmp_header;
  126. struct batadv_orig_node *orig_node = NULL;
  127. struct batadv_neigh_node *neigh_node = NULL;
  128. size_t packet_len = sizeof(struct batadv_icmp_packet);
  129. if (len < sizeof(struct batadv_icmp_header)) {
  130. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  131. "Error - can't send packet from char device: invalid packet size\n");
  132. return -EINVAL;
  133. }
  134. primary_if = batadv_primary_if_get_selected(bat_priv);
  135. if (!primary_if) {
  136. len = -EFAULT;
  137. goto out;
  138. }
  139. if (len >= BATADV_ICMP_MAX_PACKET_SIZE)
  140. packet_len = BATADV_ICMP_MAX_PACKET_SIZE;
  141. else
  142. packet_len = len;
  143. skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN);
  144. if (!skb) {
  145. len = -ENOMEM;
  146. goto out;
  147. }
  148. skb->priority = TC_PRIO_CONTROL;
  149. skb_reserve(skb, ETH_HLEN);
  150. icmp_header = (struct batadv_icmp_header *)skb_put(skb, packet_len);
  151. if (copy_from_user(icmp_header, buff, packet_len)) {
  152. len = -EFAULT;
  153. goto free_skb;
  154. }
  155. if (icmp_header->header.packet_type != BATADV_ICMP) {
  156. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  157. "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
  158. len = -EINVAL;
  159. goto free_skb;
  160. }
  161. switch (icmp_header->msg_type) {
  162. case BATADV_ECHO_REQUEST:
  163. if (len < sizeof(struct batadv_icmp_packet)) {
  164. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  165. "Error - can't send packet from char device: invalid packet size\n");
  166. len = -EINVAL;
  167. goto free_skb;
  168. }
  169. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  170. goto dst_unreach;
  171. orig_node = batadv_orig_hash_find(bat_priv, icmp_header->dst);
  172. if (!orig_node)
  173. goto dst_unreach;
  174. neigh_node = batadv_orig_node_get_router(orig_node);
  175. if (!neigh_node)
  176. goto dst_unreach;
  177. if (!neigh_node->if_incoming)
  178. goto dst_unreach;
  179. if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
  180. goto dst_unreach;
  181. icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmp_header;
  182. if (packet_len == sizeof(*icmp_packet_rr))
  183. memcpy(icmp_packet_rr->rr,
  184. neigh_node->if_incoming->net_dev->dev_addr,
  185. ETH_ALEN);
  186. break;
  187. default:
  188. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  189. "Error - can't send packet from char device: got unknown message type\n");
  190. len = -EINVAL;
  191. goto free_skb;
  192. }
  193. icmp_header->uid = socket_client->index;
  194. if (icmp_header->header.version != BATADV_COMPAT_VERSION) {
  195. icmp_header->msg_type = BATADV_PARAMETER_PROBLEM;
  196. icmp_header->header.version = BATADV_COMPAT_VERSION;
  197. batadv_socket_add_packet(socket_client, icmp_header,
  198. packet_len);
  199. goto free_skb;
  200. }
  201. memcpy(icmp_header->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
  202. batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  203. goto out;
  204. dst_unreach:
  205. icmp_header->msg_type = BATADV_DESTINATION_UNREACHABLE;
  206. batadv_socket_add_packet(socket_client, icmp_header, packet_len);
  207. free_skb:
  208. kfree_skb(skb);
  209. out:
  210. if (primary_if)
  211. batadv_hardif_free_ref(primary_if);
  212. if (neigh_node)
  213. batadv_neigh_node_free_ref(neigh_node);
  214. if (orig_node)
  215. batadv_orig_node_free_ref(orig_node);
  216. return len;
  217. }
  218. static unsigned int batadv_socket_poll(struct file *file, poll_table *wait)
  219. {
  220. struct batadv_socket_client *socket_client = file->private_data;
  221. poll_wait(file, &socket_client->queue_wait, wait);
  222. if (socket_client->queue_len > 0)
  223. return POLLIN | POLLRDNORM;
  224. return 0;
  225. }
  226. static const struct file_operations batadv_fops = {
  227. .owner = THIS_MODULE,
  228. .open = batadv_socket_open,
  229. .release = batadv_socket_release,
  230. .read = batadv_socket_read,
  231. .write = batadv_socket_write,
  232. .poll = batadv_socket_poll,
  233. .llseek = no_llseek,
  234. };
  235. int batadv_socket_setup(struct batadv_priv *bat_priv)
  236. {
  237. struct dentry *d;
  238. if (!bat_priv->debug_dir)
  239. goto err;
  240. d = debugfs_create_file(BATADV_ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
  241. bat_priv->debug_dir, bat_priv, &batadv_fops);
  242. if (!d)
  243. goto err;
  244. return 0;
  245. err:
  246. return -ENOMEM;
  247. }
  248. /**
  249. * batadv_socket_receive_packet - schedule an icmp packet to be sent to userspace
  250. * on an icmp socket.
  251. * @socket_client: the socket this packet belongs to
  252. * @icmph: pointer to the header of the icmp packet
  253. * @icmp_len: total length of the icmp packet
  254. */
  255. static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
  256. struct batadv_icmp_header *icmph,
  257. size_t icmp_len)
  258. {
  259. struct batadv_socket_packet *socket_packet;
  260. size_t len;
  261. socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
  262. if (!socket_packet)
  263. return;
  264. len = icmp_len;
  265. /* check the maximum length before filling the buffer */
  266. if (len > sizeof(socket_packet->icmp_packet))
  267. len = sizeof(socket_packet->icmp_packet);
  268. INIT_LIST_HEAD(&socket_packet->list);
  269. memcpy(&socket_packet->icmp_packet, icmph, len);
  270. socket_packet->icmp_len = len;
  271. spin_lock_bh(&socket_client->lock);
  272. /* while waiting for the lock the socket_client could have been
  273. * deleted
  274. */
  275. if (!batadv_socket_client_hash[icmph->uid]) {
  276. spin_unlock_bh(&socket_client->lock);
  277. kfree(socket_packet);
  278. return;
  279. }
  280. list_add_tail(&socket_packet->list, &socket_client->queue_list);
  281. socket_client->queue_len++;
  282. if (socket_client->queue_len > 100) {
  283. socket_packet = list_first_entry(&socket_client->queue_list,
  284. struct batadv_socket_packet,
  285. list);
  286. list_del(&socket_packet->list);
  287. kfree(socket_packet);
  288. socket_client->queue_len--;
  289. }
  290. spin_unlock_bh(&socket_client->lock);
  291. wake_up(&socket_client->queue_wait);
  292. }
  293. /**
  294. * batadv_socket_receive_packet - schedule an icmp packet to be received
  295. * locally and sent to userspace.
  296. * @icmph: pointer to the header of the icmp packet
  297. * @icmp_len: total length of the icmp packet
  298. */
  299. void batadv_socket_receive_packet(struct batadv_icmp_header *icmph,
  300. size_t icmp_len)
  301. {
  302. struct batadv_socket_client *hash;
  303. hash = batadv_socket_client_hash[icmph->uid];
  304. if (hash)
  305. batadv_socket_add_packet(hash, icmph, icmp_len);
  306. }