blob: ef3bea061b758fe9a8edb6364da2580ef99f3d5e [file] [log] [blame]
Cyrill Gorcunov432490f2016-10-21 13:03:44 +03001#include <linux/module.h>
2
3#include <linux/inet_diag.h>
4#include <linux/sock_diag.h>
5
6#include <net/raw.h>
7#include <net/rawv6.h>
8
9#ifdef pr_fmt
10# undef pr_fmt
11#endif
12
13#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
14
15static struct raw_hashinfo *
16raw_get_hashinfo(const struct inet_diag_req_v2 *r)
17{
18 if (r->sdiag_family == AF_INET) {
19 return &raw_v4_hashinfo;
20#if IS_ENABLED(CONFIG_IPV6)
21 } else if (r->sdiag_family == AF_INET6) {
22 return &raw_v6_hashinfo;
23#endif
24 } else {
25 pr_warn_once("Unexpected inet family %d\n",
26 r->sdiag_family);
27 WARN_ON_ONCE(1);
28 return ERR_PTR(-EINVAL);
29 }
30}
31
32/*
33 * Due to requirement of not breaking user API we can't simply
34 * rename @pad field in inet_diag_req_v2 structure, instead
35 * use helper to figure it out.
36 */
37
38static struct sock *raw_lookup(struct net *net, struct sock *from,
39 const struct inet_diag_req_v2 *req)
40{
41 struct inet_diag_req_raw *r = (void *)req;
42 struct sock *sk = NULL;
43
44 if (r->sdiag_family == AF_INET)
45 sk = __raw_v4_lookup(net, from, r->sdiag_raw_protocol,
46 r->id.idiag_dst[0],
47 r->id.idiag_src[0],
48 r->id.idiag_if);
49#if IS_ENABLED(CONFIG_IPV6)
50 else
51 sk = __raw_v6_lookup(net, from, r->sdiag_raw_protocol,
52 (const struct in6_addr *)r->id.idiag_src,
53 (const struct in6_addr *)r->id.idiag_dst,
54 r->id.idiag_if);
55#endif
56 return sk;
57}
58
59static struct sock *raw_sock_get(struct net *net, const struct inet_diag_req_v2 *r)
60{
61 struct raw_hashinfo *hashinfo = raw_get_hashinfo(r);
62 struct sock *sk = NULL, *s;
63 int slot;
64
65 if (IS_ERR(hashinfo))
66 return ERR_CAST(hashinfo);
67
68 read_lock(&hashinfo->lock);
69 for (slot = 0; slot < RAW_HTABLE_SIZE; slot++) {
70 sk_for_each(s, &hashinfo->ht[slot]) {
71 sk = raw_lookup(net, s, r);
72 if (sk) {
73 /*
74 * Grab it and keep until we fill
75 * diag meaage to be reported, so
76 * caller should call sock_put then.
77 * We can do that because we're keeping
78 * hashinfo->lock here.
79 */
80 sock_hold(sk);
81 break;
82 }
83 }
84 }
85 read_unlock(&hashinfo->lock);
86
87 return sk ? sk : ERR_PTR(-ENOENT);
88}
89
90static int raw_diag_dump_one(struct sk_buff *in_skb,
91 const struct nlmsghdr *nlh,
92 const struct inet_diag_req_v2 *r)
93{
94 struct net *net = sock_net(in_skb->sk);
95 struct sk_buff *rep;
96 struct sock *sk;
97 int err;
98
99 sk = raw_sock_get(net, r);
100 if (IS_ERR(sk))
101 return PTR_ERR(sk);
102
103 rep = nlmsg_new(sizeof(struct inet_diag_msg) +
104 sizeof(struct inet_diag_meminfo) + 64,
105 GFP_KERNEL);
106 if (!rep) {
107 sock_put(sk);
108 return -ENOMEM;
109 }
110
111 err = inet_sk_diag_fill(sk, NULL, rep, r,
112 sk_user_ns(NETLINK_CB(in_skb).sk),
113 NETLINK_CB(in_skb).portid,
114 nlh->nlmsg_seq, 0, nlh,
115 netlink_net_capable(in_skb, CAP_NET_ADMIN));
116 sock_put(sk);
117
118 if (err < 0) {
119 kfree_skb(rep);
120 return err;
121 }
122
123 err = netlink_unicast(net->diag_nlsk, rep,
124 NETLINK_CB(in_skb).portid,
125 MSG_DONTWAIT);
126 if (err > 0)
127 err = 0;
128 return err;
129}
130
131static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
132 struct netlink_callback *cb,
133 const struct inet_diag_req_v2 *r,
134 struct nlattr *bc, bool net_admin)
135{
136 if (!inet_diag_bc_sk(bc, sk))
137 return 0;
138
139 return inet_sk_diag_fill(sk, NULL, skb, r,
140 sk_user_ns(NETLINK_CB(cb->skb).sk),
141 NETLINK_CB(cb->skb).portid,
142 cb->nlh->nlmsg_seq, NLM_F_MULTI,
143 cb->nlh, net_admin);
144}
145
146static void raw_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
147 const struct inet_diag_req_v2 *r, struct nlattr *bc)
148{
149 bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
150 struct raw_hashinfo *hashinfo = raw_get_hashinfo(r);
151 struct net *net = sock_net(skb->sk);
152 int num, s_num, slot, s_slot;
153 struct sock *sk = NULL;
154
155 if (IS_ERR(hashinfo))
156 return;
157
158 s_slot = cb->args[0];
159 num = s_num = cb->args[1];
160
161 read_lock(&hashinfo->lock);
162 for (slot = s_slot; slot < RAW_HTABLE_SIZE; s_num = 0, slot++) {
163 num = 0;
164
165 sk_for_each(sk, &hashinfo->ht[slot]) {
166 struct inet_sock *inet = inet_sk(sk);
167
168 if (!net_eq(sock_net(sk), net))
169 continue;
170 if (num < s_num)
171 goto next;
172 if (sk->sk_family != r->sdiag_family)
173 goto next;
174 if (r->id.idiag_sport != inet->inet_sport &&
175 r->id.idiag_sport)
176 goto next;
177 if (r->id.idiag_dport != inet->inet_dport &&
178 r->id.idiag_dport)
179 goto next;
180 if (sk_diag_dump(sk, skb, cb, r, bc, net_admin) < 0)
181 goto out_unlock;
182next:
183 num++;
184 }
185 }
186
187out_unlock:
188 read_unlock(&hashinfo->lock);
189
190 cb->args[0] = slot;
191 cb->args[1] = num;
192}
193
194static void raw_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
195 void *info)
196{
197 r->idiag_rqueue = sk_rmem_alloc_get(sk);
198 r->idiag_wqueue = sk_wmem_alloc_get(sk);
199}
200
201#ifdef CONFIG_INET_DIAG_DESTROY
202static int raw_diag_destroy(struct sk_buff *in_skb,
203 const struct inet_diag_req_v2 *r)
204{
205 struct net *net = sock_net(in_skb->sk);
206 struct sock *sk;
207
208 sk = raw_sock_get(net, r);
209 if (IS_ERR(sk))
210 return PTR_ERR(sk);
211 return sock_diag_destroy(sk, ECONNABORTED);
212}
213#endif
214
215static const struct inet_diag_handler raw_diag_handler = {
216 .dump = raw_diag_dump,
217 .dump_one = raw_diag_dump_one,
218 .idiag_get_info = raw_diag_get_info,
219 .idiag_type = IPPROTO_RAW,
220 .idiag_info_size = 0,
221#ifdef CONFIG_INET_DIAG_DESTROY
222 .destroy = raw_diag_destroy,
223#endif
224};
225
226static void __always_unused __check_inet_diag_req_raw(void)
227{
228 /*
229 * Make sure the two structures are identical,
230 * except the @pad field.
231 */
232#define __offset_mismatch(m1, m2) \
233 (offsetof(struct inet_diag_req_v2, m1) != \
234 offsetof(struct inet_diag_req_raw, m2))
235
236 BUILD_BUG_ON(sizeof(struct inet_diag_req_v2) !=
237 sizeof(struct inet_diag_req_raw));
238 BUILD_BUG_ON(__offset_mismatch(sdiag_family, sdiag_family));
239 BUILD_BUG_ON(__offset_mismatch(sdiag_protocol, sdiag_protocol));
240 BUILD_BUG_ON(__offset_mismatch(idiag_ext, idiag_ext));
241 BUILD_BUG_ON(__offset_mismatch(pad, sdiag_raw_protocol));
242 BUILD_BUG_ON(__offset_mismatch(idiag_states, idiag_states));
243 BUILD_BUG_ON(__offset_mismatch(id, id));
244#undef __offset_mismatch
245}
246
247static int __init raw_diag_init(void)
248{
249 return inet_diag_register(&raw_diag_handler);
250}
251
252static void __exit raw_diag_exit(void)
253{
254 inet_diag_unregister(&raw_diag_handler);
255}
256
257module_init(raw_diag_init);
258module_exit(raw_diag_exit);
259MODULE_LICENSE("GPL");
260MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-255 /* AF_INET - IPPROTO_RAW */);
261MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10-255 /* AF_INET6 - IPPROTO_RAW */);