diag.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555
  1. /* SCTP kernel implementation
  2. * (C) Copyright Red Hat Inc. 2017
  3. *
  4. * This file is part of the SCTP kernel implementation
  5. *
  6. * These functions implement sctp diag support.
  7. *
  8. * This SCTP implementation is free software;
  9. * you can redistribute it and/or modify it under the terms of
  10. * the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2, or (at your option)
  12. * any later version.
  13. *
  14. * This SCTP implementation is distributed in the hope that it
  15. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  16. * ************************
  17. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  18. * See the GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with GNU CC; see the file COPYING. If not, see
  22. * <http://www.gnu.org/licenses/>.
  23. *
  24. * Please send any bug reports or fixes you make to the
  25. * email addresched(es):
  26. * lksctp developers <linux-sctp@vger.kernel.org>
  27. *
  28. * Written or modified by:
  29. * Xin Long <lucien.xin@gmail.com>
  30. */
  31. #include <linux/module.h>
  32. #include <linux/inet_diag.h>
  33. #include <linux/sock_diag.h>
  34. #include <net/sctp/sctp.h>
  35. static void sctp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
  36. void *info);
  37. /* define some functions to make asoc/ep fill look clean */
  38. static void inet_diag_msg_sctpasoc_fill(struct inet_diag_msg *r,
  39. struct sock *sk,
  40. struct sctp_association *asoc)
  41. {
  42. union sctp_addr laddr, paddr;
  43. struct dst_entry *dst;
  44. struct timer_list *t3_rtx = &asoc->peer.primary_path->T3_rtx_timer;
  45. laddr = list_entry(asoc->base.bind_addr.address_list.next,
  46. struct sctp_sockaddr_entry, list)->a;
  47. paddr = asoc->peer.primary_path->ipaddr;
  48. dst = asoc->peer.primary_path->dst;
  49. r->idiag_family = sk->sk_family;
  50. r->id.idiag_sport = htons(asoc->base.bind_addr.port);
  51. r->id.idiag_dport = htons(asoc->peer.port);
  52. r->id.idiag_if = dst ? dst->dev->ifindex : 0;
  53. sock_diag_save_cookie(sk, r->id.idiag_cookie);
  54. #if IS_ENABLED(CONFIG_IPV6)
  55. if (sk->sk_family == AF_INET6) {
  56. *(struct in6_addr *)r->id.idiag_src = laddr.v6.sin6_addr;
  57. *(struct in6_addr *)r->id.idiag_dst = paddr.v6.sin6_addr;
  58. } else
  59. #endif
  60. {
  61. memset(&r->id.idiag_src, 0, sizeof(r->id.idiag_src));
  62. memset(&r->id.idiag_dst, 0, sizeof(r->id.idiag_dst));
  63. r->id.idiag_src[0] = laddr.v4.sin_addr.s_addr;
  64. r->id.idiag_dst[0] = paddr.v4.sin_addr.s_addr;
  65. }
  66. r->idiag_state = asoc->state;
  67. if (timer_pending(t3_rtx)) {
  68. r->idiag_timer = SCTP_EVENT_TIMEOUT_T3_RTX;
  69. r->idiag_retrans = asoc->rtx_data_chunks;
  70. r->idiag_expires = jiffies_to_msecs(t3_rtx->expires - jiffies);
  71. } else {
  72. r->idiag_timer = 0;
  73. r->idiag_retrans = 0;
  74. r->idiag_expires = 0;
  75. }
  76. }
  77. static int inet_diag_msg_sctpladdrs_fill(struct sk_buff *skb,
  78. struct list_head *address_list)
  79. {
  80. struct sctp_sockaddr_entry *laddr;
  81. int addrlen = sizeof(struct sockaddr_storage);
  82. int addrcnt = 0;
  83. struct nlattr *attr;
  84. void *info = NULL;
  85. list_for_each_entry_rcu(laddr, address_list, list)
  86. addrcnt++;
  87. attr = nla_reserve(skb, INET_DIAG_LOCALS, addrlen * addrcnt);
  88. if (!attr)
  89. return -EMSGSIZE;
  90. info = nla_data(attr);
  91. list_for_each_entry_rcu(laddr, address_list, list) {
  92. memcpy(info, &laddr->a, sizeof(laddr->a));
  93. memset(info + sizeof(laddr->a), 0, addrlen - sizeof(laddr->a));
  94. info += addrlen;
  95. }
  96. return 0;
  97. }
  98. static int inet_diag_msg_sctpaddrs_fill(struct sk_buff *skb,
  99. struct sctp_association *asoc)
  100. {
  101. int addrlen = sizeof(struct sockaddr_storage);
  102. struct sctp_transport *from;
  103. struct nlattr *attr;
  104. void *info = NULL;
  105. attr = nla_reserve(skb, INET_DIAG_PEERS,
  106. addrlen * asoc->peer.transport_count);
  107. if (!attr)
  108. return -EMSGSIZE;
  109. info = nla_data(attr);
  110. list_for_each_entry(from, &asoc->peer.transport_addr_list,
  111. transports) {
  112. memcpy(info, &from->ipaddr, sizeof(from->ipaddr));
  113. memset(info + sizeof(from->ipaddr), 0,
  114. addrlen - sizeof(from->ipaddr));
  115. info += addrlen;
  116. }
  117. return 0;
  118. }
  119. /* sctp asoc/ep fill*/
  120. static int inet_sctp_diag_fill(struct sock *sk, struct sctp_association *asoc,
  121. struct sk_buff *skb,
  122. const struct inet_diag_req_v2 *req,
  123. struct user_namespace *user_ns,
  124. int portid, u32 seq, u16 nlmsg_flags,
  125. const struct nlmsghdr *unlh,
  126. bool net_admin)
  127. {
  128. struct sctp_endpoint *ep = sctp_sk(sk)->ep;
  129. struct list_head *addr_list;
  130. struct inet_diag_msg *r;
  131. struct nlmsghdr *nlh;
  132. int ext = req->idiag_ext;
  133. struct sctp_infox infox;
  134. void *info = NULL;
  135. nlh = nlmsg_put(skb, portid, seq, unlh->nlmsg_type, sizeof(*r),
  136. nlmsg_flags);
  137. if (!nlh)
  138. return -EMSGSIZE;
  139. r = nlmsg_data(nlh);
  140. BUG_ON(!sk_fullsock(sk));
  141. if (asoc) {
  142. inet_diag_msg_sctpasoc_fill(r, sk, asoc);
  143. } else {
  144. inet_diag_msg_common_fill(r, sk);
  145. r->idiag_state = sk->sk_state;
  146. r->idiag_timer = 0;
  147. r->idiag_retrans = 0;
  148. }
  149. if (inet_diag_msg_attrs_fill(sk, skb, r, ext, user_ns, net_admin))
  150. goto errout;
  151. if (ext & (1 << (INET_DIAG_SKMEMINFO - 1))) {
  152. u32 mem[SK_MEMINFO_VARS];
  153. int amt;
  154. if (asoc && asoc->ep->sndbuf_policy)
  155. amt = asoc->sndbuf_used;
  156. else
  157. amt = sk_wmem_alloc_get(sk);
  158. mem[SK_MEMINFO_WMEM_ALLOC] = amt;
  159. if (asoc && asoc->ep->rcvbuf_policy)
  160. amt = atomic_read(&asoc->rmem_alloc);
  161. else
  162. amt = sk_rmem_alloc_get(sk);
  163. mem[SK_MEMINFO_RMEM_ALLOC] = amt;
  164. mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf;
  165. mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf;
  166. mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc;
  167. mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued;
  168. mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc);
  169. mem[SK_MEMINFO_BACKLOG] = sk->sk_backlog.len;
  170. mem[SK_MEMINFO_DROPS] = atomic_read(&sk->sk_drops);
  171. if (nla_put(skb, INET_DIAG_SKMEMINFO, sizeof(mem), &mem) < 0)
  172. goto errout;
  173. }
  174. if (ext & (1 << (INET_DIAG_INFO - 1))) {
  175. struct nlattr *attr;
  176. attr = nla_reserve_64bit(skb, INET_DIAG_INFO,
  177. sizeof(struct sctp_info),
  178. INET_DIAG_PAD);
  179. if (!attr)
  180. goto errout;
  181. info = nla_data(attr);
  182. }
  183. infox.sctpinfo = (struct sctp_info *)info;
  184. infox.asoc = asoc;
  185. sctp_diag_get_info(sk, r, &infox);
  186. addr_list = asoc ? &asoc->base.bind_addr.address_list
  187. : &ep->base.bind_addr.address_list;
  188. if (inet_diag_msg_sctpladdrs_fill(skb, addr_list))
  189. goto errout;
  190. if (asoc && (ext & (1 << (INET_DIAG_CONG - 1))))
  191. if (nla_put_string(skb, INET_DIAG_CONG, "reno") < 0)
  192. goto errout;
  193. if (asoc && inet_diag_msg_sctpaddrs_fill(skb, asoc))
  194. goto errout;
  195. nlmsg_end(skb, nlh);
  196. return 0;
  197. errout:
  198. nlmsg_cancel(skb, nlh);
  199. return -EMSGSIZE;
  200. }
  201. /* callback and param */
  202. struct sctp_comm_param {
  203. struct sk_buff *skb;
  204. struct netlink_callback *cb;
  205. const struct inet_diag_req_v2 *r;
  206. const struct nlmsghdr *nlh;
  207. bool net_admin;
  208. };
  209. static size_t inet_assoc_attr_size(struct sctp_association *asoc)
  210. {
  211. int addrlen = sizeof(struct sockaddr_storage);
  212. int addrcnt = 0;
  213. struct sctp_sockaddr_entry *laddr;
  214. list_for_each_entry_rcu(laddr, &asoc->base.bind_addr.address_list,
  215. list)
  216. addrcnt++;
  217. return nla_total_size(sizeof(struct sctp_info))
  218. + nla_total_size(addrlen * asoc->peer.transport_count)
  219. + nla_total_size(addrlen * addrcnt)
  220. + nla_total_size(sizeof(struct inet_diag_msg))
  221. + inet_diag_msg_attrs_size()
  222. + nla_total_size(sizeof(struct inet_diag_meminfo))
  223. + 64;
  224. }
  225. static int sctp_tsp_dump_one(struct sctp_transport *tsp, void *p)
  226. {
  227. struct sctp_association *assoc = tsp->asoc;
  228. struct sock *sk = tsp->asoc->base.sk;
  229. struct sctp_comm_param *commp = p;
  230. struct sk_buff *in_skb = commp->skb;
  231. const struct inet_diag_req_v2 *req = commp->r;
  232. const struct nlmsghdr *nlh = commp->nlh;
  233. struct net *net = sock_net(in_skb->sk);
  234. struct sk_buff *rep;
  235. int err;
  236. err = sock_diag_check_cookie(sk, req->id.idiag_cookie);
  237. if (err)
  238. goto out;
  239. err = -ENOMEM;
  240. rep = nlmsg_new(inet_assoc_attr_size(assoc), GFP_KERNEL);
  241. if (!rep)
  242. goto out;
  243. lock_sock(sk);
  244. if (sk != assoc->base.sk) {
  245. release_sock(sk);
  246. sk = assoc->base.sk;
  247. lock_sock(sk);
  248. }
  249. err = inet_sctp_diag_fill(sk, assoc, rep, req,
  250. sk_user_ns(NETLINK_CB(in_skb).sk),
  251. NETLINK_CB(in_skb).portid,
  252. nlh->nlmsg_seq, 0, nlh,
  253. commp->net_admin);
  254. release_sock(sk);
  255. if (err < 0) {
  256. WARN_ON(err == -EMSGSIZE);
  257. kfree_skb(rep);
  258. goto out;
  259. }
  260. err = netlink_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid,
  261. MSG_DONTWAIT);
  262. if (err > 0)
  263. err = 0;
  264. out:
  265. return err;
  266. }
  267. static int sctp_sock_dump(struct sctp_transport *tsp, void *p)
  268. {
  269. struct sctp_endpoint *ep = tsp->asoc->ep;
  270. struct sctp_comm_param *commp = p;
  271. struct sock *sk = ep->base.sk;
  272. struct sk_buff *skb = commp->skb;
  273. struct netlink_callback *cb = commp->cb;
  274. const struct inet_diag_req_v2 *r = commp->r;
  275. struct sctp_association *assoc;
  276. int err = 0;
  277. lock_sock(sk);
  278. list_for_each_entry(assoc, &ep->asocs, asocs) {
  279. if (cb->args[4] < cb->args[1])
  280. goto next;
  281. if (r->id.idiag_sport != htons(assoc->base.bind_addr.port) &&
  282. r->id.idiag_sport)
  283. goto next;
  284. if (r->id.idiag_dport != htons(assoc->peer.port) &&
  285. r->id.idiag_dport)
  286. goto next;
  287. if (!cb->args[3] &&
  288. inet_sctp_diag_fill(sk, NULL, skb, r,
  289. sk_user_ns(NETLINK_CB(cb->skb).sk),
  290. NETLINK_CB(cb->skb).portid,
  291. cb->nlh->nlmsg_seq,
  292. NLM_F_MULTI, cb->nlh,
  293. commp->net_admin) < 0) {
  294. err = 1;
  295. goto release;
  296. }
  297. cb->args[3] = 1;
  298. if (inet_sctp_diag_fill(sk, assoc, skb, r,
  299. sk_user_ns(NETLINK_CB(cb->skb).sk),
  300. NETLINK_CB(cb->skb).portid,
  301. cb->nlh->nlmsg_seq, 0, cb->nlh,
  302. commp->net_admin) < 0) {
  303. err = 1;
  304. goto release;
  305. }
  306. next:
  307. cb->args[4]++;
  308. }
  309. cb->args[1] = 0;
  310. cb->args[3] = 0;
  311. cb->args[4] = 0;
  312. release:
  313. release_sock(sk);
  314. return err;
  315. }
  316. static int sctp_sock_filter(struct sctp_transport *tsp, void *p)
  317. {
  318. struct sctp_endpoint *ep = tsp->asoc->ep;
  319. struct sctp_comm_param *commp = p;
  320. struct sock *sk = ep->base.sk;
  321. const struct inet_diag_req_v2 *r = commp->r;
  322. struct sctp_association *assoc =
  323. list_entry(ep->asocs.next, struct sctp_association, asocs);
  324. /* find the ep only once through the transports by this condition */
  325. if (tsp->asoc != assoc)
  326. return 0;
  327. if (r->sdiag_family != AF_UNSPEC && sk->sk_family != r->sdiag_family)
  328. return 0;
  329. return 1;
  330. }
  331. static int sctp_ep_dump(struct sctp_endpoint *ep, void *p)
  332. {
  333. struct sctp_comm_param *commp = p;
  334. struct sock *sk = ep->base.sk;
  335. struct sk_buff *skb = commp->skb;
  336. struct netlink_callback *cb = commp->cb;
  337. const struct inet_diag_req_v2 *r = commp->r;
  338. struct net *net = sock_net(skb->sk);
  339. struct inet_sock *inet = inet_sk(sk);
  340. int err = 0;
  341. if (!net_eq(sock_net(sk), net))
  342. goto out;
  343. if (cb->args[4] < cb->args[1])
  344. goto next;
  345. if (!(r->idiag_states & TCPF_LISTEN) && !list_empty(&ep->asocs))
  346. goto next;
  347. if (r->sdiag_family != AF_UNSPEC &&
  348. sk->sk_family != r->sdiag_family)
  349. goto next;
  350. if (r->id.idiag_sport != inet->inet_sport &&
  351. r->id.idiag_sport)
  352. goto next;
  353. if (r->id.idiag_dport != inet->inet_dport &&
  354. r->id.idiag_dport)
  355. goto next;
  356. if (inet_sctp_diag_fill(sk, NULL, skb, r,
  357. sk_user_ns(NETLINK_CB(cb->skb).sk),
  358. NETLINK_CB(cb->skb).portid,
  359. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  360. cb->nlh, commp->net_admin) < 0) {
  361. err = 2;
  362. goto out;
  363. }
  364. next:
  365. cb->args[4]++;
  366. out:
  367. return err;
  368. }
  369. /* define the functions for sctp_diag_handler*/
  370. static void sctp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
  371. void *info)
  372. {
  373. struct sctp_infox *infox = (struct sctp_infox *)info;
  374. if (infox->asoc) {
  375. r->idiag_rqueue = atomic_read(&infox->asoc->rmem_alloc);
  376. r->idiag_wqueue = infox->asoc->sndbuf_used;
  377. } else {
  378. r->idiag_rqueue = sk->sk_ack_backlog;
  379. r->idiag_wqueue = sk->sk_max_ack_backlog;
  380. }
  381. if (infox->sctpinfo)
  382. sctp_get_sctp_info(sk, infox->asoc, infox->sctpinfo);
  383. }
  384. static int sctp_diag_dump_one(struct sk_buff *in_skb,
  385. const struct nlmsghdr *nlh,
  386. const struct inet_diag_req_v2 *req)
  387. {
  388. struct net *net = sock_net(in_skb->sk);
  389. union sctp_addr laddr, paddr;
  390. struct sctp_comm_param commp = {
  391. .skb = in_skb,
  392. .r = req,
  393. .nlh = nlh,
  394. .net_admin = netlink_net_capable(in_skb, CAP_NET_ADMIN),
  395. };
  396. if (req->sdiag_family == AF_INET) {
  397. laddr.v4.sin_port = req->id.idiag_sport;
  398. laddr.v4.sin_addr.s_addr = req->id.idiag_src[0];
  399. laddr.v4.sin_family = AF_INET;
  400. paddr.v4.sin_port = req->id.idiag_dport;
  401. paddr.v4.sin_addr.s_addr = req->id.idiag_dst[0];
  402. paddr.v4.sin_family = AF_INET;
  403. } else {
  404. laddr.v6.sin6_port = req->id.idiag_sport;
  405. memcpy(&laddr.v6.sin6_addr, req->id.idiag_src,
  406. sizeof(laddr.v6.sin6_addr));
  407. laddr.v6.sin6_family = AF_INET6;
  408. paddr.v6.sin6_port = req->id.idiag_dport;
  409. memcpy(&paddr.v6.sin6_addr, req->id.idiag_dst,
  410. sizeof(paddr.v6.sin6_addr));
  411. paddr.v6.sin6_family = AF_INET6;
  412. }
  413. return sctp_transport_lookup_process(sctp_tsp_dump_one,
  414. net, &laddr, &paddr, &commp);
  415. }
  416. static void sctp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
  417. const struct inet_diag_req_v2 *r, struct nlattr *bc)
  418. {
  419. u32 idiag_states = r->idiag_states;
  420. struct net *net = sock_net(skb->sk);
  421. struct sctp_comm_param commp = {
  422. .skb = skb,
  423. .cb = cb,
  424. .r = r,
  425. .net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN),
  426. };
  427. int pos = cb->args[2];
  428. /* eps hashtable dumps
  429. * args:
  430. * 0 : if it will traversal listen sock
  431. * 1 : to record the sock pos of this time's traversal
  432. * 4 : to work as a temporary variable to traversal list
  433. */
  434. if (cb->args[0] == 0) {
  435. if (!(idiag_states & TCPF_LISTEN))
  436. goto skip;
  437. if (sctp_for_each_endpoint(sctp_ep_dump, &commp))
  438. goto done;
  439. skip:
  440. cb->args[0] = 1;
  441. cb->args[1] = 0;
  442. cb->args[4] = 0;
  443. }
  444. /* asocs by transport hashtable dump
  445. * args:
  446. * 1 : to record the assoc pos of this time's traversal
  447. * 2 : to record the transport pos of this time's traversal
  448. * 3 : to mark if we have dumped the ep info of the current asoc
  449. * 4 : to work as a temporary variable to traversal list
  450. * 5 : to save the sk we get from travelsing the tsp list.
  451. */
  452. if (!(idiag_states & ~(TCPF_LISTEN | TCPF_CLOSE)))
  453. goto done;
  454. sctp_for_each_transport(sctp_sock_filter, sctp_sock_dump,
  455. net, &pos, &commp);
  456. cb->args[2] = pos;
  457. done:
  458. cb->args[1] = cb->args[4];
  459. cb->args[4] = 0;
  460. }
  461. static const struct inet_diag_handler sctp_diag_handler = {
  462. .dump = sctp_diag_dump,
  463. .dump_one = sctp_diag_dump_one,
  464. .idiag_get_info = sctp_diag_get_info,
  465. .idiag_type = IPPROTO_SCTP,
  466. .idiag_info_size = sizeof(struct sctp_info),
  467. };
  468. static int __init sctp_diag_init(void)
  469. {
  470. return inet_diag_register(&sctp_diag_handler);
  471. }
  472. static void __exit sctp_diag_exit(void)
  473. {
  474. inet_diag_unregister(&sctp_diag_handler);
  475. }
  476. module_init(sctp_diag_init);
  477. module_exit(sctp_diag_exit);
  478. MODULE_LICENSE("GPL");
  479. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-132);