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