icmp_socket.c 11 KB

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