1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * net/sched/act_simple.c Simple example of an action 4 * 5 * Authors: Jamal Hadi Salim (2005-8) 6 */ 7 8 #include <linux/module.h> 9 #include <linux/slab.h> 10 #include <linux/init.h> 11 #include <linux/kernel.h> 12 #include <linux/skbuff.h> 13 #include <linux/rtnetlink.h> 14 #include <net/netlink.h> 15 #include <net/pkt_sched.h> 16 #include <net/pkt_cls.h> 17 #include <net/tc_wrapper.h> 18 19 #include <linux/tc_act/tc_defact.h> 20 #include <net/tc_act/tc_defact.h> 21 22 static struct tc_action_ops act_simp_ops; 23 24 #define SIMP_MAX_DATA 32 25 TC_INDIRECT_SCOPE int tcf_simp_act(struct sk_buff *skb, 26 const struct tc_action *a, 27 struct tcf_result *res) 28 { 29 struct tcf_defact *d = to_defact(a); 30 31 spin_lock(&d->tcf_lock); 32 tcf_lastuse_update(&d->tcf_tm); 33 bstats_update(&d->tcf_bstats, skb); 34 35 /* print policy string followed by _ then packet count 36 * Example if this was the 3rd packet and the string was "hello" 37 * then it would look like "hello_3" (without quotes) 38 */ 39 pr_info("simple: %s_%llu\n", 40 (char *)d->tcfd_defdata, 41 u64_stats_read(&d->tcf_bstats.packets)); 42 spin_unlock(&d->tcf_lock); 43 return d->tcf_action; 44 } 45 46 static void tcf_simp_release(struct tc_action *a) 47 { 48 struct tcf_defact *d = to_defact(a); 49 kfree(d->tcfd_defdata); 50 } 51 52 static int alloc_defdata(struct tcf_defact *d, const struct nlattr *defdata) 53 { 54 d->tcfd_defdata = kzalloc(SIMP_MAX_DATA, GFP_KERNEL); 55 if (unlikely(!d->tcfd_defdata)) 56 return -ENOMEM; 57 nla_strscpy(d->tcfd_defdata, defdata, SIMP_MAX_DATA); 58 return 0; 59 } 60 61 static int reset_policy(struct tc_action *a, const struct nlattr *defdata, 62 struct tc_defact *p, struct tcf_proto *tp, 63 struct netlink_ext_ack *extack) 64 { 65 struct tcf_chain *goto_ch = NULL; 66 struct tcf_defact *d; 67 int err; 68 69 err = tcf_action_check_ctrlact(p->action, tp, &goto_ch, extack); 70 if (err < 0) 71 return err; 72 d = to_defact(a); 73 spin_lock_bh(&d->tcf_lock); 74 goto_ch = tcf_action_set_ctrlact(a, p->action, goto_ch); 75 nla_strscpy(d->tcfd_defdata, defdata, SIMP_MAX_DATA); 76 spin_unlock_bh(&d->tcf_lock); 77 if (goto_ch) 78 tcf_chain_put_by_act(goto_ch); 79 return 0; 80 } 81 82 static size_t tcf_simp_get_fill_size(const struct tc_action *act) 83 { 84 return nla_total_size(sizeof(struct tc_defact)) /* TCA_DEF_PARMS */ 85 + nla_total_size(SIMP_MAX_DATA); /* TCA_DEF_DATA */ 86 } 87 88 static const struct nla_policy simple_policy[TCA_DEF_MAX + 1] = { 89 [TCA_DEF_PARMS] = { .len = sizeof(struct tc_defact) }, 90 [TCA_DEF_DATA] = { .type = NLA_STRING, .len = SIMP_MAX_DATA }, 91 }; 92 93 static int tcf_simp_init(struct net *net, struct nlattr *nla, 94 struct nlattr *est, struct tc_action **a, 95 struct tcf_proto *tp, u32 flags, 96 struct netlink_ext_ack *extack) 97 { 98 struct tc_action_net *tn = net_generic(net, act_simp_ops.net_id); 99 bool bind = flags & TCA_ACT_FLAGS_BIND; 100 struct nlattr *tb[TCA_DEF_MAX + 1]; 101 struct tcf_chain *goto_ch = NULL; 102 struct tc_defact *parm; 103 struct tcf_defact *d; 104 bool exists = false; 105 int ret = 0, err; 106 u32 index; 107 108 if (nla == NULL) 109 return -EINVAL; 110 111 err = nla_parse_nested_deprecated(tb, TCA_DEF_MAX, nla, simple_policy, 112 NULL); 113 if (err < 0) 114 return err; 115 116 if (tb[TCA_DEF_PARMS] == NULL) 117 return -EINVAL; 118 119 parm = nla_data(tb[TCA_DEF_PARMS]); 120 index = parm->index; 121 err = tcf_idr_check_alloc(tn, &index, a, bind); 122 if (err < 0) 123 return err; 124 exists = err; 125 if (exists && bind) 126 return ACT_P_BOUND; 127 128 if (tb[TCA_DEF_DATA] == NULL) { 129 if (exists) 130 tcf_idr_release(*a, bind); 131 else 132 tcf_idr_cleanup(tn, index); 133 return -EINVAL; 134 } 135 136 if (!exists) { 137 ret = tcf_idr_create(tn, index, est, a, 138 &act_simp_ops, bind, false, flags); 139 if (ret) { 140 tcf_idr_cleanup(tn, index); 141 return ret; 142 } 143 144 d = to_defact(*a); 145 err = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, 146 extack); 147 if (err < 0) 148 goto release_idr; 149 150 err = alloc_defdata(d, tb[TCA_DEF_DATA]); 151 if (err < 0) 152 goto put_chain; 153 154 tcf_action_set_ctrlact(*a, parm->action, goto_ch); 155 ret = ACT_P_CREATED; 156 } else { 157 if (!(flags & TCA_ACT_FLAGS_REPLACE)) { 158 err = -EEXIST; 159 goto release_idr; 160 } 161 162 err = reset_policy(*a, tb[TCA_DEF_DATA], parm, tp, extack); 163 if (err) 164 goto release_idr; 165 } 166 167 return ret; 168 put_chain: 169 if (goto_ch) 170 tcf_chain_put_by_act(goto_ch); 171 release_idr: 172 tcf_idr_release(*a, bind); 173 return err; 174 } 175 176 static int tcf_simp_dump(struct sk_buff *skb, struct tc_action *a, 177 int bind, int ref) 178 { 179 unsigned char *b = skb_tail_pointer(skb); 180 struct tcf_defact *d = to_defact(a); 181 struct tc_defact opt = { 182 .index = d->tcf_index, 183 .refcnt = refcount_read(&d->tcf_refcnt) - ref, 184 .bindcnt = atomic_read(&d->tcf_bindcnt) - bind, 185 }; 186 struct tcf_t t; 187 188 spin_lock_bh(&d->tcf_lock); 189 opt.action = d->tcf_action; 190 if (nla_put(skb, TCA_DEF_PARMS, sizeof(opt), &opt) || 191 nla_put_string(skb, TCA_DEF_DATA, d->tcfd_defdata)) 192 goto nla_put_failure; 193 194 tcf_tm_dump(&t, &d->tcf_tm); 195 if (nla_put_64bit(skb, TCA_DEF_TM, sizeof(t), &t, TCA_DEF_PAD)) 196 goto nla_put_failure; 197 spin_unlock_bh(&d->tcf_lock); 198 199 return skb->len; 200 201 nla_put_failure: 202 spin_unlock_bh(&d->tcf_lock); 203 nlmsg_trim(skb, b); 204 return -1; 205 } 206 207 static struct tc_action_ops act_simp_ops = { 208 .kind = "simple", 209 .id = TCA_ID_SIMP, 210 .owner = THIS_MODULE, 211 .act = tcf_simp_act, 212 .dump = tcf_simp_dump, 213 .get_fill_size = tcf_simp_get_fill_size, 214 .cleanup = tcf_simp_release, 215 .init = tcf_simp_init, 216 .size = sizeof(struct tcf_defact), 217 }; 218 MODULE_ALIAS_NET_ACT("simple"); 219 220 static __net_init int simp_init_net(struct net *net) 221 { 222 struct tc_action_net *tn = net_generic(net, act_simp_ops.net_id); 223 224 return tc_action_net_init(net, tn, &act_simp_ops); 225 } 226 227 static void __net_exit simp_exit_net(struct list_head *net_list) 228 { 229 tc_action_net_exit(net_list, act_simp_ops.net_id); 230 } 231 232 static struct pernet_operations simp_net_ops = { 233 .init = simp_init_net, 234 .exit_batch = simp_exit_net, 235 .id = &act_simp_ops.net_id, 236 .size = sizeof(struct tc_action_net), 237 }; 238 239 MODULE_AUTHOR("Jamal Hadi Salim(2005)"); 240 MODULE_DESCRIPTION("Simple example action"); 241 MODULE_LICENSE("GPL"); 242 243 static int __init simp_init_module(void) 244 { 245 int ret = tcf_register_action(&act_simp_ops, &simp_net_ops); 246 if (!ret) 247 pr_info("Simple TC action Loaded\n"); 248 return ret; 249 } 250 251 static void __exit simp_cleanup_module(void) 252 { 253 tcf_unregister_action(&act_simp_ops, &simp_net_ops); 254 } 255 256 module_init(simp_init_module); 257 module_exit(simp_cleanup_module); 258