Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
| 11 | #include <linux/init.h> |
| 12 | #include <linux/kernel.h> |
| 13 | #include <linux/skbuff.h> |
| 14 | #include <linux/rtnetlink.h> |
| 15 | #include <linux/filter.h> |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 16 | #include <linux/bpf.h> |
| 17 | |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 18 | #include <net/netlink.h> |
| 19 | #include <net/pkt_sched.h> |
| 20 | |
| 21 | #include <linux/tc_act/tc_bpf.h> |
| 22 | #include <net/tc_act/tc_bpf.h> |
| 23 | |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 24 | #define BPF_TAB_MASK 15 |
| 25 | #define ACT_BPF_NAME_LEN 256 |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 26 | |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 27 | struct tcf_bpf_cfg { |
| 28 | struct bpf_prog *filter; |
| 29 | struct sock_filter *bpf_ops; |
Daniel Borkmann | f4eaed2 | 2015-07-29 18:40:56 +0200 | [diff] [blame] | 30 | const char *bpf_name; |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 31 | u32 bpf_fd; |
| 32 | u16 bpf_num_ops; |
Daniel Borkmann | f4eaed2 | 2015-07-29 18:40:56 +0200 | [diff] [blame] | 33 | bool is_ebpf; |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 34 | }; |
| 35 | |
| 36 | static int tcf_bpf(struct sk_buff *skb, const struct tc_action *act, |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 37 | struct tcf_result *res) |
| 38 | { |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 39 | struct tcf_bpf *prog = act->priv; |
Daniel Borkmann | ced585c | 2015-03-17 20:25:57 +0100 | [diff] [blame] | 40 | int action, filter_res; |
Alexei Starovoitov | 3431205 | 2015-06-04 10:11:53 -0700 | [diff] [blame] | 41 | bool at_ingress = G_TC_AT(skb->tc_verd) & AT_INGRESS; |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 42 | |
Alexei Starovoitov | a166151 | 2015-04-15 12:55:45 -0700 | [diff] [blame] | 43 | if (unlikely(!skb_mac_header_was_set(skb))) |
| 44 | return TC_ACT_UNSPEC; |
| 45 | |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 46 | spin_lock(&prog->tcf_lock); |
Daniel Borkmann | ced585c | 2015-03-17 20:25:57 +0100 | [diff] [blame] | 47 | |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 48 | prog->tcf_tm.lastuse = jiffies; |
| 49 | bstats_update(&prog->tcf_bstats, skb); |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 50 | |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 51 | /* Needed here for accessing maps. */ |
| 52 | rcu_read_lock(); |
Alexei Starovoitov | 3431205 | 2015-06-04 10:11:53 -0700 | [diff] [blame] | 53 | if (at_ingress) { |
| 54 | __skb_push(skb, skb->mac_len); |
| 55 | filter_res = BPF_PROG_RUN(prog->filter, skb); |
| 56 | __skb_pull(skb, skb->mac_len); |
| 57 | } else { |
| 58 | filter_res = BPF_PROG_RUN(prog->filter, skb); |
| 59 | } |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 60 | rcu_read_unlock(); |
Daniel Borkmann | ced585c | 2015-03-17 20:25:57 +0100 | [diff] [blame] | 61 | |
| 62 | /* A BPF program may overwrite the default action opcode. |
| 63 | * Similarly as in cls_bpf, if filter_res == -1 we use the |
| 64 | * default action specified from tc. |
| 65 | * |
| 66 | * In case a different well-known TC_ACT opcode has been |
| 67 | * returned, it will overwrite the default one. |
| 68 | * |
| 69 | * For everything else that is unkown, TC_ACT_UNSPEC is |
| 70 | * returned. |
| 71 | */ |
| 72 | switch (filter_res) { |
| 73 | case TC_ACT_PIPE: |
| 74 | case TC_ACT_RECLASSIFY: |
| 75 | case TC_ACT_OK: |
| 76 | action = filter_res; |
| 77 | break; |
| 78 | case TC_ACT_SHOT: |
| 79 | action = filter_res; |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 80 | prog->tcf_qstats.drops++; |
Daniel Borkmann | ced585c | 2015-03-17 20:25:57 +0100 | [diff] [blame] | 81 | break; |
| 82 | case TC_ACT_UNSPEC: |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 83 | action = prog->tcf_action; |
Daniel Borkmann | ced585c | 2015-03-17 20:25:57 +0100 | [diff] [blame] | 84 | break; |
| 85 | default: |
| 86 | action = TC_ACT_UNSPEC; |
| 87 | break; |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 88 | } |
| 89 | |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 90 | spin_unlock(&prog->tcf_lock); |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 91 | return action; |
| 92 | } |
| 93 | |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 94 | static bool tcf_bpf_is_ebpf(const struct tcf_bpf *prog) |
| 95 | { |
| 96 | return !prog->bpf_ops; |
| 97 | } |
| 98 | |
| 99 | static int tcf_bpf_dump_bpf_info(const struct tcf_bpf *prog, |
| 100 | struct sk_buff *skb) |
| 101 | { |
| 102 | struct nlattr *nla; |
| 103 | |
| 104 | if (nla_put_u16(skb, TCA_ACT_BPF_OPS_LEN, prog->bpf_num_ops)) |
| 105 | return -EMSGSIZE; |
| 106 | |
| 107 | nla = nla_reserve(skb, TCA_ACT_BPF_OPS, prog->bpf_num_ops * |
| 108 | sizeof(struct sock_filter)); |
| 109 | if (nla == NULL) |
| 110 | return -EMSGSIZE; |
| 111 | |
| 112 | memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla)); |
| 113 | |
| 114 | return 0; |
| 115 | } |
| 116 | |
| 117 | static int tcf_bpf_dump_ebpf_info(const struct tcf_bpf *prog, |
| 118 | struct sk_buff *skb) |
| 119 | { |
| 120 | if (nla_put_u32(skb, TCA_ACT_BPF_FD, prog->bpf_fd)) |
| 121 | return -EMSGSIZE; |
| 122 | |
| 123 | if (prog->bpf_name && |
| 124 | nla_put_string(skb, TCA_ACT_BPF_NAME, prog->bpf_name)) |
| 125 | return -EMSGSIZE; |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | static int tcf_bpf_dump(struct sk_buff *skb, struct tc_action *act, |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 131 | int bind, int ref) |
| 132 | { |
| 133 | unsigned char *tp = skb_tail_pointer(skb); |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 134 | struct tcf_bpf *prog = act->priv; |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 135 | struct tc_act_bpf opt = { |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 136 | .index = prog->tcf_index, |
| 137 | .refcnt = prog->tcf_refcnt - ref, |
| 138 | .bindcnt = prog->tcf_bindcnt - bind, |
| 139 | .action = prog->tcf_action, |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 140 | }; |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 141 | struct tcf_t tm; |
| 142 | int ret; |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 143 | |
| 144 | if (nla_put(skb, TCA_ACT_BPF_PARMS, sizeof(opt), &opt)) |
| 145 | goto nla_put_failure; |
| 146 | |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 147 | if (tcf_bpf_is_ebpf(prog)) |
| 148 | ret = tcf_bpf_dump_ebpf_info(prog, skb); |
| 149 | else |
| 150 | ret = tcf_bpf_dump_bpf_info(prog, skb); |
| 151 | if (ret) |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 152 | goto nla_put_failure; |
| 153 | |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 154 | tm.install = jiffies_to_clock_t(jiffies - prog->tcf_tm.install); |
| 155 | tm.lastuse = jiffies_to_clock_t(jiffies - prog->tcf_tm.lastuse); |
| 156 | tm.expires = jiffies_to_clock_t(prog->tcf_tm.expires); |
| 157 | |
| 158 | if (nla_put(skb, TCA_ACT_BPF_TM, sizeof(tm), &tm)) |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 159 | goto nla_put_failure; |
| 160 | |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 161 | return skb->len; |
| 162 | |
| 163 | nla_put_failure: |
| 164 | nlmsg_trim(skb, tp); |
| 165 | return -1; |
| 166 | } |
| 167 | |
| 168 | static const struct nla_policy act_bpf_policy[TCA_ACT_BPF_MAX + 1] = { |
| 169 | [TCA_ACT_BPF_PARMS] = { .len = sizeof(struct tc_act_bpf) }, |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 170 | [TCA_ACT_BPF_FD] = { .type = NLA_U32 }, |
| 171 | [TCA_ACT_BPF_NAME] = { .type = NLA_NUL_STRING, .len = ACT_BPF_NAME_LEN }, |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 172 | [TCA_ACT_BPF_OPS_LEN] = { .type = NLA_U16 }, |
| 173 | [TCA_ACT_BPF_OPS] = { .type = NLA_BINARY, |
| 174 | .len = sizeof(struct sock_filter) * BPF_MAXINSNS }, |
| 175 | }; |
| 176 | |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 177 | static int tcf_bpf_init_from_ops(struct nlattr **tb, struct tcf_bpf_cfg *cfg) |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 178 | { |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 179 | struct sock_filter *bpf_ops; |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 180 | struct sock_fprog_kern fprog_tmp; |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 181 | struct bpf_prog *fp; |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 182 | u16 bpf_size, bpf_num_ops; |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 183 | int ret; |
| 184 | |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 185 | bpf_num_ops = nla_get_u16(tb[TCA_ACT_BPF_OPS_LEN]); |
| 186 | if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0) |
| 187 | return -EINVAL; |
| 188 | |
| 189 | bpf_size = bpf_num_ops * sizeof(*bpf_ops); |
Daniel Borkmann | fd3e646 | 2015-01-22 10:58:19 +0100 | [diff] [blame] | 190 | if (bpf_size != nla_len(tb[TCA_ACT_BPF_OPS])) |
| 191 | return -EINVAL; |
| 192 | |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 193 | bpf_ops = kzalloc(bpf_size, GFP_KERNEL); |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 194 | if (bpf_ops == NULL) |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 195 | return -ENOMEM; |
| 196 | |
| 197 | memcpy(bpf_ops, nla_data(tb[TCA_ACT_BPF_OPS]), bpf_size); |
| 198 | |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 199 | fprog_tmp.len = bpf_num_ops; |
| 200 | fprog_tmp.filter = bpf_ops; |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 201 | |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 202 | ret = bpf_prog_create(&fp, &fprog_tmp); |
| 203 | if (ret < 0) { |
| 204 | kfree(bpf_ops); |
| 205 | return ret; |
| 206 | } |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 207 | |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 208 | cfg->bpf_ops = bpf_ops; |
| 209 | cfg->bpf_num_ops = bpf_num_ops; |
| 210 | cfg->filter = fp; |
Daniel Borkmann | f4eaed2 | 2015-07-29 18:40:56 +0200 | [diff] [blame] | 211 | cfg->is_ebpf = false; |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 212 | |
| 213 | return 0; |
| 214 | } |
| 215 | |
| 216 | static int tcf_bpf_init_from_efd(struct nlattr **tb, struct tcf_bpf_cfg *cfg) |
| 217 | { |
| 218 | struct bpf_prog *fp; |
| 219 | char *name = NULL; |
| 220 | u32 bpf_fd; |
| 221 | |
| 222 | bpf_fd = nla_get_u32(tb[TCA_ACT_BPF_FD]); |
| 223 | |
| 224 | fp = bpf_prog_get(bpf_fd); |
| 225 | if (IS_ERR(fp)) |
| 226 | return PTR_ERR(fp); |
| 227 | |
| 228 | if (fp->type != BPF_PROG_TYPE_SCHED_ACT) { |
| 229 | bpf_prog_put(fp); |
| 230 | return -EINVAL; |
| 231 | } |
| 232 | |
| 233 | if (tb[TCA_ACT_BPF_NAME]) { |
| 234 | name = kmemdup(nla_data(tb[TCA_ACT_BPF_NAME]), |
| 235 | nla_len(tb[TCA_ACT_BPF_NAME]), |
| 236 | GFP_KERNEL); |
| 237 | if (!name) { |
| 238 | bpf_prog_put(fp); |
| 239 | return -ENOMEM; |
| 240 | } |
| 241 | } |
| 242 | |
| 243 | cfg->bpf_fd = bpf_fd; |
| 244 | cfg->bpf_name = name; |
| 245 | cfg->filter = fp; |
Daniel Borkmann | f4eaed2 | 2015-07-29 18:40:56 +0200 | [diff] [blame] | 246 | cfg->is_ebpf = true; |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
Daniel Borkmann | f4eaed2 | 2015-07-29 18:40:56 +0200 | [diff] [blame] | 251 | static void tcf_bpf_cfg_cleanup(const struct tcf_bpf_cfg *cfg) |
| 252 | { |
| 253 | if (cfg->is_ebpf) |
| 254 | bpf_prog_put(cfg->filter); |
| 255 | else |
| 256 | bpf_prog_destroy(cfg->filter); |
| 257 | |
| 258 | kfree(cfg->bpf_ops); |
| 259 | kfree(cfg->bpf_name); |
| 260 | } |
| 261 | |
| 262 | static void tcf_bpf_prog_fill_cfg(const struct tcf_bpf *prog, |
| 263 | struct tcf_bpf_cfg *cfg) |
| 264 | { |
| 265 | cfg->is_ebpf = tcf_bpf_is_ebpf(prog); |
| 266 | cfg->filter = prog->filter; |
| 267 | |
| 268 | cfg->bpf_ops = prog->bpf_ops; |
| 269 | cfg->bpf_name = prog->bpf_name; |
| 270 | } |
| 271 | |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 272 | static int tcf_bpf_init(struct net *net, struct nlattr *nla, |
| 273 | struct nlattr *est, struct tc_action *act, |
| 274 | int replace, int bind) |
| 275 | { |
| 276 | struct nlattr *tb[TCA_ACT_BPF_MAX + 1]; |
Daniel Borkmann | f4eaed2 | 2015-07-29 18:40:56 +0200 | [diff] [blame] | 277 | struct tcf_bpf_cfg cfg, old; |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 278 | struct tc_act_bpf *parm; |
| 279 | struct tcf_bpf *prog; |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 280 | bool is_bpf, is_ebpf; |
| 281 | int ret; |
| 282 | |
| 283 | if (!nla) |
| 284 | return -EINVAL; |
| 285 | |
| 286 | ret = nla_parse_nested(tb, TCA_ACT_BPF_MAX, nla, act_bpf_policy); |
| 287 | if (ret < 0) |
| 288 | return ret; |
| 289 | |
| 290 | is_bpf = tb[TCA_ACT_BPF_OPS_LEN] && tb[TCA_ACT_BPF_OPS]; |
| 291 | is_ebpf = tb[TCA_ACT_BPF_FD]; |
| 292 | |
| 293 | if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf) || |
| 294 | !tb[TCA_ACT_BPF_PARMS]) |
| 295 | return -EINVAL; |
| 296 | |
| 297 | parm = nla_data(tb[TCA_ACT_BPF_PARMS]); |
| 298 | |
| 299 | memset(&cfg, 0, sizeof(cfg)); |
| 300 | |
| 301 | ret = is_bpf ? tcf_bpf_init_from_ops(tb, &cfg) : |
| 302 | tcf_bpf_init_from_efd(tb, &cfg); |
| 303 | if (ret < 0) |
| 304 | return ret; |
| 305 | |
| 306 | if (!tcf_hash_check(parm->index, act, bind)) { |
| 307 | ret = tcf_hash_create(parm->index, est, act, |
| 308 | sizeof(*prog), bind); |
| 309 | if (ret < 0) |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 310 | goto destroy_fp; |
| 311 | |
| 312 | ret = ACT_P_CREATED; |
| 313 | } else { |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 314 | /* Don't override defaults. */ |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 315 | if (bind) |
| 316 | goto destroy_fp; |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 317 | |
| 318 | tcf_hash_release(act, bind); |
| 319 | if (!replace) { |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 320 | ret = -EEXIST; |
| 321 | goto destroy_fp; |
| 322 | } |
| 323 | } |
| 324 | |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 325 | prog = to_bpf(act); |
| 326 | spin_lock_bh(&prog->tcf_lock); |
| 327 | |
Daniel Borkmann | f4eaed2 | 2015-07-29 18:40:56 +0200 | [diff] [blame] | 328 | if (ret != ACT_P_CREATED) |
| 329 | tcf_bpf_prog_fill_cfg(prog, &old); |
| 330 | |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 331 | prog->bpf_ops = cfg.bpf_ops; |
| 332 | prog->bpf_name = cfg.bpf_name; |
| 333 | |
| 334 | if (cfg.bpf_num_ops) |
| 335 | prog->bpf_num_ops = cfg.bpf_num_ops; |
| 336 | if (cfg.bpf_fd) |
| 337 | prog->bpf_fd = cfg.bpf_fd; |
| 338 | |
| 339 | prog->tcf_action = parm->action; |
| 340 | prog->filter = cfg.filter; |
| 341 | |
| 342 | spin_unlock_bh(&prog->tcf_lock); |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 343 | |
| 344 | if (ret == ACT_P_CREATED) |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 345 | tcf_hash_insert(act); |
Daniel Borkmann | f4eaed2 | 2015-07-29 18:40:56 +0200 | [diff] [blame] | 346 | else |
| 347 | tcf_bpf_cfg_cleanup(&old); |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 348 | |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 349 | return ret; |
| 350 | |
| 351 | destroy_fp: |
Daniel Borkmann | f4eaed2 | 2015-07-29 18:40:56 +0200 | [diff] [blame] | 352 | tcf_bpf_cfg_cleanup(&cfg); |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 353 | return ret; |
| 354 | } |
| 355 | |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 356 | static void tcf_bpf_cleanup(struct tc_action *act, int bind) |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 357 | { |
Daniel Borkmann | f4eaed2 | 2015-07-29 18:40:56 +0200 | [diff] [blame] | 358 | struct tcf_bpf_cfg tmp; |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 359 | |
Daniel Borkmann | f4eaed2 | 2015-07-29 18:40:56 +0200 | [diff] [blame] | 360 | tcf_bpf_prog_fill_cfg(act->priv, &tmp); |
| 361 | tcf_bpf_cfg_cleanup(&tmp); |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 362 | } |
| 363 | |
Daniel Borkmann | a8cb5f5 | 2015-03-20 15:11:12 +0100 | [diff] [blame] | 364 | static struct tc_action_ops act_bpf_ops __read_mostly = { |
| 365 | .kind = "bpf", |
| 366 | .type = TCA_ACT_BPF, |
| 367 | .owner = THIS_MODULE, |
| 368 | .act = tcf_bpf, |
| 369 | .dump = tcf_bpf_dump, |
| 370 | .cleanup = tcf_bpf_cleanup, |
| 371 | .init = tcf_bpf_init, |
Jiri Pirko | d23b8ad | 2015-01-15 09:52:39 +0100 | [diff] [blame] | 372 | }; |
| 373 | |
| 374 | static int __init bpf_init_module(void) |
| 375 | { |
| 376 | return tcf_register_action(&act_bpf_ops, BPF_TAB_MASK); |
| 377 | } |
| 378 | |
| 379 | static void __exit bpf_cleanup_module(void) |
| 380 | { |
| 381 | tcf_unregister_action(&act_bpf_ops); |
| 382 | } |
| 383 | |
| 384 | module_init(bpf_init_module); |
| 385 | module_exit(bpf_cleanup_module); |
| 386 | |
| 387 | MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>"); |
| 388 | MODULE_DESCRIPTION("TC BPF based action"); |
| 389 | MODULE_LICENSE("GPL v2"); |