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