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