tcp_probe.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 ", now.tv_sec, now.tv_usec);
  61. len += vscnprintf(tbuf+len, sizeof(tbuf)-len, fmt, args);
  62. va_end(args);
  63. kfifo_put(tcpw.fifo, tbuf, len);
  64. wake_up(&tcpw.wait);
  65. }
  66. static int jtcp_sendmsg(struct kiocb *iocb, struct sock *sk,
  67. struct msghdr *msg, size_t size)
  68. {
  69. const struct tcp_sock *tp = tcp_sk(sk);
  70. const struct inet_sock *inet = inet_sk(sk);
  71. if (port == 0 || ntohs(inet->dport) == port ||
  72. ntohs(inet->sport) == port) {
  73. printl("%d.%d.%d.%d:%u %d.%d.%d.%d:%u %d %#x %#x %u %u %u\n",
  74. NIPQUAD(inet->saddr), ntohs(inet->sport),
  75. NIPQUAD(inet->daddr), ntohs(inet->dport),
  76. size, tp->snd_nxt, tp->snd_una,
  77. tp->snd_cwnd, tcp_current_ssthresh(sk),
  78. tp->snd_wnd);
  79. }
  80. jprobe_return();
  81. return 0;
  82. }
  83. static struct jprobe tcp_send_probe = {
  84. .kp = { .addr = (kprobe_opcode_t *) &tcp_sendmsg, },
  85. .entry = (kprobe_opcode_t *) &jtcp_sendmsg,
  86. };
  87. static int tcpprobe_open(struct inode * inode, struct file * file)
  88. {
  89. kfifo_reset(tcpw.fifo);
  90. do_gettimeofday(&tcpw.tstart);
  91. return 0;
  92. }
  93. static ssize_t tcpprobe_read(struct file *file, char __user *buf,
  94. size_t len, loff_t *ppos)
  95. {
  96. int error = 0, cnt;
  97. unsigned char *tbuf;
  98. if (!buf || len < 0)
  99. return -EINVAL;
  100. if (len == 0)
  101. return 0;
  102. tbuf = vmalloc(len);
  103. if (!tbuf)
  104. return -ENOMEM;
  105. error = wait_event_interruptible(tcpw.wait,
  106. __kfifo_len(tcpw.fifo) != 0);
  107. if (error)
  108. return error;
  109. cnt = kfifo_get(tcpw.fifo, tbuf, len);
  110. error = copy_to_user(buf, tbuf, cnt);
  111. vfree(tbuf);
  112. return error ? error : cnt;
  113. }
  114. static struct file_operations tcpprobe_fops = {
  115. .owner = THIS_MODULE,
  116. .open = tcpprobe_open,
  117. .read = tcpprobe_read,
  118. };
  119. static __init int tcpprobe_init(void)
  120. {
  121. int ret = -ENOMEM;
  122. init_waitqueue_head(&tcpw.wait);
  123. spin_lock_init(&tcpw.lock);
  124. tcpw.fifo = kfifo_alloc(bufsize, GFP_KERNEL, &tcpw.lock);
  125. if (!proc_net_fops_create(procname, S_IRUSR, &tcpprobe_fops))
  126. goto err0;
  127. ret = register_jprobe(&tcp_send_probe);
  128. if (ret)
  129. goto err1;
  130. pr_info("TCP watch registered (port=%d)\n", port);
  131. return 0;
  132. err1:
  133. proc_net_remove(procname);
  134. err0:
  135. kfifo_free(tcpw.fifo);
  136. return ret;
  137. }
  138. module_init(tcpprobe_init);
  139. static __exit void tcpprobe_exit(void)
  140. {
  141. kfifo_free(tcpw.fifo);
  142. proc_net_remove(procname);
  143. unregister_jprobe(&tcp_send_probe);
  144. }
  145. module_exit(tcpprobe_exit);