xref: /openbmc/linux/net/psample/psample.c (revision b664e06d)
1 /*
2  * net/psample/psample.c - Netlink channel for packet sampling
3  * Copyright (c) 2017 Yotam Gigi <yotamg@mellanox.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  */
9 
10 #include <linux/types.h>
11 #include <linux/kernel.h>
12 #include <linux/skbuff.h>
13 #include <linux/module.h>
14 #include <net/net_namespace.h>
15 #include <net/sock.h>
16 #include <net/netlink.h>
17 #include <net/genetlink.h>
18 #include <net/psample.h>
19 #include <linux/spinlock.h>
20 
21 #define PSAMPLE_MAX_PACKET_SIZE 0xffff
22 
23 static LIST_HEAD(psample_groups_list);
24 static DEFINE_SPINLOCK(psample_groups_lock);
25 
26 /* multicast groups */
27 enum psample_nl_multicast_groups {
28 	PSAMPLE_NL_MCGRP_CONFIG,
29 	PSAMPLE_NL_MCGRP_SAMPLE,
30 };
31 
32 static const struct genl_multicast_group psample_nl_mcgrps[] = {
33 	[PSAMPLE_NL_MCGRP_CONFIG] = { .name = PSAMPLE_NL_MCGRP_CONFIG_NAME },
34 	[PSAMPLE_NL_MCGRP_SAMPLE] = { .name = PSAMPLE_NL_MCGRP_SAMPLE_NAME },
35 };
36 
37 static struct genl_family psample_nl_family __ro_after_init;
38 
39 static int psample_group_nl_fill(struct sk_buff *msg,
40 				 struct psample_group *group,
41 				 enum psample_command cmd, u32 portid, u32 seq,
42 				 int flags)
43 {
44 	void *hdr;
45 	int ret;
46 
47 	hdr = genlmsg_put(msg, portid, seq, &psample_nl_family, flags, cmd);
48 	if (!hdr)
49 		return -EMSGSIZE;
50 
51 	ret = nla_put_u32(msg, PSAMPLE_ATTR_SAMPLE_GROUP, group->group_num);
52 	if (ret < 0)
53 		goto error;
54 
55 	ret = nla_put_u32(msg, PSAMPLE_ATTR_GROUP_REFCOUNT, group->refcount);
56 	if (ret < 0)
57 		goto error;
58 
59 	ret = nla_put_u32(msg, PSAMPLE_ATTR_GROUP_SEQ, group->seq);
60 	if (ret < 0)
61 		goto error;
62 
63 	genlmsg_end(msg, hdr);
64 	return 0;
65 
66 error:
67 	genlmsg_cancel(msg, hdr);
68 	return -EMSGSIZE;
69 }
70 
71 static int psample_nl_cmd_get_group_dumpit(struct sk_buff *msg,
72 					   struct netlink_callback *cb)
73 {
74 	struct psample_group *group;
75 	int start = cb->args[0];
76 	int idx = 0;
77 	int err;
78 
79 	spin_lock(&psample_groups_lock);
80 	list_for_each_entry(group, &psample_groups_list, list) {
81 		if (!net_eq(group->net, sock_net(msg->sk)))
82 			continue;
83 		if (idx < start) {
84 			idx++;
85 			continue;
86 		}
87 		err = psample_group_nl_fill(msg, group, PSAMPLE_CMD_NEW_GROUP,
88 					    NETLINK_CB(cb->skb).portid,
89 					    cb->nlh->nlmsg_seq, NLM_F_MULTI);
90 		if (err)
91 			break;
92 		idx++;
93 	}
94 
95 	spin_unlock(&psample_groups_lock);
96 	cb->args[0] = idx;
97 	return msg->len;
98 }
99 
100 static const struct genl_ops psample_nl_ops[] = {
101 	{
102 		.cmd = PSAMPLE_CMD_GET_GROUP,
103 		.validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
104 		.dumpit = psample_nl_cmd_get_group_dumpit,
105 		/* can be retrieved by unprivileged users */
106 	}
107 };
108 
109 static struct genl_family psample_nl_family __ro_after_init = {
110 	.name		= PSAMPLE_GENL_NAME,
111 	.version	= PSAMPLE_GENL_VERSION,
112 	.maxattr	= PSAMPLE_ATTR_MAX,
113 	.netnsok	= true,
114 	.module		= THIS_MODULE,
115 	.mcgrps		= psample_nl_mcgrps,
116 	.ops		= psample_nl_ops,
117 	.n_ops		= ARRAY_SIZE(psample_nl_ops),
118 	.n_mcgrps	= ARRAY_SIZE(psample_nl_mcgrps),
119 };
120 
121 static void psample_group_notify(struct psample_group *group,
122 				 enum psample_command cmd)
123 {
124 	struct sk_buff *msg;
125 	int err;
126 
127 	msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
128 	if (!msg)
129 		return;
130 
131 	err = psample_group_nl_fill(msg, group, cmd, 0, 0, NLM_F_MULTI);
132 	if (!err)
133 		genlmsg_multicast_netns(&psample_nl_family, group->net, msg, 0,
134 					PSAMPLE_NL_MCGRP_CONFIG, GFP_ATOMIC);
135 	else
136 		nlmsg_free(msg);
137 }
138 
139 static struct psample_group *psample_group_create(struct net *net,
140 						  u32 group_num)
141 {
142 	struct psample_group *group;
143 
144 	group = kzalloc(sizeof(*group), GFP_ATOMIC);
145 	if (!group)
146 		return NULL;
147 
148 	group->net = net;
149 	group->group_num = group_num;
150 	list_add_tail(&group->list, &psample_groups_list);
151 
152 	psample_group_notify(group, PSAMPLE_CMD_NEW_GROUP);
153 	return group;
154 }
155 
156 static void psample_group_destroy(struct psample_group *group)
157 {
158 	psample_group_notify(group, PSAMPLE_CMD_DEL_GROUP);
159 	list_del(&group->list);
160 	kfree(group);
161 }
162 
163 static struct psample_group *
164 psample_group_lookup(struct net *net, u32 group_num)
165 {
166 	struct psample_group *group;
167 
168 	list_for_each_entry(group, &psample_groups_list, list)
169 		if ((group->group_num == group_num) && (group->net == net))
170 			return group;
171 	return NULL;
172 }
173 
174 struct psample_group *psample_group_get(struct net *net, u32 group_num)
175 {
176 	struct psample_group *group;
177 
178 	spin_lock(&psample_groups_lock);
179 
180 	group = psample_group_lookup(net, group_num);
181 	if (!group) {
182 		group = psample_group_create(net, group_num);
183 		if (!group)
184 			goto out;
185 	}
186 	group->refcount++;
187 
188 out:
189 	spin_unlock(&psample_groups_lock);
190 	return group;
191 }
192 EXPORT_SYMBOL_GPL(psample_group_get);
193 
194 void psample_group_put(struct psample_group *group)
195 {
196 	spin_lock(&psample_groups_lock);
197 
198 	if (--group->refcount == 0)
199 		psample_group_destroy(group);
200 
201 	spin_unlock(&psample_groups_lock);
202 }
203 EXPORT_SYMBOL_GPL(psample_group_put);
204 
205 void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
206 			   u32 trunc_size, int in_ifindex, int out_ifindex,
207 			   u32 sample_rate)
208 {
209 	struct sk_buff *nl_skb;
210 	int data_len;
211 	int meta_len;
212 	void *data;
213 	int ret;
214 
215 	meta_len = (in_ifindex ? nla_total_size(sizeof(u16)) : 0) +
216 		   (out_ifindex ? nla_total_size(sizeof(u16)) : 0) +
217 		   nla_total_size(sizeof(u32)) +	/* sample_rate */
218 		   nla_total_size(sizeof(u32)) +	/* orig_size */
219 		   nla_total_size(sizeof(u32)) +	/* group_num */
220 		   nla_total_size(sizeof(u32));		/* seq */
221 
222 	data_len = min(skb->len, trunc_size);
223 	if (meta_len + nla_total_size(data_len) > PSAMPLE_MAX_PACKET_SIZE)
224 		data_len = PSAMPLE_MAX_PACKET_SIZE - meta_len - NLA_HDRLEN
225 			    - NLA_ALIGNTO;
226 
227 	nl_skb = genlmsg_new(meta_len + data_len, GFP_ATOMIC);
228 	if (unlikely(!nl_skb))
229 		return;
230 
231 	data = genlmsg_put(nl_skb, 0, 0, &psample_nl_family, 0,
232 			   PSAMPLE_CMD_SAMPLE);
233 	if (unlikely(!data))
234 		goto error;
235 
236 	if (in_ifindex) {
237 		ret = nla_put_u16(nl_skb, PSAMPLE_ATTR_IIFINDEX, in_ifindex);
238 		if (unlikely(ret < 0))
239 			goto error;
240 	}
241 
242 	if (out_ifindex) {
243 		ret = nla_put_u16(nl_skb, PSAMPLE_ATTR_OIFINDEX, out_ifindex);
244 		if (unlikely(ret < 0))
245 			goto error;
246 	}
247 
248 	ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_SAMPLE_RATE, sample_rate);
249 	if (unlikely(ret < 0))
250 		goto error;
251 
252 	ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_ORIGSIZE, skb->len);
253 	if (unlikely(ret < 0))
254 		goto error;
255 
256 	ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_SAMPLE_GROUP, group->group_num);
257 	if (unlikely(ret < 0))
258 		goto error;
259 
260 	ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_GROUP_SEQ, group->seq++);
261 	if (unlikely(ret < 0))
262 		goto error;
263 
264 	if (data_len) {
265 		int nla_len = nla_total_size(data_len);
266 		struct nlattr *nla;
267 
268 		nla = skb_put(nl_skb, nla_len);
269 		nla->nla_type = PSAMPLE_ATTR_DATA;
270 		nla->nla_len = nla_attr_size(data_len);
271 
272 		if (skb_copy_bits(skb, 0, nla_data(nla), data_len))
273 			goto error;
274 	}
275 
276 	genlmsg_end(nl_skb, data);
277 	genlmsg_multicast_netns(&psample_nl_family, group->net, nl_skb, 0,
278 				PSAMPLE_NL_MCGRP_SAMPLE, GFP_ATOMIC);
279 
280 	return;
281 error:
282 	pr_err_ratelimited("Could not create psample log message\n");
283 	nlmsg_free(nl_skb);
284 }
285 EXPORT_SYMBOL_GPL(psample_sample_packet);
286 
287 static int __init psample_module_init(void)
288 {
289 	return genl_register_family(&psample_nl_family);
290 }
291 
292 static void __exit psample_module_exit(void)
293 {
294 	genl_unregister_family(&psample_nl_family);
295 }
296 
297 module_init(psample_module_init);
298 module_exit(psample_module_exit);
299 
300 MODULE_AUTHOR("Yotam Gigi <yotam.gi@gmail.com>");
301 MODULE_DESCRIPTION("netlink channel for packet sampling");
302 MODULE_LICENSE("GPL v2");
303