Merge branch 'x86-urgent-for-linus' of git://git.kernel.org/pub/scm/linux/kernel...
[cris-mirror.git] / net / batman-adv / icmp_socket.c
blobe91f29c7c638a5f9fede10b08cdd11f37d2ee1e6
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2007-2017 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/export.h>
28 #include <linux/fcntl.h>
29 #include <linux/fs.h>
30 #include <linux/gfp.h>
31 #include <linux/if_ether.h>
32 #include <linux/kernel.h>
33 #include <linux/list.h>
34 #include <linux/module.h>
35 #include <linux/netdevice.h>
36 #include <linux/pkt_sched.h>
37 #include <linux/poll.h>
38 #include <linux/printk.h>
39 #include <linux/sched.h> /* for linux/wait.h */
40 #include <linux/skbuff.h>
41 #include <linux/slab.h>
42 #include <linux/spinlock.h>
43 #include <linux/stddef.h>
44 #include <linux/string.h>
45 #include <linux/uaccess.h>
46 #include <linux/wait.h>
47 #include <uapi/linux/batadv_packet.h>
49 #include "hard-interface.h"
50 #include "log.h"
51 #include "originator.h"
52 #include "send.h"
54 static struct batadv_socket_client *batadv_socket_client_hash[256];
56 static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
57 struct batadv_icmp_header *icmph,
58 size_t icmp_len);
60 /**
61 * batadv_socket_init() - Initialize soft interface independent socket data
63 void batadv_socket_init(void)
65 memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash));
68 static int batadv_socket_open(struct inode *inode, struct file *file)
70 unsigned int i;
71 struct batadv_socket_client *socket_client;
73 if (!try_module_get(THIS_MODULE))
74 return -EBUSY;
76 nonseekable_open(inode, file);
78 socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
79 if (!socket_client) {
80 module_put(THIS_MODULE);
81 return -ENOMEM;
84 for (i = 0; i < ARRAY_SIZE(batadv_socket_client_hash); i++) {
85 if (!batadv_socket_client_hash[i]) {
86 batadv_socket_client_hash[i] = socket_client;
87 break;
91 if (i == ARRAY_SIZE(batadv_socket_client_hash)) {
92 pr_err("Error - can't add another packet client: maximum number of clients reached\n");
93 kfree(socket_client);
94 module_put(THIS_MODULE);
95 return -EXFULL;
98 INIT_LIST_HEAD(&socket_client->queue_list);
99 socket_client->queue_len = 0;
100 socket_client->index = i;
101 socket_client->bat_priv = inode->i_private;
102 spin_lock_init(&socket_client->lock);
103 init_waitqueue_head(&socket_client->queue_wait);
105 file->private_data = socket_client;
107 return 0;
110 static int batadv_socket_release(struct inode *inode, struct file *file)
112 struct batadv_socket_client *client = file->private_data;
113 struct batadv_socket_packet *packet, *tmp;
115 spin_lock_bh(&client->lock);
117 /* for all packets in the queue ... */
118 list_for_each_entry_safe(packet, tmp, &client->queue_list, list) {
119 list_del(&packet->list);
120 kfree(packet);
123 batadv_socket_client_hash[client->index] = NULL;
124 spin_unlock_bh(&client->lock);
126 kfree(client);
127 module_put(THIS_MODULE);
129 return 0;
132 static ssize_t batadv_socket_read(struct file *file, char __user *buf,
133 size_t count, loff_t *ppos)
135 struct batadv_socket_client *socket_client = file->private_data;
136 struct batadv_socket_packet *socket_packet;
137 size_t packet_len;
138 int error;
140 if ((file->f_flags & O_NONBLOCK) && socket_client->queue_len == 0)
141 return -EAGAIN;
143 if (!buf || count < sizeof(struct batadv_icmp_packet))
144 return -EINVAL;
146 if (!access_ok(VERIFY_WRITE, buf, count))
147 return -EFAULT;
149 error = wait_event_interruptible(socket_client->queue_wait,
150 socket_client->queue_len);
152 if (error)
153 return error;
155 spin_lock_bh(&socket_client->lock);
157 socket_packet = list_first_entry(&socket_client->queue_list,
158 struct batadv_socket_packet, list);
159 list_del(&socket_packet->list);
160 socket_client->queue_len--;
162 spin_unlock_bh(&socket_client->lock);
164 packet_len = min(count, socket_packet->icmp_len);
165 error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
167 kfree(socket_packet);
169 if (error)
170 return -EFAULT;
172 return packet_len;
175 static ssize_t batadv_socket_write(struct file *file, const char __user *buff,
176 size_t len, loff_t *off)
178 struct batadv_socket_client *socket_client = file->private_data;
179 struct batadv_priv *bat_priv = socket_client->bat_priv;
180 struct batadv_hard_iface *primary_if = NULL;
181 struct sk_buff *skb;
182 struct batadv_icmp_packet_rr *icmp_packet_rr;
183 struct batadv_icmp_header *icmp_header;
184 struct batadv_orig_node *orig_node = NULL;
185 struct batadv_neigh_node *neigh_node = NULL;
186 size_t packet_len = sizeof(struct batadv_icmp_packet);
187 u8 *addr;
189 if (len < sizeof(struct batadv_icmp_header)) {
190 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
191 "Error - can't send packet from char device: invalid packet size\n");
192 return -EINVAL;
195 primary_if = batadv_primary_if_get_selected(bat_priv);
197 if (!primary_if) {
198 len = -EFAULT;
199 goto out;
202 if (len >= BATADV_ICMP_MAX_PACKET_SIZE)
203 packet_len = BATADV_ICMP_MAX_PACKET_SIZE;
204 else
205 packet_len = len;
207 skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN);
208 if (!skb) {
209 len = -ENOMEM;
210 goto out;
213 skb->priority = TC_PRIO_CONTROL;
214 skb_reserve(skb, ETH_HLEN);
215 icmp_header = skb_put(skb, packet_len);
217 if (copy_from_user(icmp_header, buff, packet_len)) {
218 len = -EFAULT;
219 goto free_skb;
222 if (icmp_header->packet_type != BATADV_ICMP) {
223 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
224 "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
225 len = -EINVAL;
226 goto free_skb;
229 switch (icmp_header->msg_type) {
230 case BATADV_ECHO_REQUEST:
231 if (len < sizeof(struct batadv_icmp_packet)) {
232 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
233 "Error - can't send packet from char device: invalid packet size\n");
234 len = -EINVAL;
235 goto free_skb;
238 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
239 goto dst_unreach;
241 orig_node = batadv_orig_hash_find(bat_priv, icmp_header->dst);
242 if (!orig_node)
243 goto dst_unreach;
245 neigh_node = batadv_orig_router_get(orig_node,
246 BATADV_IF_DEFAULT);
247 if (!neigh_node)
248 goto dst_unreach;
250 if (!neigh_node->if_incoming)
251 goto dst_unreach;
253 if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
254 goto dst_unreach;
256 icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmp_header;
257 if (packet_len == sizeof(*icmp_packet_rr)) {
258 addr = neigh_node->if_incoming->net_dev->dev_addr;
259 ether_addr_copy(icmp_packet_rr->rr[0], addr);
262 break;
263 default:
264 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
265 "Error - can't send packet from char device: got unknown message type\n");
266 len = -EINVAL;
267 goto free_skb;
270 icmp_header->uid = socket_client->index;
272 if (icmp_header->version != BATADV_COMPAT_VERSION) {
273 icmp_header->msg_type = BATADV_PARAMETER_PROBLEM;
274 icmp_header->version = BATADV_COMPAT_VERSION;
275 batadv_socket_add_packet(socket_client, icmp_header,
276 packet_len);
277 goto free_skb;
280 ether_addr_copy(icmp_header->orig, primary_if->net_dev->dev_addr);
282 batadv_send_unicast_skb(skb, neigh_node);
283 goto out;
285 dst_unreach:
286 icmp_header->msg_type = BATADV_DESTINATION_UNREACHABLE;
287 batadv_socket_add_packet(socket_client, icmp_header, packet_len);
288 free_skb:
289 kfree_skb(skb);
290 out:
291 if (primary_if)
292 batadv_hardif_put(primary_if);
293 if (neigh_node)
294 batadv_neigh_node_put(neigh_node);
295 if (orig_node)
296 batadv_orig_node_put(orig_node);
297 return len;
300 static __poll_t batadv_socket_poll(struct file *file, poll_table *wait)
302 struct batadv_socket_client *socket_client = file->private_data;
304 poll_wait(file, &socket_client->queue_wait, wait);
306 if (socket_client->queue_len > 0)
307 return EPOLLIN | EPOLLRDNORM;
309 return 0;
312 static const struct file_operations batadv_fops = {
313 .owner = THIS_MODULE,
314 .open = batadv_socket_open,
315 .release = batadv_socket_release,
316 .read = batadv_socket_read,
317 .write = batadv_socket_write,
318 .poll = batadv_socket_poll,
319 .llseek = no_llseek,
323 * batadv_socket_setup() - Create debugfs "socket" file
324 * @bat_priv: the bat priv with all the soft interface information
326 * Return: 0 on success or negative error number in case of failure
328 int batadv_socket_setup(struct batadv_priv *bat_priv)
330 struct dentry *d;
332 if (!bat_priv->debug_dir)
333 goto err;
335 d = debugfs_create_file(BATADV_ICMP_SOCKET, 0600, bat_priv->debug_dir,
336 bat_priv, &batadv_fops);
337 if (!d)
338 goto err;
340 return 0;
342 err:
343 return -ENOMEM;
347 * batadv_socket_add_packet() - schedule an icmp packet to be sent to
348 * userspace on an icmp socket.
349 * @socket_client: the socket this packet belongs to
350 * @icmph: pointer to the header of the icmp packet
351 * @icmp_len: total length of the icmp packet
353 static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
354 struct batadv_icmp_header *icmph,
355 size_t icmp_len)
357 struct batadv_socket_packet *socket_packet;
358 size_t len;
360 socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
362 if (!socket_packet)
363 return;
365 len = icmp_len;
366 /* check the maximum length before filling the buffer */
367 if (len > sizeof(socket_packet->icmp_packet))
368 len = sizeof(socket_packet->icmp_packet);
370 INIT_LIST_HEAD(&socket_packet->list);
371 memcpy(&socket_packet->icmp_packet, icmph, len);
372 socket_packet->icmp_len = len;
374 spin_lock_bh(&socket_client->lock);
376 /* while waiting for the lock the socket_client could have been
377 * deleted
379 if (!batadv_socket_client_hash[icmph->uid]) {
380 spin_unlock_bh(&socket_client->lock);
381 kfree(socket_packet);
382 return;
385 list_add_tail(&socket_packet->list, &socket_client->queue_list);
386 socket_client->queue_len++;
388 if (socket_client->queue_len > 100) {
389 socket_packet = list_first_entry(&socket_client->queue_list,
390 struct batadv_socket_packet,
391 list);
393 list_del(&socket_packet->list);
394 kfree(socket_packet);
395 socket_client->queue_len--;
398 spin_unlock_bh(&socket_client->lock);
400 wake_up(&socket_client->queue_wait);
404 * batadv_socket_receive_packet() - schedule an icmp packet to be received
405 * locally and sent to userspace.
406 * @icmph: pointer to the header of the icmp packet
407 * @icmp_len: total length of the icmp packet
409 void batadv_socket_receive_packet(struct batadv_icmp_header *icmph,
410 size_t icmp_len)
412 struct batadv_socket_client *hash;
414 hash = batadv_socket_client_hash[icmph->uid];
415 if (hash)
416 batadv_socket_add_packet(hash, icmph, icmp_len);