2 * Copyright (C) 2007-2010 B.A.T.M.A.N. contributors:
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.
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.
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
22 #include <linux/device.h>
23 #include <linux/slab.h>
29 #include "hard-interface.h"
31 static struct class *batman_class;
33 static int Major; /* Major number assigned to our device driver */
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,
43 static struct device_client *device_client_hash[256];
45 void bat_device_init(void)
47 memset(device_client_hash, 0, sizeof(device_client_hash));
50 int bat_device_setup(void)
57 /* register our device - kernel assigns a free major number */
58 tmp_major = register_chrdev(0, DRIVER_DEVICE, &fops);
60 printk(KERN_ERR "batman-adv:Registering the character device failed with %d\n",
65 batman_class = class_create(THIS_MODULE, "batman-adv");
67 if (IS_ERR(batman_class)) {
68 printk(KERN_ERR "batman-adv:Could not register class 'batman-adv'\n");
72 device_create(batman_class, NULL, MKDEV(tmp_major, 0), NULL,
79 void bat_device_destroy(void)
84 device_destroy(batman_class, MKDEV(Major, 0));
85 class_destroy(batman_class);
87 /* Unregister the device */
88 unregister_chrdev(Major, DRIVER_DEVICE);
93 int bat_device_open(struct inode *inode, struct file *file)
96 struct device_client *device_client;
98 device_client = kmalloc(sizeof(struct device_client), GFP_KERNEL);
103 for (i = 0; i < ARRAY_SIZE(device_client_hash); i++) {
104 if (!device_client_hash[i]) {
105 device_client_hash[i] = device_client;
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);
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);
122 file->private_data = device_client;
128 int bat_device_release(struct inode *inode, struct file *file)
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;
136 spin_lock_irqsave(&device_client->lock, flags);
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);
144 kfree(device_packet);
147 device_client_hash[device_client->index] = NULL;
148 spin_unlock_irqrestore(&device_client->lock, flags);
150 kfree(device_client);
156 ssize_t bat_device_read(struct file *file, char __user *buf, size_t count,
159 struct device_client *device_client =
160 (struct device_client *)file->private_data;
161 struct device_packet *device_packet;
165 if ((file->f_flags & O_NONBLOCK) && (device_client->queue_len == 0))
168 if ((!buf) || (count < sizeof(struct icmp_packet)))
171 if (!access_ok(VERIFY_WRITE, buf, count))
174 error = wait_event_interruptible(device_client->queue_wait,
175 device_client->queue_len);
180 spin_lock_irqsave(&device_client->lock, flags);
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--;
187 spin_unlock_irqrestore(&device_client->lock, flags);
189 error = __copy_to_user(buf, &device_packet->icmp_packet,
190 sizeof(struct icmp_packet));
192 kfree(device_packet);
197 return sizeof(struct icmp_packet);
200 ssize_t bat_device_write(struct file *file, const char __user *buff,
201 size_t len, loff_t *off)
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];
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");
216 if (!access_ok(VERIFY_READ, buff, sizeof(struct icmp_packet)))
219 if (__copy_from_user(&icmp_packet, buff, sizeof(icmp_packet)))
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");
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");
232 icmp_packet.uid = device_client->index;
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);
241 if (atomic_read(&module_state) != MODULE_ACTIVE)
244 spin_lock_irqsave(&orig_hash_lock, flags);
245 orig_node = ((struct orig_node *)hash_find(orig_hash, icmp_packet.dst));
250 if (!orig_node->router)
253 batman_if = orig_node->batman_if;
254 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
256 spin_unlock_irqrestore(&orig_hash_lock, flags);
261 if (batman_if->if_active != IF_ACTIVE)
264 memcpy(icmp_packet.orig,
265 batman_if->net_dev->dev_addr,
268 send_raw_packet((unsigned char *)&icmp_packet,
269 sizeof(struct icmp_packet),
275 spin_unlock_irqrestore(&orig_hash_lock, flags);
277 icmp_packet.msg_type = DESTINATION_UNREACHABLE;
278 bat_device_add_packet(device_client, &icmp_packet);
283 unsigned int bat_device_poll(struct file *file, poll_table *wait)
285 struct device_client *device_client =
286 (struct device_client *)file->private_data;
288 poll_wait(file, &device_client->queue_wait, wait);
290 if (device_client->queue_len > 0)
291 return POLLIN | POLLRDNORM;
296 void bat_device_add_packet(struct device_client *device_client,
297 struct icmp_packet *icmp_packet)
299 struct device_packet *device_packet;
302 device_packet = kmalloc(sizeof(struct device_packet), GFP_KERNEL);
307 INIT_LIST_HEAD(&device_packet->list);
308 memcpy(&device_packet->icmp_packet, icmp_packet,
309 sizeof(struct icmp_packet));
311 spin_lock_irqsave(&device_client->lock, flags);
313 /* while waiting for the lock the device_client could have been
315 if (!device_client_hash[icmp_packet->uid]) {
316 spin_unlock_irqrestore(&device_client->lock, flags);
317 kfree(device_packet);
321 list_add_tail(&device_packet->list, &device_client->queue_list);
322 device_client->queue_len++;
324 if (device_client->queue_len > 100) {
325 device_packet = list_first_entry(&device_client->queue_list,
326 struct device_packet, list);
328 list_del(&device_packet->list);
329 kfree(device_packet);
330 device_client->queue_len--;
333 spin_unlock_irqrestore(&device_client->lock, flags);
335 wake_up(&device_client->queue_wait);
338 void bat_device_receive_packet(struct icmp_packet *icmp_packet)
340 struct device_client *hash = device_client_hash[icmp_packet->uid];
343 bat_device_add_packet(hash, icmp_packet);