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