icmp_socket.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  1. /* Copyright (C) 2007-2014 B.A.T.M.A.N. contributors:
  2. *
  3. * Marek Lindner
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of version 2 of the GNU General Public
  7. * License as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. */
  17. #include "main.h"
  18. #include <linux/debugfs.h>
  19. #include <linux/slab.h>
  20. #include "icmp_socket.h"
  21. #include "send.h"
  22. #include "hash.h"
  23. #include "originator.h"
  24. #include "hard-interface.h"
  25. static struct batadv_socket_client *batadv_socket_client_hash[256];
  26. static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
  27. struct batadv_icmp_header *icmph,
  28. size_t icmp_len);
  29. void batadv_socket_init(void)
  30. {
  31. memset(batadv_socket_client_hash, 0, sizeof(batadv_socket_client_hash));
  32. }
  33. static int batadv_socket_open(struct inode *inode, struct file *file)
  34. {
  35. unsigned int i;
  36. struct batadv_socket_client *socket_client;
  37. if (!try_module_get(THIS_MODULE))
  38. return -EBUSY;
  39. nonseekable_open(inode, file);
  40. socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);
  41. if (!socket_client) {
  42. module_put(THIS_MODULE);
  43. return -ENOMEM;
  44. }
  45. for (i = 0; i < ARRAY_SIZE(batadv_socket_client_hash); i++) {
  46. if (!batadv_socket_client_hash[i]) {
  47. batadv_socket_client_hash[i] = socket_client;
  48. break;
  49. }
  50. }
  51. if (i == ARRAY_SIZE(batadv_socket_client_hash)) {
  52. pr_err("Error - can't add another packet client: maximum number of clients reached\n");
  53. kfree(socket_client);
  54. module_put(THIS_MODULE);
  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. return 0;
  65. }
  66. static int batadv_socket_release(struct inode *inode, struct file *file)
  67. {
  68. struct batadv_socket_client *socket_client = file->private_data;
  69. struct batadv_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 batadv_socket_packet, list);
  76. list_del(list_pos);
  77. kfree(socket_packet);
  78. }
  79. batadv_socket_client_hash[socket_client->index] = NULL;
  80. spin_unlock_bh(&socket_client->lock);
  81. kfree(socket_client);
  82. module_put(THIS_MODULE);
  83. return 0;
  84. }
  85. static ssize_t batadv_socket_read(struct file *file, char __user *buf,
  86. size_t count, loff_t *ppos)
  87. {
  88. struct batadv_socket_client *socket_client = file->private_data;
  89. struct batadv_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 batadv_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 batadv_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 batadv_socket_write(struct file *file, const char __user *buff,
  116. size_t len, loff_t *off)
  117. {
  118. struct batadv_socket_client *socket_client = file->private_data;
  119. struct batadv_priv *bat_priv = socket_client->bat_priv;
  120. struct batadv_hard_iface *primary_if = NULL;
  121. struct sk_buff *skb;
  122. struct batadv_icmp_packet_rr *icmp_packet_rr;
  123. struct batadv_icmp_header *icmp_header;
  124. struct batadv_orig_node *orig_node = NULL;
  125. struct batadv_neigh_node *neigh_node = NULL;
  126. size_t packet_len = sizeof(struct batadv_icmp_packet);
  127. uint8_t *addr;
  128. if (len < sizeof(struct batadv_icmp_header)) {
  129. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  130. "Error - can't send packet from char device: invalid packet size\n");
  131. return -EINVAL;
  132. }
  133. primary_if = batadv_primary_if_get_selected(bat_priv);
  134. if (!primary_if) {
  135. len = -EFAULT;
  136. goto out;
  137. }
  138. if (len >= BATADV_ICMP_MAX_PACKET_SIZE)
  139. packet_len = BATADV_ICMP_MAX_PACKET_SIZE;
  140. else
  141. packet_len = len;
  142. skb = netdev_alloc_skb_ip_align(NULL, packet_len + ETH_HLEN);
  143. if (!skb) {
  144. len = -ENOMEM;
  145. goto out;
  146. }
  147. skb->priority = TC_PRIO_CONTROL;
  148. skb_reserve(skb, ETH_HLEN);
  149. icmp_header = (struct batadv_icmp_header *)skb_put(skb, packet_len);
  150. if (copy_from_user(icmp_header, buff, packet_len)) {
  151. len = -EFAULT;
  152. goto free_skb;
  153. }
  154. if (icmp_header->packet_type != BATADV_ICMP) {
  155. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  156. "Error - can't send packet from char device: got bogus packet type (expected: BAT_ICMP)\n");
  157. len = -EINVAL;
  158. goto free_skb;
  159. }
  160. switch (icmp_header->msg_type) {
  161. case BATADV_ECHO_REQUEST:
  162. if (len < sizeof(struct batadv_icmp_packet)) {
  163. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  164. "Error - can't send packet from char device: invalid packet size\n");
  165. len = -EINVAL;
  166. goto free_skb;
  167. }
  168. if (atomic_read(&bat_priv->mesh_state) != BATADV_MESH_ACTIVE)
  169. goto dst_unreach;
  170. orig_node = batadv_orig_hash_find(bat_priv, icmp_header->dst);
  171. if (!orig_node)
  172. goto dst_unreach;
  173. neigh_node = batadv_orig_router_get(orig_node,
  174. BATADV_IF_DEFAULT);
  175. if (!neigh_node)
  176. goto dst_unreach;
  177. if (!neigh_node->if_incoming)
  178. goto dst_unreach;
  179. if (neigh_node->if_incoming->if_status != BATADV_IF_ACTIVE)
  180. goto dst_unreach;
  181. icmp_packet_rr = (struct batadv_icmp_packet_rr *)icmp_header;
  182. if (packet_len == sizeof(*icmp_packet_rr)) {
  183. addr = neigh_node->if_incoming->net_dev->dev_addr;
  184. ether_addr_copy(icmp_packet_rr->rr[0], addr);
  185. }
  186. break;
  187. default:
  188. batadv_dbg(BATADV_DBG_BATMAN, bat_priv,
  189. "Error - can't send packet from char device: got unknown message type\n");
  190. len = -EINVAL;
  191. goto free_skb;
  192. }
  193. icmp_header->uid = socket_client->index;
  194. if (icmp_header->version != BATADV_COMPAT_VERSION) {
  195. icmp_header->msg_type = BATADV_PARAMETER_PROBLEM;
  196. icmp_header->version = BATADV_COMPAT_VERSION;
  197. batadv_socket_add_packet(socket_client, icmp_header,
  198. packet_len);
  199. goto free_skb;
  200. }
  201. ether_addr_copy(icmp_header->orig, primary_if->net_dev->dev_addr);
  202. batadv_send_skb_packet(skb, neigh_node->if_incoming, neigh_node->addr);
  203. goto out;
  204. dst_unreach:
  205. icmp_header->msg_type = BATADV_DESTINATION_UNREACHABLE;
  206. batadv_socket_add_packet(socket_client, icmp_header, packet_len);
  207. free_skb:
  208. kfree_skb(skb);
  209. out:
  210. if (primary_if)
  211. batadv_hardif_free_ref(primary_if);
  212. if (neigh_node)
  213. batadv_neigh_node_free_ref(neigh_node);
  214. if (orig_node)
  215. batadv_orig_node_free_ref(orig_node);
  216. return len;
  217. }
  218. static unsigned int batadv_socket_poll(struct file *file, poll_table *wait)
  219. {
  220. struct batadv_socket_client *socket_client = file->private_data;
  221. poll_wait(file, &socket_client->queue_wait, wait);
  222. if (socket_client->queue_len > 0)
  223. return POLLIN | POLLRDNORM;
  224. return 0;
  225. }
  226. static const struct file_operations batadv_fops = {
  227. .owner = THIS_MODULE,
  228. .open = batadv_socket_open,
  229. .release = batadv_socket_release,
  230. .read = batadv_socket_read,
  231. .write = batadv_socket_write,
  232. .poll = batadv_socket_poll,
  233. .llseek = no_llseek,
  234. };
  235. int batadv_socket_setup(struct batadv_priv *bat_priv)
  236. {
  237. struct dentry *d;
  238. if (!bat_priv->debug_dir)
  239. goto err;
  240. d = debugfs_create_file(BATADV_ICMP_SOCKET, S_IFREG | S_IWUSR | S_IRUSR,
  241. bat_priv->debug_dir, bat_priv, &batadv_fops);
  242. if (!d)
  243. goto err;
  244. return 0;
  245. err:
  246. return -ENOMEM;
  247. }
  248. /**
  249. * batadv_socket_receive_packet - schedule an icmp packet to be sent to userspace
  250. * on an icmp socket.
  251. * @socket_client: the socket this packet belongs to
  252. * @icmph: pointer to the header of the icmp packet
  253. * @icmp_len: total length of the icmp packet
  254. */
  255. static void batadv_socket_add_packet(struct batadv_socket_client *socket_client,
  256. struct batadv_icmp_header *icmph,
  257. size_t icmp_len)
  258. {
  259. struct batadv_socket_packet *socket_packet;
  260. size_t len;
  261. socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);
  262. if (!socket_packet)
  263. return;
  264. len = icmp_len;
  265. /* check the maximum length before filling the buffer */
  266. if (len > sizeof(socket_packet->icmp_packet))
  267. len = sizeof(socket_packet->icmp_packet);
  268. INIT_LIST_HEAD(&socket_packet->list);
  269. memcpy(&socket_packet->icmp_packet, icmph, len);
  270. socket_packet->icmp_len = len;
  271. spin_lock_bh(&socket_client->lock);
  272. /* while waiting for the lock the socket_client could have been
  273. * deleted
  274. */
  275. if (!batadv_socket_client_hash[icmph->uid]) {
  276. spin_unlock_bh(&socket_client->lock);
  277. kfree(socket_packet);
  278. return;
  279. }
  280. list_add_tail(&socket_packet->list, &socket_client->queue_list);
  281. socket_client->queue_len++;
  282. if (socket_client->queue_len > 100) {
  283. socket_packet = list_first_entry(&socket_client->queue_list,
  284. struct batadv_socket_packet,
  285. list);
  286. list_del(&socket_packet->list);
  287. kfree(socket_packet);
  288. socket_client->queue_len--;
  289. }
  290. spin_unlock_bh(&socket_client->lock);
  291. wake_up(&socket_client->queue_wait);
  292. }
  293. /**
  294. * batadv_socket_receive_packet - schedule an icmp packet to be received
  295. * locally and sent to userspace.
  296. * @icmph: pointer to the header of the icmp packet
  297. * @icmp_len: total length of the icmp packet
  298. */
  299. void batadv_socket_receive_packet(struct batadv_icmp_header *icmph,
  300. size_t icmp_len)
  301. {
  302. struct batadv_socket_client *hash;
  303. hash = batadv_socket_client_hash[icmph->uid];
  304. if (hash)
  305. batadv_socket_add_packet(hash, icmph, icmp_len);
  306. }