xref: /linux/net/ipv4/raw_diag.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2432490f9SCyrill Gorcunov #include <linux/module.h>
3432490f9SCyrill Gorcunov 
4432490f9SCyrill Gorcunov #include <linux/inet_diag.h>
5432490f9SCyrill Gorcunov #include <linux/sock_diag.h>
6432490f9SCyrill Gorcunov 
7f8da9779SArnd Bergmann #include <net/inet_sock.h>
8432490f9SCyrill Gorcunov #include <net/raw.h>
9432490f9SCyrill Gorcunov #include <net/rawv6.h>
10432490f9SCyrill Gorcunov 
11432490f9SCyrill Gorcunov #ifdef pr_fmt
12432490f9SCyrill Gorcunov # undef pr_fmt
13432490f9SCyrill Gorcunov #endif
14432490f9SCyrill Gorcunov 
15432490f9SCyrill Gorcunov #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16432490f9SCyrill Gorcunov 
17432490f9SCyrill Gorcunov static struct raw_hashinfo *
raw_get_hashinfo(const struct inet_diag_req_v2 * r)18432490f9SCyrill Gorcunov raw_get_hashinfo(const struct inet_diag_req_v2 *r)
19432490f9SCyrill Gorcunov {
20432490f9SCyrill Gorcunov 	if (r->sdiag_family == AF_INET) {
21432490f9SCyrill Gorcunov 		return &raw_v4_hashinfo;
22432490f9SCyrill Gorcunov #if IS_ENABLED(CONFIG_IPV6)
23432490f9SCyrill Gorcunov 	} else if (r->sdiag_family == AF_INET6) {
24432490f9SCyrill Gorcunov 		return &raw_v6_hashinfo;
25432490f9SCyrill Gorcunov #endif
26432490f9SCyrill Gorcunov 	} else {
27432490f9SCyrill Gorcunov 		return ERR_PTR(-EINVAL);
28432490f9SCyrill Gorcunov 	}
29432490f9SCyrill Gorcunov }
30432490f9SCyrill Gorcunov 
31432490f9SCyrill Gorcunov /*
32432490f9SCyrill Gorcunov  * Due to requirement of not breaking user API we can't simply
33432490f9SCyrill Gorcunov  * rename @pad field in inet_diag_req_v2 structure, instead
34432490f9SCyrill Gorcunov  * use helper to figure it out.
35432490f9SCyrill Gorcunov  */
36432490f9SCyrill Gorcunov 
raw_lookup(struct net * net,const struct sock * sk,const struct inet_diag_req_v2 * req)37736c8b52SEric Dumazet static bool raw_lookup(struct net *net, const struct sock *sk,
38432490f9SCyrill Gorcunov 		       const struct inet_diag_req_v2 *req)
39432490f9SCyrill Gorcunov {
40432490f9SCyrill Gorcunov 	struct inet_diag_req_raw *r = (void *)req;
41432490f9SCyrill Gorcunov 
42432490f9SCyrill Gorcunov 	if (r->sdiag_family == AF_INET)
43ba44f818SEric Dumazet 		return raw_v4_match(net, sk, r->sdiag_raw_protocol,
44432490f9SCyrill Gorcunov 				    r->id.idiag_dst[0],
45432490f9SCyrill Gorcunov 				    r->id.idiag_src[0],
4667359930SDavid Ahern 				    r->id.idiag_if, 0);
47432490f9SCyrill Gorcunov #if IS_ENABLED(CONFIG_IPV6)
48432490f9SCyrill Gorcunov 	else
49ba44f818SEric Dumazet 		return raw_v6_match(net, sk, r->sdiag_raw_protocol,
50432490f9SCyrill Gorcunov 				    (const struct in6_addr *)r->id.idiag_src,
51432490f9SCyrill Gorcunov 				    (const struct in6_addr *)r->id.idiag_dst,
525108ab4bSDavid Ahern 				    r->id.idiag_if, 0);
53432490f9SCyrill Gorcunov #endif
54ba44f818SEric Dumazet 	return false;
55432490f9SCyrill Gorcunov }
56432490f9SCyrill Gorcunov 
raw_sock_get(struct net * net,const struct inet_diag_req_v2 * r)57432490f9SCyrill Gorcunov static struct sock *raw_sock_get(struct net *net, const struct inet_diag_req_v2 *r)
58432490f9SCyrill Gorcunov {
59432490f9SCyrill Gorcunov 	struct raw_hashinfo *hashinfo = raw_get_hashinfo(r);
600a78cf72SKuniyuki Iwashima 	struct hlist_head *hlist;
61ba44f818SEric Dumazet 	struct sock *sk;
62432490f9SCyrill Gorcunov 	int slot;
63432490f9SCyrill Gorcunov 
64432490f9SCyrill Gorcunov 	if (IS_ERR(hashinfo))
65432490f9SCyrill Gorcunov 		return ERR_CAST(hashinfo);
66432490f9SCyrill Gorcunov 
670daf07e5SEric Dumazet 	rcu_read_lock();
68432490f9SCyrill Gorcunov 	for (slot = 0; slot < RAW_HTABLE_SIZE; slot++) {
690daf07e5SEric Dumazet 		hlist = &hashinfo->ht[slot];
700a78cf72SKuniyuki Iwashima 		sk_for_each_rcu(sk, hlist) {
71ba44f818SEric Dumazet 			if (raw_lookup(net, sk, r)) {
72432490f9SCyrill Gorcunov 				/*
73432490f9SCyrill Gorcunov 				 * Grab it and keep until we fill
740daf07e5SEric Dumazet 				 * diag message to be reported, so
75432490f9SCyrill Gorcunov 				 * caller should call sock_put then.
76432490f9SCyrill Gorcunov 				 */
770daf07e5SEric Dumazet 				if (refcount_inc_not_zero(&sk->sk_refcnt))
789999370fSCyrill Gorcunov 					goto out_unlock;
79432490f9SCyrill Gorcunov 			}
80432490f9SCyrill Gorcunov 		}
81432490f9SCyrill Gorcunov 	}
82ba44f818SEric Dumazet 	sk = ERR_PTR(-ENOENT);
839999370fSCyrill Gorcunov out_unlock:
840daf07e5SEric Dumazet 	rcu_read_unlock();
85432490f9SCyrill Gorcunov 
86ba44f818SEric Dumazet 	return sk;
87432490f9SCyrill Gorcunov }
88432490f9SCyrill Gorcunov 
raw_diag_dump_one(struct netlink_callback * cb,const struct inet_diag_req_v2 * r)895682d393SMartin KaFai Lau static int raw_diag_dump_one(struct netlink_callback *cb,
90432490f9SCyrill Gorcunov 			     const struct inet_diag_req_v2 *r)
91432490f9SCyrill Gorcunov {
925682d393SMartin KaFai Lau 	struct sk_buff *in_skb = cb->skb;
93432490f9SCyrill Gorcunov 	struct sk_buff *rep;
94432490f9SCyrill Gorcunov 	struct sock *sk;
955682d393SMartin KaFai Lau 	struct net *net;
96432490f9SCyrill Gorcunov 	int err;
97432490f9SCyrill Gorcunov 
985682d393SMartin KaFai Lau 	net = sock_net(in_skb->sk);
99432490f9SCyrill Gorcunov 	sk = raw_sock_get(net, r);
100432490f9SCyrill Gorcunov 	if (IS_ERR(sk))
101432490f9SCyrill Gorcunov 		return PTR_ERR(sk);
102432490f9SCyrill Gorcunov 
10383f73c5bSDmitry Yakunin 	rep = nlmsg_new(nla_total_size(sizeof(struct inet_diag_msg)) +
10483f73c5bSDmitry Yakunin 			inet_diag_msg_attrs_size() +
10583f73c5bSDmitry Yakunin 			nla_total_size(sizeof(struct inet_diag_meminfo)) + 64,
106432490f9SCyrill Gorcunov 			GFP_KERNEL);
107432490f9SCyrill Gorcunov 	if (!rep) {
108432490f9SCyrill Gorcunov 		sock_put(sk);
109432490f9SCyrill Gorcunov 		return -ENOMEM;
110432490f9SCyrill Gorcunov 	}
111432490f9SCyrill Gorcunov 
1125682d393SMartin KaFai Lau 	err = inet_sk_diag_fill(sk, NULL, rep, cb, r, 0,
113432490f9SCyrill Gorcunov 				netlink_net_capable(in_skb, CAP_NET_ADMIN));
114432490f9SCyrill Gorcunov 	sock_put(sk);
115432490f9SCyrill Gorcunov 
116432490f9SCyrill Gorcunov 	if (err < 0) {
117432490f9SCyrill Gorcunov 		kfree_skb(rep);
118432490f9SCyrill Gorcunov 		return err;
119432490f9SCyrill Gorcunov 	}
120432490f9SCyrill Gorcunov 
12101757f53SYajun Deng 	err = nlmsg_unicast(net->diag_nlsk, rep, NETLINK_CB(in_skb).portid);
12201757f53SYajun Deng 
123432490f9SCyrill Gorcunov 	return err;
124432490f9SCyrill Gorcunov }
125432490f9SCyrill Gorcunov 
sk_diag_dump(struct sock * sk,struct sk_buff * skb,struct netlink_callback * cb,const struct inet_diag_req_v2 * r,struct nlattr * bc,bool net_admin)126432490f9SCyrill Gorcunov static int sk_diag_dump(struct sock *sk, struct sk_buff *skb,
127432490f9SCyrill Gorcunov 			struct netlink_callback *cb,
128432490f9SCyrill Gorcunov 			const struct inet_diag_req_v2 *r,
129432490f9SCyrill Gorcunov 			struct nlattr *bc, bool net_admin)
130432490f9SCyrill Gorcunov {
131432490f9SCyrill Gorcunov 	if (!inet_diag_bc_sk(bc, sk))
132432490f9SCyrill Gorcunov 		return 0;
133432490f9SCyrill Gorcunov 
1345682d393SMartin KaFai Lau 	return inet_sk_diag_fill(sk, NULL, skb, cb, r, NLM_F_MULTI, net_admin);
135432490f9SCyrill Gorcunov }
136432490f9SCyrill Gorcunov 
raw_diag_dump(struct sk_buff * skb,struct netlink_callback * cb,const struct inet_diag_req_v2 * r)137432490f9SCyrill Gorcunov static void raw_diag_dump(struct sk_buff *skb, struct netlink_callback *cb,
1380df6d328SMartin KaFai Lau 			  const struct inet_diag_req_v2 *r)
139432490f9SCyrill Gorcunov {
140432490f9SCyrill Gorcunov 	bool net_admin = netlink_net_capable(cb->skb, CAP_NET_ADMIN);
141432490f9SCyrill Gorcunov 	struct raw_hashinfo *hashinfo = raw_get_hashinfo(r);
142432490f9SCyrill Gorcunov 	struct net *net = sock_net(skb->sk);
1430df6d328SMartin KaFai Lau 	struct inet_diag_dump_data *cb_data;
144432490f9SCyrill Gorcunov 	int num, s_num, slot, s_slot;
1450a78cf72SKuniyuki Iwashima 	struct hlist_head *hlist;
146432490f9SCyrill Gorcunov 	struct sock *sk = NULL;
1470df6d328SMartin KaFai Lau 	struct nlattr *bc;
148432490f9SCyrill Gorcunov 
149432490f9SCyrill Gorcunov 	if (IS_ERR(hashinfo))
150432490f9SCyrill Gorcunov 		return;
151432490f9SCyrill Gorcunov 
1520df6d328SMartin KaFai Lau 	cb_data = cb->data;
1530df6d328SMartin KaFai Lau 	bc = cb_data->inet_diag_nla_bc;
154432490f9SCyrill Gorcunov 	s_slot = cb->args[0];
155432490f9SCyrill Gorcunov 	num = s_num = cb->args[1];
156432490f9SCyrill Gorcunov 
157af185d8cSEric Dumazet 	rcu_read_lock();
158432490f9SCyrill Gorcunov 	for (slot = s_slot; slot < RAW_HTABLE_SIZE; s_num = 0, slot++) {
159432490f9SCyrill Gorcunov 		num = 0;
160432490f9SCyrill Gorcunov 
1610daf07e5SEric Dumazet 		hlist = &hashinfo->ht[slot];
1620a78cf72SKuniyuki Iwashima 		sk_for_each_rcu(sk, hlist) {
163432490f9SCyrill Gorcunov 			struct inet_sock *inet = inet_sk(sk);
164432490f9SCyrill Gorcunov 
165432490f9SCyrill Gorcunov 			if (!net_eq(sock_net(sk), net))
166432490f9SCyrill Gorcunov 				continue;
167432490f9SCyrill Gorcunov 			if (num < s_num)
168432490f9SCyrill Gorcunov 				goto next;
169432490f9SCyrill Gorcunov 			if (sk->sk_family != r->sdiag_family)
170432490f9SCyrill Gorcunov 				goto next;
171432490f9SCyrill Gorcunov 			if (r->id.idiag_sport != inet->inet_sport &&
172432490f9SCyrill Gorcunov 			    r->id.idiag_sport)
173432490f9SCyrill Gorcunov 				goto next;
174432490f9SCyrill Gorcunov 			if (r->id.idiag_dport != inet->inet_dport &&
175432490f9SCyrill Gorcunov 			    r->id.idiag_dport)
176432490f9SCyrill Gorcunov 				goto next;
177432490f9SCyrill Gorcunov 			if (sk_diag_dump(sk, skb, cb, r, bc, net_admin) < 0)
178432490f9SCyrill Gorcunov 				goto out_unlock;
179432490f9SCyrill Gorcunov next:
180432490f9SCyrill Gorcunov 			num++;
181432490f9SCyrill Gorcunov 		}
182432490f9SCyrill Gorcunov 	}
183432490f9SCyrill Gorcunov 
184432490f9SCyrill Gorcunov out_unlock:
185af185d8cSEric Dumazet 	rcu_read_unlock();
186432490f9SCyrill Gorcunov 
187432490f9SCyrill Gorcunov 	cb->args[0] = slot;
188432490f9SCyrill Gorcunov 	cb->args[1] = num;
189432490f9SCyrill Gorcunov }
190432490f9SCyrill Gorcunov 
raw_diag_get_info(struct sock * sk,struct inet_diag_msg * r,void * info)191432490f9SCyrill Gorcunov static void raw_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
192432490f9SCyrill Gorcunov 			      void *info)
193432490f9SCyrill Gorcunov {
194432490f9SCyrill Gorcunov 	r->idiag_rqueue = sk_rmem_alloc_get(sk);
195432490f9SCyrill Gorcunov 	r->idiag_wqueue = sk_wmem_alloc_get(sk);
196432490f9SCyrill Gorcunov }
197432490f9SCyrill Gorcunov 
198432490f9SCyrill Gorcunov #ifdef CONFIG_INET_DIAG_DESTROY
raw_diag_destroy(struct sk_buff * in_skb,const struct inet_diag_req_v2 * r)199432490f9SCyrill Gorcunov static int raw_diag_destroy(struct sk_buff *in_skb,
200432490f9SCyrill Gorcunov 			    const struct inet_diag_req_v2 *r)
201432490f9SCyrill Gorcunov {
202432490f9SCyrill Gorcunov 	struct net *net = sock_net(in_skb->sk);
203432490f9SCyrill Gorcunov 	struct sock *sk;
204cd05a0ecSCyrill Gorcunov 	int err;
205432490f9SCyrill Gorcunov 
206432490f9SCyrill Gorcunov 	sk = raw_sock_get(net, r);
207432490f9SCyrill Gorcunov 	if (IS_ERR(sk))
208432490f9SCyrill Gorcunov 		return PTR_ERR(sk);
209cd05a0ecSCyrill Gorcunov 	err = sock_diag_destroy(sk, ECONNABORTED);
210cd05a0ecSCyrill Gorcunov 	sock_put(sk);
211cd05a0ecSCyrill Gorcunov 	return err;
212432490f9SCyrill Gorcunov }
213432490f9SCyrill Gorcunov #endif
214432490f9SCyrill Gorcunov 
215432490f9SCyrill Gorcunov static const struct inet_diag_handler raw_diag_handler = {
216*db591469SEric Dumazet 	.owner			= THIS_MODULE,
217432490f9SCyrill Gorcunov 	.dump			= raw_diag_dump,
218432490f9SCyrill Gorcunov 	.dump_one		= raw_diag_dump_one,
219432490f9SCyrill Gorcunov 	.idiag_get_info		= raw_diag_get_info,
220432490f9SCyrill Gorcunov 	.idiag_type		= IPPROTO_RAW,
221432490f9SCyrill Gorcunov 	.idiag_info_size	= 0,
222432490f9SCyrill Gorcunov #ifdef CONFIG_INET_DIAG_DESTROY
223432490f9SCyrill Gorcunov 	.destroy		= raw_diag_destroy,
224432490f9SCyrill Gorcunov #endif
225432490f9SCyrill Gorcunov };
226432490f9SCyrill Gorcunov 
__check_inet_diag_req_raw(void)227432490f9SCyrill Gorcunov static void __always_unused __check_inet_diag_req_raw(void)
228432490f9SCyrill Gorcunov {
229432490f9SCyrill Gorcunov 	/*
230432490f9SCyrill Gorcunov 	 * Make sure the two structures are identical,
231432490f9SCyrill Gorcunov 	 * except the @pad field.
232432490f9SCyrill Gorcunov 	 */
233432490f9SCyrill Gorcunov #define __offset_mismatch(m1, m2)			\
234432490f9SCyrill Gorcunov 	(offsetof(struct inet_diag_req_v2, m1) !=	\
235432490f9SCyrill Gorcunov 	 offsetof(struct inet_diag_req_raw, m2))
236432490f9SCyrill Gorcunov 
237432490f9SCyrill Gorcunov 	BUILD_BUG_ON(sizeof(struct inet_diag_req_v2) !=
238432490f9SCyrill Gorcunov 		     sizeof(struct inet_diag_req_raw));
239432490f9SCyrill Gorcunov 	BUILD_BUG_ON(__offset_mismatch(sdiag_family, sdiag_family));
240432490f9SCyrill Gorcunov 	BUILD_BUG_ON(__offset_mismatch(sdiag_protocol, sdiag_protocol));
241432490f9SCyrill Gorcunov 	BUILD_BUG_ON(__offset_mismatch(idiag_ext, idiag_ext));
242432490f9SCyrill Gorcunov 	BUILD_BUG_ON(__offset_mismatch(pad, sdiag_raw_protocol));
243432490f9SCyrill Gorcunov 	BUILD_BUG_ON(__offset_mismatch(idiag_states, idiag_states));
244432490f9SCyrill Gorcunov 	BUILD_BUG_ON(__offset_mismatch(id, id));
245432490f9SCyrill Gorcunov #undef __offset_mismatch
246432490f9SCyrill Gorcunov }
247432490f9SCyrill Gorcunov 
raw_diag_init(void)248432490f9SCyrill Gorcunov static int __init raw_diag_init(void)
249432490f9SCyrill Gorcunov {
250432490f9SCyrill Gorcunov 	return inet_diag_register(&raw_diag_handler);
251432490f9SCyrill Gorcunov }
252432490f9SCyrill Gorcunov 
raw_diag_exit(void)253432490f9SCyrill Gorcunov static void __exit raw_diag_exit(void)
254432490f9SCyrill Gorcunov {
255432490f9SCyrill Gorcunov 	inet_diag_unregister(&raw_diag_handler);
256432490f9SCyrill Gorcunov }
257432490f9SCyrill Gorcunov 
258432490f9SCyrill Gorcunov module_init(raw_diag_init);
259432490f9SCyrill Gorcunov module_exit(raw_diag_exit);
260432490f9SCyrill Gorcunov MODULE_LICENSE("GPL");
261938dbeadSJakub Kicinski MODULE_DESCRIPTION("RAW socket monitoring via SOCK_DIAG");
262432490f9SCyrill Gorcunov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 2-255 /* AF_INET - IPPROTO_RAW */);
263432490f9SCyrill Gorcunov MODULE_ALIAS_NET_PF_PROTO_TYPE(PF_NETLINK, NETLINK_SOCK_DIAG, 10-255 /* AF_INET6 - IPPROTO_RAW */);
264