probe.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*
  2. * sctp_probe - Observe the SCTP 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 SCTP from Stephen Hemminger's code
  8. * Copyright (C) 2010, Wei Yongjun <yjwei@cn.fujitsu.com>
  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. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  25. #include <linux/kernel.h>
  26. #include <linux/kprobes.h>
  27. #include <linux/socket.h>
  28. #include <linux/sctp.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/vmalloc.h>
  31. #include <linux/module.h>
  32. #include <linux/kfifo.h>
  33. #include <linux/time.h>
  34. #include <net/net_namespace.h>
  35. #include <net/sctp/sctp.h>
  36. #include <net/sctp/sm.h>
  37. MODULE_AUTHOR("Wei Yongjun <yjwei@cn.fujitsu.com>");
  38. MODULE_DESCRIPTION("SCTP snooper");
  39. MODULE_LICENSE("GPL");
  40. static int port __read_mostly = 0;
  41. MODULE_PARM_DESC(port, "Port to match (0=all)");
  42. module_param(port, int, 0);
  43. static int bufsize __read_mostly = 64 * 1024;
  44. MODULE_PARM_DESC(bufsize, "Log buffer size (default 64k)");
  45. module_param(bufsize, int, 0);
  46. static int full __read_mostly = 1;
  47. MODULE_PARM_DESC(full, "Full log (1=every ack packet received, 0=only cwnd changes)");
  48. module_param(full, int, 0);
  49. static const char procname[] = "sctpprobe";
  50. static struct {
  51. struct kfifo fifo;
  52. spinlock_t lock;
  53. wait_queue_head_t wait;
  54. struct timespec tstart;
  55. } sctpw;
  56. static void printl(const char *fmt, ...)
  57. {
  58. va_list args;
  59. int len;
  60. char tbuf[256];
  61. va_start(args, fmt);
  62. len = vscnprintf(tbuf, sizeof(tbuf), fmt, args);
  63. va_end(args);
  64. kfifo_in_locked(&sctpw.fifo, tbuf, len, &sctpw.lock);
  65. wake_up(&sctpw.wait);
  66. }
  67. static int sctpprobe_open(struct inode *inode, struct file *file)
  68. {
  69. kfifo_reset(&sctpw.fifo);
  70. getnstimeofday(&sctpw.tstart);
  71. return 0;
  72. }
  73. static ssize_t sctpprobe_read(struct file *file, char __user *buf,
  74. size_t len, loff_t *ppos)
  75. {
  76. int error = 0, cnt = 0;
  77. unsigned char *tbuf;
  78. if (!buf)
  79. return -EINVAL;
  80. if (len == 0)
  81. return 0;
  82. tbuf = vmalloc(len);
  83. if (!tbuf)
  84. return -ENOMEM;
  85. error = wait_event_interruptible(sctpw.wait,
  86. kfifo_len(&sctpw.fifo) != 0);
  87. if (error)
  88. goto out_free;
  89. cnt = kfifo_out_locked(&sctpw.fifo, tbuf, len, &sctpw.lock);
  90. error = copy_to_user(buf, tbuf, cnt) ? -EFAULT : 0;
  91. out_free:
  92. vfree(tbuf);
  93. return error ? error : cnt;
  94. }
  95. static const struct file_operations sctpprobe_fops = {
  96. .owner = THIS_MODULE,
  97. .open = sctpprobe_open,
  98. .read = sctpprobe_read,
  99. .llseek = noop_llseek,
  100. };
  101. sctp_disposition_t jsctp_sf_eat_sack(const struct sctp_endpoint *ep,
  102. const struct sctp_association *asoc,
  103. const sctp_subtype_t type,
  104. void *arg,
  105. sctp_cmd_seq_t *commands)
  106. {
  107. struct sctp_transport *sp;
  108. static __u32 lcwnd = 0;
  109. struct timespec now;
  110. sp = asoc->peer.primary_path;
  111. if ((full || sp->cwnd != lcwnd) &&
  112. (!port || asoc->peer.port == port ||
  113. ep->base.bind_addr.port == port)) {
  114. lcwnd = sp->cwnd;
  115. getnstimeofday(&now);
  116. now = timespec_sub(now, sctpw.tstart);
  117. printl("%lu.%06lu ", (unsigned long) now.tv_sec,
  118. (unsigned long) now.tv_nsec / NSEC_PER_USEC);
  119. printl("%p %5d %5d %5d %8d %5d ", asoc,
  120. ep->base.bind_addr.port, asoc->peer.port,
  121. asoc->pathmtu, asoc->peer.rwnd, asoc->unack_data);
  122. list_for_each_entry(sp, &asoc->peer.transport_addr_list,
  123. transports) {
  124. if (sp == asoc->peer.primary_path)
  125. printl("*");
  126. if (sp->ipaddr.sa.sa_family == AF_INET)
  127. printl("%pI4 ", &sp->ipaddr.v4.sin_addr);
  128. else
  129. printl("%pI6 ", &sp->ipaddr.v6.sin6_addr);
  130. printl("%2u %8u %8u %8u %8u %8u ",
  131. sp->state, sp->cwnd, sp->ssthresh,
  132. sp->flight_size, sp->partial_bytes_acked,
  133. sp->pathmtu);
  134. }
  135. printl("\n");
  136. }
  137. jprobe_return();
  138. return 0;
  139. }
  140. static struct jprobe sctp_recv_probe = {
  141. .kp = {
  142. .symbol_name = "sctp_sf_eat_sack_6_2",
  143. },
  144. .entry = jsctp_sf_eat_sack,
  145. };
  146. static __init int sctpprobe_init(void)
  147. {
  148. int ret = -ENOMEM;
  149. init_waitqueue_head(&sctpw.wait);
  150. spin_lock_init(&sctpw.lock);
  151. if (kfifo_alloc(&sctpw.fifo, bufsize, GFP_KERNEL))
  152. return ret;
  153. if (!proc_net_fops_create(&init_net, procname, S_IRUSR,
  154. &sctpprobe_fops))
  155. goto free_kfifo;
  156. ret = register_jprobe(&sctp_recv_probe);
  157. if (ret)
  158. goto remove_proc;
  159. pr_info("probe registered (port=%d)\n", port);
  160. return 0;
  161. remove_proc:
  162. proc_net_remove(&init_net, procname);
  163. free_kfifo:
  164. kfifo_free(&sctpw.fifo);
  165. return ret;
  166. }
  167. static __exit void sctpprobe_exit(void)
  168. {
  169. kfifo_free(&sctpw.fifo);
  170. proc_net_remove(&init_net, procname);
  171. unregister_jprobe(&sctp_recv_probe);
  172. }
  173. module_init(sctpprobe_init);
  174. module_exit(sctpprobe_exit);