1 #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>
9 #include <linux/inet_diag.h>
10 #include <linux/sock_diag.h>
12 static const struct sock_diag_handler *sock_diag_handlers[AF_MAX];
13 static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh);
14 static DEFINE_MUTEX(sock_diag_table_mutex);
16 int sock_diag_check_cookie(void *sk, __u32 *cookie)
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]))
26 EXPORT_SYMBOL_GPL(sock_diag_check_cookie);
28 void sock_diag_save_cookie(void *sk, __u32 *cookie)
30 cookie[0] = (u32)(unsigned long)sk;
31 cookie[1] = (u32)(((unsigned long)sk >> 31) >> 1);
33 EXPORT_SYMBOL_GPL(sock_diag_save_cookie);
35 int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype)
37 u32 mem[SK_MEMINFO_VARS];
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);
46 mem[SK_MEMINFO_BACKLOG] = sk->sk_backlog.len;
48 return nla_put(skb, attrtype, sizeof(mem), &mem);
50 EXPORT_SYMBOL_GPL(sock_diag_put_meminfo);
52 int sock_diag_put_filterinfo(struct user_namespace *user_ns, struct sock *sk,
53 struct sk_buff *skb, int attrtype)
56 struct sk_filter *filter;
60 if (!ns_capable(user_ns, CAP_NET_ADMIN)) {
61 nla_reserve(skb, attrtype, 0);
67 filter = rcu_dereference(sk->sk_filter);
68 len = filter ? filter->len * sizeof(struct sock_filter) : 0;
70 attr = nla_reserve(skb, attrtype, len);
77 memcpy(nla_data(attr), filter->insns, len);
83 EXPORT_SYMBOL(sock_diag_put_filterinfo);
85 void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
87 mutex_lock(&sock_diag_table_mutex);
89 mutex_unlock(&sock_diag_table_mutex);
91 EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat);
93 void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh))
95 mutex_lock(&sock_diag_table_mutex);
96 inet_rcv_compat = NULL;
97 mutex_unlock(&sock_diag_table_mutex);
99 EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat);
101 int sock_diag_register(const struct sock_diag_handler *hndl)
105 if (hndl->family >= AF_MAX)
108 mutex_lock(&sock_diag_table_mutex);
109 if (sock_diag_handlers[hndl->family])
112 sock_diag_handlers[hndl->family] = hndl;
113 mutex_unlock(&sock_diag_table_mutex);
117 EXPORT_SYMBOL_GPL(sock_diag_register);
119 void sock_diag_unregister(const struct sock_diag_handler *hnld)
121 int family = hnld->family;
123 if (family >= AF_MAX)
126 mutex_lock(&sock_diag_table_mutex);
127 BUG_ON(sock_diag_handlers[family] != hnld);
128 sock_diag_handlers[family] = NULL;
129 mutex_unlock(&sock_diag_table_mutex);
131 EXPORT_SYMBOL_GPL(sock_diag_unregister);
133 static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
136 struct sock_diag_req *req = nlmsg_data(nlh);
137 const struct sock_diag_handler *hndl;
139 if (nlmsg_len(nlh) < sizeof(*req))
142 if (req->sdiag_family >= AF_MAX)
145 if (sock_diag_handlers[req->sdiag_family] == NULL)
146 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
147 NETLINK_SOCK_DIAG, req->sdiag_family);
149 mutex_lock(&sock_diag_table_mutex);
150 hndl = sock_diag_handlers[req->sdiag_family];
154 err = hndl->dump(skb, nlh);
155 mutex_unlock(&sock_diag_table_mutex);
160 static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh)
164 switch (nlh->nlmsg_type) {
165 case TCPDIAG_GETSOCK:
166 case DCCPDIAG_GETSOCK:
167 if (inet_rcv_compat == NULL)
168 request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK,
169 NETLINK_SOCK_DIAG, AF_INET);
171 mutex_lock(&sock_diag_table_mutex);
172 if (inet_rcv_compat != NULL)
173 ret = inet_rcv_compat(skb, nlh);
176 mutex_unlock(&sock_diag_table_mutex);
179 case SOCK_DIAG_BY_FAMILY:
180 return __sock_diag_rcv_msg(skb, nlh);
186 static DEFINE_MUTEX(sock_diag_mutex);
188 static void sock_diag_rcv(struct sk_buff *skb)
190 mutex_lock(&sock_diag_mutex);
191 netlink_rcv_skb(skb, &sock_diag_rcv_msg);
192 mutex_unlock(&sock_diag_mutex);
195 static int __net_init diag_net_init(struct net *net)
197 struct netlink_kernel_cfg cfg = {
198 .input = sock_diag_rcv,
201 net->diag_nlsk = netlink_kernel_create(net, NETLINK_SOCK_DIAG, &cfg);
202 return net->diag_nlsk == NULL ? -ENOMEM : 0;
205 static void __net_exit diag_net_exit(struct net *net)
207 netlink_kernel_release(net->diag_nlsk);
208 net->diag_nlsk = NULL;
211 static struct pernet_operations diag_net_ops = {
212 .init = diag_net_init,
213 .exit = diag_net_exit,
216 static int __init sock_diag_init(void)
218 return register_pernet_subsys(&diag_net_ops);
221 static void __exit sock_diag_exit(void)
223 unregister_pernet_subsys(&diag_net_ops);
226 module_init(sock_diag_init);
227 module_exit(sock_diag_exit);
228 MODULE_LICENSE("GPL");
229 MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG);