1 // SPDX-License-Identifier: GPL-2.0-only
2 #include <linux/module.h>
4 #include <linux/inet_diag.h>
5 #include <linux/sock_diag.h>
7 #include <net/inet_sock.h>
15 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
17 static struct raw_hashinfo *
18 raw_get_hashinfo(const struct inet_diag_req_v2 *r)
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;
27 pr_warn_once("Unexpected inet family %d\n",
30 return ERR_PTR(-EINVAL);
35 * Due to requirement of not breaking user API we can't simply
36 * rename @pad field in inet_diag_req_v2 structure, instead
37 * use helper to figure it out.
40 static struct sock *raw_lookup(struct net *net, struct sock *from,
41 const struct inet_diag_req_v2 *req)
43 struct inet_diag_req_raw *r = (void *)req;
44 struct sock *sk = NULL;
46 if (r->sdiag_family == AF_INET)
47 sk = __raw_v4_lookup(net, from, r->sdiag_raw_protocol,
51 #if IS_ENABLED(CONFIG_IPV6)
53 sk = __raw_v6_lookup(net, from, r->sdiag_raw_protocol,
54 (const struct in6_addr *)r->id.idiag_src,
55 (const struct in6_addr *)r->id.idiag_dst,
61 static struct sock *raw_sock_get(struct net *net, const struct inet_diag_req_v2 *r)
63 struct raw_hashinfo *hashinfo = raw_get_hashinfo(r);
64 struct sock *sk = NULL, *s;
68 return ERR_CAST(hashinfo);
70 read_lock(&hashinfo->lock);
71 for (slot = 0; slot < RAW_HTABLE_SIZE; slot++) {
72 sk_for_each(s, &hashinfo->ht[slot]) {
73 sk = raw_lookup(net, s, r);
76 * Grab it and keep until we fill
77 * diag meaage to be reported, so
78 * caller should call sock_put then.
79 * We can do that because we're keeping
80 * hashinfo->lock here.
88 read_unlock(&hashinfo->lock);
90 return sk ? sk : ERR_PTR(-ENOENT);
93 static int raw_diag_dump_one(struct sk_buff *in_skb,
94 const struct nlmsghdr *nlh,
95 const struct inet_diag_req_v2 *r)
97 struct net *net = sock_net(in_skb->sk);
102 sk = raw_sock_get(net, r);
106 rep = nlmsg_new(sizeof(struct inet_diag_msg) +
107 sizeof(struct inet_diag_meminfo) + 64,
114 err = inet_sk_diag_fill(sk, NULL, rep, r,
115 sk_user_ns(NETLINK_CB(in_skb).sk),
116 NETLINK_CB(in_skb).portid,
117 nlh->nlmsg_seq, 0, nlh,
118 netlink_net_capable(in_skb, CAP_NET_ADMIN));
126 err = netlink_unicast(net->diag_nlsk, rep,
127 NETLINK_CB(in_skb).portid,
134 static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
135 struct netlink_callback *cb,
136 const struct inet_diag_req_v2 *r,
137 struct nlattr *bc, bool net_admin)
139 if (!inet_diag_bc_sk(bc, sk))
142 return inet_sk_diag_fill(sk, NULL, skb, r,
143 sk_user_ns(NETLINK_CB(cb->skb).sk),
144 NETLINK_CB(cb->skb).portid,
145 cb->nlh->nlmsg_seq, NLM_F_MULTI,
149 static void raw_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
150 const struct inet_diag_req_v2 *r, struct nlattr *bc)
152 bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
153 struct raw_hashinfo *hashinfo = raw_get_hashinfo(r);
154 struct net *net = sock_net(skb->sk);
155 int num, s_num, slot, s_slot;
156 struct sock *sk = NULL;
158 if (IS_ERR(hashinfo))
161 s_slot = cb->args[0];
162 num = s_num = cb->args[1];
164 read_lock(&hashinfo->lock);
165 for (slot = s_slot; slot < RAW_HTABLE_SIZE; s_num = 0, slot++) {
168 sk_for_each(sk, &hashinfo->ht[slot]) {
169 struct inet_sock *inet = inet_sk(sk);
171 if (!net_eq(sock_net(sk), net))
175 if (sk->sk_family != r->sdiag_family)
177 if (r->id.idiag_sport != inet->inet_sport &&
180 if (r->id.idiag_dport != inet->inet_dport &&
183 if (sk_diag_dump(sk, skb, cb, r, bc, net_admin) < 0)
191 read_unlock(&hashinfo->lock);
197 static void raw_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
200 r->idiag_rqueue = sk_rmem_alloc_get(sk);
201 r->idiag_wqueue = sk_wmem_alloc_get(sk);
204 #ifdef CONFIG_INET_DIAG_DESTROY
205 static int raw_diag_destroy(struct sk_buff *in_skb,
206 const struct inet_diag_req_v2 *r)
208 struct net *net = sock_net(in_skb->sk);
212 sk = raw_sock_get(net, r);
215 err = sock_diag_destroy(sk, ECONNABORTED);
221 static const struct inet_diag_handler raw_diag_handler = {
222 .dump = raw_diag_dump,
223 .dump_one = raw_diag_dump_one,
224 .idiag_get_info = raw_diag_get_info,
225 .idiag_type = IPPROTO_RAW,
226 .idiag_info_size = 0,
227 #ifdef CONFIG_INET_DIAG_DESTROY
228 .destroy = raw_diag_destroy,
232 static void __always_unused __check_inet_diag_req_raw(void)
235 * Make sure the two structures are identical,
236 * except the @pad field.
238 #define __offset_mismatch(m1, m2) \
239 (offsetof(struct inet_diag_req_v2, m1) != \
240 offsetof(struct inet_diag_req_raw, m2))
242 BUILD_BUG_ON(sizeof(struct inet_diag_req_v2) !=
243 sizeof(struct inet_diag_req_raw));
244 BUILD_BUG_ON(__offset_mismatch(sdiag_family, sdiag_family));
245 BUILD_BUG_ON(__offset_mismatch(sdiag_protocol, sdiag_protocol));
246 BUILD_BUG_ON(__offset_mismatch(idiag_ext, idiag_ext));
247 BUILD_BUG_ON(__offset_mismatch(pad, sdiag_raw_protocol));
248 BUILD_BUG_ON(__offset_mismatch(idiag_states, idiag_states));
249 BUILD_BUG_ON(__offset_mismatch(id, id));
250 #undef __offset_mismatch
253 static int __init raw_diag_init(void)
255 return inet_diag_register(&raw_diag_handler);
258 static void __exit raw_diag_exit(void)
260 inet_diag_unregister(&raw_diag_handler);
263 module_init(raw_diag_init);
264 module_exit(raw_diag_exit);
265 MODULE_LICENSE("GPL");
266 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-255 /* AF_INET - IPPROTO_RAW */);
267 MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10-255 /* AF_INET6 - IPPROTO_RAW */);