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