1 /* Copyright (C) 2007-2015 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"
48 #include "originator.h"
52 static struct batadv_socket_client
*batadv_socket_client_hash
[256];
54 static void batadv_socket_add_packet(struct batadv_socket_client
*socket_client
,
55 struct batadv_icmp_header
*icmph
,
58 void batadv_socket_init(void)
60 memset(batadv_socket_client_hash
, 0, sizeof(batadv_socket_client_hash
));
63 static int batadv_socket_open(struct inode
*inode
, struct file
*file
)
66 struct batadv_socket_client
*socket_client
;
68 if (!try_module_get(THIS_MODULE
))
71 nonseekable_open(inode
, file
);
73 socket_client
= kmalloc(sizeof(*socket_client
), GFP_KERNEL
);
75 module_put(THIS_MODULE
);
79 for (i
= 0; i
< ARRAY_SIZE(batadv_socket_client_hash
); i
++) {
80 if (!batadv_socket_client_hash
[i
]) {
81 batadv_socket_client_hash
[i
] = socket_client
;
86 if (i
== ARRAY_SIZE(batadv_socket_client_hash
)) {
87 pr_err("Error - can't add another packet client: maximum number of clients reached\n");
89 module_put(THIS_MODULE
);
93 INIT_LIST_HEAD(&socket_client
->queue_list
);
94 socket_client
->queue_len
= 0;
95 socket_client
->index
= i
;
96 socket_client
->bat_priv
= inode
->i_private
;
97 spin_lock_init(&socket_client
->lock
);
98 init_waitqueue_head(&socket_client
->queue_wait
);
100 file
->private_data
= socket_client
;
105 static int batadv_socket_release(struct inode
*inode
, struct file
*file
)
107 struct batadv_socket_client
*socket_client
= file
->private_data
;
108 struct batadv_socket_packet
*socket_packet
;
109 struct list_head
*list_pos
, *list_pos_tmp
;
111 spin_lock_bh(&socket_client
->lock
);
113 /* for all packets in the queue ... */
114 list_for_each_safe(list_pos
, list_pos_tmp
, &socket_client
->queue_list
) {
115 socket_packet
= list_entry(list_pos
,
116 struct batadv_socket_packet
, list
);
119 kfree(socket_packet
);
122 batadv_socket_client_hash
[socket_client
->index
] = NULL
;
123 spin_unlock_bh(&socket_client
->lock
);
125 kfree(socket_client
);
126 module_put(THIS_MODULE
);
131 static ssize_t
batadv_socket_read(struct file
*file
, char __user
*buf
,
132 size_t count
, loff_t
*ppos
)
134 struct batadv_socket_client
*socket_client
= file
->private_data
;
135 struct batadv_socket_packet
*socket_packet
;
139 if ((file
->f_flags
& O_NONBLOCK
) && (socket_client
->queue_len
== 0))
142 if ((!buf
) || (count
< sizeof(struct batadv_icmp_packet
)))
145 if (!access_ok(VERIFY_WRITE
, buf
, count
))
148 error
= wait_event_interruptible(socket_client
->queue_wait
,
149 socket_client
->queue_len
);
154 spin_lock_bh(&socket_client
->lock
);
156 socket_packet
= list_first_entry(&socket_client
->queue_list
,
157 struct batadv_socket_packet
, list
);
158 list_del(&socket_packet
->list
);
159 socket_client
->queue_len
--;
161 spin_unlock_bh(&socket_client
->lock
);
163 packet_len
= min(count
, socket_packet
->icmp_len
);
164 error
= copy_to_user(buf
, &socket_packet
->icmp_packet
, packet_len
);
166 kfree(socket_packet
);
174 static ssize_t
batadv_socket_write(struct file
*file
, const char __user
*buff
,
175 size_t len
, loff_t
*off
)
177 struct batadv_socket_client
*socket_client
= file
->private_data
;
178 struct batadv_priv
*bat_priv
= socket_client
->bat_priv
;
179 struct batadv_hard_iface
*primary_if
= NULL
;
181 struct batadv_icmp_packet_rr
*icmp_packet_rr
;
182 struct batadv_icmp_header
*icmp_header
;
183 struct batadv_orig_node
*orig_node
= NULL
;
184 struct batadv_neigh_node
*neigh_node
= NULL
;
185 size_t packet_len
= sizeof(struct batadv_icmp_packet
);
188 if (len
< sizeof(struct batadv_icmp_header
)) {
189 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
190 "Error - can't send packet from char device: invalid packet size\n");
194 primary_if
= batadv_primary_if_get_selected(bat_priv
);
201 if (len
>= BATADV_ICMP_MAX_PACKET_SIZE
)
202 packet_len
= BATADV_ICMP_MAX_PACKET_SIZE
;
206 skb
= netdev_alloc_skb_ip_align(NULL
, packet_len
+ ETH_HLEN
);
212 skb
->priority
= TC_PRIO_CONTROL
;
213 skb_reserve(skb
, ETH_HLEN
);
214 icmp_header
= (struct batadv_icmp_header
*)skb_put(skb
, packet_len
);
216 if (copy_from_user(icmp_header
, buff
, packet_len
)) {
221 if (icmp_header
->packet_type
!= BATADV_ICMP
) {
222 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
223 "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
228 switch (icmp_header
->msg_type
) {
229 case BATADV_ECHO_REQUEST
:
230 if (len
< sizeof(struct batadv_icmp_packet
)) {
231 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
232 "Error - can't send packet from char device: invalid packet size\n");
237 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
240 orig_node
= batadv_orig_hash_find(bat_priv
, icmp_header
->dst
);
244 neigh_node
= batadv_orig_router_get(orig_node
,
249 if (!neigh_node
->if_incoming
)
252 if (neigh_node
->if_incoming
->if_status
!= BATADV_IF_ACTIVE
)
255 icmp_packet_rr
= (struct batadv_icmp_packet_rr
*)icmp_header
;
256 if (packet_len
== sizeof(*icmp_packet_rr
)) {
257 addr
= neigh_node
->if_incoming
->net_dev
->dev_addr
;
258 ether_addr_copy(icmp_packet_rr
->rr
[0], addr
);
263 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
264 "Error - can't send packet from char device: got unknown message type\n");
269 icmp_header
->uid
= socket_client
->index
;
271 if (icmp_header
->version
!= BATADV_COMPAT_VERSION
) {
272 icmp_header
->msg_type
= BATADV_PARAMETER_PROBLEM
;
273 icmp_header
->version
= BATADV_COMPAT_VERSION
;
274 batadv_socket_add_packet(socket_client
, icmp_header
,
279 ether_addr_copy(icmp_header
->orig
, primary_if
->net_dev
->dev_addr
);
281 batadv_send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
285 icmp_header
->msg_type
= BATADV_DESTINATION_UNREACHABLE
;
286 batadv_socket_add_packet(socket_client
, icmp_header
, packet_len
);
291 batadv_hardif_free_ref(primary_if
);
293 batadv_neigh_node_free_ref(neigh_node
);
295 batadv_orig_node_free_ref(orig_node
);
299 static unsigned int batadv_socket_poll(struct file
*file
, poll_table
*wait
)
301 struct batadv_socket_client
*socket_client
= file
->private_data
;
303 poll_wait(file
, &socket_client
->queue_wait
, wait
);
305 if (socket_client
->queue_len
> 0)
306 return POLLIN
| POLLRDNORM
;
311 static const struct file_operations batadv_fops
= {
312 .owner
= THIS_MODULE
,
313 .open
= batadv_socket_open
,
314 .release
= batadv_socket_release
,
315 .read
= batadv_socket_read
,
316 .write
= batadv_socket_write
,
317 .poll
= batadv_socket_poll
,
321 int batadv_socket_setup(struct batadv_priv
*bat_priv
)
325 if (!bat_priv
->debug_dir
)
328 d
= debugfs_create_file(BATADV_ICMP_SOCKET
, S_IFREG
| S_IWUSR
| S_IRUSR
,
329 bat_priv
->debug_dir
, bat_priv
, &batadv_fops
);
340 * batadv_socket_receive_packet - schedule an icmp packet to be sent to
341 * userspace on an icmp socket.
342 * @socket_client: the socket this packet belongs to
343 * @icmph: pointer to the header of the icmp packet
344 * @icmp_len: total length of the icmp packet
346 static void batadv_socket_add_packet(struct batadv_socket_client
*socket_client
,
347 struct batadv_icmp_header
*icmph
,
350 struct batadv_socket_packet
*socket_packet
;
353 socket_packet
= kmalloc(sizeof(*socket_packet
), GFP_ATOMIC
);
359 /* check the maximum length before filling the buffer */
360 if (len
> sizeof(socket_packet
->icmp_packet
))
361 len
= sizeof(socket_packet
->icmp_packet
);
363 INIT_LIST_HEAD(&socket_packet
->list
);
364 memcpy(&socket_packet
->icmp_packet
, icmph
, len
);
365 socket_packet
->icmp_len
= len
;
367 spin_lock_bh(&socket_client
->lock
);
369 /* while waiting for the lock the socket_client could have been
372 if (!batadv_socket_client_hash
[icmph
->uid
]) {
373 spin_unlock_bh(&socket_client
->lock
);
374 kfree(socket_packet
);
378 list_add_tail(&socket_packet
->list
, &socket_client
->queue_list
);
379 socket_client
->queue_len
++;
381 if (socket_client
->queue_len
> 100) {
382 socket_packet
= list_first_entry(&socket_client
->queue_list
,
383 struct batadv_socket_packet
,
386 list_del(&socket_packet
->list
);
387 kfree(socket_packet
);
388 socket_client
->queue_len
--;
391 spin_unlock_bh(&socket_client
->lock
);
393 wake_up(&socket_client
->queue_wait
);
397 * batadv_socket_receive_packet - schedule an icmp packet to be received
398 * locally and sent to userspace.
399 * @icmph: pointer to the header of the icmp packet
400 * @icmp_len: total length of the icmp packet
402 void batadv_socket_receive_packet(struct batadv_icmp_header
*icmph
,
405 struct batadv_socket_client
*hash
;
407 hash
= batadv_socket_client_hash
[icmph
->uid
];
409 batadv_socket_add_packet(hash
, icmph
, icmp_len
);