1/* 2 * Copyright (C) 2007-2010 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 "main.h" 23#include <linux/debugfs.h> 24#include <linux/slab.h> 25#include "icmp_socket.h" 26#include "send.h" 27#include "types.h" 28#include "hash.h" 29#include "hard-interface.h" 30 31 32static struct socket_client *socket_client_hash[256]; 33 34static void bat_socket_add_packet(struct socket_client *socket_client, 35 struct icmp_packet_rr *icmp_packet, 36 size_t icmp_len); 37 38void bat_socket_init(void) 39{ 40 memset(socket_client_hash, 0, sizeof(socket_client_hash)); 41} 42 43static int bat_socket_open(struct inode *inode, struct file *file) 44{ 45 unsigned int i; 46 struct socket_client *socket_client; 47 48 socket_client = kmalloc(sizeof(struct socket_client), GFP_KERNEL); 49 50 if (!socket_client) 51 return -ENOMEM; 52 53 for (i = 0; i < ARRAY_SIZE(socket_client_hash); i++) { 54 if (!socket_client_hash[i]) { 55 socket_client_hash[i] = socket_client; 56 break; 57 } 58 } 59 60 if (i == ARRAY_SIZE(socket_client_hash)) { 61 pr_err("Error - can't add another packet client: " 62 "maximum number of clients reached\n"); 63 kfree(socket_client); 64 return -EXFULL; 65 } 66 67 INIT_LIST_HEAD(&socket_client->queue_list); 68 socket_client->queue_len = 0; 69 socket_client->index = i; 70 socket_client->bat_priv = inode->i_private; 71 spin_lock_init(&socket_client->lock); 72 init_waitqueue_head(&socket_client->queue_wait); 73 74 file->private_data = socket_client; 75 76 inc_module_count(); 77 return 0; 78} 79 80static int bat_socket_release(struct inode *inode, struct file *file) 81{ 82 struct socket_client *socket_client = file->private_data; 83 struct socket_packet *socket_packet; 84 struct list_head *list_pos, *list_pos_tmp; 85 unsigned long flags; 86 87 spin_lock_irqsave(&socket_client->lock, flags); 88 89 /* for all packets in the queue ... */ 90 list_for_each_safe(list_pos, list_pos_tmp, &socket_client->queue_list) { 91 socket_packet = list_entry(list_pos, 92 struct socket_packet, list); 93 94 list_del(list_pos); 95 kfree(socket_packet); 96 } 97 98 socket_client_hash[socket_client->index] = NULL; 99 spin_unlock_irqrestore(&socket_client->lock, flags); 100 101 kfree(socket_client); 102 dec_module_count(); 103 104 return 0; 105} 106 107static ssize_t bat_socket_read(struct file *file, char __user *buf, 108 size_t count, loff_t *ppos) 109{ 110 struct socket_client *socket_client = file->private_data; 111 struct socket_packet *socket_packet; 112 size_t packet_len; 113 int error; 114 unsigned long flags; 115 116 if ((file->f_flags & O_NONBLOCK) && (socket_client->queue_len == 0)) 117 return -EAGAIN; 118 119 if ((!buf) || (count < sizeof(struct icmp_packet))) 120 return -EINVAL; 121 122 if (!access_ok(VERIFY_WRITE, buf, count)) 123 return -EFAULT; 124 125 error = wait_event_interruptible(socket_client->queue_wait, 126 socket_client->queue_len); 127 128 if (error) 129 return error; 130 131 spin_lock_irqsave(&socket_client->lock, flags); 132 133 socket_packet = list_first_entry(&socket_client->queue_list, 134 struct socket_packet, list); 135 list_del(&socket_packet->list); 136 socket_client->queue_len--; 137 138 spin_unlock_irqrestore(&socket_client->lock, flags); 139 140 error = __copy_to_user(buf, &socket_packet->icmp_packet, 141 socket_packet->icmp_len); 142 143 packet_len = socket_packet->icmp_len; 144 kfree(socket_packet); 145 146 if (error) 147 return -EFAULT; 148 149 return packet_len; 150} 151 152static ssize_t bat_socket_write(struct file *file, const char __user *buff, 153 size_t len, loff_t *off) 154{ 155 struct socket_client *socket_client = file->private_data; 156 struct bat_priv *bat_priv = socket_client->bat_priv; 157 struct icmp_packet_rr icmp_packet; 158 struct orig_node *orig_node; 159 struct batman_if *batman_if; 160 size_t packet_len = sizeof(struct icmp_packet); 161 uint8_t dstaddr[ETH_ALEN]; 162 unsigned long flags; 163 164 if (len < sizeof(struct icmp_packet)) { 165 bat_dbg(DBG_BATMAN, bat_priv, 166 "Error - can't send packet from char device: " 167 "invalid packet size\n"); 168 return -EINVAL; 169 } 170 171 if (!bat_priv->primary_if) 172 return -EFAULT; 173 174 if (len >= sizeof(struct icmp_packet_rr)) 175 packet_len = sizeof(struct icmp_packet_rr); 176 177 if (!access_ok(VERIFY_READ, buff, packet_len)) 178 return -EFAULT; 179 180 if (__copy_from_user(&icmp_packet, buff, packet_len)) 181 return -EFAULT; 182 183 if (icmp_packet.packet_type != BAT_ICMP) { 184 bat_dbg(DBG_BATMAN, bat_priv, 185 "Error - can't send packet from char device: " 186 "got bogus packet type (expected: BAT_ICMP)\n"); 187 return -EINVAL; 188 } 189 190 if (icmp_packet.msg_type != ECHO_REQUEST) { 191 bat_dbg(DBG_BATMAN, bat_priv, 192 "Error - can't send packet from char device: " 193 "got bogus message type (expected: ECHO_REQUEST)\n"); 194 return -EINVAL; 195 } 196 197 icmp_packet.uid = socket_client->index; 198 199 if (icmp_packet.version != COMPAT_VERSION) { 200 icmp_packet.msg_type = PARAMETER_PROBLEM; 201 icmp_packet.ttl = COMPAT_VERSION; 202 bat_socket_add_packet(socket_client, &icmp_packet, packet_len); 203 goto out; 204 } 205 206 if (atomic_read(&module_state) != MODULE_ACTIVE) 207 goto dst_unreach; 208 209 spin_lock_irqsave(&orig_hash_lock, flags); 210 orig_node = ((struct orig_node *)hash_find(orig_hash, icmp_packet.dst)); 211 212 if (!orig_node) 213 goto unlock; 214 215 if (!orig_node->router) 216 goto unlock; 217 218 batman_if = orig_node->router->if_incoming; 219 memcpy(dstaddr, orig_node->router->addr, ETH_ALEN); 220 221 spin_unlock_irqrestore(&orig_hash_lock, flags); 222 223 if (!batman_if) 224 goto dst_unreach; 225 226 if (batman_if->if_status != IF_ACTIVE) 227 goto dst_unreach; 228 229 memcpy(icmp_packet.orig, 230 bat_priv->primary_if->net_dev->dev_addr, ETH_ALEN); 231 232 if (packet_len == sizeof(struct icmp_packet_rr)) 233 memcpy(icmp_packet.rr, batman_if->net_dev->dev_addr, ETH_ALEN); 234 235 send_raw_packet((unsigned char *)&icmp_packet, 236 packet_len, batman_if, dstaddr); 237 238 goto out; 239 240unlock: 241 spin_unlock_irqrestore(&orig_hash_lock, flags); 242dst_unreach: 243 icmp_packet.msg_type = DESTINATION_UNREACHABLE; 244 bat_socket_add_packet(socket_client, &icmp_packet, packet_len); 245out: 246 return len; 247} 248 249static unsigned int bat_socket_poll(struct file *file, poll_table *wait) 250{ 251 struct socket_client *socket_client = file->private_data; 252 253 poll_wait(file, &socket_client->queue_wait, wait); 254 255 if (socket_client->queue_len > 0) 256 return POLLIN | POLLRDNORM; 257 258 return 0; 259} 260 261static const struct file_operations fops = { 262 .owner = THIS_MODULE, 263 .open = bat_socket_open, 264 .release = bat_socket_release, 265 .read = bat_socket_read, 266 .write = bat_socket_write, 267 .poll = bat_socket_poll, 268}; 269 270int bat_socket_setup(struct bat_priv *bat_priv) 271{ 272 struct dentry *d; 273 274 if (!bat_priv->debug_dir) 275 goto err; 276 277 d = debugfs_create_file(ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR, 278 bat_priv->debug_dir, bat_priv, &fops); 279 if (d) 280 goto err; 281 282 return 0; 283 284err: 285 return 1; 286} 287 288static void bat_socket_add_packet(struct socket_client *socket_client, 289 struct icmp_packet_rr *icmp_packet, 290 size_t icmp_len) 291{ 292 struct socket_packet *socket_packet; 293 unsigned long flags; 294 295 socket_packet = kmalloc(sizeof(struct socket_packet), GFP_ATOMIC); 296 297 if (!socket_packet) 298 return; 299 300 INIT_LIST_HEAD(&socket_packet->list); 301 memcpy(&socket_packet->icmp_packet, icmp_packet, icmp_len); 302 socket_packet->icmp_len = icmp_len; 303 304 spin_lock_irqsave(&socket_client->lock, flags); 305 306 /* while waiting for the lock the socket_client could have been 307 * deleted */ 308 if (!socket_client_hash[icmp_packet->uid]) { 309 spin_unlock_irqrestore(&socket_client->lock, flags); 310 kfree(socket_packet); 311 return; 312 } 313 314 list_add_tail(&socket_packet->list, &socket_client->queue_list); 315 socket_client->queue_len++; 316 317 if (socket_client->queue_len > 100) { 318 socket_packet = list_first_entry(&socket_client->queue_list, 319 struct socket_packet, list); 320 321 list_del(&socket_packet->list); 322 kfree(socket_packet); 323 socket_client->queue_len--; 324 } 325 326 spin_unlock_irqrestore(&socket_client->lock, flags); 327 328 wake_up(&socket_client->queue_wait); 329} 330 331void bat_socket_receive_packet(struct icmp_packet_rr *icmp_packet, 332 size_t icmp_len) 333{ 334 struct socket_client *hash = socket_client_hash[icmp_packet->uid]; 335 336 if (hash) 337 bat_socket_add_packet(hash, icmp_packet, icmp_len); 338} 339