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