cls_bpf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553
  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. struct cls_bpf_head {
  27. struct list_head plist;
  28. u32 hgen;
  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. struct tcf_exts exts;
  37. u32 handle;
  38. union {
  39. u32 bpf_fd;
  40. u16 bpf_num_ops;
  41. };
  42. struct sock_filter *bpf_ops;
  43. const char *bpf_name;
  44. struct tcf_proto *tp;
  45. struct rcu_head rcu;
  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_FD] = { .type = NLA_U32 },
  51. [TCA_BPF_NAME] = { .type = NLA_NUL_STRING, .len = CLS_BPF_NAME_LEN },
  52. [TCA_BPF_OPS_LEN] = { .type = NLA_U16 },
  53. [TCA_BPF_OPS] = { .type = NLA_BINARY,
  54. .len = sizeof(struct sock_filter) * BPF_MAXINSNS },
  55. };
  56. static int cls_bpf_exec_opcode(int code)
  57. {
  58. switch (code) {
  59. case TC_ACT_OK:
  60. case TC_ACT_SHOT:
  61. case TC_ACT_STOLEN:
  62. case TC_ACT_REDIRECT:
  63. case TC_ACT_UNSPEC:
  64. return code;
  65. default:
  66. return TC_ACT_UNSPEC;
  67. }
  68. }
  69. static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  70. struct tcf_result *res)
  71. {
  72. struct cls_bpf_head *head = rcu_dereference_bh(tp->root);
  73. struct cls_bpf_prog *prog;
  74. #ifdef CONFIG_NET_CLS_ACT
  75. bool at_ingress = G_TC_AT(skb->tc_verd) & AT_INGRESS;
  76. #else
  77. bool at_ingress = false;
  78. #endif
  79. int ret = -1;
  80. if (unlikely(!skb_mac_header_was_set(skb)))
  81. return -1;
  82. /* Needed here for accessing maps. */
  83. rcu_read_lock();
  84. list_for_each_entry_rcu(prog, &head->plist, link) {
  85. int filter_res;
  86. qdisc_skb_cb(skb)->tc_classid = prog->res.classid;
  87. if (at_ingress) {
  88. /* It is safe to push/pull even if skb_shared() */
  89. __skb_push(skb, skb->mac_len);
  90. filter_res = BPF_PROG_RUN(prog->filter, skb);
  91. __skb_pull(skb, skb->mac_len);
  92. } else {
  93. filter_res = BPF_PROG_RUN(prog->filter, skb);
  94. }
  95. if (prog->exts_integrated) {
  96. res->class = prog->res.class;
  97. res->classid = qdisc_skb_cb(skb)->tc_classid;
  98. ret = cls_bpf_exec_opcode(filter_res);
  99. if (ret == TC_ACT_UNSPEC)
  100. continue;
  101. break;
  102. }
  103. if (filter_res == 0)
  104. continue;
  105. *res = prog->res;
  106. if (filter_res != -1)
  107. res->classid = filter_res;
  108. ret = tcf_exts_exec(skb, &prog->exts, res);
  109. if (ret < 0)
  110. continue;
  111. break;
  112. }
  113. rcu_read_unlock();
  114. return ret;
  115. }
  116. static bool cls_bpf_is_ebpf(const struct cls_bpf_prog *prog)
  117. {
  118. return !prog->bpf_ops;
  119. }
  120. static int cls_bpf_init(struct tcf_proto *tp)
  121. {
  122. struct cls_bpf_head *head;
  123. head = kzalloc(sizeof(*head), GFP_KERNEL);
  124. if (head == NULL)
  125. return -ENOBUFS;
  126. INIT_LIST_HEAD_RCU(&head->plist);
  127. rcu_assign_pointer(tp->root, head);
  128. return 0;
  129. }
  130. static void cls_bpf_delete_prog(struct tcf_proto *tp, struct cls_bpf_prog *prog)
  131. {
  132. tcf_exts_destroy(&prog->exts);
  133. if (cls_bpf_is_ebpf(prog))
  134. bpf_prog_put(prog->filter);
  135. else
  136. bpf_prog_destroy(prog->filter);
  137. kfree(prog->bpf_name);
  138. kfree(prog->bpf_ops);
  139. kfree(prog);
  140. }
  141. static void __cls_bpf_delete_prog(struct rcu_head *rcu)
  142. {
  143. struct cls_bpf_prog *prog = container_of(rcu, struct cls_bpf_prog, rcu);
  144. cls_bpf_delete_prog(prog->tp, prog);
  145. }
  146. static int cls_bpf_delete(struct tcf_proto *tp, unsigned long arg)
  147. {
  148. struct cls_bpf_prog *prog = (struct cls_bpf_prog *) arg;
  149. list_del_rcu(&prog->link);
  150. tcf_unbind_filter(tp, &prog->res);
  151. call_rcu(&prog->rcu, __cls_bpf_delete_prog);
  152. return 0;
  153. }
  154. static bool cls_bpf_destroy(struct tcf_proto *tp, bool force)
  155. {
  156. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  157. struct cls_bpf_prog *prog, *tmp;
  158. if (!force && !list_empty(&head->plist))
  159. return false;
  160. list_for_each_entry_safe(prog, tmp, &head->plist, link) {
  161. list_del_rcu(&prog->link);
  162. tcf_unbind_filter(tp, &prog->res);
  163. call_rcu(&prog->rcu, __cls_bpf_delete_prog);
  164. }
  165. RCU_INIT_POINTER(tp->root, NULL);
  166. kfree_rcu(head, rcu);
  167. return true;
  168. }
  169. static unsigned long cls_bpf_get(struct tcf_proto *tp, u32 handle)
  170. {
  171. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  172. struct cls_bpf_prog *prog;
  173. unsigned long ret = 0UL;
  174. if (head == NULL)
  175. return 0UL;
  176. list_for_each_entry(prog, &head->plist, link) {
  177. if (prog->handle == handle) {
  178. ret = (unsigned long) prog;
  179. break;
  180. }
  181. }
  182. return ret;
  183. }
  184. static int cls_bpf_prog_from_ops(struct nlattr **tb, struct cls_bpf_prog *prog)
  185. {
  186. struct sock_filter *bpf_ops;
  187. struct sock_fprog_kern fprog_tmp;
  188. struct bpf_prog *fp;
  189. u16 bpf_size, bpf_num_ops;
  190. int ret;
  191. bpf_num_ops = nla_get_u16(tb[TCA_BPF_OPS_LEN]);
  192. if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0)
  193. return -EINVAL;
  194. bpf_size = bpf_num_ops * sizeof(*bpf_ops);
  195. if (bpf_size != nla_len(tb[TCA_BPF_OPS]))
  196. return -EINVAL;
  197. bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
  198. if (bpf_ops == NULL)
  199. return -ENOMEM;
  200. memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size);
  201. fprog_tmp.len = bpf_num_ops;
  202. fprog_tmp.filter = bpf_ops;
  203. ret = bpf_prog_create(&fp, &fprog_tmp);
  204. if (ret < 0) {
  205. kfree(bpf_ops);
  206. return ret;
  207. }
  208. prog->bpf_ops = bpf_ops;
  209. prog->bpf_num_ops = bpf_num_ops;
  210. prog->bpf_name = NULL;
  211. prog->filter = fp;
  212. return 0;
  213. }
  214. static int cls_bpf_prog_from_efd(struct nlattr **tb, struct cls_bpf_prog *prog,
  215. const struct tcf_proto *tp)
  216. {
  217. struct bpf_prog *fp;
  218. char *name = NULL;
  219. u32 bpf_fd;
  220. bpf_fd = nla_get_u32(tb[TCA_BPF_FD]);
  221. fp = bpf_prog_get(bpf_fd);
  222. if (IS_ERR(fp))
  223. return PTR_ERR(fp);
  224. if (fp->type != BPF_PROG_TYPE_SCHED_CLS) {
  225. bpf_prog_put(fp);
  226. return -EINVAL;
  227. }
  228. if (tb[TCA_BPF_NAME]) {
  229. name = kmemdup(nla_data(tb[TCA_BPF_NAME]),
  230. nla_len(tb[TCA_BPF_NAME]),
  231. GFP_KERNEL);
  232. if (!name) {
  233. bpf_prog_put(fp);
  234. return -ENOMEM;
  235. }
  236. }
  237. prog->bpf_ops = NULL;
  238. prog->bpf_fd = bpf_fd;
  239. prog->bpf_name = name;
  240. prog->filter = fp;
  241. if (fp->dst_needed)
  242. netif_keep_dst(qdisc_dev(tp->q));
  243. return 0;
  244. }
  245. static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp,
  246. struct cls_bpf_prog *prog,
  247. unsigned long base, struct nlattr **tb,
  248. struct nlattr *est, bool ovr)
  249. {
  250. bool is_bpf, is_ebpf, have_exts = false;
  251. struct tcf_exts exts;
  252. int ret;
  253. is_bpf = tb[TCA_BPF_OPS_LEN] && tb[TCA_BPF_OPS];
  254. is_ebpf = tb[TCA_BPF_FD];
  255. if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf))
  256. return -EINVAL;
  257. tcf_exts_init(&exts, TCA_BPF_ACT, TCA_BPF_POLICE);
  258. ret = tcf_exts_validate(net, tp, tb, est, &exts, ovr);
  259. if (ret < 0)
  260. return ret;
  261. if (tb[TCA_BPF_FLAGS]) {
  262. u32 bpf_flags = nla_get_u32(tb[TCA_BPF_FLAGS]);
  263. if (bpf_flags & ~TCA_BPF_FLAG_ACT_DIRECT) {
  264. tcf_exts_destroy(&exts);
  265. return -EINVAL;
  266. }
  267. have_exts = bpf_flags & TCA_BPF_FLAG_ACT_DIRECT;
  268. }
  269. prog->exts_integrated = have_exts;
  270. ret = is_bpf ? cls_bpf_prog_from_ops(tb, prog) :
  271. cls_bpf_prog_from_efd(tb, prog, tp);
  272. if (ret < 0) {
  273. tcf_exts_destroy(&exts);
  274. return ret;
  275. }
  276. if (tb[TCA_BPF_CLASSID]) {
  277. prog->res.classid = nla_get_u32(tb[TCA_BPF_CLASSID]);
  278. tcf_bind_filter(tp, &prog->res, base);
  279. }
  280. tcf_exts_change(tp, &prog->exts, &exts);
  281. return 0;
  282. }
  283. static u32 cls_bpf_grab_new_handle(struct tcf_proto *tp,
  284. struct cls_bpf_head *head)
  285. {
  286. unsigned int i = 0x80000000;
  287. u32 handle;
  288. do {
  289. if (++head->hgen == 0x7FFFFFFF)
  290. head->hgen = 1;
  291. } while (--i > 0 && cls_bpf_get(tp, head->hgen));
  292. if (unlikely(i == 0)) {
  293. pr_err("Insufficient number of handles\n");
  294. handle = 0;
  295. } else {
  296. handle = head->hgen;
  297. }
  298. return handle;
  299. }
  300. static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
  301. struct tcf_proto *tp, unsigned long base,
  302. u32 handle, struct nlattr **tca,
  303. unsigned long *arg, bool ovr)
  304. {
  305. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  306. struct cls_bpf_prog *oldprog = (struct cls_bpf_prog *) *arg;
  307. struct nlattr *tb[TCA_BPF_MAX + 1];
  308. struct cls_bpf_prog *prog;
  309. int ret;
  310. if (tca[TCA_OPTIONS] == NULL)
  311. return -EINVAL;
  312. ret = nla_parse_nested(tb, TCA_BPF_MAX, tca[TCA_OPTIONS], bpf_policy);
  313. if (ret < 0)
  314. return ret;
  315. prog = kzalloc(sizeof(*prog), GFP_KERNEL);
  316. if (!prog)
  317. return -ENOBUFS;
  318. tcf_exts_init(&prog->exts, TCA_BPF_ACT, TCA_BPF_POLICE);
  319. if (oldprog) {
  320. if (handle && oldprog->handle != handle) {
  321. ret = -EINVAL;
  322. goto errout;
  323. }
  324. }
  325. if (handle == 0)
  326. prog->handle = cls_bpf_grab_new_handle(tp, head);
  327. else
  328. prog->handle = handle;
  329. if (prog->handle == 0) {
  330. ret = -EINVAL;
  331. goto errout;
  332. }
  333. ret = cls_bpf_modify_existing(net, tp, prog, base, tb, tca[TCA_RATE], ovr);
  334. if (ret < 0)
  335. goto errout;
  336. if (oldprog) {
  337. list_replace_rcu(&oldprog->link, &prog->link);
  338. tcf_unbind_filter(tp, &oldprog->res);
  339. call_rcu(&oldprog->rcu, __cls_bpf_delete_prog);
  340. } else {
  341. list_add_rcu(&prog->link, &head->plist);
  342. }
  343. *arg = (unsigned long) prog;
  344. return 0;
  345. errout:
  346. kfree(prog);
  347. return ret;
  348. }
  349. static int cls_bpf_dump_bpf_info(const struct cls_bpf_prog *prog,
  350. struct sk_buff *skb)
  351. {
  352. struct nlattr *nla;
  353. if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_num_ops))
  354. return -EMSGSIZE;
  355. nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_num_ops *
  356. sizeof(struct sock_filter));
  357. if (nla == NULL)
  358. return -EMSGSIZE;
  359. memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
  360. return 0;
  361. }
  362. static int cls_bpf_dump_ebpf_info(const struct cls_bpf_prog *prog,
  363. struct sk_buff *skb)
  364. {
  365. if (nla_put_u32(skb, TCA_BPF_FD, prog->bpf_fd))
  366. return -EMSGSIZE;
  367. if (prog->bpf_name &&
  368. nla_put_string(skb, TCA_BPF_NAME, prog->bpf_name))
  369. return -EMSGSIZE;
  370. return 0;
  371. }
  372. static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
  373. struct sk_buff *skb, struct tcmsg *tm)
  374. {
  375. struct cls_bpf_prog *prog = (struct cls_bpf_prog *) fh;
  376. struct nlattr *nest;
  377. u32 bpf_flags = 0;
  378. int ret;
  379. if (prog == NULL)
  380. return skb->len;
  381. tm->tcm_handle = prog->handle;
  382. nest = nla_nest_start(skb, TCA_OPTIONS);
  383. if (nest == NULL)
  384. goto nla_put_failure;
  385. if (prog->res.classid &&
  386. nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid))
  387. goto nla_put_failure;
  388. if (cls_bpf_is_ebpf(prog))
  389. ret = cls_bpf_dump_ebpf_info(prog, skb);
  390. else
  391. ret = cls_bpf_dump_bpf_info(prog, skb);
  392. if (ret)
  393. goto nla_put_failure;
  394. if (tcf_exts_dump(skb, &prog->exts) < 0)
  395. goto nla_put_failure;
  396. if (prog->exts_integrated)
  397. bpf_flags |= TCA_BPF_FLAG_ACT_DIRECT;
  398. if (bpf_flags && nla_put_u32(skb, TCA_BPF_FLAGS, bpf_flags))
  399. goto nla_put_failure;
  400. nla_nest_end(skb, nest);
  401. if (tcf_exts_dump_stats(skb, &prog->exts) < 0)
  402. goto nla_put_failure;
  403. return skb->len;
  404. nla_put_failure:
  405. nla_nest_cancel(skb, nest);
  406. return -1;
  407. }
  408. static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  409. {
  410. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  411. struct cls_bpf_prog *prog;
  412. list_for_each_entry(prog, &head->plist, link) {
  413. if (arg->count < arg->skip)
  414. goto skip;
  415. if (arg->fn(tp, (unsigned long) prog, arg) < 0) {
  416. arg->stop = 1;
  417. break;
  418. }
  419. skip:
  420. arg->count++;
  421. }
  422. }
  423. static struct tcf_proto_ops cls_bpf_ops __read_mostly = {
  424. .kind = "bpf",
  425. .owner = THIS_MODULE,
  426. .classify = cls_bpf_classify,
  427. .init = cls_bpf_init,
  428. .destroy = cls_bpf_destroy,
  429. .get = cls_bpf_get,
  430. .change = cls_bpf_change,
  431. .delete = cls_bpf_delete,
  432. .walk = cls_bpf_walk,
  433. .dump = cls_bpf_dump,
  434. };
  435. static int __init cls_bpf_init_mod(void)
  436. {
  437. return register_tcf_proto_ops(&cls_bpf_ops);
  438. }
  439. static void __exit cls_bpf_exit_mod(void)
  440. {
  441. unregister_tcf_proto_ops(&cls_bpf_ops);
  442. }
  443. module_init(cls_bpf_init_mod);
  444. module_exit(cls_bpf_exit_mod);