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 |
Jakub Kicinski | 0d01d45 | 2016-09-21 11:43:54 +0100 | [diff] [blame] | 30 | #define CLS_BPF_SUPPORTED_GEN_FLAGS \ |
Jakub Kicinski | eadb414 | 2016-09-21 11:43:55 +0100 | [diff] [blame] | 31 | (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW) |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 32 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 33 | struct cls_bpf_head { |
| 34 | struct list_head plist; |
| 35 | u32 hgen; |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 36 | struct rcu_head rcu; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 37 | }; |
| 38 | |
| 39 | struct cls_bpf_prog { |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 40 | struct bpf_prog *filter; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 41 | struct list_head link; |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 42 | struct tcf_result res; |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 43 | bool exts_integrated; |
Jakub Kicinski | 332ae8e | 2016-09-21 11:43:53 +0100 | [diff] [blame] | 44 | bool offloaded; |
Jakub Kicinski | 0d01d45 | 2016-09-21 11:43:54 +0100 | [diff] [blame] | 45 | u32 gen_flags; |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 46 | struct tcf_exts exts; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 47 | u32 handle; |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 48 | union { |
| 49 | u32 bpf_fd; |
| 50 | u16 bpf_num_ops; |
| 51 | }; |
| 52 | struct sock_filter *bpf_ops; |
| 53 | const char *bpf_name; |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 54 | struct tcf_proto *tp; |
| 55 | struct rcu_head rcu; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | static const struct nla_policy bpf_policy[TCA_BPF_MAX + 1] = { |
| 59 | [TCA_BPF_CLASSID] = { .type = NLA_U32 }, |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 60 | [TCA_BPF_FLAGS] = { .type = NLA_U32 }, |
Jakub Kicinski | 0d01d45 | 2016-09-21 11:43:54 +0100 | [diff] [blame] | 61 | [TCA_BPF_FLAGS_GEN] = { .type = NLA_U32 }, |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 62 | [TCA_BPF_FD] = { .type = NLA_U32 }, |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 63 | [TCA_BPF_NAME] = { .type = NLA_NUL_STRING, |
| 64 | .len = CLS_BPF_NAME_LEN }, |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 65 | [TCA_BPF_OPS_LEN] = { .type = NLA_U16 }, |
| 66 | [TCA_BPF_OPS] = { .type = NLA_BINARY, |
| 67 | .len = sizeof(struct sock_filter) * BPF_MAXINSNS }, |
| 68 | }; |
| 69 | |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 70 | static int cls_bpf_exec_opcode(int code) |
| 71 | { |
| 72 | switch (code) { |
| 73 | case TC_ACT_OK: |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 74 | case TC_ACT_SHOT: |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 75 | case TC_ACT_STOLEN: |
Alexei Starovoitov | 27b29f6 | 2015-09-15 23:05:43 -0700 | [diff] [blame] | 76 | case TC_ACT_REDIRECT: |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 77 | case TC_ACT_UNSPEC: |
| 78 | return code; |
| 79 | default: |
| 80 | return TC_ACT_UNSPEC; |
| 81 | } |
| 82 | } |
| 83 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 84 | static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp, |
| 85 | struct tcf_result *res) |
| 86 | { |
WANG Cong | 80dcbd1 | 2014-09-15 14:21:50 -0700 | [diff] [blame] | 87 | struct cls_bpf_head *head = rcu_dereference_bh(tp->root); |
Daniel Borkmann | fdc5432 | 2016-01-07 15:50:22 +0100 | [diff] [blame] | 88 | bool at_ingress = skb_at_tc_ingress(skb); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 89 | struct cls_bpf_prog *prog; |
Daniel Borkmann | 54720df | 2015-03-12 20:03:12 +0100 | [diff] [blame] | 90 | int ret = -1; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 91 | |
Daniel Borkmann | 54720df | 2015-03-12 20:03:12 +0100 | [diff] [blame] | 92 | /* Needed here for accessing maps. */ |
| 93 | rcu_read_lock(); |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 94 | list_for_each_entry_rcu(prog, &head->plist, link) { |
Alexei Starovoitov | 3431205 | 2015-06-04 10:11:53 -0700 | [diff] [blame] | 95 | int filter_res; |
| 96 | |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 97 | qdisc_skb_cb(skb)->tc_classid = prog->res.classid; |
| 98 | |
Jakub Kicinski | eadb414 | 2016-09-21 11:43:55 +0100 | [diff] [blame] | 99 | if (tc_skip_sw(prog->gen_flags)) { |
| 100 | filter_res = prog->exts_integrated ? TC_ACT_UNSPEC : 0; |
| 101 | } else if (at_ingress) { |
Alexei Starovoitov | 3431205 | 2015-06-04 10:11:53 -0700 | [diff] [blame] | 102 | /* It is safe to push/pull even if skb_shared() */ |
| 103 | __skb_push(skb, skb->mac_len); |
Alexei Starovoitov | db58ba4 | 2016-05-05 19:49:12 -0700 | [diff] [blame] | 104 | bpf_compute_data_end(skb); |
Alexei Starovoitov | 3431205 | 2015-06-04 10:11:53 -0700 | [diff] [blame] | 105 | filter_res = BPF_PROG_RUN(prog->filter, skb); |
| 106 | __skb_pull(skb, skb->mac_len); |
| 107 | } else { |
Alexei Starovoitov | db58ba4 | 2016-05-05 19:49:12 -0700 | [diff] [blame] | 108 | bpf_compute_data_end(skb); |
Alexei Starovoitov | 3431205 | 2015-06-04 10:11:53 -0700 | [diff] [blame] | 109 | filter_res = BPF_PROG_RUN(prog->filter, skb); |
| 110 | } |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 111 | |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 112 | if (prog->exts_integrated) { |
Daniel Borkmann | 3a461da | 2016-03-15 22:41:22 +0100 | [diff] [blame] | 113 | res->class = 0; |
| 114 | res->classid = TC_H_MAJ(prog->res.classid) | |
| 115 | qdisc_skb_cb(skb)->tc_classid; |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 116 | |
| 117 | ret = cls_bpf_exec_opcode(filter_res); |
| 118 | if (ret == TC_ACT_UNSPEC) |
| 119 | continue; |
| 120 | break; |
| 121 | } |
| 122 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 123 | if (filter_res == 0) |
| 124 | continue; |
Daniel Borkmann | 3a461da | 2016-03-15 22:41:22 +0100 | [diff] [blame] | 125 | if (filter_res != -1) { |
| 126 | res->class = 0; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 127 | res->classid = filter_res; |
Daniel Borkmann | 3a461da | 2016-03-15 22:41:22 +0100 | [diff] [blame] | 128 | } else { |
| 129 | *res = prog->res; |
| 130 | } |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 131 | |
| 132 | ret = tcf_exts_exec(skb, &prog->exts, res); |
| 133 | if (ret < 0) |
| 134 | continue; |
| 135 | |
Daniel Borkmann | 54720df | 2015-03-12 20:03:12 +0100 | [diff] [blame] | 136 | break; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 137 | } |
Daniel Borkmann | 54720df | 2015-03-12 20:03:12 +0100 | [diff] [blame] | 138 | rcu_read_unlock(); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 139 | |
Daniel Borkmann | 54720df | 2015-03-12 20:03:12 +0100 | [diff] [blame] | 140 | return ret; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 141 | } |
| 142 | |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 143 | static bool cls_bpf_is_ebpf(const struct cls_bpf_prog *prog) |
| 144 | { |
| 145 | return !prog->bpf_ops; |
| 146 | } |
| 147 | |
Jakub Kicinski | 332ae8e | 2016-09-21 11:43:53 +0100 | [diff] [blame] | 148 | static int cls_bpf_offload_cmd(struct tcf_proto *tp, struct cls_bpf_prog *prog, |
| 149 | enum tc_clsbpf_command cmd) |
| 150 | { |
| 151 | struct net_device *dev = tp->q->dev_queue->dev; |
| 152 | struct tc_cls_bpf_offload bpf_offload = {}; |
| 153 | struct tc_to_netdev offload; |
| 154 | |
| 155 | offload.type = TC_SETUP_CLSBPF; |
| 156 | offload.cls_bpf = &bpf_offload; |
| 157 | |
| 158 | bpf_offload.command = cmd; |
| 159 | bpf_offload.exts = &prog->exts; |
| 160 | bpf_offload.prog = prog->filter; |
| 161 | bpf_offload.name = prog->bpf_name; |
| 162 | bpf_offload.exts_integrated = prog->exts_integrated; |
Jakub Kicinski | 0d01d45 | 2016-09-21 11:43:54 +0100 | [diff] [blame] | 163 | bpf_offload.gen_flags = prog->gen_flags; |
Jakub Kicinski | 332ae8e | 2016-09-21 11:43:53 +0100 | [diff] [blame] | 164 | |
| 165 | return dev->netdev_ops->ndo_setup_tc(dev, tp->q->handle, |
| 166 | tp->protocol, &offload); |
| 167 | } |
| 168 | |
Jakub Kicinski | eadb414 | 2016-09-21 11:43:55 +0100 | [diff] [blame] | 169 | static int cls_bpf_offload(struct tcf_proto *tp, struct cls_bpf_prog *prog, |
| 170 | struct cls_bpf_prog *oldprog) |
Jakub Kicinski | 332ae8e | 2016-09-21 11:43:53 +0100 | [diff] [blame] | 171 | { |
| 172 | struct net_device *dev = tp->q->dev_queue->dev; |
| 173 | struct cls_bpf_prog *obj = prog; |
| 174 | enum tc_clsbpf_command cmd; |
Jakub Kicinski | eadb414 | 2016-09-21 11:43:55 +0100 | [diff] [blame] | 175 | bool skip_sw; |
| 176 | int ret; |
| 177 | |
| 178 | skip_sw = tc_skip_sw(prog->gen_flags) || |
| 179 | (oldprog && tc_skip_sw(oldprog->gen_flags)); |
Jakub Kicinski | 332ae8e | 2016-09-21 11:43:53 +0100 | [diff] [blame] | 180 | |
| 181 | if (oldprog && oldprog->offloaded) { |
Jakub Kicinski | 0d01d45 | 2016-09-21 11:43:54 +0100 | [diff] [blame] | 182 | if (tc_should_offload(dev, tp, prog->gen_flags)) { |
Jakub Kicinski | 332ae8e | 2016-09-21 11:43:53 +0100 | [diff] [blame] | 183 | cmd = TC_CLSBPF_REPLACE; |
Jakub Kicinski | eadb414 | 2016-09-21 11:43:55 +0100 | [diff] [blame] | 184 | } else if (!tc_skip_sw(prog->gen_flags)) { |
Jakub Kicinski | 332ae8e | 2016-09-21 11:43:53 +0100 | [diff] [blame] | 185 | obj = oldprog; |
| 186 | cmd = TC_CLSBPF_DESTROY; |
Jakub Kicinski | eadb414 | 2016-09-21 11:43:55 +0100 | [diff] [blame] | 187 | } else { |
| 188 | return -EINVAL; |
Jakub Kicinski | 332ae8e | 2016-09-21 11:43:53 +0100 | [diff] [blame] | 189 | } |
| 190 | } else { |
Jakub Kicinski | 0d01d45 | 2016-09-21 11:43:54 +0100 | [diff] [blame] | 191 | if (!tc_should_offload(dev, tp, prog->gen_flags)) |
Jakub Kicinski | eadb414 | 2016-09-21 11:43:55 +0100 | [diff] [blame] | 192 | return skip_sw ? -EINVAL : 0; |
Jakub Kicinski | 332ae8e | 2016-09-21 11:43:53 +0100 | [diff] [blame] | 193 | cmd = TC_CLSBPF_ADD; |
| 194 | } |
| 195 | |
Jakub Kicinski | eadb414 | 2016-09-21 11:43:55 +0100 | [diff] [blame] | 196 | ret = cls_bpf_offload_cmd(tp, obj, cmd); |
| 197 | if (ret) |
| 198 | return skip_sw ? ret : 0; |
Jakub Kicinski | 332ae8e | 2016-09-21 11:43:53 +0100 | [diff] [blame] | 199 | |
| 200 | obj->offloaded = true; |
| 201 | if (oldprog) |
| 202 | oldprog->offloaded = false; |
Jakub Kicinski | eadb414 | 2016-09-21 11:43:55 +0100 | [diff] [blame] | 203 | |
| 204 | return 0; |
Jakub Kicinski | 332ae8e | 2016-09-21 11:43:53 +0100 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | static void cls_bpf_stop_offload(struct tcf_proto *tp, |
| 208 | struct cls_bpf_prog *prog) |
| 209 | { |
| 210 | int err; |
| 211 | |
| 212 | if (!prog->offloaded) |
| 213 | return; |
| 214 | |
| 215 | err = cls_bpf_offload_cmd(tp, prog, TC_CLSBPF_DESTROY); |
| 216 | if (err) { |
| 217 | pr_err("Stopping hardware offload failed: %d\n", err); |
| 218 | return; |
| 219 | } |
| 220 | |
| 221 | prog->offloaded = false; |
| 222 | } |
| 223 | |
Jakub Kicinski | 68d6406 | 2016-09-21 11:44:02 +0100 | [diff] [blame] | 224 | static void cls_bpf_offload_update_stats(struct tcf_proto *tp, |
| 225 | struct cls_bpf_prog *prog) |
| 226 | { |
| 227 | if (!prog->offloaded) |
| 228 | return; |
| 229 | |
| 230 | cls_bpf_offload_cmd(tp, prog, TC_CLSBPF_STATS); |
| 231 | } |
| 232 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 233 | static int cls_bpf_init(struct tcf_proto *tp) |
| 234 | { |
| 235 | struct cls_bpf_head *head; |
| 236 | |
| 237 | head = kzalloc(sizeof(*head), GFP_KERNEL); |
| 238 | if (head == NULL) |
| 239 | return -ENOBUFS; |
| 240 | |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 241 | INIT_LIST_HEAD_RCU(&head->plist); |
| 242 | rcu_assign_pointer(tp->root, head); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 243 | |
| 244 | return 0; |
| 245 | } |
| 246 | |
| 247 | static void cls_bpf_delete_prog(struct tcf_proto *tp, struct cls_bpf_prog *prog) |
| 248 | { |
WANG Cong | 18d0264 | 2014-09-25 10:26:37 -0700 | [diff] [blame] | 249 | tcf_exts_destroy(&prog->exts); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 250 | |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 251 | if (cls_bpf_is_ebpf(prog)) |
| 252 | bpf_prog_put(prog->filter); |
| 253 | else |
| 254 | bpf_prog_destroy(prog->filter); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 255 | |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 256 | kfree(prog->bpf_name); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 257 | kfree(prog->bpf_ops); |
| 258 | kfree(prog); |
| 259 | } |
| 260 | |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 261 | static void __cls_bpf_delete_prog(struct rcu_head *rcu) |
| 262 | { |
| 263 | struct cls_bpf_prog *prog = container_of(rcu, struct cls_bpf_prog, rcu); |
| 264 | |
| 265 | cls_bpf_delete_prog(prog->tp, prog); |
| 266 | } |
| 267 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 268 | static int cls_bpf_delete(struct tcf_proto *tp, unsigned long arg) |
| 269 | { |
Jiri Pirko | 472f583 | 2014-12-02 18:00:32 +0100 | [diff] [blame] | 270 | struct cls_bpf_prog *prog = (struct cls_bpf_prog *) arg; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 271 | |
Jakub Kicinski | 332ae8e | 2016-09-21 11:43:53 +0100 | [diff] [blame] | 272 | cls_bpf_stop_offload(tp, prog); |
Jiri Pirko | 472f583 | 2014-12-02 18:00:32 +0100 | [diff] [blame] | 273 | list_del_rcu(&prog->link); |
| 274 | tcf_unbind_filter(tp, &prog->res); |
| 275 | call_rcu(&prog->rcu, __cls_bpf_delete_prog); |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 276 | |
Jiri Pirko | 472f583 | 2014-12-02 18:00:32 +0100 | [diff] [blame] | 277 | return 0; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 278 | } |
| 279 | |
Cong Wang | 1e052be | 2015-03-06 11:47:59 -0800 | [diff] [blame] | 280 | static bool cls_bpf_destroy(struct tcf_proto *tp, bool force) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 281 | { |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 282 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 283 | struct cls_bpf_prog *prog, *tmp; |
| 284 | |
Cong Wang | 1e052be | 2015-03-06 11:47:59 -0800 | [diff] [blame] | 285 | if (!force && !list_empty(&head->plist)) |
| 286 | return false; |
| 287 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 288 | list_for_each_entry_safe(prog, tmp, &head->plist, link) { |
Jakub Kicinski | 332ae8e | 2016-09-21 11:43:53 +0100 | [diff] [blame] | 289 | cls_bpf_stop_offload(tp, prog); |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 290 | list_del_rcu(&prog->link); |
John Fastabend | 18cdb37 | 2014-10-05 21:28:52 -0700 | [diff] [blame] | 291 | tcf_unbind_filter(tp, &prog->res); |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 292 | call_rcu(&prog->rcu, __cls_bpf_delete_prog); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 293 | } |
| 294 | |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 295 | RCU_INIT_POINTER(tp->root, NULL); |
| 296 | kfree_rcu(head, rcu); |
Cong Wang | 1e052be | 2015-03-06 11:47:59 -0800 | [diff] [blame] | 297 | return true; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | static unsigned long cls_bpf_get(struct tcf_proto *tp, u32 handle) |
| 301 | { |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 302 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 303 | struct cls_bpf_prog *prog; |
| 304 | unsigned long ret = 0UL; |
| 305 | |
| 306 | if (head == NULL) |
| 307 | return 0UL; |
| 308 | |
Jiri Pirko | 3fe6b49 | 2014-12-02 18:00:33 +0100 | [diff] [blame] | 309 | list_for_each_entry(prog, &head->plist, link) { |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 310 | if (prog->handle == handle) { |
| 311 | ret = (unsigned long) prog; |
| 312 | break; |
| 313 | } |
| 314 | } |
| 315 | |
| 316 | return ret; |
| 317 | } |
| 318 | |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 319 | static int cls_bpf_prog_from_ops(struct nlattr **tb, struct cls_bpf_prog *prog) |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 320 | { |
| 321 | struct sock_filter *bpf_ops; |
| 322 | struct sock_fprog_kern fprog_tmp; |
| 323 | struct bpf_prog *fp; |
| 324 | u16 bpf_size, bpf_num_ops; |
| 325 | int ret; |
| 326 | |
| 327 | bpf_num_ops = nla_get_u16(tb[TCA_BPF_OPS_LEN]); |
| 328 | if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0) |
| 329 | return -EINVAL; |
| 330 | |
| 331 | bpf_size = bpf_num_ops * sizeof(*bpf_ops); |
| 332 | if (bpf_size != nla_len(tb[TCA_BPF_OPS])) |
| 333 | return -EINVAL; |
| 334 | |
| 335 | bpf_ops = kzalloc(bpf_size, GFP_KERNEL); |
| 336 | if (bpf_ops == NULL) |
| 337 | return -ENOMEM; |
| 338 | |
| 339 | memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size); |
| 340 | |
| 341 | fprog_tmp.len = bpf_num_ops; |
| 342 | fprog_tmp.filter = bpf_ops; |
| 343 | |
| 344 | ret = bpf_prog_create(&fp, &fprog_tmp); |
| 345 | if (ret < 0) { |
| 346 | kfree(bpf_ops); |
| 347 | return ret; |
| 348 | } |
| 349 | |
| 350 | prog->bpf_ops = bpf_ops; |
| 351 | prog->bpf_num_ops = bpf_num_ops; |
| 352 | prog->bpf_name = NULL; |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 353 | prog->filter = fp; |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 354 | |
| 355 | return 0; |
| 356 | } |
| 357 | |
Daniel Borkmann | c46646d | 2015-09-30 01:41:51 +0200 | [diff] [blame] | 358 | static int cls_bpf_prog_from_efd(struct nlattr **tb, struct cls_bpf_prog *prog, |
| 359 | const struct tcf_proto *tp) |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 360 | { |
| 361 | struct bpf_prog *fp; |
| 362 | char *name = NULL; |
| 363 | u32 bpf_fd; |
| 364 | |
| 365 | bpf_fd = nla_get_u32(tb[TCA_BPF_FD]); |
| 366 | |
Daniel Borkmann | 113214b | 2016-06-30 17:24:44 +0200 | [diff] [blame] | 367 | fp = bpf_prog_get_type(bpf_fd, BPF_PROG_TYPE_SCHED_CLS); |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 368 | if (IS_ERR(fp)) |
| 369 | return PTR_ERR(fp); |
| 370 | |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 371 | if (tb[TCA_BPF_NAME]) { |
Thomas Graf | b15ca18 | 2016-10-26 10:53:16 +0200 | [diff] [blame^] | 372 | name = nla_memdup(tb[TCA_BPF_NAME], GFP_KERNEL); |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 373 | if (!name) { |
| 374 | bpf_prog_put(fp); |
| 375 | return -ENOMEM; |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | prog->bpf_ops = NULL; |
| 380 | prog->bpf_fd = bpf_fd; |
| 381 | prog->bpf_name = name; |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 382 | prog->filter = fp; |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 383 | |
Daniel Borkmann | 1f211a1 | 2016-01-07 22:29:47 +0100 | [diff] [blame] | 384 | if (fp->dst_needed && !(tp->q->flags & TCQ_F_INGRESS)) |
Daniel Borkmann | c46646d | 2015-09-30 01:41:51 +0200 | [diff] [blame] | 385 | netif_keep_dst(qdisc_dev(tp->q)); |
| 386 | |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 387 | return 0; |
| 388 | } |
| 389 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 390 | static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp, |
| 391 | struct cls_bpf_prog *prog, |
| 392 | unsigned long base, struct nlattr **tb, |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 393 | struct nlattr *est, bool ovr) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 394 | { |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 395 | bool is_bpf, is_ebpf, have_exts = false; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 396 | struct tcf_exts exts; |
Jakub Kicinski | 0d01d45 | 2016-09-21 11:43:54 +0100 | [diff] [blame] | 397 | u32 gen_flags = 0; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 398 | int ret; |
| 399 | |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 400 | is_bpf = tb[TCA_BPF_OPS_LEN] && tb[TCA_BPF_OPS]; |
| 401 | is_ebpf = tb[TCA_BPF_FD]; |
Daniel Borkmann | ef146fa | 2015-09-23 21:56:47 +0200 | [diff] [blame] | 402 | if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf)) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 403 | return -EINVAL; |
| 404 | |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 405 | ret = tcf_exts_init(&exts, TCA_BPF_ACT, TCA_BPF_POLICE); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 406 | if (ret < 0) |
| 407 | return ret; |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 408 | ret = tcf_exts_validate(net, tp, tb, est, &exts, ovr); |
| 409 | if (ret < 0) |
| 410 | goto errout; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 411 | |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 412 | if (tb[TCA_BPF_FLAGS]) { |
| 413 | u32 bpf_flags = nla_get_u32(tb[TCA_BPF_FLAGS]); |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 414 | |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 415 | if (bpf_flags & ~TCA_BPF_FLAG_ACT_DIRECT) { |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 416 | ret = -EINVAL; |
| 417 | goto errout; |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | have_exts = bpf_flags & TCA_BPF_FLAG_ACT_DIRECT; |
| 421 | } |
Jakub Kicinski | 0d01d45 | 2016-09-21 11:43:54 +0100 | [diff] [blame] | 422 | if (tb[TCA_BPF_FLAGS_GEN]) { |
| 423 | gen_flags = nla_get_u32(tb[TCA_BPF_FLAGS_GEN]); |
| 424 | if (gen_flags & ~CLS_BPF_SUPPORTED_GEN_FLAGS || |
| 425 | !tc_flags_valid(gen_flags)) { |
| 426 | ret = -EINVAL; |
| 427 | goto errout; |
| 428 | } |
| 429 | } |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 430 | |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 431 | prog->exts_integrated = have_exts; |
Jakub Kicinski | 0d01d45 | 2016-09-21 11:43:54 +0100 | [diff] [blame] | 432 | prog->gen_flags = gen_flags; |
Daniel Borkmann | 045efa8 | 2015-09-15 23:05:42 -0700 | [diff] [blame] | 433 | |
| 434 | ret = is_bpf ? cls_bpf_prog_from_ops(tb, prog) : |
Daniel Borkmann | c46646d | 2015-09-30 01:41:51 +0200 | [diff] [blame] | 435 | cls_bpf_prog_from_efd(tb, prog, tp); |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 436 | if (ret < 0) |
| 437 | goto errout; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 438 | |
Daniel Borkmann | ef146fa | 2015-09-23 21:56:47 +0200 | [diff] [blame] | 439 | if (tb[TCA_BPF_CLASSID]) { |
| 440 | prog->res.classid = nla_get_u32(tb[TCA_BPF_CLASSID]); |
| 441 | tcf_bind_filter(tp, &prog->res, base); |
| 442 | } |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 443 | |
Daniel Borkmann | ef146fa | 2015-09-23 21:56:47 +0200 | [diff] [blame] | 444 | tcf_exts_change(tp, &prog->exts, &exts); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 445 | return 0; |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 446 | |
| 447 | errout: |
| 448 | tcf_exts_destroy(&exts); |
| 449 | return ret; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 450 | } |
| 451 | |
| 452 | static u32 cls_bpf_grab_new_handle(struct tcf_proto *tp, |
| 453 | struct cls_bpf_head *head) |
| 454 | { |
| 455 | unsigned int i = 0x80000000; |
Daniel Borkmann | 3f2ab13 | 2015-01-22 10:41:02 +0100 | [diff] [blame] | 456 | u32 handle; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 457 | |
| 458 | do { |
| 459 | if (++head->hgen == 0x7FFFFFFF) |
| 460 | head->hgen = 1; |
| 461 | } while (--i > 0 && cls_bpf_get(tp, head->hgen)); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 462 | |
Daniel Borkmann | 3f2ab13 | 2015-01-22 10:41:02 +0100 | [diff] [blame] | 463 | if (unlikely(i == 0)) { |
| 464 | pr_err("Insufficient number of handles\n"); |
| 465 | handle = 0; |
| 466 | } else { |
| 467 | handle = head->hgen; |
| 468 | } |
| 469 | |
| 470 | return handle; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 471 | } |
| 472 | |
| 473 | static int cls_bpf_change(struct net *net, struct sk_buff *in_skb, |
| 474 | struct tcf_proto *tp, unsigned long base, |
| 475 | u32 handle, struct nlattr **tca, |
Cong Wang | 2f7ef2f | 2014-04-25 13:54:06 -0700 | [diff] [blame] | 476 | unsigned long *arg, bool ovr) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 477 | { |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 478 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
| 479 | struct cls_bpf_prog *oldprog = (struct cls_bpf_prog *) *arg; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 480 | struct nlattr *tb[TCA_BPF_MAX + 1]; |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 481 | struct cls_bpf_prog *prog; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 482 | int ret; |
| 483 | |
| 484 | if (tca[TCA_OPTIONS] == NULL) |
| 485 | return -EINVAL; |
| 486 | |
| 487 | ret = nla_parse_nested(tb, TCA_BPF_MAX, tca[TCA_OPTIONS], bpf_policy); |
| 488 | if (ret < 0) |
| 489 | return ret; |
| 490 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 491 | prog = kzalloc(sizeof(*prog), GFP_KERNEL); |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 492 | if (!prog) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 493 | return -ENOBUFS; |
| 494 | |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 495 | ret = tcf_exts_init(&prog->exts, TCA_BPF_ACT, TCA_BPF_POLICE); |
| 496 | if (ret < 0) |
| 497 | goto errout; |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 498 | |
| 499 | if (oldprog) { |
| 500 | if (handle && oldprog->handle != handle) { |
| 501 | ret = -EINVAL; |
| 502 | goto errout; |
| 503 | } |
| 504 | } |
| 505 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 506 | if (handle == 0) |
| 507 | prog->handle = cls_bpf_grab_new_handle(tp, head); |
| 508 | else |
| 509 | prog->handle = handle; |
| 510 | if (prog->handle == 0) { |
| 511 | ret = -EINVAL; |
| 512 | goto errout; |
| 513 | } |
| 514 | |
Jamal Hadi Salim | 5a7a555 | 2016-09-18 08:45:33 -0400 | [diff] [blame] | 515 | ret = cls_bpf_modify_existing(net, tp, prog, base, tb, tca[TCA_RATE], |
| 516 | ovr); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 517 | if (ret < 0) |
| 518 | goto errout; |
| 519 | |
Jakub Kicinski | eadb414 | 2016-09-21 11:43:55 +0100 | [diff] [blame] | 520 | ret = cls_bpf_offload(tp, prog, oldprog); |
| 521 | if (ret) { |
| 522 | cls_bpf_delete_prog(tp, prog); |
| 523 | return ret; |
| 524 | } |
Jakub Kicinski | 332ae8e | 2016-09-21 11:43:53 +0100 | [diff] [blame] | 525 | |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 526 | if (oldprog) { |
Daniel Borkmann | f6bfc46 | 2015-07-17 22:38:43 +0200 | [diff] [blame] | 527 | list_replace_rcu(&oldprog->link, &prog->link); |
John Fastabend | 18cdb37 | 2014-10-05 21:28:52 -0700 | [diff] [blame] | 528 | tcf_unbind_filter(tp, &oldprog->res); |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 529 | call_rcu(&oldprog->rcu, __cls_bpf_delete_prog); |
| 530 | } else { |
| 531 | list_add_rcu(&prog->link, &head->plist); |
| 532 | } |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 533 | |
| 534 | *arg = (unsigned long) prog; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 535 | return 0; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 536 | |
WANG Cong | b9a24bb | 2016-08-19 12:36:54 -0700 | [diff] [blame] | 537 | errout: |
| 538 | tcf_exts_destroy(&prog->exts); |
| 539 | kfree(prog); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 540 | return ret; |
| 541 | } |
| 542 | |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 543 | static int cls_bpf_dump_bpf_info(const struct cls_bpf_prog *prog, |
| 544 | struct sk_buff *skb) |
| 545 | { |
| 546 | struct nlattr *nla; |
| 547 | |
| 548 | if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_num_ops)) |
| 549 | return -EMSGSIZE; |
| 550 | |
| 551 | nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_num_ops * |
| 552 | sizeof(struct sock_filter)); |
| 553 | if (nla == NULL) |
| 554 | return -EMSGSIZE; |
| 555 | |
| 556 | memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla)); |
| 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | static int cls_bpf_dump_ebpf_info(const struct cls_bpf_prog *prog, |
| 562 | struct sk_buff *skb) |
| 563 | { |
| 564 | if (nla_put_u32(skb, TCA_BPF_FD, prog->bpf_fd)) |
| 565 | return -EMSGSIZE; |
| 566 | |
| 567 | if (prog->bpf_name && |
| 568 | nla_put_string(skb, TCA_BPF_NAME, prog->bpf_name)) |
| 569 | return -EMSGSIZE; |
| 570 | |
| 571 | return 0; |
| 572 | } |
| 573 | |
WANG Cong | 832d1d5 | 2014-01-09 16:14:01 -0800 | [diff] [blame] | 574 | 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] | 575 | struct sk_buff *skb, struct tcmsg *tm) |
| 576 | { |
| 577 | struct cls_bpf_prog *prog = (struct cls_bpf_prog *) fh; |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 578 | struct nlattr *nest; |
Daniel Borkmann | bf007d1 | 2015-09-23 21:56:46 +0200 | [diff] [blame] | 579 | u32 bpf_flags = 0; |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 580 | int ret; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 581 | |
| 582 | if (prog == NULL) |
| 583 | return skb->len; |
| 584 | |
| 585 | tm->tcm_handle = prog->handle; |
| 586 | |
Jakub Kicinski | 68d6406 | 2016-09-21 11:44:02 +0100 | [diff] [blame] | 587 | cls_bpf_offload_update_stats(tp, prog); |
| 588 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 589 | nest = nla_nest_start(skb, TCA_OPTIONS); |
| 590 | if (nest == NULL) |
| 591 | goto nla_put_failure; |
| 592 | |
Daniel Borkmann | ef146fa | 2015-09-23 21:56:47 +0200 | [diff] [blame] | 593 | if (prog->res.classid && |
| 594 | nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid)) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 595 | goto nla_put_failure; |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 596 | |
Daniel Borkmann | e2e9b65 | 2015-03-01 12:31:48 +0100 | [diff] [blame] | 597 | if (cls_bpf_is_ebpf(prog)) |
| 598 | ret = cls_bpf_dump_ebpf_info(prog, skb); |
| 599 | else |
| 600 | ret = cls_bpf_dump_bpf_info(prog, skb); |
| 601 | if (ret) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 602 | goto nla_put_failure; |
| 603 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 604 | if (tcf_exts_dump(skb, &prog->exts) < 0) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 605 | goto nla_put_failure; |
| 606 | |
Daniel Borkmann | bf007d1 | 2015-09-23 21:56:46 +0200 | [diff] [blame] | 607 | if (prog->exts_integrated) |
| 608 | bpf_flags |= TCA_BPF_FLAG_ACT_DIRECT; |
| 609 | if (bpf_flags && nla_put_u32(skb, TCA_BPF_FLAGS, bpf_flags)) |
| 610 | goto nla_put_failure; |
Jakub Kicinski | 0d01d45 | 2016-09-21 11:43:54 +0100 | [diff] [blame] | 611 | if (prog->gen_flags && |
| 612 | nla_put_u32(skb, TCA_BPF_FLAGS_GEN, prog->gen_flags)) |
| 613 | goto nla_put_failure; |
Daniel Borkmann | bf007d1 | 2015-09-23 21:56:46 +0200 | [diff] [blame] | 614 | |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 615 | nla_nest_end(skb, nest); |
| 616 | |
WANG Cong | 5da57f4 | 2013-12-15 20:15:07 -0800 | [diff] [blame] | 617 | if (tcf_exts_dump_stats(skb, &prog->exts) < 0) |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 618 | goto nla_put_failure; |
| 619 | |
| 620 | return skb->len; |
| 621 | |
| 622 | nla_put_failure: |
| 623 | nla_nest_cancel(skb, nest); |
| 624 | return -1; |
| 625 | } |
| 626 | |
| 627 | static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg) |
| 628 | { |
John Fastabend | 1f947bf | 2014-09-12 20:10:24 -0700 | [diff] [blame] | 629 | struct cls_bpf_head *head = rtnl_dereference(tp->root); |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 630 | struct cls_bpf_prog *prog; |
| 631 | |
Jiri Pirko | 3fe6b49 | 2014-12-02 18:00:33 +0100 | [diff] [blame] | 632 | list_for_each_entry(prog, &head->plist, link) { |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 633 | if (arg->count < arg->skip) |
| 634 | goto skip; |
| 635 | if (arg->fn(tp, (unsigned long) prog, arg) < 0) { |
| 636 | arg->stop = 1; |
| 637 | break; |
| 638 | } |
| 639 | skip: |
| 640 | arg->count++; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | static struct tcf_proto_ops cls_bpf_ops __read_mostly = { |
| 645 | .kind = "bpf", |
| 646 | .owner = THIS_MODULE, |
| 647 | .classify = cls_bpf_classify, |
| 648 | .init = cls_bpf_init, |
| 649 | .destroy = cls_bpf_destroy, |
| 650 | .get = cls_bpf_get, |
Daniel Borkmann | 7d1d65c | 2013-10-28 16:43:02 +0100 | [diff] [blame] | 651 | .change = cls_bpf_change, |
| 652 | .delete = cls_bpf_delete, |
| 653 | .walk = cls_bpf_walk, |
| 654 | .dump = cls_bpf_dump, |
| 655 | }; |
| 656 | |
| 657 | static int __init cls_bpf_init_mod(void) |
| 658 | { |
| 659 | return register_tcf_proto_ops(&cls_bpf_ops); |
| 660 | } |
| 661 | |
| 662 | static void __exit cls_bpf_exit_mod(void) |
| 663 | { |
| 664 | unregister_tcf_proto_ops(&cls_bpf_ops); |
| 665 | } |
| 666 | |
| 667 | module_init(cls_bpf_init_mod); |
| 668 | module_exit(cls_bpf_exit_mod); |