cls_bpf.c 10 KB

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