]>
Commit | Line | Data |
---|---|---|
0b873931 | 1 | /* Copyright (C) 2007-2013 B.A.T.M.A.N. contributors: |
c6c8fea2 SE |
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 | |
c6c8fea2 SE |
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" | |
c6c8fea2 SE |
25 | #include "hash.h" |
26 | #include "originator.h" | |
27 | #include "hard-interface.h" | |
28 | ||
56303d34 | 29 | static struct batadv_socket_client *batadv_socket_client_hash[256]; |
c6c8fea2 | 30 | |
56303d34 | 31 | static void batadv_socket_add_packet(struct batadv_socket_client *socket_client, |
96412690 | 32 | struct batadv_icmp_packet_rr *icmp_packet, |
af4447f6 | 33 | size_t icmp_len); |
c6c8fea2 | 34 | |
9039dc7e | 35 | void batadv_socket_init(void) |
c6c8fea2 | 36 | { |
af4447f6 | 37 | memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash)); |
c6c8fea2 SE |
38 | } |
39 | ||
af4447f6 | 40 | static int batadv_socket_open(struct inode *inode, struct file *file) |
c6c8fea2 SE |
41 | { |
42 | unsigned int i; | |
56303d34 | 43 | struct batadv_socket_client *socket_client; |
c6c8fea2 | 44 | |
bd5b80d5 SE |
45 | if (!try_module_get(THIS_MODULE)) |
46 | return -EBUSY; | |
47 | ||
c6c8fea2 SE |
48 | nonseekable_open(inode, file); |
49 | ||
704509b8 | 50 | socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL); |
bd5b80d5 SE |
51 | if (!socket_client) { |
52 | module_put(THIS_MODULE); | |
c6c8fea2 | 53 | return -ENOMEM; |
bd5b80d5 | 54 | } |
c6c8fea2 | 55 | |
af4447f6 SE |
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; | |
c6c8fea2 SE |
59 | break; |
60 | } | |
61 | } | |
62 | ||
af4447f6 | 63 | if (i == ARRAY_SIZE(batadv_socket_client_hash)) { |
86ceb360 | 64 | pr_err("Error - can't add another packet client: maximum number of clients reached\n"); |
c6c8fea2 | 65 | kfree(socket_client); |
bd5b80d5 | 66 | module_put(THIS_MODULE); |
c6c8fea2 SE |
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 | ||
c6c8fea2 SE |
79 | return 0; |
80 | } | |
81 | ||
af4447f6 | 82 | static int batadv_socket_release(struct inode *inode, struct file *file) |
c6c8fea2 | 83 | { |
56303d34 SE |
84 | struct batadv_socket_client *socket_client = file->private_data; |
85 | struct batadv_socket_packet *socket_packet; | |
c6c8fea2 SE |
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, | |
56303d34 | 93 | struct batadv_socket_packet, list); |
c6c8fea2 SE |
94 | |
95 | list_del(list_pos); | |
96 | kfree(socket_packet); | |
97 | } | |
98 | ||
af4447f6 | 99 | batadv_socket_client_hash[socket_client->index] = NULL; |
c6c8fea2 SE |
100 | spin_unlock_bh(&socket_client->lock); |
101 | ||
102 | kfree(socket_client); | |
bd5b80d5 | 103 | module_put(THIS_MODULE); |
c6c8fea2 SE |
104 | |
105 | return 0; | |
106 | } | |
107 | ||
af4447f6 SE |
108 | static ssize_t batadv_socket_read(struct file *file, char __user *buf, |
109 | size_t count, loff_t *ppos) | |
c6c8fea2 | 110 | { |
56303d34 SE |
111 | struct batadv_socket_client *socket_client = file->private_data; |
112 | struct batadv_socket_packet *socket_packet; | |
c6c8fea2 SE |
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 | ||
96412690 | 119 | if ((!buf) || (count < sizeof(struct batadv_icmp_packet))) |
c6c8fea2 SE |
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, | |
56303d34 | 134 | struct batadv_socket_packet, list); |
c6c8fea2 SE |
135 | list_del(&socket_packet->list); |
136 | socket_client->queue_len--; | |
137 | ||
138 | spin_unlock_bh(&socket_client->lock); | |
139 | ||
b5a1eeef SE |
140 | packet_len = min(count, socket_packet->icmp_len); |
141 | error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len); | |
c6c8fea2 | 142 | |
c6c8fea2 SE |
143 | kfree(socket_packet); |
144 | ||
145 | if (error) | |
146 | return -EFAULT; | |
147 | ||
148 | return packet_len; | |
149 | } | |
150 | ||
af4447f6 SE |
151 | static ssize_t batadv_socket_write(struct file *file, const char __user *buff, |
152 | size_t len, loff_t *off) | |
c6c8fea2 | 153 | { |
56303d34 SE |
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; | |
c6c8fea2 | 157 | struct sk_buff *skb; |
96412690 | 158 | struct batadv_icmp_packet_rr *icmp_packet; |
c6c8fea2 | 159 | |
56303d34 SE |
160 | struct batadv_orig_node *orig_node = NULL; |
161 | struct batadv_neigh_node *neigh_node = NULL; | |
96412690 | 162 | size_t packet_len = sizeof(struct batadv_icmp_packet); |
c6c8fea2 | 163 | |
96412690 | 164 | if (len < sizeof(struct batadv_icmp_packet)) { |
39c75a51 | 165 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf | 166 | "Error - can't send packet from char device: invalid packet size\n"); |
c6c8fea2 SE |
167 | return -EINVAL; |
168 | } | |
169 | ||
e5d89254 | 170 | primary_if = batadv_primary_if_get_selected(bat_priv); |
32ae9b22 ML |
171 | |
172 | if (!primary_if) { | |
173 | len = -EFAULT; | |
174 | goto out; | |
175 | } | |
c6c8fea2 | 176 | |
96412690 SE |
177 | if (len >= sizeof(struct batadv_icmp_packet_rr)) |
178 | packet_len = sizeof(struct batadv_icmp_packet_rr); | |
c6c8fea2 | 179 | |
41ab6c48 | 180 | skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN); |
32ae9b22 ML |
181 | if (!skb) { |
182 | len = -ENOMEM; | |
183 | goto out; | |
184 | } | |
c6c8fea2 | 185 | |
c54f38c9 | 186 | skb->priority = TC_PRIO_CONTROL; |
41ab6c48 | 187 | skb_reserve(skb, ETH_HLEN); |
96412690 | 188 | icmp_packet = (struct batadv_icmp_packet_rr *)skb_put(skb, packet_len); |
c6c8fea2 | 189 | |
d18eb453 | 190 | if (copy_from_user(icmp_packet, buff, packet_len)) { |
c6c8fea2 SE |
191 | len = -EFAULT; |
192 | goto free_skb; | |
193 | } | |
194 | ||
0bf84c16 | 195 | if (icmp_packet->icmph.header.packet_type != BATADV_ICMP) { |
39c75a51 | 196 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf | 197 | "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n"); |
c6c8fea2 SE |
198 | len = -EINVAL; |
199 | goto free_skb; | |
200 | } | |
201 | ||
0bf84c16 | 202 | if (icmp_packet->icmph.msg_type != BATADV_ECHO_REQUEST) { |
39c75a51 | 203 | batadv_dbg(BATADV_DBG_BATMAN, bat_priv, |
1eda58bf | 204 | "Error - can't send packet from char device: got bogus message type (expected: ECHO_REQUEST)\n"); |
c6c8fea2 SE |
205 | len = -EINVAL; |
206 | goto free_skb; | |
207 | } | |
208 | ||
0bf84c16 | 209 | icmp_packet->icmph.uid = socket_client->index; |
c6c8fea2 | 210 | |
0bf84c16 AQ |
211 | if (icmp_packet->icmph.header.version != BATADV_COMPAT_VERSION) { |
212 | icmp_packet->icmph.msg_type = BATADV_PARAMETER_PROBLEM; | |
213 | icmp_packet->icmph.header.version = BATADV_COMPAT_VERSION; | |
af4447f6 SE |
214 | batadv_socket_add_packet(socket_client, icmp_packet, |
215 | packet_len); | |
c6c8fea2 SE |
216 | goto free_skb; |
217 | } | |
218 | ||
39c75a51 | 219 | if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE) |
c6c8fea2 SE |
220 | goto dst_unreach; |
221 | ||
0bf84c16 | 222 | orig_node = batadv_orig_hash_find(bat_priv, icmp_packet->icmph.dst); |
c6c8fea2 | 223 | if (!orig_node) |
e1a5382f | 224 | goto dst_unreach; |
44524fcd | 225 | |
7d211efc | 226 | neigh_node = batadv_orig_node_get_router(orig_node); |
44524fcd | 227 | if (!neigh_node) |
e1a5382f | 228 | goto dst_unreach; |
c6c8fea2 | 229 | |
d0072609 | 230 | if (!neigh_node->if_incoming) |
c6c8fea2 SE |
231 | goto dst_unreach; |
232 | ||
e9a4f295 | 233 | if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE) |
c6c8fea2 SE |
234 | goto dst_unreach; |
235 | ||
0bf84c16 | 236 | memcpy(icmp_packet->icmph.orig, |
32ae9b22 | 237 | primary_if->net_dev->dev_addr, ETH_ALEN); |
c6c8fea2 | 238 | |
96412690 | 239 | if (packet_len == sizeof(struct batadv_icmp_packet_rr)) |
44524fcd | 240 | memcpy(icmp_packet->rr, |
d0072609 | 241 | neigh_node->if_incoming->net_dev->dev_addr, ETH_ALEN); |
c6c8fea2 | 242 | |
9455e34c | 243 | batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr); |
c6c8fea2 SE |
244 | goto out; |
245 | ||
c6c8fea2 | 246 | dst_unreach: |
0bf84c16 | 247 | icmp_packet->icmph.msg_type = BATADV_DESTINATION_UNREACHABLE; |
af4447f6 | 248 | batadv_socket_add_packet(socket_client, icmp_packet, packet_len); |
c6c8fea2 SE |
249 | free_skb: |
250 | kfree_skb(skb); | |
251 | out: | |
32ae9b22 | 252 | if (primary_if) |
e5d89254 | 253 | batadv_hardif_free_ref(primary_if); |
44524fcd | 254 | if (neigh_node) |
7d211efc | 255 | batadv_neigh_node_free_ref(neigh_node); |
44524fcd | 256 | if (orig_node) |
7d211efc | 257 | batadv_orig_node_free_ref(orig_node); |
c6c8fea2 SE |
258 | return len; |
259 | } | |
260 | ||
af4447f6 | 261 | static unsigned int batadv_socket_poll(struct file *file, poll_table *wait) |
c6c8fea2 | 262 | { |
56303d34 | 263 | struct batadv_socket_client *socket_client = file->private_data; |
c6c8fea2 SE |
264 | |
265 | poll_wait(file, &socket_client->queue_wait, wait); | |
266 | ||
267 | if (socket_client->queue_len > 0) | |
268 | return POLLIN | POLLRDNORM; | |
269 | ||
270 | return 0; | |
271 | } | |
272 | ||
af4447f6 | 273 | static const struct file_operations batadv_fops = { |
c6c8fea2 | 274 | .owner = THIS_MODULE, |
af4447f6 SE |
275 | .open = batadv_socket_open, |
276 | .release = batadv_socket_release, | |
277 | .read = batadv_socket_read, | |
278 | .write = batadv_socket_write, | |
279 | .poll = batadv_socket_poll, | |
c6c8fea2 SE |
280 | .llseek = no_llseek, |
281 | }; | |
282 | ||
56303d34 | 283 | int batadv_socket_setup(struct batadv_priv *bat_priv) |
c6c8fea2 SE |
284 | { |
285 | struct dentry *d; | |
286 | ||
287 | if (!bat_priv->debug_dir) | |
288 | goto err; | |
289 | ||
64346643 | 290 | d = debugfs_create_file(BATADV_ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR, |
af4447f6 | 291 | bat_priv->debug_dir, bat_priv, &batadv_fops); |
5346c35e | 292 | if (!d) |
c6c8fea2 SE |
293 | goto err; |
294 | ||
295 | return 0; | |
296 | ||
297 | err: | |
5346c35e | 298 | return -ENOMEM; |
c6c8fea2 SE |
299 | } |
300 | ||
56303d34 | 301 | static void batadv_socket_add_packet(struct batadv_socket_client *socket_client, |
96412690 | 302 | struct batadv_icmp_packet_rr *icmp_packet, |
af4447f6 | 303 | size_t icmp_len) |
c6c8fea2 | 304 | { |
56303d34 | 305 | struct batadv_socket_packet *socket_packet; |
c6c8fea2 | 306 | |
704509b8 | 307 | socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC); |
c6c8fea2 SE |
308 | |
309 | if (!socket_packet) | |
310 | return; | |
311 | ||
312 | INIT_LIST_HEAD(&socket_packet->list); | |
313 | memcpy(&socket_packet->icmp_packet, icmp_packet, icmp_len); | |
314 | socket_packet->icmp_len = icmp_len; | |
315 | ||
316 | spin_lock_bh(&socket_client->lock); | |
317 | ||
318 | /* while waiting for the lock the socket_client could have been | |
9cfc7bd6 SE |
319 | * deleted |
320 | */ | |
0bf84c16 | 321 | if (!batadv_socket_client_hash[icmp_packet->icmph.uid]) { |
c6c8fea2 SE |
322 | spin_unlock_bh(&socket_client->lock); |
323 | kfree(socket_packet); | |
324 | return; | |
325 | } | |
326 | ||
327 | list_add_tail(&socket_packet->list, &socket_client->queue_list); | |
328 | socket_client->queue_len++; | |
329 | ||
330 | if (socket_client->queue_len > 100) { | |
331 | socket_packet = list_first_entry(&socket_client->queue_list, | |
56303d34 SE |
332 | struct batadv_socket_packet, |
333 | list); | |
c6c8fea2 SE |
334 | |
335 | list_del(&socket_packet->list); | |
336 | kfree(socket_packet); | |
337 | socket_client->queue_len--; | |
338 | } | |
339 | ||
340 | spin_unlock_bh(&socket_client->lock); | |
341 | ||
342 | wake_up(&socket_client->queue_wait); | |
343 | } | |
344 | ||
96412690 | 345 | void batadv_socket_receive_packet(struct batadv_icmp_packet_rr *icmp_packet, |
9039dc7e | 346 | size_t icmp_len) |
c6c8fea2 | 347 | { |
56303d34 | 348 | struct batadv_socket_client *hash; |
c6c8fea2 | 349 | |
0bf84c16 | 350 | hash = batadv_socket_client_hash[icmp_packet->icmph.uid]; |
c6c8fea2 | 351 | if (hash) |
af4447f6 | 352 | batadv_socket_add_packet(hash, icmp_packet, icmp_len); |
c6c8fea2 | 353 | } |