blob: d34bb79e0405eecdf7f30773cadafb00d2a107ad [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmann7a79d712018-12-31 23:59:59 +01002/* Copyright (C) 2007-2019 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00003 *
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
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010016 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000017 */
18
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000019#include "icmp_socket.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020020#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>
Sven Eckelmann48881ed2018-03-18 09:48:03 +010027#include <linux/eventpoll.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020028#include <linux/export.h>
29#include <linux/fcntl.h>
30#include <linux/fs.h>
Sven Eckelmannb92b94a2017-11-19 17:12:02 +010031#include <linux/gfp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020032#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>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020044#include <linux/stddef.h>
45#include <linux/string.h>
46#include <linux/uaccess.h>
47#include <linux/wait.h>
Sven Eckelmannfec149f2017-12-21 10:17:41 +010048#include <uapi/linux/batadv_packet.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020049
Sven Eckelmann00caf6a2018-08-10 23:36:15 +020050#include "debugfs.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000051#include "hard-interface.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020052#include "log.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020053#include "originator.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020054#include "send.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000055
Sven Eckelmann56303d32012-06-05 22:31:31 +020056static struct batadv_socket_client *batadv_socket_client_hash[256];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000057
Sven Eckelmann56303d32012-06-05 22:31:31 +020058static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
Simon Wunderlichda6b8c22013-10-22 22:50:09 +020059 struct batadv_icmp_header *icmph,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020060 size_t icmp_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000061
Sven Eckelmannff15c272017-12-02 19:51:53 +010062/**
63 * batadv_socket_init() - Initialize soft interface independent socket data
64 */
Sven Eckelmann9039dc72012-05-12 02:09:33 +020065void batadv_socket_init(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000066{
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020067 memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000068}
69
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020070static int batadv_socket_open(struct inode *inode, struct file *file)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000071{
72 unsigned int i;
Sven Eckelmann56303d32012-06-05 22:31:31 +020073 struct batadv_socket_client *socket_client;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000074
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +020075 if (!try_module_get(THIS_MODULE))
76 return -EBUSY;
77
Sven Eckelmann00caf6a2018-08-10 23:36:15 +020078 batadv_debugfs_deprecated(file, "");
79
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000080 nonseekable_open(inode, file);
81
Sven Eckelmann704509b2011-05-14 23:14:54 +020082 socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +020083 if (!socket_client) {
84 module_put(THIS_MODULE);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000085 return -ENOMEM;
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +020086 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000087
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020088 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;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000091 break;
92 }
93 }
94
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020095 if (i == ARRAY_SIZE(batadv_socket_client_hash)) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +010096 pr_err("Error - can't add another packet client: maximum number of clients reached\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000097 kfree(socket_client);
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +020098 module_put(THIS_MODULE);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000099 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
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000111 return 0;
112}
113
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200114static int batadv_socket_release(struct inode *inode, struct file *file)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000115{
Geliang Tangfb1f23e2015-12-18 23:33:31 +0800116 struct batadv_socket_client *client = file->private_data;
117 struct batadv_socket_packet *packet, *tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000118
Geliang Tangfb1f23e2015-12-18 23:33:31 +0800119 spin_lock_bh(&client->lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000120
121 /* for all packets in the queue ... */
Geliang Tangfb1f23e2015-12-18 23:33:31 +0800122 list_for_each_entry_safe(packet, tmp, &client->queue_list, list) {
123 list_del(&packet->list);
124 kfree(packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000125 }
126
Geliang Tangfb1f23e2015-12-18 23:33:31 +0800127 batadv_socket_client_hash[client->index] = NULL;
128 spin_unlock_bh(&client->lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000129
Geliang Tangfb1f23e2015-12-18 23:33:31 +0800130 kfree(client);
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +0200131 module_put(THIS_MODULE);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000132
133 return 0;
134}
135
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200136static ssize_t batadv_socket_read(struct file *file, char __user *buf,
137 size_t count, loff_t *ppos)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000138{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200139 struct batadv_socket_client *socket_client = file->private_data;
140 struct batadv_socket_packet *socket_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000141 size_t packet_len;
142 int error;
143
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200144 if ((file->f_flags & O_NONBLOCK) && socket_client->queue_len == 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000145 return -EAGAIN;
146
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200147 if (!buf || count < sizeof(struct batadv_icmp_packet))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000148 return -EINVAL;
149
150 if (!access_ok(VERIFY_WRITE, 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,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200162 struct batadv_socket_packet, list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000163 list_del(&socket_packet->list);
164 socket_client->queue_len--;
165
166 spin_unlock_bh(&socket_client->lock);
167
Sven Eckelmannb5a1eee2011-12-10 15:28:36 +0100168 packet_len = min(count, socket_packet->icmp_len);
169 error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000170
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000171 kfree(socket_packet);
172
173 if (error)
174 return -EFAULT;
175
176 return packet_len;
177}
178
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200179static ssize_t batadv_socket_write(struct file *file, const char __user *buff,
180 size_t len, loff_t *off)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000181{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200182 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;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000185 struct sk_buff *skb;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200186 struct batadv_icmp_packet_rr *icmp_packet_rr;
187 struct batadv_icmp_header *icmp_header;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200188 struct batadv_orig_node *orig_node = NULL;
189 struct batadv_neigh_node *neigh_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200190 size_t packet_len = sizeof(struct batadv_icmp_packet);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200191 u8 *addr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000192
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200193 if (len < sizeof(struct batadv_icmp_header)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200194 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200195 "Error - can't send packet from char device: invalid packet size\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000196 return -EINVAL;
197 }
198
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200199 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200200
201 if (!primary_if) {
202 len = -EFAULT;
203 goto out;
204 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000205
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200206 if (len >= BATADV_ICMP_MAX_PACKET_SIZE)
207 packet_len = BATADV_ICMP_MAX_PACKET_SIZE;
208 else
209 packet_len = len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000210
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200211 skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200212 if (!skb) {
213 len = -ENOMEM;
214 goto out;
215 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000216
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200217 skb->priority = TC_PRIO_CONTROL;
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200218 skb_reserve(skb, ETH_HLEN);
Johannes Berg4df864c2017-06-16 14:29:21 +0200219 icmp_header = skb_put(skb, packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000220
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200221 if (copy_from_user(icmp_header, buff, packet_len)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000222 len = -EFAULT;
223 goto free_skb;
224 }
225
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100226 if (icmp_header->packet_type != BATADV_ICMP) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200227 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200228 "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000229 len = -EINVAL;
230 goto free_skb;
231 }
232
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200233 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
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100249 neigh_node = batadv_orig_router_get(orig_node,
250 BATADV_IF_DEFAULT);
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200251 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;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100261 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 }
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200265
266 break;
267 default:
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200268 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200269 "Error - can't send packet from char device: got unknown message type\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000270 len = -EINVAL;
271 goto free_skb;
272 }
273
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200274 icmp_header->uid = socket_client->index;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000275
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100276 if (icmp_header->version != BATADV_COMPAT_VERSION) {
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200277 icmp_header->msg_type = BATADV_PARAMETER_PROBLEM;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100278 icmp_header->version = BATADV_COMPAT_VERSION;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200279 batadv_socket_add_packet(socket_client, icmp_header,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200280 packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000281 goto free_skb;
282 }
283
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100284 ether_addr_copy(icmp_header->orig, primary_if->net_dev->dev_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000285
Antonio Quartulli95d39272016-01-16 16:40:15 +0800286 batadv_send_unicast_skb(skb, neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000287 goto out;
288
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000289dst_unreach:
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200290 icmp_header->msg_type = BATADV_DESTINATION_UNREACHABLE;
291 batadv_socket_add_packet(socket_client, icmp_header, packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000292free_skb:
293 kfree_skb(skb);
294out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200295 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100296 batadv_hardif_put(primary_if);
Marek Lindner44524fc2011-02-10 14:33:53 +0000297 if (neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100298 batadv_neigh_node_put(neigh_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000299 if (orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +0100300 batadv_orig_node_put(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000301 return len;
302}
303
Al Viroade994f2017-07-03 00:01:49 -0400304static __poll_t batadv_socket_poll(struct file *file, poll_table *wait)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000305{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200306 struct batadv_socket_client *socket_client = file->private_data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000307
308 poll_wait(file, &socket_client->queue_wait, wait);
309
310 if (socket_client->queue_len > 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800311 return EPOLLIN | EPOLLRDNORM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000312
313 return 0;
314}
315
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200316static const struct file_operations batadv_fops = {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000317 .owner = THIS_MODULE,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200318 .open = batadv_socket_open,
319 .release = batadv_socket_release,
320 .read = batadv_socket_read,
321 .write = batadv_socket_write,
322 .poll = batadv_socket_poll,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000323 .llseek = no_llseek,
324};
325
Sven Eckelmannff15c272017-12-02 19:51:53 +0100326/**
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 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200332int batadv_socket_setup(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000333{
334 struct dentry *d;
335
336 if (!bat_priv->debug_dir)
337 goto err;
338
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200339 d = debugfs_create_file(BATADV_ICMP_SOCKET, 0600, bat_priv->debug_dir,
340 bat_priv, &batadv_fops);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200341 if (!d)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000342 goto err;
343
344 return 0;
345
346err:
Sven Eckelmann5346c352012-05-05 13:27:28 +0200347 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000348}
349
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200350/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100351 * batadv_socket_add_packet() - schedule an icmp packet to be sent to
Sven Eckelmann34473822015-05-31 10:10:20 +0200352 * userspace on an icmp socket.
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200353 * @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 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200357static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200358 struct batadv_icmp_header *icmph,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200359 size_t icmp_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000360{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200361 struct batadv_socket_packet *socket_packet;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200362 size_t len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000363
Sven Eckelmann704509b2011-05-14 23:14:54 +0200364 socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000365
366 if (!socket_packet)
367 return;
368
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200369 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
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000374 INIT_LIST_HEAD(&socket_packet->list);
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200375 memcpy(&socket_packet->icmp_packet, icmph, len);
376 socket_packet->icmp_len = len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000377
378 spin_lock_bh(&socket_client->lock);
379
380 /* while waiting for the lock the socket_client could have been
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200381 * deleted
382 */
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200383 if (!batadv_socket_client_hash[icmph->uid]) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000384 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,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200394 struct batadv_socket_packet,
395 list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000396
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
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200407/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100408 * batadv_socket_receive_packet() - schedule an icmp packet to be received
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200409 * 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 */
413void batadv_socket_receive_packet(struct batadv_icmp_header *icmph,
Sven Eckelmann9039dc72012-05-12 02:09:33 +0200414 size_t icmp_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000415{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200416 struct batadv_socket_client *hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000417
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200418 hash = batadv_socket_client_hash[icmph->uid];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000419 if (hash)
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200420 batadv_socket_add_packet(hash, icmph, icmp_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000421}