Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 1 | /* Copyright (C) 2007-2012 B.A.T.M.A.N. contributors: |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 2 | * |
| 3 | * Marek Lindner |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or |
| 6 | * modify it under the terms of version 2 of the GNU General Public |
| 7 | * License as published by the Free Software Foundation. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, but |
| 10 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
| 17 | * 02110-1301, USA |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | #include "main.h" |
| 21 | #include <linux/debugfs.h> |
| 22 | #include <linux/slab.h> |
| 23 | #include "icmp_socket.h" |
| 24 | #include "send.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 25 | #include "hash.h" |
| 26 | #include "originator.h" |
| 27 | #include "hard-interface.h" |
| 28 | |
| 29 | static struct socket_client *socket_client_hash[256]; |
| 30 | |
| 31 | static void bat_socket_add_packet(struct socket_client *socket_client, |
| 32 | struct icmp_packet_rr *icmp_packet, |
| 33 | size_t icmp_len); |
| 34 | |
Sven Eckelmann | 9039dc7 | 2012-05-12 02:09:33 +0200 | [diff] [blame] | 35 | void batadv_socket_init(void) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 36 | { |
| 37 | memset(socket_client_hash, 0, sizeof(socket_client_hash)); |
| 38 | } |
| 39 | |
| 40 | static int bat_socket_open(struct inode *inode, struct file *file) |
| 41 | { |
| 42 | unsigned int i; |
| 43 | struct socket_client *socket_client; |
| 44 | |
| 45 | nonseekable_open(inode, file); |
| 46 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 47 | socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 48 | |
| 49 | if (!socket_client) |
| 50 | return -ENOMEM; |
| 51 | |
| 52 | for (i = 0; i < ARRAY_SIZE(socket_client_hash); i++) { |
| 53 | if (!socket_client_hash[i]) { |
| 54 | socket_client_hash[i] = socket_client; |
| 55 | break; |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | if (i == ARRAY_SIZE(socket_client_hash)) { |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 60 | 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] | 61 | kfree(socket_client); |
| 62 | return -EXFULL; |
| 63 | } |
| 64 | |
| 65 | INIT_LIST_HEAD(&socket_client->queue_list); |
| 66 | socket_client->queue_len = 0; |
| 67 | socket_client->index = i; |
| 68 | socket_client->bat_priv = inode->i_private; |
| 69 | spin_lock_init(&socket_client->lock); |
| 70 | init_waitqueue_head(&socket_client->queue_wait); |
| 71 | |
| 72 | file->private_data = socket_client; |
| 73 | |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 74 | batadv_inc_module_count(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static int bat_socket_release(struct inode *inode, struct file *file) |
| 79 | { |
| 80 | struct socket_client *socket_client = file->private_data; |
| 81 | struct socket_packet *socket_packet; |
| 82 | struct list_head *list_pos, *list_pos_tmp; |
| 83 | |
| 84 | spin_lock_bh(&socket_client->lock); |
| 85 | |
| 86 | /* for all packets in the queue ... */ |
| 87 | list_for_each_safe(list_pos, list_pos_tmp, &socket_client->queue_list) { |
| 88 | socket_packet = list_entry(list_pos, |
| 89 | struct socket_packet, list); |
| 90 | |
| 91 | list_del(list_pos); |
| 92 | kfree(socket_packet); |
| 93 | } |
| 94 | |
| 95 | socket_client_hash[socket_client->index] = NULL; |
| 96 | spin_unlock_bh(&socket_client->lock); |
| 97 | |
| 98 | kfree(socket_client); |
Sven Eckelmann | 3193e8f | 2012-05-12 02:09:42 +0200 | [diff] [blame] | 99 | batadv_dec_module_count(); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | static ssize_t bat_socket_read(struct file *file, char __user *buf, |
| 105 | size_t count, loff_t *ppos) |
| 106 | { |
| 107 | struct socket_client *socket_client = file->private_data; |
| 108 | struct socket_packet *socket_packet; |
| 109 | size_t packet_len; |
| 110 | int error; |
| 111 | |
| 112 | if ((file->f_flags & O_NONBLOCK) && (socket_client->queue_len == 0)) |
| 113 | return -EAGAIN; |
| 114 | |
| 115 | if ((!buf) || (count < sizeof(struct icmp_packet))) |
| 116 | return -EINVAL; |
| 117 | |
| 118 | if (!access_ok(VERIFY_WRITE, buf, count)) |
| 119 | return -EFAULT; |
| 120 | |
| 121 | error = wait_event_interruptible(socket_client->queue_wait, |
| 122 | socket_client->queue_len); |
| 123 | |
| 124 | if (error) |
| 125 | return error; |
| 126 | |
| 127 | spin_lock_bh(&socket_client->lock); |
| 128 | |
| 129 | socket_packet = list_first_entry(&socket_client->queue_list, |
| 130 | struct socket_packet, list); |
| 131 | list_del(&socket_packet->list); |
| 132 | socket_client->queue_len--; |
| 133 | |
| 134 | spin_unlock_bh(&socket_client->lock); |
| 135 | |
Sven Eckelmann | b5a1eee | 2011-12-10 15:28:36 +0100 | [diff] [blame] | 136 | packet_len = min(count, socket_packet->icmp_len); |
| 137 | error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 138 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 139 | kfree(socket_packet); |
| 140 | |
| 141 | if (error) |
| 142 | return -EFAULT; |
| 143 | |
| 144 | return packet_len; |
| 145 | } |
| 146 | |
| 147 | static ssize_t bat_socket_write(struct file *file, const char __user *buff, |
| 148 | size_t len, loff_t *off) |
| 149 | { |
| 150 | struct socket_client *socket_client = file->private_data; |
| 151 | struct bat_priv *bat_priv = socket_client->bat_priv; |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 152 | struct hard_iface *primary_if = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 153 | struct sk_buff *skb; |
| 154 | struct icmp_packet_rr *icmp_packet; |
| 155 | |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 156 | struct orig_node *orig_node = NULL; |
| 157 | struct neigh_node *neigh_node = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 158 | size_t packet_len = sizeof(struct icmp_packet); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 159 | |
| 160 | if (len < sizeof(struct icmp_packet)) { |
| 161 | bat_dbg(DBG_BATMAN, bat_priv, |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 162 | "Error - can't send packet from char device: invalid packet size\n"); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 163 | return -EINVAL; |
| 164 | } |
| 165 | |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 166 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 167 | |
| 168 | if (!primary_if) { |
| 169 | len = -EFAULT; |
| 170 | goto out; |
| 171 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 172 | |
| 173 | if (len >= sizeof(struct icmp_packet_rr)) |
| 174 | packet_len = sizeof(struct icmp_packet_rr); |
| 175 | |
Antonio Quartulli | 0d12507 | 2012-02-18 11:27:34 +0100 | [diff] [blame] | 176 | skb = dev_alloc_skb(packet_len + ETH_HLEN); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 177 | if (!skb) { |
| 178 | len = -ENOMEM; |
| 179 | goto out; |
| 180 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 181 | |
Antonio Quartulli | 0d12507 | 2012-02-18 11:27:34 +0100 | [diff] [blame] | 182 | skb_reserve(skb, ETH_HLEN); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 183 | icmp_packet = (struct icmp_packet_rr *)skb_put(skb, packet_len); |
| 184 | |
Sven Eckelmann | d18eb45 | 2011-12-10 15:28:35 +0100 | [diff] [blame] | 185 | if (copy_from_user(icmp_packet, buff, packet_len)) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 186 | len = -EFAULT; |
| 187 | goto free_skb; |
| 188 | } |
| 189 | |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 190 | if (icmp_packet->header.packet_type != BAT_ICMP) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 191 | bat_dbg(DBG_BATMAN, bat_priv, |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 192 | "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] | 193 | len = -EINVAL; |
| 194 | goto free_skb; |
| 195 | } |
| 196 | |
| 197 | if (icmp_packet->msg_type != ECHO_REQUEST) { |
| 198 | bat_dbg(DBG_BATMAN, bat_priv, |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 199 | "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] | 200 | len = -EINVAL; |
| 201 | goto free_skb; |
| 202 | } |
| 203 | |
| 204 | icmp_packet->uid = socket_client->index; |
| 205 | |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 206 | if (icmp_packet->header.version != COMPAT_VERSION) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 207 | icmp_packet->msg_type = PARAMETER_PROBLEM; |
Sven Eckelmann | 76543d1 | 2011-11-20 15:47:38 +0100 | [diff] [blame] | 208 | icmp_packet->header.version = COMPAT_VERSION; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 209 | bat_socket_add_packet(socket_client, icmp_packet, packet_len); |
| 210 | goto free_skb; |
| 211 | } |
| 212 | |
| 213 | if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE) |
| 214 | goto dst_unreach; |
| 215 | |
Marek Lindner | 7aadf88 | 2011-02-18 12:28:09 +0000 | [diff] [blame] | 216 | orig_node = orig_hash_find(bat_priv, icmp_packet->dst); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 217 | if (!orig_node) |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 218 | goto dst_unreach; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 219 | |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 220 | neigh_node = batadv_orig_node_get_router(orig_node); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 221 | if (!neigh_node) |
Linus Lüssing | e1a5382f | 2011-03-14 22:43:37 +0000 | [diff] [blame] | 222 | goto dst_unreach; |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 223 | |
Marek Lindner | d007260 | 2011-01-19 20:01:44 +0000 | [diff] [blame] | 224 | if (!neigh_node->if_incoming) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 225 | goto dst_unreach; |
| 226 | |
Marek Lindner | d007260 | 2011-01-19 20:01:44 +0000 | [diff] [blame] | 227 | if (neigh_node->if_incoming->if_status != IF_ACTIVE) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 228 | goto dst_unreach; |
| 229 | |
| 230 | memcpy(icmp_packet->orig, |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 231 | primary_if->net_dev->dev_addr, ETH_ALEN); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 232 | |
| 233 | if (packet_len == sizeof(struct icmp_packet_rr)) |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 234 | memcpy(icmp_packet->rr, |
Marek Lindner | d007260 | 2011-01-19 20:01:44 +0000 | [diff] [blame] | 235 | neigh_node->if_incoming->net_dev->dev_addr, ETH_ALEN); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 236 | |
Sven Eckelmann | 9455e34 | 2012-05-12 02:09:37 +0200 | [diff] [blame] | 237 | batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 238 | goto out; |
| 239 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 240 | dst_unreach: |
| 241 | icmp_packet->msg_type = DESTINATION_UNREACHABLE; |
| 242 | bat_socket_add_packet(socket_client, icmp_packet, packet_len); |
| 243 | free_skb: |
| 244 | kfree_skb(skb); |
| 245 | out: |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 246 | if (primary_if) |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 247 | batadv_hardif_free_ref(primary_if); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 248 | if (neigh_node) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 249 | batadv_neigh_node_free_ref(neigh_node); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 250 | if (orig_node) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 251 | batadv_orig_node_free_ref(orig_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 252 | return len; |
| 253 | } |
| 254 | |
| 255 | static unsigned int bat_socket_poll(struct file *file, poll_table *wait) |
| 256 | { |
| 257 | struct socket_client *socket_client = file->private_data; |
| 258 | |
| 259 | poll_wait(file, &socket_client->queue_wait, wait); |
| 260 | |
| 261 | if (socket_client->queue_len > 0) |
| 262 | return POLLIN | POLLRDNORM; |
| 263 | |
| 264 | return 0; |
| 265 | } |
| 266 | |
| 267 | static const struct file_operations fops = { |
| 268 | .owner = THIS_MODULE, |
| 269 | .open = bat_socket_open, |
| 270 | .release = bat_socket_release, |
| 271 | .read = bat_socket_read, |
| 272 | .write = bat_socket_write, |
| 273 | .poll = bat_socket_poll, |
| 274 | .llseek = no_llseek, |
| 275 | }; |
| 276 | |
Sven Eckelmann | 9039dc7 | 2012-05-12 02:09:33 +0200 | [diff] [blame] | 277 | int batadv_socket_setup(struct bat_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 278 | { |
| 279 | struct dentry *d; |
| 280 | |
| 281 | if (!bat_priv->debug_dir) |
| 282 | goto err; |
| 283 | |
| 284 | d = debugfs_create_file(ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR, |
| 285 | bat_priv->debug_dir, bat_priv, &fops); |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 286 | if (!d) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 287 | goto err; |
| 288 | |
| 289 | return 0; |
| 290 | |
| 291 | err: |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 292 | return -ENOMEM; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static void bat_socket_add_packet(struct socket_client *socket_client, |
| 296 | struct icmp_packet_rr *icmp_packet, |
| 297 | size_t icmp_len) |
| 298 | { |
| 299 | struct socket_packet *socket_packet; |
| 300 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 301 | socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 302 | |
| 303 | if (!socket_packet) |
| 304 | return; |
| 305 | |
| 306 | INIT_LIST_HEAD(&socket_packet->list); |
| 307 | memcpy(&socket_packet->icmp_packet, icmp_packet, icmp_len); |
| 308 | socket_packet->icmp_len = icmp_len; |
| 309 | |
| 310 | spin_lock_bh(&socket_client->lock); |
| 311 | |
| 312 | /* while waiting for the lock the socket_client could have been |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 313 | * deleted |
| 314 | */ |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 315 | if (!socket_client_hash[icmp_packet->uid]) { |
| 316 | spin_unlock_bh(&socket_client->lock); |
| 317 | kfree(socket_packet); |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | list_add_tail(&socket_packet->list, &socket_client->queue_list); |
| 322 | socket_client->queue_len++; |
| 323 | |
| 324 | if (socket_client->queue_len > 100) { |
| 325 | socket_packet = list_first_entry(&socket_client->queue_list, |
| 326 | struct socket_packet, list); |
| 327 | |
| 328 | list_del(&socket_packet->list); |
| 329 | kfree(socket_packet); |
| 330 | socket_client->queue_len--; |
| 331 | } |
| 332 | |
| 333 | spin_unlock_bh(&socket_client->lock); |
| 334 | |
| 335 | wake_up(&socket_client->queue_wait); |
| 336 | } |
| 337 | |
Sven Eckelmann | 9039dc7 | 2012-05-12 02:09:33 +0200 | [diff] [blame] | 338 | void batadv_socket_receive_packet(struct icmp_packet_rr *icmp_packet, |
| 339 | size_t icmp_len) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 340 | { |
| 341 | struct socket_client *hash = socket_client_hash[icmp_packet->uid]; |
| 342 | |
| 343 | if (hash) |
| 344 | bat_socket_add_packet(hash, icmp_packet, icmp_len); |
| 345 | } |