]> git.proxmox.com Git - mirror_ubuntu-jammy-kernel.git/blob - drivers/staging/batman-adv/device.c
Merge branch 'master' of /home/davem/src/GIT/linux-2.6/
[mirror_ubuntu-jammy-kernel.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 <linux/device.h>
23 #include "main.h"
24 #include "device.h"
25 #include "send.h"
26 #include "types.h"
27 #include "hash.h"
28 #include "hard-interface.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 spin_lock_init(&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 uint8_t dstaddr[ETH_ALEN];
211 unsigned long flags;
212
213 if (len < sizeof(struct icmp_packet)) {
214 bat_dbg(DBG_BATMAN, "batman-adv:Error - can't send packet from char device: invalid packet size\n");
215 return -EINVAL;
216 }
217
218 if (!access_ok(VERIFY_READ, buff, sizeof(struct icmp_packet)))
219 return -EFAULT;
220
221 if (__copy_from_user(&icmp_packet, buff, sizeof(icmp_packet)))
222 return -EFAULT;
223
224 if (icmp_packet.packet_type != BAT_ICMP) {
225 bat_dbg(DBG_BATMAN, "batman-adv:Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
226 return -EINVAL;
227 }
228
229 if (icmp_packet.msg_type != ECHO_REQUEST) {
230 bat_dbg(DBG_BATMAN, "batman-adv:Error - can't send packet from char device: got bogus message type (expected: ECHO_REQUEST)\n");
231 return -EINVAL;
232 }
233
234 icmp_packet.uid = device_client->index;
235
236 if (icmp_packet.version != COMPAT_VERSION) {
237 icmp_packet.msg_type = PARAMETER_PROBLEM;
238 icmp_packet.ttl = COMPAT_VERSION;
239 bat_device_add_packet(device_client, &icmp_packet);
240 goto out;
241 }
242
243 if (atomic_read(&module_state) != MODULE_ACTIVE)
244 goto dst_unreach;
245
246 spin_lock_irqsave(&orig_hash_lock, flags);
247 orig_node = ((struct orig_node *)hash_find(orig_hash, icmp_packet.dst));
248
249 if (!orig_node)
250 goto unlock;
251
252 if (!orig_node->router)
253 goto unlock;
254
255 batman_if = orig_node->batman_if;
256 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
257
258 spin_unlock_irqrestore(&orig_hash_lock, flags);
259
260 if (!batman_if)
261 goto dst_unreach;
262
263 if (batman_if->if_active != IF_ACTIVE)
264 goto dst_unreach;
265
266 memcpy(icmp_packet.orig,
267 batman_if->net_dev->dev_addr,
268 ETH_ALEN);
269
270 send_raw_packet((unsigned char *)&icmp_packet,
271 sizeof(struct icmp_packet),
272 batman_if, dstaddr);
273
274 goto out;
275
276 unlock:
277 spin_unlock_irqrestore(&orig_hash_lock, flags);
278 dst_unreach:
279 icmp_packet.msg_type = DESTINATION_UNREACHABLE;
280 bat_device_add_packet(device_client, &icmp_packet);
281 out:
282 return len;
283 }
284
285 unsigned int bat_device_poll(struct file *file, poll_table *wait)
286 {
287 struct device_client *device_client =
288 (struct device_client *)file->private_data;
289
290 poll_wait(file, &device_client->queue_wait, wait);
291
292 if (device_client->queue_len > 0)
293 return POLLIN | POLLRDNORM;
294
295 return 0;
296 }
297
298 void bat_device_add_packet(struct device_client *device_client,
299 struct icmp_packet *icmp_packet)
300 {
301 struct device_packet *device_packet;
302 unsigned long flags;
303
304 device_packet = kmalloc(sizeof(struct device_packet), GFP_KERNEL);
305
306 if (!device_packet)
307 return;
308
309 INIT_LIST_HEAD(&device_packet->list);
310 memcpy(&device_packet->icmp_packet, icmp_packet,
311 sizeof(struct icmp_packet));
312
313 spin_lock_irqsave(&device_client->lock, flags);
314
315 /* while waiting for the lock the device_client could have been
316 * deleted */
317 if (!device_client_hash[icmp_packet->uid]) {
318 spin_unlock_irqrestore(&device_client->lock, flags);
319 kfree(device_packet);
320 return;
321 }
322
323 list_add_tail(&device_packet->list, &device_client->queue_list);
324 device_client->queue_len++;
325
326 if (device_client->queue_len > 100) {
327 device_packet = list_first_entry(&device_client->queue_list,
328 struct device_packet, list);
329
330 list_del(&device_packet->list);
331 kfree(device_packet);
332 device_client->queue_len--;
333 }
334
335 spin_unlock_irqrestore(&device_client->lock, flags);
336
337 wake_up(&device_client->queue_wait);
338 }
339
340 void bat_device_receive_packet(struct icmp_packet *icmp_packet)
341 {
342 struct device_client *hash = device_client_hash[icmp_packet->uid];
343
344 if (hash)
345 bat_device_add_packet(hash, icmp_packet);
346 }