Loading...
1#include <linux/mutex.h>
2#include <linux/socket.h>
3#include <linux/skbuff.h>
4#include <net/netlink.h>
5#include <net/net_namespace.h>
6#include <linux/module.h>
7#include <net/sock.h>
8
9#include <linux/inet_diag.h>
10#include <linux/sock_diag.h>
11
12static const struct sock_diag_handler *sock_diag_handlers[AF_MAX];
13static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh);
14static DEFINE_MUTEX(sock_diag_table_mutex);
15
16int sock_diag_check_cookie(void *sk, __u32 *cookie)
17{
18 if ((cookie[0] != INET_DIAG_NOCOOKIE ||
19 cookie[1] != INET_DIAG_NOCOOKIE) &&
20 ((u32)(unsigned long)sk != cookie[0] ||
21 (u32)((((unsigned long)sk) >> 31) >> 1) != cookie[1]))
22 return -ESTALE;
23 else
24 return 0;
25}
26EXPORT_SYMBOL_GPL(sock_diag_check_cookie);
27
28void sock_diag_save_cookie(void *sk, __u32 *cookie)
29{
30 cookie[0] = (u32)(unsigned long)sk;
31 cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
32}
33EXPORT_SYMBOL_GPL(sock_diag_save_cookie);
34
35int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype)
36{
37 u32 mem[SK_MEMINFO_VARS];
38
39 mem[SK_MEMINFO_RMEM_ALLOC] = sk_rmem_alloc_get(sk);
40 mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf;
41 mem[SK_MEMINFO_WMEM_ALLOC] = sk_wmem_alloc_get(sk);
42 mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf;
43 mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc;
44 mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued;
45 mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc);
46 mem[SK_MEMINFO_BACKLOG] = sk->sk_backlog.len;
47
48 return nla_put(skb, attrtype, sizeof(mem), &mem);
49}
50EXPORT_SYMBOL_GPL(sock_diag_put_meminfo);
51
52int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk,
53 struct sk_buff *skb, int attrtype)
54{
55 struct sock_fprog_kern *fprog;
56 struct sk_filter *filter;
57 struct nlattr *attr;
58 unsigned int flen;
59 int err = 0;
60
61 if (!may_report_filterinfo) {
62 nla_reserve(skb, attrtype, 0);
63 return 0;
64 }
65
66 rcu_read_lock();
67 filter = rcu_dereference(sk->sk_filter);
68 if (!filter)
69 goto out;
70
71 fprog = filter->orig_prog;
72 flen = sk_filter_proglen(fprog);
73
74 attr = nla_reserve(skb, attrtype, flen);
75 if (attr == NULL) {
76 err = -EMSGSIZE;
77 goto out;
78 }
79
80 memcpy(nla_data(attr), fprog->filter, flen);
81out:
82 rcu_read_unlock();
83 return err;
84}
85EXPORT_SYMBOL(sock_diag_put_filterinfo);
86
87void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
88{
89 mutex_lock(&sock_diag_table_mutex);
90 inet_rcv_compat = fn;
91 mutex_unlock(&sock_diag_table_mutex);
92}
93EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat);
94
95void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
96{
97 mutex_lock(&sock_diag_table_mutex);
98 inet_rcv_compat = NULL;
99 mutex_unlock(&sock_diag_table_mutex);
100}
101EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat);
102
103int sock_diag_register(const struct sock_diag_handler *hndl)
104{
105 int err = 0;
106
107 if (hndl->family >= AF_MAX)
108 return -EINVAL;
109
110 mutex_lock(&sock_diag_table_mutex);
111 if (sock_diag_handlers[hndl->family])
112 err = -EBUSY;
113 else
114 sock_diag_handlers[hndl->family] = hndl;
115 mutex_unlock(&sock_diag_table_mutex);
116
117 return err;
118}
119EXPORT_SYMBOL_GPL(sock_diag_register);
120
121void sock_diag_unregister(const struct sock_diag_handler *hnld)
122{
123 int family = hnld->family;
124
125 if (family >= AF_MAX)
126 return;
127
128 mutex_lock(&sock_diag_table_mutex);
129 BUG_ON(sock_diag_handlers[family] != hnld);
130 sock_diag_handlers[family] = NULL;
131 mutex_unlock(&sock_diag_table_mutex);
132}
133EXPORT_SYMBOL_GPL(sock_diag_unregister);
134
135static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
136{
137 int err;
138 struct sock_diag_req *req = nlmsg_data(nlh);
139 const struct sock_diag_handler *hndl;
140
141 if (nlmsg_len(nlh) < sizeof(*req))
142 return -EINVAL;
143
144 if (req->sdiag_family >= AF_MAX)
145 return -EINVAL;
146
147 if (sock_diag_handlers[req->sdiag_family] == NULL)
148 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
149 NETLINK_SOCK_DIAG, req->sdiag_family);
150
151 mutex_lock(&sock_diag_table_mutex);
152 hndl = sock_diag_handlers[req->sdiag_family];
153 if (hndl == NULL)
154 err = -ENOENT;
155 else
156 err = hndl->dump(skb, nlh);
157 mutex_unlock(&sock_diag_table_mutex);
158
159 return err;
160}
161
162static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
163{
164 int ret;
165
166 switch (nlh->nlmsg_type) {
167 case TCPDIAG_GETSOCK:
168 case DCCPDIAG_GETSOCK:
169 if (inet_rcv_compat == NULL)
170 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
171 NETLINK_SOCK_DIAG, AF_INET);
172
173 mutex_lock(&sock_diag_table_mutex);
174 if (inet_rcv_compat != NULL)
175 ret = inet_rcv_compat(skb, nlh);
176 else
177 ret = -EOPNOTSUPP;
178 mutex_unlock(&sock_diag_table_mutex);
179
180 return ret;
181 case SOCK_DIAG_BY_FAMILY:
182 return __sock_diag_rcv_msg(skb, nlh);
183 default:
184 return -EINVAL;
185 }
186}
187
188static DEFINE_MUTEX(sock_diag_mutex);
189
190static void sock_diag_rcv(struct sk_buff *skb)
191{
192 mutex_lock(&sock_diag_mutex);
193 netlink_rcv_skb(skb, &sock_diag_rcv_msg);
194 mutex_unlock(&sock_diag_mutex);
195}
196
197static int __net_init diag_net_init(struct net *net)
198{
199 struct netlink_kernel_cfg cfg = {
200 .input = sock_diag_rcv,
201 };
202
203 net->diag_nlsk = netlink_kernel_create(net, NETLINK_SOCK_DIAG, &cfg);
204 return net->diag_nlsk == NULL ? -ENOMEM : 0;
205}
206
207static void __net_exit diag_net_exit(struct net *net)
208{
209 netlink_kernel_release(net->diag_nlsk);
210 net->diag_nlsk = NULL;
211}
212
213static struct pernet_operations diag_net_ops = {
214 .init = diag_net_init,
215 .exit = diag_net_exit,
216};
217
218static int __init sock_diag_init(void)
219{
220 return register_pernet_subsys(&diag_net_ops);
221}
222
223static void __exit sock_diag_exit(void)
224{
225 unregister_pernet_subsys(&diag_net_ops);
226}
227
228module_init(sock_diag_init);
229module_exit(sock_diag_exit);
230MODULE_LICENSE("GPL");
231MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG);
1/* License: GPL */
2
3#include <linux/mutex.h>
4#include <linux/socket.h>
5#include <linux/skbuff.h>
6#include <net/netlink.h>
7#include <net/net_namespace.h>
8#include <linux/module.h>
9#include <net/sock.h>
10#include <linux/kernel.h>
11#include <linux/tcp.h>
12#include <linux/workqueue.h>
13#include <linux/nospec.h>
14#include <linux/cookie.h>
15#include <linux/inet_diag.h>
16#include <linux/sock_diag.h>
17
18static const struct sock_diag_handler *sock_diag_handlers[AF_MAX];
19static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh);
20static DEFINE_MUTEX(sock_diag_table_mutex);
21static struct workqueue_struct *broadcast_wq;
22
23DEFINE_COOKIE(sock_cookie);
24
25u64 __sock_gen_cookie(struct sock *sk)
26{
27 while (1) {
28 u64 res = atomic64_read(&sk->sk_cookie);
29
30 if (res)
31 return res;
32 res = gen_cookie_next(&sock_cookie);
33 atomic64_cmpxchg(&sk->sk_cookie, 0, res);
34 }
35}
36
37int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie)
38{
39 u64 res;
40
41 if (cookie[0] == INET_DIAG_NOCOOKIE && cookie[1] == INET_DIAG_NOCOOKIE)
42 return 0;
43
44 res = sock_gen_cookie(sk);
45 if ((u32)res != cookie[0] || (u32)(res >> 32) != cookie[1])
46 return -ESTALE;
47
48 return 0;
49}
50EXPORT_SYMBOL_GPL(sock_diag_check_cookie);
51
52void sock_diag_save_cookie(struct sock *sk, __u32 *cookie)
53{
54 u64 res = sock_gen_cookie(sk);
55
56 cookie[0] = (u32)res;
57 cookie[1] = (u32)(res >> 32);
58}
59EXPORT_SYMBOL_GPL(sock_diag_save_cookie);
60
61int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype)
62{
63 u32 mem[SK_MEMINFO_VARS];
64
65 sk_get_meminfo(sk, mem);
66
67 return nla_put(skb, attrtype, sizeof(mem), &mem);
68}
69EXPORT_SYMBOL_GPL(sock_diag_put_meminfo);
70
71int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk,
72 struct sk_buff *skb, int attrtype)
73{
74 struct sock_fprog_kern *fprog;
75 struct sk_filter *filter;
76 struct nlattr *attr;
77 unsigned int flen;
78 int err = 0;
79
80 if (!may_report_filterinfo) {
81 nla_reserve(skb, attrtype, 0);
82 return 0;
83 }
84
85 rcu_read_lock();
86 filter = rcu_dereference(sk->sk_filter);
87 if (!filter)
88 goto out;
89
90 fprog = filter->prog->orig_prog;
91 if (!fprog)
92 goto out;
93
94 flen = bpf_classic_proglen(fprog);
95
96 attr = nla_reserve(skb, attrtype, flen);
97 if (attr == NULL) {
98 err = -EMSGSIZE;
99 goto out;
100 }
101
102 memcpy(nla_data(attr), fprog->filter, flen);
103out:
104 rcu_read_unlock();
105 return err;
106}
107EXPORT_SYMBOL(sock_diag_put_filterinfo);
108
109struct broadcast_sk {
110 struct sock *sk;
111 struct work_struct work;
112};
113
114static size_t sock_diag_nlmsg_size(void)
115{
116 return NLMSG_ALIGN(sizeof(struct inet_diag_msg)
117 + nla_total_size(sizeof(u8)) /* INET_DIAG_PROTOCOL */
118 + nla_total_size_64bit(sizeof(struct tcp_info))); /* INET_DIAG_INFO */
119}
120
121static void sock_diag_broadcast_destroy_work(struct work_struct *work)
122{
123 struct broadcast_sk *bsk =
124 container_of(work, struct broadcast_sk, work);
125 struct sock *sk = bsk->sk;
126 const struct sock_diag_handler *hndl;
127 struct sk_buff *skb;
128 const enum sknetlink_groups group = sock_diag_destroy_group(sk);
129 int err = -1;
130
131 WARN_ON(group == SKNLGRP_NONE);
132
133 skb = nlmsg_new(sock_diag_nlmsg_size(), GFP_KERNEL);
134 if (!skb)
135 goto out;
136
137 mutex_lock(&sock_diag_table_mutex);
138 hndl = sock_diag_handlers[sk->sk_family];
139 if (hndl && hndl->get_info)
140 err = hndl->get_info(skb, sk);
141 mutex_unlock(&sock_diag_table_mutex);
142
143 if (!err)
144 nlmsg_multicast(sock_net(sk)->diag_nlsk, skb, 0, group,
145 GFP_KERNEL);
146 else
147 kfree_skb(skb);
148out:
149 sk_destruct(sk);
150 kfree(bsk);
151}
152
153void sock_diag_broadcast_destroy(struct sock *sk)
154{
155 /* Note, this function is often called from an interrupt context. */
156 struct broadcast_sk *bsk =
157 kmalloc(sizeof(struct broadcast_sk), GFP_ATOMIC);
158 if (!bsk)
159 return sk_destruct(sk);
160 bsk->sk = sk;
161 INIT_WORK(&bsk->work, sock_diag_broadcast_destroy_work);
162 queue_work(broadcast_wq, &bsk->work);
163}
164
165void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
166{
167 mutex_lock(&sock_diag_table_mutex);
168 inet_rcv_compat = fn;
169 mutex_unlock(&sock_diag_table_mutex);
170}
171EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat);
172
173void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
174{
175 mutex_lock(&sock_diag_table_mutex);
176 inet_rcv_compat = NULL;
177 mutex_unlock(&sock_diag_table_mutex);
178}
179EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat);
180
181int sock_diag_register(const struct sock_diag_handler *hndl)
182{
183 int err = 0;
184
185 if (hndl->family >= AF_MAX)
186 return -EINVAL;
187
188 mutex_lock(&sock_diag_table_mutex);
189 if (sock_diag_handlers[hndl->family])
190 err = -EBUSY;
191 else
192 sock_diag_handlers[hndl->family] = hndl;
193 mutex_unlock(&sock_diag_table_mutex);
194
195 return err;
196}
197EXPORT_SYMBOL_GPL(sock_diag_register);
198
199void sock_diag_unregister(const struct sock_diag_handler *hnld)
200{
201 int family = hnld->family;
202
203 if (family >= AF_MAX)
204 return;
205
206 mutex_lock(&sock_diag_table_mutex);
207 BUG_ON(sock_diag_handlers[family] != hnld);
208 sock_diag_handlers[family] = NULL;
209 mutex_unlock(&sock_diag_table_mutex);
210}
211EXPORT_SYMBOL_GPL(sock_diag_unregister);
212
213static int __sock_diag_cmd(struct sk_buff *skb, struct nlmsghdr *nlh)
214{
215 int err;
216 struct sock_diag_req *req = nlmsg_data(nlh);
217 const struct sock_diag_handler *hndl;
218
219 if (nlmsg_len(nlh) < sizeof(*req))
220 return -EINVAL;
221
222 if (req->sdiag_family >= AF_MAX)
223 return -EINVAL;
224 req->sdiag_family = array_index_nospec(req->sdiag_family, AF_MAX);
225
226 if (sock_diag_handlers[req->sdiag_family] == NULL)
227 sock_load_diag_module(req->sdiag_family, 0);
228
229 mutex_lock(&sock_diag_table_mutex);
230 hndl = sock_diag_handlers[req->sdiag_family];
231 if (hndl == NULL)
232 err = -ENOENT;
233 else if (nlh->nlmsg_type == SOCK_DIAG_BY_FAMILY)
234 err = hndl->dump(skb, nlh);
235 else if (nlh->nlmsg_type == SOCK_DESTROY && hndl->destroy)
236 err = hndl->destroy(skb, nlh);
237 else
238 err = -EOPNOTSUPP;
239 mutex_unlock(&sock_diag_table_mutex);
240
241 return err;
242}
243
244static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh,
245 struct netlink_ext_ack *extack)
246{
247 int ret;
248
249 switch (nlh->nlmsg_type) {
250 case TCPDIAG_GETSOCK:
251 case DCCPDIAG_GETSOCK:
252 if (inet_rcv_compat == NULL)
253 sock_load_diag_module(AF_INET, 0);
254
255 mutex_lock(&sock_diag_table_mutex);
256 if (inet_rcv_compat != NULL)
257 ret = inet_rcv_compat(skb, nlh);
258 else
259 ret = -EOPNOTSUPP;
260 mutex_unlock(&sock_diag_table_mutex);
261
262 return ret;
263 case SOCK_DIAG_BY_FAMILY:
264 case SOCK_DESTROY:
265 return __sock_diag_cmd(skb, nlh);
266 default:
267 return -EINVAL;
268 }
269}
270
271static DEFINE_MUTEX(sock_diag_mutex);
272
273static void sock_diag_rcv(struct sk_buff *skb)
274{
275 mutex_lock(&sock_diag_mutex);
276 netlink_rcv_skb(skb, &sock_diag_rcv_msg);
277 mutex_unlock(&sock_diag_mutex);
278}
279
280static int sock_diag_bind(struct net *net, int group)
281{
282 switch (group) {
283 case SKNLGRP_INET_TCP_DESTROY:
284 case SKNLGRP_INET_UDP_DESTROY:
285 if (!sock_diag_handlers[AF_INET])
286 sock_load_diag_module(AF_INET, 0);
287 break;
288 case SKNLGRP_INET6_TCP_DESTROY:
289 case SKNLGRP_INET6_UDP_DESTROY:
290 if (!sock_diag_handlers[AF_INET6])
291 sock_load_diag_module(AF_INET6, 0);
292 break;
293 }
294 return 0;
295}
296
297int sock_diag_destroy(struct sock *sk, int err)
298{
299 if (!ns_capable(sock_net(sk)->user_ns, CAP_NET_ADMIN))
300 return -EPERM;
301
302 if (!sk->sk_prot->diag_destroy)
303 return -EOPNOTSUPP;
304
305 return sk->sk_prot->diag_destroy(sk, err);
306}
307EXPORT_SYMBOL_GPL(sock_diag_destroy);
308
309static int __net_init diag_net_init(struct net *net)
310{
311 struct netlink_kernel_cfg cfg = {
312 .groups = SKNLGRP_MAX,
313 .input = sock_diag_rcv,
314 .bind = sock_diag_bind,
315 .flags = NL_CFG_F_NONROOT_RECV,
316 };
317
318 net->diag_nlsk = netlink_kernel_create(net, NETLINK_SOCK_DIAG, &cfg);
319 return net->diag_nlsk == NULL ? -ENOMEM : 0;
320}
321
322static void __net_exit diag_net_exit(struct net *net)
323{
324 netlink_kernel_release(net->diag_nlsk);
325 net->diag_nlsk = NULL;
326}
327
328static struct pernet_operations diag_net_ops = {
329 .init = diag_net_init,
330 .exit = diag_net_exit,
331};
332
333static int __init sock_diag_init(void)
334{
335 broadcast_wq = alloc_workqueue("sock_diag_events", 0, 0);
336 BUG_ON(!broadcast_wq);
337 return register_pernet_subsys(&diag_net_ops);
338}
339device_initcall(sock_diag_init);