icmp_socket.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. /*
  2. * Copyright (C) 2007-2011 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. #include "main.h"
  22. #include <linux/debugfs.h>
  23. #include <linux/slab.h>
  24. #include "icmp_socket.h"
  25. #include "send.h"
  26. #include "hash.h"
  27. #include "originator.h"
  28. #include "hard-interface.h"
  29. static struct socket_client *socket_client_hash[256];
  30. static void bat_socket_add_packet(struct socket_client *socket_client,
  31. struct icmp_packet_rr *icmp_packet,
  32. size_t icmp_len);
  33. void bat_socket_init(void)
  34. {
  35. memset(socket_client_hash, 0, sizeof(socket_client_hash));
  36. }
  37. static int bat_socket_open(struct inode *inode, struct file *file)
  38. {
  39. unsigned int i;
  40. struct socket_client *socket_client;
  41. nonseekable_open(inode, file);
  42. socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
  43. if (!socket_client)
  44. return -ENOMEM;
  45. for (i = 0; i < ARRAY_SIZE(socket_client_hash); i++) {
  46. if (!socket_client_hash[i]) {
  47. socket_client_hash[i] = socket_client;
  48. break;
  49. }
  50. }
  51. if (i == ARRAY_SIZE(socket_client_hash)) {
  52. pr_err("Error - can't add another packet client: "
  53. "maximum number of clients reached\n");
  54. kfree(socket_client);
  55. return -EXFULL;
  56. }
  57. INIT_LIST_HEAD(&socket_client->queue_list);
  58. socket_client->queue_len = 0;
  59. socket_client->index = i;
  60. socket_client->bat_priv = inode->i_private;
  61. spin_lock_init(&socket_client->lock);
  62. init_waitqueue_head(&socket_client->queue_wait);
  63. file->private_data = socket_client;
  64. inc_module_count();
  65. return 0;
  66. }
  67. static int bat_socket_release(struct inode *inode, struct file *file)
  68. {
  69. struct socket_client *socket_client = file->private_data;
  70. struct socket_packet *socket_packet;
  71. struct list_head *list_pos, *list_pos_tmp;
  72. spin_lock_bh(&socket_client->lock);
  73. /* for all packets in the queue ... */
  74. list_for_each_safe(list_pos, list_pos_tmp, &socket_client->queue_list) {
  75. socket_packet = list_entry(list_pos,
  76. struct socket_packet, list);
  77. list_del(list_pos);
  78. kfree(socket_packet);
  79. }
  80. socket_client_hash[socket_client->index] = NULL;
  81. spin_unlock_bh(&socket_client->lock);
  82. kfree(socket_client);
  83. dec_module_count();
  84. return 0;
  85. }
  86. static ssize_t bat_socket_read(struct file *file, char __user *buf,
  87. size_t count, loff_t *ppos)
  88. {
  89. struct socket_client *socket_client = file->private_data;
  90. struct socket_packet *socket_packet;
  91. size_t packet_len;
  92. int error;
  93. if ((file->f_flags & O_NONBLOCK) && (socket_client->queue_len == 0))
  94. return -EAGAIN;
  95. if ((!buf) || (count < sizeof(struct icmp_packet)))
  96. return -EINVAL;
  97. if (!access_ok(VERIFY_WRITE, buf, count))
  98. return -EFAULT;
  99. error = wait_event_interruptible(socket_client->queue_wait,
  100. socket_client->queue_len);
  101. if (error)
  102. return error;
  103. spin_lock_bh(&socket_client->lock);
  104. socket_packet = list_first_entry(&socket_client->queue_list,
  105. struct socket_packet, list);
  106. list_del(&socket_packet->list);
  107. socket_client->queue_len--;
  108. spin_unlock_bh(&socket_client->lock);
  109. packet_len = min(count, socket_packet->icmp_len);
  110. error = copy_to_user(buf, &socket_packet->icmp_packet, packet_len);
  111. kfree(socket_packet);
  112. if (error)
  113. return -EFAULT;
  114. return packet_len;
  115. }
  116. static ssize_t bat_socket_write(struct file *file, const char __user *buff,
  117. size_t len, loff_t *off)
  118. {
  119. struct socket_client *socket_client = file->private_data;
  120. struct bat_priv *bat_priv = socket_client->bat_priv;
  121. struct hard_iface *primary_if = NULL;
  122. struct sk_buff *skb;
  123. struct icmp_packet_rr *icmp_packet;
  124. struct orig_node *orig_node = NULL;
  125. struct neigh_node *neigh_node = NULL;
  126. size_t packet_len = sizeof(struct icmp_packet);
  127. if (len < sizeof(struct icmp_packet)) {
  128. bat_dbg(DBG_BATMAN, bat_priv,
  129. "Error - can't send packet from char device: "
  130. "invalid packet size\n");
  131. return -EINVAL;
  132. }
  133. primary_if = primary_if_get_selected(bat_priv);
  134. if (!primary_if) {
  135. len = -EFAULT;
  136. goto out;
  137. }
  138. if (len >= sizeof(struct icmp_packet_rr))
  139. packet_len = sizeof(struct icmp_packet_rr);
  140. skb = dev_alloc_skb(packet_len + sizeof(struct ethhdr));
  141. if (!skb) {
  142. len = -ENOMEM;
  143. goto out;
  144. }
  145. skb_reserve(skb, sizeof(struct ethhdr));
  146. icmp_packet = (struct icmp_packet_rr *)skb_put(skb, packet_len);
  147. if (copy_from_user(icmp_packet, buff, packet_len)) {
  148. len = -EFAULT;
  149. goto free_skb;
  150. }
  151. if (icmp_packet->packet_type != BAT_ICMP) {
  152. bat_dbg(DBG_BATMAN, bat_priv,
  153. "Error - can't send packet from char device: "
  154. "got bogus packet type (expected: BAT_ICMP)\n");
  155. len = -EINVAL;
  156. goto free_skb;
  157. }
  158. if (icmp_packet->msg_type != ECHO_REQUEST) {
  159. bat_dbg(DBG_BATMAN, bat_priv,
  160. "Error - can't send packet from char device: "
  161. "got bogus message type (expected: ECHO_REQUEST)\n");
  162. len = -EINVAL;
  163. goto free_skb;
  164. }
  165. icmp_packet->uid = socket_client->index;
  166. if (icmp_packet->version != COMPAT_VERSION) {
  167. icmp_packet->msg_type = PARAMETER_PROBLEM;
  168. icmp_packet->version = COMPAT_VERSION;
  169. bat_socket_add_packet(socket_client, icmp_packet, packet_len);
  170. goto free_skb;
  171. }
  172. if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
  173. goto dst_unreach;
  174. orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
  175. if (!orig_node)
  176. goto dst_unreach;
  177. neigh_node = orig_node_get_router(orig_node);
  178. if (!neigh_node)
  179. goto dst_unreach;
  180. if (!neigh_node->if_incoming)
  181. goto dst_unreach;
  182. if (neigh_node->if_incoming->if_status != IF_ACTIVE)
  183. goto dst_unreach;
  184. memcpy(icmp_packet->orig,
  185. primary_if->net_dev->dev_addr, ETH_ALEN);
  186. if (packet_len == sizeof(struct icmp_packet_rr))
  187. memcpy(icmp_packet->rr,
  188. neigh_node->if_incoming->net_dev->dev_addr, ETH_ALEN);
  189. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  190. goto out;
  191. dst_unreach:
  192. icmp_packet->msg_type = DESTINATION_UNREACHABLE;
  193. bat_socket_add_packet(socket_client, icmp_packet, packet_len);
  194. free_skb:
  195. kfree_skb(skb);
  196. out:
  197. if (primary_if)
  198. hardif_free_ref(primary_if);
  199. if (neigh_node)
  200. neigh_node_free_ref(neigh_node);
  201. if (orig_node)
  202. orig_node_free_ref(orig_node);
  203. return len;
  204. }
  205. static unsigned int bat_socket_poll(struct file *file, poll_table *wait)
  206. {
  207. struct socket_client *socket_client = file->private_data;
  208. poll_wait(file, &socket_client->queue_wait, wait);
  209. if (socket_client->queue_len > 0)
  210. return POLLIN | POLLRDNORM;
  211. return 0;
  212. }
  213. static const struct file_operations fops = {
  214. .owner = THIS_MODULE,
  215. .open = bat_socket_open,
  216. .release = bat_socket_release,
  217. .read = bat_socket_read,
  218. .write = bat_socket_write,
  219. .poll = bat_socket_poll,
  220. .llseek = no_llseek,
  221. };
  222. int bat_socket_setup(struct bat_priv *bat_priv)
  223. {
  224. struct dentry *d;
  225. if (!bat_priv->debug_dir)
  226. goto err;
  227. d = debugfs_create_file(ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
  228. bat_priv->debug_dir, bat_priv, &fops);
  229. if (d)
  230. goto err;
  231. return 0;
  232. err:
  233. return 1;
  234. }
  235. static void bat_socket_add_packet(struct socket_client *socket_client,
  236. struct icmp_packet_rr *icmp_packet,
  237. size_t icmp_len)
  238. {
  239. struct socket_packet *socket_packet;
  240. socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
  241. if (!socket_packet)
  242. return;
  243. INIT_LIST_HEAD(&socket_packet->list);
  244. memcpy(&socket_packet->icmp_packet, icmp_packet, icmp_len);
  245. socket_packet->icmp_len = icmp_len;
  246. spin_lock_bh(&socket_client->lock);
  247. /* while waiting for the lock the socket_client could have been
  248. * deleted */
  249. if (!socket_client_hash[icmp_packet->uid]) {
  250. spin_unlock_bh(&socket_client->lock);
  251. kfree(socket_packet);
  252. return;
  253. }
  254. list_add_tail(&socket_packet->list, &socket_client->queue_list);
  255. socket_client->queue_len++;
  256. if (socket_client->queue_len > 100) {
  257. socket_packet = list_first_entry(&socket_client->queue_list,
  258. struct socket_packet, list);
  259. list_del(&socket_packet->list);
  260. kfree(socket_packet);
  261. socket_client->queue_len--;
  262. }
  263. spin_unlock_bh(&socket_client->lock);
  264. wake_up(&socket_client->queue_wait);
  265. }
  266. void bat_socket_receive_packet(struct icmp_packet_rr *icmp_packet,
  267. size_t icmp_len)
  268. {
  269. struct socket_client *hash = socket_client_hash[icmp_packet->uid];
  270. if (hash)
  271. bat_socket_add_packet(hash, icmp_packet, icmp_len);
  272. }