1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2007-2017 B.A.T.M.A.N. contributors:
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"
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>
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"
51 #include "originator.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,
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)
71 struct batadv_socket_client *socket_client;
73 if (!try_module_get(THIS_MODULE))
76 nonseekable_open(inode, file);
78 socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
80 module_put(THIS_MODULE);
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;
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");
94 module_put(THIS_MODULE);
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;
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);
123 batadv_socket_client_hash[client->index] = NULL;
124 spin_unlock_bh(&client->lock);
127 module_put(THIS_MODULE);
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;
140 if ((file->f_flags & O_NONBLOCK) && socket_client->queue_len == 0)
143 if (!buf || count < sizeof(struct batadv_icmp_packet))
146 if (!access_ok(VERIFY_WRITE, buf, count))
149 error = wait_event_interruptible(socket_client->queue_wait,
150 socket_client->queue_len);
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);
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;
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);
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");
195 primary_if = batadv_primary_if_get_selected(bat_priv);
202 if (len >= BATADV_ICMP_MAX_PACKET_SIZE)
203 packet_len = BATADV_ICMP_MAX_PACKET_SIZE;
207 skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN);
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)) {
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");
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");
238 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
241 orig_node = batadv_orig_hash_find(bat_priv, icmp_header->dst);
245 neigh_node = batadv_orig_router_get(orig_node,
250 if (!neigh_node->if_incoming)
253 if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
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);
264 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
265 "Error - can't send packet from char device: got unknown message type\n");
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,
280 ether_addr_copy(icmp_header->orig, primary_if->net_dev->dev_addr);
282 batadv_send_unicast_skb(skb, neigh_node);
286 icmp_header->msg_type = BATADV_DESTINATION_UNREACHABLE;
287 batadv_socket_add_packet(socket_client, icmp_header, packet_len);
292 batadv_hardif_put(primary_if);
294 batadv_neigh_node_put(neigh_node);
296 batadv_orig_node_put(orig_node);
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 POLLIN | POLLRDNORM;
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,
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)
332 if (!bat_priv->debug_dir)
335 d = debugfs_create_file(BATADV_ICMP_SOCKET, 0600, bat_priv->debug_dir,
336 bat_priv, &batadv_fops);
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,
357 struct batadv_socket_packet *socket_packet;
360 socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
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
379 if (!batadv_socket_client_hash[icmph->uid]) {
380 spin_unlock_bh(&socket_client->lock);
381 kfree(socket_packet);
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,
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,
412 struct batadv_socket_client *hash;
414 hash = batadv_socket_client_hash[icmph->uid];
416 batadv_socket_add_packet(hash, icmph, icmp_len);