cls_bpf.c 10 KB

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