blob: ad704c757bb4a6e48c685fe0478ce6525b08a3ab [file] [log] [blame]
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00001#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>
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +00007#include <net/sock.h>
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00008
9#include <linux/inet_diag.h>
10#include <linux/sock_diag.h>
11
Shan Wei8dcf01f2012-04-24 18:21:07 +000012static const struct sock_diag_handler *sock_diag_handlers[AF_MAX];
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +000013static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh);
14static DEFINE_MUTEX(sock_diag_table_mutex);
15
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +000016int 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
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +000035int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype)
36{
Thomas Graf7b468662012-06-26 23:36:11 +000037 u32 mem[SK_MEMINFO_VARS];
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +000038
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);
Eric Dumazetd594e982012-06-04 03:50:35 +000046 mem[SK_MEMINFO_BACKLOG] = sk->sk_backlog.len;
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +000047
Thomas Graf7b468662012-06-26 23:36:11 +000048 return nla_put(skb, attrtype, sizeof(mem), &mem);
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +000049}
50EXPORT_SYMBOL_GPL(sock_diag_put_meminfo);
51
Eric W. Biedermana53b72c2014-04-23 14:26:25 -070052int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk,
Nicolas Dichtele8d96122013-04-25 06:53:54 +000053 struct sk_buff *skb, int attrtype)
54{
Daniel Borkmanna3ea2692014-03-28 18:58:19 +010055 struct sock_fprog_kern *fprog;
Nicolas Dichtele8d96122013-04-25 06:53:54 +000056 struct sk_filter *filter;
Daniel Borkmanna3ea2692014-03-28 18:58:19 +010057 struct nlattr *attr;
58 unsigned int flen;
Nicolas Dichtele8d96122013-04-25 06:53:54 +000059 int err = 0;
60
Eric W. Biedermana53b72c2014-04-23 14:26:25 -070061 if (!may_report_filterinfo) {
Nicolas Dichtele8d96122013-04-25 06:53:54 +000062 nla_reserve(skb, attrtype, 0);
63 return 0;
64 }
65
66 rcu_read_lock();
Nicolas Dichtele8d96122013-04-25 06:53:54 +000067 filter = rcu_dereference(sk->sk_filter);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +010068 if (!filter)
69 goto out;
Nicolas Dichtele8d96122013-04-25 06:53:54 +000070
Alexei Starovoitov7ae457c2014-07-30 20:34:16 -070071 fprog = filter->prog->orig_prog;
Alexei Starovoitov009937e2014-07-30 20:34:13 -070072 flen = bpf_classic_proglen(fprog);
Daniel Borkmanna3ea2692014-03-28 18:58:19 +010073
74 attr = nla_reserve(skb, attrtype, flen);
Nicolas Dichtele8d96122013-04-25 06:53:54 +000075 if (attr == NULL) {
76 err = -EMSGSIZE;
77 goto out;
78 }
79
Daniel Borkmanna3ea2692014-03-28 18:58:19 +010080 memcpy(nla_data(attr), fprog->filter, flen);
Nicolas Dichtele8d96122013-04-25 06:53:54 +000081out:
82 rcu_read_unlock();
83 return err;
84}
85EXPORT_SYMBOL(sock_diag_put_filterinfo);
86
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +000087void 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
Shan Wei8dcf01f2012-04-24 18:21:07 +0000103int sock_diag_register(const struct sock_diag_handler *hndl)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000104{
105 int err = 0;
106
Dan Carpenter6f8e4ad2011-12-07 20:49:38 +0000107 if (hndl->family >= AF_MAX)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000108 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
Shan Wei8dcf01f2012-04-24 18:21:07 +0000121void sock_diag_unregister(const struct sock_diag_handler *hnld)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000122{
123 int family = hnld->family;
124
Dan Carpenter6f8e4ad2011-12-07 20:49:38 +0000125 if (family >= AF_MAX)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000126 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
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000135static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
136{
137 int err;
Thomas Graf7b468662012-06-26 23:36:11 +0000138 struct sock_diag_req *req = nlmsg_data(nlh);
Shan Wei8dcf01f2012-04-24 18:21:07 +0000139 const struct sock_diag_handler *hndl;
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000140
141 if (nlmsg_len(nlh) < sizeof(*req))
142 return -EINVAL;
143
Mathias Krause6e601a52013-02-23 01:13:47 +0000144 if (req->sdiag_family >= AF_MAX)
145 return -EINVAL;
146
Mathias Krause8e904552013-02-23 01:13:48 +0000147 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];
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000153 if (hndl == NULL)
154 err = -ENOENT;
155 else
156 err = hndl->dump(skb, nlh);
Mathias Krause8e904552013-02-23 01:13:48 +0000157 mutex_unlock(&sock_diag_table_mutex);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000158
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,
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +0000171 NETLINK_SOCK_DIAG, AF_INET);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000172
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
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000197static int __net_init diag_net_init(struct net *net)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000198{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000199 struct netlink_kernel_cfg cfg = {
200 .input = sock_diag_rcv,
201 };
202
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000203 net->diag_nlsk = netlink_kernel_create(net, NETLINK_SOCK_DIAG, &cfg);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000204 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);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000221}
222
223static void __exit sock_diag_exit(void)
224{
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000225 unregister_pernet_subsys(&diag_net_ops);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000226}
227
228module_init(sock_diag_init);
229module_exit(sock_diag_exit);
230MODULE_LICENSE("GPL");
231MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG);