Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
0004  */
0005 
0006 #include <linux/module.h>
0007 #include <linux/init.h>
0008 #include <linux/kernel.h>
0009 #include <linux/skbuff.h>
0010 #include <linux/rtnetlink.h>
0011 #include <linux/filter.h>
0012 #include <linux/bpf.h>
0013 
0014 #include <net/netlink.h>
0015 #include <net/sock.h>
0016 #include <net/pkt_sched.h>
0017 #include <net/pkt_cls.h>
0018 
0019 #include <linux/tc_act/tc_bpf.h>
0020 #include <net/tc_act/tc_bpf.h>
0021 
0022 #define ACT_BPF_NAME_LEN    256
0023 
0024 struct tcf_bpf_cfg {
0025     struct bpf_prog *filter;
0026     struct sock_filter *bpf_ops;
0027     const char *bpf_name;
0028     u16 bpf_num_ops;
0029     bool is_ebpf;
0030 };
0031 
0032 static unsigned int bpf_net_id;
0033 static struct tc_action_ops act_bpf_ops;
0034 
0035 static int tcf_bpf_act(struct sk_buff *skb, const struct tc_action *act,
0036                struct tcf_result *res)
0037 {
0038     bool at_ingress = skb_at_tc_ingress(skb);
0039     struct tcf_bpf *prog = to_bpf(act);
0040     struct bpf_prog *filter;
0041     int action, filter_res;
0042 
0043     tcf_lastuse_update(&prog->tcf_tm);
0044     bstats_update(this_cpu_ptr(prog->common.cpu_bstats), skb);
0045 
0046     filter = rcu_dereference(prog->filter);
0047     if (at_ingress) {
0048         __skb_push(skb, skb->mac_len);
0049         bpf_compute_data_pointers(skb);
0050         filter_res = bpf_prog_run(filter, skb);
0051         __skb_pull(skb, skb->mac_len);
0052     } else {
0053         bpf_compute_data_pointers(skb);
0054         filter_res = bpf_prog_run(filter, skb);
0055     }
0056     if (unlikely(!skb->tstamp && skb->mono_delivery_time))
0057         skb->mono_delivery_time = 0;
0058     if (skb_sk_is_prefetched(skb) && filter_res != TC_ACT_OK)
0059         skb_orphan(skb);
0060 
0061     /* A BPF program may overwrite the default action opcode.
0062      * Similarly as in cls_bpf, if filter_res == -1 we use the
0063      * default action specified from tc.
0064      *
0065      * In case a different well-known TC_ACT opcode has been
0066      * returned, it will overwrite the default one.
0067      *
0068      * For everything else that is unknown, TC_ACT_UNSPEC is
0069      * returned.
0070      */
0071     switch (filter_res) {
0072     case TC_ACT_PIPE:
0073     case TC_ACT_RECLASSIFY:
0074     case TC_ACT_OK:
0075     case TC_ACT_REDIRECT:
0076         action = filter_res;
0077         break;
0078     case TC_ACT_SHOT:
0079         action = filter_res;
0080         qstats_drop_inc(this_cpu_ptr(prog->common.cpu_qstats));
0081         break;
0082     case TC_ACT_UNSPEC:
0083         action = prog->tcf_action;
0084         break;
0085     default:
0086         action = TC_ACT_UNSPEC;
0087         break;
0088     }
0089 
0090     return action;
0091 }
0092 
0093 static bool tcf_bpf_is_ebpf(const struct tcf_bpf *prog)
0094 {
0095     return !prog->bpf_ops;
0096 }
0097 
0098 static int tcf_bpf_dump_bpf_info(const struct tcf_bpf *prog,
0099                  struct sk_buff *skb)
0100 {
0101     struct nlattr *nla;
0102 
0103     if (nla_put_u16(skb, TCA_ACT_BPF_OPS_LEN, prog->bpf_num_ops))
0104         return -EMSGSIZE;
0105 
0106     nla = nla_reserve(skb, TCA_ACT_BPF_OPS, prog->bpf_num_ops *
0107               sizeof(struct sock_filter));
0108     if (nla == NULL)
0109         return -EMSGSIZE;
0110 
0111     memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
0112 
0113     return 0;
0114 }
0115 
0116 static int tcf_bpf_dump_ebpf_info(const struct tcf_bpf *prog,
0117                   struct sk_buff *skb)
0118 {
0119     struct nlattr *nla;
0120 
0121     if (prog->bpf_name &&
0122         nla_put_string(skb, TCA_ACT_BPF_NAME, prog->bpf_name))
0123         return -EMSGSIZE;
0124 
0125     if (nla_put_u32(skb, TCA_ACT_BPF_ID, prog->filter->aux->id))
0126         return -EMSGSIZE;
0127 
0128     nla = nla_reserve(skb, TCA_ACT_BPF_TAG, sizeof(prog->filter->tag));
0129     if (nla == NULL)
0130         return -EMSGSIZE;
0131 
0132     memcpy(nla_data(nla), prog->filter->tag, nla_len(nla));
0133 
0134     return 0;
0135 }
0136 
0137 static int tcf_bpf_dump(struct sk_buff *skb, struct tc_action *act,
0138             int bind, int ref)
0139 {
0140     unsigned char *tp = skb_tail_pointer(skb);
0141     struct tcf_bpf *prog = to_bpf(act);
0142     struct tc_act_bpf opt = {
0143         .index   = prog->tcf_index,
0144         .refcnt  = refcount_read(&prog->tcf_refcnt) - ref,
0145         .bindcnt = atomic_read(&prog->tcf_bindcnt) - bind,
0146     };
0147     struct tcf_t tm;
0148     int ret;
0149 
0150     spin_lock_bh(&prog->tcf_lock);
0151     opt.action = prog->tcf_action;
0152     if (nla_put(skb, TCA_ACT_BPF_PARMS, sizeof(opt), &opt))
0153         goto nla_put_failure;
0154 
0155     if (tcf_bpf_is_ebpf(prog))
0156         ret = tcf_bpf_dump_ebpf_info(prog, skb);
0157     else
0158         ret = tcf_bpf_dump_bpf_info(prog, skb);
0159     if (ret)
0160         goto nla_put_failure;
0161 
0162     tcf_tm_dump(&tm, &prog->tcf_tm);
0163     if (nla_put_64bit(skb, TCA_ACT_BPF_TM, sizeof(tm), &tm,
0164               TCA_ACT_BPF_PAD))
0165         goto nla_put_failure;
0166 
0167     spin_unlock_bh(&prog->tcf_lock);
0168     return skb->len;
0169 
0170 nla_put_failure:
0171     spin_unlock_bh(&prog->tcf_lock);
0172     nlmsg_trim(skb, tp);
0173     return -1;
0174 }
0175 
0176 static const struct nla_policy act_bpf_policy[TCA_ACT_BPF_MAX + 1] = {
0177     [TCA_ACT_BPF_PARMS] = { .len = sizeof(struct tc_act_bpf) },
0178     [TCA_ACT_BPF_FD]    = { .type = NLA_U32 },
0179     [TCA_ACT_BPF_NAME]  = { .type = NLA_NUL_STRING,
0180                     .len = ACT_BPF_NAME_LEN },
0181     [TCA_ACT_BPF_OPS_LEN]   = { .type = NLA_U16 },
0182     [TCA_ACT_BPF_OPS]   = { .type = NLA_BINARY,
0183                     .len = sizeof(struct sock_filter) * BPF_MAXINSNS },
0184 };
0185 
0186 static int tcf_bpf_init_from_ops(struct nlattr **tb, struct tcf_bpf_cfg *cfg)
0187 {
0188     struct sock_filter *bpf_ops;
0189     struct sock_fprog_kern fprog_tmp;
0190     struct bpf_prog *fp;
0191     u16 bpf_size, bpf_num_ops;
0192     int ret;
0193 
0194     bpf_num_ops = nla_get_u16(tb[TCA_ACT_BPF_OPS_LEN]);
0195     if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0)
0196         return -EINVAL;
0197 
0198     bpf_size = bpf_num_ops * sizeof(*bpf_ops);
0199     if (bpf_size != nla_len(tb[TCA_ACT_BPF_OPS]))
0200         return -EINVAL;
0201 
0202     bpf_ops = kmemdup(nla_data(tb[TCA_ACT_BPF_OPS]), bpf_size, GFP_KERNEL);
0203     if (bpf_ops == NULL)
0204         return -ENOMEM;
0205 
0206     fprog_tmp.len = bpf_num_ops;
0207     fprog_tmp.filter = bpf_ops;
0208 
0209     ret = bpf_prog_create(&fp, &fprog_tmp);
0210     if (ret < 0) {
0211         kfree(bpf_ops);
0212         return ret;
0213     }
0214 
0215     cfg->bpf_ops = bpf_ops;
0216     cfg->bpf_num_ops = bpf_num_ops;
0217     cfg->filter = fp;
0218     cfg->is_ebpf = false;
0219 
0220     return 0;
0221 }
0222 
0223 static int tcf_bpf_init_from_efd(struct nlattr **tb, struct tcf_bpf_cfg *cfg)
0224 {
0225     struct bpf_prog *fp;
0226     char *name = NULL;
0227     u32 bpf_fd;
0228 
0229     bpf_fd = nla_get_u32(tb[TCA_ACT_BPF_FD]);
0230 
0231     fp = bpf_prog_get_type(bpf_fd, BPF_PROG_TYPE_SCHED_ACT);
0232     if (IS_ERR(fp))
0233         return PTR_ERR(fp);
0234 
0235     if (tb[TCA_ACT_BPF_NAME]) {
0236         name = nla_memdup(tb[TCA_ACT_BPF_NAME], GFP_KERNEL);
0237         if (!name) {
0238             bpf_prog_put(fp);
0239             return -ENOMEM;
0240         }
0241     }
0242 
0243     cfg->bpf_name = name;
0244     cfg->filter = fp;
0245     cfg->is_ebpf = true;
0246 
0247     return 0;
0248 }
0249 
0250 static void tcf_bpf_cfg_cleanup(const struct tcf_bpf_cfg *cfg)
0251 {
0252     struct bpf_prog *filter = cfg->filter;
0253 
0254     if (filter) {
0255         if (cfg->is_ebpf)
0256             bpf_prog_put(filter);
0257         else
0258             bpf_prog_destroy(filter);
0259     }
0260 
0261     kfree(cfg->bpf_ops);
0262     kfree(cfg->bpf_name);
0263 }
0264 
0265 static void tcf_bpf_prog_fill_cfg(const struct tcf_bpf *prog,
0266                   struct tcf_bpf_cfg *cfg)
0267 {
0268     cfg->is_ebpf = tcf_bpf_is_ebpf(prog);
0269     /* updates to prog->filter are prevented, since it's called either
0270      * with tcf lock or during final cleanup in rcu callback
0271      */
0272     cfg->filter = rcu_dereference_protected(prog->filter, 1);
0273 
0274     cfg->bpf_ops = prog->bpf_ops;
0275     cfg->bpf_name = prog->bpf_name;
0276 }
0277 
0278 static int tcf_bpf_init(struct net *net, struct nlattr *nla,
0279             struct nlattr *est, struct tc_action **act,
0280             struct tcf_proto *tp, u32 flags,
0281             struct netlink_ext_ack *extack)
0282 {
0283     struct tc_action_net *tn = net_generic(net, bpf_net_id);
0284     bool bind = flags & TCA_ACT_FLAGS_BIND;
0285     struct nlattr *tb[TCA_ACT_BPF_MAX + 1];
0286     struct tcf_chain *goto_ch = NULL;
0287     struct tcf_bpf_cfg cfg, old;
0288     struct tc_act_bpf *parm;
0289     struct tcf_bpf *prog;
0290     bool is_bpf, is_ebpf;
0291     int ret, res = 0;
0292     u32 index;
0293 
0294     if (!nla)
0295         return -EINVAL;
0296 
0297     ret = nla_parse_nested_deprecated(tb, TCA_ACT_BPF_MAX, nla,
0298                       act_bpf_policy, NULL);
0299     if (ret < 0)
0300         return ret;
0301 
0302     if (!tb[TCA_ACT_BPF_PARMS])
0303         return -EINVAL;
0304 
0305     parm = nla_data(tb[TCA_ACT_BPF_PARMS]);
0306     index = parm->index;
0307     ret = tcf_idr_check_alloc(tn, &index, act, bind);
0308     if (!ret) {
0309         ret = tcf_idr_create(tn, index, est, act,
0310                      &act_bpf_ops, bind, true, flags);
0311         if (ret < 0) {
0312             tcf_idr_cleanup(tn, index);
0313             return ret;
0314         }
0315 
0316         res = ACT_P_CREATED;
0317     } else if (ret > 0) {
0318         /* Don't override defaults. */
0319         if (bind)
0320             return 0;
0321 
0322         if (!(flags & TCA_ACT_FLAGS_REPLACE)) {
0323             tcf_idr_release(*act, bind);
0324             return -EEXIST;
0325         }
0326     } else {
0327         return ret;
0328     }
0329 
0330     ret = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
0331     if (ret < 0)
0332         goto release_idr;
0333 
0334     is_bpf = tb[TCA_ACT_BPF_OPS_LEN] && tb[TCA_ACT_BPF_OPS];
0335     is_ebpf = tb[TCA_ACT_BPF_FD];
0336 
0337     if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf)) {
0338         ret = -EINVAL;
0339         goto put_chain;
0340     }
0341 
0342     memset(&cfg, 0, sizeof(cfg));
0343 
0344     ret = is_bpf ? tcf_bpf_init_from_ops(tb, &cfg) :
0345                tcf_bpf_init_from_efd(tb, &cfg);
0346     if (ret < 0)
0347         goto put_chain;
0348 
0349     prog = to_bpf(*act);
0350 
0351     spin_lock_bh(&prog->tcf_lock);
0352     if (res != ACT_P_CREATED)
0353         tcf_bpf_prog_fill_cfg(prog, &old);
0354 
0355     prog->bpf_ops = cfg.bpf_ops;
0356     prog->bpf_name = cfg.bpf_name;
0357 
0358     if (cfg.bpf_num_ops)
0359         prog->bpf_num_ops = cfg.bpf_num_ops;
0360 
0361     goto_ch = tcf_action_set_ctrlact(*act, parm->action, goto_ch);
0362     rcu_assign_pointer(prog->filter, cfg.filter);
0363     spin_unlock_bh(&prog->tcf_lock);
0364 
0365     if (goto_ch)
0366         tcf_chain_put_by_act(goto_ch);
0367 
0368     if (res != ACT_P_CREATED) {
0369         /* make sure the program being replaced is no longer executing */
0370         synchronize_rcu();
0371         tcf_bpf_cfg_cleanup(&old);
0372     }
0373 
0374     return res;
0375 
0376 put_chain:
0377     if (goto_ch)
0378         tcf_chain_put_by_act(goto_ch);
0379 
0380 release_idr:
0381     tcf_idr_release(*act, bind);
0382     return ret;
0383 }
0384 
0385 static void tcf_bpf_cleanup(struct tc_action *act)
0386 {
0387     struct tcf_bpf_cfg tmp;
0388 
0389     tcf_bpf_prog_fill_cfg(to_bpf(act), &tmp);
0390     tcf_bpf_cfg_cleanup(&tmp);
0391 }
0392 
0393 static int tcf_bpf_walker(struct net *net, struct sk_buff *skb,
0394               struct netlink_callback *cb, int type,
0395               const struct tc_action_ops *ops,
0396               struct netlink_ext_ack *extack)
0397 {
0398     struct tc_action_net *tn = net_generic(net, bpf_net_id);
0399 
0400     return tcf_generic_walker(tn, skb, cb, type, ops, extack);
0401 }
0402 
0403 static int tcf_bpf_search(struct net *net, struct tc_action **a, u32 index)
0404 {
0405     struct tc_action_net *tn = net_generic(net, bpf_net_id);
0406 
0407     return tcf_idr_search(tn, a, index);
0408 }
0409 
0410 static struct tc_action_ops act_bpf_ops __read_mostly = {
0411     .kind       =   "bpf",
0412     .id     =   TCA_ID_BPF,
0413     .owner      =   THIS_MODULE,
0414     .act        =   tcf_bpf_act,
0415     .dump       =   tcf_bpf_dump,
0416     .cleanup    =   tcf_bpf_cleanup,
0417     .init       =   tcf_bpf_init,
0418     .walk       =   tcf_bpf_walker,
0419     .lookup     =   tcf_bpf_search,
0420     .size       =   sizeof(struct tcf_bpf),
0421 };
0422 
0423 static __net_init int bpf_init_net(struct net *net)
0424 {
0425     struct tc_action_net *tn = net_generic(net, bpf_net_id);
0426 
0427     return tc_action_net_init(net, tn, &act_bpf_ops);
0428 }
0429 
0430 static void __net_exit bpf_exit_net(struct list_head *net_list)
0431 {
0432     tc_action_net_exit(net_list, bpf_net_id);
0433 }
0434 
0435 static struct pernet_operations bpf_net_ops = {
0436     .init = bpf_init_net,
0437     .exit_batch = bpf_exit_net,
0438     .id   = &bpf_net_id,
0439     .size = sizeof(struct tc_action_net),
0440 };
0441 
0442 static int __init bpf_init_module(void)
0443 {
0444     return tcf_register_action(&act_bpf_ops, &bpf_net_ops);
0445 }
0446 
0447 static void __exit bpf_cleanup_module(void)
0448 {
0449     tcf_unregister_action(&act_bpf_ops, &bpf_net_ops);
0450 }
0451 
0452 module_init(bpf_init_module);
0453 module_exit(bpf_cleanup_module);
0454 
0455 MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
0456 MODULE_DESCRIPTION("TC BPF based action");
0457 MODULE_LICENSE("GPL v2");