blob: b9868e1fd62cc0a03ab4b449e44612cf8ff8c5b0 [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
129 hndl = sock_diag_lock_handler(req->sdiag_family);
130 if (hndl == NULL)
131 err = -ENOENT;
132 else
133 err = hndl->dump(skb, nlh);
134 sock_diag_unlock_handler(hndl);
135
136 return err;
137}
138
139static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
140{
141 int ret;
142
143 switch (nlh->nlmsg_type) {
144 case TCPDIAG_GETSOCK:
145 case DCCPDIAG_GETSOCK:
146 if (inet_rcv_compat == NULL)
147 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
Pavel Emelyanovaec8dc62011-12-15 02:43:27 +0000148 NETLINK_SOCK_DIAG, AF_INET);
Pavel Emelyanov8ef874b2011-12-06 07:59:52 +0000149
150 mutex_lock(&sock_diag_table_mutex);
151 if (inet_rcv_compat != NULL)
152 ret = inet_rcv_compat(skb, nlh);
153 else
154 ret = -EOPNOTSUPP;
155 mutex_unlock(&sock_diag_table_mutex);
156
157 return ret;
158 case SOCK_DIAG_BY_FAMILY:
159 return __sock_diag_rcv_msg(skb, nlh);
160 default:
161 return -EINVAL;
162 }
163}
164
165static DEFINE_MUTEX(sock_diag_mutex);
166
167static void sock_diag_rcv(struct sk_buff *skb)
168{
169 mutex_lock(&sock_diag_mutex);
170 netlink_rcv_skb(skb, &sock_diag_rcv_msg);
171 mutex_unlock(&sock_diag_mutex);
172}
173
174struct sock *sock_diag_nlsk;
175EXPORT_SYMBOL_GPL(sock_diag_nlsk);
176
177static int __init sock_diag_init(void)
178{
179 sock_diag_nlsk = netlink_kernel_create(&init_net, NETLINK_SOCK_DIAG, 0,
180 sock_diag_rcv, NULL, THIS_MODULE);
181 return sock_diag_nlsk == NULL ? -ENOMEM : 0;
182}
183
184static void __exit sock_diag_exit(void)
185{
186 netlink_kernel_release(sock_diag_nlsk);
187}
188
189module_init(sock_diag_init);
190module_exit(sock_diag_exit);
191MODULE_LICENSE("GPL");
192MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG);