blob: 2f61500186f275b034e99e4e84edb31e75e4455f [file] [log] [blame]
Andrew Lunn5beef3c2009-11-09 21:20:10 +01001/*
2 * Copyright (C) 2007-2009 B.A.T.M.A.N. contributors:
3 *
4 * Marek Lindner
5 *
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.
9 *
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.
14 *
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
18 * 02110-1301, USA
19 *
20 */
21
Andrew Lunnb9b27e42010-01-02 11:30:50 +010022#include <linux/device.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090023#include <linux/slab.h>
Andrew Lunn5beef3c2009-11-09 21:20:10 +010024#include "main.h"
25#include "device.h"
Andrew Lunn5beef3c2009-11-09 21:20:10 +010026#include "send.h"
27#include "types.h"
28#include "hash.h"
Simon Wunderlicheb500812010-02-19 16:18:05 +010029#include "hard-interface.h"
Andrew Lunn5beef3c2009-11-09 21:20:10 +010030
Andrew Lunn5beef3c2009-11-09 21:20:10 +010031static struct class *batman_class;
32
33static int Major; /* Major number assigned to our device driver */
34
35static const struct file_operations fops = {
36 .open = bat_device_open,
37 .release = bat_device_release,
38 .read = bat_device_read,
39 .write = bat_device_write,
40 .poll = bat_device_poll,
41};
42
43static struct device_client *device_client_hash[256];
44
45void bat_device_init(void)
46{
47 int i;
48
49 for (i = 0; i < 256; i++)
50 device_client_hash[i] = NULL;
51}
52
53int bat_device_setup(void)
54{
55 int tmp_major;
56
57 if (Major)
58 return 1;
59
60 /* register our device - kernel assigns a free major number */
61 tmp_major = register_chrdev(0, DRIVER_DEVICE, &fops);
62 if (tmp_major < 0) {
Andrew Lunnbad22392009-12-12 23:39:41 +010063 printk(KERN_ERR "batman-adv:Registering the character device failed with %d\n",
Andrew Lunn5beef3c2009-11-09 21:20:10 +010064 tmp_major);
65 return 0;
66 }
67
68 batman_class = class_create(THIS_MODULE, "batman-adv");
69
70 if (IS_ERR(batman_class)) {
Andrew Lunnbad22392009-12-12 23:39:41 +010071 printk(KERN_ERR "batman-adv:Could not register class 'batman-adv' \n");
Andrew Lunn5beef3c2009-11-09 21:20:10 +010072 return 0;
73 }
74
75 device_create(batman_class, NULL, MKDEV(tmp_major, 0), NULL,
76 "batman-adv");
77
78 Major = tmp_major;
79 return 1;
80}
81
82void bat_device_destroy(void)
83{
84 if (!Major)
85 return;
86
87 device_destroy(batman_class, MKDEV(Major, 0));
88 class_destroy(batman_class);
89
90 /* Unregister the device */
91 unregister_chrdev(Major, DRIVER_DEVICE);
92
93 Major = 0;
94}
95
96int bat_device_open(struct inode *inode, struct file *file)
97{
98 unsigned int i;
99 struct device_client *device_client;
100
101 device_client = kmalloc(sizeof(struct device_client), GFP_KERNEL);
102
103 if (!device_client)
104 return -ENOMEM;
105
106 for (i = 0; i < 256; i++) {
107 if (!device_client_hash[i]) {
108 device_client_hash[i] = device_client;
109 break;
110 }
111 }
112
113 if (device_client_hash[i] != device_client) {
Andrew Lunnbad22392009-12-12 23:39:41 +0100114 printk(KERN_ERR "batman-adv:Error - can't add another packet client: maximum number of clients reached \n");
Andrew Lunn5beef3c2009-11-09 21:20:10 +0100115 kfree(device_client);
116 return -EXFULL;
117 }
118
119 INIT_LIST_HEAD(&device_client->queue_list);
120 device_client->queue_len = 0;
121 device_client->index = i;
Sven-Thorsten Dietrichc852ab62010-02-23 13:59:18 -0800122 spin_lock_init(&device_client->lock);
Andrew Lunn5beef3c2009-11-09 21:20:10 +0100123 init_waitqueue_head(&device_client->queue_wait);
124
125 file->private_data = device_client;
126
127 inc_module_count();
128 return 0;
129}
130
131int bat_device_release(struct inode *inode, struct file *file)
132{
133 struct device_client *device_client =
134 (struct device_client *)file->private_data;
135 struct device_packet *device_packet;
136 struct list_head *list_pos, *list_pos_tmp;
Simon Wunderliche7017192010-01-02 11:30:48 +0100137 unsigned long flags;
Andrew Lunn5beef3c2009-11-09 21:20:10 +0100138
Simon Wunderliche7017192010-01-02 11:30:48 +0100139 spin_lock_irqsave(&device_client->lock, flags);
Andrew Lunn5beef3c2009-11-09 21:20:10 +0100140
141 /* for all packets in the queue ... */
142 list_for_each_safe(list_pos, list_pos_tmp, &device_client->queue_list) {
143 device_packet = list_entry(list_pos,
144 struct device_packet, list);
145
146 list_del(list_pos);
147 kfree(device_packet);
148 }
149
150 device_client_hash[device_client->index] = NULL;
Simon Wunderliche7017192010-01-02 11:30:48 +0100151 spin_unlock_irqrestore(&device_client->lock, flags);
Andrew Lunn5beef3c2009-11-09 21:20:10 +0100152
153 kfree(device_client);
154 dec_module_count();
155
156 return 0;
157}
158
159ssize_t bat_device_read(struct file *file, char __user *buf, size_t count,
160 loff_t *ppos)
161{
162 struct device_client *device_client =
163 (struct device_client *)file->private_data;
164 struct device_packet *device_packet;
165 int error;
Simon Wunderliche7017192010-01-02 11:30:48 +0100166 unsigned long flags;
Andrew Lunn5beef3c2009-11-09 21:20:10 +0100167
168 if ((file->f_flags & O_NONBLOCK) && (device_client->queue_len == 0))
169 return -EAGAIN;
170
171 if ((!buf) || (count < sizeof(struct icmp_packet)))
172 return -EINVAL;
173
174 if (!access_ok(VERIFY_WRITE, buf, count))
175 return -EFAULT;
176
177 error = wait_event_interruptible(device_client->queue_wait,
178 device_client->queue_len);
179
180 if (error)
181 return error;
182
Simon Wunderliche7017192010-01-02 11:30:48 +0100183 spin_lock_irqsave(&device_client->lock, flags);
Andrew Lunn5beef3c2009-11-09 21:20:10 +0100184
185 device_packet = list_first_entry(&device_client->queue_list,
186 struct device_packet, list);
187 list_del(&device_packet->list);
188 device_client->queue_len--;
189
Simon Wunderliche7017192010-01-02 11:30:48 +0100190 spin_unlock_irqrestore(&device_client->lock, flags);
Andrew Lunn5beef3c2009-11-09 21:20:10 +0100191
192 error = __copy_to_user(buf, &device_packet->icmp_packet,
193 sizeof(struct icmp_packet));
194
195 kfree(device_packet);
196
197 if (error)
198 return error;
199
200 return sizeof(struct icmp_packet);
201}
202
203ssize_t bat_device_write(struct file *file, const char __user *buff,
204 size_t len, loff_t *off)
205{
206 struct device_client *device_client =
207 (struct device_client *)file->private_data;
208 struct icmp_packet icmp_packet;
209 struct orig_node *orig_node;
210 struct batman_if *batman_if;
Simon Wunderlicheb500812010-02-19 16:18:05 +0100211 uint8_t dstaddr[ETH_ALEN];
Simon Wunderliche7017192010-01-02 11:30:48 +0100212 unsigned long flags;
Andrew Lunn5beef3c2009-11-09 21:20:10 +0100213
214 if (len < sizeof(struct icmp_packet)) {
Andrew Lunn4ce21a72010-01-02 11:30:38 +0100215 bat_dbg(DBG_BATMAN, "batman-adv:Error - can't send packet from char device: invalid packet size\n");
Andrew Lunn5beef3c2009-11-09 21:20:10 +0100216 return -EINVAL;
217 }
218
219 if (!access_ok(VERIFY_READ, buff, sizeof(struct icmp_packet)))
220 return -EFAULT;
221
222 if (__copy_from_user(&icmp_packet, buff, sizeof(icmp_packet)))
223 return -EFAULT;
224
225 if (icmp_packet.packet_type != BAT_ICMP) {
Andrew Lunn4ce21a72010-01-02 11:30:38 +0100226 bat_dbg(DBG_BATMAN, "batman-adv:Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
Andrew Lunn5beef3c2009-11-09 21:20:10 +0100227 return -EINVAL;
228 }
229
230 if (icmp_packet.msg_type != ECHO_REQUEST) {
Andrew Lunn4ce21a72010-01-02 11:30:38 +0100231 bat_dbg(DBG_BATMAN, "batman-adv:Error - can't send packet from char device: got bogus message type (expected: ECHO_REQUEST)\n");
Andrew Lunn5beef3c2009-11-09 21:20:10 +0100232 return -EINVAL;
233 }
234
235 icmp_packet.uid = device_client->index;
236
237 if (icmp_packet.version != COMPAT_VERSION) {
238 icmp_packet.msg_type = PARAMETER_PROBLEM;
239 icmp_packet.ttl = COMPAT_VERSION;
240 bat_device_add_packet(device_client, &icmp_packet);
241 goto out;
242 }
243
244 if (atomic_read(&module_state) != MODULE_ACTIVE)
245 goto dst_unreach;
246
Simon Wunderliche7017192010-01-02 11:30:48 +0100247 spin_lock_irqsave(&orig_hash_lock, flags);
Andrew Lunn5beef3c2009-11-09 21:20:10 +0100248 orig_node = ((struct orig_node *)hash_find(orig_hash, icmp_packet.dst));
249
250 if (!orig_node)
251 goto unlock;
252
253 if (!orig_node->router)
254 goto unlock;
255
256 batman_if = orig_node->batman_if;
Simon Wunderlicheb500812010-02-19 16:18:05 +0100257 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
258
259 spin_unlock_irqrestore(&orig_hash_lock, flags);
Andrew Lunn5beef3c2009-11-09 21:20:10 +0100260
261 if (!batman_if)
Simon Wunderlicheb500812010-02-19 16:18:05 +0100262 goto dst_unreach;
263
264 if (batman_if->if_active != IF_ACTIVE)
265 goto dst_unreach;
Andrew Lunn5beef3c2009-11-09 21:20:10 +0100266
267 memcpy(icmp_packet.orig,
268 batman_if->net_dev->dev_addr,
269 ETH_ALEN);
270
271 send_raw_packet((unsigned char *)&icmp_packet,
272 sizeof(struct icmp_packet),
Simon Wunderlicheb500812010-02-19 16:18:05 +0100273 batman_if, dstaddr);
Andrew Lunn5beef3c2009-11-09 21:20:10 +0100274
Andrew Lunn5beef3c2009-11-09 21:20:10 +0100275 goto out;
276
277unlock:
Simon Wunderliche7017192010-01-02 11:30:48 +0100278 spin_unlock_irqrestore(&orig_hash_lock, flags);
Andrew Lunn5beef3c2009-11-09 21:20:10 +0100279dst_unreach:
280 icmp_packet.msg_type = DESTINATION_UNREACHABLE;
281 bat_device_add_packet(device_client, &icmp_packet);
282out:
283 return len;
284}
285
286unsigned int bat_device_poll(struct file *file, poll_table *wait)
287{
288 struct device_client *device_client =
289 (struct device_client *)file->private_data;
290
291 poll_wait(file, &device_client->queue_wait, wait);
292
293 if (device_client->queue_len > 0)
294 return POLLIN | POLLRDNORM;
295
296 return 0;
297}
298
299void bat_device_add_packet(struct device_client *device_client,
300 struct icmp_packet *icmp_packet)
301{
302 struct device_packet *device_packet;
Simon Wunderliche7017192010-01-02 11:30:48 +0100303 unsigned long flags;
Andrew Lunn5beef3c2009-11-09 21:20:10 +0100304
305 device_packet = kmalloc(sizeof(struct device_packet), GFP_KERNEL);
306
307 if (!device_packet)
308 return;
309
310 INIT_LIST_HEAD(&device_packet->list);
311 memcpy(&device_packet->icmp_packet, icmp_packet,
312 sizeof(struct icmp_packet));
313
Simon Wunderliche7017192010-01-02 11:30:48 +0100314 spin_lock_irqsave(&device_client->lock, flags);
Andrew Lunn5beef3c2009-11-09 21:20:10 +0100315
316 /* while waiting for the lock the device_client could have been
317 * deleted */
318 if (!device_client_hash[icmp_packet->uid]) {
Simon Wunderliche7017192010-01-02 11:30:48 +0100319 spin_unlock_irqrestore(&device_client->lock, flags);
Andrew Lunn5beef3c2009-11-09 21:20:10 +0100320 kfree(device_packet);
321 return;
322 }
323
324 list_add_tail(&device_packet->list, &device_client->queue_list);
325 device_client->queue_len++;
326
327 if (device_client->queue_len > 100) {
328 device_packet = list_first_entry(&device_client->queue_list,
329 struct device_packet, list);
330
331 list_del(&device_packet->list);
332 kfree(device_packet);
333 device_client->queue_len--;
334 }
335
Simon Wunderliche7017192010-01-02 11:30:48 +0100336 spin_unlock_irqrestore(&device_client->lock, flags);
Andrew Lunn5beef3c2009-11-09 21:20:10 +0100337
338 wake_up(&device_client->queue_wait);
339}
340
341void bat_device_receive_packet(struct icmp_packet *icmp_packet)
342{
343 struct device_client *hash = device_client_hash[icmp_packet->uid];
344
345 if (hash)
346 bat_device_add_packet(hash, icmp_packet);
347}