cls_bpf.c 12 KB

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