1 // SPDX-License-Identifier: GPL-2.0
2 /* Copyright (C) 2007-2018 B.A.T.M.A.N. contributors:
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.
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.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, see <http://www.gnu.org/licenses/>.
19 #include "icmp_socket.h"
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/eventpoll.h>
28 #include <linux/export.h>
29 #include <linux/fcntl.h>
31 #include <linux/gfp.h>
32 #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>
44 #include <linux/stddef.h>
45 #include <linux/string.h>
46 #include <linux/uaccess.h>
47 #include <linux/wait.h>
48 #include <uapi/linux/batadv_packet.h>
51 #include "hard-interface.h"
53 #include "originator.h"
56 static struct batadv_socket_client
*batadv_socket_client_hash
[256];
58 static void batadv_socket_add_packet(struct batadv_socket_client
*socket_client
,
59 struct batadv_icmp_header
*icmph
,
63 * batadv_socket_init() - Initialize soft interface independent socket data
65 void batadv_socket_init(void)
67 memset(batadv_socket_client_hash
, 0, sizeof(batadv_socket_client_hash
));
70 static int batadv_socket_open(struct inode
*inode
, struct file
*file
)
73 struct batadv_socket_client
*socket_client
;
75 if (!try_module_get(THIS_MODULE
))
78 batadv_debugfs_deprecated(file
, "");
80 nonseekable_open(inode
, file
);
82 socket_client
= kmalloc(sizeof(*socket_client
), GFP_KERNEL
);
84 module_put(THIS_MODULE
);
88 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
;
95 if (i
== ARRAY_SIZE(batadv_socket_client_hash
)) {
96 pr_err("Error - can't add another packet client: maximum number of clients reached\n");
98 module_put(THIS_MODULE
);
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
);
109 file
->private_data
= socket_client
;
114 static int batadv_socket_release(struct inode
*inode
, struct file
*file
)
116 struct batadv_socket_client
*client
= file
->private_data
;
117 struct batadv_socket_packet
*packet
, *tmp
;
119 spin_lock_bh(&client
->lock
);
121 /* for all packets in the queue ... */
122 list_for_each_entry_safe(packet
, tmp
, &client
->queue_list
, list
) {
123 list_del(&packet
->list
);
127 batadv_socket_client_hash
[client
->index
] = NULL
;
128 spin_unlock_bh(&client
->lock
);
131 module_put(THIS_MODULE
);
136 static ssize_t
batadv_socket_read(struct file
*file
, char __user
*buf
,
137 size_t count
, loff_t
*ppos
)
139 struct batadv_socket_client
*socket_client
= file
->private_data
;
140 struct batadv_socket_packet
*socket_packet
;
144 if ((file
->f_flags
& O_NONBLOCK
) && socket_client
->queue_len
== 0)
147 if (!buf
|| count
< sizeof(struct batadv_icmp_packet
))
150 if (!access_ok(VERIFY_WRITE
, buf
, count
))
153 error
= wait_event_interruptible(socket_client
->queue_wait
,
154 socket_client
->queue_len
);
159 spin_lock_bh(&socket_client
->lock
);
161 socket_packet
= list_first_entry(&socket_client
->queue_list
,
162 struct batadv_socket_packet
, list
);
163 list_del(&socket_packet
->list
);
164 socket_client
->queue_len
--;
166 spin_unlock_bh(&socket_client
->lock
);
168 packet_len
= min(count
, socket_packet
->icmp_len
);
169 error
= copy_to_user(buf
, &socket_packet
->icmp_packet
, packet_len
);
171 kfree(socket_packet
);
179 static ssize_t
batadv_socket_write(struct file
*file
, const char __user
*buff
,
180 size_t len
, loff_t
*off
)
182 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
;
186 struct batadv_icmp_packet_rr
*icmp_packet_rr
;
187 struct batadv_icmp_header
*icmp_header
;
188 struct batadv_orig_node
*orig_node
= NULL
;
189 struct batadv_neigh_node
*neigh_node
= NULL
;
190 size_t packet_len
= sizeof(struct batadv_icmp_packet
);
193 if (len
< sizeof(struct batadv_icmp_header
)) {
194 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
195 "Error - can't send packet from char device: invalid packet size\n");
199 primary_if
= batadv_primary_if_get_selected(bat_priv
);
206 if (len
>= BATADV_ICMP_MAX_PACKET_SIZE
)
207 packet_len
= BATADV_ICMP_MAX_PACKET_SIZE
;
211 skb
= netdev_alloc_skb_ip_align(NULL
, packet_len
+ ETH_HLEN
);
217 skb
->priority
= TC_PRIO_CONTROL
;
218 skb_reserve(skb
, ETH_HLEN
);
219 icmp_header
= skb_put(skb
, packet_len
);
221 if (copy_from_user(icmp_header
, buff
, packet_len
)) {
226 if (icmp_header
->packet_type
!= BATADV_ICMP
) {
227 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
228 "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
233 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");
242 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
245 orig_node
= batadv_orig_hash_find(bat_priv
, icmp_header
->dst
);
249 neigh_node
= batadv_orig_router_get(orig_node
,
254 if (!neigh_node
->if_incoming
)
257 if (neigh_node
->if_incoming
->if_status
!= BATADV_IF_ACTIVE
)
260 icmp_packet_rr
= (struct batadv_icmp_packet_rr
*)icmp_header
;
261 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
);
268 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
269 "Error - can't send packet from char device: got unknown message type\n");
274 icmp_header
->uid
= socket_client
->index
;
276 if (icmp_header
->version
!= BATADV_COMPAT_VERSION
) {
277 icmp_header
->msg_type
= BATADV_PARAMETER_PROBLEM
;
278 icmp_header
->version
= BATADV_COMPAT_VERSION
;
279 batadv_socket_add_packet(socket_client
, icmp_header
,
284 ether_addr_copy(icmp_header
->orig
, primary_if
->net_dev
->dev_addr
);
286 batadv_send_unicast_skb(skb
, neigh_node
);
290 icmp_header
->msg_type
= BATADV_DESTINATION_UNREACHABLE
;
291 batadv_socket_add_packet(socket_client
, icmp_header
, packet_len
);
296 batadv_hardif_put(primary_if
);
298 batadv_neigh_node_put(neigh_node
);
300 batadv_orig_node_put(orig_node
);
304 static __poll_t
batadv_socket_poll(struct file
*file
, poll_table
*wait
)
306 struct batadv_socket_client
*socket_client
= file
->private_data
;
308 poll_wait(file
, &socket_client
->queue_wait
, wait
);
310 if (socket_client
->queue_len
> 0)
311 return EPOLLIN
| EPOLLRDNORM
;
316 static const struct file_operations batadv_fops
= {
317 .owner
= THIS_MODULE
,
318 .open
= batadv_socket_open
,
319 .release
= batadv_socket_release
,
320 .read
= batadv_socket_read
,
321 .write
= batadv_socket_write
,
322 .poll
= batadv_socket_poll
,
327 * batadv_socket_setup() - Create debugfs "socket" file
328 * @bat_priv: the bat priv with all the soft interface information
330 * Return: 0 on success or negative error number in case of failure
332 int batadv_socket_setup(struct batadv_priv
*bat_priv
)
336 if (!bat_priv
->debug_dir
)
339 d
= debugfs_create_file(BATADV_ICMP_SOCKET
, 0600, bat_priv
->debug_dir
,
340 bat_priv
, &batadv_fops
);
351 * batadv_socket_add_packet() - schedule an icmp packet to be sent to
352 * userspace on an icmp socket.
353 * @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
357 static void batadv_socket_add_packet(struct batadv_socket_client
*socket_client
,
358 struct batadv_icmp_header
*icmph
,
361 struct batadv_socket_packet
*socket_packet
;
364 socket_packet
= kmalloc(sizeof(*socket_packet
), GFP_ATOMIC
);
370 /* check the maximum length before filling the buffer */
371 if (len
> sizeof(socket_packet
->icmp_packet
))
372 len
= sizeof(socket_packet
->icmp_packet
);
374 INIT_LIST_HEAD(&socket_packet
->list
);
375 memcpy(&socket_packet
->icmp_packet
, icmph
, len
);
376 socket_packet
->icmp_len
= len
;
378 spin_lock_bh(&socket_client
->lock
);
380 /* while waiting for the lock the socket_client could have been
383 if (!batadv_socket_client_hash
[icmph
->uid
]) {
384 spin_unlock_bh(&socket_client
->lock
);
385 kfree(socket_packet
);
389 list_add_tail(&socket_packet
->list
, &socket_client
->queue_list
);
390 socket_client
->queue_len
++;
392 if (socket_client
->queue_len
> 100) {
393 socket_packet
= list_first_entry(&socket_client
->queue_list
,
394 struct batadv_socket_packet
,
397 list_del(&socket_packet
->list
);
398 kfree(socket_packet
);
399 socket_client
->queue_len
--;
402 spin_unlock_bh(&socket_client
->lock
);
404 wake_up(&socket_client
->queue_wait
);
408 * batadv_socket_receive_packet() - schedule an icmp packet to be received
409 * locally and sent to userspace.
410 * @icmph: pointer to the header of the icmp packet
411 * @icmp_len: total length of the icmp packet
413 void batadv_socket_receive_packet(struct batadv_icmp_header
*icmph
,
416 struct batadv_socket_client
*hash
;
418 hash
= batadv_socket_client_hash
[icmph
->uid
];
420 batadv_socket_add_packet(hash
, icmph
, icmp_len
);