icmp_socket.c 11 KB

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