blob: 0a70b66e8770a679b372e5839dbe4c8a031ecd58 [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
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00005 */
6
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00007#include "icmp_socket.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +02008#include "main.h"
9
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>
Sven Eckelmann48881ed2018-03-18 09:48:03 +010015#include <linux/eventpoll.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020016#include <linux/export.h>
17#include <linux/fcntl.h>
18#include <linux/fs.h>
Sven Eckelmannb92b94a2017-11-19 17:12:02 +010019#include <linux/gfp.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020020#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>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020032#include <linux/stddef.h>
33#include <linux/string.h>
34#include <linux/uaccess.h>
35#include <linux/wait.h>
Sven Eckelmannfec149f2017-12-21 10:17:41 +010036#include <uapi/linux/batadv_packet.h>
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020037
Sven Eckelmann00caf6a2018-08-10 23:36:15 +020038#include "debugfs.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000039#include "hard-interface.h"
Sven Eckelmannba412082016-05-15 23:48:31 +020040#include "log.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020041#include "originator.h"
Sven Eckelmann1e2c2a42015-04-17 19:40:28 +020042#include "send.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000043
Sven Eckelmann56303d32012-06-05 22:31:31 +020044static struct batadv_socket_client *batadv_socket_client_hash[256];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000045
Sven Eckelmann56303d32012-06-05 22:31:31 +020046static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
Simon Wunderlichda6b8c22013-10-22 22:50:09 +020047 struct batadv_icmp_header *icmph,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020048 size_t icmp_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000049
Sven Eckelmannff15c272017-12-02 19:51:53 +010050/**
51 * batadv_socket_init() - Initialize soft interface independent socket data
52 */
Sven Eckelmann9039dc72012-05-12 02:09:33 +020053void batadv_socket_init(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000054{
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020055 memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000056}
57
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020058static int batadv_socket_open(struct inode *inode, struct file *file)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000059{
60 unsigned int i;
Sven Eckelmann56303d32012-06-05 22:31:31 +020061 struct batadv_socket_client *socket_client;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000062
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +020063 if (!try_module_get(THIS_MODULE))
64 return -EBUSY;
65
Sven Eckelmann00caf6a2018-08-10 23:36:15 +020066 batadv_debugfs_deprecated(file, "");
67
Kirill Smelkovc5bf68f2019-03-26 23:51:19 +030068 stream_open(inode, file);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000069
Sven Eckelmann704509b2011-05-14 23:14:54 +020070 socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +020071 if (!socket_client) {
72 module_put(THIS_MODULE);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000073 return -ENOMEM;
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +020074 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000075
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020076 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;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000079 break;
80 }
81 }
82
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020083 if (i == ARRAY_SIZE(batadv_socket_client_hash)) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +010084 pr_err("Error - can't add another packet client: maximum number of clients reached\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000085 kfree(socket_client);
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +020086 module_put(THIS_MODULE);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000087 return -EXFULL;
88 }
89
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);
96
97 file->private_data = socket_client;
98
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000099 return 0;
100}
101
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200102static int batadv_socket_release(struct inode *inode, struct file *file)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000103{
Geliang Tangfb1f23e2015-12-18 23:33:31 +0800104 struct batadv_socket_client *client = file->private_data;
105 struct batadv_socket_packet *packet, *tmp;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000106
Geliang Tangfb1f23e2015-12-18 23:33:31 +0800107 spin_lock_bh(&client->lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000108
109 /* for all packets in the queue ... */
Geliang Tangfb1f23e2015-12-18 23:33:31 +0800110 list_for_each_entry_safe(packet, tmp, &client->queue_list, list) {
111 list_del(&packet->list);
112 kfree(packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000113 }
114
Geliang Tangfb1f23e2015-12-18 23:33:31 +0800115 batadv_socket_client_hash[client->index] = NULL;
116 spin_unlock_bh(&client->lock);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000117
Geliang Tangfb1f23e2015-12-18 23:33:31 +0800118 kfree(client);
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +0200119 module_put(THIS_MODULE);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000120
121 return 0;
122}
123
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200124static ssize_t batadv_socket_read(struct file *file, char __user *buf,
125 size_t count, loff_t *ppos)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000126{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200127 struct batadv_socket_client *socket_client = file->private_data;
128 struct batadv_socket_packet *socket_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000129 size_t packet_len;
130 int error;
131
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200132 if ((file->f_flags & O_NONBLOCK) && socket_client->queue_len == 0)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000133 return -EAGAIN;
134
Sven Eckelmann825ffe12017-08-23 21:52:13 +0200135 if (!buf || count < sizeof(struct batadv_icmp_packet))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000136 return -EINVAL;
137
Linus Torvalds96d4f262019-01-03 18:57:57 -0800138 if (!access_ok(buf, count))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000139 return -EFAULT;
140
141 error = wait_event_interruptible(socket_client->queue_wait,
142 socket_client->queue_len);
143
144 if (error)
145 return error;
146
147 spin_lock_bh(&socket_client->lock);
148
149 socket_packet = list_first_entry(&socket_client->queue_list,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200150 struct batadv_socket_packet, list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000151 list_del(&socket_packet->list);
152 socket_client->queue_len--;
153
154 spin_unlock_bh(&socket_client->lock);
155
Sven Eckelmannb5a1eee2011-12-10 15:28:36 +0100156 packet_len = min(count, socket_packet->icmp_len);
157 error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000158
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000159 kfree(socket_packet);
160
161 if (error)
162 return -EFAULT;
163
164 return packet_len;
165}
166
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200167static ssize_t batadv_socket_write(struct file *file, const char __user *buff,
168 size_t len, loff_t *off)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000169{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200170 struct batadv_socket_client *socket_client = file->private_data;
171 struct batadv_priv *bat_priv = socket_client->bat_priv;
172 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000173 struct sk_buff *skb;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200174 struct batadv_icmp_packet_rr *icmp_packet_rr;
175 struct batadv_icmp_header *icmp_header;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200176 struct batadv_orig_node *orig_node = NULL;
177 struct batadv_neigh_node *neigh_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200178 size_t packet_len = sizeof(struct batadv_icmp_packet);
Sven Eckelmann6b5e9712015-05-26 18:34:26 +0200179 u8 *addr;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000180
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200181 if (len < sizeof(struct batadv_icmp_header)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200182 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200183 "Error - can't send packet from char device: invalid packet size\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000184 return -EINVAL;
185 }
186
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200187 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200188
189 if (!primary_if) {
190 len = -EFAULT;
191 goto out;
192 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000193
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200194 if (len >= BATADV_ICMP_MAX_PACKET_SIZE)
195 packet_len = BATADV_ICMP_MAX_PACKET_SIZE;
196 else
197 packet_len = len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000198
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200199 skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200200 if (!skb) {
201 len = -ENOMEM;
202 goto out;
203 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000204
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200205 skb->priority = TC_PRIO_CONTROL;
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200206 skb_reserve(skb, ETH_HLEN);
Johannes Berg4df864c2017-06-16 14:29:21 +0200207 icmp_header = skb_put(skb, packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000208
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200209 if (copy_from_user(icmp_header, buff, packet_len)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000210 len = -EFAULT;
211 goto free_skb;
212 }
213
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100214 if (icmp_header->packet_type != BATADV_ICMP) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200215 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200216 "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000217 len = -EINVAL;
218 goto free_skb;
219 }
220
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200221 switch (icmp_header->msg_type) {
222 case BATADV_ECHO_REQUEST:
223 if (len < sizeof(struct batadv_icmp_packet)) {
224 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
225 "Error - can't send packet from char device: invalid packet size\n");
226 len = -EINVAL;
227 goto free_skb;
228 }
229
230 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
231 goto dst_unreach;
232
233 orig_node = batadv_orig_hash_find(bat_priv, icmp_header->dst);
234 if (!orig_node)
235 goto dst_unreach;
236
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100237 neigh_node = batadv_orig_router_get(orig_node,
238 BATADV_IF_DEFAULT);
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200239 if (!neigh_node)
240 goto dst_unreach;
241
242 if (!neigh_node->if_incoming)
243 goto dst_unreach;
244
245 if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
246 goto dst_unreach;
247
248 icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmp_header;
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100249 if (packet_len == sizeof(*icmp_packet_rr)) {
250 addr = neigh_node->if_incoming->net_dev->dev_addr;
251 ether_addr_copy(icmp_packet_rr->rr[0], addr);
252 }
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200253
254 break;
255 default:
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200256 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200257 "Error - can't send packet from char device: got unknown message type\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000258 len = -EINVAL;
259 goto free_skb;
260 }
261
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200262 icmp_header->uid = socket_client->index;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000263
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100264 if (icmp_header->version != BATADV_COMPAT_VERSION) {
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200265 icmp_header->msg_type = BATADV_PARAMETER_PROBLEM;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100266 icmp_header->version = BATADV_COMPAT_VERSION;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200267 batadv_socket_add_packet(socket_client, icmp_header,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200268 packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000269 goto free_skb;
270 }
271
Antonio Quartulli8fdd0152014-01-22 00:42:11 +0100272 ether_addr_copy(icmp_header->orig, primary_if->net_dev->dev_addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000273
Antonio Quartulli95d39272016-01-16 16:40:15 +0800274 batadv_send_unicast_skb(skb, neigh_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000275 goto out;
276
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000277dst_unreach:
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200278 icmp_header->msg_type = BATADV_DESTINATION_UNREACHABLE;
279 batadv_socket_add_packet(socket_client, icmp_header, packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000280free_skb:
281 kfree_skb(skb);
282out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200283 if (primary_if)
Sven Eckelmann82047ad2016-01-17 11:01:10 +0100284 batadv_hardif_put(primary_if);
Marek Lindner44524fc2011-02-10 14:33:53 +0000285 if (neigh_node)
Sven Eckelmann25bb2502016-01-17 11:01:11 +0100286 batadv_neigh_node_put(neigh_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000287 if (orig_node)
Sven Eckelmann5d967312016-01-17 11:01:09 +0100288 batadv_orig_node_put(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000289 return len;
290}
291
Al Viroade994f2017-07-03 00:01:49 -0400292static __poll_t batadv_socket_poll(struct file *file, poll_table *wait)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000293{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200294 struct batadv_socket_client *socket_client = file->private_data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000295
296 poll_wait(file, &socket_client->queue_wait, wait);
297
298 if (socket_client->queue_len > 0)
Linus Torvaldsa9a08842018-02-11 14:34:03 -0800299 return EPOLLIN | EPOLLRDNORM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000300
301 return 0;
302}
303
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200304static const struct file_operations batadv_fops = {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000305 .owner = THIS_MODULE,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200306 .open = batadv_socket_open,
307 .release = batadv_socket_release,
308 .read = batadv_socket_read,
309 .write = batadv_socket_write,
310 .poll = batadv_socket_poll,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000311 .llseek = no_llseek,
312};
313
Sven Eckelmannff15c272017-12-02 19:51:53 +0100314/**
315 * batadv_socket_setup() - Create debugfs "socket" file
316 * @bat_priv: the bat priv with all the soft interface information
Sven Eckelmannff15c272017-12-02 19:51:53 +0100317 */
Greg Kroah-Hartman3bcacd12019-06-14 09:11:23 +0200318void batadv_socket_setup(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000319{
Greg Kroah-Hartman3bcacd12019-06-14 09:11:23 +0200320 debugfs_create_file(BATADV_ICMP_SOCKET, 0600, bat_priv->debug_dir,
321 bat_priv, &batadv_fops);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000322}
323
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200324/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100325 * batadv_socket_add_packet() - schedule an icmp packet to be sent to
Sven Eckelmann34473822015-05-31 10:10:20 +0200326 * userspace on an icmp socket.
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200327 * @socket_client: the socket this packet belongs to
328 * @icmph: pointer to the header of the icmp packet
329 * @icmp_len: total length of the icmp packet
330 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200331static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200332 struct batadv_icmp_header *icmph,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200333 size_t icmp_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000334{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200335 struct batadv_socket_packet *socket_packet;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200336 size_t len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000337
Sven Eckelmann704509b2011-05-14 23:14:54 +0200338 socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000339
340 if (!socket_packet)
341 return;
342
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200343 len = icmp_len;
344 /* check the maximum length before filling the buffer */
345 if (len > sizeof(socket_packet->icmp_packet))
346 len = sizeof(socket_packet->icmp_packet);
347
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000348 INIT_LIST_HEAD(&socket_packet->list);
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200349 memcpy(&socket_packet->icmp_packet, icmph, len);
350 socket_packet->icmp_len = len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000351
352 spin_lock_bh(&socket_client->lock);
353
354 /* while waiting for the lock the socket_client could have been
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200355 * deleted
356 */
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200357 if (!batadv_socket_client_hash[icmph->uid]) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000358 spin_unlock_bh(&socket_client->lock);
359 kfree(socket_packet);
360 return;
361 }
362
363 list_add_tail(&socket_packet->list, &socket_client->queue_list);
364 socket_client->queue_len++;
365
366 if (socket_client->queue_len > 100) {
367 socket_packet = list_first_entry(&socket_client->queue_list,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200368 struct batadv_socket_packet,
369 list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000370
371 list_del(&socket_packet->list);
372 kfree(socket_packet);
373 socket_client->queue_len--;
374 }
375
376 spin_unlock_bh(&socket_client->lock);
377
378 wake_up(&socket_client->queue_wait);
379}
380
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200381/**
Sven Eckelmann7e9a8c22017-12-02 19:51:47 +0100382 * batadv_socket_receive_packet() - schedule an icmp packet to be received
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200383 * locally and sent to userspace.
384 * @icmph: pointer to the header of the icmp packet
385 * @icmp_len: total length of the icmp packet
386 */
387void batadv_socket_receive_packet(struct batadv_icmp_header *icmph,
Sven Eckelmann9039dc72012-05-12 02:09:33 +0200388 size_t icmp_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000389{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200390 struct batadv_socket_client *hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000391
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200392 hash = batadv_socket_client_hash[icmph->uid];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000393 if (hash)
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200394 batadv_socket_add_packet(hash, icmph, icmp_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000395}