blob: b27508b8085cd84727bf89c434b0b56adcdcd87f [file] [log] [blame]
Antonio Quartulli0b873932013-01-04 03:05:31 +01001/* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors:
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +00002 *
3 * Marek Lindner
4 *
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.
8 *
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.
13 *
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
17 * 02110-1301, USA
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000018 */
19
20#include "main.h"
21#include <linux/debugfs.h>
22#include <linux/slab.h>
23#include "icmp_socket.h"
24#include "send.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000025#include "hash.h"
26#include "originator.h"
27#include "hard-interface.h"
28
Sven Eckelmann56303d32012-06-05 22:31:31 +020029static struct batadv_socket_client *batadv_socket_client_hash[256];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000030
Sven Eckelmann56303d32012-06-05 22:31:31 +020031static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
Sven Eckelmann96412692012-06-05 22:31:30 +020032 struct batadv_icmp_packet_rr *icmp_packet,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020033 size_t icmp_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000034
Sven Eckelmann9039dc72012-05-12 02:09:33 +020035void batadv_socket_init(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000036{
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020037 memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000038}
39
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020040static int batadv_socket_open(struct inode *inode, struct file *file)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000041{
42 unsigned int i;
Sven Eckelmann56303d32012-06-05 22:31:31 +020043 struct batadv_socket_client *socket_client;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000044
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +020045 if (!try_module_get(THIS_MODULE))
46 return -EBUSY;
47
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000048 nonseekable_open(inode, file);
49
Sven Eckelmann704509b2011-05-14 23:14:54 +020050 socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +020051 if (!socket_client) {
52 module_put(THIS_MODULE);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000053 return -ENOMEM;
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +020054 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000055
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020056 for (i = 0; i < ARRAY_SIZE(batadv_socket_client_hash); i++) {
57 if (!batadv_socket_client_hash[i]) {
58 batadv_socket_client_hash[i] = socket_client;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000059 break;
60 }
61 }
62
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020063 if (i == ARRAY_SIZE(batadv_socket_client_hash)) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +010064 pr_err("Error - can't add another packet client: maximum number of clients reached\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000065 kfree(socket_client);
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +020066 module_put(THIS_MODULE);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000067 return -EXFULL;
68 }
69
70 INIT_LIST_HEAD(&socket_client->queue_list);
71 socket_client->queue_len = 0;
72 socket_client->index = i;
73 socket_client->bat_priv = inode->i_private;
74 spin_lock_init(&socket_client->lock);
75 init_waitqueue_head(&socket_client->queue_wait);
76
77 file->private_data = socket_client;
78
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000079 return 0;
80}
81
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020082static int batadv_socket_release(struct inode *inode, struct file *file)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000083{
Sven Eckelmann56303d32012-06-05 22:31:31 +020084 struct batadv_socket_client *socket_client = file->private_data;
85 struct batadv_socket_packet *socket_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000086 struct list_head *list_pos, *list_pos_tmp;
87
88 spin_lock_bh(&socket_client->lock);
89
90 /* for all packets in the queue ... */
91 list_for_each_safe(list_pos, list_pos_tmp, &socket_client->queue_list) {
92 socket_packet = list_entry(list_pos,
Sven Eckelmann56303d32012-06-05 22:31:31 +020093 struct batadv_socket_packet, list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000094
95 list_del(list_pos);
96 kfree(socket_packet);
97 }
98
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020099 batadv_socket_client_hash[socket_client->index] = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000100 spin_unlock_bh(&socket_client->lock);
101
102 kfree(socket_client);
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +0200103 module_put(THIS_MODULE);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000104
105 return 0;
106}
107
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200108static ssize_t batadv_socket_read(struct file *file, char __user *buf,
109 size_t count, loff_t *ppos)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000110{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200111 struct batadv_socket_client *socket_client = file->private_data;
112 struct batadv_socket_packet *socket_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000113 size_t packet_len;
114 int error;
115
116 if ((file->f_flags & O_NONBLOCK) && (socket_client->queue_len == 0))
117 return -EAGAIN;
118
Sven Eckelmann96412692012-06-05 22:31:30 +0200119 if ((!buf) || (count < sizeof(struct batadv_icmp_packet)))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000120 return -EINVAL;
121
122 if (!access_ok(VERIFY_WRITE, buf, count))
123 return -EFAULT;
124
125 error = wait_event_interruptible(socket_client->queue_wait,
126 socket_client->queue_len);
127
128 if (error)
129 return error;
130
131 spin_lock_bh(&socket_client->lock);
132
133 socket_packet = list_first_entry(&socket_client->queue_list,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200134 struct batadv_socket_packet, list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000135 list_del(&socket_packet->list);
136 socket_client->queue_len--;
137
138 spin_unlock_bh(&socket_client->lock);
139
Sven Eckelmannb5a1eee2011-12-10 15:28:36 +0100140 packet_len = min(count, socket_packet->icmp_len);
141 error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000142
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000143 kfree(socket_packet);
144
145 if (error)
146 return -EFAULT;
147
148 return packet_len;
149}
150
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200151static ssize_t batadv_socket_write(struct file *file, const char __user *buff,
152 size_t len, loff_t *off)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000153{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200154 struct batadv_socket_client *socket_client = file->private_data;
155 struct batadv_priv *bat_priv = socket_client->bat_priv;
156 struct batadv_hard_iface *primary_if = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000157 struct sk_buff *skb;
Sven Eckelmann96412692012-06-05 22:31:30 +0200158 struct batadv_icmp_packet_rr *icmp_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000159
Sven Eckelmann56303d32012-06-05 22:31:31 +0200160 struct batadv_orig_node *orig_node = NULL;
161 struct batadv_neigh_node *neigh_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200162 size_t packet_len = sizeof(struct batadv_icmp_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000163
Sven Eckelmann96412692012-06-05 22:31:30 +0200164 if (len < sizeof(struct batadv_icmp_packet)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200165 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200166 "Error - can't send packet from char device: invalid packet size\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000167 return -EINVAL;
168 }
169
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200170 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200171
172 if (!primary_if) {
173 len = -EFAULT;
174 goto out;
175 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000176
Sven Eckelmann96412692012-06-05 22:31:30 +0200177 if (len >= sizeof(struct batadv_icmp_packet_rr))
178 packet_len = sizeof(struct batadv_icmp_packet_rr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000179
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200180 skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200181 if (!skb) {
182 len = -ENOMEM;
183 goto out;
184 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000185
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200186 skb_reserve(skb, ETH_HLEN);
Sven Eckelmann96412692012-06-05 22:31:30 +0200187 icmp_packet = (struct batadv_icmp_packet_rr *)skb_put(skb, packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000188
Sven Eckelmannd18eb452011-12-10 15:28:35 +0100189 if (copy_from_user(icmp_packet, buff, packet_len)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000190 len = -EFAULT;
191 goto free_skb;
192 }
193
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200194 if (icmp_packet->header.packet_type != BATADV_ICMP) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200195 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200196 "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000197 len = -EINVAL;
198 goto free_skb;
199 }
200
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200201 if (icmp_packet->msg_type != BATADV_ECHO_REQUEST) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200202 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200203 "Error - can't send packet from char device: got bogus message type (expected: ECHO_REQUEST)\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000204 len = -EINVAL;
205 goto free_skb;
206 }
207
208 icmp_packet->uid = socket_client->index;
209
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200210 if (icmp_packet->header.version != BATADV_COMPAT_VERSION) {
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200211 icmp_packet->msg_type = BATADV_PARAMETER_PROBLEM;
Sven Eckelmann7e071c72012-06-03 22:19:13 +0200212 icmp_packet->header.version = BATADV_COMPAT_VERSION;
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200213 batadv_socket_add_packet(socket_client, icmp_packet,
214 packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000215 goto free_skb;
216 }
217
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200218 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000219 goto dst_unreach;
220
Sven Eckelmannda641192012-05-12 13:48:56 +0200221 orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->dst);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000222 if (!orig_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000223 goto dst_unreach;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000224
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200225 neigh_node = batadv_orig_node_get_router(orig_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000226 if (!neigh_node)
Linus Lüssinge1a5382f2011-03-14 22:43:37 +0000227 goto dst_unreach;
Marek Lindner44524fc2011-02-10 14:33:53 +0000228
Marek Lindnerd0072602011-01-19 20:01:44 +0000229 if (!neigh_node->if_incoming)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000230 goto dst_unreach;
231
Sven Eckelmanne9a4f292012-06-03 22:19:19 +0200232 if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000233 goto dst_unreach;
234
235 memcpy(icmp_packet->orig,
Marek Lindner32ae9b22011-04-20 15:40:58 +0200236 primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000237
Sven Eckelmann96412692012-06-05 22:31:30 +0200238 if (packet_len == sizeof(struct batadv_icmp_packet_rr))
Marek Lindner44524fc2011-02-10 14:33:53 +0000239 memcpy(icmp_packet->rr,
Marek Lindnerd0072602011-01-19 20:01:44 +0000240 neigh_node->if_incoming->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000241
Sven Eckelmann9455e342012-05-12 02:09:37 +0200242 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000243 goto out;
244
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000245dst_unreach:
Sven Eckelmannacd34af2012-06-03 22:19:21 +0200246 icmp_packet->msg_type = BATADV_DESTINATION_UNREACHABLE;
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200247 batadv_socket_add_packet(socket_client, icmp_packet, packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000248free_skb:
249 kfree_skb(skb);
250out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200251 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200252 batadv_hardif_free_ref(primary_if);
Marek Lindner44524fc2011-02-10 14:33:53 +0000253 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200254 batadv_neigh_node_free_ref(neigh_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000255 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200256 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000257 return len;
258}
259
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200260static unsigned int batadv_socket_poll(struct file *file, poll_table *wait)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000261{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200262 struct batadv_socket_client *socket_client = file->private_data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000263
264 poll_wait(file, &socket_client->queue_wait, wait);
265
266 if (socket_client->queue_len > 0)
267 return POLLIN | POLLRDNORM;
268
269 return 0;
270}
271
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200272static const struct file_operations batadv_fops = {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000273 .owner = THIS_MODULE,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200274 .open = batadv_socket_open,
275 .release = batadv_socket_release,
276 .read = batadv_socket_read,
277 .write = batadv_socket_write,
278 .poll = batadv_socket_poll,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000279 .llseek = no_llseek,
280};
281
Sven Eckelmann56303d32012-06-05 22:31:31 +0200282int batadv_socket_setup(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000283{
284 struct dentry *d;
285
286 if (!bat_priv->debug_dir)
287 goto err;
288
Sven Eckelmann64346642012-06-03 22:19:12 +0200289 d = debugfs_create_file(BATADV_ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200290 bat_priv->debug_dir, bat_priv, &batadv_fops);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200291 if (!d)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000292 goto err;
293
294 return 0;
295
296err:
Sven Eckelmann5346c352012-05-05 13:27:28 +0200297 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000298}
299
Sven Eckelmann56303d32012-06-05 22:31:31 +0200300static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
Sven Eckelmann96412692012-06-05 22:31:30 +0200301 struct batadv_icmp_packet_rr *icmp_packet,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200302 size_t icmp_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000303{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200304 struct batadv_socket_packet *socket_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000305
Sven Eckelmann704509b2011-05-14 23:14:54 +0200306 socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000307
308 if (!socket_packet)
309 return;
310
311 INIT_LIST_HEAD(&socket_packet->list);
312 memcpy(&socket_packet->icmp_packet, icmp_packet, icmp_len);
313 socket_packet->icmp_len = icmp_len;
314
315 spin_lock_bh(&socket_client->lock);
316
317 /* while waiting for the lock the socket_client could have been
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200318 * deleted
319 */
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200320 if (!batadv_socket_client_hash[icmp_packet->uid]) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000321 spin_unlock_bh(&socket_client->lock);
322 kfree(socket_packet);
323 return;
324 }
325
326 list_add_tail(&socket_packet->list, &socket_client->queue_list);
327 socket_client->queue_len++;
328
329 if (socket_client->queue_len > 100) {
330 socket_packet = list_first_entry(&socket_client->queue_list,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200331 struct batadv_socket_packet,
332 list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000333
334 list_del(&socket_packet->list);
335 kfree(socket_packet);
336 socket_client->queue_len--;
337 }
338
339 spin_unlock_bh(&socket_client->lock);
340
341 wake_up(&socket_client->queue_wait);
342}
343
Sven Eckelmann96412692012-06-05 22:31:30 +0200344void batadv_socket_receive_packet(struct batadv_icmp_packet_rr *icmp_packet,
Sven Eckelmann9039dc72012-05-12 02:09:33 +0200345 size_t icmp_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000346{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200347 struct batadv_socket_client *hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000348
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200349 hash = batadv_socket_client_hash[icmp_packet->uid];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000350 if (hash)
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200351 batadv_socket_add_packet(hash, icmp_packet, icmp_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000352}