]>
Commit | Line | Data |
---|---|---|
c4121432 SE |
1 | /* |
2 | * Copyright (C) 2007-2010 B.A.T.M.A.N. contributors: | |
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 | |
16 | * along with this program; if not, write to the Free Software | |
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA | |
18 | * 02110-1301, USA | |
19 | * | |
20 | */ | |
21 | ||
c1641862 | 22 | #include "main.h" |
c4121432 SE |
23 | #include <linux/debugfs.h> |
24 | #include <linux/slab.h> | |
c4121432 SE |
25 | #include "icmp_socket.h" |
26 | #include "send.h" | |
27 | #include "types.h" | |
28 | #include "hash.h" | |
29 | #include "hard-interface.h" | |
30 | ||
31 | ||
32 | static struct socket_client *socket_client_hash[256]; | |
33 | ||
34 | static void bat_socket_add_packet(struct socket_client *socket_client, | |
e4cb3720 DS |
35 | struct icmp_packet_rr *icmp_packet, |
36 | size_t icmp_len); | |
c4121432 SE |
37 | |
38 | void bat_socket_init(void) | |
39 | { | |
40 | memset(socket_client_hash, 0, sizeof(socket_client_hash)); | |
41 | } | |
42 | ||
43 | static int bat_socket_open(struct inode *inode, struct file *file) | |
44 | { | |
45 | unsigned int i; | |
46 | struct socket_client *socket_client; | |
47 | ||
48 | socket_client = kmalloc(sizeof(struct socket_client), GFP_KERNEL); | |
49 | ||
50 | if (!socket_client) | |
51 | return -ENOMEM; | |
52 | ||
53 | for (i = 0; i < ARRAY_SIZE(socket_client_hash); i++) { | |
54 | if (!socket_client_hash[i]) { | |
55 | socket_client_hash[i] = socket_client; | |
56 | break; | |
57 | } | |
58 | } | |
59 | ||
60 | if (i == ARRAY_SIZE(socket_client_hash)) { | |
c1641862 | 61 | pr_err("Error - can't add another packet client: " |
c4121432 SE |
62 | "maximum number of clients reached\n"); |
63 | kfree(socket_client); | |
64 | return -EXFULL; | |
65 | } | |
66 | ||
67 | INIT_LIST_HEAD(&socket_client->queue_list); | |
68 | socket_client->queue_len = 0; | |
69 | socket_client->index = i; | |
70 | spin_lock_init(&socket_client->lock); | |
71 | init_waitqueue_head(&socket_client->queue_wait); | |
72 | ||
73 | file->private_data = socket_client; | |
74 | ||
75 | inc_module_count(); | |
76 | return 0; | |
77 | } | |
78 | ||
79 | static int bat_socket_release(struct inode *inode, struct file *file) | |
80 | { | |
ba952d84 | 81 | struct socket_client *socket_client = file->private_data; |
c4121432 SE |
82 | struct socket_packet *socket_packet; |
83 | struct list_head *list_pos, *list_pos_tmp; | |
84 | unsigned long flags; | |
85 | ||
86 | spin_lock_irqsave(&socket_client->lock, flags); | |
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, | |
91 | struct socket_packet, list); | |
92 | ||
93 | list_del(list_pos); | |
94 | kfree(socket_packet); | |
95 | } | |
96 | ||
97 | socket_client_hash[socket_client->index] = NULL; | |
98 | spin_unlock_irqrestore(&socket_client->lock, flags); | |
99 | ||
100 | kfree(socket_client); | |
101 | dec_module_count(); | |
102 | ||
103 | return 0; | |
104 | } | |
105 | ||
106 | static ssize_t bat_socket_read(struct file *file, char __user *buf, | |
107 | size_t count, loff_t *ppos) | |
108 | { | |
ba952d84 | 109 | struct socket_client *socket_client = file->private_data; |
c4121432 | 110 | struct socket_packet *socket_packet; |
e4cb3720 | 111 | size_t packet_len; |
c4121432 SE |
112 | int error; |
113 | unsigned long flags; | |
114 | ||
115 | if ((file->f_flags & O_NONBLOCK) && (socket_client->queue_len == 0)) | |
116 | return -EAGAIN; | |
117 | ||
118 | if ((!buf) || (count < sizeof(struct icmp_packet))) | |
119 | return -EINVAL; | |
120 | ||
121 | if (!access_ok(VERIFY_WRITE, buf, count)) | |
122 | return -EFAULT; | |
123 | ||
124 | error = wait_event_interruptible(socket_client->queue_wait, | |
125 | socket_client->queue_len); | |
126 | ||
127 | if (error) | |
128 | return error; | |
129 | ||
130 | spin_lock_irqsave(&socket_client->lock, flags); | |
131 | ||
132 | socket_packet = list_first_entry(&socket_client->queue_list, | |
133 | struct socket_packet, list); | |
134 | list_del(&socket_packet->list); | |
135 | socket_client->queue_len--; | |
136 | ||
137 | spin_unlock_irqrestore(&socket_client->lock, flags); | |
138 | ||
139 | error = __copy_to_user(buf, &socket_packet->icmp_packet, | |
e4cb3720 | 140 | socket_packet->icmp_len); |
c4121432 | 141 | |
e4cb3720 | 142 | packet_len = socket_packet->icmp_len; |
c4121432 SE |
143 | kfree(socket_packet); |
144 | ||
145 | if (error) | |
146 | return -EFAULT; | |
147 | ||
e4cb3720 | 148 | return packet_len; |
c4121432 SE |
149 | } |
150 | ||
151 | static ssize_t bat_socket_write(struct file *file, const char __user *buff, | |
152 | size_t len, loff_t *off) | |
153 | { | |
84ec0864 ML |
154 | /* FIXME: each orig_node->batman_if will be attached to a softif */ |
155 | struct bat_priv *bat_priv = netdev_priv(soft_device); | |
ba952d84 | 156 | struct socket_client *socket_client = file->private_data; |
e4cb3720 | 157 | struct icmp_packet_rr icmp_packet; |
c4121432 SE |
158 | struct orig_node *orig_node; |
159 | struct batman_if *batman_if; | |
e4cb3720 | 160 | size_t packet_len = sizeof(struct icmp_packet); |
c4121432 SE |
161 | uint8_t dstaddr[ETH_ALEN]; |
162 | unsigned long flags; | |
163 | ||
164 | if (len < sizeof(struct icmp_packet)) { | |
84ec0864 | 165 | bat_dbg(DBG_BATMAN, bat_priv, |
c4121432 SE |
166 | "Error - can't send packet from char device: " |
167 | "invalid packet size\n"); | |
168 | return -EINVAL; | |
169 | } | |
170 | ||
e4cb3720 DS |
171 | if (len >= sizeof(struct icmp_packet_rr)) |
172 | packet_len = sizeof(struct icmp_packet_rr); | |
173 | ||
174 | if (!access_ok(VERIFY_READ, buff, packet_len)) | |
c4121432 SE |
175 | return -EFAULT; |
176 | ||
e4cb3720 | 177 | if (__copy_from_user(&icmp_packet, buff, packet_len)) |
c4121432 SE |
178 | return -EFAULT; |
179 | ||
180 | if (icmp_packet.packet_type != BAT_ICMP) { | |
84ec0864 | 181 | bat_dbg(DBG_BATMAN, bat_priv, |
c4121432 SE |
182 | "Error - can't send packet from char device: " |
183 | "got bogus packet type (expected: BAT_ICMP)\n"); | |
184 | return -EINVAL; | |
185 | } | |
186 | ||
187 | if (icmp_packet.msg_type != ECHO_REQUEST) { | |
84ec0864 | 188 | bat_dbg(DBG_BATMAN, bat_priv, |
c4121432 SE |
189 | "Error - can't send packet from char device: " |
190 | "got bogus message type (expected: ECHO_REQUEST)\n"); | |
191 | return -EINVAL; | |
192 | } | |
193 | ||
194 | icmp_packet.uid = socket_client->index; | |
195 | ||
196 | if (icmp_packet.version != COMPAT_VERSION) { | |
197 | icmp_packet.msg_type = PARAMETER_PROBLEM; | |
198 | icmp_packet.ttl = COMPAT_VERSION; | |
e4cb3720 | 199 | bat_socket_add_packet(socket_client, &icmp_packet, packet_len); |
c4121432 SE |
200 | goto out; |
201 | } | |
202 | ||
203 | if (atomic_read(&module_state) != MODULE_ACTIVE) | |
204 | goto dst_unreach; | |
205 | ||
206 | spin_lock_irqsave(&orig_hash_lock, flags); | |
207 | orig_node = ((struct orig_node *)hash_find(orig_hash, icmp_packet.dst)); | |
208 | ||
209 | if (!orig_node) | |
210 | goto unlock; | |
211 | ||
212 | if (!orig_node->router) | |
213 | goto unlock; | |
214 | ||
215 | batman_if = orig_node->router->if_incoming; | |
216 | memcpy(dstaddr, orig_node->router->addr, ETH_ALEN); | |
217 | ||
218 | spin_unlock_irqrestore(&orig_hash_lock, flags); | |
219 | ||
220 | if (!batman_if) | |
221 | goto dst_unreach; | |
222 | ||
223 | if (batman_if->if_status != IF_ACTIVE) | |
224 | goto dst_unreach; | |
225 | ||
e4cb3720 DS |
226 | memcpy(icmp_packet.orig, batman_if->net_dev->dev_addr, ETH_ALEN); |
227 | ||
228 | if (packet_len == sizeof(struct icmp_packet_rr)) | |
229 | memcpy(icmp_packet.rr, batman_if->net_dev->dev_addr, ETH_ALEN); | |
c4121432 SE |
230 | |
231 | send_raw_packet((unsigned char *)&icmp_packet, | |
e4cb3720 | 232 | packet_len, batman_if, dstaddr); |
c4121432 SE |
233 | |
234 | goto out; | |
235 | ||
236 | unlock: | |
237 | spin_unlock_irqrestore(&orig_hash_lock, flags); | |
238 | dst_unreach: | |
239 | icmp_packet.msg_type = DESTINATION_UNREACHABLE; | |
e4cb3720 | 240 | bat_socket_add_packet(socket_client, &icmp_packet, packet_len); |
c4121432 SE |
241 | out: |
242 | return len; | |
243 | } | |
244 | ||
245 | static unsigned int bat_socket_poll(struct file *file, poll_table *wait) | |
246 | { | |
ba952d84 | 247 | struct socket_client *socket_client = file->private_data; |
c4121432 SE |
248 | |
249 | poll_wait(file, &socket_client->queue_wait, wait); | |
250 | ||
251 | if (socket_client->queue_len > 0) | |
252 | return POLLIN | POLLRDNORM; | |
253 | ||
254 | return 0; | |
255 | } | |
256 | ||
257 | static const struct file_operations fops = { | |
258 | .owner = THIS_MODULE, | |
259 | .open = bat_socket_open, | |
260 | .release = bat_socket_release, | |
261 | .read = bat_socket_read, | |
262 | .write = bat_socket_write, | |
263 | .poll = bat_socket_poll, | |
264 | }; | |
265 | ||
266 | int bat_socket_setup(struct bat_priv *bat_priv) | |
267 | { | |
268 | struct dentry *d; | |
269 | ||
270 | if (!bat_priv->debug_dir) | |
271 | goto err; | |
272 | ||
273 | d = debugfs_create_file(ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR, | |
274 | bat_priv->debug_dir, NULL, &fops); | |
275 | if (d) | |
276 | goto err; | |
277 | ||
278 | return 0; | |
279 | ||
280 | err: | |
281 | return 1; | |
282 | } | |
283 | ||
284 | static void bat_socket_add_packet(struct socket_client *socket_client, | |
e4cb3720 DS |
285 | struct icmp_packet_rr *icmp_packet, |
286 | size_t icmp_len) | |
c4121432 SE |
287 | { |
288 | struct socket_packet *socket_packet; | |
289 | unsigned long flags; | |
290 | ||
291 | socket_packet = kmalloc(sizeof(struct socket_packet), GFP_ATOMIC); | |
292 | ||
293 | if (!socket_packet) | |
294 | return; | |
295 | ||
296 | INIT_LIST_HEAD(&socket_packet->list); | |
e4cb3720 DS |
297 | memcpy(&socket_packet->icmp_packet, icmp_packet, icmp_len); |
298 | socket_packet->icmp_len = icmp_len; | |
c4121432 SE |
299 | |
300 | spin_lock_irqsave(&socket_client->lock, flags); | |
301 | ||
302 | /* while waiting for the lock the socket_client could have been | |
303 | * deleted */ | |
304 | if (!socket_client_hash[icmp_packet->uid]) { | |
305 | spin_unlock_irqrestore(&socket_client->lock, flags); | |
306 | kfree(socket_packet); | |
307 | return; | |
308 | } | |
309 | ||
310 | list_add_tail(&socket_packet->list, &socket_client->queue_list); | |
311 | socket_client->queue_len++; | |
312 | ||
313 | if (socket_client->queue_len > 100) { | |
314 | socket_packet = list_first_entry(&socket_client->queue_list, | |
315 | struct socket_packet, list); | |
316 | ||
317 | list_del(&socket_packet->list); | |
318 | kfree(socket_packet); | |
319 | socket_client->queue_len--; | |
320 | } | |
321 | ||
322 | spin_unlock_irqrestore(&socket_client->lock, flags); | |
323 | ||
324 | wake_up(&socket_client->queue_wait); | |
325 | } | |
326 | ||
e4cb3720 DS |
327 | void bat_socket_receive_packet(struct icmp_packet_rr *icmp_packet, |
328 | size_t icmp_len) | |
c4121432 SE |
329 | { |
330 | struct socket_client *hash = socket_client_hash[icmp_packet->uid]; | |
331 | ||
332 | if (hash) | |
e4cb3720 | 333 | bat_socket_add_packet(hash, icmp_packet, icmp_len); |
c4121432 | 334 | } |