]>
Commit | Line | Data |
---|---|---|
7db7d9f3 | 1 | // SPDX-License-Identifier: GPL-2.0 |
6b1aea8c | 2 | /* Copyright (C) 2007-2018 B.A.T.M.A.N. contributors: |
c6c8fea2 SE |
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 | |
ebf38fb7 | 16 | * along with this program; if not, see <http://www.gnu.org/licenses/>. |
c6c8fea2 SE |
17 | */ |
18 | ||
1e2c2a4f | 19 | #include "icmp_socket.h" |
c6c8fea2 | 20 | #include "main.h" |
1e2c2a4f SE |
21 | |
22 | #include <linux/atomic.h> | |
23 | #include <linux/compiler.h> | |
c6c8fea2 | 24 | #include <linux/debugfs.h> |
1e2c2a4f SE |
25 | #include <linux/errno.h> |
26 | #include <linux/etherdevice.h> | |
48881ed5 | 27 | #include <linux/eventpoll.h> |
1e2c2a4f SE |
28 | #include <linux/export.h> |
29 | #include <linux/fcntl.h> | |
30 | #include <linux/fs.h> | |
b92b94ac | 31 | #include <linux/gfp.h> |
1e2c2a4f SE |
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> | |
c6c8fea2 | 42 | #include <linux/slab.h> |
1e2c2a4f | 43 | #include <linux/spinlock.h> |
1e2c2a4f SE |
44 | #include <linux/stddef.h> |
45 | #include <linux/string.h> | |
46 | #include <linux/uaccess.h> | |
47 | #include <linux/wait.h> | |
fec149f5 | 48 | #include <uapi/linux/batadv_packet.h> |
1e2c2a4f | 49 | |
00caf6a2 | 50 | #include "debugfs.h" |
c6c8fea2 | 51 | #include "hard-interface.h" |
ba412080 | 52 | #include "log.h" |
1e2c2a4f | 53 | #include "originator.h" |
1e2c2a4f | 54 | #include "send.h" |
c6c8fea2 | 55 | |
56303d34 | 56 | static struct batadv_socket_client *batadv_socket_client_hash[256]; |
c6c8fea2 | 57 | |
56303d34 | 58 | static void batadv_socket_add_packet(struct batadv_socket_client *socket_client, |
da6b8c20 | 59 | struct batadv_icmp_header *icmph, |
af4447f6 | 60 | size_t icmp_len); |
c6c8fea2 | 61 | |
ff15c27c SE |
62 | /** |
63 | * batadv_socket_init() - Initialize soft interface independent socket data | |
64 | */ | |
9039dc7e | 65 | void batadv_socket_init(void) |
c6c8fea2 | 66 | { |
af4447f6 | 67 | memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash)); |
c6c8fea2 SE |
68 | } |
69 | ||
af4447f6 | 70 | static int batadv_socket_open(struct inode *inode, struct file *file) |
c6c8fea2 SE |
71 | { |
72 | unsigned int i; | |
56303d34 | 73 | struct batadv_socket_client *socket_client; |
c6c8fea2 | 74 | |
bd5b80d5 SE |
75 | if (!try_module_get(THIS_MODULE)) |
76 | return -EBUSY; | |
77 | ||
00caf6a2 SE |
78 | batadv_debugfs_deprecated(file, ""); |
79 | ||
c6c8fea2 SE |
80 | nonseekable_open(inode, file); |
81 | ||
704509b8 | 82 | socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL); |
bd5b80d5 SE |
83 | if (!socket_client) { |
84 | module_put(THIS_MODULE); | |
c6c8fea2 | 85 | return -ENOMEM; |
bd5b80d5 | 86 | } |
c6c8fea2 | 87 | |
af4447f6 SE |
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; | |
c6c8fea2 SE |
91 | break; |
92 | } | |
93 | } | |
94 | ||
af4447f6 | 95 | if (i == ARRAY_SIZE(batadv_socket_client_hash)) { |
86ceb360 | 96 | pr_err("Error - can't add another packet client: maximum number of clients reached\n"); |
c6c8fea2 | 97 | kfree(socket_client); |
bd5b80d5 | 98 | module_put(THIS_MODULE); |
c6c8fea2 SE |
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 | ||
c6c8fea2 SE |
111 | return 0; |
112 | } | |
113 | ||
af4447f6 | 114 | static int batadv_socket_release(struct inode *inode, struct file *file) |
c6c8fea2 | 115 | { |
fb1f23ea GT |
116 | struct batadv_socket_client *client = file->private_data; |
117 | struct batadv_socket_packet *packet, *tmp; | |
c6c8fea2 | 118 | |
fb1f23ea | 119 | spin_lock_bh(&client->lock); |
c6c8fea2 SE |
120 | |
121 | /* for all packets in the queue ... */ | |
fb1f23ea GT |
122 | list_for_each_entry_safe(packet, tmp, &client->queue_list, list) { |
123 | list_del(&packet->list); | |
124 | kfree(packet); | |
c6c8fea2 SE |
125 | } |
126 | ||
fb1f23ea GT |
127 | batadv_socket_client_hash[client->index] = NULL; |
128 | spin_unlock_bh(&client->lock); | |
c6c8fea2 | 129 | |
fb1f23ea | 130 | kfree(client); |
bd5b80d5 | 131 | module_put(THIS_MODULE); |
c6c8fea2 SE |
132 | |
133 | return 0; | |
134 | } | |
135 | ||
af4447f6 SE |
136 | static ssize_t batadv_socket_read(struct file *file, char __user *buf, |
137 | size_t count, loff_t *ppos) | |
c6c8fea2 | 138 | { |
56303d34 SE |
139 | struct batadv_socket_client *socket_client = file->private_data; |
140 | struct batadv_socket_packet *socket_packet; | |
c6c8fea2 SE |
141 | size_t packet_len; |
142 | int error; | |
143 | ||
825ffe1f | 144 | if ((file->f_flags & O_NONBLOCK) && socket_client->queue_len == 0) |
c6c8fea2 SE |
145 | return -EAGAIN; |
146 | ||
825ffe1f | 147 | if (!buf || count < sizeof(struct batadv_icmp_packet)) |
c6c8fea2 SE |
148 | return -EINVAL; |
149 | ||
96d4f267 | 150 | if (!access_ok(buf, count)) |
c6c8fea2 SE |
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, | |
56303d34 | 162 | struct batadv_socket_packet, list); |
c6c8fea2 SE |
163 | list_del(&socket_packet->list); |
164 | socket_client->queue_len--; | |
165 | ||
166 | spin_unlock_bh(&socket_client->lock); | |
167 | ||
b5a1eeef SE |
168 | packet_len = min(count, socket_packet->icmp_len); |
169 | error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len); | |
c6c8fea2 | 170 | |
c6c8fea2 SE |
171 | kfree(socket_packet); |
172 | ||
173 | if (error) | |
174 | return -EFAULT; | |
175 | ||
176 | return packet_len; | |
177 | } | |
178 | ||
af4447f6 SE |
179 | static ssize_t batadv_socket_write(struct file *file, const char __user *buff, |
180 | size_t len, loff_t *off) | |
c6c8fea2 | 181 | { |
56303d34 SE |
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; | |
c6c8fea2 | 185 | struct sk_buff *skb; |
da6b8c20 SW |
186 | struct batadv_icmp_packet_rr *icmp_packet_rr; |
187 | struct batadv_icmp_header *icmp_header; | |
56303d34 SE |
188 | struct batadv_orig_node *orig_node = NULL; |
189 | struct batadv_neigh_node *neigh_node = NULL; | |
96412690 | 190 | size_t packet_len = sizeof(struct batadv_icmp_packet); |
6b5e971a | 191 | u8 *addr; |
c6c8fea2 | 192 | |
da6b8c20 | 193 | if (len < sizeof(struct batadv_icmp_header)) { |
39c75a51 | 194 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf | 195 | "Error - can't send packet from char device: invalid packet size\n"); |
c6c8fea2 SE |
196 | return -EINVAL; |
197 | } | |
198 | ||
e5d89254 | 199 | primary_if = batadv_primary_if_get_selected(bat_priv); |
32ae9b22 ML |
200 | |
201 | if (!primary_if) { | |
202 | len = -EFAULT; | |
203 | goto out; | |
204 | } | |
c6c8fea2 | 205 | |
da6b8c20 SW |
206 | if (len >= BATADV_ICMP_MAX_PACKET_SIZE) |
207 | packet_len = BATADV_ICMP_MAX_PACKET_SIZE; | |
208 | else | |
209 | packet_len = len; | |
c6c8fea2 | 210 | |
41ab6c48 | 211 | skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN); |
32ae9b22 ML |
212 | if (!skb) { |
213 | len = -ENOMEM; | |
214 | goto out; | |
215 | } | |
c6c8fea2 | 216 | |
c54f38c9 | 217 | skb->priority = TC_PRIO_CONTROL; |
41ab6c48 | 218 | skb_reserve(skb, ETH_HLEN); |
4df864c1 | 219 | icmp_header = skb_put(skb, packet_len); |
c6c8fea2 | 220 | |
da6b8c20 | 221 | if (copy_from_user(icmp_header, buff, packet_len)) { |
c6c8fea2 SE |
222 | len = -EFAULT; |
223 | goto free_skb; | |
224 | } | |
225 | ||
a40d9b07 | 226 | if (icmp_header->packet_type != BATADV_ICMP) { |
39c75a51 | 227 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf | 228 | "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n"); |
c6c8fea2 SE |
229 | len = -EINVAL; |
230 | goto free_skb; | |
231 | } | |
232 | ||
da6b8c20 SW |
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 | ||
7351a482 SW |
249 | neigh_node = batadv_orig_router_get(orig_node, |
250 | BATADV_IF_DEFAULT); | |
da6b8c20 SW |
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; | |
8fdd0153 AQ |
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 | } | |
da6b8c20 SW |
265 | |
266 | break; | |
267 | default: | |
39c75a51 | 268 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
da6b8c20 | 269 | "Error - can't send packet from char device: got unknown message type\n"); |
c6c8fea2 SE |
270 | len = -EINVAL; |
271 | goto free_skb; | |
272 | } | |
273 | ||
da6b8c20 | 274 | icmp_header->uid = socket_client->index; |
c6c8fea2 | 275 | |
a40d9b07 | 276 | if (icmp_header->version != BATADV_COMPAT_VERSION) { |
da6b8c20 | 277 | icmp_header->msg_type = BATADV_PARAMETER_PROBLEM; |
a40d9b07 | 278 | icmp_header->version = BATADV_COMPAT_VERSION; |
da6b8c20 | 279 | batadv_socket_add_packet(socket_client, icmp_header, |
af4447f6 | 280 | packet_len); |
c6c8fea2 SE |
281 | goto free_skb; |
282 | } | |
283 | ||
8fdd0153 | 284 | ether_addr_copy(icmp_header->orig, primary_if->net_dev->dev_addr); |
c6c8fea2 | 285 | |
95d39278 | 286 | batadv_send_unicast_skb(skb, neigh_node); |
c6c8fea2 SE |
287 | goto out; |
288 | ||
c6c8fea2 | 289 | dst_unreach: |
da6b8c20 SW |
290 | icmp_header->msg_type = BATADV_DESTINATION_UNREACHABLE; |
291 | batadv_socket_add_packet(socket_client, icmp_header, packet_len); | |
c6c8fea2 SE |
292 | free_skb: |
293 | kfree_skb(skb); | |
294 | out: | |
32ae9b22 | 295 | if (primary_if) |
82047ad7 | 296 | batadv_hardif_put(primary_if); |
44524fcd | 297 | if (neigh_node) |
25bb2509 | 298 | batadv_neigh_node_put(neigh_node); |
44524fcd | 299 | if (orig_node) |
5d967310 | 300 | batadv_orig_node_put(orig_node); |
c6c8fea2 SE |
301 | return len; |
302 | } | |
303 | ||
ade994f4 | 304 | static __poll_t batadv_socket_poll(struct file *file, poll_table *wait) |
c6c8fea2 | 305 | { |
56303d34 | 306 | struct batadv_socket_client *socket_client = file->private_data; |
c6c8fea2 SE |
307 | |
308 | poll_wait(file, &socket_client->queue_wait, wait); | |
309 | ||
310 | if (socket_client->queue_len > 0) | |
a9a08845 | 311 | return EPOLLIN | EPOLLRDNORM; |
c6c8fea2 SE |
312 | |
313 | return 0; | |
314 | } | |
315 | ||
af4447f6 | 316 | static const struct file_operations batadv_fops = { |
c6c8fea2 | 317 | .owner = THIS_MODULE, |
af4447f6 SE |
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, | |
c6c8fea2 SE |
323 | .llseek = no_llseek, |
324 | }; | |
325 | ||
ff15c27c SE |
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 | */ | |
56303d34 | 332 | int batadv_socket_setup(struct batadv_priv *bat_priv) |
c6c8fea2 SE |
333 | { |
334 | struct dentry *d; | |
335 | ||
336 | if (!bat_priv->debug_dir) | |
337 | goto err; | |
338 | ||
507b37cf SE |
339 | d = debugfs_create_file(BATADV_ICMP_SOCKET, 0600, bat_priv->debug_dir, |
340 | bat_priv, &batadv_fops); | |
5346c35e | 341 | if (!d) |
c6c8fea2 SE |
342 | goto err; |
343 | ||
344 | return 0; | |
345 | ||
346 | err: | |
5346c35e | 347 | return -ENOMEM; |
c6c8fea2 SE |
348 | } |
349 | ||
da6b8c20 | 350 | /** |
7e9a8c2c | 351 | * batadv_socket_add_packet() - schedule an icmp packet to be sent to |
34473822 | 352 | * userspace on an icmp socket. |
da6b8c20 SW |
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 | */ | |
56303d34 | 357 | static void batadv_socket_add_packet(struct batadv_socket_client *socket_client, |
da6b8c20 | 358 | struct batadv_icmp_header *icmph, |
af4447f6 | 359 | size_t icmp_len) |
c6c8fea2 | 360 | { |
56303d34 | 361 | struct batadv_socket_packet *socket_packet; |
da6b8c20 | 362 | size_t len; |
c6c8fea2 | 363 | |
704509b8 | 364 | socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC); |
c6c8fea2 SE |
365 | |
366 | if (!socket_packet) | |
367 | return; | |
368 | ||
da6b8c20 SW |
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 | ||
c6c8fea2 | 374 | INIT_LIST_HEAD(&socket_packet->list); |
da6b8c20 SW |
375 | memcpy(&socket_packet->icmp_packet, icmph, len); |
376 | socket_packet->icmp_len = len; | |
c6c8fea2 SE |
377 | |
378 | spin_lock_bh(&socket_client->lock); | |
379 | ||
380 | /* while waiting for the lock the socket_client could have been | |
9cfc7bd6 SE |
381 | * deleted |
382 | */ | |
da6b8c20 | 383 | if (!batadv_socket_client_hash[icmph->uid]) { |
c6c8fea2 SE |
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, | |
56303d34 SE |
394 | struct batadv_socket_packet, |
395 | list); | |
c6c8fea2 SE |
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 | ||
da6b8c20 | 407 | /** |
7e9a8c2c | 408 | * batadv_socket_receive_packet() - schedule an icmp packet to be received |
da6b8c20 SW |
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, | |
9039dc7e | 414 | size_t icmp_len) |
c6c8fea2 | 415 | { |
56303d34 | 416 | struct batadv_socket_client *hash; |
c6c8fea2 | 417 | |
da6b8c20 | 418 | hash = batadv_socket_client_hash[icmph->uid]; |
c6c8fea2 | 419 | if (hash) |
da6b8c20 | 420 | batadv_socket_add_packet(hash, icmph, icmp_len); |
c6c8fea2 | 421 | } |