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