cls_bpf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Berkeley Packet Filter based traffic classifier
  4. *
  5. * Might be used to classify traffic through flexible, user-defined and
  6. * possibly JIT-ed BPF filters for traffic control as an alternative to
  7. * ematches.
  8. *
  9. * (C) 2013 Daniel Borkmann <dborkman@redhat.com>
  10. */
  11. #include <linux/module.h>
  12. #include <linux/types.h>
  13. #include <linux/skbuff.h>
  14. #include <linux/filter.h>
  15. #include <linux/bpf.h>
  16. #include <linux/idr.h>
  17. #include <net/rtnetlink.h>
  18. #include <net/pkt_cls.h>
  19. #include <net/sock.h>
  20. #include <net/tc_wrapper.h>
  21. MODULE_LICENSE("GPL");
  22. MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
  23. MODULE_DESCRIPTION("TC BPF based classifier");
  24. #define CLS_BPF_NAME_LEN 256
  25. #define CLS_BPF_SUPPORTED_GEN_FLAGS \
  26. (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)
  27. struct cls_bpf_head {
  28. struct list_head plist;
  29. struct idr handle_idr;
  30. struct rcu_head rcu;
  31. };
  32. struct cls_bpf_prog {
  33. struct bpf_prog *filter;
  34. struct list_head link;
  35. struct tcf_result res;
  36. bool exts_integrated;
  37. u32 gen_flags;
  38. unsigned int in_hw_count;
  39. struct tcf_exts exts;
  40. u32 handle;
  41. u16 bpf_num_ops;
  42. struct sock_filter *bpf_ops;
  43. const char *bpf_name;
  44. struct tcf_proto *tp;
  45. struct rcu_work rwork;
  46. };
  47. static const struct nla_policy bpf_policy[TCA_BPF_MAX + 1] = {
  48. [TCA_BPF_CLASSID] = { .type = NLA_U32 },
  49. [TCA_BPF_FLAGS] = { .type = NLA_U32 },
  50. [TCA_BPF_FLAGS_GEN] = { .type = NLA_U32 },
  51. [TCA_BPF_FD] = { .type = NLA_U32 },
  52. [TCA_BPF_NAME] = { .type = NLA_NUL_STRING,
  53. .len = CLS_BPF_NAME_LEN },
  54. [TCA_BPF_OPS_LEN] = { .type = NLA_U16 },
  55. [TCA_BPF_OPS] = { .type = NLA_BINARY,
  56. .len = sizeof(struct sock_filter) * BPF_MAXINSNS },
  57. };
  58. static int cls_bpf_exec_opcode(int code)
  59. {
  60. switch (code) {
  61. case TC_ACT_OK:
  62. case TC_ACT_SHOT:
  63. case TC_ACT_STOLEN:
  64. case TC_ACT_TRAP:
  65. case TC_ACT_REDIRECT:
  66. case TC_ACT_UNSPEC:
  67. return code;
  68. default:
  69. return TC_ACT_UNSPEC;
  70. }
  71. }
  72. TC_INDIRECT_SCOPE int cls_bpf_classify(struct sk_buff *skb,
  73. const struct tcf_proto *tp,
  74. struct tcf_result *res)
  75. {
  76. struct cls_bpf_head *head = rcu_dereference_bh(tp->root);
  77. bool at_ingress = skb_at_tc_ingress(skb);
  78. struct cls_bpf_prog *prog;
  79. int ret = -1;
  80. list_for_each_entry_rcu(prog, &head->plist, link) {
  81. int filter_res;
  82. qdisc_skb_cb(skb)->tc_classid = prog->res.classid;
  83. if (tc_skip_sw(prog->gen_flags)) {
  84. filter_res = prog->exts_integrated ? TC_ACT_UNSPEC : 0;
  85. } else if (at_ingress) {
  86. /* It is safe to push/pull even if skb_shared() */
  87. __skb_push(skb, skb->mac_len);
  88. bpf_compute_data_pointers(skb);
  89. filter_res = bpf_prog_run(prog->filter, skb);
  90. __skb_pull(skb, skb->mac_len);
  91. } else {
  92. bpf_compute_data_pointers(skb);
  93. filter_res = bpf_prog_run(prog->filter, skb);
  94. }
  95. if (unlikely(!skb->tstamp && skb->tstamp_type))
  96. skb->tstamp_type = SKB_CLOCK_REALTIME;
  97. if (prog->exts_integrated) {
  98. res->class = 0;
  99. res->classid = TC_H_MAJ(prog->res.classid) |
  100. qdisc_skb_cb(skb)->tc_classid;
  101. ret = cls_bpf_exec_opcode(filter_res);
  102. if (ret == TC_ACT_UNSPEC)
  103. continue;
  104. break;
  105. }
  106. if (filter_res == 0)
  107. continue;
  108. if (filter_res != -1) {
  109. res->class = 0;
  110. res->classid = filter_res;
  111. } else {
  112. *res = prog->res;
  113. }
  114. ret = tcf_exts_exec(skb, &prog->exts, res);
  115. if (ret < 0)
  116. continue;
  117. break;
  118. }
  119. return ret;
  120. }
  121. static bool cls_bpf_is_ebpf(const struct cls_bpf_prog *prog)
  122. {
  123. return !prog->bpf_ops;
  124. }
  125. static int cls_bpf_offload_cmd(struct tcf_proto *tp, struct cls_bpf_prog *prog,
  126. struct cls_bpf_prog *oldprog,
  127. struct netlink_ext_ack *extack)
  128. {
  129. struct tcf_block *block = tp->chain->block;
  130. struct tc_cls_bpf_offload cls_bpf = {};
  131. struct cls_bpf_prog *obj;
  132. bool skip_sw;
  133. int err;
  134. skip_sw = prog && tc_skip_sw(prog->gen_flags);
  135. obj = prog ?: oldprog;
  136. tc_cls_common_offload_init(&cls_bpf.common, tp, obj->gen_flags, extack);
  137. cls_bpf.command = TC_CLSBPF_OFFLOAD;
  138. cls_bpf.exts = &obj->exts;
  139. cls_bpf.prog = prog ? prog->filter : NULL;
  140. cls_bpf.oldprog = oldprog ? oldprog->filter : NULL;
  141. cls_bpf.name = obj->bpf_name;
  142. cls_bpf.exts_integrated = obj->exts_integrated;
  143. if (oldprog && prog)
  144. err = tc_setup_cb_replace(block, tp, TC_SETUP_CLSBPF, &cls_bpf,
  145. skip_sw, &oldprog->gen_flags,
  146. &oldprog->in_hw_count,
  147. &prog->gen_flags, &prog->in_hw_count,
  148. true);
  149. else if (prog)
  150. err = tc_setup_cb_add(block, tp, TC_SETUP_CLSBPF, &cls_bpf,
  151. skip_sw, &prog->gen_flags,
  152. &prog->in_hw_count, true);
  153. else
  154. err = tc_setup_cb_destroy(block, tp, TC_SETUP_CLSBPF, &cls_bpf,
  155. skip_sw, &oldprog->gen_flags,
  156. &oldprog->in_hw_count, true);
  157. if (prog && err) {
  158. cls_bpf_offload_cmd(tp, oldprog, prog, extack);
  159. return err;
  160. }
  161. if (prog && skip_sw && !(prog->gen_flags & TCA_CLS_FLAGS_IN_HW))
  162. return -EINVAL;
  163. return 0;
  164. }
  165. static u32 cls_bpf_flags(u32 flags)
  166. {
  167. return flags & CLS_BPF_SUPPORTED_GEN_FLAGS;
  168. }
  169. static int cls_bpf_offload(struct tcf_proto *tp, struct cls_bpf_prog *prog,
  170. struct cls_bpf_prog *oldprog,
  171. struct netlink_ext_ack *extack)
  172. {
  173. if (prog && oldprog &&
  174. cls_bpf_flags(prog->gen_flags) !=
  175. cls_bpf_flags(oldprog->gen_flags))
  176. return -EINVAL;
  177. if (prog && tc_skip_hw(prog->gen_flags))
  178. prog = NULL;
  179. if (oldprog && tc_skip_hw(oldprog->gen_flags))
  180. oldprog = NULL;
  181. if (!prog && !oldprog)
  182. return 0;
  183. return cls_bpf_offload_cmd(tp, prog, oldprog, extack);
  184. }
  185. static void cls_bpf_stop_offload(struct tcf_proto *tp,
  186. struct cls_bpf_prog *prog,
  187. struct netlink_ext_ack *extack)
  188. {
  189. int err;
  190. err = cls_bpf_offload_cmd(tp, NULL, prog, extack);
  191. if (err)
  192. pr_err("Stopping hardware offload failed: %d\n", err);
  193. }
  194. static void cls_bpf_offload_update_stats(struct tcf_proto *tp,
  195. struct cls_bpf_prog *prog)
  196. {
  197. struct tcf_block *block = tp->chain->block;
  198. struct tc_cls_bpf_offload cls_bpf = {};
  199. tc_cls_common_offload_init(&cls_bpf.common, tp, prog->gen_flags, NULL);
  200. cls_bpf.command = TC_CLSBPF_STATS;
  201. cls_bpf.exts = &prog->exts;
  202. cls_bpf.prog = prog->filter;
  203. cls_bpf.name = prog->bpf_name;
  204. cls_bpf.exts_integrated = prog->exts_integrated;
  205. tc_setup_cb_call(block, TC_SETUP_CLSBPF, &cls_bpf, false, true);
  206. }
  207. static int cls_bpf_init(struct tcf_proto *tp)
  208. {
  209. struct cls_bpf_head *head;
  210. head = kzalloc(sizeof(*head), GFP_KERNEL);
  211. if (head == NULL)
  212. return -ENOBUFS;
  213. INIT_LIST_HEAD_RCU(&head->plist);
  214. idr_init(&head->handle_idr);
  215. rcu_assign_pointer(tp->root, head);
  216. return 0;
  217. }
  218. static void cls_bpf_free_parms(struct cls_bpf_prog *prog)
  219. {
  220. if (cls_bpf_is_ebpf(prog))
  221. bpf_prog_put(prog->filter);
  222. else
  223. bpf_prog_destroy(prog->filter);
  224. kfree(prog->bpf_name);
  225. kfree(prog->bpf_ops);
  226. }
  227. static void __cls_bpf_delete_prog(struct cls_bpf_prog *prog)
  228. {
  229. tcf_exts_destroy(&prog->exts);
  230. tcf_exts_put_net(&prog->exts);
  231. cls_bpf_free_parms(prog);
  232. kfree(prog);
  233. }
  234. static void cls_bpf_delete_prog_work(struct work_struct *work)
  235. {
  236. struct cls_bpf_prog *prog = container_of(to_rcu_work(work),
  237. struct cls_bpf_prog,
  238. rwork);
  239. rtnl_lock();
  240. __cls_bpf_delete_prog(prog);
  241. rtnl_unlock();
  242. }
  243. static void __cls_bpf_delete(struct tcf_proto *tp, struct cls_bpf_prog *prog,
  244. struct netlink_ext_ack *extack)
  245. {
  246. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  247. idr_remove(&head->handle_idr, prog->handle);
  248. cls_bpf_stop_offload(tp, prog, extack);
  249. list_del_rcu(&prog->link);
  250. tcf_unbind_filter(tp, &prog->res);
  251. if (tcf_exts_get_net(&prog->exts))
  252. tcf_queue_work(&prog->rwork, cls_bpf_delete_prog_work);
  253. else
  254. __cls_bpf_delete_prog(prog);
  255. }
  256. static int cls_bpf_delete(struct tcf_proto *tp, void *arg, bool *last,
  257. bool rtnl_held, struct netlink_ext_ack *extack)
  258. {
  259. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  260. __cls_bpf_delete(tp, arg, extack);
  261. *last = list_empty(&head->plist);
  262. return 0;
  263. }
  264. static void cls_bpf_destroy(struct tcf_proto *tp, bool rtnl_held,
  265. struct netlink_ext_ack *extack)
  266. {
  267. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  268. struct cls_bpf_prog *prog, *tmp;
  269. list_for_each_entry_safe(prog, tmp, &head->plist, link)
  270. __cls_bpf_delete(tp, prog, extack);
  271. idr_destroy(&head->handle_idr);
  272. kfree_rcu(head, rcu);
  273. }
  274. static void *cls_bpf_get(struct tcf_proto *tp, u32 handle)
  275. {
  276. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  277. struct cls_bpf_prog *prog;
  278. list_for_each_entry(prog, &head->plist, link) {
  279. if (prog->handle == handle)
  280. return prog;
  281. }
  282. return NULL;
  283. }
  284. static int cls_bpf_prog_from_ops(struct nlattr **tb, struct cls_bpf_prog *prog)
  285. {
  286. struct sock_filter *bpf_ops;
  287. struct sock_fprog_kern fprog_tmp;
  288. struct bpf_prog *fp;
  289. u16 bpf_size, bpf_num_ops;
  290. int ret;
  291. bpf_num_ops = nla_get_u16(tb[TCA_BPF_OPS_LEN]);
  292. if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0)
  293. return -EINVAL;
  294. bpf_size = bpf_num_ops * sizeof(*bpf_ops);
  295. if (bpf_size != nla_len(tb[TCA_BPF_OPS]))
  296. return -EINVAL;
  297. bpf_ops = kmemdup(nla_data(tb[TCA_BPF_OPS]), bpf_size, GFP_KERNEL);
  298. if (bpf_ops == NULL)
  299. return -ENOMEM;
  300. fprog_tmp.len = bpf_num_ops;
  301. fprog_tmp.filter = bpf_ops;
  302. ret = bpf_prog_create(&fp, &fprog_tmp);
  303. if (ret < 0) {
  304. kfree(bpf_ops);
  305. return ret;
  306. }
  307. prog->bpf_ops = bpf_ops;
  308. prog->bpf_num_ops = bpf_num_ops;
  309. prog->bpf_name = NULL;
  310. prog->filter = fp;
  311. return 0;
  312. }
  313. static int cls_bpf_prog_from_efd(struct nlattr **tb, struct cls_bpf_prog *prog,
  314. u32 gen_flags, const struct tcf_proto *tp)
  315. {
  316. struct bpf_prog *fp;
  317. char *name = NULL;
  318. bool skip_sw;
  319. u32 bpf_fd;
  320. bpf_fd = nla_get_u32(tb[TCA_BPF_FD]);
  321. skip_sw = gen_flags & TCA_CLS_FLAGS_SKIP_SW;
  322. fp = bpf_prog_get_type_dev(bpf_fd, BPF_PROG_TYPE_SCHED_CLS, skip_sw);
  323. if (IS_ERR(fp))
  324. return PTR_ERR(fp);
  325. if (tb[TCA_BPF_NAME]) {
  326. name = nla_memdup(tb[TCA_BPF_NAME], GFP_KERNEL);
  327. if (!name) {
  328. bpf_prog_put(fp);
  329. return -ENOMEM;
  330. }
  331. }
  332. prog->bpf_ops = NULL;
  333. prog->bpf_name = name;
  334. prog->filter = fp;
  335. if (fp->dst_needed)
  336. tcf_block_netif_keep_dst(tp->chain->block);
  337. return 0;
  338. }
  339. static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
  340. struct tcf_proto *tp, unsigned long base,
  341. u32 handle, struct nlattr **tca,
  342. void **arg, u32 flags,
  343. struct netlink_ext_ack *extack)
  344. {
  345. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  346. bool is_bpf, is_ebpf, have_exts = false;
  347. struct cls_bpf_prog *oldprog = *arg;
  348. struct nlattr *tb[TCA_BPF_MAX + 1];
  349. bool bound_to_filter = false;
  350. struct cls_bpf_prog *prog;
  351. u32 gen_flags = 0;
  352. int ret;
  353. if (tca[TCA_OPTIONS] == NULL)
  354. return -EINVAL;
  355. ret = nla_parse_nested_deprecated(tb, TCA_BPF_MAX, tca[TCA_OPTIONS],
  356. bpf_policy, NULL);
  357. if (ret < 0)
  358. return ret;
  359. prog = kzalloc(sizeof(*prog), GFP_KERNEL);
  360. if (!prog)
  361. return -ENOBUFS;
  362. ret = tcf_exts_init(&prog->exts, net, TCA_BPF_ACT, TCA_BPF_POLICE);
  363. if (ret < 0)
  364. goto errout;
  365. if (oldprog) {
  366. if (handle && oldprog->handle != handle) {
  367. ret = -EINVAL;
  368. goto errout;
  369. }
  370. }
  371. if (handle == 0) {
  372. handle = 1;
  373. ret = idr_alloc_u32(&head->handle_idr, prog, &handle,
  374. INT_MAX, GFP_KERNEL);
  375. } else if (!oldprog) {
  376. ret = idr_alloc_u32(&head->handle_idr, prog, &handle,
  377. handle, GFP_KERNEL);
  378. }
  379. if (ret)
  380. goto errout;
  381. prog->handle = handle;
  382. is_bpf = tb[TCA_BPF_OPS_LEN] && tb[TCA_BPF_OPS];
  383. is_ebpf = tb[TCA_BPF_FD];
  384. if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf)) {
  385. ret = -EINVAL;
  386. goto errout_idr;
  387. }
  388. ret = tcf_exts_validate(net, tp, tb, tca[TCA_RATE], &prog->exts,
  389. flags, extack);
  390. if (ret < 0)
  391. goto errout_idr;
  392. if (tb[TCA_BPF_FLAGS]) {
  393. u32 bpf_flags = nla_get_u32(tb[TCA_BPF_FLAGS]);
  394. if (bpf_flags & ~TCA_BPF_FLAG_ACT_DIRECT) {
  395. ret = -EINVAL;
  396. goto errout_idr;
  397. }
  398. have_exts = bpf_flags & TCA_BPF_FLAG_ACT_DIRECT;
  399. }
  400. if (tb[TCA_BPF_FLAGS_GEN]) {
  401. gen_flags = nla_get_u32(tb[TCA_BPF_FLAGS_GEN]);
  402. if (gen_flags & ~CLS_BPF_SUPPORTED_GEN_FLAGS ||
  403. !tc_flags_valid(gen_flags)) {
  404. ret = -EINVAL;
  405. goto errout_idr;
  406. }
  407. }
  408. prog->exts_integrated = have_exts;
  409. prog->gen_flags = gen_flags;
  410. ret = is_bpf ? cls_bpf_prog_from_ops(tb, prog) :
  411. cls_bpf_prog_from_efd(tb, prog, gen_flags, tp);
  412. if (ret < 0)
  413. goto errout_idr;
  414. if (tb[TCA_BPF_CLASSID]) {
  415. prog->res.classid = nla_get_u32(tb[TCA_BPF_CLASSID]);
  416. tcf_bind_filter(tp, &prog->res, base);
  417. bound_to_filter = true;
  418. }
  419. ret = cls_bpf_offload(tp, prog, oldprog, extack);
  420. if (ret)
  421. goto errout_parms;
  422. if (!tc_in_hw(prog->gen_flags))
  423. prog->gen_flags |= TCA_CLS_FLAGS_NOT_IN_HW;
  424. tcf_proto_update_usesw(tp, prog->gen_flags);
  425. if (oldprog) {
  426. idr_replace(&head->handle_idr, prog, handle);
  427. list_replace_rcu(&oldprog->link, &prog->link);
  428. tcf_unbind_filter(tp, &oldprog->res);
  429. tcf_exts_get_net(&oldprog->exts);
  430. tcf_queue_work(&oldprog->rwork, cls_bpf_delete_prog_work);
  431. } else {
  432. list_add_rcu(&prog->link, &head->plist);
  433. }
  434. *arg = prog;
  435. return 0;
  436. errout_parms:
  437. if (bound_to_filter)
  438. tcf_unbind_filter(tp, &prog->res);
  439. cls_bpf_free_parms(prog);
  440. errout_idr:
  441. if (!oldprog)
  442. idr_remove(&head->handle_idr, prog->handle);
  443. errout:
  444. tcf_exts_destroy(&prog->exts);
  445. kfree(prog);
  446. return ret;
  447. }
  448. static int cls_bpf_dump_bpf_info(const struct cls_bpf_prog *prog,
  449. struct sk_buff *skb)
  450. {
  451. struct nlattr *nla;
  452. if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_num_ops))
  453. return -EMSGSIZE;
  454. nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_num_ops *
  455. sizeof(struct sock_filter));
  456. if (nla == NULL)
  457. return -EMSGSIZE;
  458. memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
  459. return 0;
  460. }
  461. static int cls_bpf_dump_ebpf_info(const struct cls_bpf_prog *prog,
  462. struct sk_buff *skb)
  463. {
  464. struct nlattr *nla;
  465. if (prog->bpf_name &&
  466. nla_put_string(skb, TCA_BPF_NAME, prog->bpf_name))
  467. return -EMSGSIZE;
  468. if (nla_put_u32(skb, TCA_BPF_ID, prog->filter->aux->id))
  469. return -EMSGSIZE;
  470. nla = nla_reserve(skb, TCA_BPF_TAG, sizeof(prog->filter->tag));
  471. if (nla == NULL)
  472. return -EMSGSIZE;
  473. memcpy(nla_data(nla), prog->filter->tag, nla_len(nla));
  474. return 0;
  475. }
  476. static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, void *fh,
  477. struct sk_buff *skb, struct tcmsg *tm, bool rtnl_held)
  478. {
  479. struct cls_bpf_prog *prog = fh;
  480. struct nlattr *nest;
  481. u32 bpf_flags = 0;
  482. int ret;
  483. if (prog == NULL)
  484. return skb->len;
  485. tm->tcm_handle = prog->handle;
  486. cls_bpf_offload_update_stats(tp, prog);
  487. nest = nla_nest_start_noflag(skb, TCA_OPTIONS);
  488. if (nest == NULL)
  489. goto nla_put_failure;
  490. if (prog->res.classid &&
  491. nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid))
  492. goto nla_put_failure;
  493. if (cls_bpf_is_ebpf(prog))
  494. ret = cls_bpf_dump_ebpf_info(prog, skb);
  495. else
  496. ret = cls_bpf_dump_bpf_info(prog, skb);
  497. if (ret)
  498. goto nla_put_failure;
  499. if (tcf_exts_dump(skb, &prog->exts) < 0)
  500. goto nla_put_failure;
  501. if (prog->exts_integrated)
  502. bpf_flags |= TCA_BPF_FLAG_ACT_DIRECT;
  503. if (bpf_flags && nla_put_u32(skb, TCA_BPF_FLAGS, bpf_flags))
  504. goto nla_put_failure;
  505. if (prog->gen_flags &&
  506. nla_put_u32(skb, TCA_BPF_FLAGS_GEN, prog->gen_flags))
  507. goto nla_put_failure;
  508. nla_nest_end(skb, nest);
  509. if (tcf_exts_dump_stats(skb, &prog->exts) < 0)
  510. goto nla_put_failure;
  511. return skb->len;
  512. nla_put_failure:
  513. nla_nest_cancel(skb, nest);
  514. return -1;
  515. }
  516. static void cls_bpf_bind_class(void *fh, u32 classid, unsigned long cl,
  517. void *q, unsigned long base)
  518. {
  519. struct cls_bpf_prog *prog = fh;
  520. tc_cls_bind_class(classid, cl, q, &prog->res, base);
  521. }
  522. static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg,
  523. bool rtnl_held)
  524. {
  525. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  526. struct cls_bpf_prog *prog;
  527. list_for_each_entry(prog, &head->plist, link) {
  528. if (!tc_cls_stats_dump(tp, arg, prog))
  529. break;
  530. }
  531. }
  532. static int cls_bpf_reoffload(struct tcf_proto *tp, bool add, flow_setup_cb_t *cb,
  533. void *cb_priv, struct netlink_ext_ack *extack)
  534. {
  535. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  536. struct tcf_block *block = tp->chain->block;
  537. struct tc_cls_bpf_offload cls_bpf = {};
  538. struct cls_bpf_prog *prog;
  539. int err;
  540. list_for_each_entry(prog, &head->plist, link) {
  541. if (tc_skip_hw(prog->gen_flags))
  542. continue;
  543. tc_cls_common_offload_init(&cls_bpf.common, tp, prog->gen_flags,
  544. extack);
  545. cls_bpf.command = TC_CLSBPF_OFFLOAD;
  546. cls_bpf.exts = &prog->exts;
  547. cls_bpf.prog = add ? prog->filter : NULL;
  548. cls_bpf.oldprog = add ? NULL : prog->filter;
  549. cls_bpf.name = prog->bpf_name;
  550. cls_bpf.exts_integrated = prog->exts_integrated;
  551. err = tc_setup_cb_reoffload(block, tp, add, cb, TC_SETUP_CLSBPF,
  552. &cls_bpf, cb_priv, &prog->gen_flags,
  553. &prog->in_hw_count);
  554. if (err)
  555. return err;
  556. }
  557. return 0;
  558. }
  559. static struct tcf_proto_ops cls_bpf_ops __read_mostly = {
  560. .kind = "bpf",
  561. .owner = THIS_MODULE,
  562. .classify = cls_bpf_classify,
  563. .init = cls_bpf_init,
  564. .destroy = cls_bpf_destroy,
  565. .get = cls_bpf_get,
  566. .change = cls_bpf_change,
  567. .delete = cls_bpf_delete,
  568. .walk = cls_bpf_walk,
  569. .reoffload = cls_bpf_reoffload,
  570. .dump = cls_bpf_dump,
  571. .bind_class = cls_bpf_bind_class,
  572. };
  573. MODULE_ALIAS_NET_CLS("bpf");
  574. static int __init cls_bpf_init_mod(void)
  575. {
  576. return register_tcf_proto_ops(&cls_bpf_ops);
  577. }
  578. static void __exit cls_bpf_exit_mod(void)
  579. {
  580. unregister_tcf_proto_ops(&cls_bpf_ops);
  581. }
  582. module_init(cls_bpf_init_mod);
  583. module_exit(cls_bpf_exit_mod);