]>
Commit | Line | Data |
---|---|---|
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 | ||
17 | static struct raw_hashinfo * | |
18 | raw_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 |
37 | static 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 | ||
57 | static 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); | |
0a78cf72 | 60 | struct hlist_head *hlist; |
ba44f818 | 61 | struct sock *sk; |
432490f9 CG |
62 | int slot; |
63 | ||
64 | if (IS_ERR(hashinfo)) | |
65 | return ERR_CAST(hashinfo); | |
66 | ||
0daf07e5 | 67 | rcu_read_lock(); |
432490f9 | 68 | for (slot = 0; slot < RAW_HTABLE_SIZE; slot++) { |
0daf07e5 | 69 | hlist = &hashinfo->ht[slot]; |
0a78cf72 | 70 | sk_for_each_rcu(sk, hlist) { |
ba44f818 | 71 | if (raw_lookup(net, sk, r)) { |
432490f9 CG |
72 | /* |
73 | * Grab it and keep until we fill | |
0daf07e5 | 74 | * diag message to be reported, so |
432490f9 | 75 | * caller should call sock_put then. |
432490f9 | 76 | */ |
0daf07e5 ED |
77 | if (refcount_inc_not_zero(&sk->sk_refcnt)) |
78 | goto out_unlock; | |
432490f9 CG |
79 | } |
80 | } | |
81 | } | |
ba44f818 | 82 | sk = ERR_PTR(-ENOENT); |
9999370f | 83 | out_unlock: |
0daf07e5 | 84 | rcu_read_unlock(); |
432490f9 | 85 | |
ba44f818 | 86 | return sk; |
432490f9 CG |
87 | } |
88 | ||
5682d393 | 89 | static int raw_diag_dump_one(struct netlink_callback *cb, |
432490f9 CG |
90 | const struct inet_diag_req_v2 *r) |
91 | { | |
5682d393 | 92 | struct sk_buff *in_skb = cb->skb; |
432490f9 CG |
93 | struct sk_buff *rep; |
94 | struct sock *sk; | |
5682d393 | 95 | struct net *net; |
432490f9 CG |
96 | int err; |
97 | ||
5682d393 | 98 | net = sock_net(in_skb->sk); |
432490f9 CG |
99 | sk = raw_sock_get(net, r); |
100 | if (IS_ERR(sk)) | |
101 | return PTR_ERR(sk); | |
102 | ||
83f73c5b DY |
103 | rep = nlmsg_new(nla_total_size(sizeof(struct inet_diag_msg)) + |
104 | inet_diag_msg_attrs_size() + | |
105 | nla_total_size(sizeof(struct inet_diag_meminfo)) + 64, | |
432490f9 CG |
106 | GFP_KERNEL); |
107 | if (!rep) { | |
108 | sock_put(sk); | |
109 | return -ENOMEM; | |
110 | } | |
111 | ||
5682d393 | 112 | err = inet_sk_diag_fill(sk, NULL, rep, cb, r, 0, |
432490f9 CG |
113 | netlink_net_capable(in_skb, CAP_NET_ADMIN)); |
114 | sock_put(sk); | |
115 | ||
116 | if (err < 0) { | |
117 | kfree_skb(rep); | |
118 | return err; | |
119 | } | |
120 | ||
01757f53 YD |
121 | err = nlmsg_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid); |
122 | ||
432490f9 CG |
123 | return err; |
124 | } | |
125 | ||
126 | static int sk_diag_dump(struct sock *sk, struct sk_buff *skb, | |
127 | struct netlink_callback *cb, | |
128 | const struct inet_diag_req_v2 *r, | |
129 | struct nlattr *bc, bool net_admin) | |
130 | { | |
131 | if (!inet_diag_bc_sk(bc, sk)) | |
132 | return 0; | |
133 | ||
5682d393 | 134 | return inet_sk_diag_fill(sk, NULL, skb, cb, r, NLM_F_MULTI, net_admin); |
432490f9 CG |
135 | } |
136 | ||
137 | static void raw_diag_dump(struct sk_buff *skb, struct netlink_callback *cb, | |
0df6d328 | 138 | const struct inet_diag_req_v2 *r) |
432490f9 CG |
139 | { |
140 | bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN); | |
141 | struct raw_hashinfo *hashinfo = raw_get_hashinfo(r); | |
142 | struct net *net = sock_net(skb->sk); | |
0df6d328 | 143 | struct inet_diag_dump_data *cb_data; |
432490f9 | 144 | int num, s_num, slot, s_slot; |
0a78cf72 | 145 | struct hlist_head *hlist; |
432490f9 | 146 | struct sock *sk = NULL; |
0df6d328 | 147 | struct nlattr *bc; |
432490f9 CG |
148 | |
149 | if (IS_ERR(hashinfo)) | |
150 | return; | |
151 | ||
0df6d328 MKL |
152 | cb_data = cb->data; |
153 | bc = cb_data->inet_diag_nla_bc; | |
432490f9 CG |
154 | s_slot = cb->args[0]; |
155 | num = s_num = cb->args[1]; | |
156 | ||
af185d8c | 157 | rcu_read_lock(); |
432490f9 CG |
158 | for (slot = s_slot; slot < RAW_HTABLE_SIZE; s_num = 0, slot++) { |
159 | num = 0; | |
160 | ||
0daf07e5 | 161 | hlist = &hashinfo->ht[slot]; |
0a78cf72 | 162 | sk_for_each_rcu(sk, hlist) { |
432490f9 CG |
163 | struct inet_sock *inet = inet_sk(sk); |
164 | ||
165 | if (!net_eq(sock_net(sk), net)) | |
166 | continue; | |
167 | if (num < s_num) | |
168 | goto next; | |
169 | if (sk->sk_family != r->sdiag_family) | |
170 | goto next; | |
171 | if (r->id.idiag_sport != inet->inet_sport && | |
172 | r->id.idiag_sport) | |
173 | goto next; | |
174 | if (r->id.idiag_dport != inet->inet_dport && | |
175 | r->id.idiag_dport) | |
176 | goto next; | |
177 | if (sk_diag_dump(sk, skb, cb, r, bc, net_admin) < 0) | |
178 | goto out_unlock; | |
179 | next: | |
180 | num++; | |
181 | } | |
182 | } | |
183 | ||
184 | out_unlock: | |
af185d8c | 185 | rcu_read_unlock(); |
432490f9 CG |
186 | |
187 | cb->args[0] = slot; | |
188 | cb->args[1] = num; | |
189 | } | |
190 | ||
191 | static void raw_diag_get_info(struct sock *sk, struct inet_diag_msg *r, | |
192 | void *info) | |
193 | { | |
194 | r->idiag_rqueue = sk_rmem_alloc_get(sk); | |
195 | r->idiag_wqueue = sk_wmem_alloc_get(sk); | |
196 | } | |
197 | ||
198 | #ifdef CONFIG_INET_DIAG_DESTROY | |
199 | static int raw_diag_destroy(struct sk_buff *in_skb, | |
200 | const struct inet_diag_req_v2 *r) | |
201 | { | |
202 | struct net *net = sock_net(in_skb->sk); | |
203 | struct sock *sk; | |
cd05a0ec | 204 | int err; |
432490f9 CG |
205 | |
206 | sk = raw_sock_get(net, r); | |
207 | if (IS_ERR(sk)) | |
208 | return PTR_ERR(sk); | |
cd05a0ec CG |
209 | err = sock_diag_destroy(sk, ECONNABORTED); |
210 | sock_put(sk); | |
211 | return err; | |
432490f9 CG |
212 | } |
213 | #endif | |
214 | ||
215 | static 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 | ||
226 | static 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 | ||
247 | static int __init raw_diag_init(void) | |
248 | { | |
249 | return inet_diag_register(&raw_diag_handler); | |
250 | } | |
251 | ||
252 | static void __exit raw_diag_exit(void) | |
253 | { | |
254 | inet_diag_unregister(&raw_diag_handler); | |
255 | } | |
256 | ||
257 | module_init(raw_diag_init); | |
258 | module_exit(raw_diag_exit); | |
259 | MODULE_LICENSE("GPL"); | |
260 | MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-255 /* AF_INET - IPPROTO_RAW */); | |
261 | MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10-255 /* AF_INET6 - IPPROTO_RAW */); |