tcp_probe.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. * tcpprobe - Observe the TCP 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. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20. */
  21. #include <linux/kernel.h>
  22. #include <linux/kprobes.h>
  23. #include <linux/socket.h>
  24. #include <linux/tcp.h>
  25. #include <linux/proc_fs.h>
  26. #include <linux/module.h>
  27. #include <linux/kfifo.h>
  28. #include <linux/vmalloc.h>
  29. #include <net/tcp.h>
  30. MODULE_AUTHOR("Stephen Hemminger <shemminger@osdl.org>");
  31. MODULE_DESCRIPTION("TCP cwnd snooper");
  32. MODULE_LICENSE("GPL");
  33. static int port = 0;
  34. MODULE_PARM_DESC(port, "Port to match (0=all)");
  35. module_param(port, int, 0);
  36. static int bufsize = 64*1024;
  37. MODULE_PARM_DESC(bufsize, "Log buffer size (default 64k)");
  38. module_param(bufsize, int, 0);
  39. static const char procname[] = "tcpprobe";
  40. struct {
  41. struct kfifo *fifo;
  42. spinlock_t lock;
  43. wait_queue_head_t wait;
  44. struct timeval tstart;
  45. } tcpw;
  46. static void printl(const char *fmt, ...)
  47. {
  48. va_list args;
  49. int len;
  50. struct timeval now;
  51. char tbuf[256];
  52. va_start(args, fmt);
  53. do_gettimeofday(&now);
  54. now.tv_sec -= tcpw.tstart.tv_sec;
  55. now.tv_usec -= tcpw.tstart.tv_usec;
  56. if (now.tv_usec < 0) {
  57. --now.tv_sec;
  58. now.tv_usec += 1000000;
  59. }
  60. len = sprintf(tbuf, "%lu.%06lu ",
  61. (unsigned long) now.tv_sec,
  62. (unsigned long) now.tv_usec);
  63. len += vscnprintf(tbuf+len, sizeof(tbuf)-len, fmt, args);
  64. va_end(args);
  65. kfifo_put(tcpw.fifo, tbuf, len);
  66. wake_up(&tcpw.wait);
  67. }
  68. static int jtcp_sendmsg(struct kiocb *iocb, struct sock *sk,
  69. struct msghdr *msg, size_t size)
  70. {
  71. const struct tcp_sock *tp = tcp_sk(sk);
  72. const struct inet_sock *inet = inet_sk(sk);
  73. if (port == 0 || ntohs(inet->dport) == port ||
  74. ntohs(inet->sport) == port) {
  75. printl("%d.%d.%d.%d:%u %d.%d.%d.%d:%u %d %#x %#x %u %u %u\n",
  76. NIPQUAD(inet->saddr), ntohs(inet->sport),
  77. NIPQUAD(inet->daddr), ntohs(inet->dport),
  78. size, tp->snd_nxt, tp->snd_una,
  79. tp->snd_cwnd, tcp_current_ssthresh(sk),
  80. tp->snd_wnd);
  81. }
  82. jprobe_return();
  83. return 0;
  84. }
  85. static struct jprobe tcp_send_probe = {
  86. .kp = { .addr = (kprobe_opcode_t *) &tcp_sendmsg, },
  87. .entry = (kprobe_opcode_t *) &jtcp_sendmsg,
  88. };
  89. static int tcpprobe_open(struct inode * inode, struct file * file)
  90. {
  91. kfifo_reset(tcpw.fifo);
  92. do_gettimeofday(&tcpw.tstart);
  93. return 0;
  94. }
  95. static ssize_t tcpprobe_read(struct file *file, char __user *buf,
  96. size_t len, loff_t *ppos)
  97. {
  98. int error = 0, cnt = 0;
  99. unsigned char *tbuf;
  100. if (!buf || len < 0)
  101. return -EINVAL;
  102. if (len == 0)
  103. return 0;
  104. tbuf = vmalloc(len);
  105. if (!tbuf)
  106. return -ENOMEM;
  107. error = wait_event_interruptible(tcpw.wait,
  108. __kfifo_len(tcpw.fifo) != 0);
  109. if (error)
  110. goto out_free;
  111. cnt = kfifo_get(tcpw.fifo, tbuf, len);
  112. error = copy_to_user(buf, tbuf, cnt);
  113. out_free:
  114. vfree(tbuf);
  115. return error ? error : cnt;
  116. }
  117. static struct file_operations tcpprobe_fops = {
  118. .owner = THIS_MODULE,
  119. .open = tcpprobe_open,
  120. .read = tcpprobe_read,
  121. };
  122. static __init int tcpprobe_init(void)
  123. {
  124. int ret = -ENOMEM;
  125. init_waitqueue_head(&tcpw.wait);
  126. spin_lock_init(&tcpw.lock);
  127. tcpw.fifo = kfifo_alloc(bufsize, GFP_KERNEL, &tcpw.lock);
  128. if (!proc_net_fops_create(procname, S_IRUSR, &tcpprobe_fops))
  129. goto err0;
  130. ret = register_jprobe(&tcp_send_probe);
  131. if (ret)
  132. goto err1;
  133. pr_info("TCP watch registered (port=%d)\n", port);
  134. return 0;
  135. err1:
  136. proc_net_remove(procname);
  137. err0:
  138. kfifo_free(tcpw.fifo);
  139. return ret;
  140. }
  141. module_init(tcpprobe_init);
  142. static __exit void tcpprobe_exit(void)
  143. {
  144. kfifo_free(tcpw.fifo);
  145. proc_net_remove(procname);
  146. unregister_jprobe(&tcp_send_probe);
  147. }
  148. module_exit(tcpprobe_exit);