1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2007-2020 B.A.T.M.A.N. contributors:
7 #include "icmp_socket.h"
10 #include <linux/atomic.h>
11 #include <linux/compiler.h>
12 #include <linux/debugfs.h>
13 #include <linux/errno.h>
14 #include <linux/etherdevice.h>
15 #include <linux/eventpoll.h>
16 #include <linux/export.h>
17 #include <linux/fcntl.h>
19 #include <linux/gfp.h>
20 #include <linux/if_ether.h>
21 #include <linux/kernel.h>
22 #include <linux/list.h>
23 #include <linux/module.h>
24 #include <linux/netdevice.h>
25 #include <linux/pkt_sched.h>
26 #include <linux/poll.h>
27 #include <linux/printk.h>
28 #include <linux/sched.h> /* for linux/wait.h */
29 #include <linux/skbuff.h>
30 #include <linux/slab.h>
31 #include <linux/spinlock.h>
32 #include <linux/stddef.h>
33 #include <linux/string.h>
34 #include <linux/uaccess.h>
35 #include <linux/wait.h>
36 #include <uapi/linux/batadv_packet.h>
39 #include "hard-interface.h"
41 #include "originator.h"
44 static struct batadv_socket_client *batadv_socket_client_hash[256];
46 static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
47 struct batadv_icmp_header *icmph,
51 * batadv_socket_init() - Initialize soft interface independent socket data
53 void batadv_socket_init(void)
55 memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash));
58 static int batadv_socket_open(struct inode *inode, struct file *file)
61 struct batadv_socket_client *socket_client;
63 if (!try_module_get(THIS_MODULE))
66 batadv_debugfs_deprecated(file, "");
68 stream_open(inode, file);
70 socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
72 module_put(THIS_MODULE);
76 for (i = 0; i < ARRAY_SIZE(batadv_socket_client_hash); i++) {
77 if (!batadv_socket_client_hash[i]) {
78 batadv_socket_client_hash[i] = socket_client;
83 if (i == ARRAY_SIZE(batadv_socket_client_hash)) {
84 pr_err("Error - can't add another packet client: maximum number of clients reached\n");
86 module_put(THIS_MODULE);
90 INIT_LIST_HEAD(&socket_client->queue_list);
91 socket_client->queue_len = 0;
92 socket_client->index = i;
93 socket_client->bat_priv = inode->i_private;
94 spin_lock_init(&socket_client->lock);
95 init_waitqueue_head(&socket_client->queue_wait);
97 file->private_data = socket_client;
102 static int batadv_socket_release(struct inode *inode, struct file *file)
104 struct batadv_socket_client *client = file->private_data;
105 struct batadv_socket_packet *packet, *tmp;
107 spin_lock_bh(&client->lock);
109 /* for all packets in the queue ... */
110 list_for_each_entry_safe(packet, tmp, &client->queue_list, list) {
111 list_del(&packet->list);
115 batadv_socket_client_hash[client->index] = NULL;
116 spin_unlock_bh(&client->lock);
119 module_put(THIS_MODULE);
124 static ssize_t batadv_socket_read(struct file *file, char __user *buf,
125 size_t count, loff_t *ppos)
127 struct batadv_socket_client *socket_client = file->private_data;
128 struct batadv_socket_packet *socket_packet;
132 if ((file->f_flags & O_NONBLOCK) && socket_client->queue_len == 0)
135 if (!buf || count < sizeof(struct batadv_icmp_packet))
138 error = wait_event_interruptible(socket_client->queue_wait,
139 socket_client->queue_len);
144 spin_lock_bh(&socket_client->lock);
146 socket_packet = list_first_entry(&socket_client->queue_list,
147 struct batadv_socket_packet, list);
148 list_del(&socket_packet->list);
149 socket_client->queue_len--;
151 spin_unlock_bh(&socket_client->lock);
153 packet_len = min(count, socket_packet->icmp_len);
154 error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
156 kfree(socket_packet);
164 static ssize_t batadv_socket_write(struct file *file, const char __user *buff,
165 size_t len, loff_t *off)
167 struct batadv_socket_client *socket_client = file->private_data;
168 struct batadv_priv *bat_priv = socket_client->bat_priv;
169 struct batadv_hard_iface *primary_if = NULL;
171 struct batadv_icmp_packet_rr *icmp_packet_rr;
172 struct batadv_icmp_header *icmp_header;
173 struct batadv_orig_node *orig_node = NULL;
174 struct batadv_neigh_node *neigh_node = NULL;
175 size_t packet_len = sizeof(struct batadv_icmp_packet);
178 if (len < sizeof(struct batadv_icmp_header)) {
179 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
180 "Error - can't send packet from char device: invalid packet size\n");
184 primary_if = batadv_primary_if_get_selected(bat_priv);
191 if (len >= BATADV_ICMP_MAX_PACKET_SIZE)
192 packet_len = BATADV_ICMP_MAX_PACKET_SIZE;
196 skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN);
202 skb->priority = TC_PRIO_CONTROL;
203 skb_reserve(skb, ETH_HLEN);
204 icmp_header = skb_put(skb, packet_len);
206 if (copy_from_user(icmp_header, buff, packet_len)) {
211 if (icmp_header->packet_type != BATADV_ICMP) {
212 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
213 "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
218 switch (icmp_header->msg_type) {
219 case BATADV_ECHO_REQUEST:
220 if (len < sizeof(struct batadv_icmp_packet)) {
221 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
222 "Error - can't send packet from char device: invalid packet size\n");
227 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
230 orig_node = batadv_orig_hash_find(bat_priv, icmp_header->dst);
234 neigh_node = batadv_orig_router_get(orig_node,
239 if (!neigh_node->if_incoming)
242 if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
245 icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmp_header;
246 if (packet_len == sizeof(*icmp_packet_rr)) {
247 addr = neigh_node->if_incoming->net_dev->dev_addr;
248 ether_addr_copy(icmp_packet_rr->rr[0], addr);
253 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
254 "Error - can't send packet from char device: got unknown message type\n");
259 icmp_header->uid = socket_client->index;
261 if (icmp_header->version != BATADV_COMPAT_VERSION) {
262 icmp_header->msg_type = BATADV_PARAMETER_PROBLEM;
263 icmp_header->version = BATADV_COMPAT_VERSION;
264 batadv_socket_add_packet(socket_client, icmp_header,
269 ether_addr_copy(icmp_header->orig, primary_if->net_dev->dev_addr);
271 batadv_send_unicast_skb(skb, neigh_node);
275 icmp_header->msg_type = BATADV_DESTINATION_UNREACHABLE;
276 batadv_socket_add_packet(socket_client, icmp_header, packet_len);
281 batadv_hardif_put(primary_if);
283 batadv_neigh_node_put(neigh_node);
285 batadv_orig_node_put(orig_node);
289 static __poll_t batadv_socket_poll(struct file *file, poll_table *wait)
291 struct batadv_socket_client *socket_client = file->private_data;
293 poll_wait(file, &socket_client->queue_wait, wait);
295 if (socket_client->queue_len > 0)
296 return EPOLLIN | EPOLLRDNORM;
301 static const struct file_operations batadv_fops = {
302 .owner = THIS_MODULE,
303 .open = batadv_socket_open,
304 .release = batadv_socket_release,
305 .read = batadv_socket_read,
306 .write = batadv_socket_write,
307 .poll = batadv_socket_poll,
312 * batadv_socket_setup() - Create debugfs "socket" file
313 * @bat_priv: the bat priv with all the soft interface information
315 void batadv_socket_setup(struct batadv_priv *bat_priv)
317 debugfs_create_file(BATADV_ICMP_SOCKET, 0600, bat_priv->debug_dir,
318 bat_priv, &batadv_fops);
322 * batadv_socket_add_packet() - schedule an icmp packet to be sent to
323 * userspace on an icmp socket.
324 * @socket_client: the socket this packet belongs to
325 * @icmph: pointer to the header of the icmp packet
326 * @icmp_len: total length of the icmp packet
328 static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
329 struct batadv_icmp_header *icmph,
332 struct batadv_socket_packet *socket_packet;
335 socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
341 /* check the maximum length before filling the buffer */
342 if (len > sizeof(socket_packet->icmp_packet))
343 len = sizeof(socket_packet->icmp_packet);
345 INIT_LIST_HEAD(&socket_packet->list);
346 memcpy(&socket_packet->icmp_packet, icmph, len);
347 socket_packet->icmp_len = len;
349 spin_lock_bh(&socket_client->lock);
351 /* while waiting for the lock the socket_client could have been
354 if (!batadv_socket_client_hash[icmph->uid]) {
355 spin_unlock_bh(&socket_client->lock);
356 kfree(socket_packet);
360 list_add_tail(&socket_packet->list, &socket_client->queue_list);
361 socket_client->queue_len++;
363 if (socket_client->queue_len > 100) {
364 socket_packet = list_first_entry(&socket_client->queue_list,
365 struct batadv_socket_packet,
368 list_del(&socket_packet->list);
369 kfree(socket_packet);
370 socket_client->queue_len--;
373 spin_unlock_bh(&socket_client->lock);
375 wake_up(&socket_client->queue_wait);
379 * batadv_socket_receive_packet() - schedule an icmp packet to be received
380 * locally and sent to userspace.
381 * @icmph: pointer to the header of the icmp packet
382 * @icmp_len: total length of the icmp packet
384 void batadv_socket_receive_packet(struct batadv_icmp_header *icmph,
387 struct batadv_socket_client *hash;
389 hash = batadv_socket_client_hash[icmph->uid];
391 batadv_socket_add_packet(hash, icmph, icmp_len);