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