1 /* Copyright (C) 2007-2012 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, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 #include <linux/debugfs.h>
22 #include <linux/slab.h>
23 #include "icmp_socket.h"
26 #include "originator.h"
27 #include "hard-interface.h"
29 static struct batadv_socket_client
*batadv_socket_client_hash
[256];
31 static void batadv_socket_add_packet(struct batadv_socket_client
*socket_client
,
32 struct batadv_icmp_packet_rr
*icmp_packet
,
35 void batadv_socket_init(void)
37 memset(batadv_socket_client_hash
, 0, sizeof(batadv_socket_client_hash
));
40 static int batadv_socket_open(struct inode
*inode
, struct file
*file
)
43 struct batadv_socket_client
*socket_client
;
45 nonseekable_open(inode
, file
);
47 socket_client
= kmalloc(sizeof(*socket_client
), GFP_KERNEL
);
52 for (i
= 0; i
< ARRAY_SIZE(batadv_socket_client_hash
); i
++) {
53 if (!batadv_socket_client_hash
[i
]) {
54 batadv_socket_client_hash
[i
] = socket_client
;
59 if (i
== ARRAY_SIZE(batadv_socket_client_hash
)) {
60 pr_err("Error - can't add another packet client: maximum number of clients reached\n");
65 INIT_LIST_HEAD(&socket_client
->queue_list
);
66 socket_client
->queue_len
= 0;
67 socket_client
->index
= i
;
68 socket_client
->bat_priv
= inode
->i_private
;
69 spin_lock_init(&socket_client
->lock
);
70 init_waitqueue_head(&socket_client
->queue_wait
);
72 file
->private_data
= socket_client
;
74 batadv_inc_module_count();
78 static int batadv_socket_release(struct inode
*inode
, struct file
*file
)
80 struct batadv_socket_client
*socket_client
= file
->private_data
;
81 struct batadv_socket_packet
*socket_packet
;
82 struct list_head
*list_pos
, *list_pos_tmp
;
84 spin_lock_bh(&socket_client
->lock
);
86 /* for all packets in the queue ... */
87 list_for_each_safe(list_pos
, list_pos_tmp
, &socket_client
->queue_list
) {
88 socket_packet
= list_entry(list_pos
,
89 struct batadv_socket_packet
, list
);
95 batadv_socket_client_hash
[socket_client
->index
] = NULL
;
96 spin_unlock_bh(&socket_client
->lock
);
99 batadv_dec_module_count();
104 static ssize_t
batadv_socket_read(struct file
*file
, char __user
*buf
,
105 size_t count
, loff_t
*ppos
)
107 struct batadv_socket_client
*socket_client
= file
->private_data
;
108 struct batadv_socket_packet
*socket_packet
;
112 if ((file
->f_flags
& O_NONBLOCK
) && (socket_client
->queue_len
== 0))
115 if ((!buf
) || (count
< sizeof(struct batadv_icmp_packet
)))
118 if (!access_ok(VERIFY_WRITE
, buf
, count
))
121 error
= wait_event_interruptible(socket_client
->queue_wait
,
122 socket_client
->queue_len
);
127 spin_lock_bh(&socket_client
->lock
);
129 socket_packet
= list_first_entry(&socket_client
->queue_list
,
130 struct batadv_socket_packet
, list
);
131 list_del(&socket_packet
->list
);
132 socket_client
->queue_len
--;
134 spin_unlock_bh(&socket_client
->lock
);
136 packet_len
= min(count
, socket_packet
->icmp_len
);
137 error
= copy_to_user(buf
, &socket_packet
->icmp_packet
, packet_len
);
139 kfree(socket_packet
);
147 static ssize_t
batadv_socket_write(struct file
*file
, const char __user
*buff
,
148 size_t len
, loff_t
*off
)
150 struct batadv_socket_client
*socket_client
= file
->private_data
;
151 struct batadv_priv
*bat_priv
= socket_client
->bat_priv
;
152 struct batadv_hard_iface
*primary_if
= NULL
;
154 struct batadv_icmp_packet_rr
*icmp_packet
;
156 struct batadv_orig_node
*orig_node
= NULL
;
157 struct batadv_neigh_node
*neigh_node
= NULL
;
158 size_t packet_len
= sizeof(struct batadv_icmp_packet
);
160 if (len
< sizeof(struct batadv_icmp_packet
)) {
161 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
162 "Error - can't send packet from char device: invalid packet size\n");
166 primary_if
= batadv_primary_if_get_selected(bat_priv
);
173 if (len
>= sizeof(struct batadv_icmp_packet_rr
))
174 packet_len
= sizeof(struct batadv_icmp_packet_rr
);
176 skb
= dev_alloc_skb(packet_len
+ ETH_HLEN
);
182 skb_reserve(skb
, ETH_HLEN
);
183 icmp_packet
= (struct batadv_icmp_packet_rr
*)skb_put(skb
, packet_len
);
185 if (copy_from_user(icmp_packet
, buff
, packet_len
)) {
190 if (icmp_packet
->header
.packet_type
!= BATADV_ICMP
) {
191 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
192 "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
197 if (icmp_packet
->msg_type
!= BATADV_ECHO_REQUEST
) {
198 batadv_dbg(BATADV_DBG_BATMAN
, bat_priv
,
199 "Error - can't send packet from char device: got bogus message type (expected: ECHO_REQUEST)\n");
204 icmp_packet
->uid
= socket_client
->index
;
206 if (icmp_packet
->header
.version
!= BATADV_COMPAT_VERSION
) {
207 icmp_packet
->msg_type
= BATADV_PARAMETER_PROBLEM
;
208 icmp_packet
->header
.version
= BATADV_COMPAT_VERSION
;
209 batadv_socket_add_packet(socket_client
, icmp_packet
,
214 if (atomic_read(&bat_priv
->mesh_state
) != BATADV_MESH_ACTIVE
)
217 orig_node
= batadv_orig_hash_find(bat_priv
, icmp_packet
->dst
);
221 neigh_node
= batadv_orig_node_get_router(orig_node
);
225 if (!neigh_node
->if_incoming
)
228 if (neigh_node
->if_incoming
->if_status
!= BATADV_IF_ACTIVE
)
231 memcpy(icmp_packet
->orig
,
232 primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
234 if (packet_len
== sizeof(struct batadv_icmp_packet_rr
))
235 memcpy(icmp_packet
->rr
,
236 neigh_node
->if_incoming
->net_dev
->dev_addr
, ETH_ALEN
);
238 batadv_send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
242 icmp_packet
->msg_type
= BATADV_DESTINATION_UNREACHABLE
;
243 batadv_socket_add_packet(socket_client
, icmp_packet
, packet_len
);
248 batadv_hardif_free_ref(primary_if
);
250 batadv_neigh_node_free_ref(neigh_node
);
252 batadv_orig_node_free_ref(orig_node
);
256 static unsigned int batadv_socket_poll(struct file
*file
, poll_table
*wait
)
258 struct batadv_socket_client
*socket_client
= file
->private_data
;
260 poll_wait(file
, &socket_client
->queue_wait
, wait
);
262 if (socket_client
->queue_len
> 0)
263 return POLLIN
| POLLRDNORM
;
268 static const struct file_operations batadv_fops
= {
269 .owner
= THIS_MODULE
,
270 .open
= batadv_socket_open
,
271 .release
= batadv_socket_release
,
272 .read
= batadv_socket_read
,
273 .write
= batadv_socket_write
,
274 .poll
= batadv_socket_poll
,
278 int batadv_socket_setup(struct batadv_priv
*bat_priv
)
282 if (!bat_priv
->debug_dir
)
285 d
= debugfs_create_file(BATADV_ICMP_SOCKET
, S_IFREG
| S_IWUSR
| S_IRUSR
,
286 bat_priv
->debug_dir
, bat_priv
, &batadv_fops
);
296 static void batadv_socket_add_packet(struct batadv_socket_client
*socket_client
,
297 struct batadv_icmp_packet_rr
*icmp_packet
,
300 struct batadv_socket_packet
*socket_packet
;
302 socket_packet
= kmalloc(sizeof(*socket_packet
), GFP_ATOMIC
);
307 INIT_LIST_HEAD(&socket_packet
->list
);
308 memcpy(&socket_packet
->icmp_packet
, icmp_packet
, icmp_len
);
309 socket_packet
->icmp_len
= icmp_len
;
311 spin_lock_bh(&socket_client
->lock
);
313 /* while waiting for the lock the socket_client could have been
316 if (!batadv_socket_client_hash
[icmp_packet
->uid
]) {
317 spin_unlock_bh(&socket_client
->lock
);
318 kfree(socket_packet
);
322 list_add_tail(&socket_packet
->list
, &socket_client
->queue_list
);
323 socket_client
->queue_len
++;
325 if (socket_client
->queue_len
> 100) {
326 socket_packet
= list_first_entry(&socket_client
->queue_list
,
327 struct batadv_socket_packet
,
330 list_del(&socket_packet
->list
);
331 kfree(socket_packet
);
332 socket_client
->queue_len
--;
335 spin_unlock_bh(&socket_client
->lock
);
337 wake_up(&socket_client
->queue_wait
);
340 void batadv_socket_receive_packet(struct batadv_icmp_packet_rr
*icmp_packet
,
343 struct batadv_socket_client
*hash
;
345 hash
= batadv_socket_client_hash
[icmp_packet
->uid
];
347 batadv_socket_add_packet(hash
, icmp_packet
, icmp_len
);