xref: /openbmc/linux/net/ipv4/raw_diag.c (revision f66501dc)
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 
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 {
27 		pr_warn_once("Unexpected inet family %d\n",
28 			     r->sdiag_family);
29 		WARN_ON_ONCE(1);
30 		return ERR_PTR(-EINVAL);
31 	}
32 }
33 
34 /*
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.
38  */
39 
40 static struct sock *raw_lookup(struct net *net, struct sock *from,
41 			       const struct inet_diag_req_v2 *req)
42 {
43 	struct inet_diag_req_raw *r = (void *)req;
44 	struct sock *sk = NULL;
45 
46 	if (r->sdiag_family == AF_INET)
47 		sk = __raw_v4_lookup(net, from, r->sdiag_raw_protocol,
48 				     r->id.idiag_dst[0],
49 				     r->id.idiag_src[0],
50 				     r->id.idiag_if, 0);
51 #if IS_ENABLED(CONFIG_IPV6)
52 	else
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,
56 				     r->id.idiag_if, 0);
57 #endif
58 	return sk;
59 }
60 
61 static struct sock *raw_sock_get(struct net *net, const struct inet_diag_req_v2 *r)
62 {
63 	struct raw_hashinfo *hashinfo = raw_get_hashinfo(r);
64 	struct sock *sk = NULL, *s;
65 	int slot;
66 
67 	if (IS_ERR(hashinfo))
68 		return ERR_CAST(hashinfo);
69 
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);
74 			if (sk) {
75 				/*
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.
81 				 */
82 				sock_hold(sk);
83 				goto out_unlock;
84 			}
85 		}
86 	}
87 out_unlock:
88 	read_unlock(&hashinfo->lock);
89 
90 	return sk ? sk : ERR_PTR(-ENOENT);
91 }
92 
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)
96 {
97 	struct net *net = sock_net(in_skb->sk);
98 	struct sk_buff *rep;
99 	struct sock *sk;
100 	int err;
101 
102 	sk = raw_sock_get(net, r);
103 	if (IS_ERR(sk))
104 		return PTR_ERR(sk);
105 
106 	rep = nlmsg_new(sizeof(struct inet_diag_msg) +
107 			sizeof(struct inet_diag_meminfo) + 64,
108 			GFP_KERNEL);
109 	if (!rep) {
110 		sock_put(sk);
111 		return -ENOMEM;
112 	}
113 
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));
119 	sock_put(sk);
120 
121 	if (err < 0) {
122 		kfree_skb(rep);
123 		return err;
124 	}
125 
126 	err = netlink_unicast(net->diag_nlsk, rep,
127 			      NETLINK_CB(in_skb).portid,
128 			      MSG_DONTWAIT);
129 	if (err > 0)
130 		err = 0;
131 	return err;
132 }
133 
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)
138 {
139 	if (!inet_diag_bc_sk(bc, sk))
140 		return 0;
141 
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,
146 				 cb->nlh, net_admin);
147 }
148 
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)
151 {
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;
157 
158 	if (IS_ERR(hashinfo))
159 		return;
160 
161 	s_slot = cb->args[0];
162 	num = s_num = cb->args[1];
163 
164 	read_lock(&hashinfo->lock);
165 	for (slot = s_slot; slot < RAW_HTABLE_SIZE; s_num = 0, slot++) {
166 		num = 0;
167 
168 		sk_for_each(sk, &hashinfo->ht[slot]) {
169 			struct inet_sock *inet = inet_sk(sk);
170 
171 			if (!net_eq(sock_net(sk), net))
172 				continue;
173 			if (num < s_num)
174 				goto next;
175 			if (sk->sk_family != r->sdiag_family)
176 				goto next;
177 			if (r->id.idiag_sport != inet->inet_sport &&
178 			    r->id.idiag_sport)
179 				goto next;
180 			if (r->id.idiag_dport != inet->inet_dport &&
181 			    r->id.idiag_dport)
182 				goto next;
183 			if (sk_diag_dump(sk, skb, cb, r, bc, net_admin) < 0)
184 				goto out_unlock;
185 next:
186 			num++;
187 		}
188 	}
189 
190 out_unlock:
191 	read_unlock(&hashinfo->lock);
192 
193 	cb->args[0] = slot;
194 	cb->args[1] = num;
195 }
196 
197 static void raw_diag_get_info(struct sock *sk, struct inet_diag_msg *r,
198 			      void *info)
199 {
200 	r->idiag_rqueue = sk_rmem_alloc_get(sk);
201 	r->idiag_wqueue = sk_wmem_alloc_get(sk);
202 }
203 
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)
207 {
208 	struct net *net = sock_net(in_skb->sk);
209 	struct sock *sk;
210 	int err;
211 
212 	sk = raw_sock_get(net, r);
213 	if (IS_ERR(sk))
214 		return PTR_ERR(sk);
215 	err = sock_diag_destroy(sk, ECONNABORTED);
216 	sock_put(sk);
217 	return err;
218 }
219 #endif
220 
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,
229 #endif
230 };
231 
232 static void __always_unused __check_inet_diag_req_raw(void)
233 {
234 	/*
235 	 * Make sure the two structures are identical,
236 	 * except the @pad field.
237 	 */
238 #define __offset_mismatch(m1, m2)			\
239 	(offsetof(struct inet_diag_req_v2, m1) !=	\
240 	 offsetof(struct inet_diag_req_raw, m2))
241 
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
251 }
252 
253 static int __init raw_diag_init(void)
254 {
255 	return inet_diag_register(&raw_diag_handler);
256 }
257 
258 static void __exit raw_diag_exit(void)
259 {
260 	inet_diag_unregister(&raw_diag_handler);
261 }
262 
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 */);
268