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> |
| 19 | #include <net/rtnetlink.h> |
| 20 | #include <net/pkt_cls.h> |
| 21 | #include <net/sock.h> |
| 22 | |
| 23 | MODULE_LICENSE("GPL"); |
| 24 | MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>"); |
| 25 | MODULE_DESCRIPTION("TC BPF based classifier"); |
| 26 | |
| 27 | struct cls_bpf_head { |
| 28 | struct list_head plist; |
| 29 | u32 hgen; |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 30 | struct rcu_head rcu; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 31 | }; |
| 32 | |
| 33 | struct cls_bpf_prog { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 34 | struct bpf_prog *filter; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 35 | struct sock_filter *bpf_ops; |
| 36 | struct tcf_exts exts; |
| 37 | struct tcf_result res; |
| 38 | struct list_head link; |
| 39 | u32 handle; |
Jiri Pirko | 33e9fcc | 2015-01-15 09:52:40 +0100 | [diff] [blame] | 40 | u16 bpf_num_ops; |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 41 | struct tcf_proto *tp; |
| 42 | struct rcu_head rcu; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 43 | }; |
| 44 | |
| 45 | static const struct nla_policy bpf_policy[TCA_BPF_MAX + 1] = { |
| 46 | [TCA_BPF_CLASSID] = { .type = NLA_U32 }, |
| 47 | [TCA_BPF_OPS_LEN] = { .type = NLA_U16 }, |
| 48 | [TCA_BPF_OPS] = { .type = NLA_BINARY, |
| 49 | .len = sizeof(struct sock_filter) * BPF_MAXINSNS }, |
| 50 | }; |
| 51 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 52 | static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
| 53 | struct tcf_result *res) |
| 54 | { |
WANG Cong | 80dcbd1 | 2014-09-15 14:21:50 -0700 | [diff] [blame] | 55 | struct cls_bpf_head *head = rcu_dereference_bh(tp->root); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 56 | struct cls_bpf_prog *prog; |
| 57 | int ret; |
| 58 | |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 59 | list_for_each_entry_rcu(prog, &head->plist, link) { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 60 | int filter_res = BPF_PROG_RUN(prog->filter, skb); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 61 | |
| 62 | if (filter_res == 0) |
| 63 | continue; |
| 64 | |
| 65 | *res = prog->res; |
| 66 | if (filter_res != -1) |
| 67 | res->classid = filter_res; |
| 68 | |
| 69 | ret = tcf_exts_exec(skb, &prog->exts, res); |
| 70 | if (ret < 0) |
| 71 | continue; |
| 72 | |
| 73 | return ret; |
| 74 | } |
| 75 | |
| 76 | return -1; |
| 77 | } |
| 78 | |
| 79 | static int cls_bpf_init(struct tcf_proto *tp) |
| 80 | { |
| 81 | struct cls_bpf_head *head; |
| 82 | |
| 83 | head = kzalloc(sizeof(*head), GFP_KERNEL); |
| 84 | if (head == NULL) |
| 85 | return -ENOBUFS; |
| 86 | |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 87 | INIT_LIST_HEAD_RCU(&head->plist); |
| 88 | rcu_assign_pointer(tp->root, head); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 89 | |
| 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | static void cls_bpf_delete_prog(struct tcf_proto *tp, struct cls_bpf_prog *prog) |
| 94 | { |
WANG Cong | 18d0264 | 2014-09-25 10:26:37 -0700 | [diff] [blame] | 95 | tcf_exts_destroy(&prog->exts); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 96 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 97 | bpf_prog_destroy(prog->filter); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 98 | |
| 99 | kfree(prog->bpf_ops); |
| 100 | kfree(prog); |
| 101 | } |
| 102 | |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 103 | static void __cls_bpf_delete_prog(struct rcu_head *rcu) |
| 104 | { |
| 105 | struct cls_bpf_prog *prog = container_of(rcu, struct cls_bpf_prog, rcu); |
| 106 | |
| 107 | cls_bpf_delete_prog(prog->tp, prog); |
| 108 | } |
| 109 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 110 | static int cls_bpf_delete(struct tcf_proto *tp, unsigned long arg) |
| 111 | { |
Jiri Pirko | 472f583 | 2014-12-02 18:00:32 +0100 | [diff] [blame] | 112 | struct cls_bpf_prog *prog = (struct cls_bpf_prog *) arg; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 113 | |
Jiri Pirko | 472f583 | 2014-12-02 18:00:32 +0100 | [diff] [blame] | 114 | list_del_rcu(&prog->link); |
| 115 | tcf_unbind_filter(tp, &prog->res); |
| 116 | call_rcu(&prog->rcu, __cls_bpf_delete_prog); |
| 117 | return 0; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | static void cls_bpf_destroy(struct tcf_proto *tp) |
| 121 | { |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 122 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 123 | struct cls_bpf_prog *prog, *tmp; |
| 124 | |
| 125 | list_for_each_entry_safe(prog, tmp, &head->plist, link) { |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 126 | list_del_rcu(&prog->link); |
John Fastabend | 18cdb37 | 2014-10-05 21:28:52 -0700 | [diff] [blame] | 127 | tcf_unbind_filter(tp, &prog->res); |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 128 | call_rcu(&prog->rcu, __cls_bpf_delete_prog); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 129 | } |
| 130 | |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 131 | RCU_INIT_POINTER(tp->root, NULL); |
| 132 | kfree_rcu(head, rcu); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | static unsigned long cls_bpf_get(struct tcf_proto *tp, u32 handle) |
| 136 | { |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 137 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 138 | struct cls_bpf_prog *prog; |
| 139 | unsigned long ret = 0UL; |
| 140 | |
| 141 | if (head == NULL) |
| 142 | return 0UL; |
| 143 | |
Jiri Pirko | 3fe6b49 | 2014-12-02 18:00:33 +0100 | [diff] [blame] | 144 | list_for_each_entry(prog, &head->plist, link) { |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 145 | if (prog->handle == handle) { |
| 146 | ret = (unsigned long) prog; |
| 147 | break; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | return ret; |
| 152 | } |
| 153 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 154 | static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp, |
| 155 | struct cls_bpf_prog *prog, |
| 156 | unsigned long base, struct nlattr **tb, |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 157 | struct nlattr *est, bool ovr) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 158 | { |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 159 | struct sock_filter *bpf_ops; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 160 | struct tcf_exts exts; |
Daniel Borkmann | b1fcd35 | 2014-05-23 18:43:58 +0200 | [diff] [blame] | 161 | struct sock_fprog_kern tmp; |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 162 | struct bpf_prog *fp; |
Jiri Pirko | 33e9fcc | 2015-01-15 09:52:40 +0100 | [diff] [blame] | 163 | u16 bpf_size, bpf_num_ops; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 164 | u32 classid; |
| 165 | int ret; |
| 166 | |
| 167 | if (!tb[TCA_BPF_OPS_LEN] || !tb[TCA_BPF_OPS] || !tb[TCA_BPF_CLASSID]) |
| 168 | return -EINVAL; |
| 169 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 170 | tcf_exts_init(&exts, TCA_BPF_ACT, TCA_BPF_POLICE); |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 171 | ret = tcf_exts_validate(net, tp, tb, est, &exts, ovr); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 172 | if (ret < 0) |
| 173 | return ret; |
| 174 | |
| 175 | classid = nla_get_u32(tb[TCA_BPF_CLASSID]); |
Jiri Pirko | 33e9fcc | 2015-01-15 09:52:40 +0100 | [diff] [blame] | 176 | bpf_num_ops = nla_get_u16(tb[TCA_BPF_OPS_LEN]); |
| 177 | if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0) { |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 178 | ret = -EINVAL; |
| 179 | goto errout; |
| 180 | } |
| 181 | |
Jiri Pirko | 33e9fcc | 2015-01-15 09:52:40 +0100 | [diff] [blame] | 182 | bpf_size = bpf_num_ops * sizeof(*bpf_ops); |
Daniel Borkmann | 7913ecf | 2015-01-22 10:41:01 +0100 | [diff] [blame] | 183 | if (bpf_size != nla_len(tb[TCA_BPF_OPS])) { |
| 184 | ret = -EINVAL; |
| 185 | goto errout; |
| 186 | } |
| 187 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 188 | bpf_ops = kzalloc(bpf_size, GFP_KERNEL); |
| 189 | if (bpf_ops == NULL) { |
| 190 | ret = -ENOMEM; |
| 191 | goto errout; |
| 192 | } |
| 193 | |
| 194 | memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size); |
| 195 | |
Jiri Pirko | 33e9fcc | 2015-01-15 09:52:40 +0100 | [diff] [blame] | 196 | tmp.len = bpf_num_ops; |
Daniel Borkmann | b1fcd35 | 2014-05-23 18:43:58 +0200 | [diff] [blame] | 197 | tmp.filter = bpf_ops; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 198 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 199 | ret = bpf_prog_create(&fp, &tmp); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 200 | if (ret) |
| 201 | goto errout_free; |
| 202 | |
Jiri Pirko | 33e9fcc | 2015-01-15 09:52:40 +0100 | [diff] [blame] | 203 | prog->bpf_num_ops = bpf_num_ops; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 204 | prog->bpf_ops = bpf_ops; |
| 205 | prog->filter = fp; |
| 206 | prog->res.classid = classid; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 207 | |
| 208 | tcf_bind_filter(tp, &prog->res, base); |
| 209 | tcf_exts_change(tp, &prog->exts, &exts); |
| 210 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 211 | return 0; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 212 | errout_free: |
| 213 | kfree(bpf_ops); |
| 214 | errout: |
WANG Cong | 18d0264 | 2014-09-25 10:26:37 -0700 | [diff] [blame] | 215 | tcf_exts_destroy(&exts); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 216 | return ret; |
| 217 | } |
| 218 | |
| 219 | static u32 cls_bpf_grab_new_handle(struct tcf_proto *tp, |
| 220 | struct cls_bpf_head *head) |
| 221 | { |
| 222 | unsigned int i = 0x80000000; |
Daniel Borkmann | 3f2ab13 | 2015-01-22 10:41:02 +0100 | [diff] [blame] | 223 | u32 handle; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 224 | |
| 225 | do { |
| 226 | if (++head->hgen == 0x7FFFFFFF) |
| 227 | head->hgen = 1; |
| 228 | } while (--i > 0 && cls_bpf_get(tp, head->hgen)); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 229 | |
Daniel Borkmann | 3f2ab13 | 2015-01-22 10:41:02 +0100 | [diff] [blame] | 230 | if (unlikely(i == 0)) { |
| 231 | pr_err("Insufficient number of handles\n"); |
| 232 | handle = 0; |
| 233 | } else { |
| 234 | handle = head->hgen; |
| 235 | } |
| 236 | |
| 237 | return handle; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static int cls_bpf_change(struct net *net, struct sk_buff *in_skb, |
| 241 | struct tcf_proto *tp, unsigned long base, |
| 242 | u32 handle, struct nlattr **tca, |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 243 | unsigned long *arg, bool ovr) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 244 | { |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 245 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
| 246 | struct cls_bpf_prog *oldprog = (struct cls_bpf_prog *) *arg; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 247 | struct nlattr *tb[TCA_BPF_MAX + 1]; |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 248 | struct cls_bpf_prog *prog; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 249 | int ret; |
| 250 | |
| 251 | if (tca[TCA_OPTIONS] == NULL) |
| 252 | return -EINVAL; |
| 253 | |
| 254 | ret = nla_parse_nested(tb, TCA_BPF_MAX, tca[TCA_OPTIONS], bpf_policy); |
| 255 | if (ret < 0) |
| 256 | return ret; |
| 257 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 258 | prog = kzalloc(sizeof(*prog), GFP_KERNEL); |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 259 | if (!prog) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 260 | return -ENOBUFS; |
| 261 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 262 | tcf_exts_init(&prog->exts, TCA_BPF_ACT, TCA_BPF_POLICE); |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 263 | |
| 264 | if (oldprog) { |
| 265 | if (handle && oldprog->handle != handle) { |
| 266 | ret = -EINVAL; |
| 267 | goto errout; |
| 268 | } |
| 269 | } |
| 270 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 271 | if (handle == 0) |
| 272 | prog->handle = cls_bpf_grab_new_handle(tp, head); |
| 273 | else |
| 274 | prog->handle = handle; |
| 275 | if (prog->handle == 0) { |
| 276 | ret = -EINVAL; |
| 277 | goto errout; |
| 278 | } |
| 279 | |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 280 | 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] | 281 | if (ret < 0) |
| 282 | goto errout; |
| 283 | |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 284 | if (oldprog) { |
| 285 | list_replace_rcu(&prog->link, &oldprog->link); |
John Fastabend | 18cdb37 | 2014-10-05 21:28:52 -0700 | [diff] [blame] | 286 | tcf_unbind_filter(tp, &oldprog->res); |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 287 | call_rcu(&oldprog->rcu, __cls_bpf_delete_prog); |
| 288 | } else { |
| 289 | list_add_rcu(&prog->link, &head->plist); |
| 290 | } |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 291 | |
| 292 | *arg = (unsigned long) prog; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 293 | return 0; |
| 294 | errout: |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 295 | kfree(prog); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 296 | |
| 297 | return ret; |
| 298 | } |
| 299 | |
WANG Cong | 832d1d5 | 2014-01-09 16:14:01 -0800 | [diff] [blame] | 300 | 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] | 301 | struct sk_buff *skb, struct tcmsg *tm) |
| 302 | { |
| 303 | struct cls_bpf_prog *prog = (struct cls_bpf_prog *) fh; |
| 304 | struct nlattr *nest, *nla; |
| 305 | |
| 306 | if (prog == NULL) |
| 307 | return skb->len; |
| 308 | |
| 309 | tm->tcm_handle = prog->handle; |
| 310 | |
| 311 | nest = nla_nest_start(skb, TCA_OPTIONS); |
| 312 | if (nest == NULL) |
| 313 | goto nla_put_failure; |
| 314 | |
| 315 | if (nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid)) |
| 316 | goto nla_put_failure; |
Jiri Pirko | 33e9fcc | 2015-01-15 09:52:40 +0100 | [diff] [blame] | 317 | if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_num_ops)) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 318 | goto nla_put_failure; |
| 319 | |
Jiri Pirko | 33e9fcc | 2015-01-15 09:52:40 +0100 | [diff] [blame] | 320 | nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_num_ops * |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 321 | sizeof(struct sock_filter)); |
| 322 | if (nla == NULL) |
| 323 | goto nla_put_failure; |
| 324 | |
Yang Yingliang | 1fab9ab | 2013-12-10 20:55:30 +0800 | [diff] [blame] | 325 | memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla)); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 326 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 327 | if (tcf_exts_dump(skb, &prog->exts) < 0) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 328 | goto nla_put_failure; |
| 329 | |
| 330 | nla_nest_end(skb, nest); |
| 331 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 332 | if (tcf_exts_dump_stats(skb, &prog->exts) < 0) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 333 | goto nla_put_failure; |
| 334 | |
| 335 | return skb->len; |
| 336 | |
| 337 | nla_put_failure: |
| 338 | nla_nest_cancel(skb, nest); |
| 339 | return -1; |
| 340 | } |
| 341 | |
| 342 | static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg) |
| 343 | { |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 344 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 345 | struct cls_bpf_prog *prog; |
| 346 | |
Jiri Pirko | 3fe6b49 | 2014-12-02 18:00:33 +0100 | [diff] [blame] | 347 | list_for_each_entry(prog, &head->plist, link) { |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 348 | if (arg->count < arg->skip) |
| 349 | goto skip; |
| 350 | if (arg->fn(tp, (unsigned long) prog, arg) < 0) { |
| 351 | arg->stop = 1; |
| 352 | break; |
| 353 | } |
| 354 | skip: |
| 355 | arg->count++; |
| 356 | } |
| 357 | } |
| 358 | |
| 359 | static struct tcf_proto_ops cls_bpf_ops __read_mostly = { |
| 360 | .kind = "bpf", |
| 361 | .owner = THIS_MODULE, |
| 362 | .classify = cls_bpf_classify, |
| 363 | .init = cls_bpf_init, |
| 364 | .destroy = cls_bpf_destroy, |
| 365 | .get = cls_bpf_get, |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 366 | .change = cls_bpf_change, |
| 367 | .delete = cls_bpf_delete, |
| 368 | .walk = cls_bpf_walk, |
| 369 | .dump = cls_bpf_dump, |
| 370 | }; |
| 371 | |
| 372 | static int __init cls_bpf_init_mod(void) |
| 373 | { |
| 374 | return register_tcf_proto_ops(&cls_bpf_ops); |
| 375 | } |
| 376 | |
| 377 | static void __exit cls_bpf_exit_mod(void) |
| 378 | { |
| 379 | unregister_tcf_proto_ops(&cls_bpf_ops); |
| 380 | } |
| 381 | |
| 382 | module_init(cls_bpf_init_mod); |
| 383 | module_exit(cls_bpf_exit_mod); |