blob: abb9d6e0388be65a20e0c19d377f7bdda47ea1da [file] [log] [blame]
Simon Wunderliche19f9752014-01-04 18:04:25 +01001/* Copyright (C) 2007-2014 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
Antonio Quartulliebf38fb2013-11-03 20:40:48 +010015 * along with this program; if not, see <http://www.gnu.org/licenses/>.
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000016 */
17
18#include "main.h"
19#include <linux/debugfs.h>
20#include <linux/slab.h>
21#include "icmp_socket.h"
22#include "send.h"
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000023#include "hash.h"
24#include "originator.h"
25#include "hard-interface.h"
26
Sven Eckelmann56303d32012-06-05 22:31:31 +020027static struct batadv_socket_client *batadv_socket_client_hash[256];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000028
Sven Eckelmann56303d32012-06-05 22:31:31 +020029static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
Simon Wunderlichda6b8c22013-10-22 22:50:09 +020030 struct batadv_icmp_header *icmph,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020031 size_t icmp_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000032
Sven Eckelmann9039dc72012-05-12 02:09:33 +020033void batadv_socket_init(void)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000034{
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020035 memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash));
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000036}
37
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020038static int batadv_socket_open(struct inode *inode, struct file *file)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000039{
40 unsigned int i;
Sven Eckelmann56303d32012-06-05 22:31:31 +020041 struct batadv_socket_client *socket_client;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000042
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +020043 if (!try_module_get(THIS_MODULE))
44 return -EBUSY;
45
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000046 nonseekable_open(inode, file);
47
Sven Eckelmann704509b2011-05-14 23:14:54 +020048 socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +020049 if (!socket_client) {
50 module_put(THIS_MODULE);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000051 return -ENOMEM;
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +020052 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000053
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020054 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;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000057 break;
58 }
59 }
60
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020061 if (i == ARRAY_SIZE(batadv_socket_client_hash)) {
Sven Eckelmann86ceb362012-03-07 09:07:45 +010062 pr_err("Error - can't add another packet client: maximum number of clients reached\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000063 kfree(socket_client);
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +020064 module_put(THIS_MODULE);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000065 return -EXFULL;
66 }
67
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);
74
75 file->private_data = socket_client;
76
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000077 return 0;
78}
79
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020080static int batadv_socket_release(struct inode *inode, struct file *file)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000081{
Sven Eckelmann56303d32012-06-05 22:31:31 +020082 struct batadv_socket_client *socket_client = file->private_data;
83 struct batadv_socket_packet *socket_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000084 struct list_head *list_pos, *list_pos_tmp;
85
86 spin_lock_bh(&socket_client->lock);
87
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,
Sven Eckelmann56303d32012-06-05 22:31:31 +020091 struct batadv_socket_packet, list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000092
93 list_del(list_pos);
94 kfree(socket_packet);
95 }
96
Sven Eckelmannaf4447f2012-05-12 18:33:59 +020097 batadv_socket_client_hash[socket_client->index] = NULL;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +000098 spin_unlock_bh(&socket_client->lock);
99
100 kfree(socket_client);
Sven Eckelmannbd5b80d2012-08-20 23:37:26 +0200101 module_put(THIS_MODULE);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000102
103 return 0;
104}
105
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200106static ssize_t batadv_socket_read(struct file *file, char __user *buf,
107 size_t count, loff_t *ppos)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000108{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200109 struct batadv_socket_client *socket_client = file->private_data;
110 struct batadv_socket_packet *socket_packet;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000111 size_t packet_len;
112 int error;
113
114 if ((file->f_flags & O_NONBLOCK) && (socket_client->queue_len == 0))
115 return -EAGAIN;
116
Sven Eckelmann96412692012-06-05 22:31:30 +0200117 if ((!buf) || (count < sizeof(struct batadv_icmp_packet)))
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000118 return -EINVAL;
119
120 if (!access_ok(VERIFY_WRITE, buf, count))
121 return -EFAULT;
122
123 error = wait_event_interruptible(socket_client->queue_wait,
124 socket_client->queue_len);
125
126 if (error)
127 return error;
128
129 spin_lock_bh(&socket_client->lock);
130
131 socket_packet = list_first_entry(&socket_client->queue_list,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200132 struct batadv_socket_packet, list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000133 list_del(&socket_packet->list);
134 socket_client->queue_len--;
135
136 spin_unlock_bh(&socket_client->lock);
137
Sven Eckelmannb5a1eee2011-12-10 15:28:36 +0100138 packet_len = min(count, socket_packet->icmp_len);
139 error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000140
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000141 kfree(socket_packet);
142
143 if (error)
144 return -EFAULT;
145
146 return packet_len;
147}
148
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200149static ssize_t batadv_socket_write(struct file *file, const char __user *buff,
150 size_t len, loff_t *off)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000151{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200152 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;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000155 struct sk_buff *skb;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200156 struct batadv_icmp_packet_rr *icmp_packet_rr;
157 struct batadv_icmp_header *icmp_header;
Sven Eckelmann56303d32012-06-05 22:31:31 +0200158 struct batadv_orig_node *orig_node = NULL;
159 struct batadv_neigh_node *neigh_node = NULL;
Sven Eckelmann96412692012-06-05 22:31:30 +0200160 size_t packet_len = sizeof(struct batadv_icmp_packet);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000161
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200162 if (len < sizeof(struct batadv_icmp_header)) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200163 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200164 "Error - can't send packet from char device: invalid packet size\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000165 return -EINVAL;
166 }
167
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200168 primary_if = batadv_primary_if_get_selected(bat_priv);
Marek Lindner32ae9b22011-04-20 15:40:58 +0200169
170 if (!primary_if) {
171 len = -EFAULT;
172 goto out;
173 }
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000174
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200175 if (len >= BATADV_ICMP_MAX_PACKET_SIZE)
176 packet_len = BATADV_ICMP_MAX_PACKET_SIZE;
177 else
178 packet_len = len;
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
Simon Wunderlichc54f38c2013-07-29 17:56:44 +0200186 skb->priority = TC_PRIO_CONTROL;
Antonio Quartulli41ab6c42013-04-02 22:28:44 +0200187 skb_reserve(skb, ETH_HLEN);
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200188 icmp_header = (struct batadv_icmp_header *)skb_put(skb, packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000189
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200190 if (copy_from_user(icmp_header, buff, packet_len)) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000191 len = -EFAULT;
192 goto free_skb;
193 }
194
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100195 if (icmp_header->packet_type != BATADV_ICMP) {
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200196 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Sven Eckelmann1eda58b2012-05-12 13:48:58 +0200197 "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000198 len = -EINVAL;
199 goto free_skb;
200 }
201
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200202 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");
207 len = -EINVAL;
208 goto free_skb;
209 }
210
211 if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
212 goto dst_unreach;
213
214 orig_node = batadv_orig_hash_find(bat_priv, icmp_header->dst);
215 if (!orig_node)
216 goto dst_unreach;
217
Simon Wunderlich7351a4822013-11-13 19:14:47 +0100218 neigh_node = batadv_orig_router_get(orig_node,
219 BATADV_IF_DEFAULT);
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200220 if (!neigh_node)
221 goto dst_unreach;
222
223 if (!neigh_node->if_incoming)
224 goto dst_unreach;
225
226 if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
227 goto dst_unreach;
228
229 icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmp_header;
230 if (packet_len == sizeof(*icmp_packet_rr))
231 memcpy(icmp_packet_rr->rr,
232 neigh_node->if_incoming->net_dev->dev_addr,
233 ETH_ALEN);
234
235 break;
236 default:
Sven Eckelmann39c75a52012-06-03 22:19:22 +0200237 batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200238 "Error - can't send packet from char device: got unknown message type\n");
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000239 len = -EINVAL;
240 goto free_skb;
241 }
242
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200243 icmp_header->uid = socket_client->index;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000244
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100245 if (icmp_header->version != BATADV_COMPAT_VERSION) {
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200246 icmp_header->msg_type = BATADV_PARAMETER_PROBLEM;
Simon Wunderlicha40d9b02013-12-02 20:38:31 +0100247 icmp_header->version = BATADV_COMPAT_VERSION;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200248 batadv_socket_add_packet(socket_client, icmp_header,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200249 packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000250 goto free_skb;
251 }
252
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200253 memcpy(icmp_header->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000254
Sven Eckelmann9455e342012-05-12 02:09:37 +0200255 batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000256 goto out;
257
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000258dst_unreach:
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200259 icmp_header->msg_type = BATADV_DESTINATION_UNREACHABLE;
260 batadv_socket_add_packet(socket_client, icmp_header, packet_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000261free_skb:
262 kfree_skb(skb);
263out:
Marek Lindner32ae9b22011-04-20 15:40:58 +0200264 if (primary_if)
Sven Eckelmanne5d89252012-05-12 13:48:54 +0200265 batadv_hardif_free_ref(primary_if);
Marek Lindner44524fc2011-02-10 14:33:53 +0000266 if (neigh_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200267 batadv_neigh_node_free_ref(neigh_node);
Marek Lindner44524fc2011-02-10 14:33:53 +0000268 if (orig_node)
Sven Eckelmann7d211ef2012-05-12 02:09:34 +0200269 batadv_orig_node_free_ref(orig_node);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000270 return len;
271}
272
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200273static unsigned int batadv_socket_poll(struct file *file, poll_table *wait)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000274{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200275 struct batadv_socket_client *socket_client = file->private_data;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000276
277 poll_wait(file, &socket_client->queue_wait, wait);
278
279 if (socket_client->queue_len > 0)
280 return POLLIN | POLLRDNORM;
281
282 return 0;
283}
284
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200285static const struct file_operations batadv_fops = {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000286 .owner = THIS_MODULE,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200287 .open = batadv_socket_open,
288 .release = batadv_socket_release,
289 .read = batadv_socket_read,
290 .write = batadv_socket_write,
291 .poll = batadv_socket_poll,
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000292 .llseek = no_llseek,
293};
294
Sven Eckelmann56303d32012-06-05 22:31:31 +0200295int batadv_socket_setup(struct batadv_priv *bat_priv)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000296{
297 struct dentry *d;
298
299 if (!bat_priv->debug_dir)
300 goto err;
301
Sven Eckelmann64346642012-06-03 22:19:12 +0200302 d = debugfs_create_file(BATADV_ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200303 bat_priv->debug_dir, bat_priv, &batadv_fops);
Sven Eckelmann5346c352012-05-05 13:27:28 +0200304 if (!d)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000305 goto err;
306
307 return 0;
308
309err:
Sven Eckelmann5346c352012-05-05 13:27:28 +0200310 return -ENOMEM;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000311}
312
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200313/**
314 * batadv_socket_receive_packet - schedule an icmp packet to be sent to userspace
315 * on an icmp socket.
316 * @socket_client: the socket this packet belongs to
317 * @icmph: pointer to the header of the icmp packet
318 * @icmp_len: total length of the icmp packet
319 */
Sven Eckelmann56303d32012-06-05 22:31:31 +0200320static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200321 struct batadv_icmp_header *icmph,
Sven Eckelmannaf4447f2012-05-12 18:33:59 +0200322 size_t icmp_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000323{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200324 struct batadv_socket_packet *socket_packet;
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200325 size_t len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000326
Sven Eckelmann704509b2011-05-14 23:14:54 +0200327 socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000328
329 if (!socket_packet)
330 return;
331
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200332 len = icmp_len;
333 /* check the maximum length before filling the buffer */
334 if (len > sizeof(socket_packet->icmp_packet))
335 len = sizeof(socket_packet->icmp_packet);
336
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000337 INIT_LIST_HEAD(&socket_packet->list);
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200338 memcpy(&socket_packet->icmp_packet, icmph, len);
339 socket_packet->icmp_len = len;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000340
341 spin_lock_bh(&socket_client->lock);
342
343 /* while waiting for the lock the socket_client could have been
Sven Eckelmann9cfc7bd2012-05-12 02:09:43 +0200344 * deleted
345 */
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200346 if (!batadv_socket_client_hash[icmph->uid]) {
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000347 spin_unlock_bh(&socket_client->lock);
348 kfree(socket_packet);
349 return;
350 }
351
352 list_add_tail(&socket_packet->list, &socket_client->queue_list);
353 socket_client->queue_len++;
354
355 if (socket_client->queue_len > 100) {
356 socket_packet = list_first_entry(&socket_client->queue_list,
Sven Eckelmann56303d32012-06-05 22:31:31 +0200357 struct batadv_socket_packet,
358 list);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000359
360 list_del(&socket_packet->list);
361 kfree(socket_packet);
362 socket_client->queue_len--;
363 }
364
365 spin_unlock_bh(&socket_client->lock);
366
367 wake_up(&socket_client->queue_wait);
368}
369
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200370/**
371 * batadv_socket_receive_packet - schedule an icmp packet to be received
372 * locally and sent to userspace.
373 * @icmph: pointer to the header of the icmp packet
374 * @icmp_len: total length of the icmp packet
375 */
376void batadv_socket_receive_packet(struct batadv_icmp_header *icmph,
Sven Eckelmann9039dc72012-05-12 02:09:33 +0200377 size_t icmp_len)
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000378{
Sven Eckelmann56303d32012-06-05 22:31:31 +0200379 struct batadv_socket_client *hash;
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000380
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200381 hash = batadv_socket_client_hash[icmph->uid];
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000382 if (hash)
Simon Wunderlichda6b8c22013-10-22 22:50:09 +0200383 batadv_socket_add_packet(hash, icmph, icmp_len);
Sven Eckelmannc6c8fea2010-12-13 11:19:28 +0000384}