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