cls_bpf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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. #include <linux/module.h>
  15. #include <linux/types.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/filter.h>
  18. #include <linux/bpf.h>
  19. #include <linux/idr.h>
  20. #include <net/rtnetlink.h>
  21. #include <net/pkt_cls.h>
  22. #include <net/sock.h>
  23. MODULE_LICENSE("GPL");
  24. MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
  25. MODULE_DESCRIPTION("TC BPF based classifier");
  26. #define CLS_BPF_NAME_LEN 256
  27. #define CLS_BPF_SUPPORTED_GEN_FLAGS \
  28. (TCA_CLS_FLAGS_SKIP_HW | TCA_CLS_FLAGS_SKIP_SW)
  29. struct cls_bpf_head {
  30. struct list_head plist;
  31. struct idr handle_idr;
  32. struct rcu_head rcu;
  33. };
  34. struct cls_bpf_prog {
  35. struct bpf_prog *filter;
  36. struct list_head link;
  37. struct tcf_result res;
  38. bool exts_integrated;
  39. u32 gen_flags;
  40. unsigned int in_hw_count;
  41. struct tcf_exts exts;
  42. u32 handle;
  43. u16 bpf_num_ops;
  44. struct sock_filter *bpf_ops;
  45. const char *bpf_name;
  46. struct tcf_proto *tp;
  47. struct rcu_work rwork;
  48. };
  49. static const struct nla_policy bpf_policy[TCA_BPF_MAX + 1] = {
  50. [TCA_BPF_CLASSID] = { .type = NLA_U32 },
  51. [TCA_BPF_FLAGS] = { .type = NLA_U32 },
  52. [TCA_BPF_FLAGS_GEN] = { .type = NLA_U32 },
  53. [TCA_BPF_FD] = { .type = NLA_U32 },
  54. [TCA_BPF_NAME] = { .type = NLA_NUL_STRING,
  55. .len = CLS_BPF_NAME_LEN },
  56. [TCA_BPF_OPS_LEN] = { .type = NLA_U16 },
  57. [TCA_BPF_OPS] = { .type = NLA_BINARY,
  58. .len = sizeof(struct sock_filter) * BPF_MAXINSNS },
  59. };
  60. static int cls_bpf_exec_opcode(int code)
  61. {
  62. switch (code) {
  63. case TC_ACT_OK:
  64. case TC_ACT_SHOT:
  65. case TC_ACT_STOLEN:
  66. case TC_ACT_TRAP:
  67. case TC_ACT_REDIRECT:
  68. case TC_ACT_UNSPEC:
  69. return code;
  70. default:
  71. return TC_ACT_UNSPEC;
  72. }
  73. }
  74. static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  75. struct tcf_result *res)
  76. {
  77. struct cls_bpf_head *head = rcu_dereference_bh(tp->root);
  78. bool at_ingress = skb_at_tc_ingress(skb);
  79. struct cls_bpf_prog *prog;
  80. int ret = -1;
  81. /* Needed here for accessing maps. */
  82. rcu_read_lock();
  83. list_for_each_entry_rcu(prog, &head->plist, link) {
  84. int filter_res;
  85. qdisc_skb_cb(skb)->tc_classid = prog->res.classid;
  86. if (tc_skip_sw(prog->gen_flags)) {
  87. filter_res = prog->exts_integrated ? TC_ACT_UNSPEC : 0;
  88. } else if (at_ingress) {
  89. /* It is safe to push/pull even if skb_shared() */
  90. __skb_push(skb, skb->mac_len);
  91. bpf_compute_data_pointers(skb);
  92. filter_res = BPF_PROG_RUN(prog->filter, skb);
  93. __skb_pull(skb, skb->mac_len);
  94. } else {
  95. bpf_compute_data_pointers(skb);
  96. filter_res = BPF_PROG_RUN(prog->filter, skb);
  97. }
  98. if (prog->exts_integrated) {
  99. res->class = 0;
  100. res->classid = TC_H_MAJ(prog->res.classid) |
  101. qdisc_skb_cb(skb)->tc_classid;
  102. ret = cls_bpf_exec_opcode(filter_res);
  103. if (ret == TC_ACT_UNSPEC)
  104. continue;
  105. break;
  106. }
  107. if (filter_res == 0)
  108. continue;
  109. if (filter_res != -1) {
  110. res->class = 0;
  111. res->classid = filter_res;
  112. } else {
  113. *res = prog->res;
  114. }
  115. ret = tcf_exts_exec(skb, &prog->exts, res);
  116. if (ret < 0)
  117. continue;
  118. break;
  119. }
  120. rcu_read_unlock();
  121. return ret;
  122. }
  123. static bool cls_bpf_is_ebpf(const struct cls_bpf_prog *prog)
  124. {
  125. return !prog->bpf_ops;
  126. }
  127. static int cls_bpf_offload_cmd(struct tcf_proto *tp, struct cls_bpf_prog *prog,
  128. struct cls_bpf_prog *oldprog,
  129. struct netlink_ext_ack *extack)
  130. {
  131. struct tcf_block *block = tp->chain->block;
  132. struct tc_cls_bpf_offload cls_bpf = {};
  133. struct cls_bpf_prog *obj;
  134. bool skip_sw;
  135. int err;
  136. skip_sw = prog && tc_skip_sw(prog->gen_flags);
  137. obj = prog ?: oldprog;
  138. tc_cls_common_offload_init(&cls_bpf.common, tp, obj->gen_flags,
  139. extack);
  140. cls_bpf.command = TC_CLSBPF_OFFLOAD;
  141. cls_bpf.exts = &obj->exts;
  142. cls_bpf.prog = prog ? prog->filter : NULL;
  143. cls_bpf.oldprog = oldprog ? oldprog->filter : NULL;
  144. cls_bpf.name = obj->bpf_name;
  145. cls_bpf.exts_integrated = obj->exts_integrated;
  146. if (oldprog)
  147. tcf_block_offload_dec(block, &oldprog->gen_flags);
  148. err = tc_setup_cb_call(block, NULL, TC_SETUP_CLSBPF, &cls_bpf, skip_sw);
  149. if (prog) {
  150. if (err < 0) {
  151. cls_bpf_offload_cmd(tp, oldprog, prog, extack);
  152. return err;
  153. } else if (err > 0) {
  154. prog->in_hw_count = err;
  155. tcf_block_offload_inc(block, &prog->gen_flags);
  156. }
  157. }
  158. if (prog && skip_sw && !(prog->gen_flags & TCA_CLS_FLAGS_IN_HW))
  159. return -EINVAL;
  160. return 0;
  161. }
  162. static u32 cls_bpf_flags(u32 flags)
  163. {
  164. return flags & CLS_BPF_SUPPORTED_GEN_FLAGS;
  165. }
  166. static int cls_bpf_offload(struct tcf_proto *tp, struct cls_bpf_prog *prog,
  167. struct cls_bpf_prog *oldprog,
  168. struct netlink_ext_ack *extack)
  169. {
  170. if (prog && oldprog &&
  171. cls_bpf_flags(prog->gen_flags) !=
  172. cls_bpf_flags(oldprog->gen_flags))
  173. return -EINVAL;
  174. if (prog && tc_skip_hw(prog->gen_flags))
  175. prog = NULL;
  176. if (oldprog && tc_skip_hw(oldprog->gen_flags))
  177. oldprog = NULL;
  178. if (!prog && !oldprog)
  179. return 0;
  180. return cls_bpf_offload_cmd(tp, prog, oldprog, extack);
  181. }
  182. static void cls_bpf_stop_offload(struct tcf_proto *tp,
  183. struct cls_bpf_prog *prog,
  184. struct netlink_ext_ack *extack)
  185. {
  186. int err;
  187. err = cls_bpf_offload_cmd(tp, NULL, prog, extack);
  188. if (err)
  189. pr_err("Stopping hardware offload failed: %d\n", err);
  190. }
  191. static void cls_bpf_offload_update_stats(struct tcf_proto *tp,
  192. struct cls_bpf_prog *prog)
  193. {
  194. struct tcf_block *block = tp->chain->block;
  195. struct tc_cls_bpf_offload cls_bpf = {};
  196. tc_cls_common_offload_init(&cls_bpf.common, tp, prog->gen_flags, NULL);
  197. cls_bpf.command = TC_CLSBPF_STATS;
  198. cls_bpf.exts = &prog->exts;
  199. cls_bpf.prog = prog->filter;
  200. cls_bpf.name = prog->bpf_name;
  201. cls_bpf.exts_integrated = prog->exts_integrated;
  202. tc_setup_cb_call(block, NULL, TC_SETUP_CLSBPF, &cls_bpf, false);
  203. }
  204. static int cls_bpf_init(struct tcf_proto *tp)
  205. {
  206. struct cls_bpf_head *head;
  207. head = kzalloc(sizeof(*head), GFP_KERNEL);
  208. if (head == NULL)
  209. return -ENOBUFS;
  210. INIT_LIST_HEAD_RCU(&head->plist);
  211. idr_init(&head->handle_idr);
  212. rcu_assign_pointer(tp->root, head);
  213. return 0;
  214. }
  215. static void cls_bpf_free_parms(struct cls_bpf_prog *prog)
  216. {
  217. if (cls_bpf_is_ebpf(prog))
  218. bpf_prog_put(prog->filter);
  219. else
  220. bpf_prog_destroy(prog->filter);
  221. kfree(prog->bpf_name);
  222. kfree(prog->bpf_ops);
  223. }
  224. static void __cls_bpf_delete_prog(struct cls_bpf_prog *prog)
  225. {
  226. tcf_exts_destroy(&prog->exts);
  227. tcf_exts_put_net(&prog->exts);
  228. cls_bpf_free_parms(prog);
  229. kfree(prog);
  230. }
  231. static void cls_bpf_delete_prog_work(struct work_struct *work)
  232. {
  233. struct cls_bpf_prog *prog = container_of(to_rcu_work(work),
  234. struct cls_bpf_prog,
  235. rwork);
  236. rtnl_lock();
  237. __cls_bpf_delete_prog(prog);
  238. rtnl_unlock();
  239. }
  240. static void __cls_bpf_delete(struct tcf_proto *tp, struct cls_bpf_prog *prog,
  241. struct netlink_ext_ack *extack)
  242. {
  243. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  244. idr_remove(&head->handle_idr, prog->handle);
  245. cls_bpf_stop_offload(tp, prog, extack);
  246. list_del_rcu(&prog->link);
  247. tcf_unbind_filter(tp, &prog->res);
  248. if (tcf_exts_get_net(&prog->exts))
  249. tcf_queue_work(&prog->rwork, cls_bpf_delete_prog_work);
  250. else
  251. __cls_bpf_delete_prog(prog);
  252. }
  253. static int cls_bpf_delete(struct tcf_proto *tp, void *arg, bool *last,
  254. struct netlink_ext_ack *extack)
  255. {
  256. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  257. __cls_bpf_delete(tp, arg, extack);
  258. *last = list_empty(&head->plist);
  259. return 0;
  260. }
  261. static void cls_bpf_destroy(struct tcf_proto *tp,
  262. struct netlink_ext_ack *extack)
  263. {
  264. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  265. struct cls_bpf_prog *prog, *tmp;
  266. list_for_each_entry_safe(prog, tmp, &head->plist, link)
  267. __cls_bpf_delete(tp, prog, extack);
  268. idr_destroy(&head->handle_idr);
  269. kfree_rcu(head, rcu);
  270. }
  271. static void *cls_bpf_get(struct tcf_proto *tp, u32 handle)
  272. {
  273. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  274. struct cls_bpf_prog *prog;
  275. list_for_each_entry(prog, &head->plist, link) {
  276. if (prog->handle == handle)
  277. return prog;
  278. }
  279. return NULL;
  280. }
  281. static int cls_bpf_prog_from_ops(struct nlattr **tb, struct cls_bpf_prog *prog)
  282. {
  283. struct sock_filter *bpf_ops;
  284. struct sock_fprog_kern fprog_tmp;
  285. struct bpf_prog *fp;
  286. u16 bpf_size, bpf_num_ops;
  287. int ret;
  288. bpf_num_ops = nla_get_u16(tb[TCA_BPF_OPS_LEN]);
  289. if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0)
  290. return -EINVAL;
  291. bpf_size = bpf_num_ops * sizeof(*bpf_ops);
  292. if (bpf_size != nla_len(tb[TCA_BPF_OPS]))
  293. return -EINVAL;
  294. bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
  295. if (bpf_ops == NULL)
  296. return -ENOMEM;
  297. memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size);
  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_set_parms(struct net *net, struct tcf_proto *tp,
  338. struct cls_bpf_prog *prog, unsigned long base,
  339. struct nlattr **tb, struct nlattr *est, bool ovr,
  340. struct netlink_ext_ack *extack)
  341. {
  342. bool is_bpf, is_ebpf, have_exts = false;
  343. u32 gen_flags = 0;
  344. int ret;
  345. is_bpf = tb[TCA_BPF_OPS_LEN] && tb[TCA_BPF_OPS];
  346. is_ebpf = tb[TCA_BPF_FD];
  347. if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf))
  348. return -EINVAL;
  349. ret = tcf_exts_validate(net, tp, tb, est, &prog->exts, ovr, extack);
  350. if (ret < 0)
  351. return ret;
  352. if (tb[TCA_BPF_FLAGS]) {
  353. u32 bpf_flags = nla_get_u32(tb[TCA_BPF_FLAGS]);
  354. if (bpf_flags & ~TCA_BPF_FLAG_ACT_DIRECT)
  355. return -EINVAL;
  356. have_exts = bpf_flags & TCA_BPF_FLAG_ACT_DIRECT;
  357. }
  358. if (tb[TCA_BPF_FLAGS_GEN]) {
  359. gen_flags = nla_get_u32(tb[TCA_BPF_FLAGS_GEN]);
  360. if (gen_flags & ~CLS_BPF_SUPPORTED_GEN_FLAGS ||
  361. !tc_flags_valid(gen_flags))
  362. return -EINVAL;
  363. }
  364. prog->exts_integrated = have_exts;
  365. prog->gen_flags = gen_flags;
  366. ret = is_bpf ? cls_bpf_prog_from_ops(tb, prog) :
  367. cls_bpf_prog_from_efd(tb, prog, gen_flags, tp);
  368. if (ret < 0)
  369. return ret;
  370. if (tb[TCA_BPF_CLASSID]) {
  371. prog->res.classid = nla_get_u32(tb[TCA_BPF_CLASSID]);
  372. tcf_bind_filter(tp, &prog->res, base);
  373. }
  374. return 0;
  375. }
  376. static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
  377. struct tcf_proto *tp, unsigned long base,
  378. u32 handle, struct nlattr **tca,
  379. void **arg, bool ovr, struct netlink_ext_ack *extack)
  380. {
  381. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  382. struct cls_bpf_prog *oldprog = *arg;
  383. struct nlattr *tb[TCA_BPF_MAX + 1];
  384. struct cls_bpf_prog *prog;
  385. int ret;
  386. if (tca[TCA_OPTIONS] == NULL)
  387. return -EINVAL;
  388. ret = nla_parse_nested(tb, TCA_BPF_MAX, tca[TCA_OPTIONS], bpf_policy,
  389. NULL);
  390. if (ret < 0)
  391. return ret;
  392. prog = kzalloc(sizeof(*prog), GFP_KERNEL);
  393. if (!prog)
  394. return -ENOBUFS;
  395. ret = tcf_exts_init(&prog->exts, TCA_BPF_ACT, TCA_BPF_POLICE);
  396. if (ret < 0)
  397. goto errout;
  398. if (oldprog) {
  399. if (handle && oldprog->handle != handle) {
  400. ret = -EINVAL;
  401. goto errout;
  402. }
  403. }
  404. if (handle == 0) {
  405. handle = 1;
  406. ret = idr_alloc_u32(&head->handle_idr, prog, &handle,
  407. INT_MAX, GFP_KERNEL);
  408. } else if (!oldprog) {
  409. ret = idr_alloc_u32(&head->handle_idr, prog, &handle,
  410. handle, GFP_KERNEL);
  411. }
  412. if (ret)
  413. goto errout;
  414. prog->handle = handle;
  415. ret = cls_bpf_set_parms(net, tp, prog, base, tb, tca[TCA_RATE], ovr,
  416. extack);
  417. if (ret < 0)
  418. goto errout_idr;
  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. if (oldprog) {
  425. idr_replace(&head->handle_idr, prog, handle);
  426. list_replace_rcu(&oldprog->link, &prog->link);
  427. tcf_unbind_filter(tp, &oldprog->res);
  428. tcf_exts_get_net(&oldprog->exts);
  429. tcf_queue_work(&oldprog->rwork, cls_bpf_delete_prog_work);
  430. } else {
  431. list_add_rcu(&prog->link, &head->plist);
  432. }
  433. *arg = prog;
  434. return 0;
  435. errout_parms:
  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)
  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(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. {
  515. struct cls_bpf_prog *prog = fh;
  516. if (prog && prog->res.classid == classid)
  517. prog->res.class = cl;
  518. }
  519. static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  520. {
  521. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  522. struct cls_bpf_prog *prog;
  523. list_for_each_entry(prog, &head->plist, link) {
  524. if (arg->count < arg->skip)
  525. goto skip;
  526. if (arg->fn(tp, prog, arg) < 0) {
  527. arg->stop = 1;
  528. break;
  529. }
  530. skip:
  531. arg->count++;
  532. }
  533. }
  534. static int cls_bpf_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb,
  535. void *cb_priv, struct netlink_ext_ack *extack)
  536. {
  537. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  538. struct tcf_block *block = tp->chain->block;
  539. struct tc_cls_bpf_offload cls_bpf = {};
  540. struct cls_bpf_prog *prog;
  541. int err;
  542. list_for_each_entry(prog, &head->plist, link) {
  543. if (tc_skip_hw(prog->gen_flags))
  544. continue;
  545. tc_cls_common_offload_init(&cls_bpf.common, tp, prog->gen_flags,
  546. extack);
  547. cls_bpf.command = TC_CLSBPF_OFFLOAD;
  548. cls_bpf.exts = &prog->exts;
  549. cls_bpf.prog = add ? prog->filter : NULL;
  550. cls_bpf.oldprog = add ? NULL : prog->filter;
  551. cls_bpf.name = prog->bpf_name;
  552. cls_bpf.exts_integrated = prog->exts_integrated;
  553. err = cb(TC_SETUP_CLSBPF, &cls_bpf, cb_priv);
  554. if (err) {
  555. if (add && tc_skip_sw(prog->gen_flags))
  556. return err;
  557. continue;
  558. }
  559. tc_cls_offload_cnt_update(block, &prog->in_hw_count,
  560. &prog->gen_flags, add);
  561. }
  562. return 0;
  563. }
  564. static struct tcf_proto_ops cls_bpf_ops __read_mostly = {
  565. .kind = "bpf",
  566. .owner = THIS_MODULE,
  567. .classify = cls_bpf_classify,
  568. .init = cls_bpf_init,
  569. .destroy = cls_bpf_destroy,
  570. .get = cls_bpf_get,
  571. .change = cls_bpf_change,
  572. .delete = cls_bpf_delete,
  573. .walk = cls_bpf_walk,
  574. .reoffload = cls_bpf_reoffload,
  575. .dump = cls_bpf_dump,
  576. .bind_class = cls_bpf_bind_class,
  577. };
  578. static int __init cls_bpf_init_mod(void)
  579. {
  580. return register_tcf_proto_ops(&cls_bpf_ops);
  581. }
  582. static void __exit cls_bpf_exit_mod(void)
  583. {
  584. unregister_tcf_proto_ops(&cls_bpf_ops);
  585. }
  586. module_init(cls_bpf_init_mod);
  587. module_exit(cls_bpf_exit_mod);