probe.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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_SOFTDEP("pre: sctp");
  38. MODULE_AUTHOR("Wei Yongjun <yjwei@cn.fujitsu.com>");
  39. MODULE_DESCRIPTION("SCTP snooper");
  40. MODULE_LICENSE("GPL");
  41. static int port __read_mostly = 0;
  42. MODULE_PARM_DESC(port, "Port to match (0=all)");
  43. module_param(port, int, 0);
  44. static unsigned int fwmark __read_mostly = 0;
  45. MODULE_PARM_DESC(fwmark, "skb mark to match (0=no mark)");
  46. module_param(fwmark, uint, 0);
  47. static int bufsize __read_mostly = 64 * 1024;
  48. MODULE_PARM_DESC(bufsize, "Log buffer size (default 64k)");
  49. module_param(bufsize, int, 0);
  50. static int full __read_mostly = 1;
  51. MODULE_PARM_DESC(full, "Full log (1=every ack packet received, 0=only cwnd changes)");
  52. module_param(full, int, 0);
  53. static const char procname[] = "sctpprobe";
  54. static struct {
  55. struct kfifo fifo;
  56. spinlock_t lock;
  57. wait_queue_head_t wait;
  58. struct timespec tstart;
  59. } sctpw;
  60. static __printf(1, 2) void printl(const char *fmt, ...)
  61. {
  62. va_list args;
  63. int len;
  64. char tbuf[256];
  65. va_start(args, fmt);
  66. len = vscnprintf(tbuf, sizeof(tbuf), fmt, args);
  67. va_end(args);
  68. kfifo_in_locked(&sctpw.fifo, tbuf, len, &sctpw.lock);
  69. wake_up(&sctpw.wait);
  70. }
  71. static int sctpprobe_open(struct inode *inode, struct file *file)
  72. {
  73. kfifo_reset(&sctpw.fifo);
  74. getnstimeofday(&sctpw.tstart);
  75. return 0;
  76. }
  77. static ssize_t sctpprobe_read(struct file *file, char __user *buf,
  78. size_t len, loff_t *ppos)
  79. {
  80. int error = 0, cnt = 0;
  81. unsigned char *tbuf;
  82. if (!buf)
  83. return -EINVAL;
  84. if (len == 0)
  85. return 0;
  86. tbuf = vmalloc(len);
  87. if (!tbuf)
  88. return -ENOMEM;
  89. error = wait_event_interruptible(sctpw.wait,
  90. kfifo_len(&sctpw.fifo) != 0);
  91. if (error)
  92. goto out_free;
  93. cnt = kfifo_out_locked(&sctpw.fifo, tbuf, len, &sctpw.lock);
  94. error = copy_to_user(buf, tbuf, cnt) ? -EFAULT : 0;
  95. out_free:
  96. vfree(tbuf);
  97. return error ? error : cnt;
  98. }
  99. static const struct file_operations sctpprobe_fops = {
  100. .owner = THIS_MODULE,
  101. .open = sctpprobe_open,
  102. .read = sctpprobe_read,
  103. .llseek = noop_llseek,
  104. };
  105. static sctp_disposition_t jsctp_sf_eat_sack(struct net *net,
  106. const struct sctp_endpoint *ep,
  107. const struct sctp_association *asoc,
  108. const sctp_subtype_t type,
  109. void *arg,
  110. sctp_cmd_seq_t *commands)
  111. {
  112. struct sctp_chunk *chunk = arg;
  113. struct sk_buff *skb = chunk->skb;
  114. struct sctp_transport *sp;
  115. static __u32 lcwnd = 0;
  116. struct timespec now;
  117. sp = asoc->peer.primary_path;
  118. if (((port == 0 && fwmark == 0) ||
  119. asoc->peer.port == port ||
  120. ep->base.bind_addr.port == port ||
  121. (fwmark > 0 && skb->mark == fwmark)) &&
  122. (full || sp->cwnd != lcwnd)) {
  123. lcwnd = sp->cwnd;
  124. getnstimeofday(&now);
  125. now = timespec_sub(now, sctpw.tstart);
  126. printl("%lu.%06lu ", (unsigned long) now.tv_sec,
  127. (unsigned long) now.tv_nsec / NSEC_PER_USEC);
  128. printl("%p %5d %5d %5d %8d %5d ", asoc,
  129. ep->base.bind_addr.port, asoc->peer.port,
  130. asoc->pathmtu, asoc->peer.rwnd, asoc->unack_data);
  131. list_for_each_entry(sp, &asoc->peer.transport_addr_list,
  132. transports) {
  133. if (sp == asoc->peer.primary_path)
  134. printl("*");
  135. printl("%pISc %2u %8u %8u %8u %8u %8u ",
  136. &sp->ipaddr, sp->state, sp->cwnd, sp->ssthresh,
  137. sp->flight_size, sp->partial_bytes_acked,
  138. sp->pathmtu);
  139. }
  140. printl("\n");
  141. }
  142. jprobe_return();
  143. return 0;
  144. }
  145. static struct jprobe sctp_recv_probe = {
  146. .kp = {
  147. .symbol_name = "sctp_sf_eat_sack_6_2",
  148. },
  149. .entry = jsctp_sf_eat_sack,
  150. };
  151. static __init int sctp_setup_jprobe(void)
  152. {
  153. int ret = register_jprobe(&sctp_recv_probe);
  154. if (ret) {
  155. if (request_module("sctp"))
  156. goto out;
  157. ret = register_jprobe(&sctp_recv_probe);
  158. }
  159. out:
  160. return ret;
  161. }
  162. static __init int sctpprobe_init(void)
  163. {
  164. int ret = -ENOMEM;
  165. /* Warning: if the function signature of sctp_sf_eat_sack_6_2,
  166. * has been changed, you also have to change the signature of
  167. * jsctp_sf_eat_sack, otherwise you end up right here!
  168. */
  169. BUILD_BUG_ON(__same_type(sctp_sf_eat_sack_6_2,
  170. jsctp_sf_eat_sack) == 0);
  171. init_waitqueue_head(&sctpw.wait);
  172. spin_lock_init(&sctpw.lock);
  173. if (kfifo_alloc(&sctpw.fifo, bufsize, GFP_KERNEL))
  174. return ret;
  175. if (!proc_create(procname, S_IRUSR, init_net.proc_net,
  176. &sctpprobe_fops))
  177. goto free_kfifo;
  178. ret = sctp_setup_jprobe();
  179. if (ret)
  180. goto remove_proc;
  181. pr_info("probe registered (port=%d/fwmark=%u) bufsize=%u\n",
  182. port, fwmark, bufsize);
  183. return 0;
  184. remove_proc:
  185. remove_proc_entry(procname, init_net.proc_net);
  186. free_kfifo:
  187. kfifo_free(&sctpw.fifo);
  188. return ret;
  189. }
  190. static __exit void sctpprobe_exit(void)
  191. {
  192. kfifo_free(&sctpw.fifo);
  193. remove_proc_entry(procname, init_net.proc_net);
  194. unregister_jprobe(&sctp_recv_probe);
  195. }
  196. module_init(sctpprobe_init);
  197. module_exit(sctpprobe_exit);