Blame view
net/batman-adv/icmp_socket.c
9.95 KB
7db7d9f36
|
1 |
// SPDX-License-Identifier: GPL-2.0 |
7a79d717e
|
2 |
/* Copyright (C) 2007-2019 B.A.T.M.A.N. contributors: |
c6c8fea29
|
3 4 |
* * Marek Lindner |
c6c8fea29
|
5 |
*/ |
1e2c2a4fe
|
6 |
#include "icmp_socket.h" |
c6c8fea29
|
7 |
#include "main.h" |
1e2c2a4fe
|
8 9 10 |
#include <linux/atomic.h> #include <linux/compiler.h> |
c6c8fea29
|
11 |
#include <linux/debugfs.h> |
1e2c2a4fe
|
12 13 |
#include <linux/errno.h> #include <linux/etherdevice.h> |
48881ed56
|
14 |
#include <linux/eventpoll.h> |
1e2c2a4fe
|
15 16 17 |
#include <linux/export.h> #include <linux/fcntl.h> #include <linux/fs.h> |
b92b94ac7
|
18 |
#include <linux/gfp.h> |
1e2c2a4fe
|
19 20 21 22 23 24 25 26 27 28 |
#include <linux/if_ether.h> #include <linux/kernel.h> #include <linux/list.h> #include <linux/module.h> #include <linux/netdevice.h> #include <linux/pkt_sched.h> #include <linux/poll.h> #include <linux/printk.h> #include <linux/sched.h> /* for linux/wait.h */ #include <linux/skbuff.h> |
c6c8fea29
|
29 |
#include <linux/slab.h> |
1e2c2a4fe
|
30 |
#include <linux/spinlock.h> |
1e2c2a4fe
|
31 32 33 34 |
#include <linux/stddef.h> #include <linux/string.h> #include <linux/uaccess.h> #include <linux/wait.h> |
fec149f5d
|
35 |
#include <uapi/linux/batadv_packet.h> |
1e2c2a4fe
|
36 |
|
00caf6a2b
|
37 |
#include "debugfs.h" |
c6c8fea29
|
38 |
#include "hard-interface.h" |
ba412080f
|
39 |
#include "log.h" |
1e2c2a4fe
|
40 |
#include "originator.h" |
1e2c2a4fe
|
41 |
#include "send.h" |
c6c8fea29
|
42 |
|
56303d34a
|
43 |
static struct batadv_socket_client *batadv_socket_client_hash[256]; |
c6c8fea29
|
44 |
|
56303d34a
|
45 |
static void batadv_socket_add_packet(struct batadv_socket_client *socket_client, |
da6b8c20a
|
46 |
struct batadv_icmp_header *icmph, |
af4447f62
|
47 |
size_t icmp_len); |
c6c8fea29
|
48 |
|
ff15c27c9
|
49 50 51 |
/** * batadv_socket_init() - Initialize soft interface independent socket data */ |
9039dc7e8
|
52 |
void batadv_socket_init(void) |
c6c8fea29
|
53 |
{ |
af4447f62
|
54 |
memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash)); |
c6c8fea29
|
55 |
} |
af4447f62
|
56 |
static int batadv_socket_open(struct inode *inode, struct file *file) |
c6c8fea29
|
57 58 |
{ unsigned int i; |
56303d34a
|
59 |
struct batadv_socket_client *socket_client; |
c6c8fea29
|
60 |
|
bd5b80d51
|
61 62 |
if (!try_module_get(THIS_MODULE)) return -EBUSY; |
00caf6a2b
|
63 |
batadv_debugfs_deprecated(file, ""); |
c5bf68fe0
|
64 |
stream_open(inode, file); |
c6c8fea29
|
65 |
|
704509b8d
|
66 |
socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL); |
bd5b80d51
|
67 68 |
if (!socket_client) { module_put(THIS_MODULE); |
c6c8fea29
|
69 |
return -ENOMEM; |
bd5b80d51
|
70 |
} |
c6c8fea29
|
71 |
|
af4447f62
|
72 73 74 |
for (i = 0; i < ARRAY_SIZE(batadv_socket_client_hash); i++) { if (!batadv_socket_client_hash[i]) { batadv_socket_client_hash[i] = socket_client; |
c6c8fea29
|
75 76 77 |
break; } } |
af4447f62
|
78 |
if (i == ARRAY_SIZE(batadv_socket_client_hash)) { |
86ceb3605
|
79 80 |
pr_err("Error - can't add another packet client: maximum number of clients reached "); |
c6c8fea29
|
81 |
kfree(socket_client); |
bd5b80d51
|
82 |
module_put(THIS_MODULE); |
c6c8fea29
|
83 84 85 86 87 88 89 90 91 92 93 |
return -EXFULL; } INIT_LIST_HEAD(&socket_client->queue_list); socket_client->queue_len = 0; socket_client->index = i; socket_client->bat_priv = inode->i_private; spin_lock_init(&socket_client->lock); init_waitqueue_head(&socket_client->queue_wait); file->private_data = socket_client; |
c6c8fea29
|
94 95 |
return 0; } |
af4447f62
|
96 |
static int batadv_socket_release(struct inode *inode, struct file *file) |
c6c8fea29
|
97 |
{ |
fb1f23eab
|
98 99 |
struct batadv_socket_client *client = file->private_data; struct batadv_socket_packet *packet, *tmp; |
c6c8fea29
|
100 |
|
fb1f23eab
|
101 |
spin_lock_bh(&client->lock); |
c6c8fea29
|
102 103 |
/* for all packets in the queue ... */ |
fb1f23eab
|
104 105 106 |
list_for_each_entry_safe(packet, tmp, &client->queue_list, list) { list_del(&packet->list); kfree(packet); |
c6c8fea29
|
107 |
} |
fb1f23eab
|
108 109 |
batadv_socket_client_hash[client->index] = NULL; spin_unlock_bh(&client->lock); |
c6c8fea29
|
110 |
|
fb1f23eab
|
111 |
kfree(client); |
bd5b80d51
|
112 |
module_put(THIS_MODULE); |
c6c8fea29
|
113 114 115 |
return 0; } |
af4447f62
|
116 117 |
static ssize_t batadv_socket_read(struct file *file, char __user *buf, size_t count, loff_t *ppos) |
c6c8fea29
|
118 |
{ |
56303d34a
|
119 120 |
struct batadv_socket_client *socket_client = file->private_data; struct batadv_socket_packet *socket_packet; |
c6c8fea29
|
121 122 |
size_t packet_len; int error; |
825ffe1f7
|
123 |
if ((file->f_flags & O_NONBLOCK) && socket_client->queue_len == 0) |
c6c8fea29
|
124 |
return -EAGAIN; |
825ffe1f7
|
125 |
if (!buf || count < sizeof(struct batadv_icmp_packet)) |
c6c8fea29
|
126 |
return -EINVAL; |
96d4f267e
|
127 |
if (!access_ok(buf, count)) |
c6c8fea29
|
128 129 130 131 132 133 134 135 136 137 138 |
return -EFAULT; error = wait_event_interruptible(socket_client->queue_wait, socket_client->queue_len); if (error) return error; spin_lock_bh(&socket_client->lock); socket_packet = list_first_entry(&socket_client->queue_list, |
56303d34a
|
139 |
struct batadv_socket_packet, list); |
c6c8fea29
|
140 141 142 143 |
list_del(&socket_packet->list); socket_client->queue_len--; spin_unlock_bh(&socket_client->lock); |
b5a1eeef0
|
144 145 |
packet_len = min(count, socket_packet->icmp_len); error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len); |
c6c8fea29
|
146 |
|
c6c8fea29
|
147 148 149 150 151 152 153 |
kfree(socket_packet); if (error) return -EFAULT; return packet_len; } |
af4447f62
|
154 155 |
static ssize_t batadv_socket_write(struct file *file, const char __user *buff, size_t len, loff_t *off) |
c6c8fea29
|
156 |
{ |
56303d34a
|
157 158 159 |
struct batadv_socket_client *socket_client = file->private_data; struct batadv_priv *bat_priv = socket_client->bat_priv; struct batadv_hard_iface *primary_if = NULL; |
c6c8fea29
|
160 |
struct sk_buff *skb; |
da6b8c20a
|
161 162 |
struct batadv_icmp_packet_rr *icmp_packet_rr; struct batadv_icmp_header *icmp_header; |
56303d34a
|
163 164 |
struct batadv_orig_node *orig_node = NULL; struct batadv_neigh_node *neigh_node = NULL; |
964126901
|
165 |
size_t packet_len = sizeof(struct batadv_icmp_packet); |
6b5e971a2
|
166 |
u8 *addr; |
c6c8fea29
|
167 |
|
da6b8c20a
|
168 |
if (len < sizeof(struct batadv_icmp_header)) { |
39c75a51e
|
169 |
batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bfc
|
170 171 |
"Error - can't send packet from char device: invalid packet size "); |
c6c8fea29
|
172 173 |
return -EINVAL; } |
e5d89254b
|
174 |
primary_if = batadv_primary_if_get_selected(bat_priv); |
32ae9b221
|
175 176 177 178 179 |
if (!primary_if) { len = -EFAULT; goto out; } |
c6c8fea29
|
180 |
|
da6b8c20a
|
181 182 183 184 |
if (len >= BATADV_ICMP_MAX_PACKET_SIZE) packet_len = BATADV_ICMP_MAX_PACKET_SIZE; else packet_len = len; |
c6c8fea29
|
185 |
|
41ab6c489
|
186 |
skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN); |
32ae9b221
|
187 188 189 190 |
if (!skb) { len = -ENOMEM; goto out; } |
c6c8fea29
|
191 |
|
c54f38c9a
|
192 |
skb->priority = TC_PRIO_CONTROL; |
41ab6c489
|
193 |
skb_reserve(skb, ETH_HLEN); |
4df864c1d
|
194 |
icmp_header = skb_put(skb, packet_len); |
c6c8fea29
|
195 |
|
da6b8c20a
|
196 |
if (copy_from_user(icmp_header, buff, packet_len)) { |
c6c8fea29
|
197 198 199 |
len = -EFAULT; goto free_skb; } |
a40d9b075
|
200 |
if (icmp_header->packet_type != BATADV_ICMP) { |
39c75a51e
|
201 |
batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bfc
|
202 203 |
"Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP) "); |
c6c8fea29
|
204 205 206 |
len = -EINVAL; goto free_skb; } |
da6b8c20a
|
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
switch (icmp_header->msg_type) { case BATADV_ECHO_REQUEST: if (len < sizeof(struct batadv_icmp_packet)) { batadv_dbg(BATADV_DBG_BATMAN, bat_priv, "Error - can't send packet from char device: invalid packet size "); len = -EINVAL; goto free_skb; } if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE) goto dst_unreach; orig_node = batadv_orig_hash_find(bat_priv, icmp_header->dst); if (!orig_node) goto dst_unreach; |
7351a4822
|
223 224 |
neigh_node = batadv_orig_router_get(orig_node, BATADV_IF_DEFAULT); |
da6b8c20a
|
225 226 227 228 229 230 231 232 233 234 |
if (!neigh_node) goto dst_unreach; if (!neigh_node->if_incoming) goto dst_unreach; if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE) goto dst_unreach; icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmp_header; |
8fdd01530
|
235 236 237 238 |
if (packet_len == sizeof(*icmp_packet_rr)) { addr = neigh_node->if_incoming->net_dev->dev_addr; ether_addr_copy(icmp_packet_rr->rr[0], addr); } |
da6b8c20a
|
239 240 241 |
break; default: |
39c75a51e
|
242 |
batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
da6b8c20a
|
243 244 |
"Error - can't send packet from char device: got unknown message type "); |
c6c8fea29
|
245 246 247 |
len = -EINVAL; goto free_skb; } |
da6b8c20a
|
248 |
icmp_header->uid = socket_client->index; |
c6c8fea29
|
249 |
|
a40d9b075
|
250 |
if (icmp_header->version != BATADV_COMPAT_VERSION) { |
da6b8c20a
|
251 |
icmp_header->msg_type = BATADV_PARAMETER_PROBLEM; |
a40d9b075
|
252 |
icmp_header->version = BATADV_COMPAT_VERSION; |
da6b8c20a
|
253 |
batadv_socket_add_packet(socket_client, icmp_header, |
af4447f62
|
254 |
packet_len); |
c6c8fea29
|
255 256 |
goto free_skb; } |
8fdd01530
|
257 |
ether_addr_copy(icmp_header->orig, primary_if->net_dev->dev_addr); |
c6c8fea29
|
258 |
|
95d392784
|
259 |
batadv_send_unicast_skb(skb, neigh_node); |
c6c8fea29
|
260 |
goto out; |
c6c8fea29
|
261 |
dst_unreach: |
da6b8c20a
|
262 263 |
icmp_header->msg_type = BATADV_DESTINATION_UNREACHABLE; batadv_socket_add_packet(socket_client, icmp_header, packet_len); |
c6c8fea29
|
264 265 266 |
free_skb: kfree_skb(skb); out: |
32ae9b221
|
267 |
if (primary_if) |
82047ad7f
|
268 |
batadv_hardif_put(primary_if); |
44524fcdf
|
269 |
if (neigh_node) |
25bb25099
|
270 |
batadv_neigh_node_put(neigh_node); |
44524fcdf
|
271 |
if (orig_node) |
5d9673109
|
272 |
batadv_orig_node_put(orig_node); |
c6c8fea29
|
273 274 |
return len; } |
ade994f4f
|
275 |
static __poll_t batadv_socket_poll(struct file *file, poll_table *wait) |
c6c8fea29
|
276 |
{ |
56303d34a
|
277 |
struct batadv_socket_client *socket_client = file->private_data; |
c6c8fea29
|
278 279 280 281 |
poll_wait(file, &socket_client->queue_wait, wait); if (socket_client->queue_len > 0) |
a9a08845e
|
282 |
return EPOLLIN | EPOLLRDNORM; |
c6c8fea29
|
283 284 285 |
return 0; } |
af4447f62
|
286 |
static const struct file_operations batadv_fops = { |
c6c8fea29
|
287 |
.owner = THIS_MODULE, |
af4447f62
|
288 289 290 291 292 |
.open = batadv_socket_open, .release = batadv_socket_release, .read = batadv_socket_read, .write = batadv_socket_write, .poll = batadv_socket_poll, |
c6c8fea29
|
293 294 |
.llseek = no_llseek, }; |
ff15c27c9
|
295 296 297 |
/** * batadv_socket_setup() - Create debugfs "socket" file * @bat_priv: the bat priv with all the soft interface information |
ff15c27c9
|
298 |
*/ |
3bcacd1e0
|
299 |
void batadv_socket_setup(struct batadv_priv *bat_priv) |
c6c8fea29
|
300 |
{ |
3bcacd1e0
|
301 302 |
debugfs_create_file(BATADV_ICMP_SOCKET, 0600, bat_priv->debug_dir, bat_priv, &batadv_fops); |
c6c8fea29
|
303 |
} |
da6b8c20a
|
304 |
/** |
7e9a8c2ce
|
305 |
* batadv_socket_add_packet() - schedule an icmp packet to be sent to |
34473822d
|
306 |
* userspace on an icmp socket. |
da6b8c20a
|
307 308 309 310 |
* @socket_client: the socket this packet belongs to * @icmph: pointer to the header of the icmp packet * @icmp_len: total length of the icmp packet */ |
56303d34a
|
311 |
static void batadv_socket_add_packet(struct batadv_socket_client *socket_client, |
da6b8c20a
|
312 |
struct batadv_icmp_header *icmph, |
af4447f62
|
313 |
size_t icmp_len) |
c6c8fea29
|
314 |
{ |
56303d34a
|
315 |
struct batadv_socket_packet *socket_packet; |
da6b8c20a
|
316 |
size_t len; |
c6c8fea29
|
317 |
|
704509b8d
|
318 |
socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC); |
c6c8fea29
|
319 320 321 |
if (!socket_packet) return; |
da6b8c20a
|
322 323 324 325 |
len = icmp_len; /* check the maximum length before filling the buffer */ if (len > sizeof(socket_packet->icmp_packet)) len = sizeof(socket_packet->icmp_packet); |
c6c8fea29
|
326 |
INIT_LIST_HEAD(&socket_packet->list); |
da6b8c20a
|
327 328 |
memcpy(&socket_packet->icmp_packet, icmph, len); socket_packet->icmp_len = len; |
c6c8fea29
|
329 330 331 332 |
spin_lock_bh(&socket_client->lock); /* while waiting for the lock the socket_client could have been |
9cfc7bd60
|
333 334 |
* deleted */ |
da6b8c20a
|
335 |
if (!batadv_socket_client_hash[icmph->uid]) { |
c6c8fea29
|
336 337 338 339 340 341 342 343 344 345 |
spin_unlock_bh(&socket_client->lock); kfree(socket_packet); return; } list_add_tail(&socket_packet->list, &socket_client->queue_list); socket_client->queue_len++; if (socket_client->queue_len > 100) { socket_packet = list_first_entry(&socket_client->queue_list, |
56303d34a
|
346 347 |
struct batadv_socket_packet, list); |
c6c8fea29
|
348 349 350 351 352 353 354 355 356 357 |
list_del(&socket_packet->list); kfree(socket_packet); socket_client->queue_len--; } spin_unlock_bh(&socket_client->lock); wake_up(&socket_client->queue_wait); } |
da6b8c20a
|
358 |
/** |
7e9a8c2ce
|
359 |
* batadv_socket_receive_packet() - schedule an icmp packet to be received |
da6b8c20a
|
360 361 362 363 364 |
* locally and sent to userspace. * @icmph: pointer to the header of the icmp packet * @icmp_len: total length of the icmp packet */ void batadv_socket_receive_packet(struct batadv_icmp_header *icmph, |
9039dc7e8
|
365 |
size_t icmp_len) |
c6c8fea29
|
366 |
{ |
56303d34a
|
367 |
struct batadv_socket_client *hash; |
c6c8fea29
|
368 |
|
da6b8c20a
|
369 |
hash = batadv_socket_client_hash[icmph->uid]; |
c6c8fea29
|
370 |
if (hash) |
da6b8c20a
|
371 |
batadv_socket_add_packet(hash, icmph, icmp_len); |
c6c8fea29
|
372 |
} |