cls_bpf.c 17 KB

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