~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/net/batman-adv/icmp_socket.c

Version: ~ [ linux-5.3-rc5 ] ~ [ linux-5.2.9 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.67 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.139 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.189 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.189 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.72 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  1 /*
  2  * Copyright (C) 2007-2012 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 
 22 #include "main.h"
 23 #include <linux/debugfs.h>
 24 #include <linux/slab.h>
 25 #include "icmp_socket.h"
 26 #include "send.h"
 27 #include "hash.h"
 28 #include "originator.h"
 29 #include "hard-interface.h"
 30 
 31 static struct socket_client *socket_client_hash[256];
 32 
 33 static void bat_socket_add_packet(struct socket_client *socket_client,
 34                                   struct icmp_packet_rr *icmp_packet,
 35                                   size_t icmp_len);
 36 
 37 void bat_socket_init(void)
 38 {
 39         memset(socket_client_hash, 0, sizeof(socket_client_hash));
 40 }
 41 
 42 static int bat_socket_open(struct inode *inode, struct file *file)
 43 {
 44         unsigned int i;
 45         struct socket_client *socket_client;
 46 
 47         nonseekable_open(inode, file);
 48 
 49         socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
 50 
 51         if (!socket_client)
 52                 return -ENOMEM;
 53 
 54         for (i = 0; i < ARRAY_SIZE(socket_client_hash); i++) {
 55                 if (!socket_client_hash[i]) {
 56                         socket_client_hash[i] = socket_client;
 57                         break;
 58                 }
 59         }
 60 
 61         if (i == ARRAY_SIZE(socket_client_hash)) {
 62                 pr_err("Error - can't add another packet client: 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         socket_client->bat_priv = inode->i_private;
 71         spin_lock_init(&socket_client->lock);
 72         init_waitqueue_head(&socket_client->queue_wait);
 73 
 74         file->private_data = socket_client;
 75 
 76         inc_module_count();
 77         return 0;
 78 }
 79 
 80 static int bat_socket_release(struct inode *inode, struct file *file)
 81 {
 82         struct socket_client *socket_client = file->private_data;
 83         struct socket_packet *socket_packet;
 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,
 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_bh(&socket_client->lock);
 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 {
109         struct socket_client *socket_client = file->private_data;
110         struct socket_packet *socket_packet;
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 
117         if ((!buf) || (count < sizeof(struct icmp_packet)))
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,
132                                          struct socket_packet, list);
133         list_del(&socket_packet->list);
134         socket_client->queue_len--;
135 
136         spin_unlock_bh(&socket_client->lock);
137 
138         packet_len = min(count, socket_packet->icmp_len);
139         error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
140 
141         kfree(socket_packet);
142 
143         if (error)
144                 return -EFAULT;
145 
146         return packet_len;
147 }
148 
149 static ssize_t bat_socket_write(struct file *file, const char __user *buff,
150                                 size_t len, loff_t *off)
151 {
152         struct socket_client *socket_client = file->private_data;
153         struct bat_priv *bat_priv = socket_client->bat_priv;
154         struct hard_iface *primary_if = NULL;
155         struct sk_buff *skb;
156         struct icmp_packet_rr *icmp_packet;
157 
158         struct orig_node *orig_node = NULL;
159         struct neigh_node *neigh_node = NULL;
160         size_t packet_len = sizeof(struct icmp_packet);
161 
162         if (len < sizeof(struct icmp_packet)) {
163                 bat_dbg(DBG_BATMAN, bat_priv,
164                         "Error - can't send packet from char device: invalid packet size\n");
165                 return -EINVAL;
166         }
167 
168         primary_if = primary_if_get_selected(bat_priv);
169 
170         if (!primary_if) {
171                 len = -EFAULT;
172                 goto out;
173         }
174 
175         if (len >= sizeof(struct icmp_packet_rr))
176                 packet_len = sizeof(struct icmp_packet_rr);
177 
178         skb = dev_alloc_skb(packet_len + sizeof(struct ethhdr));
179         if (!skb) {
180                 len = -ENOMEM;
181                 goto out;
182         }
183 
184         skb_reserve(skb, sizeof(struct ethhdr));
185         icmp_packet = (struct icmp_packet_rr *)skb_put(skb, packet_len);
186 
187         if (copy_from_user(icmp_packet, buff, packet_len)) {
188                 len = -EFAULT;
189                 goto free_skb;
190         }
191 
192         if (icmp_packet->header.packet_type != BAT_ICMP) {
193                 bat_dbg(DBG_BATMAN, bat_priv,
194                         "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
195                 len = -EINVAL;
196                 goto free_skb;
197         }
198 
199         if (icmp_packet->msg_type != ECHO_REQUEST) {
200                 bat_dbg(DBG_BATMAN, bat_priv,
201                         "Error - can't send packet from char device: got bogus message type (expected: ECHO_REQUEST)\n");
202                 len = -EINVAL;
203                 goto free_skb;
204         }
205 
206         icmp_packet->uid = socket_client->index;
207 
208         if (icmp_packet->header.version != COMPAT_VERSION) {
209                 icmp_packet->msg_type = PARAMETER_PROBLEM;
210                 icmp_packet->header.version = COMPAT_VERSION;
211                 bat_socket_add_packet(socket_client, icmp_packet, packet_len);
212                 goto free_skb;
213         }
214 
215         if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
216                 goto dst_unreach;
217 
218         orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
219         if (!orig_node)
220                 goto dst_unreach;
221 
222         neigh_node = orig_node_get_router(orig_node);
223         if (!neigh_node)
224                 goto dst_unreach;
225 
226         if (!neigh_node->if_incoming)
227                 goto dst_unreach;
228 
229         if (neigh_node->if_incoming->if_status != IF_ACTIVE)
230                 goto dst_unreach;
231 
232         memcpy(icmp_packet->orig,
233                primary_if->net_dev->dev_addr, ETH_ALEN);
234 
235         if (packet_len == sizeof(struct icmp_packet_rr))
236                 memcpy(icmp_packet->rr,
237                        neigh_node->if_incoming->net_dev->dev_addr, ETH_ALEN);
238 
239         send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
240         goto out;
241 
242 dst_unreach:
243         icmp_packet->msg_type = DESTINATION_UNREACHABLE;
244         bat_socket_add_packet(socket_client, icmp_packet, packet_len);
245 free_skb:
246         kfree_skb(skb);
247 out:
248         if (primary_if)
249                 hardif_free_ref(primary_if);
250         if (neigh_node)
251                 neigh_node_free_ref(neigh_node);
252         if (orig_node)
253                 orig_node_free_ref(orig_node);
254         return len;
255 }
256 
257 static unsigned int bat_socket_poll(struct file *file, poll_table *wait)
258 {
259         struct socket_client *socket_client = file->private_data;
260 
261         poll_wait(file, &socket_client->queue_wait, wait);
262 
263         if (socket_client->queue_len > 0)
264                 return POLLIN | POLLRDNORM;
265 
266         return 0;
267 }
268 
269 static const struct file_operations fops = {
270         .owner = THIS_MODULE,
271         .open = bat_socket_open,
272         .release = bat_socket_release,
273         .read = bat_socket_read,
274         .write = bat_socket_write,
275         .poll = bat_socket_poll,
276         .llseek = no_llseek,
277 };
278 
279 int bat_socket_setup(struct bat_priv *bat_priv)
280 {
281         struct dentry *d;
282 
283         if (!bat_priv->debug_dir)
284                 goto err;
285 
286         d = debugfs_create_file(ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
287                                 bat_priv->debug_dir, bat_priv, &fops);
288         if (d)
289                 goto err;
290 
291         return 0;
292 
293 err:
294         return 1;
295 }
296 
297 static void bat_socket_add_packet(struct socket_client *socket_client,
298                                   struct icmp_packet_rr *icmp_packet,
299                                   size_t icmp_len)
300 {
301         struct socket_packet *socket_packet;
302 
303         socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
304 
305         if (!socket_packet)
306                 return;
307 
308         INIT_LIST_HEAD(&socket_packet->list);
309         memcpy(&socket_packet->icmp_packet, icmp_packet, icmp_len);
310         socket_packet->icmp_len = icmp_len;
311 
312         spin_lock_bh(&socket_client->lock);
313 
314         /* while waiting for the lock the socket_client could have been
315          * deleted */
316         if (!socket_client_hash[icmp_packet->uid]) {
317                 spin_unlock_bh(&socket_client->lock);
318                 kfree(socket_packet);
319                 return;
320         }
321 
322         list_add_tail(&socket_packet->list, &socket_client->queue_list);
323         socket_client->queue_len++;
324 
325         if (socket_client->queue_len > 100) {
326                 socket_packet = list_first_entry(&socket_client->queue_list,
327                                                  struct socket_packet, list);
328 
329                 list_del(&socket_packet->list);
330                 kfree(socket_packet);
331                 socket_client->queue_len--;
332         }
333 
334         spin_unlock_bh(&socket_client->lock);
335 
336         wake_up(&socket_client->queue_wait);
337 }
338 
339 void bat_socket_receive_packet(struct icmp_packet_rr *icmp_packet,
340                                size_t icmp_len)
341 {
342         struct socket_client *hash = socket_client_hash[icmp_packet->uid];
343 
344         if (hash)
345                 bat_socket_add_packet(hash, icmp_packet, icmp_len);
346 }
347 

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp