Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /*
0003  * net/psample/psample.c - Netlink channel for packet sampling
0004  * Copyright (c) 2017 Yotam Gigi <yotamg@mellanox.com>
0005  */
0006 
0007 #include <linux/types.h>
0008 #include <linux/kernel.h>
0009 #include <linux/skbuff.h>
0010 #include <linux/module.h>
0011 #include <linux/timekeeping.h>
0012 #include <net/net_namespace.h>
0013 #include <net/sock.h>
0014 #include <net/netlink.h>
0015 #include <net/genetlink.h>
0016 #include <net/psample.h>
0017 #include <linux/spinlock.h>
0018 #include <net/ip_tunnels.h>
0019 #include <net/dst_metadata.h>
0020 
0021 #define PSAMPLE_MAX_PACKET_SIZE 0xffff
0022 
0023 static LIST_HEAD(psample_groups_list);
0024 static DEFINE_SPINLOCK(psample_groups_lock);
0025 
0026 /* multicast groups */
0027 enum psample_nl_multicast_groups {
0028     PSAMPLE_NL_MCGRP_CONFIG,
0029     PSAMPLE_NL_MCGRP_SAMPLE,
0030 };
0031 
0032 static const struct genl_multicast_group psample_nl_mcgrps[] = {
0033     [PSAMPLE_NL_MCGRP_CONFIG] = { .name = PSAMPLE_NL_MCGRP_CONFIG_NAME },
0034     [PSAMPLE_NL_MCGRP_SAMPLE] = { .name = PSAMPLE_NL_MCGRP_SAMPLE_NAME },
0035 };
0036 
0037 static struct genl_family psample_nl_family __ro_after_init;
0038 
0039 static int psample_group_nl_fill(struct sk_buff *msg,
0040                  struct psample_group *group,
0041                  enum psample_command cmd, u32 portid, u32 seq,
0042                  int flags)
0043 {
0044     void *hdr;
0045     int ret;
0046 
0047     hdr = genlmsg_put(msg, portid, seq, &psample_nl_family, flags, cmd);
0048     if (!hdr)
0049         return -EMSGSIZE;
0050 
0051     ret = nla_put_u32(msg, PSAMPLE_ATTR_SAMPLE_GROUP, group->group_num);
0052     if (ret < 0)
0053         goto error;
0054 
0055     ret = nla_put_u32(msg, PSAMPLE_ATTR_GROUP_REFCOUNT, group->refcount);
0056     if (ret < 0)
0057         goto error;
0058 
0059     ret = nla_put_u32(msg, PSAMPLE_ATTR_GROUP_SEQ, group->seq);
0060     if (ret < 0)
0061         goto error;
0062 
0063     genlmsg_end(msg, hdr);
0064     return 0;
0065 
0066 error:
0067     genlmsg_cancel(msg, hdr);
0068     return -EMSGSIZE;
0069 }
0070 
0071 static int psample_nl_cmd_get_group_dumpit(struct sk_buff *msg,
0072                        struct netlink_callback *cb)
0073 {
0074     struct psample_group *group;
0075     int start = cb->args[0];
0076     int idx = 0;
0077     int err;
0078 
0079     spin_lock_bh(&psample_groups_lock);
0080     list_for_each_entry(group, &psample_groups_list, list) {
0081         if (!net_eq(group->net, sock_net(msg->sk)))
0082             continue;
0083         if (idx < start) {
0084             idx++;
0085             continue;
0086         }
0087         err = psample_group_nl_fill(msg, group, PSAMPLE_CMD_NEW_GROUP,
0088                         NETLINK_CB(cb->skb).portid,
0089                         cb->nlh->nlmsg_seq, NLM_F_MULTI);
0090         if (err)
0091             break;
0092         idx++;
0093     }
0094 
0095     spin_unlock_bh(&psample_groups_lock);
0096     cb->args[0] = idx;
0097     return msg->len;
0098 }
0099 
0100 static const struct genl_small_ops psample_nl_ops[] = {
0101     {
0102         .cmd = PSAMPLE_CMD_GET_GROUP,
0103         .validate = GENL_DONT_VALIDATE_STRICT | GENL_DONT_VALIDATE_DUMP,
0104         .dumpit = psample_nl_cmd_get_group_dumpit,
0105         /* can be retrieved by unprivileged users */
0106     }
0107 };
0108 
0109 static struct genl_family psample_nl_family __ro_after_init = {
0110     .name       = PSAMPLE_GENL_NAME,
0111     .version    = PSAMPLE_GENL_VERSION,
0112     .maxattr    = PSAMPLE_ATTR_MAX,
0113     .netnsok    = true,
0114     .module     = THIS_MODULE,
0115     .mcgrps     = psample_nl_mcgrps,
0116     .small_ops  = psample_nl_ops,
0117     .n_small_ops    = ARRAY_SIZE(psample_nl_ops),
0118     .n_mcgrps   = ARRAY_SIZE(psample_nl_mcgrps),
0119 };
0120 
0121 static void psample_group_notify(struct psample_group *group,
0122                  enum psample_command cmd)
0123 {
0124     struct sk_buff *msg;
0125     int err;
0126 
0127     msg = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
0128     if (!msg)
0129         return;
0130 
0131     err = psample_group_nl_fill(msg, group, cmd, 0, 0, NLM_F_MULTI);
0132     if (!err)
0133         genlmsg_multicast_netns(&psample_nl_family, group->net, msg, 0,
0134                     PSAMPLE_NL_MCGRP_CONFIG, GFP_ATOMIC);
0135     else
0136         nlmsg_free(msg);
0137 }
0138 
0139 static struct psample_group *psample_group_create(struct net *net,
0140                           u32 group_num)
0141 {
0142     struct psample_group *group;
0143 
0144     group = kzalloc(sizeof(*group), GFP_ATOMIC);
0145     if (!group)
0146         return NULL;
0147 
0148     group->net = net;
0149     group->group_num = group_num;
0150     list_add_tail(&group->list, &psample_groups_list);
0151 
0152     psample_group_notify(group, PSAMPLE_CMD_NEW_GROUP);
0153     return group;
0154 }
0155 
0156 static void psample_group_destroy(struct psample_group *group)
0157 {
0158     psample_group_notify(group, PSAMPLE_CMD_DEL_GROUP);
0159     list_del(&group->list);
0160     kfree_rcu(group, rcu);
0161 }
0162 
0163 static struct psample_group *
0164 psample_group_lookup(struct net *net, u32 group_num)
0165 {
0166     struct psample_group *group;
0167 
0168     list_for_each_entry(group, &psample_groups_list, list)
0169         if ((group->group_num == group_num) && (group->net == net))
0170             return group;
0171     return NULL;
0172 }
0173 
0174 struct psample_group *psample_group_get(struct net *net, u32 group_num)
0175 {
0176     struct psample_group *group;
0177 
0178     spin_lock_bh(&psample_groups_lock);
0179 
0180     group = psample_group_lookup(net, group_num);
0181     if (!group) {
0182         group = psample_group_create(net, group_num);
0183         if (!group)
0184             goto out;
0185     }
0186     group->refcount++;
0187 
0188 out:
0189     spin_unlock_bh(&psample_groups_lock);
0190     return group;
0191 }
0192 EXPORT_SYMBOL_GPL(psample_group_get);
0193 
0194 void psample_group_take(struct psample_group *group)
0195 {
0196     spin_lock_bh(&psample_groups_lock);
0197     group->refcount++;
0198     spin_unlock_bh(&psample_groups_lock);
0199 }
0200 EXPORT_SYMBOL_GPL(psample_group_take);
0201 
0202 void psample_group_put(struct psample_group *group)
0203 {
0204     spin_lock_bh(&psample_groups_lock);
0205 
0206     if (--group->refcount == 0)
0207         psample_group_destroy(group);
0208 
0209     spin_unlock_bh(&psample_groups_lock);
0210 }
0211 EXPORT_SYMBOL_GPL(psample_group_put);
0212 
0213 #ifdef CONFIG_INET
0214 static int __psample_ip_tun_to_nlattr(struct sk_buff *skb,
0215                   struct ip_tunnel_info *tun_info)
0216 {
0217     unsigned short tun_proto = ip_tunnel_info_af(tun_info);
0218     const void *tun_opts = ip_tunnel_info_opts(tun_info);
0219     const struct ip_tunnel_key *tun_key = &tun_info->key;
0220     int tun_opts_len = tun_info->options_len;
0221 
0222     if (tun_key->tun_flags & TUNNEL_KEY &&
0223         nla_put_be64(skb, PSAMPLE_TUNNEL_KEY_ATTR_ID, tun_key->tun_id,
0224              PSAMPLE_TUNNEL_KEY_ATTR_PAD))
0225         return -EMSGSIZE;
0226 
0227     if (tun_info->mode & IP_TUNNEL_INFO_BRIDGE &&
0228         nla_put_flag(skb, PSAMPLE_TUNNEL_KEY_ATTR_IPV4_INFO_BRIDGE))
0229         return -EMSGSIZE;
0230 
0231     switch (tun_proto) {
0232     case AF_INET:
0233         if (tun_key->u.ipv4.src &&
0234             nla_put_in_addr(skb, PSAMPLE_TUNNEL_KEY_ATTR_IPV4_SRC,
0235                     tun_key->u.ipv4.src))
0236             return -EMSGSIZE;
0237         if (tun_key->u.ipv4.dst &&
0238             nla_put_in_addr(skb, PSAMPLE_TUNNEL_KEY_ATTR_IPV4_DST,
0239                     tun_key->u.ipv4.dst))
0240             return -EMSGSIZE;
0241         break;
0242     case AF_INET6:
0243         if (!ipv6_addr_any(&tun_key->u.ipv6.src) &&
0244             nla_put_in6_addr(skb, PSAMPLE_TUNNEL_KEY_ATTR_IPV6_SRC,
0245                      &tun_key->u.ipv6.src))
0246             return -EMSGSIZE;
0247         if (!ipv6_addr_any(&tun_key->u.ipv6.dst) &&
0248             nla_put_in6_addr(skb, PSAMPLE_TUNNEL_KEY_ATTR_IPV6_DST,
0249                      &tun_key->u.ipv6.dst))
0250             return -EMSGSIZE;
0251         break;
0252     }
0253     if (tun_key->tos &&
0254         nla_put_u8(skb, PSAMPLE_TUNNEL_KEY_ATTR_TOS, tun_key->tos))
0255         return -EMSGSIZE;
0256     if (nla_put_u8(skb, PSAMPLE_TUNNEL_KEY_ATTR_TTL, tun_key->ttl))
0257         return -EMSGSIZE;
0258     if ((tun_key->tun_flags & TUNNEL_DONT_FRAGMENT) &&
0259         nla_put_flag(skb, PSAMPLE_TUNNEL_KEY_ATTR_DONT_FRAGMENT))
0260         return -EMSGSIZE;
0261     if ((tun_key->tun_flags & TUNNEL_CSUM) &&
0262         nla_put_flag(skb, PSAMPLE_TUNNEL_KEY_ATTR_CSUM))
0263         return -EMSGSIZE;
0264     if (tun_key->tp_src &&
0265         nla_put_be16(skb, PSAMPLE_TUNNEL_KEY_ATTR_TP_SRC, tun_key->tp_src))
0266         return -EMSGSIZE;
0267     if (tun_key->tp_dst &&
0268         nla_put_be16(skb, PSAMPLE_TUNNEL_KEY_ATTR_TP_DST, tun_key->tp_dst))
0269         return -EMSGSIZE;
0270     if ((tun_key->tun_flags & TUNNEL_OAM) &&
0271         nla_put_flag(skb, PSAMPLE_TUNNEL_KEY_ATTR_OAM))
0272         return -EMSGSIZE;
0273     if (tun_opts_len) {
0274         if (tun_key->tun_flags & TUNNEL_GENEVE_OPT &&
0275             nla_put(skb, PSAMPLE_TUNNEL_KEY_ATTR_GENEVE_OPTS,
0276                 tun_opts_len, tun_opts))
0277             return -EMSGSIZE;
0278         else if (tun_key->tun_flags & TUNNEL_ERSPAN_OPT &&
0279              nla_put(skb, PSAMPLE_TUNNEL_KEY_ATTR_ERSPAN_OPTS,
0280                  tun_opts_len, tun_opts))
0281             return -EMSGSIZE;
0282     }
0283 
0284     return 0;
0285 }
0286 
0287 static int psample_ip_tun_to_nlattr(struct sk_buff *skb,
0288                 struct ip_tunnel_info *tun_info)
0289 {
0290     struct nlattr *nla;
0291     int err;
0292 
0293     nla = nla_nest_start_noflag(skb, PSAMPLE_ATTR_TUNNEL);
0294     if (!nla)
0295         return -EMSGSIZE;
0296 
0297     err = __psample_ip_tun_to_nlattr(skb, tun_info);
0298     if (err) {
0299         nla_nest_cancel(skb, nla);
0300         return err;
0301     }
0302 
0303     nla_nest_end(skb, nla);
0304 
0305     return 0;
0306 }
0307 
0308 static int psample_tunnel_meta_len(struct ip_tunnel_info *tun_info)
0309 {
0310     unsigned short tun_proto = ip_tunnel_info_af(tun_info);
0311     const struct ip_tunnel_key *tun_key = &tun_info->key;
0312     int tun_opts_len = tun_info->options_len;
0313     int sum = nla_total_size(0);    /* PSAMPLE_ATTR_TUNNEL */
0314 
0315     if (tun_key->tun_flags & TUNNEL_KEY)
0316         sum += nla_total_size_64bit(sizeof(u64));
0317 
0318     if (tun_info->mode & IP_TUNNEL_INFO_BRIDGE)
0319         sum += nla_total_size(0);
0320 
0321     switch (tun_proto) {
0322     case AF_INET:
0323         if (tun_key->u.ipv4.src)
0324             sum += nla_total_size(sizeof(u32));
0325         if (tun_key->u.ipv4.dst)
0326             sum += nla_total_size(sizeof(u32));
0327         break;
0328     case AF_INET6:
0329         if (!ipv6_addr_any(&tun_key->u.ipv6.src))
0330             sum += nla_total_size(sizeof(struct in6_addr));
0331         if (!ipv6_addr_any(&tun_key->u.ipv6.dst))
0332             sum += nla_total_size(sizeof(struct in6_addr));
0333         break;
0334     }
0335     if (tun_key->tos)
0336         sum += nla_total_size(sizeof(u8));
0337     sum += nla_total_size(sizeof(u8));  /* TTL */
0338     if (tun_key->tun_flags & TUNNEL_DONT_FRAGMENT)
0339         sum += nla_total_size(0);
0340     if (tun_key->tun_flags & TUNNEL_CSUM)
0341         sum += nla_total_size(0);
0342     if (tun_key->tp_src)
0343         sum += nla_total_size(sizeof(u16));
0344     if (tun_key->tp_dst)
0345         sum += nla_total_size(sizeof(u16));
0346     if (tun_key->tun_flags & TUNNEL_OAM)
0347         sum += nla_total_size(0);
0348     if (tun_opts_len) {
0349         if (tun_key->tun_flags & TUNNEL_GENEVE_OPT)
0350             sum += nla_total_size(tun_opts_len);
0351         else if (tun_key->tun_flags & TUNNEL_ERSPAN_OPT)
0352             sum += nla_total_size(tun_opts_len);
0353     }
0354 
0355     return sum;
0356 }
0357 #endif
0358 
0359 void psample_sample_packet(struct psample_group *group, struct sk_buff *skb,
0360                u32 sample_rate, const struct psample_metadata *md)
0361 {
0362     ktime_t tstamp = ktime_get_real();
0363     int out_ifindex = md->out_ifindex;
0364     int in_ifindex = md->in_ifindex;
0365     u32 trunc_size = md->trunc_size;
0366 #ifdef CONFIG_INET
0367     struct ip_tunnel_info *tun_info;
0368 #endif
0369     struct sk_buff *nl_skb;
0370     int data_len;
0371     int meta_len;
0372     void *data;
0373     int ret;
0374 
0375     meta_len = (in_ifindex ? nla_total_size(sizeof(u16)) : 0) +
0376            (out_ifindex ? nla_total_size(sizeof(u16)) : 0) +
0377            (md->out_tc_valid ? nla_total_size(sizeof(u16)) : 0) +
0378            (md->out_tc_occ_valid ? nla_total_size_64bit(sizeof(u64)) : 0) +
0379            (md->latency_valid ? nla_total_size_64bit(sizeof(u64)) : 0) +
0380            nla_total_size(sizeof(u32)) +    /* sample_rate */
0381            nla_total_size(sizeof(u32)) +    /* orig_size */
0382            nla_total_size(sizeof(u32)) +    /* group_num */
0383            nla_total_size(sizeof(u32)) +    /* seq */
0384            nla_total_size_64bit(sizeof(u64)) +  /* timestamp */
0385            nla_total_size(sizeof(u16));     /* protocol */
0386 
0387 #ifdef CONFIG_INET
0388     tun_info = skb_tunnel_info(skb);
0389     if (tun_info)
0390         meta_len += psample_tunnel_meta_len(tun_info);
0391 #endif
0392 
0393     data_len = min(skb->len, trunc_size);
0394     if (meta_len + nla_total_size(data_len) > PSAMPLE_MAX_PACKET_SIZE)
0395         data_len = PSAMPLE_MAX_PACKET_SIZE - meta_len - NLA_HDRLEN
0396                 - NLA_ALIGNTO;
0397 
0398     nl_skb = genlmsg_new(meta_len + nla_total_size(data_len), GFP_ATOMIC);
0399     if (unlikely(!nl_skb))
0400         return;
0401 
0402     data = genlmsg_put(nl_skb, 0, 0, &psample_nl_family, 0,
0403                PSAMPLE_CMD_SAMPLE);
0404     if (unlikely(!data))
0405         goto error;
0406 
0407     if (in_ifindex) {
0408         ret = nla_put_u16(nl_skb, PSAMPLE_ATTR_IIFINDEX, in_ifindex);
0409         if (unlikely(ret < 0))
0410             goto error;
0411     }
0412 
0413     if (out_ifindex) {
0414         ret = nla_put_u16(nl_skb, PSAMPLE_ATTR_OIFINDEX, out_ifindex);
0415         if (unlikely(ret < 0))
0416             goto error;
0417     }
0418 
0419     ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_SAMPLE_RATE, sample_rate);
0420     if (unlikely(ret < 0))
0421         goto error;
0422 
0423     ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_ORIGSIZE, skb->len);
0424     if (unlikely(ret < 0))
0425         goto error;
0426 
0427     ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_SAMPLE_GROUP, group->group_num);
0428     if (unlikely(ret < 0))
0429         goto error;
0430 
0431     ret = nla_put_u32(nl_skb, PSAMPLE_ATTR_GROUP_SEQ, group->seq++);
0432     if (unlikely(ret < 0))
0433         goto error;
0434 
0435     if (md->out_tc_valid) {
0436         ret = nla_put_u16(nl_skb, PSAMPLE_ATTR_OUT_TC, md->out_tc);
0437         if (unlikely(ret < 0))
0438             goto error;
0439     }
0440 
0441     if (md->out_tc_occ_valid) {
0442         ret = nla_put_u64_64bit(nl_skb, PSAMPLE_ATTR_OUT_TC_OCC,
0443                     md->out_tc_occ, PSAMPLE_ATTR_PAD);
0444         if (unlikely(ret < 0))
0445             goto error;
0446     }
0447 
0448     if (md->latency_valid) {
0449         ret = nla_put_u64_64bit(nl_skb, PSAMPLE_ATTR_LATENCY,
0450                     md->latency, PSAMPLE_ATTR_PAD);
0451         if (unlikely(ret < 0))
0452             goto error;
0453     }
0454 
0455     ret = nla_put_u64_64bit(nl_skb, PSAMPLE_ATTR_TIMESTAMP,
0456                 ktime_to_ns(tstamp), PSAMPLE_ATTR_PAD);
0457     if (unlikely(ret < 0))
0458         goto error;
0459 
0460     ret = nla_put_u16(nl_skb, PSAMPLE_ATTR_PROTO,
0461               be16_to_cpu(skb->protocol));
0462     if (unlikely(ret < 0))
0463         goto error;
0464 
0465     if (data_len) {
0466         int nla_len = nla_total_size(data_len);
0467         struct nlattr *nla;
0468 
0469         nla = skb_put(nl_skb, nla_len);
0470         nla->nla_type = PSAMPLE_ATTR_DATA;
0471         nla->nla_len = nla_attr_size(data_len);
0472 
0473         if (skb_copy_bits(skb, 0, nla_data(nla), data_len))
0474             goto error;
0475     }
0476 
0477 #ifdef CONFIG_INET
0478     if (tun_info) {
0479         ret = psample_ip_tun_to_nlattr(nl_skb, tun_info);
0480         if (unlikely(ret < 0))
0481             goto error;
0482     }
0483 #endif
0484 
0485     genlmsg_end(nl_skb, data);
0486     genlmsg_multicast_netns(&psample_nl_family, group->net, nl_skb, 0,
0487                 PSAMPLE_NL_MCGRP_SAMPLE, GFP_ATOMIC);
0488 
0489     return;
0490 error:
0491     pr_err_ratelimited("Could not create psample log message\n");
0492     nlmsg_free(nl_skb);
0493 }
0494 EXPORT_SYMBOL_GPL(psample_sample_packet);
0495 
0496 static int __init psample_module_init(void)
0497 {
0498     return genl_register_family(&psample_nl_family);
0499 }
0500 
0501 static void __exit psample_module_exit(void)
0502 {
0503     genl_unregister_family(&psample_nl_family);
0504 }
0505 
0506 module_init(psample_module_init);
0507 module_exit(psample_module_exit);
0508 
0509 MODULE_AUTHOR("Yotam Gigi <yotam.gi@gmail.com>");
0510 MODULE_DESCRIPTION("netlink channel for packet sampling");
0511 MODULE_LICENSE("GPL v2");