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>
28 #include "hard-interface.h"
30 static struct class *batman_class
;
32 static int Major
; /* Major number assigned to our device driver */
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
,
42 static struct device_client
*device_client_hash
[256];
44 void bat_device_init(void)
48 for (i
= 0; i
< 256; i
++)
49 device_client_hash
[i
] = NULL
;
52 int bat_device_setup(void)
59 /* register our device - kernel assigns a free major number */
60 tmp_major
= register_chrdev(0, DRIVER_DEVICE
, &fops
);
62 printk(KERN_ERR
"batman-adv:Registering the character device failed with %d\n",
67 batman_class
= class_create(THIS_MODULE
, "batman-adv");
69 if (IS_ERR(batman_class
)) {
70 printk(KERN_ERR
"batman-adv:Could not register class 'batman-adv' \n");
74 device_create(batman_class
, NULL
, MKDEV(tmp_major
, 0), NULL
,
81 void bat_device_destroy(void)
86 device_destroy(batman_class
, MKDEV(Major
, 0));
87 class_destroy(batman_class
);
89 /* Unregister the device */
90 unregister_chrdev(Major
, DRIVER_DEVICE
);
95 int bat_device_open(struct inode
*inode
, struct file
*file
)
98 struct device_client
*device_client
;
100 device_client
= kmalloc(sizeof(struct device_client
), GFP_KERNEL
);
105 for (i
= 0; i
< 256; i
++) {
106 if (!device_client_hash
[i
]) {
107 device_client_hash
[i
] = device_client
;
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
);
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
);
124 file
->private_data
= device_client
;
130 int bat_device_release(struct inode
*inode
, struct file
*file
)
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
;
138 spin_lock_irqsave(&device_client
->lock
, flags
);
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
);
146 kfree(device_packet
);
149 device_client_hash
[device_client
->index
] = NULL
;
150 spin_unlock_irqrestore(&device_client
->lock
, flags
);
152 kfree(device_client
);
158 ssize_t
bat_device_read(struct file
*file
, char __user
*buf
, size_t count
,
161 struct device_client
*device_client
=
162 (struct device_client
*)file
->private_data
;
163 struct device_packet
*device_packet
;
167 if ((file
->f_flags
& O_NONBLOCK
) && (device_client
->queue_len
== 0))
170 if ((!buf
) || (count
< sizeof(struct icmp_packet
)))
173 if (!access_ok(VERIFY_WRITE
, buf
, count
))
176 error
= wait_event_interruptible(device_client
->queue_wait
,
177 device_client
->queue_len
);
182 spin_lock_irqsave(&device_client
->lock
, flags
);
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
--;
189 spin_unlock_irqrestore(&device_client
->lock
, flags
);
191 error
= __copy_to_user(buf
, &device_packet
->icmp_packet
,
192 sizeof(struct icmp_packet
));
194 kfree(device_packet
);
199 return sizeof(struct icmp_packet
);
202 ssize_t
bat_device_write(struct file
*file
, const char __user
*buff
,
203 size_t len
, loff_t
*off
)
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
];
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");
218 if (!access_ok(VERIFY_READ
, buff
, sizeof(struct icmp_packet
)))
221 if (__copy_from_user(&icmp_packet
, buff
, sizeof(icmp_packet
)))
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");
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");
234 icmp_packet
.uid
= device_client
->index
;
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
);
243 if (atomic_read(&module_state
) != MODULE_ACTIVE
)
246 spin_lock_irqsave(&orig_hash_lock
, flags
);
247 orig_node
= ((struct orig_node
*)hash_find(orig_hash
, icmp_packet
.dst
));
252 if (!orig_node
->router
)
255 batman_if
= orig_node
->batman_if
;
256 memcpy(dstaddr
, orig_node
->router
->addr
, ETH_ALEN
);
258 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
263 if (batman_if
->if_active
!= IF_ACTIVE
)
266 memcpy(icmp_packet
.orig
,
267 batman_if
->net_dev
->dev_addr
,
270 send_raw_packet((unsigned char *)&icmp_packet
,
271 sizeof(struct icmp_packet
),
277 spin_unlock_irqrestore(&orig_hash_lock
, flags
);
279 icmp_packet
.msg_type
= DESTINATION_UNREACHABLE
;
280 bat_device_add_packet(device_client
, &icmp_packet
);
285 unsigned int bat_device_poll(struct file
*file
, poll_table
*wait
)
287 struct device_client
*device_client
=
288 (struct device_client
*)file
->private_data
;
290 poll_wait(file
, &device_client
->queue_wait
, wait
);
292 if (device_client
->queue_len
> 0)
293 return POLLIN
| POLLRDNORM
;
298 void bat_device_add_packet(struct device_client
*device_client
,
299 struct icmp_packet
*icmp_packet
)
301 struct device_packet
*device_packet
;
304 device_packet
= kmalloc(sizeof(struct device_packet
), GFP_KERNEL
);
309 INIT_LIST_HEAD(&device_packet
->list
);
310 memcpy(&device_packet
->icmp_packet
, icmp_packet
,
311 sizeof(struct icmp_packet
));
313 spin_lock_irqsave(&device_client
->lock
, flags
);
315 /* while waiting for the lock the device_client could have been
317 if (!device_client_hash
[icmp_packet
->uid
]) {
318 spin_unlock_irqrestore(&device_client
->lock
, flags
);
319 kfree(device_packet
);
323 list_add_tail(&device_packet
->list
, &device_client
->queue_list
);
324 device_client
->queue_len
++;
326 if (device_client
->queue_len
> 100) {
327 device_packet
= list_first_entry(&device_client
->queue_list
,
328 struct device_packet
, list
);
330 list_del(&device_packet
->list
);
331 kfree(device_packet
);
332 device_client
->queue_len
--;
335 spin_unlock_irqrestore(&device_client
->lock
, flags
);
337 wake_up(&device_client
->queue_wait
);
340 void bat_device_receive_packet(struct icmp_packet
*icmp_packet
)
342 struct device_client
*hash
= device_client_hash
[icmp_packet
->uid
];
345 bat_device_add_packet(hash
, icmp_packet
);