root/trunk/batman-adv-kernelland/device.c @ 1574

Revision 1574, 8.7 kB (checked in by marek, 7 months ago)

batman-adv: record route for ICMP messages

The standard layer 3 ping utility can use the record route (RR) option
of IP to collect route data for sent ping messages (ping -R). This
patch introduces comparable functionality for batman-adv ICMP messages.

The patch adds a second batman ICMP packet format (icmp_packet_rr) such
that up to 17 MAC addresses can be recorded (sufficient for up to 8
hops per direction). When no RR is wanted, the old icmp_packet without
the RR overhead can be sent.

Signed-off-by: Daniel Seither <post@…>
Signed-off-by: Marek Lindner <lindner_marek@…>

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