cls_bpf.c 15 KB

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