9859ababb82e3d7863ddf3faf1e4c2c1eb757d01
[sfrench/cifs-2.6.git] / net / batman-adv / icmp_socket.c
1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2007-2019  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
19 #include "icmp_socket.h"
20 #include "main.h"
21
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>
49
50 #include "debugfs.h"
51 #include "hard-interface.h"
52 #include "log.h"
53 #include "originator.h"
54 #include "send.h"
55
56 static struct batadv_socket_client *batadv_socket_client_hash[256];
57
58 static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
59                                      struct batadv_icmp_header *icmph,
60                                      size_t icmp_len);
61
62 /**
63  * batadv_socket_init() - Initialize soft interface independent socket data
64  */
65 void batadv_socket_init(void)
66 {
67         memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash));
68 }
69
70 static int batadv_socket_open(struct inode *inode, struct file *file)
71 {
72         unsigned int i;
73         struct batadv_socket_client *socket_client;
74
75         if (!try_module_get(THIS_MODULE))
76                 return -EBUSY;
77
78         batadv_debugfs_deprecated(file, "");
79
80         nonseekable_open(inode, file);
81
82         socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
83         if (!socket_client) {
84                 module_put(THIS_MODULE);
85                 return -ENOMEM;
86         }
87
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;
92                 }
93         }
94
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;
100         }
101
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);
108
109         file->private_data = socket_client;
110
111         return 0;
112 }
113
114 static int batadv_socket_release(struct inode *inode, struct file *file)
115 {
116         struct batadv_socket_client *client = file->private_data;
117         struct batadv_socket_packet *packet, *tmp;
118
119         spin_lock_bh(&client->lock);
120
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);
125         }
126
127         batadv_socket_client_hash[client->index] = NULL;
128         spin_unlock_bh(&client->lock);
129
130         kfree(client);
131         module_put(THIS_MODULE);
132
133         return 0;
134 }
135
136 static ssize_t batadv_socket_read(struct file *file, char __user *buf,
137                                   size_t count, loff_t *ppos)
138 {
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;
143
144         if ((file->f_flags & O_NONBLOCK) && socket_client->queue_len == 0)
145                 return -EAGAIN;
146
147         if (!buf || count < sizeof(struct batadv_icmp_packet))
148                 return -EINVAL;
149
150         if (!access_ok(buf, count))
151                 return -EFAULT;
152
153         error = wait_event_interruptible(socket_client->queue_wait,
154                                          socket_client->queue_len);
155
156         if (error)
157                 return error;
158
159         spin_lock_bh(&socket_client->lock);
160
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--;
165
166         spin_unlock_bh(&socket_client->lock);
167
168         packet_len = min(count, socket_packet->icmp_len);
169         error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
170
171         kfree(socket_packet);
172
173         if (error)
174                 return -EFAULT;
175
176         return packet_len;
177 }
178
179 static ssize_t batadv_socket_write(struct file *file, const char __user *buff,
180                                    size_t len, loff_t *off)
181 {
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;
192
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;
197         }
198
199         primary_if = batadv_primary_if_get_selected(bat_priv);
200
201         if (!primary_if) {
202                 len = -EFAULT;
203                 goto out;
204         }
205
206         if (len >= BATADV_ICMP_MAX_PACKET_SIZE)
207                 packet_len = BATADV_ICMP_MAX_PACKET_SIZE;
208         else
209                 packet_len = len;
210
211         skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN);
212         if (!skb) {
213                 len = -ENOMEM;
214                 goto out;
215         }
216
217         skb->priority = TC_PRIO_CONTROL;
218         skb_reserve(skb, ETH_HLEN);
219         icmp_header = skb_put(skb, packet_len);
220
221         if (copy_from_user(icmp_header, buff, packet_len)) {
222                 len = -EFAULT;
223                 goto free_skb;
224         }
225
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;
231         }
232
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;
240                 }
241
242                 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
243                         goto dst_unreach;
244
245                 orig_node = batadv_orig_hash_find(bat_priv, icmp_header->dst);
246                 if (!orig_node)
247                         goto dst_unreach;
248
249                 neigh_node = batadv_orig_router_get(orig_node,
250                                                     BATADV_IF_DEFAULT);
251                 if (!neigh_node)
252                         goto dst_unreach;
253
254                 if (!neigh_node->if_incoming)
255                         goto dst_unreach;
256
257                 if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
258                         goto dst_unreach;
259
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);
264                 }
265
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;
272         }
273
274         icmp_header->uid = socket_client->index;
275
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;
282         }
283
284         ether_addr_copy(icmp_header->orig, primary_if->net_dev->dev_addr);
285
286         batadv_send_unicast_skb(skb, neigh_node);
287         goto out;
288
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;
302 }
303
304 static __poll_t batadv_socket_poll(struct file *file, poll_table *wait)
305 {
306         struct batadv_socket_client *socket_client = file->private_data;
307
308         poll_wait(file, &socket_client->queue_wait, wait);
309
310         if (socket_client->queue_len > 0)
311                 return EPOLLIN | EPOLLRDNORM;
312
313         return 0;
314 }
315
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,
324 };
325
326 /**
327  * batadv_socket_setup() - Create debugfs "socket" file
328  * @bat_priv: the bat priv with all the soft interface information
329  *
330  * Return: 0 on success or negative error number in case of failure
331  */
332 int batadv_socket_setup(struct batadv_priv *bat_priv)
333 {
334         struct dentry *d;
335
336         if (!bat_priv->debug_dir)
337                 goto err;
338
339         d = debugfs_create_file(BATADV_ICMP_SOCKET, 0600, bat_priv->debug_dir,
340                                 bat_priv, &batadv_fops);
341         if (!d)
342                 goto err;
343
344         return 0;
345
346 err:
347         return -ENOMEM;
348 }
349
350 /**
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
356  */
357 static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
358                                      struct batadv_icmp_header *icmph,
359                                      size_t icmp_len)
360 {
361         struct batadv_socket_packet *socket_packet;
362         size_t len;
363
364         socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
365
366         if (!socket_packet)
367                 return;
368
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);
373
374         INIT_LIST_HEAD(&socket_packet->list);
375         memcpy(&socket_packet->icmp_packet, icmph, len);
376         socket_packet->icmp_len = len;
377
378         spin_lock_bh(&socket_client->lock);
379
380         /* while waiting for the lock the socket_client could have been
381          * deleted
382          */
383         if (!batadv_socket_client_hash[icmph->uid]) {
384                 spin_unlock_bh(&socket_client->lock);
385                 kfree(socket_packet);
386                 return;
387         }
388
389         list_add_tail(&socket_packet->list, &socket_client->queue_list);
390         socket_client->queue_len++;
391
392         if (socket_client->queue_len > 100) {
393                 socket_packet = list_first_entry(&socket_client->queue_list,
394                                                  struct batadv_socket_packet,
395                                                  list);
396
397                 list_del(&socket_packet->list);
398                 kfree(socket_packet);
399                 socket_client->queue_len--;
400         }
401
402         spin_unlock_bh(&socket_client->lock);
403
404         wake_up(&socket_client->queue_wait);
405 }
406
407 /**
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
412  */
413 void batadv_socket_receive_packet(struct batadv_icmp_header *icmph,
414                                   size_t icmp_len)
415 {
416         struct batadv_socket_client *hash;
417
418         hash = batadv_socket_client_hash[icmph->uid];
419         if (hash)
420                 batadv_socket_add_packet(hash, icmph, icmp_len);
421 }