Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 1 | /* |
Sven Eckelmann | 567db7b | 2012-01-01 00:41:38 +0100 | [diff] [blame] | 2 | * Copyright (C) 2007-2012 B.A.T.M.A.N. contributors: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 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" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 27 | #include "hash.h" |
| 28 | #include "originator.h" |
| 29 | #include "hard-interface.h" |
| 30 | |
| 31 | static struct socket_client *socket_client_hash[256]; |
| 32 | |
| 33 | static void bat_socket_add_packet(struct socket_client *socket_client, |
| 34 | struct icmp_packet_rr *icmp_packet, |
| 35 | size_t icmp_len); |
| 36 | |
| 37 | void bat_socket_init(void) |
| 38 | { |
| 39 | memset(socket_client_hash, 0, sizeof(socket_client_hash)); |
| 40 | } |
| 41 | |
| 42 | static int bat_socket_open(struct inode *inode, struct file *file) |
| 43 | { |
| 44 | unsigned int i; |
| 45 | struct socket_client *socket_client; |
| 46 | |
| 47 | nonseekable_open(inode, file); |
| 48 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 49 | socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 50 | |
| 51 | if (!socket_client) |
| 52 | return -ENOMEM; |
| 53 | |
| 54 | for (i = 0; i < ARRAY_SIZE(socket_client_hash); i++) { |
| 55 | if (!socket_client_hash[i]) { |
| 56 | socket_client_hash[i] = socket_client; |
| 57 | break; |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | if (i == ARRAY_SIZE(socket_client_hash)) { |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 62 | pr_err("Error - can't add another packet client: maximum number of clients reached\n"); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 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 | |
| 80 | static 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 | |
| 86 | spin_lock_bh(&socket_client->lock); |
| 87 | |
| 88 | /* for all packets in the queue ... */ |
| 89 | list_for_each_safe(list_pos, list_pos_tmp, &socket_client->queue_list) { |
| 90 | socket_packet = list_entry(list_pos, |
| 91 | struct socket_packet, list); |
| 92 | |
| 93 | list_del(list_pos); |
| 94 | kfree(socket_packet); |
| 95 | } |
| 96 | |
| 97 | socket_client_hash[socket_client->index] = NULL; |
| 98 | spin_unlock_bh(&socket_client->lock); |
| 99 | |
| 100 | kfree(socket_client); |
| 101 | dec_module_count(); |
| 102 | |
| 103 | return 0; |
| 104 | } |
| 105 | |
| 106 | static ssize_t bat_socket_read(struct file *file, char __user *buf, |
| 107 | size_t count, loff_t *ppos) |
| 108 | { |
| 109 | struct socket_client *socket_client = file->private_data; |
| 110 | struct socket_packet *socket_packet; |
| 111 | size_t packet_len; |
| 112 | int error; |
| 113 | |
| 114 | if ((file->f_flags & O_NONBLOCK) && (socket_client->queue_len == 0)) |
| 115 | return -EAGAIN; |
| 116 | |
| 117 | if ((!buf) || (count < sizeof(struct icmp_packet))) |
| 118 | return -EINVAL; |
| 119 | |
| 120 | if (!access_ok(VERIFY_WRITE, buf, count)) |
| 121 | return -EFAULT; |
| 122 | |
| 123 | error = wait_event_interruptible(socket_client->queue_wait, |
| 124 | socket_client->queue_len); |
| 125 | |
| 126 | if (error) |
| 127 | return error; |
| 128 | |
| 129 | spin_lock_bh(&socket_client->lock); |
| 130 | |
| 131 | socket_packet = list_first_entry(&socket_client->queue_list, |
| 132 | struct socket_packet, list); |
| 133 | list_del(&socket_packet->list); |
| 134 | socket_client->queue_len--; |
| 135 | |
| 136 | spin_unlock_bh(&socket_client->lock); |
| 137 | |
Sven Eckelmann | b5a1eee | 2011-12-10 15:28:36 +0100 | [diff] [blame] | 138 | packet_len = min(count, socket_packet->icmp_len); |
| 139 | error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 140 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 141 | kfree(socket_packet); |
| 142 | |
| 143 | if (error) |
| 144 | return -EFAULT; |
| 145 | |
| 146 | return packet_len; |
| 147 | } |
| 148 | |
| 149 | static ssize_t bat_socket_write(struct file *file, const char __user *buff, |
| 150 | size_t len, loff_t *off) |
| 151 | { |
| 152 | struct socket_client *socket_client = file->private_data; |
| 153 | struct bat_priv *bat_priv = socket_client->bat_priv; |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 154 | struct hard_iface *primary_if = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 155 | struct sk_buff *skb; |
| 156 | struct icmp_packet_rr *icmp_packet; |
| 157 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 158 | struct orig_node *orig_node = NULL; |
| 159 | struct neigh_node *neigh_node = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 160 | size_t packet_len = sizeof(struct icmp_packet); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 161 | |
| 162 | if (len < sizeof(struct icmp_packet)) { |
| 163 | bat_dbg(DBG_BATMAN, bat_priv, |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 164 | "Error - can't send packet from char device: invalid packet size\n"); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 165 | return -EINVAL; |
| 166 | } |
| 167 | |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 168 | primary_if = primary_if_get_selected(bat_priv); |
| 169 | |
| 170 | if (!primary_if) { |
| 171 | len = -EFAULT; |
| 172 | goto out; |
| 173 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 174 | |
| 175 | if (len >= sizeof(struct icmp_packet_rr)) |
| 176 | packet_len = sizeof(struct icmp_packet_rr); |
| 177 | |
Antonio Quartulli | 0d12507 | 2012-02-18 11:27:34 +0100 | [diff] [blame] | 178 | skb = dev_alloc_skb(packet_len + ETH_HLEN); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 179 | if (!skb) { |
| 180 | len = -ENOMEM; |
| 181 | goto out; |
| 182 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 183 | |
Antonio Quartulli | 0d12507 | 2012-02-18 11:27:34 +0100 | [diff] [blame] | 184 | skb_reserve(skb, ETH_HLEN); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 185 | icmp_packet = (struct icmp_packet_rr *)skb_put(skb, packet_len); |
| 186 | |
Sven Eckelmann | d18eb45 | 2011-12-10 15:28:35 +0100 | [diff] [blame] | 187 | if (copy_from_user(icmp_packet, buff, packet_len)) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 188 | len = -EFAULT; |
| 189 | goto free_skb; |
| 190 | } |
| 191 | |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 192 | if (icmp_packet->header.packet_type != BAT_ICMP) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 193 | bat_dbg(DBG_BATMAN, bat_priv, |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 194 | "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n"); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 195 | len = -EINVAL; |
| 196 | goto free_skb; |
| 197 | } |
| 198 | |
| 199 | if (icmp_packet->msg_type != ECHO_REQUEST) { |
| 200 | bat_dbg(DBG_BATMAN, bat_priv, |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 201 | "Error - can't send packet from char device: got bogus message type (expected: ECHO_REQUEST)\n"); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 202 | len = -EINVAL; |
| 203 | goto free_skb; |
| 204 | } |
| 205 | |
| 206 | icmp_packet->uid = socket_client->index; |
| 207 | |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 208 | if (icmp_packet->header.version != COMPAT_VERSION) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 209 | icmp_packet->msg_type = PARAMETER_PROBLEM; |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 210 | icmp_packet->header.version = COMPAT_VERSION; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 211 | bat_socket_add_packet(socket_client, icmp_packet, packet_len); |
| 212 | goto free_skb; |
| 213 | } |
| 214 | |
| 215 | if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE) |
| 216 | goto dst_unreach; |
| 217 | |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 218 | orig_node = orig_hash_find(bat_priv, icmp_packet->dst); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 219 | if (!orig_node) |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 220 | goto dst_unreach; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 221 | |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 222 | neigh_node = orig_node_get_router(orig_node); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 223 | if (!neigh_node) |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 224 | goto dst_unreach; |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 225 | |
Marek Lindner | d007260 | 2011-01-19 20:01:44 +0000 | [diff] [blame] | 226 | if (!neigh_node->if_incoming) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 227 | goto dst_unreach; |
| 228 | |
Marek Lindner | d007260 | 2011-01-19 20:01:44 +0000 | [diff] [blame] | 229 | if (neigh_node->if_incoming->if_status != IF_ACTIVE) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 230 | goto dst_unreach; |
| 231 | |
| 232 | memcpy(icmp_packet->orig, |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 233 | primary_if->net_dev->dev_addr, ETH_ALEN); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 234 | |
| 235 | if (packet_len == sizeof(struct icmp_packet_rr)) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 236 | memcpy(icmp_packet->rr, |
Marek Lindner | d007260 | 2011-01-19 20:01:44 +0000 | [diff] [blame] | 237 | neigh_node->if_incoming->net_dev->dev_addr, ETH_ALEN); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 238 | |
Marek Lindner | d007260 | 2011-01-19 20:01:44 +0000 | [diff] [blame] | 239 | send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 240 | goto out; |
| 241 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 242 | dst_unreach: |
| 243 | icmp_packet->msg_type = DESTINATION_UNREACHABLE; |
| 244 | bat_socket_add_packet(socket_client, icmp_packet, packet_len); |
| 245 | free_skb: |
| 246 | kfree_skb(skb); |
| 247 | out: |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 248 | if (primary_if) |
| 249 | hardif_free_ref(primary_if); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 250 | if (neigh_node) |
| 251 | neigh_node_free_ref(neigh_node); |
| 252 | if (orig_node) |
Marek Lindner | 7b36e8e | 2011-02-18 12:28:10 +0000 | [diff] [blame] | 253 | orig_node_free_ref(orig_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 254 | return len; |
| 255 | } |
| 256 | |
| 257 | static unsigned int bat_socket_poll(struct file *file, poll_table *wait) |
| 258 | { |
| 259 | struct socket_client *socket_client = file->private_data; |
| 260 | |
| 261 | poll_wait(file, &socket_client->queue_wait, wait); |
| 262 | |
| 263 | if (socket_client->queue_len > 0) |
| 264 | return POLLIN | POLLRDNORM; |
| 265 | |
| 266 | return 0; |
| 267 | } |
| 268 | |
| 269 | static const struct file_operations fops = { |
| 270 | .owner = THIS_MODULE, |
| 271 | .open = bat_socket_open, |
| 272 | .release = bat_socket_release, |
| 273 | .read = bat_socket_read, |
| 274 | .write = bat_socket_write, |
| 275 | .poll = bat_socket_poll, |
| 276 | .llseek = no_llseek, |
| 277 | }; |
| 278 | |
| 279 | int bat_socket_setup(struct bat_priv *bat_priv) |
| 280 | { |
| 281 | struct dentry *d; |
| 282 | |
| 283 | if (!bat_priv->debug_dir) |
| 284 | goto err; |
| 285 | |
| 286 | d = debugfs_create_file(ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR, |
| 287 | bat_priv->debug_dir, bat_priv, &fops); |
| 288 | if (d) |
| 289 | goto err; |
| 290 | |
| 291 | return 0; |
| 292 | |
| 293 | err: |
| 294 | return 1; |
| 295 | } |
| 296 | |
| 297 | static void bat_socket_add_packet(struct socket_client *socket_client, |
| 298 | struct icmp_packet_rr *icmp_packet, |
| 299 | size_t icmp_len) |
| 300 | { |
| 301 | struct socket_packet *socket_packet; |
| 302 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 303 | socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 304 | |
| 305 | if (!socket_packet) |
| 306 | return; |
| 307 | |
| 308 | INIT_LIST_HEAD(&socket_packet->list); |
| 309 | memcpy(&socket_packet->icmp_packet, icmp_packet, icmp_len); |
| 310 | socket_packet->icmp_len = icmp_len; |
| 311 | |
| 312 | spin_lock_bh(&socket_client->lock); |
| 313 | |
| 314 | /* while waiting for the lock the socket_client could have been |
| 315 | * deleted */ |
| 316 | if (!socket_client_hash[icmp_packet->uid]) { |
| 317 | spin_unlock_bh(&socket_client->lock); |
| 318 | kfree(socket_packet); |
| 319 | return; |
| 320 | } |
| 321 | |
| 322 | list_add_tail(&socket_packet->list, &socket_client->queue_list); |
| 323 | socket_client->queue_len++; |
| 324 | |
| 325 | if (socket_client->queue_len > 100) { |
| 326 | socket_packet = list_first_entry(&socket_client->queue_list, |
| 327 | struct socket_packet, list); |
| 328 | |
| 329 | list_del(&socket_packet->list); |
| 330 | kfree(socket_packet); |
| 331 | socket_client->queue_len--; |
| 332 | } |
| 333 | |
| 334 | spin_unlock_bh(&socket_client->lock); |
| 335 | |
| 336 | wake_up(&socket_client->queue_wait); |
| 337 | } |
| 338 | |
| 339 | void bat_socket_receive_packet(struct icmp_packet_rr *icmp_packet, |
| 340 | size_t icmp_len) |
| 341 | { |
| 342 | struct socket_client *hash = socket_client_hash[icmp_packet->uid]; |
| 343 | |
| 344 | if (hash) |
| 345 | bat_socket_add_packet(hash, icmp_packet, icmp_len); |
| 346 | } |