blob: a0e9cf6379de3eac8ac1182c3be73e8d140b4a1b [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
Nicolas Dichtele8d96122013-04-25 06:53:54 +000052int sock_diag_put_filterinfo(struct user_namespace *user_ns, struct sock *sk,
53 struct sk_buff *skb, int attrtype)
54{
55 struct nlattr *attr;
56 struct sk_filter *filter;
57 unsigned int len;
58 int err = 0;
59
60 if (!ns_capable(user_ns, CAP_NET_ADMIN)) {
61 nla_reserve(skb, attrtype, 0);
62 return 0;
63 }
64
65 rcu_read_lock();
66
67 filter = rcu_dereference(sk->sk_filter);
68 len = filter ? filter->len * sizeof(struct sock_filter) : 0;
69
70 attr = nla_reserve(skb, attrtype, len);
71 if (attr == NULL) {
72 err = -EMSGSIZE;
73 goto out;
74 }
75
Nicolas Dichteled139982013-06-05 15:30:55 +020076 if (filter) {
77 struct sock_filter *fb = (struct sock_filter *)nla_data(attr);
78 int i;
79
80 for (i = 0; i < filter->len; i++, fb++)
81 sk_decode_filter(&filter->insns[i], fb);
82 }
Nicolas Dichtele8d96122013-04-25 06:53:54 +000083
84out:
85 rcu_read_unlock();
86 return err;
87}
88EXPORT_SYMBOL(sock_diag_put_filterinfo);
89
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +000090void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
91{
92 mutex_lock(&sock_diag_table_mutex);
93 inet_rcv_compat = fn;
94 mutex_unlock(&sock_diag_table_mutex);
95}
96EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat);
97
98void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
99{
100 mutex_lock(&sock_diag_table_mutex);
101 inet_rcv_compat = NULL;
102 mutex_unlock(&sock_diag_table_mutex);
103}
104EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat);
105
Shan Wei8dcf01f2012-04-24 18:21:07 +0000106int sock_diag_register(const struct sock_diag_handler *hndl)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000107{
108 int err = 0;
109
Dan Carpenter6f8e4ad2011-12-07 20:49:38 +0000110 if (hndl->family >= AF_MAX)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000111 return -EINVAL;
112
113 mutex_lock(&sock_diag_table_mutex);
114 if (sock_diag_handlers[hndl->family])
115 err = -EBUSY;
116 else
117 sock_diag_handlers[hndl->family] = hndl;
118 mutex_unlock(&sock_diag_table_mutex);
119
120 return err;
121}
122EXPORT_SYMBOL_GPL(sock_diag_register);
123
Shan Wei8dcf01f2012-04-24 18:21:07 +0000124void sock_diag_unregister(const struct sock_diag_handler *hnld)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000125{
126 int family = hnld->family;
127
Dan Carpenter6f8e4ad2011-12-07 20:49:38 +0000128 if (family >= AF_MAX)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000129 return;
130
131 mutex_lock(&sock_diag_table_mutex);
132 BUG_ON(sock_diag_handlers[family] != hnld);
133 sock_diag_handlers[family] = NULL;
134 mutex_unlock(&sock_diag_table_mutex);
135}
136EXPORT_SYMBOL_GPL(sock_diag_unregister);
137
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000138static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
139{
140 int err;
Thomas Graf7b468662012-06-26 23:36:11 +0000141 struct sock_diag_req *req = nlmsg_data(nlh);
Shan Wei8dcf01f2012-04-24 18:21:07 +0000142 const struct sock_diag_handler *hndl;
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000143
144 if (nlmsg_len(nlh) < sizeof(*req))
145 return -EINVAL;
146
Mathias Krause6e601a52013-02-23 01:13:47 +0000147 if (req->sdiag_family >= AF_MAX)
148 return -EINVAL;
149
Mathias Krause8e904552013-02-23 01:13:48 +0000150 if (sock_diag_handlers[req->sdiag_family] == NULL)
151 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
152 NETLINK_SOCK_DIAG, req->sdiag_family);
153
154 mutex_lock(&sock_diag_table_mutex);
155 hndl = sock_diag_handlers[req->sdiag_family];
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000156 if (hndl == NULL)
157 err = -ENOENT;
158 else
159 err = hndl->dump(skb, nlh);
Mathias Krause8e904552013-02-23 01:13:48 +0000160 mutex_unlock(&sock_diag_table_mutex);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000161
162 return err;
163}
164
165static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
166{
167 int ret;
168
169 switch (nlh->nlmsg_type) {
170 case TCPDIAG_GETSOCK:
171 case DCCPDIAG_GETSOCK:
172 if (inet_rcv_compat == NULL)
173 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +0000174 NETLINK_SOCK_DIAG, AF_INET);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000175
176 mutex_lock(&sock_diag_table_mutex);
177 if (inet_rcv_compat != NULL)
178 ret = inet_rcv_compat(skb, nlh);
179 else
180 ret = -EOPNOTSUPP;
181 mutex_unlock(&sock_diag_table_mutex);
182
183 return ret;
184 case SOCK_DIAG_BY_FAMILY:
185 return __sock_diag_rcv_msg(skb, nlh);
186 default:
187 return -EINVAL;
188 }
189}
190
191static DEFINE_MUTEX(sock_diag_mutex);
192
193static void sock_diag_rcv(struct sk_buff *skb)
194{
195 mutex_lock(&sock_diag_mutex);
196 netlink_rcv_skb(skb, &sock_diag_rcv_msg);
197 mutex_unlock(&sock_diag_mutex);
198}
199
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000200static int __net_init diag_net_init(struct net *net)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000201{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000202 struct netlink_kernel_cfg cfg = {
203 .input = sock_diag_rcv,
204 };
205
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000206 net->diag_nlsk = netlink_kernel_create(net, NETLINK_SOCK_DIAG, &cfg);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000207 return net->diag_nlsk == NULL ? -ENOMEM : 0;
208}
209
210static void __net_exit diag_net_exit(struct net *net)
211{
212 netlink_kernel_release(net->diag_nlsk);
213 net->diag_nlsk = NULL;
214}
215
216static struct pernet_operations diag_net_ops = {
217 .init = diag_net_init,
218 .exit = diag_net_exit,
219};
220
221static int __init sock_diag_init(void)
222{
223 return register_pernet_subsys(&diag_net_ops);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000224}
225
226static void __exit sock_diag_exit(void)
227{
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000228 unregister_pernet_subsys(&diag_net_ops);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000229}
230
231module_init(sock_diag_init);
232module_exit(sock_diag_exit);
233MODULE_LICENSE("GPL");
234MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG);