icmp_socket.c 9.9 KB

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