1 /* Copyright (C) 2007-2016 B.A.T.M.A.N. contributors:
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public
7 * License as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, see <http://www.gnu.org/licenses/>.
18 #include "icmp_socket.h"
21 #include <linux/atomic.h>
22 #include <linux/compiler.h>
23 #include <linux/debugfs.h>
24 #include <linux/errno.h>
25 #include <linux/etherdevice.h>
26 #include <linux/export.h>
27 #include <linux/fcntl.h>
29 #include <linux/if_ether.h>
30 #include <linux/kernel.h>
31 #include <linux/list.h>
32 #include <linux/module.h>
33 #include <linux/netdevice.h>
34 #include <linux/pkt_sched.h>
35 #include <linux/poll.h>
36 #include <linux/printk.h>
37 #include <linux/sched.h> /* for linux/wait.h */
38 #include <linux/skbuff.h>
39 #include <linux/slab.h>
40 #include <linux/spinlock.h>
41 #include <linux/stat.h>
42 #include <linux/stddef.h>
43 #include <linux/string.h>
44 #include <linux/uaccess.h>
45 #include <linux/wait.h>
47 #include "hard-interface.h"
49 #include "originator.h"
53 static struct batadv_socket_client
*batadv_socket_client_hash
[256];
55 static void batadv_socket_add_packet(struct batadv_socket_client
*socket_client
,
56 struct batadv_icmp_header
*icmph
,
59 void batadv_socket_init(void)
61 memset(batadv_socket_client_hash
, 0, sizeof(batadv_socket_client_hash
));
64 static int batadv_socket_open(struct inode
*inode
, struct file
*file
)
67 struct batadv_socket_client
*socket_client
;
69 if (!try_module_get(THIS_MODULE
))
72 nonseekable_open(inode
, file
);
74 socket_client
= kmalloc(sizeof(*socket_client
), GFP_KERNEL
);
76 module_put(THIS_MODULE
);
80 for (i
= 0; i
< ARRAY_SIZE(batadv_socket_client_hash
); i
++) {
81 if (!batadv_socket_client_hash
[i
]) {
82 batadv_socket_client_hash
[i
] = socket_client
;
87 if (i
== ARRAY_SIZE(batadv_socket_client_hash
)) {
88 pr_err("Error - can't add another packet client: maximum number of clients reached\n");
90 module_put(THIS_MODULE
);
94 INIT_LIST_HEAD(&socket_client
->queue_list
);
95 socket_client
->queue_len
= 0;
96 socket_client
->index
= i
;
97 socket_client
->bat_priv
= inode
->i_private
;
98 spin_lock_init(&socket_client
->lock
);
99 init_waitqueue_head(&socket_client
->queue_wait
);
101 file
->private_data
= socket_client
;
106 static int batadv_socket_release(struct inode
*inode
, struct file
*file
)
108 struct batadv_socket_client
*client
= file
->private_data
;
109 struct batadv_socket_packet
*packet
, *tmp
;
111 spin_lock_bh(&client
->lock
);
113 /* for all packets in the queue ... */
114 list_for_each_entry_safe(packet
, tmp
, &client
->queue_list
, list
) {
115 list_del(&packet
->list
);
119 batadv_socket_client_hash
[client
->index
] = NULL
;
120 spin_unlock_bh(&client
->lock
);
123 module_put(THIS_MODULE
);
128 static ssize_t
batadv_socket_read(struct file
*file
, char __user
*buf
,
129 size_t count
, loff_t
*ppos
)
131 struct batadv_socket_client
*socket_client
= file
->private_data
;
132 struct batadv_socket_packet
*socket_packet
;
136 if ((file
->f_flags
& O_NONBLOCK
) && (socket_client
->queue_len
== 0))
139 if ((!buf
) || (count
< sizeof(struct batadv_icmp_packet
)))
142 if (!access_ok(VERIFY_WRITE
, buf
, count
))
145 error
= wait_event_interruptible(socket_client
->queue_wait
,
146 socket_client
->queue_len
);
151 spin_lock_bh(&socket_client
->lock
);
153 socket_packet
= list_first_entry(&socket_client
->queue_list
,
154 struct batadv_socket_packet
, list
);
155 list_del(&socket_packet
->list
);
156 socket_client
->queue_len
--;
158 spin_unlock_bh(&socket_client
->lock
);
160 packet_len
= min(count
, socket_packet
->icmp_len
);
161 error
= copy_to_user(buf
, &socket_packet
->icmp_packet
, packet_len
);
163 kfree(socket_packet
);
171 static ssize_t
batadv_socket_write(struct file
*file
, const char __user
*buff
,
172 size_t len
, loff_t
*off
)
174 struct batadv_socket_client
*socket_client
= file
->private_data
;
175 struct batadv_priv
*bat_priv
= socket_client
->bat_priv
;
176 struct batadv_hard_iface
*primary_if
= NULL
;
178 struct batadv_icmp_packet_rr
*icmp_packet_rr
;
179 struct batadv_icmp_header
*icmp_header
;
180 struct batadv_orig_node
*orig_node
= NULL
;
181 struct batadv_neigh_node
*neigh_node
= NULL
;
182 size_t packet_len
= sizeof(struct batadv_icmp_packet
);
185 if (len
< sizeof(struct batadv_icmp_header
)) {
186 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
187 "Error - can't send packet from char device: invalid packet size\n");
191 primary_if
= batadv_primary_if_get_selected(bat_priv
);
198 if (len
>= BATADV_ICMP_MAX_PACKET_SIZE
)
199 packet_len
= BATADV_ICMP_MAX_PACKET_SIZE
;
203 skb
= netdev_alloc_skb_ip_align(NULL
, packet_len
+ ETH_HLEN
);
209 skb
->priority
= TC_PRIO_CONTROL
;
210 skb_reserve(skb
, ETH_HLEN
);
211 icmp_header
= (struct batadv_icmp_header
*)skb_put(skb
, packet_len
);
213 if (copy_from_user(icmp_header
, buff
, packet_len
)) {
218 if (icmp_header
->packet_type
!= BATADV_ICMP
) {
219 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
220 "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
225 switch (icmp_header
->msg_type
) {
226 case BATADV_ECHO_REQUEST
:
227 if (len
< sizeof(struct batadv_icmp_packet
)) {
228 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
229 "Error - can't send packet from char device: invalid packet size\n");
234 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
237 orig_node
= batadv_orig_hash_find(bat_priv
, icmp_header
->dst
);
241 neigh_node
= batadv_orig_router_get(orig_node
,
246 if (!neigh_node
->if_incoming
)
249 if (neigh_node
->if_incoming
->if_status
!= BATADV_IF_ACTIVE
)
252 icmp_packet_rr
= (struct batadv_icmp_packet_rr
*)icmp_header
;
253 if (packet_len
== sizeof(*icmp_packet_rr
)) {
254 addr
= neigh_node
->if_incoming
->net_dev
->dev_addr
;
255 ether_addr_copy(icmp_packet_rr
->rr
[0], addr
);
260 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
261 "Error - can't send packet from char device: got unknown message type\n");
266 icmp_header
->uid
= socket_client
->index
;
268 if (icmp_header
->version
!= BATADV_COMPAT_VERSION
) {
269 icmp_header
->msg_type
= BATADV_PARAMETER_PROBLEM
;
270 icmp_header
->version
= BATADV_COMPAT_VERSION
;
271 batadv_socket_add_packet(socket_client
, icmp_header
,
276 ether_addr_copy(icmp_header
->orig
, primary_if
->net_dev
->dev_addr
);
278 batadv_send_unicast_skb(skb
, neigh_node
);
282 icmp_header
->msg_type
= BATADV_DESTINATION_UNREACHABLE
;
283 batadv_socket_add_packet(socket_client
, icmp_header
, packet_len
);
288 batadv_hardif_put(primary_if
);
290 batadv_neigh_node_put(neigh_node
);
292 batadv_orig_node_put(orig_node
);
296 static unsigned int batadv_socket_poll(struct file
*file
, poll_table
*wait
)
298 struct batadv_socket_client
*socket_client
= file
->private_data
;
300 poll_wait(file
, &socket_client
->queue_wait
, wait
);
302 if (socket_client
->queue_len
> 0)
303 return POLLIN
| POLLRDNORM
;
308 static const struct file_operations batadv_fops
= {
309 .owner
= THIS_MODULE
,
310 .open
= batadv_socket_open
,
311 .release
= batadv_socket_release
,
312 .read
= batadv_socket_read
,
313 .write
= batadv_socket_write
,
314 .poll
= batadv_socket_poll
,
318 int batadv_socket_setup(struct batadv_priv
*bat_priv
)
322 if (!bat_priv
->debug_dir
)
325 d
= debugfs_create_file(BATADV_ICMP_SOCKET
, S_IFREG
| S_IWUSR
| S_IRUSR
,
326 bat_priv
->debug_dir
, bat_priv
, &batadv_fops
);
337 * batadv_socket_add_packet - schedule an icmp packet to be sent to
338 * userspace on an icmp socket.
339 * @socket_client: the socket this packet belongs to
340 * @icmph: pointer to the header of the icmp packet
341 * @icmp_len: total length of the icmp packet
343 static void batadv_socket_add_packet(struct batadv_socket_client
*socket_client
,
344 struct batadv_icmp_header
*icmph
,
347 struct batadv_socket_packet
*socket_packet
;
350 socket_packet
= kmalloc(sizeof(*socket_packet
), GFP_ATOMIC
);
356 /* check the maximum length before filling the buffer */
357 if (len
> sizeof(socket_packet
->icmp_packet
))
358 len
= sizeof(socket_packet
->icmp_packet
);
360 INIT_LIST_HEAD(&socket_packet
->list
);
361 memcpy(&socket_packet
->icmp_packet
, icmph
, len
);
362 socket_packet
->icmp_len
= len
;
364 spin_lock_bh(&socket_client
->lock
);
366 /* while waiting for the lock the socket_client could have been
369 if (!batadv_socket_client_hash
[icmph
->uid
]) {
370 spin_unlock_bh(&socket_client
->lock
);
371 kfree(socket_packet
);
375 list_add_tail(&socket_packet
->list
, &socket_client
->queue_list
);
376 socket_client
->queue_len
++;
378 if (socket_client
->queue_len
> 100) {
379 socket_packet
= list_first_entry(&socket_client
->queue_list
,
380 struct batadv_socket_packet
,
383 list_del(&socket_packet
->list
);
384 kfree(socket_packet
);
385 socket_client
->queue_len
--;
388 spin_unlock_bh(&socket_client
->lock
);
390 wake_up(&socket_client
->queue_wait
);
394 * batadv_socket_receive_packet - schedule an icmp packet to be received
395 * locally and sent to userspace.
396 * @icmph: pointer to the header of the icmp packet
397 * @icmp_len: total length of the icmp packet
399 void batadv_socket_receive_packet(struct batadv_icmp_header
*icmph
,
402 struct batadv_socket_client
*hash
;
404 hash
= batadv_socket_client_hash
[icmph
->uid
];
406 batadv_socket_add_packet(hash
, icmph
, icmp_len
);