proc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  1. /* SCTP kernel implementation
  2. * Copyright (c) 2003 International Business Machines, Corp.
  3. *
  4. * This file is part of the SCTP kernel implementation
  5. *
  6. * This SCTP implementation is free software;
  7. * you can redistribute it and/or modify it under the terms of
  8. * the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This SCTP implementation is distributed in the hope that it
  13. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  14. * ************************
  15. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. * See the GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with GNU CC; see the file COPYING. If not, see
  20. * <http://www.gnu.org/licenses/>.
  21. *
  22. * Please send any bug reports or fixes you make to the
  23. * email address(es):
  24. * lksctp developers <linux-sctp@vger.kernel.org>
  25. *
  26. * Written or modified by:
  27. * Sridhar Samudrala <sri@us.ibm.com>
  28. */
  29. #include <linux/types.h>
  30. #include <linux/seq_file.h>
  31. #include <linux/init.h>
  32. #include <linux/export.h>
  33. #include <net/sctp/sctp.h>
  34. #include <net/ip.h> /* for snmp_fold_field */
  35. static const struct snmp_mib sctp_snmp_list[] = {
  36. SNMP_MIB_ITEM("SctpCurrEstab", SCTP_MIB_CURRESTAB),
  37. SNMP_MIB_ITEM("SctpActiveEstabs", SCTP_MIB_ACTIVEESTABS),
  38. SNMP_MIB_ITEM("SctpPassiveEstabs", SCTP_MIB_PASSIVEESTABS),
  39. SNMP_MIB_ITEM("SctpAborteds", SCTP_MIB_ABORTEDS),
  40. SNMP_MIB_ITEM("SctpShutdowns", SCTP_MIB_SHUTDOWNS),
  41. SNMP_MIB_ITEM("SctpOutOfBlues", SCTP_MIB_OUTOFBLUES),
  42. SNMP_MIB_ITEM("SctpChecksumErrors", SCTP_MIB_CHECKSUMERRORS),
  43. SNMP_MIB_ITEM("SctpOutCtrlChunks", SCTP_MIB_OUTCTRLCHUNKS),
  44. SNMP_MIB_ITEM("SctpOutOrderChunks", SCTP_MIB_OUTORDERCHUNKS),
  45. SNMP_MIB_ITEM("SctpOutUnorderChunks", SCTP_MIB_OUTUNORDERCHUNKS),
  46. SNMP_MIB_ITEM("SctpInCtrlChunks", SCTP_MIB_INCTRLCHUNKS),
  47. SNMP_MIB_ITEM("SctpInOrderChunks", SCTP_MIB_INORDERCHUNKS),
  48. SNMP_MIB_ITEM("SctpInUnorderChunks", SCTP_MIB_INUNORDERCHUNKS),
  49. SNMP_MIB_ITEM("SctpFragUsrMsgs", SCTP_MIB_FRAGUSRMSGS),
  50. SNMP_MIB_ITEM("SctpReasmUsrMsgs", SCTP_MIB_REASMUSRMSGS),
  51. SNMP_MIB_ITEM("SctpOutSCTPPacks", SCTP_MIB_OUTSCTPPACKS),
  52. SNMP_MIB_ITEM("SctpInSCTPPacks", SCTP_MIB_INSCTPPACKS),
  53. SNMP_MIB_ITEM("SctpT1InitExpireds", SCTP_MIB_T1_INIT_EXPIREDS),
  54. SNMP_MIB_ITEM("SctpT1CookieExpireds", SCTP_MIB_T1_COOKIE_EXPIREDS),
  55. SNMP_MIB_ITEM("SctpT2ShutdownExpireds", SCTP_MIB_T2_SHUTDOWN_EXPIREDS),
  56. SNMP_MIB_ITEM("SctpT3RtxExpireds", SCTP_MIB_T3_RTX_EXPIREDS),
  57. SNMP_MIB_ITEM("SctpT4RtoExpireds", SCTP_MIB_T4_RTO_EXPIREDS),
  58. SNMP_MIB_ITEM("SctpT5ShutdownGuardExpireds", SCTP_MIB_T5_SHUTDOWN_GUARD_EXPIREDS),
  59. SNMP_MIB_ITEM("SctpDelaySackExpireds", SCTP_MIB_DELAY_SACK_EXPIREDS),
  60. SNMP_MIB_ITEM("SctpAutocloseExpireds", SCTP_MIB_AUTOCLOSE_EXPIREDS),
  61. SNMP_MIB_ITEM("SctpT3Retransmits", SCTP_MIB_T3_RETRANSMITS),
  62. SNMP_MIB_ITEM("SctpPmtudRetransmits", SCTP_MIB_PMTUD_RETRANSMITS),
  63. SNMP_MIB_ITEM("SctpFastRetransmits", SCTP_MIB_FAST_RETRANSMITS),
  64. SNMP_MIB_ITEM("SctpInPktSoftirq", SCTP_MIB_IN_PKT_SOFTIRQ),
  65. SNMP_MIB_ITEM("SctpInPktBacklog", SCTP_MIB_IN_PKT_BACKLOG),
  66. SNMP_MIB_ITEM("SctpInPktDiscards", SCTP_MIB_IN_PKT_DISCARDS),
  67. SNMP_MIB_ITEM("SctpInDataChunkDiscards", SCTP_MIB_IN_DATA_CHUNK_DISCARDS),
  68. SNMP_MIB_SENTINEL
  69. };
  70. /* Display sctp snmp mib statistics(/proc/net/sctp/snmp). */
  71. static int sctp_snmp_seq_show(struct seq_file *seq, void *v)
  72. {
  73. unsigned long buff[SCTP_MIB_MAX];
  74. struct net *net = seq->private;
  75. int i;
  76. memset(buff, 0, sizeof(unsigned long) * SCTP_MIB_MAX);
  77. snmp_get_cpu_field_batch(buff, sctp_snmp_list,
  78. net->sctp.sctp_statistics);
  79. for (i = 0; sctp_snmp_list[i].name; i++)
  80. seq_printf(seq, "%-32s\t%ld\n", sctp_snmp_list[i].name,
  81. buff[i]);
  82. return 0;
  83. }
  84. /* Initialize the seq file operations for 'snmp' object. */
  85. static int sctp_snmp_seq_open(struct inode *inode, struct file *file)
  86. {
  87. return single_open_net(inode, file, sctp_snmp_seq_show);
  88. }
  89. static const struct file_operations sctp_snmp_seq_fops = {
  90. .open = sctp_snmp_seq_open,
  91. .read = seq_read,
  92. .llseek = seq_lseek,
  93. .release = single_release_net,
  94. };
  95. /* Dump local addresses of an association/endpoint. */
  96. static void sctp_seq_dump_local_addrs(struct seq_file *seq, struct sctp_ep_common *epb)
  97. {
  98. struct sctp_association *asoc;
  99. struct sctp_sockaddr_entry *laddr;
  100. struct sctp_transport *peer;
  101. union sctp_addr *addr, *primary = NULL;
  102. struct sctp_af *af;
  103. if (epb->type == SCTP_EP_TYPE_ASSOCIATION) {
  104. asoc = sctp_assoc(epb);
  105. peer = asoc->peer.primary_path;
  106. if (unlikely(peer == NULL)) {
  107. WARN(1, "Association %p with NULL primary path!\n", asoc);
  108. return;
  109. }
  110. primary = &peer->saddr;
  111. }
  112. rcu_read_lock();
  113. list_for_each_entry_rcu(laddr, &epb->bind_addr.address_list, list) {
  114. if (!laddr->valid)
  115. continue;
  116. addr = &laddr->a;
  117. af = sctp_get_af_specific(addr->sa.sa_family);
  118. if (primary && af->cmp_addr(addr, primary)) {
  119. seq_printf(seq, "*");
  120. }
  121. af->seq_dump_addr(seq, addr);
  122. }
  123. rcu_read_unlock();
  124. }
  125. /* Dump remote addresses of an association. */
  126. static void sctp_seq_dump_remote_addrs(struct seq_file *seq, struct sctp_association *assoc)
  127. {
  128. struct sctp_transport *transport;
  129. union sctp_addr *addr, *primary;
  130. struct sctp_af *af;
  131. primary = &assoc->peer.primary_addr;
  132. list_for_each_entry_rcu(transport, &assoc->peer.transport_addr_list,
  133. transports) {
  134. addr = &transport->ipaddr;
  135. af = sctp_get_af_specific(addr->sa.sa_family);
  136. if (af->cmp_addr(addr, primary)) {
  137. seq_printf(seq, "*");
  138. }
  139. af->seq_dump_addr(seq, addr);
  140. }
  141. }
  142. static void *sctp_eps_seq_start(struct seq_file *seq, loff_t *pos)
  143. {
  144. if (*pos >= sctp_ep_hashsize)
  145. return NULL;
  146. if (*pos < 0)
  147. *pos = 0;
  148. if (*pos == 0)
  149. seq_printf(seq, " ENDPT SOCK STY SST HBKT LPORT UID INODE LADDRS\n");
  150. return (void *)pos;
  151. }
  152. static void sctp_eps_seq_stop(struct seq_file *seq, void *v)
  153. {
  154. }
  155. static void *sctp_eps_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  156. {
  157. if (++*pos >= sctp_ep_hashsize)
  158. return NULL;
  159. return pos;
  160. }
  161. /* Display sctp endpoints (/proc/net/sctp/eps). */
  162. static int sctp_eps_seq_show(struct seq_file *seq, void *v)
  163. {
  164. struct sctp_hashbucket *head;
  165. struct sctp_ep_common *epb;
  166. struct sctp_endpoint *ep;
  167. struct sock *sk;
  168. int hash = *(loff_t *)v;
  169. if (hash >= sctp_ep_hashsize)
  170. return -ENOMEM;
  171. head = &sctp_ep_hashtable[hash];
  172. read_lock_bh(&head->lock);
  173. sctp_for_each_hentry(epb, &head->chain) {
  174. ep = sctp_ep(epb);
  175. sk = epb->sk;
  176. if (!net_eq(sock_net(sk), seq_file_net(seq)))
  177. continue;
  178. seq_printf(seq, "%8pK %8pK %-3d %-3d %-4d %-5d %5u %5lu ", ep, sk,
  179. sctp_sk(sk)->type, sk->sk_state, hash,
  180. epb->bind_addr.port,
  181. from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)),
  182. sock_i_ino(sk));
  183. sctp_seq_dump_local_addrs(seq, epb);
  184. seq_printf(seq, "\n");
  185. }
  186. read_unlock_bh(&head->lock);
  187. return 0;
  188. }
  189. static const struct seq_operations sctp_eps_ops = {
  190. .start = sctp_eps_seq_start,
  191. .next = sctp_eps_seq_next,
  192. .stop = sctp_eps_seq_stop,
  193. .show = sctp_eps_seq_show,
  194. };
  195. /* Initialize the seq file operations for 'eps' object. */
  196. static int sctp_eps_seq_open(struct inode *inode, struct file *file)
  197. {
  198. return seq_open_net(inode, file, &sctp_eps_ops,
  199. sizeof(struct seq_net_private));
  200. }
  201. static const struct file_operations sctp_eps_seq_fops = {
  202. .open = sctp_eps_seq_open,
  203. .read = seq_read,
  204. .llseek = seq_lseek,
  205. .release = seq_release_net,
  206. };
  207. struct sctp_ht_iter {
  208. struct seq_net_private p;
  209. struct rhashtable_iter hti;
  210. int start_fail;
  211. };
  212. static void *sctp_transport_seq_start(struct seq_file *seq, loff_t *pos)
  213. {
  214. struct sctp_ht_iter *iter = seq->private;
  215. sctp_transport_walk_start(&iter->hti);
  216. iter->start_fail = 0;
  217. return sctp_transport_get_idx(seq_file_net(seq), &iter->hti, *pos);
  218. }
  219. static void sctp_transport_seq_stop(struct seq_file *seq, void *v)
  220. {
  221. struct sctp_ht_iter *iter = seq->private;
  222. if (iter->start_fail)
  223. return;
  224. sctp_transport_walk_stop(&iter->hti);
  225. }
  226. static void *sctp_transport_seq_next(struct seq_file *seq, void *v, loff_t *pos)
  227. {
  228. struct sctp_ht_iter *iter = seq->private;
  229. ++*pos;
  230. return sctp_transport_get_next(seq_file_net(seq), &iter->hti);
  231. }
  232. /* Display sctp associations (/proc/net/sctp/assocs). */
  233. static int sctp_assocs_seq_show(struct seq_file *seq, void *v)
  234. {
  235. struct sctp_transport *transport;
  236. struct sctp_association *assoc;
  237. struct sctp_ep_common *epb;
  238. struct sock *sk;
  239. if (v == SEQ_START_TOKEN) {
  240. seq_printf(seq, " ASSOC SOCK STY SST ST HBKT "
  241. "ASSOC-ID TX_QUEUE RX_QUEUE UID INODE LPORT "
  242. "RPORT LADDRS <-> RADDRS "
  243. "HBINT INS OUTS MAXRT T1X T2X RTXC "
  244. "wmema wmemq sndbuf rcvbuf\n");
  245. return 0;
  246. }
  247. transport = (struct sctp_transport *)v;
  248. if (!sctp_transport_hold(transport))
  249. return 0;
  250. assoc = transport->asoc;
  251. epb = &assoc->base;
  252. sk = epb->sk;
  253. seq_printf(seq,
  254. "%8pK %8pK %-3d %-3d %-2d %-4d "
  255. "%4d %8d %8d %7u %5lu %-5d %5d ",
  256. assoc, sk, sctp_sk(sk)->type, sk->sk_state,
  257. assoc->state, 0,
  258. assoc->assoc_id,
  259. assoc->sndbuf_used,
  260. atomic_read(&assoc->rmem_alloc),
  261. from_kuid_munged(seq_user_ns(seq), sock_i_uid(sk)),
  262. sock_i_ino(sk),
  263. epb->bind_addr.port,
  264. assoc->peer.port);
  265. seq_printf(seq, " ");
  266. sctp_seq_dump_local_addrs(seq, epb);
  267. seq_printf(seq, "<-> ");
  268. sctp_seq_dump_remote_addrs(seq, assoc);
  269. seq_printf(seq, "\t%8lu %5d %5d %4d %4d %4d %8d "
  270. "%8d %8d %8d %8d",
  271. assoc->hbinterval, assoc->stream.incnt,
  272. assoc->stream.outcnt, assoc->max_retrans,
  273. assoc->init_retries, assoc->shutdown_retries,
  274. assoc->rtx_data_chunks,
  275. refcount_read(&sk->sk_wmem_alloc),
  276. sk->sk_wmem_queued,
  277. sk->sk_sndbuf,
  278. sk->sk_rcvbuf);
  279. seq_printf(seq, "\n");
  280. sctp_transport_put(transport);
  281. return 0;
  282. }
  283. static const struct seq_operations sctp_assoc_ops = {
  284. .start = sctp_transport_seq_start,
  285. .next = sctp_transport_seq_next,
  286. .stop = sctp_transport_seq_stop,
  287. .show = sctp_assocs_seq_show,
  288. };
  289. /* Initialize the seq file operations for 'assocs' object. */
  290. static int sctp_assocs_seq_open(struct inode *inode, struct file *file)
  291. {
  292. return seq_open_net(inode, file, &sctp_assoc_ops,
  293. sizeof(struct sctp_ht_iter));
  294. }
  295. static const struct file_operations sctp_assocs_seq_fops = {
  296. .open = sctp_assocs_seq_open,
  297. .read = seq_read,
  298. .llseek = seq_lseek,
  299. .release = seq_release_net,
  300. };
  301. static int sctp_remaddr_seq_show(struct seq_file *seq, void *v)
  302. {
  303. struct sctp_association *assoc;
  304. struct sctp_transport *transport, *tsp;
  305. if (v == SEQ_START_TOKEN) {
  306. seq_printf(seq, "ADDR ASSOC_ID HB_ACT RTO MAX_PATH_RTX "
  307. "REM_ADDR_RTX START STATE\n");
  308. return 0;
  309. }
  310. transport = (struct sctp_transport *)v;
  311. if (!sctp_transport_hold(transport))
  312. return 0;
  313. assoc = transport->asoc;
  314. list_for_each_entry_rcu(tsp, &assoc->peer.transport_addr_list,
  315. transports) {
  316. /*
  317. * The remote address (ADDR)
  318. */
  319. tsp->af_specific->seq_dump_addr(seq, &tsp->ipaddr);
  320. seq_printf(seq, " ");
  321. /*
  322. * The association ID (ASSOC_ID)
  323. */
  324. seq_printf(seq, "%d ", tsp->asoc->assoc_id);
  325. /*
  326. * If the Heartbeat is active (HB_ACT)
  327. * Note: 1 = Active, 0 = Inactive
  328. */
  329. seq_printf(seq, "%d ", timer_pending(&tsp->hb_timer));
  330. /*
  331. * Retransmit time out (RTO)
  332. */
  333. seq_printf(seq, "%lu ", tsp->rto);
  334. /*
  335. * Maximum path retransmit count (PATH_MAX_RTX)
  336. */
  337. seq_printf(seq, "%d ", tsp->pathmaxrxt);
  338. /*
  339. * remote address retransmit count (REM_ADDR_RTX)
  340. * Note: We don't have a way to tally this at the moment
  341. * so lets just leave it as zero for the moment
  342. */
  343. seq_puts(seq, "0 ");
  344. /*
  345. * remote address start time (START). This is also not
  346. * currently implemented, but we can record it with a
  347. * jiffies marker in a subsequent patch
  348. */
  349. seq_puts(seq, "0 ");
  350. /*
  351. * The current state of this destination. I.e.
  352. * SCTP_ACTIVE, SCTP_INACTIVE, ...
  353. */
  354. seq_printf(seq, "%d", tsp->state);
  355. seq_printf(seq, "\n");
  356. }
  357. sctp_transport_put(transport);
  358. return 0;
  359. }
  360. static const struct seq_operations sctp_remaddr_ops = {
  361. .start = sctp_transport_seq_start,
  362. .next = sctp_transport_seq_next,
  363. .stop = sctp_transport_seq_stop,
  364. .show = sctp_remaddr_seq_show,
  365. };
  366. static int sctp_remaddr_seq_open(struct inode *inode, struct file *file)
  367. {
  368. return seq_open_net(inode, file, &sctp_remaddr_ops,
  369. sizeof(struct sctp_ht_iter));
  370. }
  371. static const struct file_operations sctp_remaddr_seq_fops = {
  372. .open = sctp_remaddr_seq_open,
  373. .read = seq_read,
  374. .llseek = seq_lseek,
  375. .release = seq_release_net,
  376. };
  377. /* Set up the proc fs entry for the SCTP protocol. */
  378. int __net_init sctp_proc_init(struct net *net)
  379. {
  380. net->sctp.proc_net_sctp = proc_net_mkdir(net, "sctp", net->proc_net);
  381. if (!net->sctp.proc_net_sctp)
  382. return -ENOMEM;
  383. if (!proc_create("snmp", 0444, net->sctp.proc_net_sctp,
  384. &sctp_snmp_seq_fops))
  385. goto cleanup;
  386. if (!proc_create("eps", 0444, net->sctp.proc_net_sctp,
  387. &sctp_eps_seq_fops))
  388. goto cleanup;
  389. if (!proc_create("assocs", 0444, net->sctp.proc_net_sctp,
  390. &sctp_assocs_seq_fops))
  391. goto cleanup;
  392. if (!proc_create("remaddr", 0444, net->sctp.proc_net_sctp,
  393. &sctp_remaddr_seq_fops))
  394. goto cleanup;
  395. return 0;
  396. cleanup:
  397. remove_proc_subtree("sctp", net->proc_net);
  398. net->sctp.proc_net_sctp = NULL;
  399. return -ENOMEM;
  400. }