probe.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * dccp_probe - Observe the DCCP flow with kprobes.
  3. *
  4. * The idea for this came from Werner Almesberger's umlsim
  5. * Copyright (C) 2004, Stephen Hemminger <shemminger@osdl.org>
  6. *
  7. * Modified for DCCP from Stephen Hemminger's code
  8. * Copyright (C) 2006, Ian McDonald <ian.mcdonald@jandi.co.nz>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program; if not, write to the Free Software
  22. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  23. */
  24. #include <linux/kernel.h>
  25. #include <linux/kprobes.h>
  26. #include <linux/socket.h>
  27. #include <linux/dccp.h>
  28. #include <linux/proc_fs.h>
  29. #include <linux/module.h>
  30. #include <linux/kfifo.h>
  31. #include <linux/vmalloc.h>
  32. #include <linux/time64.h>
  33. #include <linux/gfp.h>
  34. #include <net/net_namespace.h>
  35. #include "dccp.h"
  36. #include "ccid.h"
  37. #include "ccids/ccid3.h"
  38. static int port;
  39. static int bufsize = 64 * 1024;
  40. static const char procname[] = "dccpprobe";
  41. static struct {
  42. struct kfifo fifo;
  43. spinlock_t lock;
  44. wait_queue_head_t wait;
  45. struct timespec64 tstart;
  46. } dccpw;
  47. static void printl(const char *fmt, ...)
  48. {
  49. va_list args;
  50. int len;
  51. struct timespec64 now;
  52. char tbuf[256];
  53. va_start(args, fmt);
  54. getnstimeofday64(&now);
  55. now = timespec64_sub(now, dccpw.tstart);
  56. len = sprintf(tbuf, "%lu.%06lu ",
  57. (unsigned long) now.tv_sec,
  58. (unsigned long) now.tv_nsec / NSEC_PER_USEC);
  59. len += vscnprintf(tbuf+len, sizeof(tbuf)-len, fmt, args);
  60. va_end(args);
  61. kfifo_in_locked(&dccpw.fifo, tbuf, len, &dccpw.lock);
  62. wake_up(&dccpw.wait);
  63. }
  64. static int jdccp_sendmsg(struct sock *sk, struct msghdr *msg, size_t size)
  65. {
  66. const struct inet_sock *inet = inet_sk(sk);
  67. struct ccid3_hc_tx_sock *hc = NULL;
  68. if (ccid_get_current_tx_ccid(dccp_sk(sk)) == DCCPC_CCID3)
  69. hc = ccid3_hc_tx_sk(sk);
  70. if (port == 0 || ntohs(inet->inet_dport) == port ||
  71. ntohs(inet->inet_sport) == port) {
  72. if (hc)
  73. printl("%pI4:%u %pI4:%u %d %d %d %d %u %llu %llu %d\n",
  74. &inet->inet_saddr, ntohs(inet->inet_sport),
  75. &inet->inet_daddr, ntohs(inet->inet_dport), size,
  76. hc->tx_s, hc->tx_rtt, hc->tx_p,
  77. hc->tx_x_calc, hc->tx_x_recv >> 6,
  78. hc->tx_x >> 6, hc->tx_t_ipi);
  79. else
  80. printl("%pI4:%u %pI4:%u %d\n",
  81. &inet->inet_saddr, ntohs(inet->inet_sport),
  82. &inet->inet_daddr, ntohs(inet->inet_dport),
  83. size);
  84. }
  85. jprobe_return();
  86. return 0;
  87. }
  88. static struct jprobe dccp_send_probe = {
  89. .kp = {
  90. .symbol_name = "dccp_sendmsg",
  91. },
  92. .entry = jdccp_sendmsg,
  93. };
  94. static int dccpprobe_open(struct inode *inode, struct file *file)
  95. {
  96. kfifo_reset(&dccpw.fifo);
  97. getnstimeofday64(&dccpw.tstart);
  98. return 0;
  99. }
  100. static ssize_t dccpprobe_read(struct file *file, char __user *buf,
  101. size_t len, loff_t *ppos)
  102. {
  103. int error = 0, cnt = 0;
  104. unsigned char *tbuf;
  105. if (!buf)
  106. return -EINVAL;
  107. if (len == 0)
  108. return 0;
  109. tbuf = vmalloc(len);
  110. if (!tbuf)
  111. return -ENOMEM;
  112. error = wait_event_interruptible(dccpw.wait,
  113. kfifo_len(&dccpw.fifo) != 0);
  114. if (error)
  115. goto out_free;
  116. cnt = kfifo_out_locked(&dccpw.fifo, tbuf, len, &dccpw.lock);
  117. error = copy_to_user(buf, tbuf, cnt) ? -EFAULT : 0;
  118. out_free:
  119. vfree(tbuf);
  120. return error ? error : cnt;
  121. }
  122. static const struct file_operations dccpprobe_fops = {
  123. .owner = THIS_MODULE,
  124. .open = dccpprobe_open,
  125. .read = dccpprobe_read,
  126. .llseek = noop_llseek,
  127. };
  128. static __init int dccpprobe_init(void)
  129. {
  130. int ret = -ENOMEM;
  131. init_waitqueue_head(&dccpw.wait);
  132. spin_lock_init(&dccpw.lock);
  133. if (kfifo_alloc(&dccpw.fifo, bufsize, GFP_KERNEL))
  134. return ret;
  135. if (!proc_create(procname, S_IRUSR, init_net.proc_net, &dccpprobe_fops))
  136. goto err0;
  137. ret = register_jprobe(&dccp_send_probe);
  138. if (ret) {
  139. ret = request_module("dccp");
  140. if (!ret)
  141. ret = register_jprobe(&dccp_send_probe);
  142. }
  143. if (ret)
  144. goto err1;
  145. pr_info("DCCP watch registered (port=%d)\n", port);
  146. return 0;
  147. err1:
  148. remove_proc_entry(procname, init_net.proc_net);
  149. err0:
  150. kfifo_free(&dccpw.fifo);
  151. return ret;
  152. }
  153. module_init(dccpprobe_init);
  154. static __exit void dccpprobe_exit(void)
  155. {
  156. kfifo_free(&dccpw.fifo);
  157. remove_proc_entry(procname, init_net.proc_net);
  158. unregister_jprobe(&dccp_send_probe);
  159. }
  160. module_exit(dccpprobe_exit);
  161. MODULE_PARM_DESC(port, "Port to match (0=all)");
  162. module_param(port, int, 0);
  163. MODULE_PARM_DESC(bufsize, "Log buffer size (default 64k)");
  164. module_param(bufsize, int, 0);
  165. MODULE_AUTHOR("Ian McDonald <ian.mcdonald@jandi.co.nz>");
  166. MODULE_DESCRIPTION("DCCP snooper");
  167. MODULE_LICENSE("GPL");