]>
Commit | Line | Data |
---|---|---|
2874c5fd | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
17b085ea ACM |
2 | /* |
3 | * tcp_diag.c Module for monitoring TCP transport protocols sockets. | |
4 | * | |
17b085ea | 5 | * Authors: Alexey Kuznetsov, <[email protected]> |
17b085ea ACM |
6 | */ |
7 | ||
17b085ea | 8 | #include <linux/module.h> |
c1e64e29 LC |
9 | #include <linux/net.h> |
10 | #include <linux/sock_diag.h> | |
17b085ea ACM |
11 | #include <linux/inet_diag.h> |
12 | ||
13 | #include <linux/tcp.h> | |
14 | ||
c03fa9bc | 15 | #include <net/netlink.h> |
17b085ea ACM |
16 | #include <net/tcp.h> |
17 | ||
18 | static void tcp_diag_get_info(struct sock *sk, struct inet_diag_msg *r, | |
19 | void *_info) | |
20 | { | |
17b085ea ACM |
21 | struct tcp_info *info = _info; |
22 | ||
986ffdfd | 23 | if (inet_sk_state_load(sk) == TCP_LISTEN) { |
47da8ee6 | 24 | r->idiag_rqueue = sk->sk_ack_backlog; |
5ee3afba | 25 | r->idiag_wqueue = sk->sk_max_ack_backlog; |
35ac838a CG |
26 | } else if (sk->sk_type == SOCK_STREAM) { |
27 | const struct tcp_sock *tp = tcp_sk(sk); | |
28 | ||
49d09007 | 29 | r->idiag_rqueue = max_t(int, tp->rcv_nxt - tp->copied_seq, 0); |
5ee3afba RJ |
30 | r->idiag_wqueue = tp->write_seq - tp->snd_una; |
31 | } | |
00db4124 | 32 | if (info) |
17b085ea ACM |
33 | tcp_get_info(sk, info); |
34 | } | |
35 | ||
c03fa9bc ID |
36 | #ifdef CONFIG_TCP_MD5SIG |
37 | static void tcp_diag_md5sig_fill(struct tcp_diag_md5sig *info, | |
38 | const struct tcp_md5sig_key *key) | |
39 | { | |
40 | info->tcpm_family = key->family; | |
41 | info->tcpm_prefixlen = key->prefixlen; | |
42 | info->tcpm_keylen = key->keylen; | |
43 | memcpy(info->tcpm_key, key->key, key->keylen); | |
44 | ||
45 | if (key->family == AF_INET) | |
46 | info->tcpm_addr[0] = key->addr.a4.s_addr; | |
47 | #if IS_ENABLED(CONFIG_IPV6) | |
48 | else if (key->family == AF_INET6) | |
49 | memcpy(&info->tcpm_addr, &key->addr.a6, | |
50 | sizeof(info->tcpm_addr)); | |
51 | #endif | |
52 | } | |
53 | ||
54 | static int tcp_diag_put_md5sig(struct sk_buff *skb, | |
55 | const struct tcp_md5sig_info *md5sig) | |
56 | { | |
57 | const struct tcp_md5sig_key *key; | |
58 | struct tcp_diag_md5sig *info; | |
59 | struct nlattr *attr; | |
60 | int md5sig_count = 0; | |
61 | ||
62 | hlist_for_each_entry_rcu(key, &md5sig->head, node) | |
63 | md5sig_count++; | |
64 | if (md5sig_count == 0) | |
65 | return 0; | |
66 | ||
67 | attr = nla_reserve(skb, INET_DIAG_MD5SIG, | |
68 | md5sig_count * sizeof(struct tcp_diag_md5sig)); | |
69 | if (!attr) | |
70 | return -EMSGSIZE; | |
71 | ||
72 | info = nla_data(attr); | |
73 | memset(info, 0, md5sig_count * sizeof(struct tcp_diag_md5sig)); | |
74 | hlist_for_each_entry_rcu(key, &md5sig->head, node) { | |
75 | tcp_diag_md5sig_fill(info++, key); | |
76 | if (--md5sig_count == 0) | |
77 | break; | |
78 | } | |
79 | ||
80 | return 0; | |
81 | } | |
82 | #endif | |
83 | ||
84 | static int tcp_diag_get_aux(struct sock *sk, bool net_admin, | |
85 | struct sk_buff *skb) | |
86 | { | |
87 | #ifdef CONFIG_TCP_MD5SIG | |
88 | if (net_admin) { | |
89 | struct tcp_md5sig_info *md5sig; | |
90 | int err = 0; | |
91 | ||
92 | rcu_read_lock(); | |
93 | md5sig = rcu_dereference(tcp_sk(sk)->md5sig_info); | |
94 | if (md5sig) | |
95 | err = tcp_diag_put_md5sig(skb, md5sig); | |
96 | rcu_read_unlock(); | |
97 | if (err < 0) | |
98 | return err; | |
99 | } | |
100 | #endif | |
101 | ||
102 | return 0; | |
103 | } | |
104 | ||
105 | static size_t tcp_diag_get_aux_size(struct sock *sk, bool net_admin) | |
106 | { | |
107 | size_t size = 0; | |
108 | ||
109 | #ifdef CONFIG_TCP_MD5SIG | |
110 | if (net_admin && sk_fullsock(sk)) { | |
111 | const struct tcp_md5sig_info *md5sig; | |
112 | const struct tcp_md5sig_key *key; | |
113 | size_t md5sig_count = 0; | |
114 | ||
115 | rcu_read_lock(); | |
116 | md5sig = rcu_dereference(tcp_sk(sk)->md5sig_info); | |
117 | if (md5sig) { | |
118 | hlist_for_each_entry_rcu(key, &md5sig->head, node) | |
119 | md5sig_count++; | |
120 | } | |
121 | rcu_read_unlock(); | |
122 | size += nla_total_size(md5sig_count * | |
123 | sizeof(struct tcp_diag_md5sig)); | |
124 | } | |
125 | #endif | |
126 | ||
127 | return size; | |
128 | } | |
129 | ||
1942c518 | 130 | static void tcp_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, |
34160ea3 | 131 | const struct inet_diag_req_v2 *r, struct nlattr *bc) |
1942c518 PE |
132 | { |
133 | inet_diag_dump_icsk(&tcp_hashinfo, skb, cb, r, bc); | |
134 | } | |
135 | ||
136 | static int tcp_diag_dump_one(struct sk_buff *in_skb, const struct nlmsghdr *nlh, | |
34160ea3 | 137 | const struct inet_diag_req_v2 *req) |
1942c518 PE |
138 | { |
139 | return inet_diag_dump_one_icsk(&tcp_hashinfo, in_skb, nlh, req); | |
140 | } | |
141 | ||
c1e64e29 LC |
142 | #ifdef CONFIG_INET_DIAG_DESTROY |
143 | static int tcp_diag_destroy(struct sk_buff *in_skb, | |
144 | const struct inet_diag_req_v2 *req) | |
145 | { | |
146 | struct net *net = sock_net(in_skb->sk); | |
147 | struct sock *sk = inet_diag_find_one_icsk(net, &tcp_hashinfo, req); | |
d7226c7a | 148 | int err; |
c1e64e29 LC |
149 | |
150 | if (IS_ERR(sk)) | |
151 | return PTR_ERR(sk); | |
152 | ||
d7226c7a DA |
153 | err = sock_diag_destroy(sk, ECONNABORTED); |
154 | ||
155 | sock_gen_put(sk); | |
156 | ||
157 | return err; | |
c1e64e29 LC |
158 | } |
159 | #endif | |
160 | ||
a7a0d6a8 | 161 | static const struct inet_diag_handler tcp_diag_handler = { |
c03fa9bc ID |
162 | .dump = tcp_diag_dump, |
163 | .dump_one = tcp_diag_dump_one, | |
164 | .idiag_get_info = tcp_diag_get_info, | |
165 | .idiag_get_aux = tcp_diag_get_aux, | |
166 | .idiag_get_aux_size = tcp_diag_get_aux_size, | |
167 | .idiag_type = IPPROTO_TCP, | |
168 | .idiag_info_size = sizeof(struct tcp_info), | |
c1e64e29 | 169 | #ifdef CONFIG_INET_DIAG_DESTROY |
c03fa9bc | 170 | .destroy = tcp_diag_destroy, |
c1e64e29 | 171 | #endif |
17b085ea ACM |
172 | }; |
173 | ||
174 | static int __init tcp_diag_init(void) | |
175 | { | |
176 | return inet_diag_register(&tcp_diag_handler); | |
177 | } | |
178 | ||
179 | static void __exit tcp_diag_exit(void) | |
180 | { | |
181 | inet_diag_unregister(&tcp_diag_handler); | |
182 | } | |
183 | ||
184 | module_init(tcp_diag_init); | |
185 | module_exit(tcp_diag_exit); | |
186 | MODULE_LICENSE("GPL"); | |
aec8dc62 | 187 | MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-6 /* AF_INET - IPPROTO_TCP */); |