hc
2024-10-22 8ac6c7a54ed1b98d142dce24b11c6de6a1e239a5
kernel/net/sched/act_bpf.c
....@@ -1,10 +1,6 @@
1
+// SPDX-License-Identifier: GPL-2.0-or-later
12 /*
23 * 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.
84 */
95
106 #include <linux/module.h>
....@@ -16,7 +12,9 @@
1612 #include <linux/bpf.h>
1713
1814 #include <net/netlink.h>
15
+#include <net/sock.h>
1916 #include <net/pkt_sched.h>
17
+#include <net/pkt_cls.h>
2018
2119 #include <linux/tc_act/tc_bpf.h>
2220 #include <net/tc_act/tc_bpf.h>
....@@ -56,6 +54,8 @@
5654 bpf_compute_data_pointers(skb);
5755 filter_res = BPF_PROG_RUN(filter, skb);
5856 }
57
+ if (skb_sk_is_prefetched(skb) && filter_res != TC_ACT_OK)
58
+ skb_orphan(skb);
5959 rcu_read_unlock();
6060
6161 /* A BPF program may overwrite the default action opcode.
....@@ -278,10 +278,12 @@
278278 static int tcf_bpf_init(struct net *net, struct nlattr *nla,
279279 struct nlattr *est, struct tc_action **act,
280280 int replace, int bind, bool rtnl_held,
281
+ struct tcf_proto *tp, u32 flags,
281282 struct netlink_ext_ack *extack)
282283 {
283284 struct tc_action_net *tn = net_generic(net, bpf_net_id);
284285 struct nlattr *tb[TCA_ACT_BPF_MAX + 1];
286
+ struct tcf_chain *goto_ch = NULL;
285287 struct tcf_bpf_cfg cfg, old;
286288 struct tc_act_bpf *parm;
287289 struct tcf_bpf *prog;
....@@ -292,7 +294,8 @@
292294 if (!nla)
293295 return -EINVAL;
294296
295
- ret = nla_parse_nested(tb, TCA_ACT_BPF_MAX, nla, act_bpf_policy, NULL);
297
+ ret = nla_parse_nested_deprecated(tb, TCA_ACT_BPF_MAX, nla,
298
+ act_bpf_policy, NULL);
296299 if (ret < 0)
297300 return ret;
298301
....@@ -304,7 +307,7 @@
304307 ret = tcf_idr_check_alloc(tn, &index, act, bind);
305308 if (!ret) {
306309 ret = tcf_idr_create(tn, index, est, act,
307
- &act_bpf_ops, bind, true);
310
+ &act_bpf_ops, bind, true, flags);
308311 if (ret < 0) {
309312 tcf_idr_cleanup(tn, index);
310313 return ret;
....@@ -324,12 +327,16 @@
324327 return ret;
325328 }
326329
330
+ ret = tcf_action_check_ctrlact(parm->action, tp, &goto_ch, extack);
331
+ if (ret < 0)
332
+ goto release_idr;
333
+
327334 is_bpf = tb[TCA_ACT_BPF_OPS_LEN] && tb[TCA_ACT_BPF_OPS];
328335 is_ebpf = tb[TCA_ACT_BPF_FD];
329336
330337 if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf)) {
331338 ret = -EINVAL;
332
- goto out;
339
+ goto put_chain;
333340 }
334341
335342 memset(&cfg, 0, sizeof(cfg));
....@@ -337,7 +344,7 @@
337344 ret = is_bpf ? tcf_bpf_init_from_ops(tb, &cfg) :
338345 tcf_bpf_init_from_efd(tb, &cfg);
339346 if (ret < 0)
340
- goto out;
347
+ goto put_chain;
341348
342349 prog = to_bpf(*act);
343350
....@@ -351,22 +358,27 @@
351358 if (cfg.bpf_num_ops)
352359 prog->bpf_num_ops = cfg.bpf_num_ops;
353360
354
- prog->tcf_action = parm->action;
361
+ goto_ch = tcf_action_set_ctrlact(*act, parm->action, goto_ch);
355362 rcu_assign_pointer(prog->filter, cfg.filter);
356363 spin_unlock_bh(&prog->tcf_lock);
357364
358
- if (res == ACT_P_CREATED) {
359
- tcf_idr_insert(tn, *act);
360
- } else {
365
+ if (goto_ch)
366
+ tcf_chain_put_by_act(goto_ch);
367
+
368
+ if (res != ACT_P_CREATED) {
361369 /* make sure the program being replaced is no longer executing */
362370 synchronize_rcu();
363371 tcf_bpf_cfg_cleanup(&old);
364372 }
365373
366374 return res;
367
-out:
368
- tcf_idr_release(*act, bind);
369375
376
+put_chain:
377
+ if (goto_ch)
378
+ tcf_chain_put_by_act(goto_ch);
379
+
380
+release_idr:
381
+ tcf_idr_release(*act, bind);
370382 return ret;
371383 }
372384
....@@ -388,8 +400,7 @@
388400 return tcf_generic_walker(tn, skb, cb, type, ops, extack);
389401 }
390402
391
-static int tcf_bpf_search(struct net *net, struct tc_action **a, u32 index,
392
- struct netlink_ext_ack *extack)
403
+static int tcf_bpf_search(struct net *net, struct tc_action **a, u32 index)
393404 {
394405 struct tc_action_net *tn = net_generic(net, bpf_net_id);
395406
....@@ -398,7 +409,7 @@
398409
399410 static struct tc_action_ops act_bpf_ops __read_mostly = {
400411 .kind = "bpf",
401
- .type = TCA_ACT_BPF,
412
+ .id = TCA_ID_BPF,
402413 .owner = THIS_MODULE,
403414 .act = tcf_bpf_act,
404415 .dump = tcf_bpf_dump,