blob: 29ae4efe3543e4bfc6ff013b9c97628b9e395209 [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,
Simon Wunderlichda6b8c22013-10-22 22:50:09 +020032 struct batadv_icmp_header *icmph,
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;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200158 struct batadv_icmp_packet_rr *icmp_packet_rr;
159 struct batadv_icmp_header *icmp_header;
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
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200164 if (len < sizeof(struct batadv_icmp_header)) {
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
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200177 if (len >= BATADV_ICMP_MAX_PACKET_SIZE)
178 packet_len = BATADV_ICMP_MAX_PACKET_SIZE;
179 else
180 packet_len = len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000181
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200182 skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200183 if (!skb) {
184 len = -ENOMEM;
185 goto out;
186 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000187
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200188 skb->priority = TC_PRIO_CONTROL;
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200189 skb_reserve(skb, ETH_HLEN);
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200190 icmp_header = (struct batadv_icmp_header *)skb_put(skb, packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000191
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200192 if (copy_from_user(icmp_header, buff, packet_len)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000193 len = -EFAULT;
194 goto free_skb;
195 }
196
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200197 if (icmp_header->header.packet_type != BATADV_ICMP) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200198 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200199 "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000200 len = -EINVAL;
201 goto free_skb;
202 }
203
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200204 switch (icmp_header->msg_type) {
205 case BATADV_ECHO_REQUEST:
206 if (len < sizeof(struct batadv_icmp_packet)) {
207 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
208 "Error - can't send packet from char device: invalid packet size\n");
209 len = -EINVAL;
210 goto free_skb;
211 }
212
213 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
214 goto dst_unreach;
215
216 orig_node = batadv_orig_hash_find(bat_priv, icmp_header->dst);
217 if (!orig_node)
218 goto dst_unreach;
219
220 neigh_node = batadv_orig_node_get_router(orig_node);
221 if (!neigh_node)
222 goto dst_unreach;
223
224 if (!neigh_node->if_incoming)
225 goto dst_unreach;
226
227 if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
228 goto dst_unreach;
229
230 icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmp_header;
231 if (packet_len == sizeof(*icmp_packet_rr))
232 memcpy(icmp_packet_rr->rr,
233 neigh_node->if_incoming->net_dev->dev_addr,
234 ETH_ALEN);
235
236 break;
237 default:
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200238 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200239 "Error - can't send packet from char device: got unknown message type\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000240 len = -EINVAL;
241 goto free_skb;
242 }
243
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200244 icmp_header->uid = socket_client->index;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000245
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200246 if (icmp_header->header.version != BATADV_COMPAT_VERSION) {
247 icmp_header->msg_type = BATADV_PARAMETER_PROBLEM;
248 icmp_header->header.version = BATADV_COMPAT_VERSION;
249 batadv_socket_add_packet(socket_client, icmp_header,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200250 packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000251 goto free_skb;
252 }
253
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200254 memcpy(icmp_header->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000255
Sven Eckelmann9455e342012-05-12 02:09:37 +0200256 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000257 goto out;
258
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000259dst_unreach:
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200260 icmp_header->msg_type = BATADV_DESTINATION_UNREACHABLE;
261 batadv_socket_add_packet(socket_client, icmp_header, packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000262free_skb:
263 kfree_skb(skb);
264out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200265 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200266 batadv_hardif_free_ref(primary_if);
Marek Lindner44524fc2011-02-10 14:33:53 +0000267 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200268 batadv_neigh_node_free_ref(neigh_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000269 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200270 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000271 return len;
272}
273
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200274static unsigned int batadv_socket_poll(struct file *file, poll_table *wait)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000275{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200276 struct batadv_socket_client *socket_client = file->private_data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000277
278 poll_wait(file, &socket_client->queue_wait, wait);
279
280 if (socket_client->queue_len > 0)
281 return POLLIN | POLLRDNORM;
282
283 return 0;
284}
285
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200286static const struct file_operations batadv_fops = {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000287 .owner = THIS_MODULE,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200288 .open = batadv_socket_open,
289 .release = batadv_socket_release,
290 .read = batadv_socket_read,
291 .write = batadv_socket_write,
292 .poll = batadv_socket_poll,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000293 .llseek = no_llseek,
294};
295
Sven Eckelmann56303d32012-06-05 22:31:31 +0200296int batadv_socket_setup(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000297{
298 struct dentry *d;
299
300 if (!bat_priv->debug_dir)
301 goto err;
302
Sven Eckelmann64346642012-06-03 22:19:12 +0200303 d = debugfs_create_file(BATADV_ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200304 bat_priv->debug_dir, bat_priv, &batadv_fops);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200305 if (!d)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000306 goto err;
307
308 return 0;
309
310err:
Sven Eckelmann5346c352012-05-05 13:27:28 +0200311 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000312}
313
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200314/**
315 * batadv_socket_receive_packet - schedule an icmp packet to be sent to userspace
316 * on an icmp socket.
317 * @socket_client: the socket this packet belongs to
318 * @icmph: pointer to the header of the icmp packet
319 * @icmp_len: total length of the icmp packet
320 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200321static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200322 struct batadv_icmp_header *icmph,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200323 size_t icmp_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000324{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200325 struct batadv_socket_packet *socket_packet;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200326 size_t len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000327
Sven Eckelmann704509b2011-05-14 23:14:54 +0200328 socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000329
330 if (!socket_packet)
331 return;
332
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200333 len = icmp_len;
334 /* check the maximum length before filling the buffer */
335 if (len > sizeof(socket_packet->icmp_packet))
336 len = sizeof(socket_packet->icmp_packet);
337
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000338 INIT_LIST_HEAD(&socket_packet->list);
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200339 memcpy(&socket_packet->icmp_packet, icmph, len);
340 socket_packet->icmp_len = len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000341
342 spin_lock_bh(&socket_client->lock);
343
344 /* while waiting for the lock the socket_client could have been
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200345 * deleted
346 */
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200347 if (!batadv_socket_client_hash[icmph->uid]) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000348 spin_unlock_bh(&socket_client->lock);
349 kfree(socket_packet);
350 return;
351 }
352
353 list_add_tail(&socket_packet->list, &socket_client->queue_list);
354 socket_client->queue_len++;
355
356 if (socket_client->queue_len > 100) {
357 socket_packet = list_first_entry(&socket_client->queue_list,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200358 struct batadv_socket_packet,
359 list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000360
361 list_del(&socket_packet->list);
362 kfree(socket_packet);
363 socket_client->queue_len--;
364 }
365
366 spin_unlock_bh(&socket_client->lock);
367
368 wake_up(&socket_client->queue_wait);
369}
370
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200371/**
372 * batadv_socket_receive_packet - schedule an icmp packet to be received
373 * locally and sent to userspace.
374 * @icmph: pointer to the header of the icmp packet
375 * @icmp_len: total length of the icmp packet
376 */
377void batadv_socket_receive_packet(struct batadv_icmp_header *icmph,
Sven Eckelmann9039dc72012-05-12 02:09:33 +0200378 size_t icmp_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000379{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200380 struct batadv_socket_client *hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000381
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200382 hash = batadv_socket_client_hash[icmph->uid];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000383 if (hash)
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200384 batadv_socket_add_packet(hash, icmph, icmp_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000385}