Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Berkeley Packet Filter based traffic classifier |
| 3 | * |
| 4 | * Might be used to classify traffic through flexible, user-defined and |
| 5 | * possibly JIT-ed BPF filters for traffic control as an alternative to |
| 6 | * ematches. |
| 7 | * |
| 8 | * (C) 2013 Daniel Borkmann <dborkman@redhat.com> |
| 9 | * |
| 10 | * This program is free software; you can redistribute it and/or modify |
| 11 | * it under the terms of the GNU General Public License version 2 as |
| 12 | * published by the Free Software Foundation. |
| 13 | */ |
| 14 | |
| 15 | #include <linux/module.h> |
| 16 | #include <linux/types.h> |
| 17 | #include <linux/skbuff.h> |
| 18 | #include <linux/filter.h> |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 19 | #include <linux/bpf.h> |
| 20 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 21 | #include <net/rtnetlink.h> |
| 22 | #include <net/pkt_cls.h> |
| 23 | #include <net/sock.h> |
| 24 | |
| 25 | MODULE_LICENSE("GPL"); |
| 26 | MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>"); |
| 27 | MODULE_DESCRIPTION("TC BPF based classifier"); |
| 28 | |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 29 | #define CLS_BPF_NAME_LEN 256 |
| 30 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 31 | struct cls_bpf_head { |
| 32 | struct list_head plist; |
| 33 | u32 hgen; |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 34 | struct rcu_head rcu; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 35 | }; |
| 36 | |
| 37 | struct cls_bpf_prog { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 38 | struct bpf_prog *filter; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 39 | struct list_head link; |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 40 | struct tcf_result res; |
| 41 | struct tcf_exts exts; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 42 | u32 handle; |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 43 | union { |
| 44 | u32 bpf_fd; |
| 45 | u16 bpf_num_ops; |
| 46 | }; |
| 47 | struct sock_filter *bpf_ops; |
| 48 | const char *bpf_name; |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 49 | struct tcf_proto *tp; |
| 50 | struct rcu_head rcu; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 51 | }; |
| 52 | |
| 53 | static const struct nla_policy bpf_policy[TCA_BPF_MAX + 1] = { |
| 54 | [TCA_BPF_CLASSID] = { .type = NLA_U32 }, |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 55 | [TCA_BPF_FD] = { .type = NLA_U32 }, |
| 56 | [TCA_BPF_NAME] = { .type = NLA_NUL_STRING, .len = CLS_BPF_NAME_LEN }, |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 57 | [TCA_BPF_OPS_LEN] = { .type = NLA_U16 }, |
| 58 | [TCA_BPF_OPS] = { .type = NLA_BINARY, |
| 59 | .len = sizeof(struct sock_filter) * BPF_MAXINSNS }, |
| 60 | }; |
| 61 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 62 | static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
| 63 | struct tcf_result *res) |
| 64 | { |
WANG Cong | 80dcbd1 | 2014-09-15 14:21:50 -0700 | [diff] [blame] | 65 | struct cls_bpf_head *head = rcu_dereference_bh(tp->root); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 66 | struct cls_bpf_prog *prog; |
| 67 | int ret; |
| 68 | |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 69 | list_for_each_entry_rcu(prog, &head->plist, link) { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 70 | int filter_res = BPF_PROG_RUN(prog->filter, skb); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 71 | |
| 72 | if (filter_res == 0) |
| 73 | continue; |
| 74 | |
| 75 | *res = prog->res; |
| 76 | if (filter_res != -1) |
| 77 | res->classid = filter_res; |
| 78 | |
| 79 | ret = tcf_exts_exec(skb, &prog->exts, res); |
| 80 | if (ret < 0) |
| 81 | continue; |
| 82 | |
| 83 | return ret; |
| 84 | } |
| 85 | |
| 86 | return -1; |
| 87 | } |
| 88 | |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 89 | static bool cls_bpf_is_ebpf(const struct cls_bpf_prog *prog) |
| 90 | { |
| 91 | return !prog->bpf_ops; |
| 92 | } |
| 93 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 94 | static int cls_bpf_init(struct tcf_proto *tp) |
| 95 | { |
| 96 | struct cls_bpf_head *head; |
| 97 | |
| 98 | head = kzalloc(sizeof(*head), GFP_KERNEL); |
| 99 | if (head == NULL) |
| 100 | return -ENOBUFS; |
| 101 | |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 102 | INIT_LIST_HEAD_RCU(&head->plist); |
| 103 | rcu_assign_pointer(tp->root, head); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 104 | |
| 105 | return 0; |
| 106 | } |
| 107 | |
| 108 | static void cls_bpf_delete_prog(struct tcf_proto *tp, struct cls_bpf_prog *prog) |
| 109 | { |
WANG Cong | 18d0264 | 2014-09-25 10:26:37 -0700 | [diff] [blame] | 110 | tcf_exts_destroy(&prog->exts); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 111 | |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 112 | if (cls_bpf_is_ebpf(prog)) |
| 113 | bpf_prog_put(prog->filter); |
| 114 | else |
| 115 | bpf_prog_destroy(prog->filter); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 116 | |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 117 | kfree(prog->bpf_name); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 118 | kfree(prog->bpf_ops); |
| 119 | kfree(prog); |
| 120 | } |
| 121 | |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 122 | static void __cls_bpf_delete_prog(struct rcu_head *rcu) |
| 123 | { |
| 124 | struct cls_bpf_prog *prog = container_of(rcu, struct cls_bpf_prog, rcu); |
| 125 | |
| 126 | cls_bpf_delete_prog(prog->tp, prog); |
| 127 | } |
| 128 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 129 | static int cls_bpf_delete(struct tcf_proto *tp, unsigned long arg) |
| 130 | { |
Jiri Pirko | 472f583 | 2014-12-02 18:00:32 +0100 | [diff] [blame] | 131 | struct cls_bpf_prog *prog = (struct cls_bpf_prog *) arg; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 132 | |
Jiri Pirko | 472f583 | 2014-12-02 18:00:32 +0100 | [diff] [blame] | 133 | list_del_rcu(&prog->link); |
| 134 | tcf_unbind_filter(tp, &prog->res); |
| 135 | call_rcu(&prog->rcu, __cls_bpf_delete_prog); |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 136 | |
Jiri Pirko | 472f583 | 2014-12-02 18:00:32 +0100 | [diff] [blame] | 137 | return 0; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 138 | } |
| 139 | |
Cong Wang | 1e052be | 2015-03-06 11:47:59 -0800 | [diff] [blame^] | 140 | static bool cls_bpf_destroy(struct tcf_proto *tp, bool force) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 141 | { |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 142 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 143 | struct cls_bpf_prog *prog, *tmp; |
| 144 | |
Cong Wang | 1e052be | 2015-03-06 11:47:59 -0800 | [diff] [blame^] | 145 | if (!force && !list_empty(&head->plist)) |
| 146 | return false; |
| 147 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 148 | list_for_each_entry_safe(prog, tmp, &head->plist, link) { |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 149 | list_del_rcu(&prog->link); |
John Fastabend | 18cdb37 | 2014-10-05 21:28:52 -0700 | [diff] [blame] | 150 | tcf_unbind_filter(tp, &prog->res); |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 151 | call_rcu(&prog->rcu, __cls_bpf_delete_prog); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 152 | } |
| 153 | |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 154 | RCU_INIT_POINTER(tp->root, NULL); |
| 155 | kfree_rcu(head, rcu); |
Cong Wang | 1e052be | 2015-03-06 11:47:59 -0800 | [diff] [blame^] | 156 | return true; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | static unsigned long cls_bpf_get(struct tcf_proto *tp, u32 handle) |
| 160 | { |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 161 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 162 | struct cls_bpf_prog *prog; |
| 163 | unsigned long ret = 0UL; |
| 164 | |
| 165 | if (head == NULL) |
| 166 | return 0UL; |
| 167 | |
Jiri Pirko | 3fe6b49 | 2014-12-02 18:00:33 +0100 | [diff] [blame] | 168 | list_for_each_entry(prog, &head->plist, link) { |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 169 | if (prog->handle == handle) { |
| 170 | ret = (unsigned long) prog; |
| 171 | break; |
| 172 | } |
| 173 | } |
| 174 | |
| 175 | return ret; |
| 176 | } |
| 177 | |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 178 | static int cls_bpf_prog_from_ops(struct nlattr **tb, |
| 179 | struct cls_bpf_prog *prog, u32 classid) |
| 180 | { |
| 181 | struct sock_filter *bpf_ops; |
| 182 | struct sock_fprog_kern fprog_tmp; |
| 183 | struct bpf_prog *fp; |
| 184 | u16 bpf_size, bpf_num_ops; |
| 185 | int ret; |
| 186 | |
| 187 | bpf_num_ops = nla_get_u16(tb[TCA_BPF_OPS_LEN]); |
| 188 | if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0) |
| 189 | return -EINVAL; |
| 190 | |
| 191 | bpf_size = bpf_num_ops * sizeof(*bpf_ops); |
| 192 | if (bpf_size != nla_len(tb[TCA_BPF_OPS])) |
| 193 | return -EINVAL; |
| 194 | |
| 195 | bpf_ops = kzalloc(bpf_size, GFP_KERNEL); |
| 196 | if (bpf_ops == NULL) |
| 197 | return -ENOMEM; |
| 198 | |
| 199 | memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size); |
| 200 | |
| 201 | fprog_tmp.len = bpf_num_ops; |
| 202 | fprog_tmp.filter = bpf_ops; |
| 203 | |
| 204 | ret = bpf_prog_create(&fp, &fprog_tmp); |
| 205 | if (ret < 0) { |
| 206 | kfree(bpf_ops); |
| 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | prog->bpf_ops = bpf_ops; |
| 211 | prog->bpf_num_ops = bpf_num_ops; |
| 212 | prog->bpf_name = NULL; |
| 213 | |
| 214 | prog->filter = fp; |
| 215 | prog->res.classid = classid; |
| 216 | |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | static int cls_bpf_prog_from_efd(struct nlattr **tb, |
| 221 | struct cls_bpf_prog *prog, u32 classid) |
| 222 | { |
| 223 | struct bpf_prog *fp; |
| 224 | char *name = NULL; |
| 225 | u32 bpf_fd; |
| 226 | |
| 227 | bpf_fd = nla_get_u32(tb[TCA_BPF_FD]); |
| 228 | |
| 229 | fp = bpf_prog_get(bpf_fd); |
| 230 | if (IS_ERR(fp)) |
| 231 | return PTR_ERR(fp); |
| 232 | |
| 233 | if (fp->type != BPF_PROG_TYPE_SCHED_CLS) { |
| 234 | bpf_prog_put(fp); |
| 235 | return -EINVAL; |
| 236 | } |
| 237 | |
| 238 | if (tb[TCA_BPF_NAME]) { |
| 239 | name = kmemdup(nla_data(tb[TCA_BPF_NAME]), |
| 240 | nla_len(tb[TCA_BPF_NAME]), |
| 241 | GFP_KERNEL); |
| 242 | if (!name) { |
| 243 | bpf_prog_put(fp); |
| 244 | return -ENOMEM; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | prog->bpf_ops = NULL; |
| 249 | prog->bpf_fd = bpf_fd; |
| 250 | prog->bpf_name = name; |
| 251 | |
| 252 | prog->filter = fp; |
| 253 | prog->res.classid = classid; |
| 254 | |
| 255 | return 0; |
| 256 | } |
| 257 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 258 | static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp, |
| 259 | struct cls_bpf_prog *prog, |
| 260 | unsigned long base, struct nlattr **tb, |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 261 | struct nlattr *est, bool ovr) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 262 | { |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 263 | struct tcf_exts exts; |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 264 | bool is_bpf, is_ebpf; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 265 | u32 classid; |
| 266 | int ret; |
| 267 | |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 268 | is_bpf = tb[TCA_BPF_OPS_LEN] && tb[TCA_BPF_OPS]; |
| 269 | is_ebpf = tb[TCA_BPF_FD]; |
| 270 | |
| 271 | if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf) || |
| 272 | !tb[TCA_BPF_CLASSID]) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 273 | return -EINVAL; |
| 274 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 275 | tcf_exts_init(&exts, TCA_BPF_ACT, TCA_BPF_POLICE); |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 276 | ret = tcf_exts_validate(net, tp, tb, est, &exts, ovr); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 277 | if (ret < 0) |
| 278 | return ret; |
| 279 | |
| 280 | classid = nla_get_u32(tb[TCA_BPF_CLASSID]); |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 281 | |
| 282 | ret = is_bpf ? cls_bpf_prog_from_ops(tb, prog, classid) : |
| 283 | cls_bpf_prog_from_efd(tb, prog, classid); |
| 284 | if (ret < 0) { |
| 285 | tcf_exts_destroy(&exts); |
| 286 | return ret; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 287 | } |
| 288 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 289 | tcf_bind_filter(tp, &prog->res, base); |
| 290 | tcf_exts_change(tp, &prog->exts, &exts); |
| 291 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 292 | return 0; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 293 | } |
| 294 | |
| 295 | static u32 cls_bpf_grab_new_handle(struct tcf_proto *tp, |
| 296 | struct cls_bpf_head *head) |
| 297 | { |
| 298 | unsigned int i = 0x80000000; |
Daniel Borkmann | 3f2ab13 | 2015-01-22 10:41:02 +0100 | [diff] [blame] | 299 | u32 handle; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 300 | |
| 301 | do { |
| 302 | if (++head->hgen == 0x7FFFFFFF) |
| 303 | head->hgen = 1; |
| 304 | } while (--i > 0 && cls_bpf_get(tp, head->hgen)); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 305 | |
Daniel Borkmann | 3f2ab13 | 2015-01-22 10:41:02 +0100 | [diff] [blame] | 306 | if (unlikely(i == 0)) { |
| 307 | pr_err("Insufficient number of handles\n"); |
| 308 | handle = 0; |
| 309 | } else { |
| 310 | handle = head->hgen; |
| 311 | } |
| 312 | |
| 313 | return handle; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | static int cls_bpf_change(struct net *net, struct sk_buff *in_skb, |
| 317 | struct tcf_proto *tp, unsigned long base, |
| 318 | u32 handle, struct nlattr **tca, |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 319 | unsigned long *arg, bool ovr) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 320 | { |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 321 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
| 322 | struct cls_bpf_prog *oldprog = (struct cls_bpf_prog *) *arg; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 323 | struct nlattr *tb[TCA_BPF_MAX + 1]; |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 324 | struct cls_bpf_prog *prog; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 325 | int ret; |
| 326 | |
| 327 | if (tca[TCA_OPTIONS] == NULL) |
| 328 | return -EINVAL; |
| 329 | |
| 330 | ret = nla_parse_nested(tb, TCA_BPF_MAX, tca[TCA_OPTIONS], bpf_policy); |
| 331 | if (ret < 0) |
| 332 | return ret; |
| 333 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 334 | prog = kzalloc(sizeof(*prog), GFP_KERNEL); |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 335 | if (!prog) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 336 | return -ENOBUFS; |
| 337 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 338 | tcf_exts_init(&prog->exts, TCA_BPF_ACT, TCA_BPF_POLICE); |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 339 | |
| 340 | if (oldprog) { |
| 341 | if (handle && oldprog->handle != handle) { |
| 342 | ret = -EINVAL; |
| 343 | goto errout; |
| 344 | } |
| 345 | } |
| 346 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 347 | if (handle == 0) |
| 348 | prog->handle = cls_bpf_grab_new_handle(tp, head); |
| 349 | else |
| 350 | prog->handle = handle; |
| 351 | if (prog->handle == 0) { |
| 352 | ret = -EINVAL; |
| 353 | goto errout; |
| 354 | } |
| 355 | |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 356 | ret = cls_bpf_modify_existing(net, tp, prog, base, tb, tca[TCA_RATE], ovr); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 357 | if (ret < 0) |
| 358 | goto errout; |
| 359 | |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 360 | if (oldprog) { |
| 361 | list_replace_rcu(&prog->link, &oldprog->link); |
John Fastabend | 18cdb37 | 2014-10-05 21:28:52 -0700 | [diff] [blame] | 362 | tcf_unbind_filter(tp, &oldprog->res); |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 363 | call_rcu(&oldprog->rcu, __cls_bpf_delete_prog); |
| 364 | } else { |
| 365 | list_add_rcu(&prog->link, &head->plist); |
| 366 | } |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 367 | |
| 368 | *arg = (unsigned long) prog; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 369 | return 0; |
| 370 | errout: |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 371 | kfree(prog); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 372 | |
| 373 | return ret; |
| 374 | } |
| 375 | |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 376 | static int cls_bpf_dump_bpf_info(const struct cls_bpf_prog *prog, |
| 377 | struct sk_buff *skb) |
| 378 | { |
| 379 | struct nlattr *nla; |
| 380 | |
| 381 | if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_num_ops)) |
| 382 | return -EMSGSIZE; |
| 383 | |
| 384 | nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_num_ops * |
| 385 | sizeof(struct sock_filter)); |
| 386 | if (nla == NULL) |
| 387 | return -EMSGSIZE; |
| 388 | |
| 389 | memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla)); |
| 390 | |
| 391 | return 0; |
| 392 | } |
| 393 | |
| 394 | static int cls_bpf_dump_ebpf_info(const struct cls_bpf_prog *prog, |
| 395 | struct sk_buff *skb) |
| 396 | { |
| 397 | if (nla_put_u32(skb, TCA_BPF_FD, prog->bpf_fd)) |
| 398 | return -EMSGSIZE; |
| 399 | |
| 400 | if (prog->bpf_name && |
| 401 | nla_put_string(skb, TCA_BPF_NAME, prog->bpf_name)) |
| 402 | return -EMSGSIZE; |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
WANG Cong | 832d1d5 | 2014-01-09 16:14:01 -0800 | [diff] [blame] | 407 | static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, unsigned long fh, |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 408 | struct sk_buff *skb, struct tcmsg *tm) |
| 409 | { |
| 410 | struct cls_bpf_prog *prog = (struct cls_bpf_prog *) fh; |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 411 | struct nlattr *nest; |
| 412 | int ret; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 413 | |
| 414 | if (prog == NULL) |
| 415 | return skb->len; |
| 416 | |
| 417 | tm->tcm_handle = prog->handle; |
| 418 | |
| 419 | nest = nla_nest_start(skb, TCA_OPTIONS); |
| 420 | if (nest == NULL) |
| 421 | goto nla_put_failure; |
| 422 | |
| 423 | if (nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid)) |
| 424 | goto nla_put_failure; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 425 | |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 426 | if (cls_bpf_is_ebpf(prog)) |
| 427 | ret = cls_bpf_dump_ebpf_info(prog, skb); |
| 428 | else |
| 429 | ret = cls_bpf_dump_bpf_info(prog, skb); |
| 430 | if (ret) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 431 | goto nla_put_failure; |
| 432 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 433 | if (tcf_exts_dump(skb, &prog->exts) < 0) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 434 | goto nla_put_failure; |
| 435 | |
| 436 | nla_nest_end(skb, nest); |
| 437 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 438 | if (tcf_exts_dump_stats(skb, &prog->exts) < 0) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 439 | goto nla_put_failure; |
| 440 | |
| 441 | return skb->len; |
| 442 | |
| 443 | nla_put_failure: |
| 444 | nla_nest_cancel(skb, nest); |
| 445 | return -1; |
| 446 | } |
| 447 | |
| 448 | static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg) |
| 449 | { |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 450 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 451 | struct cls_bpf_prog *prog; |
| 452 | |
Jiri Pirko | 3fe6b49 | 2014-12-02 18:00:33 +0100 | [diff] [blame] | 453 | list_for_each_entry(prog, &head->plist, link) { |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 454 | if (arg->count < arg->skip) |
| 455 | goto skip; |
| 456 | if (arg->fn(tp, (unsigned long) prog, arg) < 0) { |
| 457 | arg->stop = 1; |
| 458 | break; |
| 459 | } |
| 460 | skip: |
| 461 | arg->count++; |
| 462 | } |
| 463 | } |
| 464 | |
| 465 | static struct tcf_proto_ops cls_bpf_ops __read_mostly = { |
| 466 | .kind = "bpf", |
| 467 | .owner = THIS_MODULE, |
| 468 | .classify = cls_bpf_classify, |
| 469 | .init = cls_bpf_init, |
| 470 | .destroy = cls_bpf_destroy, |
| 471 | .get = cls_bpf_get, |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 472 | .change = cls_bpf_change, |
| 473 | .delete = cls_bpf_delete, |
| 474 | .walk = cls_bpf_walk, |
| 475 | .dump = cls_bpf_dump, |
| 476 | }; |
| 477 | |
| 478 | static int __init cls_bpf_init_mod(void) |
| 479 | { |
| 480 | return register_tcf_proto_ops(&cls_bpf_ops); |
| 481 | } |
| 482 | |
| 483 | static void __exit cls_bpf_exit_mod(void) |
| 484 | { |
| 485 | unregister_tcf_proto_ops(&cls_bpf_ops); |
| 486 | } |
| 487 | |
| 488 | module_init(cls_bpf_init_mod); |
| 489 | module_exit(cls_bpf_exit_mod); |