icmp_socket.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. error = copy_to_user(buf, &socket_packet->icmp_packet,
  110. socket_packet->icmp_len);
  111. packet_len = socket_packet->icmp_len;
  112. kfree(socket_packet);
  113. if (error)
  114. return -EFAULT;
  115. return packet_len;
  116. }
  117. static ssize_t bat_socket_write(struct file *file, const char __user *buff,
  118. size_t len, loff_t *off)
  119. {
  120. struct socket_client *socket_client = file->private_data;
  121. struct bat_priv *bat_priv = socket_client->bat_priv;
  122. struct hard_iface *primary_if = NULL;
  123. struct sk_buff *skb;
  124. struct icmp_packet_rr *icmp_packet;
  125. struct orig_node *orig_node = NULL;
  126. struct neigh_node *neigh_node = NULL;
  127. size_t packet_len = sizeof(struct icmp_packet);
  128. if (len < sizeof(struct icmp_packet)) {
  129. bat_dbg(DBG_BATMAN, bat_priv,
  130. "Error - can't send packet from char device: "
  131. "invalid packet size\n");
  132. return -EINVAL;
  133. }
  134. primary_if = primary_if_get_selected(bat_priv);
  135. if (!primary_if) {
  136. len = -EFAULT;
  137. goto out;
  138. }
  139. if (len >= sizeof(struct icmp_packet_rr))
  140. packet_len = sizeof(struct icmp_packet_rr);
  141. skb = dev_alloc_skb(packet_len + sizeof(struct ethhdr));
  142. if (!skb) {
  143. len = -ENOMEM;
  144. goto out;
  145. }
  146. skb_reserve(skb, sizeof(struct ethhdr));
  147. icmp_packet = (struct icmp_packet_rr *)skb_put(skb, packet_len);
  148. if (copy_from_user(icmp_packet, buff, packet_len)) {
  149. len = -EFAULT;
  150. goto free_skb;
  151. }
  152. if (icmp_packet->packet_type != BAT_ICMP) {
  153. bat_dbg(DBG_BATMAN, bat_priv,
  154. "Error - can't send packet from char device: "
  155. "got bogus packet type (expected: BAT_ICMP)\n");
  156. len = -EINVAL;
  157. goto free_skb;
  158. }
  159. if (icmp_packet->msg_type != ECHO_REQUEST) {
  160. bat_dbg(DBG_BATMAN, bat_priv,
  161. "Error - can't send packet from char device: "
  162. "got bogus message type (expected: ECHO_REQUEST)\n");
  163. len = -EINVAL;
  164. goto free_skb;
  165. }
  166. icmp_packet->uid = socket_client->index;
  167. if (icmp_packet->version != COMPAT_VERSION) {
  168. icmp_packet->msg_type = PARAMETER_PROBLEM;
  169. icmp_packet->version = COMPAT_VERSION;
  170. bat_socket_add_packet(socket_client, icmp_packet, packet_len);
  171. goto free_skb;
  172. }
  173. if (atomic_read(&bat_priv->mesh_state) != MESH_ACTIVE)
  174. goto dst_unreach;
  175. orig_node = orig_hash_find(bat_priv, icmp_packet->dst);
  176. if (!orig_node)
  177. goto dst_unreach;
  178. neigh_node = orig_node_get_router(orig_node);
  179. if (!neigh_node)
  180. goto dst_unreach;
  181. if (!neigh_node->if_incoming)
  182. goto dst_unreach;
  183. if (neigh_node->if_incoming->if_status != IF_ACTIVE)
  184. goto dst_unreach;
  185. memcpy(icmp_packet->orig,
  186. primary_if->net_dev->dev_addr, ETH_ALEN);
  187. if (packet_len == sizeof(struct icmp_packet_rr))
  188. memcpy(icmp_packet->rr,
  189. neigh_node->if_incoming->net_dev->dev_addr, ETH_ALEN);
  190. send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  191. goto out;
  192. dst_unreach:
  193. icmp_packet->msg_type = DESTINATION_UNREACHABLE;
  194. bat_socket_add_packet(socket_client, icmp_packet, packet_len);
  195. free_skb:
  196. kfree_skb(skb);
  197. out:
  198. if (primary_if)
  199. hardif_free_ref(primary_if);
  200. if (neigh_node)
  201. neigh_node_free_ref(neigh_node);
  202. if (orig_node)
  203. orig_node_free_ref(orig_node);
  204. return len;
  205. }
  206. static unsigned int bat_socket_poll(struct file *file, poll_table *wait)
  207. {
  208. struct socket_client *socket_client = file->private_data;
  209. poll_wait(file, &socket_client->queue_wait, wait);
  210. if (socket_client->queue_len > 0)
  211. return POLLIN | POLLRDNORM;
  212. return 0;
  213. }
  214. static const struct file_operations fops = {
  215. .owner = THIS_MODULE,
  216. .open = bat_socket_open,
  217. .release = bat_socket_release,
  218. .read = bat_socket_read,
  219. .write = bat_socket_write,
  220. .poll = bat_socket_poll,
  221. .llseek = no_llseek,
  222. };
  223. int bat_socket_setup(struct bat_priv *bat_priv)
  224. {
  225. struct dentry *d;
  226. if (!bat_priv->debug_dir)
  227. goto err;
  228. d = debugfs_create_file(ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
  229. bat_priv->debug_dir, bat_priv, &fops);
  230. if (d)
  231. goto err;
  232. return 0;
  233. err:
  234. return 1;
  235. }
  236. static void bat_socket_add_packet(struct socket_client *socket_client,
  237. struct icmp_packet_rr *icmp_packet,
  238. size_t icmp_len)
  239. {
  240. struct socket_packet *socket_packet;
  241. socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
  242. if (!socket_packet)
  243. return;
  244. INIT_LIST_HEAD(&socket_packet->list);
  245. memcpy(&socket_packet->icmp_packet, icmp_packet, icmp_len);
  246. socket_packet->icmp_len = icmp_len;
  247. spin_lock_bh(&socket_client->lock);
  248. /* while waiting for the lock the socket_client could have been
  249. * deleted */
  250. if (!socket_client_hash[icmp_packet->uid]) {
  251. spin_unlock_bh(&socket_client->lock);
  252. kfree(socket_packet);
  253. return;
  254. }
  255. list_add_tail(&socket_packet->list, &socket_client->queue_list);
  256. socket_client->queue_len++;
  257. if (socket_client->queue_len > 100) {
  258. socket_packet = list_first_entry(&socket_client->queue_list,
  259. struct socket_packet, list);
  260. list_del(&socket_packet->list);
  261. kfree(socket_packet);
  262. socket_client->queue_len--;
  263. }
  264. spin_unlock_bh(&socket_client->lock);
  265. wake_up(&socket_client->queue_wait);
  266. }
  267. void bat_socket_receive_packet(struct icmp_packet_rr *icmp_packet,
  268. size_t icmp_len)
  269. {
  270. struct socket_client *hash = socket_client_hash[icmp_packet->uid];
  271. if (hash)
  272. bat_socket_add_packet(hash, icmp_packet, icmp_len);
  273. }