icmp_socket.c 8.5 KB

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