blob: 602cd637182ebb321af6773d2ccfe9a8945d44c5 [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
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +000052void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
53{
54 mutex_lock(&sock_diag_table_mutex);
55 inet_rcv_compat = fn;
56 mutex_unlock(&sock_diag_table_mutex);
57}
58EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat);
59
60void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
61{
62 mutex_lock(&sock_diag_table_mutex);
63 inet_rcv_compat = NULL;
64 mutex_unlock(&sock_diag_table_mutex);
65}
66EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat);
67
Shan Wei8dcf01f2012-04-24 18:21:07 +000068int sock_diag_register(const struct sock_diag_handler *hndl)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +000069{
70 int err = 0;
71
Dan Carpenter6f8e4ad2011-12-07 20:49:38 +000072 if (hndl->family >= AF_MAX)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +000073 return -EINVAL;
74
75 mutex_lock(&sock_diag_table_mutex);
76 if (sock_diag_handlers[hndl->family])
77 err = -EBUSY;
78 else
79 sock_diag_handlers[hndl->family] = hndl;
80 mutex_unlock(&sock_diag_table_mutex);
81
82 return err;
83}
84EXPORT_SYMBOL_GPL(sock_diag_register);
85
Shan Wei8dcf01f2012-04-24 18:21:07 +000086void sock_diag_unregister(const struct sock_diag_handler *hnld)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +000087{
88 int family = hnld->family;
89
Dan Carpenter6f8e4ad2011-12-07 20:49:38 +000090 if (family >= AF_MAX)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +000091 return;
92
93 mutex_lock(&sock_diag_table_mutex);
94 BUG_ON(sock_diag_handlers[family] != hnld);
95 sock_diag_handlers[family] = NULL;
96 mutex_unlock(&sock_diag_table_mutex);
97}
98EXPORT_SYMBOL_GPL(sock_diag_unregister);
99
Shan Wei8dcf01f2012-04-24 18:21:07 +0000100static const inline struct sock_diag_handler *sock_diag_lock_handler(int family)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000101{
102 if (sock_diag_handlers[family] == NULL)
103 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +0000104 NETLINK_SOCK_DIAG, family);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000105
106 mutex_lock(&sock_diag_table_mutex);
107 return sock_diag_handlers[family];
108}
109
Shan Wei8dcf01f2012-04-24 18:21:07 +0000110static inline void sock_diag_unlock_handler(const struct sock_diag_handler *h)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000111{
112 mutex_unlock(&sock_diag_table_mutex);
113}
114
115static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
116{
117 int err;
Thomas Graf7b468662012-06-26 23:36:11 +0000118 struct sock_diag_req *req = nlmsg_data(nlh);
Shan Wei8dcf01f2012-04-24 18:21:07 +0000119 const struct sock_diag_handler *hndl;
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000120
121 if (nlmsg_len(nlh) < sizeof(*req))
122 return -EINVAL;
123
124 hndl = sock_diag_lock_handler(req->sdiag_family);
125 if (hndl == NULL)
126 err = -ENOENT;
127 else
128 err = hndl->dump(skb, nlh);
129 sock_diag_unlock_handler(hndl);
130
131 return err;
132}
133
134static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
135{
136 int ret;
137
138 switch (nlh->nlmsg_type) {
139 case TCPDIAG_GETSOCK:
140 case DCCPDIAG_GETSOCK:
141 if (inet_rcv_compat == NULL)
142 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +0000143 NETLINK_SOCK_DIAG, AF_INET);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000144
145 mutex_lock(&sock_diag_table_mutex);
146 if (inet_rcv_compat != NULL)
147 ret = inet_rcv_compat(skb, nlh);
148 else
149 ret = -EOPNOTSUPP;
150 mutex_unlock(&sock_diag_table_mutex);
151
152 return ret;
153 case SOCK_DIAG_BY_FAMILY:
154 return __sock_diag_rcv_msg(skb, nlh);
155 default:
156 return -EINVAL;
157 }
158}
159
160static DEFINE_MUTEX(sock_diag_mutex);
161
162static void sock_diag_rcv(struct sk_buff *skb)
163{
164 mutex_lock(&sock_diag_mutex);
165 netlink_rcv_skb(skb, &sock_diag_rcv_msg);
166 mutex_unlock(&sock_diag_mutex);
167}
168
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000169static int __net_init diag_net_init(struct net *net)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000170{
Pablo Neira Ayusoa31f2d12012-06-29 06:15:21 +0000171 struct netlink_kernel_cfg cfg = {
172 .input = sock_diag_rcv,
173 };
174
Pablo Neira Ayuso9f00d972012-09-08 02:53:54 +0000175 net->diag_nlsk = netlink_kernel_create(net, NETLINK_SOCK_DIAG, &cfg);
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000176 return net->diag_nlsk == NULL ? -ENOMEM : 0;
177}
178
179static void __net_exit diag_net_exit(struct net *net)
180{
181 netlink_kernel_release(net->diag_nlsk);
182 net->diag_nlsk = NULL;
183}
184
185static struct pernet_operations diag_net_ops = {
186 .init = diag_net_init,
187 .exit = diag_net_exit,
188};
189
190static int __init sock_diag_init(void)
191{
192 return register_pernet_subsys(&diag_net_ops);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000193}
194
195static void __exit sock_diag_exit(void)
196{
Andrey Vagin51d7ccc2012-07-16 04:28:49 +0000197 unregister_pernet_subsys(&diag_net_ops);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000198}
199
200module_init(sock_diag_init);
201module_exit(sock_diag_exit);
202MODULE_LICENSE("GPL");
203MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG);