Sven Eckelmann | 0046b04 | 2016-01-01 00:01:03 +0100 | [diff] [blame] | 1 | /* Copyright (C) 2007-2016 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 |
Antonio Quartulli | ebf38fb | 2013-11-03 20:40:48 +0100 | [diff] [blame] | 15 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 16 | */ |
| 17 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 18 | #include "icmp_socket.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 19 | #include "main.h" |
| 20 | |
| 21 | #include <linux/atomic.h> |
| 22 | #include <linux/compiler.h> |
| 23 | #include <linux/debugfs.h> |
| 24 | #include <linux/errno.h> |
| 25 | #include <linux/etherdevice.h> |
| 26 | #include <linux/export.h> |
| 27 | #include <linux/fcntl.h> |
| 28 | #include <linux/fs.h> |
| 29 | #include <linux/if_ether.h> |
| 30 | #include <linux/kernel.h> |
| 31 | #include <linux/list.h> |
| 32 | #include <linux/module.h> |
| 33 | #include <linux/netdevice.h> |
| 34 | #include <linux/pkt_sched.h> |
| 35 | #include <linux/poll.h> |
| 36 | #include <linux/printk.h> |
| 37 | #include <linux/sched.h> /* for linux/wait.h */ |
| 38 | #include <linux/skbuff.h> |
| 39 | #include <linux/slab.h> |
| 40 | #include <linux/spinlock.h> |
| 41 | #include <linux/stat.h> |
| 42 | #include <linux/stddef.h> |
| 43 | #include <linux/string.h> |
| 44 | #include <linux/uaccess.h> |
| 45 | #include <linux/wait.h> |
| 46 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 47 | #include "hard-interface.h" |
Sven Eckelmann | 1e2c2a4 | 2015-04-17 19:40:28 +0200 | [diff] [blame] | 48 | #include "originator.h" |
| 49 | #include "packet.h" |
| 50 | #include "send.h" |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 51 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 52 | static struct batadv_socket_client *batadv_socket_client_hash[256]; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 53 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 54 | static void batadv_socket_add_packet(struct batadv_socket_client *socket_client, |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 55 | struct batadv_icmp_header *icmph, |
Sven Eckelmann | af4447f | 2012-05-12 18:33:59 +0200 | [diff] [blame] | 56 | size_t icmp_len); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 57 | |
Sven Eckelmann | 9039dc7 | 2012-05-12 02:09:33 +0200 | [diff] [blame] | 58 | void batadv_socket_init(void) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 59 | { |
Sven Eckelmann | af4447f | 2012-05-12 18:33:59 +0200 | [diff] [blame] | 60 | memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash)); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Sven Eckelmann | af4447f | 2012-05-12 18:33:59 +0200 | [diff] [blame] | 63 | static int batadv_socket_open(struct inode *inode, struct file *file) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 64 | { |
| 65 | unsigned int i; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 66 | struct batadv_socket_client *socket_client; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 67 | |
Sven Eckelmann | bd5b80d | 2012-08-20 23:37:26 +0200 | [diff] [blame] | 68 | if (!try_module_get(THIS_MODULE)) |
| 69 | return -EBUSY; |
| 70 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 71 | nonseekable_open(inode, file); |
| 72 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 73 | socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL); |
Sven Eckelmann | bd5b80d | 2012-08-20 23:37:26 +0200 | [diff] [blame] | 74 | if (!socket_client) { |
| 75 | module_put(THIS_MODULE); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 76 | return -ENOMEM; |
Sven Eckelmann | bd5b80d | 2012-08-20 23:37:26 +0200 | [diff] [blame] | 77 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 78 | |
Sven Eckelmann | af4447f | 2012-05-12 18:33:59 +0200 | [diff] [blame] | 79 | for (i = 0; i < ARRAY_SIZE(batadv_socket_client_hash); i++) { |
| 80 | if (!batadv_socket_client_hash[i]) { |
| 81 | batadv_socket_client_hash[i] = socket_client; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 82 | break; |
| 83 | } |
| 84 | } |
| 85 | |
Sven Eckelmann | af4447f | 2012-05-12 18:33:59 +0200 | [diff] [blame] | 86 | if (i == ARRAY_SIZE(batadv_socket_client_hash)) { |
Sven Eckelmann | 86ceb36 | 2012-03-07 09:07:45 +0100 | [diff] [blame] | 87 | 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] | 88 | kfree(socket_client); |
Sven Eckelmann | bd5b80d | 2012-08-20 23:37:26 +0200 | [diff] [blame] | 89 | module_put(THIS_MODULE); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 90 | return -EXFULL; |
| 91 | } |
| 92 | |
| 93 | INIT_LIST_HEAD(&socket_client->queue_list); |
| 94 | socket_client->queue_len = 0; |
| 95 | socket_client->index = i; |
| 96 | socket_client->bat_priv = inode->i_private; |
| 97 | spin_lock_init(&socket_client->lock); |
| 98 | init_waitqueue_head(&socket_client->queue_wait); |
| 99 | |
| 100 | file->private_data = socket_client; |
| 101 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 102 | return 0; |
| 103 | } |
| 104 | |
Sven Eckelmann | af4447f | 2012-05-12 18:33:59 +0200 | [diff] [blame] | 105 | static int batadv_socket_release(struct inode *inode, struct file *file) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 106 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 107 | struct batadv_socket_client *socket_client = file->private_data; |
| 108 | struct batadv_socket_packet *socket_packet; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 109 | struct list_head *list_pos, *list_pos_tmp; |
| 110 | |
| 111 | spin_lock_bh(&socket_client->lock); |
| 112 | |
| 113 | /* for all packets in the queue ... */ |
| 114 | list_for_each_safe(list_pos, list_pos_tmp, &socket_client->queue_list) { |
| 115 | socket_packet = list_entry(list_pos, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 116 | struct batadv_socket_packet, list); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 117 | |
| 118 | list_del(list_pos); |
| 119 | kfree(socket_packet); |
| 120 | } |
| 121 | |
Sven Eckelmann | af4447f | 2012-05-12 18:33:59 +0200 | [diff] [blame] | 122 | batadv_socket_client_hash[socket_client->index] = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 123 | spin_unlock_bh(&socket_client->lock); |
| 124 | |
| 125 | kfree(socket_client); |
Sven Eckelmann | bd5b80d | 2012-08-20 23:37:26 +0200 | [diff] [blame] | 126 | module_put(THIS_MODULE); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 127 | |
| 128 | return 0; |
| 129 | } |
| 130 | |
Sven Eckelmann | af4447f | 2012-05-12 18:33:59 +0200 | [diff] [blame] | 131 | static ssize_t batadv_socket_read(struct file *file, char __user *buf, |
| 132 | size_t count, loff_t *ppos) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 133 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 134 | struct batadv_socket_client *socket_client = file->private_data; |
| 135 | struct batadv_socket_packet *socket_packet; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 136 | size_t packet_len; |
| 137 | int error; |
| 138 | |
| 139 | if ((file->f_flags & O_NONBLOCK) && (socket_client->queue_len == 0)) |
| 140 | return -EAGAIN; |
| 141 | |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 142 | if ((!buf) || (count < sizeof(struct batadv_icmp_packet))) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 143 | return -EINVAL; |
| 144 | |
| 145 | if (!access_ok(VERIFY_WRITE, buf, count)) |
| 146 | return -EFAULT; |
| 147 | |
| 148 | error = wait_event_interruptible(socket_client->queue_wait, |
| 149 | socket_client->queue_len); |
| 150 | |
| 151 | if (error) |
| 152 | return error; |
| 153 | |
| 154 | spin_lock_bh(&socket_client->lock); |
| 155 | |
| 156 | socket_packet = list_first_entry(&socket_client->queue_list, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 157 | struct batadv_socket_packet, list); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 158 | list_del(&socket_packet->list); |
| 159 | socket_client->queue_len--; |
| 160 | |
| 161 | spin_unlock_bh(&socket_client->lock); |
| 162 | |
Sven Eckelmann | b5a1eee | 2011-12-10 15:28:36 +0100 | [diff] [blame] | 163 | packet_len = min(count, socket_packet->icmp_len); |
| 164 | error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 165 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 166 | kfree(socket_packet); |
| 167 | |
| 168 | if (error) |
| 169 | return -EFAULT; |
| 170 | |
| 171 | return packet_len; |
| 172 | } |
| 173 | |
Sven Eckelmann | af4447f | 2012-05-12 18:33:59 +0200 | [diff] [blame] | 174 | static ssize_t batadv_socket_write(struct file *file, const char __user *buff, |
| 175 | size_t len, loff_t *off) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 176 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 177 | struct batadv_socket_client *socket_client = file->private_data; |
| 178 | struct batadv_priv *bat_priv = socket_client->bat_priv; |
| 179 | struct batadv_hard_iface *primary_if = NULL; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 180 | struct sk_buff *skb; |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 181 | struct batadv_icmp_packet_rr *icmp_packet_rr; |
| 182 | struct batadv_icmp_header *icmp_header; |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 183 | struct batadv_orig_node *orig_node = NULL; |
| 184 | struct batadv_neigh_node *neigh_node = NULL; |
Sven Eckelmann | 9641269 | 2012-06-05 22:31:30 +0200 | [diff] [blame] | 185 | size_t packet_len = sizeof(struct batadv_icmp_packet); |
Sven Eckelmann | 6b5e971 | 2015-05-26 18:34:26 +0200 | [diff] [blame] | 186 | u8 *addr; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 187 | |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 188 | if (len < sizeof(struct batadv_icmp_header)) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 189 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 190 | "Error - can't send packet from char device: invalid packet size\n"); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 191 | return -EINVAL; |
| 192 | } |
| 193 | |
Sven Eckelmann | e5d8925 | 2012-05-12 13:48:54 +0200 | [diff] [blame] | 194 | primary_if = batadv_primary_if_get_selected(bat_priv); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 195 | |
| 196 | if (!primary_if) { |
| 197 | len = -EFAULT; |
| 198 | goto out; |
| 199 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 200 | |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 201 | if (len >= BATADV_ICMP_MAX_PACKET_SIZE) |
| 202 | packet_len = BATADV_ICMP_MAX_PACKET_SIZE; |
| 203 | else |
| 204 | packet_len = len; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 205 | |
Antonio Quartulli | 41ab6c4 | 2013-04-02 22:28:44 +0200 | [diff] [blame] | 206 | skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN); |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 207 | if (!skb) { |
| 208 | len = -ENOMEM; |
| 209 | goto out; |
| 210 | } |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 211 | |
Simon Wunderlich | c54f38c9 | 2013-07-29 17:56:44 +0200 | [diff] [blame] | 212 | skb->priority = TC_PRIO_CONTROL; |
Antonio Quartulli | 41ab6c4 | 2013-04-02 22:28:44 +0200 | [diff] [blame] | 213 | skb_reserve(skb, ETH_HLEN); |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 214 | icmp_header = (struct batadv_icmp_header *)skb_put(skb, packet_len); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 215 | |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 216 | if (copy_from_user(icmp_header, buff, packet_len)) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 217 | len = -EFAULT; |
| 218 | goto free_skb; |
| 219 | } |
| 220 | |
Simon Wunderlich | a40d9b0 | 2013-12-02 20:38:31 +0100 | [diff] [blame] | 221 | if (icmp_header->packet_type != BATADV_ICMP) { |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 222 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Sven Eckelmann | 1eda58b | 2012-05-12 13:48:58 +0200 | [diff] [blame] | 223 | "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] | 224 | len = -EINVAL; |
| 225 | goto free_skb; |
| 226 | } |
| 227 | |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 228 | switch (icmp_header->msg_type) { |
| 229 | case BATADV_ECHO_REQUEST: |
| 230 | if (len < sizeof(struct batadv_icmp_packet)) { |
| 231 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
| 232 | "Error - can't send packet from char device: invalid packet size\n"); |
| 233 | len = -EINVAL; |
| 234 | goto free_skb; |
| 235 | } |
| 236 | |
| 237 | if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE) |
| 238 | goto dst_unreach; |
| 239 | |
| 240 | orig_node = batadv_orig_hash_find(bat_priv, icmp_header->dst); |
| 241 | if (!orig_node) |
| 242 | goto dst_unreach; |
| 243 | |
Simon Wunderlich | 7351a482 | 2013-11-13 19:14:47 +0100 | [diff] [blame] | 244 | neigh_node = batadv_orig_router_get(orig_node, |
| 245 | BATADV_IF_DEFAULT); |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 246 | if (!neigh_node) |
| 247 | goto dst_unreach; |
| 248 | |
| 249 | if (!neigh_node->if_incoming) |
| 250 | goto dst_unreach; |
| 251 | |
| 252 | if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE) |
| 253 | goto dst_unreach; |
| 254 | |
| 255 | icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmp_header; |
Antonio Quartulli | 8fdd015 | 2014-01-22 00:42:11 +0100 | [diff] [blame] | 256 | if (packet_len == sizeof(*icmp_packet_rr)) { |
| 257 | addr = neigh_node->if_incoming->net_dev->dev_addr; |
| 258 | ether_addr_copy(icmp_packet_rr->rr[0], addr); |
| 259 | } |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 260 | |
| 261 | break; |
| 262 | default: |
Sven Eckelmann | 39c75a5 | 2012-06-03 22:19:22 +0200 | [diff] [blame] | 263 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 264 | "Error - can't send packet from char device: got unknown message type\n"); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 265 | len = -EINVAL; |
| 266 | goto free_skb; |
| 267 | } |
| 268 | |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 269 | icmp_header->uid = socket_client->index; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 270 | |
Simon Wunderlich | a40d9b0 | 2013-12-02 20:38:31 +0100 | [diff] [blame] | 271 | if (icmp_header->version != BATADV_COMPAT_VERSION) { |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 272 | icmp_header->msg_type = BATADV_PARAMETER_PROBLEM; |
Simon Wunderlich | a40d9b0 | 2013-12-02 20:38:31 +0100 | [diff] [blame] | 273 | icmp_header->version = BATADV_COMPAT_VERSION; |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 274 | batadv_socket_add_packet(socket_client, icmp_header, |
Sven Eckelmann | af4447f | 2012-05-12 18:33:59 +0200 | [diff] [blame] | 275 | packet_len); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 276 | goto free_skb; |
| 277 | } |
| 278 | |
Antonio Quartulli | 8fdd015 | 2014-01-22 00:42:11 +0100 | [diff] [blame] | 279 | ether_addr_copy(icmp_header->orig, primary_if->net_dev->dev_addr); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 280 | |
Sven Eckelmann | 9455e34 | 2012-05-12 02:09:37 +0200 | [diff] [blame] | 281 | batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 282 | goto out; |
| 283 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 284 | dst_unreach: |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 285 | icmp_header->msg_type = BATADV_DESTINATION_UNREACHABLE; |
| 286 | batadv_socket_add_packet(socket_client, icmp_header, packet_len); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 287 | free_skb: |
| 288 | kfree_skb(skb); |
| 289 | out: |
Marek Lindner | 32ae9b2 | 2011-04-20 15:40:58 +0200 | [diff] [blame] | 290 | if (primary_if) |
Sven Eckelmann | 82047ad | 2016-01-17 11:01:10 +0100 | [diff] [blame^] | 291 | batadv_hardif_put(primary_if); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 292 | if (neigh_node) |
Sven Eckelmann | 7d211ef | 2012-05-12 02:09:34 +0200 | [diff] [blame] | 293 | batadv_neigh_node_free_ref(neigh_node); |
Marek Lindner | 44524fc | 2011-02-10 14:33:53 +0000 | [diff] [blame] | 294 | if (orig_node) |
Sven Eckelmann | 5d96731 | 2016-01-17 11:01:09 +0100 | [diff] [blame] | 295 | batadv_orig_node_put(orig_node); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 296 | return len; |
| 297 | } |
| 298 | |
Sven Eckelmann | af4447f | 2012-05-12 18:33:59 +0200 | [diff] [blame] | 299 | static unsigned int batadv_socket_poll(struct file *file, poll_table *wait) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 300 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 301 | struct batadv_socket_client *socket_client = file->private_data; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 302 | |
| 303 | poll_wait(file, &socket_client->queue_wait, wait); |
| 304 | |
| 305 | if (socket_client->queue_len > 0) |
| 306 | return POLLIN | POLLRDNORM; |
| 307 | |
| 308 | return 0; |
| 309 | } |
| 310 | |
Sven Eckelmann | af4447f | 2012-05-12 18:33:59 +0200 | [diff] [blame] | 311 | static const struct file_operations batadv_fops = { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 312 | .owner = THIS_MODULE, |
Sven Eckelmann | af4447f | 2012-05-12 18:33:59 +0200 | [diff] [blame] | 313 | .open = batadv_socket_open, |
| 314 | .release = batadv_socket_release, |
| 315 | .read = batadv_socket_read, |
| 316 | .write = batadv_socket_write, |
| 317 | .poll = batadv_socket_poll, |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 318 | .llseek = no_llseek, |
| 319 | }; |
| 320 | |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 321 | int batadv_socket_setup(struct batadv_priv *bat_priv) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 322 | { |
| 323 | struct dentry *d; |
| 324 | |
| 325 | if (!bat_priv->debug_dir) |
| 326 | goto err; |
| 327 | |
Sven Eckelmann | 6434664 | 2012-06-03 22:19:12 +0200 | [diff] [blame] | 328 | d = debugfs_create_file(BATADV_ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR, |
Sven Eckelmann | af4447f | 2012-05-12 18:33:59 +0200 | [diff] [blame] | 329 | bat_priv->debug_dir, bat_priv, &batadv_fops); |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 330 | if (!d) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 331 | goto err; |
| 332 | |
| 333 | return 0; |
| 334 | |
| 335 | err: |
Sven Eckelmann | 5346c35 | 2012-05-05 13:27:28 +0200 | [diff] [blame] | 336 | return -ENOMEM; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 337 | } |
| 338 | |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 339 | /** |
Sven Eckelmann | 3447382 | 2015-05-31 10:10:20 +0200 | [diff] [blame] | 340 | * batadv_socket_receive_packet - schedule an icmp packet to be sent to |
| 341 | * userspace on an icmp socket. |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 342 | * @socket_client: the socket this packet belongs to |
| 343 | * @icmph: pointer to the header of the icmp packet |
| 344 | * @icmp_len: total length of the icmp packet |
| 345 | */ |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 346 | static void batadv_socket_add_packet(struct batadv_socket_client *socket_client, |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 347 | struct batadv_icmp_header *icmph, |
Sven Eckelmann | af4447f | 2012-05-12 18:33:59 +0200 | [diff] [blame] | 348 | size_t icmp_len) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 349 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 350 | struct batadv_socket_packet *socket_packet; |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 351 | size_t len; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 352 | |
Sven Eckelmann | 704509b | 2011-05-14 23:14:54 +0200 | [diff] [blame] | 353 | socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 354 | |
| 355 | if (!socket_packet) |
| 356 | return; |
| 357 | |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 358 | len = icmp_len; |
| 359 | /* check the maximum length before filling the buffer */ |
| 360 | if (len > sizeof(socket_packet->icmp_packet)) |
| 361 | len = sizeof(socket_packet->icmp_packet); |
| 362 | |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 363 | INIT_LIST_HEAD(&socket_packet->list); |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 364 | memcpy(&socket_packet->icmp_packet, icmph, len); |
| 365 | socket_packet->icmp_len = len; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 366 | |
| 367 | spin_lock_bh(&socket_client->lock); |
| 368 | |
| 369 | /* while waiting for the lock the socket_client could have been |
Sven Eckelmann | 9cfc7bd | 2012-05-12 02:09:43 +0200 | [diff] [blame] | 370 | * deleted |
| 371 | */ |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 372 | if (!batadv_socket_client_hash[icmph->uid]) { |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 373 | spin_unlock_bh(&socket_client->lock); |
| 374 | kfree(socket_packet); |
| 375 | return; |
| 376 | } |
| 377 | |
| 378 | list_add_tail(&socket_packet->list, &socket_client->queue_list); |
| 379 | socket_client->queue_len++; |
| 380 | |
| 381 | if (socket_client->queue_len > 100) { |
| 382 | socket_packet = list_first_entry(&socket_client->queue_list, |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 383 | struct batadv_socket_packet, |
| 384 | list); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 385 | |
| 386 | list_del(&socket_packet->list); |
| 387 | kfree(socket_packet); |
| 388 | socket_client->queue_len--; |
| 389 | } |
| 390 | |
| 391 | spin_unlock_bh(&socket_client->lock); |
| 392 | |
| 393 | wake_up(&socket_client->queue_wait); |
| 394 | } |
| 395 | |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 396 | /** |
| 397 | * batadv_socket_receive_packet - schedule an icmp packet to be received |
| 398 | * locally and sent to userspace. |
| 399 | * @icmph: pointer to the header of the icmp packet |
| 400 | * @icmp_len: total length of the icmp packet |
| 401 | */ |
| 402 | void batadv_socket_receive_packet(struct batadv_icmp_header *icmph, |
Sven Eckelmann | 9039dc7 | 2012-05-12 02:09:33 +0200 | [diff] [blame] | 403 | size_t icmp_len) |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 404 | { |
Sven Eckelmann | 56303d3 | 2012-06-05 22:31:31 +0200 | [diff] [blame] | 405 | struct batadv_socket_client *hash; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 406 | |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 407 | hash = batadv_socket_client_hash[icmph->uid]; |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 408 | if (hash) |
Simon Wunderlich | da6b8c2 | 2013-10-22 22:50:09 +0200 | [diff] [blame] | 409 | batadv_socket_add_packet(hash, icmph, icmp_len); |
Sven Eckelmann | c6c8fea | 2010-12-13 11:19:28 +0000 | [diff] [blame] | 410 | } |