2 * Copyright (C) 2007-2012 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, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
23 #include <linux/debugfs.h>
24 #include <linux/slab.h>
25 #include "icmp_socket.h"
28 #include "originator.h"
29 #include "hard-interface.h"
31 static struct socket_client
*socket_client_hash
[256];
33 static void bat_socket_add_packet(struct socket_client
*socket_client
,
34 struct icmp_packet_rr
*icmp_packet
,
37 void bat_socket_init(void)
39 memset(socket_client_hash
, 0, sizeof(socket_client_hash
));
42 static int bat_socket_open(struct inode
*inode
, struct file
*file
)
45 struct socket_client
*socket_client
;
47 nonseekable_open(inode
, file
);
49 socket_client
= kmalloc(sizeof(*socket_client
), GFP_KERNEL
);
54 for (i
= 0; i
< ARRAY_SIZE(socket_client_hash
); i
++) {
55 if (!socket_client_hash
[i
]) {
56 socket_client_hash
[i
] = socket_client
;
61 if (i
== ARRAY_SIZE(socket_client_hash
)) {
62 pr_err("Error - can't add another packet client: maximum number of clients reached\n");
67 INIT_LIST_HEAD(&socket_client
->queue_list
);
68 socket_client
->queue_len
= 0;
69 socket_client
->index
= i
;
70 socket_client
->bat_priv
= inode
->i_private
;
71 spin_lock_init(&socket_client
->lock
);
72 init_waitqueue_head(&socket_client
->queue_wait
);
74 file
->private_data
= socket_client
;
80 static int bat_socket_release(struct inode
*inode
, struct file
*file
)
82 struct socket_client
*socket_client
= file
->private_data
;
83 struct socket_packet
*socket_packet
;
84 struct list_head
*list_pos
, *list_pos_tmp
;
86 spin_lock_bh(&socket_client
->lock
);
88 /* for all packets in the queue ... */
89 list_for_each_safe(list_pos
, list_pos_tmp
, &socket_client
->queue_list
) {
90 socket_packet
= list_entry(list_pos
,
91 struct socket_packet
, list
);
97 socket_client_hash
[socket_client
->index
] = NULL
;
98 spin_unlock_bh(&socket_client
->lock
);
100 kfree(socket_client
);
106 static ssize_t
bat_socket_read(struct file
*file
, char __user
*buf
,
107 size_t count
, loff_t
*ppos
)
109 struct socket_client
*socket_client
= file
->private_data
;
110 struct socket_packet
*socket_packet
;
114 if ((file
->f_flags
& O_NONBLOCK
) && (socket_client
->queue_len
== 0))
117 if ((!buf
) || (count
< sizeof(struct icmp_packet
)))
120 if (!access_ok(VERIFY_WRITE
, buf
, count
))
123 error
= wait_event_interruptible(socket_client
->queue_wait
,
124 socket_client
->queue_len
);
129 spin_lock_bh(&socket_client
->lock
);
131 socket_packet
= list_first_entry(&socket_client
->queue_list
,
132 struct socket_packet
, list
);
133 list_del(&socket_packet
->list
);
134 socket_client
->queue_len
--;
136 spin_unlock_bh(&socket_client
->lock
);
138 packet_len
= min(count
, socket_packet
->icmp_len
);
139 error
= copy_to_user(buf
, &socket_packet
->icmp_packet
, packet_len
);
141 kfree(socket_packet
);
149 static ssize_t
bat_socket_write(struct file
*file
, const char __user
*buff
,
150 size_t len
, loff_t
*off
)
152 struct socket_client
*socket_client
= file
->private_data
;
153 struct bat_priv
*bat_priv
= socket_client
->bat_priv
;
154 struct hard_iface
*primary_if
= NULL
;
156 struct icmp_packet_rr
*icmp_packet
;
158 struct orig_node
*orig_node
= NULL
;
159 struct neigh_node
*neigh_node
= NULL
;
160 size_t packet_len
= sizeof(struct icmp_packet
);
162 if (len
< sizeof(struct icmp_packet
)) {
163 bat_dbg(DBG_BATMAN
, bat_priv
,
164 "Error - can't send packet from char device: invalid packet size\n");
168 primary_if
= primary_if_get_selected(bat_priv
);
175 if (len
>= sizeof(struct icmp_packet_rr
))
176 packet_len
= sizeof(struct icmp_packet_rr
);
178 skb
= dev_alloc_skb(packet_len
+ ETH_HLEN
);
184 skb_reserve(skb
, ETH_HLEN
);
185 icmp_packet
= (struct icmp_packet_rr
*)skb_put(skb
, packet_len
);
187 if (copy_from_user(icmp_packet
, buff
, packet_len
)) {
192 if (icmp_packet
->header
.packet_type
!= BAT_ICMP
) {
193 bat_dbg(DBG_BATMAN
, bat_priv
,
194 "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
199 if (icmp_packet
->msg_type
!= ECHO_REQUEST
) {
200 bat_dbg(DBG_BATMAN
, bat_priv
,
201 "Error - can't send packet from char device: got bogus message type (expected: ECHO_REQUEST)\n");
206 icmp_packet
->uid
= socket_client
->index
;
208 if (icmp_packet
->header
.version
!= COMPAT_VERSION
) {
209 icmp_packet
->msg_type
= PARAMETER_PROBLEM
;
210 icmp_packet
->header
.version
= COMPAT_VERSION
;
211 bat_socket_add_packet(socket_client
, icmp_packet
, packet_len
);
215 if (atomic_read(&bat_priv
->mesh_state
) != MESH_ACTIVE
)
218 orig_node
= orig_hash_find(bat_priv
, icmp_packet
->dst
);
222 neigh_node
= orig_node_get_router(orig_node
);
226 if (!neigh_node
->if_incoming
)
229 if (neigh_node
->if_incoming
->if_status
!= IF_ACTIVE
)
232 memcpy(icmp_packet
->orig
,
233 primary_if
->net_dev
->dev_addr
, ETH_ALEN
);
235 if (packet_len
== sizeof(struct icmp_packet_rr
))
236 memcpy(icmp_packet
->rr
,
237 neigh_node
->if_incoming
->net_dev
->dev_addr
, ETH_ALEN
);
239 send_skb_packet(skb
, neigh_node
->if_incoming
, neigh_node
->addr
);
243 icmp_packet
->msg_type
= DESTINATION_UNREACHABLE
;
244 bat_socket_add_packet(socket_client
, icmp_packet
, packet_len
);
249 hardif_free_ref(primary_if
);
251 neigh_node_free_ref(neigh_node
);
253 orig_node_free_ref(orig_node
);
257 static unsigned int bat_socket_poll(struct file
*file
, poll_table
*wait
)
259 struct socket_client
*socket_client
= file
->private_data
;
261 poll_wait(file
, &socket_client
->queue_wait
, wait
);
263 if (socket_client
->queue_len
> 0)
264 return POLLIN
| POLLRDNORM
;
269 static const struct file_operations fops
= {
270 .owner
= THIS_MODULE
,
271 .open
= bat_socket_open
,
272 .release
= bat_socket_release
,
273 .read
= bat_socket_read
,
274 .write
= bat_socket_write
,
275 .poll
= bat_socket_poll
,
279 int bat_socket_setup(struct bat_priv
*bat_priv
)
283 if (!bat_priv
->debug_dir
)
286 d
= debugfs_create_file(ICMP_SOCKET
, S_IFREG
| S_IWUSR
| S_IRUSR
,
287 bat_priv
->debug_dir
, bat_priv
, &fops
);
297 static void bat_socket_add_packet(struct socket_client
*socket_client
,
298 struct icmp_packet_rr
*icmp_packet
,
301 struct socket_packet
*socket_packet
;
303 socket_packet
= kmalloc(sizeof(*socket_packet
), GFP_ATOMIC
);
308 INIT_LIST_HEAD(&socket_packet
->list
);
309 memcpy(&socket_packet
->icmp_packet
, icmp_packet
, icmp_len
);
310 socket_packet
->icmp_len
= icmp_len
;
312 spin_lock_bh(&socket_client
->lock
);
314 /* while waiting for the lock the socket_client could have been
316 if (!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 socket_packet
, list
);
329 list_del(&socket_packet
->list
);
330 kfree(socket_packet
);
331 socket_client
->queue_len
--;
334 spin_unlock_bh(&socket_client
->lock
);
336 wake_up(&socket_client
->queue_wait
);
339 void bat_socket_receive_packet(struct icmp_packet_rr
*icmp_packet
,
342 struct socket_client
*hash
= socket_client_hash
[icmp_packet
->uid
];
345 bat_socket_add_packet(hash
, icmp_packet
, icmp_len
);