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