icmp_socket.c 10 KB

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