icmp_socket.c 11 KB

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