sctp_diag.c 14 KB

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