tcp_diag.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * tcp_diag.c Module for monitoring TCP transport protocols sockets.
  4. *
  5. * Authors: Alexey Kuznetsov, <kuznet@ms2.inr.ac.ru>
  6. */
  7. #include <linux/module.h>
  8. #include <linux/net.h>
  9. #include <linux/sock_diag.h>
  10. #include <linux/inet_diag.h>
  11. #include <linux/tcp.h>
  12. #include <net/inet_hashtables.h>
  13. #include <net/inet6_hashtables.h>
  14. #include <net/inet_timewait_sock.h>
  15. #include <net/netlink.h>
  16. #include <net/tcp.h>
  17. static void tcp_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
  18. void *_info)
  19. {
  20. struct tcp_info *info = _info;
  21. if (inet_sk_state_load(sk) == TCP_LISTEN) {
  22. r->idiag_rqueue = READ_ONCE(sk->sk_ack_backlog);
  23. r->idiag_wqueue = READ_ONCE(sk->sk_max_ack_backlog);
  24. } else if (sk->sk_type == SOCK_STREAM) {
  25. const struct tcp_sock *tp = tcp_sk(sk);
  26. r->idiag_rqueue = max_t(int, READ_ONCE(tp->rcv_nxt) -
  27. READ_ONCE(tp->copied_seq), 0);
  28. r->idiag_wqueue = READ_ONCE(tp->write_seq) - tp->snd_una;
  29. }
  30. if (info)
  31. tcp_get_info(sk, info);
  32. }
  33. #ifdef CONFIG_TCP_MD5SIG
  34. static void tcp_diag_md5sig_fill(struct tcp_diag_md5sig *info,
  35. const struct tcp_md5sig_key *key)
  36. {
  37. info->tcpm_family = key->family;
  38. info->tcpm_prefixlen = key->prefixlen;
  39. info->tcpm_keylen = key->keylen;
  40. memcpy(info->tcpm_key, key->key, key->keylen);
  41. if (key->family == AF_INET)
  42. info->tcpm_addr[0] = key->addr.a4.s_addr;
  43. #if IS_ENABLED(CONFIG_IPV6)
  44. else if (key->family == AF_INET6)
  45. memcpy(&info->tcpm_addr, &key->addr.a6,
  46. sizeof(info->tcpm_addr));
  47. #endif
  48. }
  49. static int tcp_diag_put_md5sig(struct sk_buff *skb,
  50. const struct tcp_md5sig_info *md5sig)
  51. {
  52. const struct tcp_md5sig_key *key;
  53. struct tcp_diag_md5sig *info;
  54. struct nlattr *attr;
  55. int md5sig_count = 0;
  56. hlist_for_each_entry_rcu(key, &md5sig->head, node)
  57. md5sig_count++;
  58. if (md5sig_count == 0)
  59. return 0;
  60. attr = nla_reserve(skb, INET_DIAG_MD5SIG,
  61. md5sig_count * sizeof(struct tcp_diag_md5sig));
  62. if (!attr)
  63. return -EMSGSIZE;
  64. info = nla_data(attr);
  65. memset(info, 0, md5sig_count * sizeof(struct tcp_diag_md5sig));
  66. hlist_for_each_entry_rcu(key, &md5sig->head, node) {
  67. tcp_diag_md5sig_fill(info++, key);
  68. if (--md5sig_count == 0)
  69. break;
  70. }
  71. return 0;
  72. }
  73. #endif
  74. static int tcp_diag_put_ulp(struct sk_buff *skb, struct sock *sk,
  75. const struct tcp_ulp_ops *ulp_ops, bool net_admin)
  76. {
  77. struct nlattr *nest;
  78. int err;
  79. nest = nla_nest_start_noflag(skb, INET_DIAG_ULP_INFO);
  80. if (!nest)
  81. return -EMSGSIZE;
  82. err = nla_put_string(skb, INET_ULP_INFO_NAME, ulp_ops->name);
  83. if (err)
  84. goto nla_failure;
  85. if (ulp_ops->get_info)
  86. err = ulp_ops->get_info(sk, skb, net_admin);
  87. if (err)
  88. goto nla_failure;
  89. nla_nest_end(skb, nest);
  90. return 0;
  91. nla_failure:
  92. nla_nest_cancel(skb, nest);
  93. return err;
  94. }
  95. static int tcp_diag_get_aux(struct sock *sk, bool net_admin,
  96. struct sk_buff *skb)
  97. {
  98. struct inet_connection_sock *icsk = inet_csk(sk);
  99. const struct tcp_ulp_ops *ulp_ops;
  100. int err = 0;
  101. #ifdef CONFIG_TCP_MD5SIG
  102. if (net_admin) {
  103. struct tcp_md5sig_info *md5sig;
  104. rcu_read_lock();
  105. md5sig = rcu_dereference(tcp_sk(sk)->md5sig_info);
  106. if (md5sig)
  107. err = tcp_diag_put_md5sig(skb, md5sig);
  108. rcu_read_unlock();
  109. if (err < 0)
  110. return err;
  111. }
  112. #endif
  113. ulp_ops = icsk->icsk_ulp_ops;
  114. if (ulp_ops) {
  115. err = tcp_diag_put_ulp(skb, sk, ulp_ops, net_admin);
  116. if (err < 0)
  117. return err;
  118. }
  119. return 0;
  120. }
  121. static size_t tcp_diag_get_aux_size(struct sock *sk, bool net_admin)
  122. {
  123. struct inet_connection_sock *icsk = inet_csk(sk);
  124. size_t size = 0;
  125. #ifdef CONFIG_TCP_MD5SIG
  126. if (net_admin && sk_fullsock(sk)) {
  127. const struct tcp_md5sig_info *md5sig;
  128. const struct tcp_md5sig_key *key;
  129. size_t md5sig_count = 0;
  130. rcu_read_lock();
  131. md5sig = rcu_dereference(tcp_sk(sk)->md5sig_info);
  132. if (md5sig) {
  133. hlist_for_each_entry_rcu(key, &md5sig->head, node)
  134. md5sig_count++;
  135. }
  136. rcu_read_unlock();
  137. size += nla_total_size(md5sig_count *
  138. sizeof(struct tcp_diag_md5sig));
  139. }
  140. #endif
  141. if (sk_fullsock(sk)) {
  142. const struct tcp_ulp_ops *ulp_ops;
  143. ulp_ops = icsk->icsk_ulp_ops;
  144. if (ulp_ops) {
  145. size += nla_total_size(0) +
  146. nla_total_size(TCP_ULP_NAME_MAX);
  147. if (ulp_ops->get_info_size)
  148. size += ulp_ops->get_info_size(sk, net_admin);
  149. }
  150. }
  151. return size
  152. + nla_total_size(sizeof(struct tcp_info))
  153. + nla_total_size(sizeof(struct inet_diag_msg))
  154. + inet_diag_msg_attrs_size()
  155. + nla_total_size(sizeof(struct inet_diag_meminfo))
  156. + nla_total_size(SK_MEMINFO_VARS * sizeof(u32))
  157. + nla_total_size(TCP_CA_NAME_MAX)
  158. + nla_total_size(sizeof(struct tcpvegas_info))
  159. + 64;
  160. }
  161. static int tcp_twsk_diag_fill(struct sock *sk,
  162. struct sk_buff *skb,
  163. struct netlink_callback *cb,
  164. u16 nlmsg_flags, bool net_admin)
  165. {
  166. struct inet_timewait_sock *tw = inet_twsk(sk);
  167. struct inet_diag_msg *r;
  168. struct nlmsghdr *nlh;
  169. long tmo;
  170. nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid,
  171. cb->nlh->nlmsg_seq, cb->nlh->nlmsg_type,
  172. sizeof(*r), nlmsg_flags);
  173. if (!nlh)
  174. return -EMSGSIZE;
  175. r = nlmsg_data(nlh);
  176. DEBUG_NET_WARN_ON_ONCE(tw->tw_state != TCP_TIME_WAIT);
  177. inet_diag_msg_common_fill(r, sk);
  178. r->idiag_retrans = 0;
  179. r->idiag_state = READ_ONCE(tw->tw_substate);
  180. r->idiag_timer = 3;
  181. tmo = tw->tw_timer.expires - jiffies;
  182. r->idiag_expires = jiffies_delta_to_msecs(tmo);
  183. r->idiag_rqueue = 0;
  184. r->idiag_wqueue = 0;
  185. r->idiag_uid = 0;
  186. r->idiag_inode = 0;
  187. if (net_admin && nla_put_u32(skb, INET_DIAG_MARK,
  188. tw->tw_mark)) {
  189. nlmsg_cancel(skb, nlh);
  190. return -EMSGSIZE;
  191. }
  192. nlmsg_end(skb, nlh);
  193. return 0;
  194. }
  195. static int tcp_req_diag_fill(struct sock *sk, struct sk_buff *skb,
  196. struct netlink_callback *cb,
  197. u16 nlmsg_flags, bool net_admin)
  198. {
  199. struct request_sock *reqsk = inet_reqsk(sk);
  200. struct inet_diag_msg *r;
  201. struct nlmsghdr *nlh;
  202. long tmo;
  203. nlh = nlmsg_put(skb, NETLINK_CB(cb->skb).portid, cb->nlh->nlmsg_seq,
  204. cb->nlh->nlmsg_type, sizeof(*r), nlmsg_flags);
  205. if (!nlh)
  206. return -EMSGSIZE;
  207. r = nlmsg_data(nlh);
  208. inet_diag_msg_common_fill(r, sk);
  209. r->idiag_state = TCP_SYN_RECV;
  210. r->idiag_timer = 1;
  211. r->idiag_retrans = READ_ONCE(reqsk->num_retrans);
  212. BUILD_BUG_ON(offsetof(struct inet_request_sock, ir_cookie) !=
  213. offsetof(struct sock, sk_cookie));
  214. tmo = READ_ONCE(inet_reqsk(sk)->rsk_timer.expires) - jiffies;
  215. r->idiag_expires = jiffies_delta_to_msecs(tmo);
  216. r->idiag_rqueue = 0;
  217. r->idiag_wqueue = 0;
  218. r->idiag_uid = 0;
  219. r->idiag_inode = 0;
  220. if (net_admin && nla_put_u32(skb, INET_DIAG_MARK,
  221. inet_rsk(reqsk)->ir_mark)) {
  222. nlmsg_cancel(skb, nlh);
  223. return -EMSGSIZE;
  224. }
  225. nlmsg_end(skb, nlh);
  226. return 0;
  227. }
  228. static int sk_diag_fill(struct sock *sk, struct sk_buff *skb,
  229. struct netlink_callback *cb,
  230. const struct inet_diag_req_v2 *r,
  231. u16 nlmsg_flags, bool net_admin)
  232. {
  233. if (sk->sk_state == TCP_TIME_WAIT)
  234. return tcp_twsk_diag_fill(sk, skb, cb, nlmsg_flags, net_admin);
  235. if (sk->sk_state == TCP_NEW_SYN_RECV)
  236. return tcp_req_diag_fill(sk, skb, cb, nlmsg_flags, net_admin);
  237. return inet_sk_diag_fill(sk, inet_csk(sk), skb, cb, r, nlmsg_flags,
  238. net_admin);
  239. }
  240. static void twsk_build_assert(void)
  241. {
  242. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_family) !=
  243. offsetof(struct sock, sk_family));
  244. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_num) !=
  245. offsetof(struct inet_sock, inet_num));
  246. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_dport) !=
  247. offsetof(struct inet_sock, inet_dport));
  248. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_rcv_saddr) !=
  249. offsetof(struct inet_sock, inet_rcv_saddr));
  250. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_daddr) !=
  251. offsetof(struct inet_sock, inet_daddr));
  252. #if IS_ENABLED(CONFIG_IPV6)
  253. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_rcv_saddr) !=
  254. offsetof(struct sock, sk_v6_rcv_saddr));
  255. BUILD_BUG_ON(offsetof(struct inet_timewait_sock, tw_v6_daddr) !=
  256. offsetof(struct sock, sk_v6_daddr));
  257. #endif
  258. }
  259. static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
  260. const struct inet_diag_req_v2 *r)
  261. {
  262. bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
  263. struct inet_diag_dump_data *cb_data = cb->data;
  264. struct net *net = sock_net(skb->sk);
  265. u32 idiag_states = r->idiag_states;
  266. struct inet_hashinfo *hashinfo;
  267. int i, num, s_i, s_num;
  268. struct sock *sk;
  269. hashinfo = net->ipv4.tcp_death_row.hashinfo;
  270. if (idiag_states & TCPF_SYN_RECV)
  271. idiag_states |= TCPF_NEW_SYN_RECV;
  272. s_i = cb->args[1];
  273. s_num = num = cb->args[2];
  274. if (cb->args[0] == 0) {
  275. if (!(idiag_states & TCPF_LISTEN) || r->id.idiag_dport)
  276. goto skip_listen_ht;
  277. for (i = s_i; i <= hashinfo->lhash2_mask; i++) {
  278. struct inet_listen_hashbucket *ilb;
  279. struct hlist_nulls_node *node;
  280. num = 0;
  281. ilb = &hashinfo->lhash2[i];
  282. if (hlist_nulls_empty(&ilb->nulls_head)) {
  283. s_num = 0;
  284. continue;
  285. }
  286. spin_lock(&ilb->lock);
  287. sk_nulls_for_each(sk, node, &ilb->nulls_head) {
  288. struct inet_sock *inet = inet_sk(sk);
  289. if (!net_eq(sock_net(sk), net))
  290. continue;
  291. if (num < s_num) {
  292. num++;
  293. continue;
  294. }
  295. if (r->sdiag_family != AF_UNSPEC &&
  296. sk->sk_family != r->sdiag_family)
  297. goto next_listen;
  298. if (r->id.idiag_sport != inet->inet_sport &&
  299. r->id.idiag_sport)
  300. goto next_listen;
  301. if (!inet_diag_bc_sk(cb_data, sk))
  302. goto next_listen;
  303. if (inet_sk_diag_fill(sk, inet_csk(sk), skb,
  304. cb, r, NLM_F_MULTI,
  305. net_admin) < 0) {
  306. spin_unlock(&ilb->lock);
  307. goto done;
  308. }
  309. next_listen:
  310. ++num;
  311. }
  312. spin_unlock(&ilb->lock);
  313. s_num = 0;
  314. }
  315. skip_listen_ht:
  316. cb->args[0] = 1;
  317. s_i = num = s_num = 0;
  318. }
  319. /* Process a maximum of SKARR_SZ sockets at a time when walking hash buckets
  320. * with bh disabled.
  321. */
  322. #define SKARR_SZ 16
  323. /* Dump bound but inactive (not listening, connecting, etc.) sockets */
  324. if (cb->args[0] == 1) {
  325. if (!(idiag_states & TCPF_BOUND_INACTIVE))
  326. goto skip_bind_ht;
  327. for (i = s_i; i < hashinfo->bhash_size; i++) {
  328. struct inet_bind_hashbucket *ibb;
  329. struct inet_bind2_bucket *tb2;
  330. struct sock *sk_arr[SKARR_SZ];
  331. int num_arr[SKARR_SZ];
  332. int idx, accum, res;
  333. resume_bind_walk:
  334. num = 0;
  335. accum = 0;
  336. ibb = &hashinfo->bhash2[i];
  337. if (hlist_empty(&ibb->chain)) {
  338. s_num = 0;
  339. continue;
  340. }
  341. spin_lock_bh(&ibb->lock);
  342. inet_bind_bucket_for_each(tb2, &ibb->chain) {
  343. if (!net_eq(ib2_net(tb2), net))
  344. continue;
  345. sk_for_each_bound(sk, &tb2->owners) {
  346. struct inet_sock *inet = inet_sk(sk);
  347. if (num < s_num)
  348. goto next_bind;
  349. if (sk->sk_state != TCP_CLOSE ||
  350. !inet->inet_num)
  351. goto next_bind;
  352. if (r->sdiag_family != AF_UNSPEC &&
  353. r->sdiag_family != sk->sk_family)
  354. goto next_bind;
  355. if (!inet_diag_bc_sk(cb_data, sk))
  356. goto next_bind;
  357. sock_hold(sk);
  358. num_arr[accum] = num;
  359. sk_arr[accum] = sk;
  360. if (++accum == SKARR_SZ)
  361. goto pause_bind_walk;
  362. next_bind:
  363. num++;
  364. }
  365. }
  366. pause_bind_walk:
  367. spin_unlock_bh(&ibb->lock);
  368. res = 0;
  369. for (idx = 0; idx < accum; idx++) {
  370. if (res >= 0) {
  371. res = inet_sk_diag_fill(sk_arr[idx],
  372. NULL, skb, cb,
  373. r, NLM_F_MULTI,
  374. net_admin);
  375. if (res < 0)
  376. num = num_arr[idx];
  377. }
  378. sock_put(sk_arr[idx]);
  379. }
  380. if (res < 0)
  381. goto done;
  382. cond_resched();
  383. if (accum == SKARR_SZ) {
  384. s_num = num + 1;
  385. goto resume_bind_walk;
  386. }
  387. s_num = 0;
  388. }
  389. skip_bind_ht:
  390. cb->args[0] = 2;
  391. s_i = num = s_num = 0;
  392. }
  393. if (!(idiag_states & ~TCPF_LISTEN))
  394. goto out;
  395. for (i = s_i; i <= hashinfo->ehash_mask; i++) {
  396. struct inet_ehash_bucket *head = &hashinfo->ehash[i];
  397. spinlock_t *lock = inet_ehash_lockp(hashinfo, i);
  398. struct hlist_nulls_node *node;
  399. struct sock *sk_arr[SKARR_SZ];
  400. int num_arr[SKARR_SZ];
  401. int idx, accum, res;
  402. if (hlist_nulls_empty(&head->chain))
  403. continue;
  404. if (i > s_i)
  405. s_num = 0;
  406. next_chunk:
  407. num = 0;
  408. accum = 0;
  409. spin_lock_bh(lock);
  410. sk_nulls_for_each(sk, node, &head->chain) {
  411. int state;
  412. if (!net_eq(sock_net(sk), net))
  413. continue;
  414. if (num < s_num)
  415. goto next_normal;
  416. state = (sk->sk_state == TCP_TIME_WAIT) ?
  417. READ_ONCE(inet_twsk(sk)->tw_substate) : sk->sk_state;
  418. if (!(idiag_states & (1 << state)))
  419. goto next_normal;
  420. if (r->sdiag_family != AF_UNSPEC &&
  421. sk->sk_family != r->sdiag_family)
  422. goto next_normal;
  423. if (r->id.idiag_sport != htons(READ_ONCE(sk->sk_num)) &&
  424. r->id.idiag_sport)
  425. goto next_normal;
  426. if (r->id.idiag_dport != sk->sk_dport &&
  427. r->id.idiag_dport)
  428. goto next_normal;
  429. twsk_build_assert();
  430. if (!inet_diag_bc_sk(cb_data, sk))
  431. goto next_normal;
  432. if (!refcount_inc_not_zero(&sk->sk_refcnt))
  433. goto next_normal;
  434. num_arr[accum] = num;
  435. sk_arr[accum] = sk;
  436. if (++accum == SKARR_SZ)
  437. break;
  438. next_normal:
  439. ++num;
  440. }
  441. spin_unlock_bh(lock);
  442. res = 0;
  443. for (idx = 0; idx < accum; idx++) {
  444. if (res >= 0) {
  445. res = sk_diag_fill(sk_arr[idx], skb, cb, r,
  446. NLM_F_MULTI, net_admin);
  447. if (res < 0)
  448. num = num_arr[idx];
  449. }
  450. sock_gen_put(sk_arr[idx]);
  451. }
  452. if (res < 0)
  453. break;
  454. cond_resched();
  455. if (accum == SKARR_SZ) {
  456. s_num = num + 1;
  457. goto next_chunk;
  458. }
  459. }
  460. done:
  461. cb->args[1] = i;
  462. cb->args[2] = num;
  463. out:
  464. ;
  465. }
  466. static struct sock *tcp_diag_find_one_icsk(struct net *net,
  467. const struct inet_diag_req_v2 *req)
  468. {
  469. struct sock *sk;
  470. rcu_read_lock();
  471. if (req->sdiag_family == AF_INET) {
  472. sk = inet_lookup(net, NULL, 0, req->id.idiag_dst[0],
  473. req->id.idiag_dport, req->id.idiag_src[0],
  474. req->id.idiag_sport, req->id.idiag_if);
  475. #if IS_ENABLED(CONFIG_IPV6)
  476. } else if (req->sdiag_family == AF_INET6) {
  477. if (ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_dst) &&
  478. ipv6_addr_v4mapped((struct in6_addr *)req->id.idiag_src))
  479. sk = inet_lookup(net, NULL, 0, req->id.idiag_dst[3],
  480. req->id.idiag_dport, req->id.idiag_src[3],
  481. req->id.idiag_sport, req->id.idiag_if);
  482. else
  483. sk = inet6_lookup(net, NULL, 0,
  484. (struct in6_addr *)req->id.idiag_dst,
  485. req->id.idiag_dport,
  486. (struct in6_addr *)req->id.idiag_src,
  487. req->id.idiag_sport,
  488. req->id.idiag_if);
  489. #endif
  490. } else {
  491. rcu_read_unlock();
  492. return ERR_PTR(-EINVAL);
  493. }
  494. rcu_read_unlock();
  495. if (!sk)
  496. return ERR_PTR(-ENOENT);
  497. if (sock_diag_check_cookie(sk, req->id.idiag_cookie)) {
  498. sock_gen_put(sk);
  499. return ERR_PTR(-ENOENT);
  500. }
  501. return sk;
  502. }
  503. static int tcp_diag_dump_one(struct netlink_callback *cb,
  504. const struct inet_diag_req_v2 *req)
  505. {
  506. struct sk_buff *in_skb = cb->skb;
  507. struct sk_buff *rep;
  508. struct sock *sk;
  509. struct net *net;
  510. bool net_admin;
  511. int err;
  512. net = sock_net(in_skb->sk);
  513. sk = tcp_diag_find_one_icsk(net, req);
  514. if (IS_ERR(sk))
  515. return PTR_ERR(sk);
  516. net_admin = netlink_net_capable(in_skb, CAP_NET_ADMIN);
  517. rep = nlmsg_new(tcp_diag_get_aux_size(sk, net_admin), GFP_KERNEL);
  518. if (!rep) {
  519. err = -ENOMEM;
  520. goto out;
  521. }
  522. err = sk_diag_fill(sk, rep, cb, req, 0, net_admin);
  523. if (err < 0) {
  524. WARN_ON(err == -EMSGSIZE);
  525. nlmsg_free(rep);
  526. goto out;
  527. }
  528. err = nlmsg_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid);
  529. out:
  530. if (sk)
  531. sock_gen_put(sk);
  532. return err;
  533. }
  534. #ifdef CONFIG_INET_DIAG_DESTROY
  535. static int tcp_diag_destroy(struct sk_buff *in_skb,
  536. const struct inet_diag_req_v2 *req)
  537. {
  538. struct net *net = sock_net(in_skb->sk);
  539. struct sock *sk;
  540. int err;
  541. sk = tcp_diag_find_one_icsk(net, req);
  542. if (IS_ERR(sk))
  543. return PTR_ERR(sk);
  544. err = sock_diag_destroy(sk, ECONNABORTED);
  545. sock_gen_put(sk);
  546. return err;
  547. }
  548. #endif
  549. static const struct inet_diag_handler tcp_diag_handler = {
  550. .owner = THIS_MODULE,
  551. .dump = tcp_diag_dump,
  552. .dump_one = tcp_diag_dump_one,
  553. .idiag_get_info = tcp_diag_get_info,
  554. .idiag_get_aux = tcp_diag_get_aux,
  555. .idiag_type = IPPROTO_TCP,
  556. .idiag_info_size = sizeof(struct tcp_info),
  557. #ifdef CONFIG_INET_DIAG_DESTROY
  558. .destroy = tcp_diag_destroy,
  559. #endif
  560. };
  561. static int __init tcp_diag_init(void)
  562. {
  563. return inet_diag_register(&tcp_diag_handler);
  564. }
  565. static void __exit tcp_diag_exit(void)
  566. {
  567. inet_diag_unregister(&tcp_diag_handler);
  568. }
  569. module_init(tcp_diag_init);
  570. module_exit(tcp_diag_exit);
  571. MODULE_LICENSE("GPL");
  572. MODULE_DESCRIPTION("TCP socket monitoring via SOCK_DIAG");
  573. MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-6 /* AF_INET - IPPROTO_TCP */);