blob: aa74be442dfbf88dfd1f7940e1eeaee63a3238fc [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 <linux/rtnetlink.h>
8#include <net/sock.h>
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +00009
10#include <linux/inet_diag.h>
11#include <linux/sock_diag.h>
12
13static struct sock_diag_handler *sock_diag_handlers[AF_MAX];
14static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh);
15static DEFINE_MUTEX(sock_diag_table_mutex);
16
Pavel Emelyanovf65c1b52011-12-15 02:43:44 +000017int sock_diag_check_cookie(void *sk, __u32 *cookie)
18{
19 if ((cookie[0] != INET_DIAG_NOCOOKIE ||
20 cookie[1] != INET_DIAG_NOCOOKIE) &&
21 ((u32)(unsigned long)sk != cookie[0] ||
22 (u32)((((unsigned long)sk) >> 31) >> 1) != cookie[1]))
23 return -ESTALE;
24 else
25 return 0;
26}
27EXPORT_SYMBOL_GPL(sock_diag_check_cookie);
28
29void sock_diag_save_cookie(void *sk, __u32 *cookie)
30{
31 cookie[0] = (u32)(unsigned long)sk;
32 cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
33}
34EXPORT_SYMBOL_GPL(sock_diag_save_cookie);
35
Pavel Emelyanov5d2e5f22011-12-30 00:53:13 +000036int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype)
37{
38 __u32 *mem;
39
40 mem = RTA_DATA(__RTA_PUT(skb, attrtype, SK_MEMINFO_VARS * sizeof(__u32)));
41
42 mem[SK_MEMINFO_RMEM_ALLOC] = sk_rmem_alloc_get(sk);
43 mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf;
44 mem[SK_MEMINFO_WMEM_ALLOC] = sk_wmem_alloc_get(sk);
45 mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf;
46 mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc;
47 mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued;
48 mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc);
49
50 return 0;
51
52rtattr_failure:
53 return -EMSGSIZE;
54}
55EXPORT_SYMBOL_GPL(sock_diag_put_meminfo);
56
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +000057void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
58{
59 mutex_lock(&sock_diag_table_mutex);
60 inet_rcv_compat = fn;
61 mutex_unlock(&sock_diag_table_mutex);
62}
63EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat);
64
65void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
66{
67 mutex_lock(&sock_diag_table_mutex);
68 inet_rcv_compat = NULL;
69 mutex_unlock(&sock_diag_table_mutex);
70}
71EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat);
72
73int sock_diag_register(struct sock_diag_handler *hndl)
74{
75 int err = 0;
76
Dan Carpenter6f8e4ad2011-12-07 20:49:38 +000077 if (hndl->family >= AF_MAX)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +000078 return -EINVAL;
79
80 mutex_lock(&sock_diag_table_mutex);
81 if (sock_diag_handlers[hndl->family])
82 err = -EBUSY;
83 else
84 sock_diag_handlers[hndl->family] = hndl;
85 mutex_unlock(&sock_diag_table_mutex);
86
87 return err;
88}
89EXPORT_SYMBOL_GPL(sock_diag_register);
90
91void sock_diag_unregister(struct sock_diag_handler *hnld)
92{
93 int family = hnld->family;
94
Dan Carpenter6f8e4ad2011-12-07 20:49:38 +000095 if (family >= AF_MAX)
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +000096 return;
97
98 mutex_lock(&sock_diag_table_mutex);
99 BUG_ON(sock_diag_handlers[family] != hnld);
100 sock_diag_handlers[family] = NULL;
101 mutex_unlock(&sock_diag_table_mutex);
102}
103EXPORT_SYMBOL_GPL(sock_diag_unregister);
104
105static inline struct sock_diag_handler *sock_diag_lock_handler(int family)
106{
107 if (sock_diag_handlers[family] == NULL)
108 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +0000109 NETLINK_SOCK_DIAG, family);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000110
111 mutex_lock(&sock_diag_table_mutex);
112 return sock_diag_handlers[family];
113}
114
115static inline void sock_diag_unlock_handler(struct sock_diag_handler *h)
116{
117 mutex_unlock(&sock_diag_table_mutex);
118}
119
120static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
121{
122 int err;
123 struct sock_diag_req *req = NLMSG_DATA(nlh);
124 struct sock_diag_handler *hndl;
125
126 if (nlmsg_len(nlh) < sizeof(*req))
127 return -EINVAL;
128
Mathias Krause1178efd2013-02-23 01:13:47 +0000129 if (req->sdiag_family >= AF_MAX)
130 return -EINVAL;
131
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000132 hndl = sock_diag_lock_handler(req->sdiag_family);
133 if (hndl == NULL)
134 err = -ENOENT;
135 else
136 err = hndl->dump(skb, nlh);
137 sock_diag_unlock_handler(hndl);
138
139 return err;
140}
141
142static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
143{
144 int ret;
145
146 switch (nlh->nlmsg_type) {
147 case TCPDIAG_GETSOCK:
148 case DCCPDIAG_GETSOCK:
149 if (inet_rcv_compat == NULL)
150 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +0000151 NETLINK_SOCK_DIAG, AF_INET);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000152
153 mutex_lock(&sock_diag_table_mutex);
154 if (inet_rcv_compat != NULL)
155 ret = inet_rcv_compat(skb, nlh);
156 else
157 ret = -EOPNOTSUPP;
158 mutex_unlock(&sock_diag_table_mutex);
159
160 return ret;
161 case SOCK_DIAG_BY_FAMILY:
162 return __sock_diag_rcv_msg(skb, nlh);
163 default:
164 return -EINVAL;
165 }
166}
167
168static DEFINE_MUTEX(sock_diag_mutex);
169
170static void sock_diag_rcv(struct sk_buff *skb)
171{
172 mutex_lock(&sock_diag_mutex);
173 netlink_rcv_skb(skb, &sock_diag_rcv_msg);
174 mutex_unlock(&sock_diag_mutex);
175}
176
177struct sock *sock_diag_nlsk;
178EXPORT_SYMBOL_GPL(sock_diag_nlsk);
179
180static int __init sock_diag_init(void)
181{
182 sock_diag_nlsk = netlink_kernel_create(&init_net, NETLINK_SOCK_DIAG, 0,
183 sock_diag_rcv, NULL, THIS_MODULE);
184 return sock_diag_nlsk == NULL ? -ENOMEM : 0;
185}
186
187static void __exit sock_diag_exit(void)
188{
189 netlink_kernel_release(sock_diag_nlsk);
190}
191
192module_init(sock_diag_init);
193module_exit(sock_diag_exit);
194MODULE_LICENSE("GPL");
195MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG);