kconfig: fix memory leak when EOF is encountered in quotation
[linux/fpc-iii.git] / net / batman-adv / icmp_socket.c
blobd70f363c52ae2219aa819d897c1872278005db27
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2007-2018 B.A.T.M.A.N. contributors:
4 * Marek Lindner
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.
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.
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/>.
19 #include "icmp_socket.h"
20 #include "main.h"
22 #include <linux/atomic.h>
23 #include <linux/compiler.h>
24 #include <linux/debugfs.h>
25 #include <linux/errno.h>
26 #include <linux/etherdevice.h>
27 #include <linux/eventpoll.h>
28 #include <linux/export.h>
29 #include <linux/fcntl.h>
30 #include <linux/fs.h>
31 #include <linux/gfp.h>
32 #include <linux/if_ether.h>
33 #include <linux/kernel.h>
34 #include <linux/list.h>
35 #include <linux/module.h>
36 #include <linux/netdevice.h>
37 #include <linux/pkt_sched.h>
38 #include <linux/poll.h>
39 #include <linux/printk.h>
40 #include <linux/sched.h> /* for linux/wait.h */
41 #include <linux/skbuff.h>
42 #include <linux/slab.h>
43 #include <linux/spinlock.h>
44 #include <linux/stddef.h>
45 #include <linux/string.h>
46 #include <linux/uaccess.h>
47 #include <linux/wait.h>
48 #include <uapi/linux/batadv_packet.h>
50 #include "debugfs.h"
51 #include "hard-interface.h"
52 #include "log.h"
53 #include "originator.h"
54 #include "send.h"
56 static struct batadv_socket_client *batadv_socket_client_hash[256];
58 static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
59 struct batadv_icmp_header *icmph,
60 size_t icmp_len);
62 /**
63 * batadv_socket_init() - Initialize soft interface independent socket data
65 void batadv_socket_init(void)
67 memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash));
70 static int batadv_socket_open(struct inode *inode, struct file *file)
72 unsigned int i;
73 struct batadv_socket_client *socket_client;
75 if (!try_module_get(THIS_MODULE))
76 return -EBUSY;
78 batadv_debugfs_deprecated(file, "");
80 nonseekable_open(inode, file);
82 socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
83 if (!socket_client) {
84 module_put(THIS_MODULE);
85 return -ENOMEM;
88 for (i = 0; i < ARRAY_SIZE(batadv_socket_client_hash); i++) {
89 if (!batadv_socket_client_hash[i]) {
90 batadv_socket_client_hash[i] = socket_client;
91 break;
95 if (i == ARRAY_SIZE(batadv_socket_client_hash)) {
96 pr_err("Error - can't add another packet client: maximum number of clients reached\n");
97 kfree(socket_client);
98 module_put(THIS_MODULE);
99 return -EXFULL;
102 INIT_LIST_HEAD(&socket_client->queue_list);
103 socket_client->queue_len = 0;
104 socket_client->index = i;
105 socket_client->bat_priv = inode->i_private;
106 spin_lock_init(&socket_client->lock);
107 init_waitqueue_head(&socket_client->queue_wait);
109 file->private_data = socket_client;
111 return 0;
114 static int batadv_socket_release(struct inode *inode, struct file *file)
116 struct batadv_socket_client *client = file->private_data;
117 struct batadv_socket_packet *packet, *tmp;
119 spin_lock_bh(&client->lock);
121 /* for all packets in the queue ... */
122 list_for_each_entry_safe(packet, tmp, &client->queue_list, list) {
123 list_del(&packet->list);
124 kfree(packet);
127 batadv_socket_client_hash[client->index] = NULL;
128 spin_unlock_bh(&client->lock);
130 kfree(client);
131 module_put(THIS_MODULE);
133 return 0;
136 static ssize_t batadv_socket_read(struct file *file, char __user *buf,
137 size_t count, loff_t *ppos)
139 struct batadv_socket_client *socket_client = file->private_data;
140 struct batadv_socket_packet *socket_packet;
141 size_t packet_len;
142 int error;
144 if ((file->f_flags & O_NONBLOCK) && socket_client->queue_len == 0)
145 return -EAGAIN;
147 if (!buf || count < sizeof(struct batadv_icmp_packet))
148 return -EINVAL;
150 if (!access_ok(VERIFY_WRITE, buf, count))
151 return -EFAULT;
153 error = wait_event_interruptible(socket_client->queue_wait,
154 socket_client->queue_len);
156 if (error)
157 return error;
159 spin_lock_bh(&socket_client->lock);
161 socket_packet = list_first_entry(&socket_client->queue_list,
162 struct batadv_socket_packet, list);
163 list_del(&socket_packet->list);
164 socket_client->queue_len--;
166 spin_unlock_bh(&socket_client->lock);
168 packet_len = min(count, socket_packet->icmp_len);
169 error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
171 kfree(socket_packet);
173 if (error)
174 return -EFAULT;
176 return packet_len;
179 static ssize_t batadv_socket_write(struct file *file, const char __user *buff,
180 size_t len, loff_t *off)
182 struct batadv_socket_client *socket_client = file->private_data;
183 struct batadv_priv *bat_priv = socket_client->bat_priv;
184 struct batadv_hard_iface *primary_if = NULL;
185 struct sk_buff *skb;
186 struct batadv_icmp_packet_rr *icmp_packet_rr;
187 struct batadv_icmp_header *icmp_header;
188 struct batadv_orig_node *orig_node = NULL;
189 struct batadv_neigh_node *neigh_node = NULL;
190 size_t packet_len = sizeof(struct batadv_icmp_packet);
191 u8 *addr;
193 if (len < sizeof(struct batadv_icmp_header)) {
194 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
195 "Error - can't send packet from char device: invalid packet size\n");
196 return -EINVAL;
199 primary_if = batadv_primary_if_get_selected(bat_priv);
201 if (!primary_if) {
202 len = -EFAULT;
203 goto out;
206 if (len >= BATADV_ICMP_MAX_PACKET_SIZE)
207 packet_len = BATADV_ICMP_MAX_PACKET_SIZE;
208 else
209 packet_len = len;
211 skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN);
212 if (!skb) {
213 len = -ENOMEM;
214 goto out;
217 skb->priority = TC_PRIO_CONTROL;
218 skb_reserve(skb, ETH_HLEN);
219 icmp_header = skb_put(skb, packet_len);
221 if (copy_from_user(icmp_header, buff, packet_len)) {
222 len = -EFAULT;
223 goto free_skb;
226 if (icmp_header->packet_type != BATADV_ICMP) {
227 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
228 "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
229 len = -EINVAL;
230 goto free_skb;
233 switch (icmp_header->msg_type) {
234 case BATADV_ECHO_REQUEST:
235 if (len < sizeof(struct batadv_icmp_packet)) {
236 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
237 "Error - can't send packet from char device: invalid packet size\n");
238 len = -EINVAL;
239 goto free_skb;
242 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
243 goto dst_unreach;
245 orig_node = batadv_orig_hash_find(bat_priv, icmp_header->dst);
246 if (!orig_node)
247 goto dst_unreach;
249 neigh_node = batadv_orig_router_get(orig_node,
250 BATADV_IF_DEFAULT);
251 if (!neigh_node)
252 goto dst_unreach;
254 if (!neigh_node->if_incoming)
255 goto dst_unreach;
257 if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
258 goto dst_unreach;
260 icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmp_header;
261 if (packet_len == sizeof(*icmp_packet_rr)) {
262 addr = neigh_node->if_incoming->net_dev->dev_addr;
263 ether_addr_copy(icmp_packet_rr->rr[0], addr);
266 break;
267 default:
268 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
269 "Error - can't send packet from char device: got unknown message type\n");
270 len = -EINVAL;
271 goto free_skb;
274 icmp_header->uid = socket_client->index;
276 if (icmp_header->version != BATADV_COMPAT_VERSION) {
277 icmp_header->msg_type = BATADV_PARAMETER_PROBLEM;
278 icmp_header->version = BATADV_COMPAT_VERSION;
279 batadv_socket_add_packet(socket_client, icmp_header,
280 packet_len);
281 goto free_skb;
284 ether_addr_copy(icmp_header->orig, primary_if->net_dev->dev_addr);
286 batadv_send_unicast_skb(skb, neigh_node);
287 goto out;
289 dst_unreach:
290 icmp_header->msg_type = BATADV_DESTINATION_UNREACHABLE;
291 batadv_socket_add_packet(socket_client, icmp_header, packet_len);
292 free_skb:
293 kfree_skb(skb);
294 out:
295 if (primary_if)
296 batadv_hardif_put(primary_if);
297 if (neigh_node)
298 batadv_neigh_node_put(neigh_node);
299 if (orig_node)
300 batadv_orig_node_put(orig_node);
301 return len;
304 static __poll_t batadv_socket_poll(struct file *file, poll_table *wait)
306 struct batadv_socket_client *socket_client = file->private_data;
308 poll_wait(file, &socket_client->queue_wait, wait);
310 if (socket_client->queue_len > 0)
311 return EPOLLIN | EPOLLRDNORM;
313 return 0;
316 static const struct file_operations batadv_fops = {
317 .owner = THIS_MODULE,
318 .open = batadv_socket_open,
319 .release = batadv_socket_release,
320 .read = batadv_socket_read,
321 .write = batadv_socket_write,
322 .poll = batadv_socket_poll,
323 .llseek = no_llseek,
327 * batadv_socket_setup() - Create debugfs "socket" file
328 * @bat_priv: the bat priv with all the soft interface information
330 * Return: 0 on success or negative error number in case of failure
332 int batadv_socket_setup(struct batadv_priv *bat_priv)
334 struct dentry *d;
336 if (!bat_priv->debug_dir)
337 goto err;
339 d = debugfs_create_file(BATADV_ICMP_SOCKET, 0600, bat_priv->debug_dir,
340 bat_priv, &batadv_fops);
341 if (!d)
342 goto err;
344 return 0;
346 err:
347 return -ENOMEM;
351 * batadv_socket_add_packet() - schedule an icmp packet to be sent to
352 * userspace on an icmp socket.
353 * @socket_client: the socket this packet belongs to
354 * @icmph: pointer to the header of the icmp packet
355 * @icmp_len: total length of the icmp packet
357 static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
358 struct batadv_icmp_header *icmph,
359 size_t icmp_len)
361 struct batadv_socket_packet *socket_packet;
362 size_t len;
364 socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
366 if (!socket_packet)
367 return;
369 len = icmp_len;
370 /* check the maximum length before filling the buffer */
371 if (len > sizeof(socket_packet->icmp_packet))
372 len = sizeof(socket_packet->icmp_packet);
374 INIT_LIST_HEAD(&socket_packet->list);
375 memcpy(&socket_packet->icmp_packet, icmph, len);
376 socket_packet->icmp_len = len;
378 spin_lock_bh(&socket_client->lock);
380 /* while waiting for the lock the socket_client could have been
381 * deleted
383 if (!batadv_socket_client_hash[icmph->uid]) {
384 spin_unlock_bh(&socket_client->lock);
385 kfree(socket_packet);
386 return;
389 list_add_tail(&socket_packet->list, &socket_client->queue_list);
390 socket_client->queue_len++;
392 if (socket_client->queue_len > 100) {
393 socket_packet = list_first_entry(&socket_client->queue_list,
394 struct batadv_socket_packet,
395 list);
397 list_del(&socket_packet->list);
398 kfree(socket_packet);
399 socket_client->queue_len--;
402 spin_unlock_bh(&socket_client->lock);
404 wake_up(&socket_client->queue_wait);
408 * batadv_socket_receive_packet() - schedule an icmp packet to be received
409 * locally and sent to userspace.
410 * @icmph: pointer to the header of the icmp packet
411 * @icmp_len: total length of the icmp packet
413 void batadv_socket_receive_packet(struct batadv_icmp_header *icmph,
414 size_t icmp_len)
416 struct batadv_socket_client *hash;
418 hash = batadv_socket_client_hash[icmph->uid];
419 if (hash)
420 batadv_socket_add_packet(hash, icmph, icmp_len);