2 * Copyright (C) 2007-2009 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)
49 for (i = 0; i < 256; i++)
50 device_client_hash[i] = NULL;
53 int bat_device_setup(void)
60 /* register our device - kernel assigns a free major number */
61 tmp_major = register_chrdev(0, DRIVER_DEVICE, &fops);
63 printk(KERN_ERR "batman-adv:Registering the character device failed with %d\n",
68 batman_class = class_create(THIS_MODULE, "batman-adv");
70 if (IS_ERR(batman_class)) {
71 printk(KERN_ERR "batman-adv:Could not register class 'batman-adv' \n");
75 device_create(batman_class, NULL, MKDEV(tmp_major, 0), NULL,
82 void bat_device_destroy(void)
87 device_destroy(batman_class, MKDEV(Major, 0));
88 class_destroy(batman_class);
90 /* Unregister the device */
91 unregister_chrdev(Major, DRIVER_DEVICE);
96 int bat_device_open(struct inode *inode, struct file *file)
99 struct device_client *device_client;
101 device_client = kmalloc(sizeof(struct device_client), GFP_KERNEL);
106 for (i = 0; i < 256; i++) {
107 if (!device_client_hash[i]) {
108 device_client_hash[i] = device_client;
113 if (device_client_hash[i] != device_client) {
114 printk(KERN_ERR "batman-adv:Error - can't add another packet client: maximum number of clients reached \n");
115 kfree(device_client);
119 INIT_LIST_HEAD(&device_client->queue_list);
120 device_client->queue_len = 0;
121 device_client->index = i;
122 spin_lock_init(&device_client->lock);
123 init_waitqueue_head(&device_client->queue_wait);
125 file->private_data = device_client;
131 int bat_device_release(struct inode *inode, struct file *file)
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;
139 spin_lock_irqsave(&device_client->lock, flags);
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);
147 kfree(device_packet);
150 device_client_hash[device_client->index] = NULL;
151 spin_unlock_irqrestore(&device_client->lock, flags);
153 kfree(device_client);
159 ssize_t bat_device_read(struct file *file, char __user *buf, size_t count,
162 struct device_client *device_client =
163 (struct device_client *)file->private_data;
164 struct device_packet *device_packet;
168 if ((file->f_flags & O_NONBLOCK) && (device_client->queue_len == 0))
171 if ((!buf) || (count < sizeof(struct icmp_packet)))
174 if (!access_ok(VERIFY_WRITE, buf, count))
177 error = wait_event_interruptible(device_client->queue_wait,
178 device_client->queue_len);
183 spin_lock_irqsave(&device_client->lock, flags);
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--;
190 spin_unlock_irqrestore(&device_client->lock, flags);
192 error = __copy_to_user(buf, &device_packet->icmp_packet,
193 sizeof(struct icmp_packet));
195 kfree(device_packet);
200 return sizeof(struct icmp_packet);
203 ssize_t bat_device_write(struct file *file, const char __user *buff,
204 size_t len, loff_t *off)
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;
211 uint8_t dstaddr[ETH_ALEN];
214 if (len < sizeof(struct icmp_packet)) {
215 bat_dbg(DBG_BATMAN, "batman-adv:Error - can't send packet from char device: invalid packet size\n");
219 if (!access_ok(VERIFY_READ, buff, sizeof(struct icmp_packet)))
222 if (__copy_from_user(&icmp_packet, buff, sizeof(icmp_packet)))
225 if (icmp_packet.packet_type != BAT_ICMP) {
226 bat_dbg(DBG_BATMAN, "batman-adv:Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
230 if (icmp_packet.msg_type != ECHO_REQUEST) {
231 bat_dbg(DBG_BATMAN, "batman-adv:Error - can't send packet from char device: got bogus message type (expected: ECHO_REQUEST)\n");
235 icmp_packet.uid = device_client->index;
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);
244 if (atomic_read(&module_state) != MODULE_ACTIVE)
247 spin_lock_irqsave(&orig_hash_lock, flags);
248 orig_node = ((struct orig_node *)hash_find(orig_hash, icmp_packet.dst));
253 if (!orig_node->router)
256 batman_if = orig_node->batman_if;
257 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN);
259 spin_unlock_irqrestore(&orig_hash_lock, flags);
264 if (batman_if->if_active != IF_ACTIVE)
267 memcpy(icmp_packet.orig,
268 batman_if->net_dev->dev_addr,
271 send_raw_packet((unsigned char *)&icmp_packet,
272 sizeof(struct icmp_packet),
278 spin_unlock_irqrestore(&orig_hash_lock, flags);
280 icmp_packet.msg_type = DESTINATION_UNREACHABLE;
281 bat_device_add_packet(device_client, &icmp_packet);
286 unsigned int bat_device_poll(struct file *file, poll_table *wait)
288 struct device_client *device_client =
289 (struct device_client *)file->private_data;
291 poll_wait(file, &device_client->queue_wait, wait);
293 if (device_client->queue_len > 0)
294 return POLLIN | POLLRDNORM;
299 void bat_device_add_packet(struct device_client *device_client,
300 struct icmp_packet *icmp_packet)
302 struct device_packet *device_packet;
305 device_packet = kmalloc(sizeof(struct device_packet), GFP_KERNEL);
310 INIT_LIST_HEAD(&device_packet->list);
311 memcpy(&device_packet->icmp_packet, icmp_packet,
312 sizeof(struct icmp_packet));
314 spin_lock_irqsave(&device_client->lock, flags);
316 /* while waiting for the lock the device_client could have been
318 if (!device_client_hash[icmp_packet->uid]) {
319 spin_unlock_irqrestore(&device_client->lock, flags);
320 kfree(device_packet);
324 list_add_tail(&device_packet->list, &device_client->queue_list);
325 device_client->queue_len++;
327 if (device_client->queue_len > 100) {
328 device_packet = list_first_entry(&device_client->queue_list,
329 struct device_packet, list);
331 list_del(&device_packet->list);
332 kfree(device_packet);
333 device_client->queue_len--;
336 spin_unlock_irqrestore(&device_client->lock, flags);
338 wake_up(&device_client->queue_wait);
341 void bat_device_receive_packet(struct icmp_packet *icmp_packet)
343 struct device_client *hash = device_client_hash[icmp_packet->uid];
346 bat_device_add_packet(hash, icmp_packet);