mtd: rawnand: marvell: fix command xtype in BCH write hook
[linux/fpc-iii.git] / net / batman-adv / icmp_socket.c
blob5daa3d50da17721d56abccc644c63460fa4b9050
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/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 "hard-interface.h"
51 #include "log.h"
52 #include "originator.h"
53 #include "send.h"
55 static struct batadv_socket_client *batadv_socket_client_hash[256];
57 static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
58 struct batadv_icmp_header *icmph,
59 size_t icmp_len);
61 /**
62 * batadv_socket_init() - Initialize soft interface independent socket data
64 void batadv_socket_init(void)
66 memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash));
69 static int batadv_socket_open(struct inode *inode, struct file *file)
71 unsigned int i;
72 struct batadv_socket_client *socket_client;
74 if (!try_module_get(THIS_MODULE))
75 return -EBUSY;
77 nonseekable_open(inode, file);
79 socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
80 if (!socket_client) {
81 module_put(THIS_MODULE);
82 return -ENOMEM;
85 for (i = 0; i < ARRAY_SIZE(batadv_socket_client_hash); i++) {
86 if (!batadv_socket_client_hash[i]) {
87 batadv_socket_client_hash[i] = socket_client;
88 break;
92 if (i == ARRAY_SIZE(batadv_socket_client_hash)) {
93 pr_err("Error - can't add another packet client: maximum number of clients reached\n");
94 kfree(socket_client);
95 module_put(THIS_MODULE);
96 return -EXFULL;
99 INIT_LIST_HEAD(&socket_client->queue_list);
100 socket_client->queue_len = 0;
101 socket_client->index = i;
102 socket_client->bat_priv = inode->i_private;
103 spin_lock_init(&socket_client->lock);
104 init_waitqueue_head(&socket_client->queue_wait);
106 file->private_data = socket_client;
108 return 0;
111 static int batadv_socket_release(struct inode *inode, struct file *file)
113 struct batadv_socket_client *client = file->private_data;
114 struct batadv_socket_packet *packet, *tmp;
116 spin_lock_bh(&client->lock);
118 /* for all packets in the queue ... */
119 list_for_each_entry_safe(packet, tmp, &client->queue_list, list) {
120 list_del(&packet->list);
121 kfree(packet);
124 batadv_socket_client_hash[client->index] = NULL;
125 spin_unlock_bh(&client->lock);
127 kfree(client);
128 module_put(THIS_MODULE);
130 return 0;
133 static ssize_t batadv_socket_read(struct file *file, char __user *buf,
134 size_t count, loff_t *ppos)
136 struct batadv_socket_client *socket_client = file->private_data;
137 struct batadv_socket_packet *socket_packet;
138 size_t packet_len;
139 int error;
141 if ((file->f_flags & O_NONBLOCK) && socket_client->queue_len == 0)
142 return -EAGAIN;
144 if (!buf || count < sizeof(struct batadv_icmp_packet))
145 return -EINVAL;
147 if (!access_ok(VERIFY_WRITE, buf, count))
148 return -EFAULT;
150 error = wait_event_interruptible(socket_client->queue_wait,
151 socket_client->queue_len);
153 if (error)
154 return error;
156 spin_lock_bh(&socket_client->lock);
158 socket_packet = list_first_entry(&socket_client->queue_list,
159 struct batadv_socket_packet, list);
160 list_del(&socket_packet->list);
161 socket_client->queue_len--;
163 spin_unlock_bh(&socket_client->lock);
165 packet_len = min(count, socket_packet->icmp_len);
166 error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
168 kfree(socket_packet);
170 if (error)
171 return -EFAULT;
173 return packet_len;
176 static ssize_t batadv_socket_write(struct file *file, const char __user *buff,
177 size_t len, loff_t *off)
179 struct batadv_socket_client *socket_client = file->private_data;
180 struct batadv_priv *bat_priv = socket_client->bat_priv;
181 struct batadv_hard_iface *primary_if = NULL;
182 struct sk_buff *skb;
183 struct batadv_icmp_packet_rr *icmp_packet_rr;
184 struct batadv_icmp_header *icmp_header;
185 struct batadv_orig_node *orig_node = NULL;
186 struct batadv_neigh_node *neigh_node = NULL;
187 size_t packet_len = sizeof(struct batadv_icmp_packet);
188 u8 *addr;
190 if (len < sizeof(struct batadv_icmp_header)) {
191 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
192 "Error - can't send packet from char device: invalid packet size\n");
193 return -EINVAL;
196 primary_if = batadv_primary_if_get_selected(bat_priv);
198 if (!primary_if) {
199 len = -EFAULT;
200 goto out;
203 if (len >= BATADV_ICMP_MAX_PACKET_SIZE)
204 packet_len = BATADV_ICMP_MAX_PACKET_SIZE;
205 else
206 packet_len = len;
208 skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN);
209 if (!skb) {
210 len = -ENOMEM;
211 goto out;
214 skb->priority = TC_PRIO_CONTROL;
215 skb_reserve(skb, ETH_HLEN);
216 icmp_header = skb_put(skb, packet_len);
218 if (copy_from_user(icmp_header, buff, packet_len)) {
219 len = -EFAULT;
220 goto free_skb;
223 if (icmp_header->packet_type != BATADV_ICMP) {
224 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
225 "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
226 len = -EINVAL;
227 goto free_skb;
230 switch (icmp_header->msg_type) {
231 case BATADV_ECHO_REQUEST:
232 if (len < sizeof(struct batadv_icmp_packet)) {
233 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
234 "Error - can't send packet from char device: invalid packet size\n");
235 len = -EINVAL;
236 goto free_skb;
239 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
240 goto dst_unreach;
242 orig_node = batadv_orig_hash_find(bat_priv, icmp_header->dst);
243 if (!orig_node)
244 goto dst_unreach;
246 neigh_node = batadv_orig_router_get(orig_node,
247 BATADV_IF_DEFAULT);
248 if (!neigh_node)
249 goto dst_unreach;
251 if (!neigh_node->if_incoming)
252 goto dst_unreach;
254 if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
255 goto dst_unreach;
257 icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmp_header;
258 if (packet_len == sizeof(*icmp_packet_rr)) {
259 addr = neigh_node->if_incoming->net_dev->dev_addr;
260 ether_addr_copy(icmp_packet_rr->rr[0], addr);
263 break;
264 default:
265 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
266 "Error - can't send packet from char device: got unknown message type\n");
267 len = -EINVAL;
268 goto free_skb;
271 icmp_header->uid = socket_client->index;
273 if (icmp_header->version != BATADV_COMPAT_VERSION) {
274 icmp_header->msg_type = BATADV_PARAMETER_PROBLEM;
275 icmp_header->version = BATADV_COMPAT_VERSION;
276 batadv_socket_add_packet(socket_client, icmp_header,
277 packet_len);
278 goto free_skb;
281 ether_addr_copy(icmp_header->orig, primary_if->net_dev->dev_addr);
283 batadv_send_unicast_skb(skb, neigh_node);
284 goto out;
286 dst_unreach:
287 icmp_header->msg_type = BATADV_DESTINATION_UNREACHABLE;
288 batadv_socket_add_packet(socket_client, icmp_header, packet_len);
289 free_skb:
290 kfree_skb(skb);
291 out:
292 if (primary_if)
293 batadv_hardif_put(primary_if);
294 if (neigh_node)
295 batadv_neigh_node_put(neigh_node);
296 if (orig_node)
297 batadv_orig_node_put(orig_node);
298 return len;
301 static __poll_t batadv_socket_poll(struct file *file, poll_table *wait)
303 struct batadv_socket_client *socket_client = file->private_data;
305 poll_wait(file, &socket_client->queue_wait, wait);
307 if (socket_client->queue_len > 0)
308 return EPOLLIN | EPOLLRDNORM;
310 return 0;
313 static const struct file_operations batadv_fops = {
314 .owner = THIS_MODULE,
315 .open = batadv_socket_open,
316 .release = batadv_socket_release,
317 .read = batadv_socket_read,
318 .write = batadv_socket_write,
319 .poll = batadv_socket_poll,
320 .llseek = no_llseek,
324 * batadv_socket_setup() - Create debugfs "socket" file
325 * @bat_priv: the bat priv with all the soft interface information
327 * Return: 0 on success or negative error number in case of failure
329 int batadv_socket_setup(struct batadv_priv *bat_priv)
331 struct dentry *d;
333 if (!bat_priv->debug_dir)
334 goto err;
336 d = debugfs_create_file(BATADV_ICMP_SOCKET, 0600, bat_priv->debug_dir,
337 bat_priv, &batadv_fops);
338 if (!d)
339 goto err;
341 return 0;
343 err:
344 return -ENOMEM;
348 * batadv_socket_add_packet() - schedule an icmp packet to be sent to
349 * userspace on an icmp socket.
350 * @socket_client: the socket this packet belongs to
351 * @icmph: pointer to the header of the icmp packet
352 * @icmp_len: total length of the icmp packet
354 static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
355 struct batadv_icmp_header *icmph,
356 size_t icmp_len)
358 struct batadv_socket_packet *socket_packet;
359 size_t len;
361 socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
363 if (!socket_packet)
364 return;
366 len = icmp_len;
367 /* check the maximum length before filling the buffer */
368 if (len > sizeof(socket_packet->icmp_packet))
369 len = sizeof(socket_packet->icmp_packet);
371 INIT_LIST_HEAD(&socket_packet->list);
372 memcpy(&socket_packet->icmp_packet, icmph, len);
373 socket_packet->icmp_len = len;
375 spin_lock_bh(&socket_client->lock);
377 /* while waiting for the lock the socket_client could have been
378 * deleted
380 if (!batadv_socket_client_hash[icmph->uid]) {
381 spin_unlock_bh(&socket_client->lock);
382 kfree(socket_packet);
383 return;
386 list_add_tail(&socket_packet->list, &socket_client->queue_list);
387 socket_client->queue_len++;
389 if (socket_client->queue_len > 100) {
390 socket_packet = list_first_entry(&socket_client->queue_list,
391 struct batadv_socket_packet,
392 list);
394 list_del(&socket_packet->list);
395 kfree(socket_packet);
396 socket_client->queue_len--;
399 spin_unlock_bh(&socket_client->lock);
401 wake_up(&socket_client->queue_wait);
405 * batadv_socket_receive_packet() - schedule an icmp packet to be received
406 * locally and sent to userspace.
407 * @icmph: pointer to the header of the icmp packet
408 * @icmp_len: total length of the icmp packet
410 void batadv_socket_receive_packet(struct batadv_icmp_header *icmph,
411 size_t icmp_len)
413 struct batadv_socket_client *hash;
415 hash = batadv_socket_client_hash[icmph->uid];
416 if (hash)
417 batadv_socket_add_packet(hash, icmph, icmp_len);