File Index
Files and directories
Sign in
FILE

raw_diag.c

File preview with syntax highlighting.

linux-6.19.14/net/ipv4/raw_diag.c C
  1// SPDX-License-Identifier: GPL-2.0-only
  2#include <linux/module.h>
  3
  4#include <linux/inet_diag.h>
  5#include <linux/sock_diag.h>
  6
  7#include <net/inet_sock.h>
  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 {
 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
 37static bool raw_lookup(struct net *net, const struct sock *sk,
 38		       const struct inet_diag_req_v2 *req)
 39{
 40	struct inet_diag_req_raw *r = (void *)req;
 41
 42	if (r->sdiag_family == AF_INET)
 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);
 47#if IS_ENABLED(CONFIG_IPV6)
 48	else
 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);
 53#endif
 54	return false;
 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);
 60	struct hlist_head *hlist;
 61	struct sock *sk;
 62	int slot;
 63
 64	if (IS_ERR(hashinfo))
 65		return ERR_CAST(hashinfo);
 66
 67	rcu_read_lock();
 68	for (slot = 0; slot < RAW_HTABLE_SIZE; slot++) {
 69		hlist = &hashinfo->ht[slot];
 70		sk_for_each_rcu(sk, hlist) {
 71			if (raw_lookup(net, sk, r)) {
 72				/*
 73				 * Grab it and keep until we fill
 74				 * diag message to be reported, so
 75				 * caller should call sock_put then.
 76				 */
 77				if (refcount_inc_not_zero(&sk->sk_refcnt))
 78					goto out_unlock;
 79			}
 80		}
 81	}
 82	sk = ERR_PTR(-ENOENT);
 83out_unlock:
 84	rcu_read_unlock();
 85
 86	return sk;
 87}
 88
 89static int raw_diag_dump_one(struct netlink_callback *cb,
 90			     const struct inet_diag_req_v2 *r)
 91{
 92	struct sk_buff *in_skb = cb->skb;
 93	struct sk_buff *rep;
 94	struct sock *sk;
 95	struct net *net;
 96	int err;
 97
 98	net = sock_net(in_skb->sk);
 99	sk = raw_sock_get(net, r);
100	if (IS_ERR(sk))
101		return PTR_ERR(sk);
102
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,
106			GFP_KERNEL);
107	if (!rep) {
108		sock_put(sk);
109		return -ENOMEM;
110	}
111
112	err = inet_sk_diag_fill(sk, NULL, rep, cb, r, 0,
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
121	err = nlmsg_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid);
122
123	return err;
124}
125
126static 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			bool net_admin)
130{
131	if (!inet_diag_bc_sk(cb->data, sk))
132		return 0;
133
134	return inet_sk_diag_fill(sk, NULL, skb, cb, r, NLM_F_MULTI, net_admin);
135}
136
137static void raw_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
138			  const struct inet_diag_req_v2 *r)
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);
143	int num, s_num, slot, s_slot;
144	struct hlist_head *hlist;
145	struct sock *sk = NULL;
146
147	if (IS_ERR(hashinfo))
148		return;
149
150	s_slot = cb->args[0];
151	num = s_num = cb->args[1];
152
153	rcu_read_lock();
154	for (slot = s_slot; slot < RAW_HTABLE_SIZE; s_num = 0, slot++) {
155		num = 0;
156
157		hlist = &hashinfo->ht[slot];
158		sk_for_each_rcu(sk, hlist) {
159			struct inet_sock *inet = inet_sk(sk);
160
161			if (!net_eq(sock_net(sk), net))
162				continue;
163			if (num < s_num)
164				goto next;
165			if (sk->sk_family != r->sdiag_family)
166				goto next;
167			if (r->id.idiag_sport != inet->inet_sport &&
168			    r->id.idiag_sport)
169				goto next;
170			if (r->id.idiag_dport != inet->inet_dport &&
171			    r->id.idiag_dport)
172				goto next;
173			if (sk_diag_dump(sk, skb, cb, r, net_admin) < 0)
174				goto out_unlock;
175next:
176			num++;
177		}
178	}
179
180out_unlock:
181	rcu_read_unlock();
182
183	cb->args[0] = slot;
184	cb->args[1] = num;
185}
186
187static void raw_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
188			      void *info)
189{
190	r->idiag_rqueue = sk_rmem_alloc_get(sk);
191	r->idiag_wqueue = sk_wmem_alloc_get(sk);
192}
193
194#ifdef CONFIG_INET_DIAG_DESTROY
195static int raw_diag_destroy(struct sk_buff *in_skb,
196			    const struct inet_diag_req_v2 *r)
197{
198	struct net *net = sock_net(in_skb->sk);
199	struct sock *sk;
200	int err;
201
202	sk = raw_sock_get(net, r);
203	if (IS_ERR(sk))
204		return PTR_ERR(sk);
205	err = sock_diag_destroy(sk, ECONNABORTED);
206	sock_put(sk);
207	return err;
208}
209#endif
210
211static const struct inet_diag_handler raw_diag_handler = {
212	.owner			= THIS_MODULE,
213	.dump			= raw_diag_dump,
214	.dump_one		= raw_diag_dump_one,
215	.idiag_get_info		= raw_diag_get_info,
216	.idiag_type		= IPPROTO_RAW,
217	.idiag_info_size	= 0,
218#ifdef CONFIG_INET_DIAG_DESTROY
219	.destroy		= raw_diag_destroy,
220#endif
221};
222
223static void __always_unused __check_inet_diag_req_raw(void)
224{
225	/*
226	 * Make sure the two structures are identical,
227	 * except the @pad field.
228	 */
229#define __offset_mismatch(m1, m2)			\
230	(offsetof(struct inet_diag_req_v2, m1) !=	\
231	 offsetof(struct inet_diag_req_raw, m2))
232
233	BUILD_BUG_ON(sizeof(struct inet_diag_req_v2) !=
234		     sizeof(struct inet_diag_req_raw));
235	BUILD_BUG_ON(__offset_mismatch(sdiag_family, sdiag_family));
236	BUILD_BUG_ON(__offset_mismatch(sdiag_protocol, sdiag_protocol));
237	BUILD_BUG_ON(__offset_mismatch(idiag_ext, idiag_ext));
238	BUILD_BUG_ON(__offset_mismatch(pad, sdiag_raw_protocol));
239	BUILD_BUG_ON(__offset_mismatch(idiag_states, idiag_states));
240	BUILD_BUG_ON(__offset_mismatch(id, id));
241#undef __offset_mismatch
242}
243
244static int __init raw_diag_init(void)
245{
246	return inet_diag_register(&raw_diag_handler);
247}
248
249static void __exit raw_diag_exit(void)
250{
251	inet_diag_unregister(&raw_diag_handler);
252}
253
254module_init(raw_diag_init);
255module_exit(raw_diag_exit);
256MODULE_LICENSE("GPL");
257MODULE_DESCRIPTION("RAW socket monitoring via SOCK_DIAG");
258MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-255 /* AF_INET - IPPROTO_RAW */);
259MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10-255 /* AF_INET6 - IPPROTO_RAW */);
~/ Read-only file preview.