Commit | Line | Data |
---|---|---|
8ef874bf PE |
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> | |
5d2e5f27 | 7 | #include <net/sock.h> |
eb4cb008 CG |
8 | #include <linux/kernel.h> |
9 | #include <linux/tcp.h> | |
10 | #include <linux/workqueue.h> | |
8ef874bf PE |
11 | |
12 | #include <linux/inet_diag.h> | |
13 | #include <linux/sock_diag.h> | |
14 | ||
8dcf01fc | 15 | static const struct sock_diag_handler *sock_diag_handlers[AF_MAX]; |
8ef874bf PE |
16 | static int (*inet_rcv_compat)(struct sk_buff *skb, struct nlmsghdr *nlh); |
17 | static DEFINE_MUTEX(sock_diag_table_mutex); | |
eb4cb008 | 18 | static struct workqueue_struct *broadcast_wq; |
8ef874bf | 19 | |
33cf7c90 | 20 | static u64 sock_gen_cookie(struct sock *sk) |
f65c1b53 | 21 | { |
33cf7c90 ED |
22 | while (1) { |
23 | u64 res = atomic64_read(&sk->sk_cookie); | |
24 | ||
25 | if (res) | |
26 | return res; | |
27 | res = atomic64_inc_return(&sock_net(sk)->cookie_gen); | |
28 | atomic64_cmpxchg(&sk->sk_cookie, 0, res); | |
29 | } | |
30 | } | |
31 | ||
32 | int sock_diag_check_cookie(struct sock *sk, const __u32 *cookie) | |
33 | { | |
34 | u64 res; | |
35 | ||
36 | if (cookie[0] == INET_DIAG_NOCOOKIE && cookie[1] == INET_DIAG_NOCOOKIE) | |
f65c1b53 | 37 | return 0; |
33cf7c90 ED |
38 | |
39 | res = sock_gen_cookie(sk); | |
40 | if ((u32)res != cookie[0] || (u32)(res >> 32) != cookie[1]) | |
41 | return -ESTALE; | |
42 | ||
43 | return 0; | |
f65c1b53 PE |
44 | } |
45 | EXPORT_SYMBOL_GPL(sock_diag_check_cookie); | |
46 | ||
33cf7c90 | 47 | void sock_diag_save_cookie(struct sock *sk, __u32 *cookie) |
f65c1b53 | 48 | { |
33cf7c90 ED |
49 | u64 res = sock_gen_cookie(sk); |
50 | ||
51 | cookie[0] = (u32)res; | |
52 | cookie[1] = (u32)(res >> 32); | |
f65c1b53 PE |
53 | } |
54 | EXPORT_SYMBOL_GPL(sock_diag_save_cookie); | |
55 | ||
5d2e5f27 PE |
56 | int sock_diag_put_meminfo(struct sock *sk, struct sk_buff *skb, int attrtype) |
57 | { | |
7b46866d | 58 | u32 mem[SK_MEMINFO_VARS]; |
5d2e5f27 PE |
59 | |
60 | mem[SK_MEMINFO_RMEM_ALLOC] = sk_rmem_alloc_get(sk); | |
61 | mem[SK_MEMINFO_RCVBUF] = sk->sk_rcvbuf; | |
62 | mem[SK_MEMINFO_WMEM_ALLOC] = sk_wmem_alloc_get(sk); | |
63 | mem[SK_MEMINFO_SNDBUF] = sk->sk_sndbuf; | |
64 | mem[SK_MEMINFO_FWD_ALLOC] = sk->sk_forward_alloc; | |
65 | mem[SK_MEMINFO_WMEM_QUEUED] = sk->sk_wmem_queued; | |
66 | mem[SK_MEMINFO_OPTMEM] = atomic_read(&sk->sk_omem_alloc); | |
d594e987 | 67 | mem[SK_MEMINFO_BACKLOG] = sk->sk_backlog.len; |
5d2e5f27 | 68 | |
7b46866d | 69 | return nla_put(skb, attrtype, sizeof(mem), &mem); |
5d2e5f27 PE |
70 | } |
71 | EXPORT_SYMBOL_GPL(sock_diag_put_meminfo); | |
72 | ||
a53b72c8 | 73 | int sock_diag_put_filterinfo(bool may_report_filterinfo, struct sock *sk, |
e8d9612c ND |
74 | struct sk_buff *skb, int attrtype) |
75 | { | |
a3ea269b | 76 | struct sock_fprog_kern *fprog; |
e8d9612c | 77 | struct sk_filter *filter; |
a3ea269b DB |
78 | struct nlattr *attr; |
79 | unsigned int flen; | |
e8d9612c ND |
80 | int err = 0; |
81 | ||
a53b72c8 | 82 | if (!may_report_filterinfo) { |
e8d9612c ND |
83 | nla_reserve(skb, attrtype, 0); |
84 | return 0; | |
85 | } | |
86 | ||
87 | rcu_read_lock(); | |
e8d9612c | 88 | filter = rcu_dereference(sk->sk_filter); |
a3ea269b DB |
89 | if (!filter) |
90 | goto out; | |
e8d9612c | 91 | |
7ae457c1 | 92 | fprog = filter->prog->orig_prog; |
009937e7 | 93 | flen = bpf_classic_proglen(fprog); |
a3ea269b DB |
94 | |
95 | attr = nla_reserve(skb, attrtype, flen); | |
e8d9612c ND |
96 | if (attr == NULL) { |
97 | err = -EMSGSIZE; | |
98 | goto out; | |
99 | } | |
100 | ||
a3ea269b | 101 | memcpy(nla_data(attr), fprog->filter, flen); |
e8d9612c ND |
102 | out: |
103 | rcu_read_unlock(); | |
104 | return err; | |
105 | } | |
106 | EXPORT_SYMBOL(sock_diag_put_filterinfo); | |
107 | ||
eb4cb008 CG |
108 | struct broadcast_sk { |
109 | struct sock *sk; | |
110 | struct work_struct work; | |
111 | }; | |
112 | ||
113 | static size_t sock_diag_nlmsg_size(void) | |
114 | { | |
115 | return NLMSG_ALIGN(sizeof(struct inet_diag_msg) | |
116 | + nla_total_size(sizeof(u8)) /* INET_DIAG_PROTOCOL */ | |
117 | + nla_total_size(sizeof(struct tcp_info))); /* INET_DIAG_INFO */ | |
118 | } | |
119 | ||
120 | static void sock_diag_broadcast_destroy_work(struct work_struct *work) | |
121 | { | |
122 | struct broadcast_sk *bsk = | |
123 | container_of(work, struct broadcast_sk, work); | |
124 | struct sock *sk = bsk->sk; | |
125 | const struct sock_diag_handler *hndl; | |
126 | struct sk_buff *skb; | |
127 | const enum sknetlink_groups group = sock_diag_destroy_group(sk); | |
128 | int err = -1; | |
129 | ||
130 | WARN_ON(group == SKNLGRP_NONE); | |
131 | ||
132 | skb = nlmsg_new(sock_diag_nlmsg_size(), GFP_KERNEL); | |
133 | if (!skb) | |
134 | goto out; | |
135 | ||
136 | mutex_lock(&sock_diag_table_mutex); | |
137 | hndl = sock_diag_handlers[sk->sk_family]; | |
138 | if (hndl && hndl->get_info) | |
139 | err = hndl->get_info(skb, sk); | |
140 | mutex_unlock(&sock_diag_table_mutex); | |
141 | ||
142 | if (!err) | |
143 | nlmsg_multicast(sock_net(sk)->diag_nlsk, skb, 0, group, | |
144 | GFP_KERNEL); | |
145 | else | |
146 | kfree_skb(skb); | |
147 | out: | |
148 | sk_destruct(sk); | |
149 | kfree(bsk); | |
150 | } | |
151 | ||
152 | void sock_diag_broadcast_destroy(struct sock *sk) | |
153 | { | |
154 | /* Note, this function is often called from an interrupt context. */ | |
155 | struct broadcast_sk *bsk = | |
156 | kmalloc(sizeof(struct broadcast_sk), GFP_ATOMIC); | |
157 | if (!bsk) | |
158 | return sk_destruct(sk); | |
159 | bsk->sk = sk; | |
160 | INIT_WORK(&bsk->work, sock_diag_broadcast_destroy_work); | |
161 | queue_work(broadcast_wq, &bsk->work); | |
162 | } | |
163 | ||
8ef874bf PE |
164 | void sock_diag_register_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh)) |
165 | { | |
166 | mutex_lock(&sock_diag_table_mutex); | |
167 | inet_rcv_compat = fn; | |
168 | mutex_unlock(&sock_diag_table_mutex); | |
169 | } | |
170 | EXPORT_SYMBOL_GPL(sock_diag_register_inet_compat); | |
171 | ||
172 | void sock_diag_unregister_inet_compat(int (*fn)(struct sk_buff *skb, struct nlmsghdr *nlh)) | |
173 | { | |
174 | mutex_lock(&sock_diag_table_mutex); | |
175 | inet_rcv_compat = NULL; | |
176 | mutex_unlock(&sock_diag_table_mutex); | |
177 | } | |
178 | EXPORT_SYMBOL_GPL(sock_diag_unregister_inet_compat); | |
179 | ||
8dcf01fc | 180 | int sock_diag_register(const struct sock_diag_handler *hndl) |
8ef874bf PE |
181 | { |
182 | int err = 0; | |
183 | ||
6f8e4ad0 | 184 | if (hndl->family >= AF_MAX) |
8ef874bf PE |
185 | return -EINVAL; |
186 | ||
187 | mutex_lock(&sock_diag_table_mutex); | |
188 | if (sock_diag_handlers[hndl->family]) | |
189 | err = -EBUSY; | |
190 | else | |
191 | sock_diag_handlers[hndl->family] = hndl; | |
192 | mutex_unlock(&sock_diag_table_mutex); | |
193 | ||
194 | return err; | |
195 | } | |
196 | EXPORT_SYMBOL_GPL(sock_diag_register); | |
197 | ||
8dcf01fc | 198 | void sock_diag_unregister(const struct sock_diag_handler *hnld) |
8ef874bf PE |
199 | { |
200 | int family = hnld->family; | |
201 | ||
6f8e4ad0 | 202 | if (family >= AF_MAX) |
8ef874bf PE |
203 | return; |
204 | ||
205 | mutex_lock(&sock_diag_table_mutex); | |
206 | BUG_ON(sock_diag_handlers[family] != hnld); | |
207 | sock_diag_handlers[family] = NULL; | |
208 | mutex_unlock(&sock_diag_table_mutex); | |
209 | } | |
210 | EXPORT_SYMBOL_GPL(sock_diag_unregister); | |
211 | ||
8ef874bf PE |
212 | static int __sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) |
213 | { | |
214 | int err; | |
7b46866d | 215 | struct sock_diag_req *req = nlmsg_data(nlh); |
8dcf01fc | 216 | const struct sock_diag_handler *hndl; |
8ef874bf PE |
217 | |
218 | if (nlmsg_len(nlh) < sizeof(*req)) | |
219 | return -EINVAL; | |
220 | ||
6e601a53 MK |
221 | if (req->sdiag_family >= AF_MAX) |
222 | return -EINVAL; | |
223 | ||
8e904550 MK |
224 | if (sock_diag_handlers[req->sdiag_family] == NULL) |
225 | request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, | |
226 | NETLINK_SOCK_DIAG, req->sdiag_family); | |
227 | ||
228 | mutex_lock(&sock_diag_table_mutex); | |
229 | hndl = sock_diag_handlers[req->sdiag_family]; | |
8ef874bf PE |
230 | if (hndl == NULL) |
231 | err = -ENOENT; | |
232 | else | |
233 | err = hndl->dump(skb, nlh); | |
8e904550 | 234 | mutex_unlock(&sock_diag_table_mutex); |
8ef874bf PE |
235 | |
236 | return err; | |
237 | } | |
238 | ||
239 | static int sock_diag_rcv_msg(struct sk_buff *skb, struct nlmsghdr *nlh) | |
240 | { | |
241 | int ret; | |
242 | ||
243 | switch (nlh->nlmsg_type) { | |
244 | case TCPDIAG_GETSOCK: | |
245 | case DCCPDIAG_GETSOCK: | |
246 | if (inet_rcv_compat == NULL) | |
247 | request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, | |
aec8dc62 | 248 | NETLINK_SOCK_DIAG, AF_INET); |
8ef874bf PE |
249 | |
250 | mutex_lock(&sock_diag_table_mutex); | |
251 | if (inet_rcv_compat != NULL) | |
252 | ret = inet_rcv_compat(skb, nlh); | |
253 | else | |
254 | ret = -EOPNOTSUPP; | |
255 | mutex_unlock(&sock_diag_table_mutex); | |
256 | ||
257 | return ret; | |
258 | case SOCK_DIAG_BY_FAMILY: | |
259 | return __sock_diag_rcv_msg(skb, nlh); | |
260 | default: | |
261 | return -EINVAL; | |
262 | } | |
263 | } | |
264 | ||
265 | static DEFINE_MUTEX(sock_diag_mutex); | |
266 | ||
267 | static void sock_diag_rcv(struct sk_buff *skb) | |
268 | { | |
269 | mutex_lock(&sock_diag_mutex); | |
270 | netlink_rcv_skb(skb, &sock_diag_rcv_msg); | |
271 | mutex_unlock(&sock_diag_mutex); | |
272 | } | |
273 | ||
eb4cb008 CG |
274 | static int sock_diag_bind(struct net *net, int group) |
275 | { | |
276 | switch (group) { | |
277 | case SKNLGRP_INET_TCP_DESTROY: | |
278 | case SKNLGRP_INET_UDP_DESTROY: | |
279 | if (!sock_diag_handlers[AF_INET]) | |
280 | request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, | |
281 | NETLINK_SOCK_DIAG, AF_INET); | |
282 | break; | |
283 | case SKNLGRP_INET6_TCP_DESTROY: | |
284 | case SKNLGRP_INET6_UDP_DESTROY: | |
285 | if (!sock_diag_handlers[AF_INET6]) | |
286 | request_module("net-pf-%d-proto-%d-type-%d", PF_NETLINK, | |
287 | NETLINK_SOCK_DIAG, AF_INET); | |
288 | break; | |
289 | } | |
290 | return 0; | |
291 | } | |
292 | ||
51d7cccf | 293 | static int __net_init diag_net_init(struct net *net) |
8ef874bf | 294 | { |
a31f2d17 | 295 | struct netlink_kernel_cfg cfg = { |
eb4cb008 | 296 | .groups = SKNLGRP_MAX, |
a31f2d17 | 297 | .input = sock_diag_rcv, |
eb4cb008 CG |
298 | .bind = sock_diag_bind, |
299 | .flags = NL_CFG_F_NONROOT_RECV, | |
a31f2d17 PNA |
300 | }; |
301 | ||
9f00d977 | 302 | net->diag_nlsk = netlink_kernel_create(net, NETLINK_SOCK_DIAG, &cfg); |
51d7cccf AV |
303 | return net->diag_nlsk == NULL ? -ENOMEM : 0; |
304 | } | |
305 | ||
306 | static void __net_exit diag_net_exit(struct net *net) | |
307 | { | |
308 | netlink_kernel_release(net->diag_nlsk); | |
309 | net->diag_nlsk = NULL; | |
310 | } | |
311 | ||
312 | static struct pernet_operations diag_net_ops = { | |
313 | .init = diag_net_init, | |
314 | .exit = diag_net_exit, | |
315 | }; | |
316 | ||
317 | static int __init sock_diag_init(void) | |
318 | { | |
eb4cb008 CG |
319 | broadcast_wq = alloc_workqueue("sock_diag_events", 0, 0); |
320 | BUG_ON(!broadcast_wq); | |
51d7cccf | 321 | return register_pernet_subsys(&diag_net_ops); |
8ef874bf PE |
322 | } |
323 | ||
324 | static void __exit sock_diag_exit(void) | |
325 | { | |
51d7cccf | 326 | unregister_pernet_subsys(&diag_net_ops); |
eb4cb008 | 327 | destroy_workqueue(broadcast_wq); |
8ef874bf PE |
328 | } |
329 | ||
330 | module_init(sock_diag_init); | |
331 | module_exit(sock_diag_exit); | |
332 | MODULE_LICENSE("GPL"); | |
333 | MODULE_ALIAS_NET_PF_PROTO(PF_NETLINK, NETLINK_SOCK_DIAG); |