cls_bpf.c 10 KB

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