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