cls_bpf.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549
  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. kfree_rcu(head, rcu);
  166. return true;
  167. }
  168. static unsigned long cls_bpf_get(struct tcf_proto *tp, u32 handle)
  169. {
  170. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  171. struct cls_bpf_prog *prog;
  172. unsigned long ret = 0UL;
  173. list_for_each_entry(prog, &head->plist, link) {
  174. if (prog->handle == handle) {
  175. ret = (unsigned long) prog;
  176. break;
  177. }
  178. }
  179. return ret;
  180. }
  181. static int cls_bpf_prog_from_ops(struct nlattr **tb, struct cls_bpf_prog *prog)
  182. {
  183. struct sock_filter *bpf_ops;
  184. struct sock_fprog_kern fprog_tmp;
  185. struct bpf_prog *fp;
  186. u16 bpf_size, bpf_num_ops;
  187. int ret;
  188. bpf_num_ops = nla_get_u16(tb[TCA_BPF_OPS_LEN]);
  189. if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0)
  190. return -EINVAL;
  191. bpf_size = bpf_num_ops * sizeof(*bpf_ops);
  192. if (bpf_size != nla_len(tb[TCA_BPF_OPS]))
  193. return -EINVAL;
  194. bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
  195. if (bpf_ops == NULL)
  196. return -ENOMEM;
  197. memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size);
  198. fprog_tmp.len = bpf_num_ops;
  199. fprog_tmp.filter = bpf_ops;
  200. ret = bpf_prog_create(&fp, &fprog_tmp);
  201. if (ret < 0) {
  202. kfree(bpf_ops);
  203. return ret;
  204. }
  205. prog->bpf_ops = bpf_ops;
  206. prog->bpf_num_ops = bpf_num_ops;
  207. prog->bpf_name = NULL;
  208. prog->filter = fp;
  209. return 0;
  210. }
  211. static int cls_bpf_prog_from_efd(struct nlattr **tb, struct cls_bpf_prog *prog,
  212. const struct tcf_proto *tp)
  213. {
  214. struct bpf_prog *fp;
  215. char *name = NULL;
  216. u32 bpf_fd;
  217. bpf_fd = nla_get_u32(tb[TCA_BPF_FD]);
  218. fp = bpf_prog_get(bpf_fd);
  219. if (IS_ERR(fp))
  220. return PTR_ERR(fp);
  221. if (fp->type != BPF_PROG_TYPE_SCHED_CLS) {
  222. bpf_prog_put(fp);
  223. return -EINVAL;
  224. }
  225. if (tb[TCA_BPF_NAME]) {
  226. name = kmemdup(nla_data(tb[TCA_BPF_NAME]),
  227. nla_len(tb[TCA_BPF_NAME]),
  228. GFP_KERNEL);
  229. if (!name) {
  230. bpf_prog_put(fp);
  231. return -ENOMEM;
  232. }
  233. }
  234. prog->bpf_ops = NULL;
  235. prog->bpf_fd = bpf_fd;
  236. prog->bpf_name = name;
  237. prog->filter = fp;
  238. if (fp->dst_needed)
  239. netif_keep_dst(qdisc_dev(tp->q));
  240. return 0;
  241. }
  242. static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp,
  243. struct cls_bpf_prog *prog,
  244. unsigned long base, struct nlattr **tb,
  245. struct nlattr *est, bool ovr)
  246. {
  247. bool is_bpf, is_ebpf, have_exts = false;
  248. struct tcf_exts exts;
  249. int ret;
  250. is_bpf = tb[TCA_BPF_OPS_LEN] && tb[TCA_BPF_OPS];
  251. is_ebpf = tb[TCA_BPF_FD];
  252. if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf))
  253. return -EINVAL;
  254. tcf_exts_init(&exts, TCA_BPF_ACT, TCA_BPF_POLICE);
  255. ret = tcf_exts_validate(net, tp, tb, est, &exts, ovr);
  256. if (ret < 0)
  257. return ret;
  258. if (tb[TCA_BPF_FLAGS]) {
  259. u32 bpf_flags = nla_get_u32(tb[TCA_BPF_FLAGS]);
  260. if (bpf_flags & ~TCA_BPF_FLAG_ACT_DIRECT) {
  261. tcf_exts_destroy(&exts);
  262. return -EINVAL;
  263. }
  264. have_exts = bpf_flags & TCA_BPF_FLAG_ACT_DIRECT;
  265. }
  266. prog->exts_integrated = have_exts;
  267. ret = is_bpf ? cls_bpf_prog_from_ops(tb, prog) :
  268. cls_bpf_prog_from_efd(tb, prog, tp);
  269. if (ret < 0) {
  270. tcf_exts_destroy(&exts);
  271. return ret;
  272. }
  273. if (tb[TCA_BPF_CLASSID]) {
  274. prog->res.classid = nla_get_u32(tb[TCA_BPF_CLASSID]);
  275. tcf_bind_filter(tp, &prog->res, base);
  276. }
  277. tcf_exts_change(tp, &prog->exts, &exts);
  278. return 0;
  279. }
  280. static u32 cls_bpf_grab_new_handle(struct tcf_proto *tp,
  281. struct cls_bpf_head *head)
  282. {
  283. unsigned int i = 0x80000000;
  284. u32 handle;
  285. do {
  286. if (++head->hgen == 0x7FFFFFFF)
  287. head->hgen = 1;
  288. } while (--i > 0 && cls_bpf_get(tp, head->hgen));
  289. if (unlikely(i == 0)) {
  290. pr_err("Insufficient number of handles\n");
  291. handle = 0;
  292. } else {
  293. handle = head->hgen;
  294. }
  295. return handle;
  296. }
  297. static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
  298. struct tcf_proto *tp, unsigned long base,
  299. u32 handle, struct nlattr **tca,
  300. unsigned long *arg, bool ovr)
  301. {
  302. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  303. struct cls_bpf_prog *oldprog = (struct cls_bpf_prog *) *arg;
  304. struct nlattr *tb[TCA_BPF_MAX + 1];
  305. struct cls_bpf_prog *prog;
  306. int ret;
  307. if (tca[TCA_OPTIONS] == NULL)
  308. return -EINVAL;
  309. ret = nla_parse_nested(tb, TCA_BPF_MAX, tca[TCA_OPTIONS], bpf_policy);
  310. if (ret < 0)
  311. return ret;
  312. prog = kzalloc(sizeof(*prog), GFP_KERNEL);
  313. if (!prog)
  314. return -ENOBUFS;
  315. tcf_exts_init(&prog->exts, TCA_BPF_ACT, TCA_BPF_POLICE);
  316. if (oldprog) {
  317. if (handle && oldprog->handle != handle) {
  318. ret = -EINVAL;
  319. goto errout;
  320. }
  321. }
  322. if (handle == 0)
  323. prog->handle = cls_bpf_grab_new_handle(tp, head);
  324. else
  325. prog->handle = handle;
  326. if (prog->handle == 0) {
  327. ret = -EINVAL;
  328. goto errout;
  329. }
  330. ret = cls_bpf_modify_existing(net, tp, prog, base, tb, tca[TCA_RATE], ovr);
  331. if (ret < 0)
  332. goto errout;
  333. if (oldprog) {
  334. list_replace_rcu(&oldprog->link, &prog->link);
  335. tcf_unbind_filter(tp, &oldprog->res);
  336. call_rcu(&oldprog->rcu, __cls_bpf_delete_prog);
  337. } else {
  338. list_add_rcu(&prog->link, &head->plist);
  339. }
  340. *arg = (unsigned long) prog;
  341. return 0;
  342. errout:
  343. kfree(prog);
  344. return ret;
  345. }
  346. static int cls_bpf_dump_bpf_info(const struct cls_bpf_prog *prog,
  347. struct sk_buff *skb)
  348. {
  349. struct nlattr *nla;
  350. if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_num_ops))
  351. return -EMSGSIZE;
  352. nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_num_ops *
  353. sizeof(struct sock_filter));
  354. if (nla == NULL)
  355. return -EMSGSIZE;
  356. memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
  357. return 0;
  358. }
  359. static int cls_bpf_dump_ebpf_info(const struct cls_bpf_prog *prog,
  360. struct sk_buff *skb)
  361. {
  362. if (nla_put_u32(skb, TCA_BPF_FD, prog->bpf_fd))
  363. return -EMSGSIZE;
  364. if (prog->bpf_name &&
  365. nla_put_string(skb, TCA_BPF_NAME, prog->bpf_name))
  366. return -EMSGSIZE;
  367. return 0;
  368. }
  369. static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
  370. struct sk_buff *skb, struct tcmsg *tm)
  371. {
  372. struct cls_bpf_prog *prog = (struct cls_bpf_prog *) fh;
  373. struct nlattr *nest;
  374. u32 bpf_flags = 0;
  375. int ret;
  376. if (prog == NULL)
  377. return skb->len;
  378. tm->tcm_handle = prog->handle;
  379. nest = nla_nest_start(skb, TCA_OPTIONS);
  380. if (nest == NULL)
  381. goto nla_put_failure;
  382. if (prog->res.classid &&
  383. nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid))
  384. goto nla_put_failure;
  385. if (cls_bpf_is_ebpf(prog))
  386. ret = cls_bpf_dump_ebpf_info(prog, skb);
  387. else
  388. ret = cls_bpf_dump_bpf_info(prog, skb);
  389. if (ret)
  390. goto nla_put_failure;
  391. if (tcf_exts_dump(skb, &prog->exts) < 0)
  392. goto nla_put_failure;
  393. if (prog->exts_integrated)
  394. bpf_flags |= TCA_BPF_FLAG_ACT_DIRECT;
  395. if (bpf_flags && nla_put_u32(skb, TCA_BPF_FLAGS, bpf_flags))
  396. goto nla_put_failure;
  397. nla_nest_end(skb, nest);
  398. if (tcf_exts_dump_stats(skb, &prog->exts) < 0)
  399. goto nla_put_failure;
  400. return skb->len;
  401. nla_put_failure:
  402. nla_nest_cancel(skb, nest);
  403. return -1;
  404. }
  405. static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  406. {
  407. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  408. struct cls_bpf_prog *prog;
  409. list_for_each_entry(prog, &head->plist, link) {
  410. if (arg->count < arg->skip)
  411. goto skip;
  412. if (arg->fn(tp, (unsigned long) prog, arg) < 0) {
  413. arg->stop = 1;
  414. break;
  415. }
  416. skip:
  417. arg->count++;
  418. }
  419. }
  420. static struct tcf_proto_ops cls_bpf_ops __read_mostly = {
  421. .kind = "bpf",
  422. .owner = THIS_MODULE,
  423. .classify = cls_bpf_classify,
  424. .init = cls_bpf_init,
  425. .destroy = cls_bpf_destroy,
  426. .get = cls_bpf_get,
  427. .change = cls_bpf_change,
  428. .delete = cls_bpf_delete,
  429. .walk = cls_bpf_walk,
  430. .dump = cls_bpf_dump,
  431. };
  432. static int __init cls_bpf_init_mod(void)
  433. {
  434. return register_tcf_proto_ops(&cls_bpf_ops);
  435. }
  436. static void __exit cls_bpf_exit_mod(void)
  437. {
  438. unregister_tcf_proto_ops(&cls_bpf_ops);
  439. }
  440. module_init(cls_bpf_init_mod);
  441. module_exit(cls_bpf_exit_mod);