1 /* Copyright (C) 2007-2013 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/>.
19 #include <linux/debugfs.h>
20 #include <linux/slab.h>
21 #include "icmp_socket.h"
24 #include "originator.h"
25 #include "hard-interface.h"
27 static struct batadv_socket_client *batadv_socket_client_hash[256];
29 static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
30 struct batadv_icmp_header *icmph,
33 void batadv_socket_init(void)
35 memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash));
38 static int batadv_socket_open(struct inode *inode, struct file *file)
41 struct batadv_socket_client *socket_client;
43 if (!try_module_get(THIS_MODULE))
46 nonseekable_open(inode, file);
48 socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
50 module_put(THIS_MODULE);
54 for (i = 0; i < ARRAY_SIZE(batadv_socket_client_hash); i++) {
55 if (!batadv_socket_client_hash[i]) {
56 batadv_socket_client_hash[i] = socket_client;
61 if (i == ARRAY_SIZE(batadv_socket_client_hash)) {
62 pr_err("Error - can't add another packet client: maximum number of clients reached\n");
64 module_put(THIS_MODULE);
68 INIT_LIST_HEAD(&socket_client->queue_list);
69 socket_client->queue_len = 0;
70 socket_client->index = i;
71 socket_client->bat_priv = inode->i_private;
72 spin_lock_init(&socket_client->lock);
73 init_waitqueue_head(&socket_client->queue_wait);
75 file->private_data = socket_client;
80 static int batadv_socket_release(struct inode *inode, struct file *file)
82 struct batadv_socket_client *socket_client = file->private_data;
83 struct batadv_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 batadv_socket_packet, list);
97 batadv_socket_client_hash[socket_client->index] = NULL;
98 spin_unlock_bh(&socket_client->lock);
100 kfree(socket_client);
101 module_put(THIS_MODULE);
106 static ssize_t batadv_socket_read(struct file *file, char __user *buf,
107 size_t count, loff_t *ppos)
109 struct batadv_socket_client *socket_client = file->private_data;
110 struct batadv_socket_packet *socket_packet;
114 if ((file->f_flags & O_NONBLOCK) && (socket_client->queue_len == 0))
117 if ((!buf) || (count < sizeof(struct batadv_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 batadv_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 batadv_socket_write(struct file *file, const char __user *buff,
150 size_t len, loff_t *off)
152 struct batadv_socket_client *socket_client = file->private_data;
153 struct batadv_priv *bat_priv = socket_client->bat_priv;
154 struct batadv_hard_iface *primary_if = NULL;
156 struct batadv_icmp_packet_rr *icmp_packet_rr;
157 struct batadv_icmp_header *icmp_header;
158 struct batadv_orig_node *orig_node = NULL;
159 struct batadv_neigh_node *neigh_node = NULL;
160 size_t packet_len = sizeof(struct batadv_icmp_packet);
162 if (len < sizeof(struct batadv_icmp_header)) {
163 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
164 "Error - can't send packet from char device: invalid packet size\n");
168 primary_if = batadv_primary_if_get_selected(bat_priv);
175 if (len >= BATADV_ICMP_MAX_PACKET_SIZE)
176 packet_len = BATADV_ICMP_MAX_PACKET_SIZE;
180 skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN);
186 skb->priority = TC_PRIO_CONTROL;
187 skb_reserve(skb, ETH_HLEN);
188 icmp_header = (struct batadv_icmp_header *)skb_put(skb, packet_len);
190 if (copy_from_user(icmp_header, buff, packet_len)) {
195 if (icmp_header->packet_type != BATADV_ICMP) {
196 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
197 "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
202 switch (icmp_header->msg_type) {
203 case BATADV_ECHO_REQUEST:
204 if (len < sizeof(struct batadv_icmp_packet)) {
205 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
206 "Error - can't send packet from char device: invalid packet size\n");
211 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
214 orig_node = batadv_orig_hash_find(bat_priv, icmp_header->dst);
218 neigh_node = batadv_orig_node_get_router(orig_node);
222 if (!neigh_node->if_incoming)
225 if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
228 icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmp_header;
229 if (packet_len == sizeof(*icmp_packet_rr))
230 memcpy(icmp_packet_rr->rr,
231 neigh_node->if_incoming->net_dev->dev_addr,
236 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
237 "Error - can't send packet from char device: got unknown message type\n");
242 icmp_header->uid = socket_client->index;
244 if (icmp_header->version != BATADV_COMPAT_VERSION) {
245 icmp_header->msg_type = BATADV_PARAMETER_PROBLEM;
246 icmp_header->version = BATADV_COMPAT_VERSION;
247 batadv_socket_add_packet(socket_client, icmp_header,
252 memcpy(icmp_header->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
254 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
258 icmp_header->msg_type = BATADV_DESTINATION_UNREACHABLE;
259 batadv_socket_add_packet(socket_client, icmp_header, packet_len);
264 batadv_hardif_free_ref(primary_if);
266 batadv_neigh_node_free_ref(neigh_node);
268 batadv_orig_node_free_ref(orig_node);
272 static unsigned int batadv_socket_poll(struct file *file, poll_table *wait)
274 struct batadv_socket_client *socket_client = file->private_data;
276 poll_wait(file, &socket_client->queue_wait, wait);
278 if (socket_client->queue_len > 0)
279 return POLLIN | POLLRDNORM;
284 static const struct file_operations batadv_fops = {
285 .owner = THIS_MODULE,
286 .open = batadv_socket_open,
287 .release = batadv_socket_release,
288 .read = batadv_socket_read,
289 .write = batadv_socket_write,
290 .poll = batadv_socket_poll,
294 int batadv_socket_setup(struct batadv_priv *bat_priv)
298 if (!bat_priv->debug_dir)
301 d = debugfs_create_file(BATADV_ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
302 bat_priv->debug_dir, bat_priv, &batadv_fops);
313 * batadv_socket_receive_packet - schedule an icmp packet to be sent to userspace
315 * @socket_client: the socket this packet belongs to
316 * @icmph: pointer to the header of the icmp packet
317 * @icmp_len: total length of the icmp packet
319 static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
320 struct batadv_icmp_header *icmph,
323 struct batadv_socket_packet *socket_packet;
326 socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
332 /* check the maximum length before filling the buffer */
333 if (len > sizeof(socket_packet->icmp_packet))
334 len = sizeof(socket_packet->icmp_packet);
336 INIT_LIST_HEAD(&socket_packet->list);
337 memcpy(&socket_packet->icmp_packet, icmph, len);
338 socket_packet->icmp_len = len;
340 spin_lock_bh(&socket_client->lock);
342 /* while waiting for the lock the socket_client could have been
345 if (!batadv_socket_client_hash[icmph->uid]) {
346 spin_unlock_bh(&socket_client->lock);
347 kfree(socket_packet);
351 list_add_tail(&socket_packet->list, &socket_client->queue_list);
352 socket_client->queue_len++;
354 if (socket_client->queue_len > 100) {
355 socket_packet = list_first_entry(&socket_client->queue_list,
356 struct batadv_socket_packet,
359 list_del(&socket_packet->list);
360 kfree(socket_packet);
361 socket_client->queue_len--;
364 spin_unlock_bh(&socket_client->lock);
366 wake_up(&socket_client->queue_wait);
370 * batadv_socket_receive_packet - schedule an icmp packet to be received
371 * locally and sent to userspace.
372 * @icmph: pointer to the header of the icmp packet
373 * @icmp_len: total length of the icmp packet
375 void batadv_socket_receive_packet(struct batadv_icmp_header *icmph,
378 struct batadv_socket_client *hash;
380 hash = batadv_socket_client_hash[icmph->uid];
382 batadv_socket_add_packet(hash, icmph, icmp_len);