blob: e91f29c7c638a5f9fede10b08cdd11f37d2ee1e6 [file] [log] [blame]
Sven Eckelmann7db7d9f2017-11-19 15:05:11 +01001// SPDX-License-Identifier: GPL-2.0
Sven Eckelmannac79cbb2017-01-01 00:00:00 +01002/* Copyright (C) 2007-2017 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>
27#include <linux/export.h>
28#include <linux/fcntl.h>
29#include <linux/fs.h>
Sven Eckelmannb92b94a2017-11-19 17:12:02 +010030#include <linux/gfp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020031#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>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020043#include <linux/stddef.h>
44#include <linux/string.h>
45#include <linux/uaccess.h>
46#include <linux/wait.h>
Sven Eckelmannfec149f2017-12-21 10:17:41 +010047#include <uapi/linux/batadv_packet.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020048
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000049#include "hard-interface.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020050#include "log.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020051#include "originator.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020052#include "send.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000053
Sven Eckelmann56303d32012-06-05 22:31:31 +020054static struct batadv_socket_client *batadv_socket_client_hash[256];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000055
Sven Eckelmann56303d32012-06-05 22:31:31 +020056static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
Simon Wunderlichda6b8c22013-10-22 22:50:09 +020057 struct batadv_icmp_header *icmph,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020058 size_t icmp_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000059
Sven Eckelmannff15c272017-12-02 19:51:53 +010060/**
61 * batadv_socket_init() - Initialize soft interface independent socket data
62 */
Sven Eckelmann9039dc72012-05-12 02:09:33 +020063void batadv_socket_init(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000064{
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020065 memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000066}
67
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020068static int batadv_socket_open(struct inode *inode, struct file *file)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000069{
70 unsigned int i;
Sven Eckelmann56303d32012-06-05 22:31:31 +020071 struct batadv_socket_client *socket_client;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000072
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +020073 if (!try_module_get(THIS_MODULE))
74 return -EBUSY;
75
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000076 nonseekable_open(inode, file);
77
Sven Eckelmann704509b2011-05-14 23:14:54 +020078 socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +020079 if (!socket_client) {
80 module_put(THIS_MODULE);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000081 return -ENOMEM;
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +020082 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000083
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020084 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;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000087 break;
88 }
89 }
90
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020091 if (i == ARRAY_SIZE(batadv_socket_client_hash)) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +010092 pr_err("Error - can't add another packet client: maximum number of clients reached\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000093 kfree(socket_client);
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +020094 module_put(THIS_MODULE);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000095 return -EXFULL;
96 }
97
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);
104
105 file->private_data = socket_client;
106
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000107 return 0;
108}
109
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200110static int batadv_socket_release(struct inode *inode, struct file *file)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000111{
Geliang Tangfb1f23e2015-12-18 23:33:31 +0800112 struct batadv_socket_client *client = file->private_data;
113 struct batadv_socket_packet *packet, *tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000114
Geliang Tangfb1f23e2015-12-18 23:33:31 +0800115 spin_lock_bh(&client->lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000116
117 /* for all packets in the queue ... */
Geliang Tangfb1f23e2015-12-18 23:33:31 +0800118 list_for_each_entry_safe(packet, tmp, &client->queue_list, list) {
119 list_del(&packet->list);
120 kfree(packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000121 }
122
Geliang Tangfb1f23e2015-12-18 23:33:31 +0800123 batadv_socket_client_hash[client->index] = NULL;
124 spin_unlock_bh(&client->lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000125
Geliang Tangfb1f23e2015-12-18 23:33:31 +0800126 kfree(client);
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +0200127 module_put(THIS_MODULE);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000128
129 return 0;
130}
131
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200132static ssize_t batadv_socket_read(struct file *file, char __user *buf,
133 size_t count, loff_t *ppos)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000134{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200135 struct batadv_socket_client *socket_client = file->private_data;
136 struct batadv_socket_packet *socket_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000137 size_t packet_len;
138 int error;
139
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200140 if ((file->f_flags & O_NONBLOCK) && socket_client->queue_len == 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000141 return -EAGAIN;
142
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200143 if (!buf || count < sizeof(struct batadv_icmp_packet))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000144 return -EINVAL;
145
146 if (!access_ok(VERIFY_WRITE, buf, count))
147 return -EFAULT;
148
149 error = wait_event_interruptible(socket_client->queue_wait,
150 socket_client->queue_len);
151
152 if (error)
153 return error;
154
155 spin_lock_bh(&socket_client->lock);
156
157 socket_packet = list_first_entry(&socket_client->queue_list,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200158 struct batadv_socket_packet, list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000159 list_del(&socket_packet->list);
160 socket_client->queue_len--;
161
162 spin_unlock_bh(&socket_client->lock);
163
Sven Eckelmannb5a1eee2011-12-10 15:28:36 +0100164 packet_len = min(count, socket_packet->icmp_len);
165 error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000166
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000167 kfree(socket_packet);
168
169 if (error)
170 return -EFAULT;
171
172 return packet_len;
173}
174
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200175static ssize_t batadv_socket_write(struct file *file, const char __user *buff,
176 size_t len, loff_t *off)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000177{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200178 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;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000181 struct sk_buff *skb;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200182 struct batadv_icmp_packet_rr *icmp_packet_rr;
183 struct batadv_icmp_header *icmp_header;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200184 struct batadv_orig_node *orig_node = NULL;
185 struct batadv_neigh_node *neigh_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200186 size_t packet_len = sizeof(struct batadv_icmp_packet);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200187 u8 *addr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200189 if (len < sizeof(struct batadv_icmp_header)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200190 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200191 "Error - can't send packet from char device: invalid packet size\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000192 return -EINVAL;
193 }
194
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200195 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200196
197 if (!primary_if) {
198 len = -EFAULT;
199 goto out;
200 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000201
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200202 if (len >= BATADV_ICMP_MAX_PACKET_SIZE)
203 packet_len = BATADV_ICMP_MAX_PACKET_SIZE;
204 else
205 packet_len = len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000206
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200207 skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200208 if (!skb) {
209 len = -ENOMEM;
210 goto out;
211 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000212
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200213 skb->priority = TC_PRIO_CONTROL;
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200214 skb_reserve(skb, ETH_HLEN);
Johannes Berg4df864c2017-06-16 14:29:21 +0200215 icmp_header = skb_put(skb, packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000216
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200217 if (copy_from_user(icmp_header, buff, packet_len)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000218 len = -EFAULT;
219 goto free_skb;
220 }
221
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100222 if (icmp_header->packet_type != BATADV_ICMP) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200223 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200224 "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000225 len = -EINVAL;
226 goto free_skb;
227 }
228
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200229 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");
234 len = -EINVAL;
235 goto free_skb;
236 }
237
238 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
239 goto dst_unreach;
240
241 orig_node = batadv_orig_hash_find(bat_priv, icmp_header->dst);
242 if (!orig_node)
243 goto dst_unreach;
244
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100245 neigh_node = batadv_orig_router_get(orig_node,
246 BATADV_IF_DEFAULT);
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200247 if (!neigh_node)
248 goto dst_unreach;
249
250 if (!neigh_node->if_incoming)
251 goto dst_unreach;
252
253 if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
254 goto dst_unreach;
255
256 icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmp_header;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100257 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);
260 }
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200261
262 break;
263 default:
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200264 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200265 "Error - can't send packet from char device: got unknown message type\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000266 len = -EINVAL;
267 goto free_skb;
268 }
269
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200270 icmp_header->uid = socket_client->index;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000271
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100272 if (icmp_header->version != BATADV_COMPAT_VERSION) {
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200273 icmp_header->msg_type = BATADV_PARAMETER_PROBLEM;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100274 icmp_header->version = BATADV_COMPAT_VERSION;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200275 batadv_socket_add_packet(socket_client, icmp_header,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200276 packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000277 goto free_skb;
278 }
279
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100280 ether_addr_copy(icmp_header->orig, primary_if->net_dev->dev_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000281
Antonio Quartulli95d39272016-01-16 16:40:15 +0800282 batadv_send_unicast_skb(skb, neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000283 goto out;
284
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000285dst_unreach:
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200286 icmp_header->msg_type = BATADV_DESTINATION_UNREACHABLE;
287 batadv_socket_add_packet(socket_client, icmp_header, packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000288free_skb:
289 kfree_skb(skb);
290out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200291 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100292 batadv_hardif_put(primary_if);
Marek Lindner44524fc2011-02-10 14:33:53 +0000293 if (neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100294 batadv_neigh_node_put(neigh_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000295 if (orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +0100296 batadv_orig_node_put(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000297 return len;
298}
299
Al Viroade994f2017-07-03 00:01:49 -0400300static __poll_t batadv_socket_poll(struct file *file, poll_table *wait)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000301{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200302 struct batadv_socket_client *socket_client = file->private_data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000303
304 poll_wait(file, &socket_client->queue_wait, wait);
305
306 if (socket_client->queue_len > 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800307 return EPOLLIN | EPOLLRDNORM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000308
309 return 0;
310}
311
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200312static const struct file_operations batadv_fops = {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000313 .owner = THIS_MODULE,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200314 .open = batadv_socket_open,
315 .release = batadv_socket_release,
316 .read = batadv_socket_read,
317 .write = batadv_socket_write,
318 .poll = batadv_socket_poll,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000319 .llseek = no_llseek,
320};
321
Sven Eckelmannff15c272017-12-02 19:51:53 +0100322/**
323 * batadv_socket_setup() - Create debugfs "socket" file
324 * @bat_priv: the bat priv with all the soft interface information
325 *
326 * Return: 0 on success or negative error number in case of failure
327 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200328int batadv_socket_setup(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000329{
330 struct dentry *d;
331
332 if (!bat_priv->debug_dir)
333 goto err;
334
Sven Eckelmann507b37c2016-09-01 10:25:12 +0200335 d = debugfs_create_file(BATADV_ICMP_SOCKET, 0600, bat_priv->debug_dir,
336 bat_priv, &batadv_fops);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200337 if (!d)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000338 goto err;
339
340 return 0;
341
342err:
Sven Eckelmann5346c352012-05-05 13:27:28 +0200343 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000344}
345
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200346/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100347 * batadv_socket_add_packet() - schedule an icmp packet to be sent to
Sven Eckelmann34473822015-05-31 10:10:20 +0200348 * userspace on an icmp socket.
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200349 * @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
352 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200353static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200354 struct batadv_icmp_header *icmph,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200355 size_t icmp_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000356{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200357 struct batadv_socket_packet *socket_packet;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200358 size_t len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000359
Sven Eckelmann704509b2011-05-14 23:14:54 +0200360 socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000361
362 if (!socket_packet)
363 return;
364
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200365 len = icmp_len;
366 /* check the maximum length before filling the buffer */
367 if (len > sizeof(socket_packet->icmp_packet))
368 len = sizeof(socket_packet->icmp_packet);
369
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370 INIT_LIST_HEAD(&socket_packet->list);
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200371 memcpy(&socket_packet->icmp_packet, icmph, len);
372 socket_packet->icmp_len = len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000373
374 spin_lock_bh(&socket_client->lock);
375
376 /* while waiting for the lock the socket_client could have been
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200377 * deleted
378 */
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200379 if (!batadv_socket_client_hash[icmph->uid]) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000380 spin_unlock_bh(&socket_client->lock);
381 kfree(socket_packet);
382 return;
383 }
384
385 list_add_tail(&socket_packet->list, &socket_client->queue_list);
386 socket_client->queue_len++;
387
388 if (socket_client->queue_len > 100) {
389 socket_packet = list_first_entry(&socket_client->queue_list,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200390 struct batadv_socket_packet,
391 list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000392
393 list_del(&socket_packet->list);
394 kfree(socket_packet);
395 socket_client->queue_len--;
396 }
397
398 spin_unlock_bh(&socket_client->lock);
399
400 wake_up(&socket_client->queue_wait);
401}
402
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200403/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100404 * batadv_socket_receive_packet() - schedule an icmp packet to be received
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200405 * locally and sent to userspace.
406 * @icmph: pointer to the header of the icmp packet
407 * @icmp_len: total length of the icmp packet
408 */
409void batadv_socket_receive_packet(struct batadv_icmp_header *icmph,
Sven Eckelmann9039dc72012-05-12 02:09:33 +0200410 size_t icmp_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000411{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200412 struct batadv_socket_client *hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000413
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200414 hash = batadv_socket_client_hash[icmph->uid];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000415 if (hash)
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200416 batadv_socket_add_packet(hash, icmph, icmp_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000417}