]> rtime.felk.cvut.cz Git - lisovros/linux_canprio.git/blob - drivers/staging/batman-adv/device.c
9887f05925e1ee1113049ae50c65e0927427e9e7
[lisovros/linux_canprio.git] / drivers / staging / batman-adv / device.c
1 /*
2  * Copyright (C) 2007-2010 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
22 #include <linux/device.h>
23 #include <linux/slab.h>
24 #include "main.h"
25 #include "device.h"
26 #include "send.h"
27 #include "types.h"
28 #include "hash.h"
29 #include "hard-interface.h"
30
31 static struct class *batman_class;
32
33 static int Major;       /* Major number assigned to our device driver */
34
35 static 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
43 static struct device_client *device_client_hash[256];
44
45 void bat_device_init(void)
46 {
47         memset(device_client_hash, 0, sizeof(device_client_hash));
48 }
49
50 int bat_device_setup(void)
51 {
52         int tmp_major;
53
54         if (Major)
55                 return 1;
56
57         /* register our device - kernel assigns a free major number */
58         tmp_major = register_chrdev(0, DRIVER_DEVICE, &fops);
59         if (tmp_major < 0) {
60                 printk(KERN_ERR "batman-adv:Registering the character device failed with %d\n",
61                           tmp_major);
62                 return 0;
63         }
64
65         batman_class = class_create(THIS_MODULE, "batman-adv");
66
67         if (IS_ERR(batman_class)) {
68                 printk(KERN_ERR "batman-adv:Could not register class 'batman-adv'\n");
69                 return 0;
70         }
71
72         device_create(batman_class, NULL, MKDEV(tmp_major, 0), NULL,
73                       "batman-adv");
74
75         Major = tmp_major;
76         return 1;
77 }
78
79 void bat_device_destroy(void)
80 {
81         if (!Major)
82                 return;
83
84         device_destroy(batman_class, MKDEV(Major, 0));
85         class_destroy(batman_class);
86
87         /* Unregister the device */
88         unregister_chrdev(Major, DRIVER_DEVICE);
89
90         Major = 0;
91 }
92
93 int bat_device_open(struct inode *inode, struct file *file)
94 {
95         unsigned int i;
96         struct device_client *device_client;
97
98         device_client = kmalloc(sizeof(struct device_client), GFP_KERNEL);
99
100         if (!device_client)
101                 return -ENOMEM;
102
103         for (i = 0; i < ARRAY_SIZE(device_client_hash); i++) {
104                 if (!device_client_hash[i]) {
105                         device_client_hash[i] = device_client;
106                         break;
107                 }
108         }
109
110         if (i == ARRAY_SIZE(device_client_hash)) {
111                 printk(KERN_ERR "batman-adv:Error - can't add another packet client: maximum number of clients reached\n");
112                 kfree(device_client);
113                 return -EXFULL;
114         }
115
116         INIT_LIST_HEAD(&device_client->queue_list);
117         device_client->queue_len = 0;
118         device_client->index = i;
119         spin_lock_init(&device_client->lock);
120         init_waitqueue_head(&device_client->queue_wait);
121
122         file->private_data = device_client;
123
124         inc_module_count();
125         return 0;
126 }
127
128 int bat_device_release(struct inode *inode, struct file *file)
129 {
130         struct device_client *device_client =
131                 (struct device_client *)file->private_data;
132         struct device_packet *device_packet;
133         struct list_head *list_pos, *list_pos_tmp;
134         unsigned long flags;
135
136         spin_lock_irqsave(&device_client->lock, flags);
137
138         /* for all packets in the queue ... */
139         list_for_each_safe(list_pos, list_pos_tmp, &device_client->queue_list) {
140                 device_packet = list_entry(list_pos,
141                                            struct device_packet, list);
142
143                 list_del(list_pos);
144                 kfree(device_packet);
145         }
146
147         device_client_hash[device_client->index] = NULL;
148         spin_unlock_irqrestore(&device_client->lock, flags);
149
150         kfree(device_client);
151         dec_module_count();
152
153         return 0;
154 }
155
156 ssize_t bat_device_read(struct file *file, char __user *buf, size_t count,
157                         loff_t *ppos)
158 {
159         struct device_client *device_client =
160                 (struct device_client *)file->private_data;
161         struct device_packet *device_packet;
162         int error;
163         unsigned long flags;
164
165         if ((file->f_flags & O_NONBLOCK) && (device_client->queue_len == 0))
166                 return -EAGAIN;
167
168         if ((!buf) || (count < sizeof(struct icmp_packet)))
169                 return -EINVAL;
170
171         if (!access_ok(VERIFY_WRITE, buf, count))
172                 return -EFAULT;
173
174         error = wait_event_interruptible(device_client->queue_wait,
175                                          device_client->queue_len);
176
177         if (error)
178                 return error;
179
180         spin_lock_irqsave(&device_client->lock, flags);
181
182         device_packet = list_first_entry(&device_client->queue_list,
183                                          struct device_packet, list);
184         list_del(&device_packet->list);
185         device_client->queue_len--;
186
187         spin_unlock_irqrestore(&device_client->lock, flags);
188
189         error = __copy_to_user(buf, &device_packet->icmp_packet,
190                                sizeof(struct icmp_packet));
191
192         kfree(device_packet);
193
194         if (error)
195                 return error;
196
197         return sizeof(struct icmp_packet);
198 }
199
200 ssize_t bat_device_write(struct file *file, const char __user *buff,
201                          size_t len, loff_t *off)
202 {
203         struct device_client *device_client =
204                 (struct device_client *)file->private_data;
205         struct icmp_packet icmp_packet;
206         struct orig_node *orig_node;
207         struct batman_if *batman_if;
208         uint8_t dstaddr[ETH_ALEN];
209         unsigned long flags;
210
211         if (len < sizeof(struct icmp_packet)) {
212                 bat_dbg(DBG_BATMAN, "batman-adv:Error - can't send packet from char device: invalid packet size\n");
213                 return -EINVAL;
214         }
215
216         if (!access_ok(VERIFY_READ, buff, sizeof(struct icmp_packet)))
217                 return -EFAULT;
218
219         if (__copy_from_user(&icmp_packet, buff, sizeof(icmp_packet)))
220                 return -EFAULT;
221
222         if (icmp_packet.packet_type != BAT_ICMP) {
223                 bat_dbg(DBG_BATMAN, "batman-adv:Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
224                 return -EINVAL;
225         }
226
227         if (icmp_packet.msg_type != ECHO_REQUEST) {
228                 bat_dbg(DBG_BATMAN, "batman-adv:Error - can't send packet from char device: got bogus message type (expected: ECHO_REQUEST)\n");
229                 return -EINVAL;
230         }
231
232         icmp_packet.uid = device_client->index;
233
234         if (icmp_packet.version != COMPAT_VERSION) {
235                 icmp_packet.msg_type = PARAMETER_PROBLEM;
236                 icmp_packet.ttl = COMPAT_VERSION;
237                 bat_device_add_packet(device_client, &icmp_packet);
238                 goto out;
239         }
240
241         if (atomic_read(&module_state) != MODULE_ACTIVE)
242                 goto dst_unreach;
243
244         spin_lock_irqsave(&orig_hash_lock, flags);
245         orig_node = ((struct orig_node *)hash_find(orig_hash, icmp_packet.dst));
246
247         if (!orig_node)
248                 goto unlock;
249
250         if (!orig_node->router)
251                 goto unlock;
252
253         batman_if = orig_node->batman_if;
254         memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
255
256         spin_unlock_irqrestore(&orig_hash_lock, flags);
257
258         if (!batman_if)
259                 goto dst_unreach;
260
261         if (batman_if->if_active != IF_ACTIVE)
262                 goto dst_unreach;
263
264         memcpy(icmp_packet.orig,
265                batman_if->net_dev->dev_addr,
266                ETH_ALEN);
267
268         send_raw_packet((unsigned char *)&icmp_packet,
269                         sizeof(struct icmp_packet),
270                         batman_if, dstaddr);
271
272         goto out;
273
274 unlock:
275         spin_unlock_irqrestore(&orig_hash_lock, flags);
276 dst_unreach:
277         icmp_packet.msg_type = DESTINATION_UNREACHABLE;
278         bat_device_add_packet(device_client, &icmp_packet);
279 out:
280         return len;
281 }
282
283 unsigned int bat_device_poll(struct file *file, poll_table *wait)
284 {
285         struct device_client *device_client =
286                 (struct device_client *)file->private_data;
287
288         poll_wait(file, &device_client->queue_wait, wait);
289
290         if (device_client->queue_len > 0)
291                 return POLLIN | POLLRDNORM;
292
293         return 0;
294 }
295
296 void bat_device_add_packet(struct device_client *device_client,
297                            struct icmp_packet *icmp_packet)
298 {
299         struct device_packet *device_packet;
300         unsigned long flags;
301
302         device_packet = kmalloc(sizeof(struct device_packet), GFP_KERNEL);
303
304         if (!device_packet)
305                 return;
306
307         INIT_LIST_HEAD(&device_packet->list);
308         memcpy(&device_packet->icmp_packet, icmp_packet,
309                sizeof(struct icmp_packet));
310
311         spin_lock_irqsave(&device_client->lock, flags);
312
313         /* while waiting for the lock the device_client could have been
314          * deleted */
315         if (!device_client_hash[icmp_packet->uid]) {
316                 spin_unlock_irqrestore(&device_client->lock, flags);
317                 kfree(device_packet);
318                 return;
319         }
320
321         list_add_tail(&device_packet->list, &device_client->queue_list);
322         device_client->queue_len++;
323
324         if (device_client->queue_len > 100) {
325                 device_packet = list_first_entry(&device_client->queue_list,
326                                                  struct device_packet, list);
327
328                 list_del(&device_packet->list);
329                 kfree(device_packet);
330                 device_client->queue_len--;
331         }
332
333         spin_unlock_irqrestore(&device_client->lock, flags);
334
335         wake_up(&device_client->queue_wait);
336 }
337
338 void bat_device_receive_packet(struct icmp_packet *icmp_packet)
339 {
340         struct device_client *hash = device_client_hash[icmp_packet->uid];
341
342         if (hash)
343                 bat_device_add_packet(hash, icmp_packet);
344 }