icmp_socket.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  1. /* Copyright (C) 2007-2016 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, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "icmp_socket.h"
  18. #include "main.h"
  19. #include <linux/atomic.h>
  20. #include <linux/compiler.h>
  21. #include <linux/debugfs.h>
  22. #include <linux/errno.h>
  23. #include <linux/etherdevice.h>
  24. #include <linux/export.h>
  25. #include <linux/fcntl.h>
  26. #include <linux/fs.h>
  27. #include <linux/if_ether.h>
  28. #include <linux/kernel.h>
  29. #include <linux/list.h>
  30. #include <linux/module.h>
  31. #include <linux/netdevice.h>
  32. #include <linux/pkt_sched.h>
  33. #include <linux/poll.h>
  34. #include <linux/printk.h>
  35. #include <linux/sched.h> /* for linux/wait.h */
  36. #include <linux/skbuff.h>
  37. #include <linux/slab.h>
  38. #include <linux/spinlock.h>
  39. #include <linux/stat.h>
  40. #include <linux/stddef.h>
  41. #include <linux/string.h>
  42. #include <linux/uaccess.h>
  43. #include <linux/wait.h>
  44. #include "hard-interface.h"
  45. #include "log.h"
  46. #include "originator.h"
  47. #include "packet.h"
  48. #include "send.h"
  49. static struct batadv_socket_client *batadv_socket_client_hash[256];
  50. static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
  51. struct batadv_icmp_header *icmph,
  52. size_t icmp_len);
  53. void batadv_socket_init(void)
  54. {
  55. memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash));
  56. }
  57. static int batadv_socket_open(struct inode *inode, struct file *file)
  58. {
  59. unsigned int i;
  60. struct batadv_socket_client *socket_client;
  61. if (!try_module_get(THIS_MODULE))
  62. return -EBUSY;
  63. nonseekable_open(inode, file);
  64. socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
  65. if (!socket_client) {
  66. module_put(THIS_MODULE);
  67. return -ENOMEM;
  68. }
  69. for (i = 0; i < ARRAY_SIZE(batadv_socket_client_hash); i++) {
  70. if (!batadv_socket_client_hash[i]) {
  71. batadv_socket_client_hash[i] = socket_client;
  72. break;
  73. }
  74. }
  75. if (i == ARRAY_SIZE(batadv_socket_client_hash)) {
  76. pr_err("Error - can't add another packet client: maximum number of clients reached\n");
  77. kfree(socket_client);
  78. module_put(THIS_MODULE);
  79. return -EXFULL;
  80. }
  81. INIT_LIST_HEAD(&socket_client->queue_list);
  82. socket_client->queue_len = 0;
  83. socket_client->index = i;
  84. socket_client->bat_priv = inode->i_private;
  85. spin_lock_init(&socket_client->lock);
  86. init_waitqueue_head(&socket_client->queue_wait);
  87. file->private_data = socket_client;
  88. return 0;
  89. }
  90. static int batadv_socket_release(struct inode *inode, struct file *file)
  91. {
  92. struct batadv_socket_client *client = file->private_data;
  93. struct batadv_socket_packet *packet, *tmp;
  94. spin_lock_bh(&client->lock);
  95. /* for all packets in the queue ... */
  96. list_for_each_entry_safe(packet, tmp, &client->queue_list, list) {
  97. list_del(&packet->list);
  98. kfree(packet);
  99. }
  100. batadv_socket_client_hash[client->index] = NULL;
  101. spin_unlock_bh(&client->lock);
  102. kfree(client);
  103. module_put(THIS_MODULE);
  104. return 0;
  105. }
  106. static ssize_t batadv_socket_read(struct file *file, char __user *buf,
  107. size_t count, loff_t *ppos)
  108. {
  109. struct batadv_socket_client *socket_client = file->private_data;
  110. struct batadv_socket_packet *socket_packet;
  111. size_t packet_len;
  112. int error;
  113. if ((file->f_flags & O_NONBLOCK) && (socket_client->queue_len == 0))
  114. return -EAGAIN;
  115. if ((!buf) || (count < sizeof(struct batadv_icmp_packet)))
  116. return -EINVAL;
  117. if (!access_ok(VERIFY_WRITE, buf, count))
  118. return -EFAULT;
  119. error = wait_event_interruptible(socket_client->queue_wait,
  120. socket_client->queue_len);
  121. if (error)
  122. return error;
  123. spin_lock_bh(&socket_client->lock);
  124. socket_packet = list_first_entry(&socket_client->queue_list,
  125. struct batadv_socket_packet, list);
  126. list_del(&socket_packet->list);
  127. socket_client->queue_len--;
  128. spin_unlock_bh(&socket_client->lock);
  129. packet_len = min(count, socket_packet->icmp_len);
  130. error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
  131. kfree(socket_packet);
  132. if (error)
  133. return -EFAULT;
  134. return packet_len;
  135. }
  136. static ssize_t batadv_socket_write(struct file *file, const char __user *buff,
  137. size_t len, loff_t *off)
  138. {
  139. struct batadv_socket_client *socket_client = file->private_data;
  140. struct batadv_priv *bat_priv = socket_client->bat_priv;
  141. struct batadv_hard_iface *primary_if = NULL;
  142. struct sk_buff *skb;
  143. struct batadv_icmp_packet_rr *icmp_packet_rr;
  144. struct batadv_icmp_header *icmp_header;
  145. struct batadv_orig_node *orig_node = NULL;
  146. struct batadv_neigh_node *neigh_node = NULL;
  147. size_t packet_len = sizeof(struct batadv_icmp_packet);
  148. u8 *addr;
  149. if (len < sizeof(struct batadv_icmp_header)) {
  150. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  151. "Error - can't send packet from char device: invalid packet size\n");
  152. return -EINVAL;
  153. }
  154. primary_if = batadv_primary_if_get_selected(bat_priv);
  155. if (!primary_if) {
  156. len = -EFAULT;
  157. goto out;
  158. }
  159. if (len >= BATADV_ICMP_MAX_PACKET_SIZE)
  160. packet_len = BATADV_ICMP_MAX_PACKET_SIZE;
  161. else
  162. packet_len = len;
  163. skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN);
  164. if (!skb) {
  165. len = -ENOMEM;
  166. goto out;
  167. }
  168. skb->priority = TC_PRIO_CONTROL;
  169. skb_reserve(skb, ETH_HLEN);
  170. icmp_header = (struct batadv_icmp_header *)skb_put(skb, packet_len);
  171. if (copy_from_user(icmp_header, buff, packet_len)) {
  172. len = -EFAULT;
  173. goto free_skb;
  174. }
  175. if (icmp_header->packet_type != BATADV_ICMP) {
  176. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  177. "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
  178. len = -EINVAL;
  179. goto free_skb;
  180. }
  181. switch (icmp_header->msg_type) {
  182. case BATADV_ECHO_REQUEST:
  183. if (len < sizeof(struct batadv_icmp_packet)) {
  184. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  185. "Error - can't send packet from char device: invalid packet size\n");
  186. len = -EINVAL;
  187. goto free_skb;
  188. }
  189. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  190. goto dst_unreach;
  191. orig_node = batadv_orig_hash_find(bat_priv, icmp_header->dst);
  192. if (!orig_node)
  193. goto dst_unreach;
  194. neigh_node = batadv_orig_router_get(orig_node,
  195. BATADV_IF_DEFAULT);
  196. if (!neigh_node)
  197. goto dst_unreach;
  198. if (!neigh_node->if_incoming)
  199. goto dst_unreach;
  200. if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
  201. goto dst_unreach;
  202. icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmp_header;
  203. if (packet_len == sizeof(*icmp_packet_rr)) {
  204. addr = neigh_node->if_incoming->net_dev->dev_addr;
  205. ether_addr_copy(icmp_packet_rr->rr[0], addr);
  206. }
  207. break;
  208. default:
  209. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  210. "Error - can't send packet from char device: got unknown message type\n");
  211. len = -EINVAL;
  212. goto free_skb;
  213. }
  214. icmp_header->uid = socket_client->index;
  215. if (icmp_header->version != BATADV_COMPAT_VERSION) {
  216. icmp_header->msg_type = BATADV_PARAMETER_PROBLEM;
  217. icmp_header->version = BATADV_COMPAT_VERSION;
  218. batadv_socket_add_packet(socket_client, icmp_header,
  219. packet_len);
  220. goto free_skb;
  221. }
  222. ether_addr_copy(icmp_header->orig, primary_if->net_dev->dev_addr);
  223. batadv_send_unicast_skb(skb, neigh_node);
  224. goto out;
  225. dst_unreach:
  226. icmp_header->msg_type = BATADV_DESTINATION_UNREACHABLE;
  227. batadv_socket_add_packet(socket_client, icmp_header, packet_len);
  228. free_skb:
  229. kfree_skb(skb);
  230. out:
  231. if (primary_if)
  232. batadv_hardif_put(primary_if);
  233. if (neigh_node)
  234. batadv_neigh_node_put(neigh_node);
  235. if (orig_node)
  236. batadv_orig_node_put(orig_node);
  237. return len;
  238. }
  239. static unsigned int batadv_socket_poll(struct file *file, poll_table *wait)
  240. {
  241. struct batadv_socket_client *socket_client = file->private_data;
  242. poll_wait(file, &socket_client->queue_wait, wait);
  243. if (socket_client->queue_len > 0)
  244. return POLLIN | POLLRDNORM;
  245. return 0;
  246. }
  247. static const struct file_operations batadv_fops = {
  248. .owner = THIS_MODULE,
  249. .open = batadv_socket_open,
  250. .release = batadv_socket_release,
  251. .read = batadv_socket_read,
  252. .write = batadv_socket_write,
  253. .poll = batadv_socket_poll,
  254. .llseek = no_llseek,
  255. };
  256. int batadv_socket_setup(struct batadv_priv *bat_priv)
  257. {
  258. struct dentry *d;
  259. if (!bat_priv->debug_dir)
  260. goto err;
  261. d = debugfs_create_file(BATADV_ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
  262. bat_priv->debug_dir, bat_priv, &batadv_fops);
  263. if (!d)
  264. goto err;
  265. return 0;
  266. err:
  267. return -ENOMEM;
  268. }
  269. /**
  270. * batadv_socket_add_packet - schedule an icmp packet to be sent to
  271. * userspace on an icmp socket.
  272. * @socket_client: the socket this packet belongs to
  273. * @icmph: pointer to the header of the icmp packet
  274. * @icmp_len: total length of the icmp packet
  275. */
  276. static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
  277. struct batadv_icmp_header *icmph,
  278. size_t icmp_len)
  279. {
  280. struct batadv_socket_packet *socket_packet;
  281. size_t len;
  282. socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
  283. if (!socket_packet)
  284. return;
  285. len = icmp_len;
  286. /* check the maximum length before filling the buffer */
  287. if (len > sizeof(socket_packet->icmp_packet))
  288. len = sizeof(socket_packet->icmp_packet);
  289. INIT_LIST_HEAD(&socket_packet->list);
  290. memcpy(&socket_packet->icmp_packet, icmph, len);
  291. socket_packet->icmp_len = len;
  292. spin_lock_bh(&socket_client->lock);
  293. /* while waiting for the lock the socket_client could have been
  294. * deleted
  295. */
  296. if (!batadv_socket_client_hash[icmph->uid]) {
  297. spin_unlock_bh(&socket_client->lock);
  298. kfree(socket_packet);
  299. return;
  300. }
  301. list_add_tail(&socket_packet->list, &socket_client->queue_list);
  302. socket_client->queue_len++;
  303. if (socket_client->queue_len > 100) {
  304. socket_packet = list_first_entry(&socket_client->queue_list,
  305. struct batadv_socket_packet,
  306. list);
  307. list_del(&socket_packet->list);
  308. kfree(socket_packet);
  309. socket_client->queue_len--;
  310. }
  311. spin_unlock_bh(&socket_client->lock);
  312. wake_up(&socket_client->queue_wait);
  313. }
  314. /**
  315. * batadv_socket_receive_packet - schedule an icmp packet to be received
  316. * locally and sent to userspace.
  317. * @icmph: pointer to the header of the icmp packet
  318. * @icmp_len: total length of the icmp packet
  319. */
  320. void batadv_socket_receive_packet(struct batadv_icmp_header *icmph,
  321. size_t icmp_len)
  322. {
  323. struct batadv_socket_client *hash;
  324. hash = batadv_socket_client_hash[icmph->uid];
  325. if (hash)
  326. batadv_socket_add_packet(hash, icmph, icmp_len);
  327. }