cls_bpf.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  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 = kmemdup(nla_data(tb[TCA_BPF_OPS]), bpf_size, GFP_KERNEL);
  295. if (bpf_ops == NULL)
  296. return -ENOMEM;
  297. fprog_tmp.len = bpf_num_ops;
  298. fprog_tmp.filter = bpf_ops;
  299. ret = bpf_prog_create(&fp, &fprog_tmp);
  300. if (ret < 0) {
  301. kfree(bpf_ops);
  302. return ret;
  303. }
  304. prog->bpf_ops = bpf_ops;
  305. prog->bpf_num_ops = bpf_num_ops;
  306. prog->bpf_name = NULL;
  307. prog->filter = fp;
  308. return 0;
  309. }
  310. static int cls_bpf_prog_from_efd(struct nlattr **tb, struct cls_bpf_prog *prog,
  311. u32 gen_flags, const struct tcf_proto *tp)
  312. {
  313. struct bpf_prog *fp;
  314. char *name = NULL;
  315. bool skip_sw;
  316. u32 bpf_fd;
  317. bpf_fd = nla_get_u32(tb[TCA_BPF_FD]);
  318. skip_sw = gen_flags & TCA_CLS_FLAGS_SKIP_SW;
  319. fp = bpf_prog_get_type_dev(bpf_fd, BPF_PROG_TYPE_SCHED_CLS, skip_sw);
  320. if (IS_ERR(fp))
  321. return PTR_ERR(fp);
  322. if (tb[TCA_BPF_NAME]) {
  323. name = nla_memdup(tb[TCA_BPF_NAME], GFP_KERNEL);
  324. if (!name) {
  325. bpf_prog_put(fp);
  326. return -ENOMEM;
  327. }
  328. }
  329. prog->bpf_ops = NULL;
  330. prog->bpf_name = name;
  331. prog->filter = fp;
  332. if (fp->dst_needed)
  333. tcf_block_netif_keep_dst(tp->chain->block);
  334. return 0;
  335. }
  336. static int cls_bpf_set_parms(struct net *net, struct tcf_proto *tp,
  337. struct cls_bpf_prog *prog, unsigned long base,
  338. struct nlattr **tb, struct nlattr *est, bool ovr,
  339. struct netlink_ext_ack *extack)
  340. {
  341. bool is_bpf, is_ebpf, have_exts = false;
  342. u32 gen_flags = 0;
  343. int ret;
  344. is_bpf = tb[TCA_BPF_OPS_LEN] && tb[TCA_BPF_OPS];
  345. is_ebpf = tb[TCA_BPF_FD];
  346. if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf))
  347. return -EINVAL;
  348. ret = tcf_exts_validate(net, tp, tb, est, &prog->exts, ovr, extack);
  349. if (ret < 0)
  350. return ret;
  351. if (tb[TCA_BPF_FLAGS]) {
  352. u32 bpf_flags = nla_get_u32(tb[TCA_BPF_FLAGS]);
  353. if (bpf_flags & ~TCA_BPF_FLAG_ACT_DIRECT)
  354. return -EINVAL;
  355. have_exts = bpf_flags & TCA_BPF_FLAG_ACT_DIRECT;
  356. }
  357. if (tb[TCA_BPF_FLAGS_GEN]) {
  358. gen_flags = nla_get_u32(tb[TCA_BPF_FLAGS_GEN]);
  359. if (gen_flags & ~CLS_BPF_SUPPORTED_GEN_FLAGS ||
  360. !tc_flags_valid(gen_flags))
  361. return -EINVAL;
  362. }
  363. prog->exts_integrated = have_exts;
  364. prog->gen_flags = gen_flags;
  365. ret = is_bpf ? cls_bpf_prog_from_ops(tb, prog) :
  366. cls_bpf_prog_from_efd(tb, prog, gen_flags, tp);
  367. if (ret < 0)
  368. return ret;
  369. if (tb[TCA_BPF_CLASSID]) {
  370. prog->res.classid = nla_get_u32(tb[TCA_BPF_CLASSID]);
  371. tcf_bind_filter(tp, &prog->res, base);
  372. }
  373. return 0;
  374. }
  375. static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
  376. struct tcf_proto *tp, unsigned long base,
  377. u32 handle, struct nlattr **tca,
  378. void **arg, bool ovr, struct netlink_ext_ack *extack)
  379. {
  380. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  381. struct cls_bpf_prog *oldprog = *arg;
  382. struct nlattr *tb[TCA_BPF_MAX + 1];
  383. struct cls_bpf_prog *prog;
  384. int ret;
  385. if (tca[TCA_OPTIONS] == NULL)
  386. return -EINVAL;
  387. ret = nla_parse_nested(tb, TCA_BPF_MAX, tca[TCA_OPTIONS], bpf_policy,
  388. NULL);
  389. if (ret < 0)
  390. return ret;
  391. prog = kzalloc(sizeof(*prog), GFP_KERNEL);
  392. if (!prog)
  393. return -ENOBUFS;
  394. ret = tcf_exts_init(&prog->exts, TCA_BPF_ACT, TCA_BPF_POLICE);
  395. if (ret < 0)
  396. goto errout;
  397. if (oldprog) {
  398. if (handle && oldprog->handle != handle) {
  399. ret = -EINVAL;
  400. goto errout;
  401. }
  402. }
  403. if (handle == 0) {
  404. handle = 1;
  405. ret = idr_alloc_u32(&head->handle_idr, prog, &handle,
  406. INT_MAX, GFP_KERNEL);
  407. } else if (!oldprog) {
  408. ret = idr_alloc_u32(&head->handle_idr, prog, &handle,
  409. handle, GFP_KERNEL);
  410. }
  411. if (ret)
  412. goto errout;
  413. prog->handle = handle;
  414. ret = cls_bpf_set_parms(net, tp, prog, base, tb, tca[TCA_RATE], ovr,
  415. extack);
  416. if (ret < 0)
  417. goto errout_idr;
  418. ret = cls_bpf_offload(tp, prog, oldprog, extack);
  419. if (ret)
  420. goto errout_parms;
  421. if (!tc_in_hw(prog->gen_flags))
  422. prog->gen_flags |= TCA_CLS_FLAGS_NOT_IN_HW;
  423. if (oldprog) {
  424. idr_replace(&head->handle_idr, prog, handle);
  425. list_replace_rcu(&oldprog->link, &prog->link);
  426. tcf_unbind_filter(tp, &oldprog->res);
  427. tcf_exts_get_net(&oldprog->exts);
  428. tcf_queue_work(&oldprog->rwork, cls_bpf_delete_prog_work);
  429. } else {
  430. list_add_rcu(&prog->link, &head->plist);
  431. }
  432. *arg = prog;
  433. return 0;
  434. errout_parms:
  435. cls_bpf_free_parms(prog);
  436. errout_idr:
  437. if (!oldprog)
  438. idr_remove(&head->handle_idr, prog->handle);
  439. errout:
  440. tcf_exts_destroy(&prog->exts);
  441. kfree(prog);
  442. return ret;
  443. }
  444. static int cls_bpf_dump_bpf_info(const struct cls_bpf_prog *prog,
  445. struct sk_buff *skb)
  446. {
  447. struct nlattr *nla;
  448. if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_num_ops))
  449. return -EMSGSIZE;
  450. nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_num_ops *
  451. sizeof(struct sock_filter));
  452. if (nla == NULL)
  453. return -EMSGSIZE;
  454. memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
  455. return 0;
  456. }
  457. static int cls_bpf_dump_ebpf_info(const struct cls_bpf_prog *prog,
  458. struct sk_buff *skb)
  459. {
  460. struct nlattr *nla;
  461. if (prog->bpf_name &&
  462. nla_put_string(skb, TCA_BPF_NAME, prog->bpf_name))
  463. return -EMSGSIZE;
  464. if (nla_put_u32(skb, TCA_BPF_ID, prog->filter->aux->id))
  465. return -EMSGSIZE;
  466. nla = nla_reserve(skb, TCA_BPF_TAG, sizeof(prog->filter->tag));
  467. if (nla == NULL)
  468. return -EMSGSIZE;
  469. memcpy(nla_data(nla), prog->filter->tag, nla_len(nla));
  470. return 0;
  471. }
  472. static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, void *fh,
  473. struct sk_buff *skb, struct tcmsg *tm)
  474. {
  475. struct cls_bpf_prog *prog = fh;
  476. struct nlattr *nest;
  477. u32 bpf_flags = 0;
  478. int ret;
  479. if (prog == NULL)
  480. return skb->len;
  481. tm->tcm_handle = prog->handle;
  482. cls_bpf_offload_update_stats(tp, prog);
  483. nest = nla_nest_start(skb, TCA_OPTIONS);
  484. if (nest == NULL)
  485. goto nla_put_failure;
  486. if (prog->res.classid &&
  487. nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid))
  488. goto nla_put_failure;
  489. if (cls_bpf_is_ebpf(prog))
  490. ret = cls_bpf_dump_ebpf_info(prog, skb);
  491. else
  492. ret = cls_bpf_dump_bpf_info(prog, skb);
  493. if (ret)
  494. goto nla_put_failure;
  495. if (tcf_exts_dump(skb, &prog->exts) < 0)
  496. goto nla_put_failure;
  497. if (prog->exts_integrated)
  498. bpf_flags |= TCA_BPF_FLAG_ACT_DIRECT;
  499. if (bpf_flags && nla_put_u32(skb, TCA_BPF_FLAGS, bpf_flags))
  500. goto nla_put_failure;
  501. if (prog->gen_flags &&
  502. nla_put_u32(skb, TCA_BPF_FLAGS_GEN, prog->gen_flags))
  503. goto nla_put_failure;
  504. nla_nest_end(skb, nest);
  505. if (tcf_exts_dump_stats(skb, &prog->exts) < 0)
  506. goto nla_put_failure;
  507. return skb->len;
  508. nla_put_failure:
  509. nla_nest_cancel(skb, nest);
  510. return -1;
  511. }
  512. static void cls_bpf_bind_class(void *fh, u32 classid, unsigned long cl,
  513. void *q, unsigned long base)
  514. {
  515. struct cls_bpf_prog *prog = fh;
  516. if (prog && prog->res.classid == classid) {
  517. if (cl)
  518. __tcf_bind_filter(q, &prog->res, base);
  519. else
  520. __tcf_unbind_filter(q, &prog->res);
  521. }
  522. }
  523. static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  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 (arg->count < arg->skip)
  529. goto skip;
  530. if (arg->fn(tp, prog, arg) < 0) {
  531. arg->stop = 1;
  532. break;
  533. }
  534. skip:
  535. arg->count++;
  536. }
  537. }
  538. static int cls_bpf_reoffload(struct tcf_proto *tp, bool add, tc_setup_cb_t *cb,
  539. void *cb_priv, struct netlink_ext_ack *extack)
  540. {
  541. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  542. struct tcf_block *block = tp->chain->block;
  543. struct tc_cls_bpf_offload cls_bpf = {};
  544. struct cls_bpf_prog *prog;
  545. int err;
  546. list_for_each_entry(prog, &head->plist, link) {
  547. if (tc_skip_hw(prog->gen_flags))
  548. continue;
  549. tc_cls_common_offload_init(&cls_bpf.common, tp, prog->gen_flags,
  550. extack);
  551. cls_bpf.command = TC_CLSBPF_OFFLOAD;
  552. cls_bpf.exts = &prog->exts;
  553. cls_bpf.prog = add ? prog->filter : NULL;
  554. cls_bpf.oldprog = add ? NULL : prog->filter;
  555. cls_bpf.name = prog->bpf_name;
  556. cls_bpf.exts_integrated = prog->exts_integrated;
  557. err = cb(TC_SETUP_CLSBPF, &cls_bpf, cb_priv);
  558. if (err) {
  559. if (add && tc_skip_sw(prog->gen_flags))
  560. return err;
  561. continue;
  562. }
  563. tc_cls_offload_cnt_update(block, &prog->in_hw_count,
  564. &prog->gen_flags, add);
  565. }
  566. return 0;
  567. }
  568. static struct tcf_proto_ops cls_bpf_ops __read_mostly = {
  569. .kind = "bpf",
  570. .owner = THIS_MODULE,
  571. .classify = cls_bpf_classify,
  572. .init = cls_bpf_init,
  573. .destroy = cls_bpf_destroy,
  574. .get = cls_bpf_get,
  575. .change = cls_bpf_change,
  576. .delete = cls_bpf_delete,
  577. .walk = cls_bpf_walk,
  578. .reoffload = cls_bpf_reoffload,
  579. .dump = cls_bpf_dump,
  580. .bind_class = cls_bpf_bind_class,
  581. };
  582. static int __init cls_bpf_init_mod(void)
  583. {
  584. return register_tcf_proto_ops(&cls_bpf_ops);
  585. }
  586. static void __exit cls_bpf_exit_mod(void)
  587. {
  588. unregister_tcf_proto_ops(&cls_bpf_ops);
  589. }
  590. module_init(cls_bpf_init_mod);
  591. module_exit(cls_bpf_exit_mod);