cls_bpf.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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 <net/rtnetlink.h>
  19. #include <net/pkt_cls.h>
  20. #include <net/sock.h>
  21. MODULE_LICENSE("GPL");
  22. MODULE_AUTHOR("Daniel Borkmann <dborkman@redhat.com>");
  23. MODULE_DESCRIPTION("TC BPF based classifier");
  24. struct cls_bpf_head {
  25. struct list_head plist;
  26. u32 hgen;
  27. struct rcu_head rcu;
  28. };
  29. struct cls_bpf_prog {
  30. struct bpf_prog *filter;
  31. struct sock_filter *bpf_ops;
  32. struct tcf_exts exts;
  33. struct tcf_result res;
  34. struct list_head link;
  35. u32 handle;
  36. u16 bpf_len;
  37. struct tcf_proto *tp;
  38. struct rcu_head rcu;
  39. };
  40. static const struct nla_policy bpf_policy[TCA_BPF_MAX + 1] = {
  41. [TCA_BPF_CLASSID] = { .type = NLA_U32 },
  42. [TCA_BPF_OPS_LEN] = { .type = NLA_U16 },
  43. [TCA_BPF_OPS] = { .type = NLA_BINARY,
  44. .len = sizeof(struct sock_filter) * BPF_MAXINSNS },
  45. };
  46. static int cls_bpf_classify(struct sk_buff *skb, const struct tcf_proto *tp,
  47. struct tcf_result *res)
  48. {
  49. struct cls_bpf_head *head = rcu_dereference_bh(tp->root);
  50. struct cls_bpf_prog *prog;
  51. int ret;
  52. list_for_each_entry_rcu(prog, &head->plist, link) {
  53. int filter_res = BPF_PROG_RUN(prog->filter, skb);
  54. if (filter_res == 0)
  55. continue;
  56. *res = prog->res;
  57. if (filter_res != -1)
  58. res->classid = filter_res;
  59. ret = tcf_exts_exec(skb, &prog->exts, res);
  60. if (ret < 0)
  61. continue;
  62. return ret;
  63. }
  64. return -1;
  65. }
  66. static int cls_bpf_init(struct tcf_proto *tp)
  67. {
  68. struct cls_bpf_head *head;
  69. head = kzalloc(sizeof(*head), GFP_KERNEL);
  70. if (head == NULL)
  71. return -ENOBUFS;
  72. INIT_LIST_HEAD_RCU(&head->plist);
  73. rcu_assign_pointer(tp->root, head);
  74. return 0;
  75. }
  76. static void cls_bpf_delete_prog(struct tcf_proto *tp, struct cls_bpf_prog *prog)
  77. {
  78. tcf_exts_destroy(&prog->exts);
  79. bpf_prog_destroy(prog->filter);
  80. kfree(prog->bpf_ops);
  81. kfree(prog);
  82. }
  83. static void __cls_bpf_delete_prog(struct rcu_head *rcu)
  84. {
  85. struct cls_bpf_prog *prog = container_of(rcu, struct cls_bpf_prog, rcu);
  86. cls_bpf_delete_prog(prog->tp, prog);
  87. }
  88. static int cls_bpf_delete(struct tcf_proto *tp, unsigned long arg)
  89. {
  90. struct cls_bpf_prog *prog = (struct cls_bpf_prog *) arg;
  91. list_del_rcu(&prog->link);
  92. tcf_unbind_filter(tp, &prog->res);
  93. call_rcu(&prog->rcu, __cls_bpf_delete_prog);
  94. return 0;
  95. }
  96. static void cls_bpf_destroy(struct tcf_proto *tp)
  97. {
  98. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  99. struct cls_bpf_prog *prog, *tmp;
  100. list_for_each_entry_safe(prog, tmp, &head->plist, link) {
  101. list_del_rcu(&prog->link);
  102. tcf_unbind_filter(tp, &prog->res);
  103. call_rcu(&prog->rcu, __cls_bpf_delete_prog);
  104. }
  105. RCU_INIT_POINTER(tp->root, NULL);
  106. kfree_rcu(head, rcu);
  107. }
  108. static unsigned long cls_bpf_get(struct tcf_proto *tp, u32 handle)
  109. {
  110. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  111. struct cls_bpf_prog *prog;
  112. unsigned long ret = 0UL;
  113. if (head == NULL)
  114. return 0UL;
  115. list_for_each_entry(prog, &head->plist, link) {
  116. if (prog->handle == handle) {
  117. ret = (unsigned long) prog;
  118. break;
  119. }
  120. }
  121. return ret;
  122. }
  123. static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp,
  124. struct cls_bpf_prog *prog,
  125. unsigned long base, struct nlattr **tb,
  126. struct nlattr *est, bool ovr)
  127. {
  128. struct sock_filter *bpf_ops;
  129. struct tcf_exts exts;
  130. struct sock_fprog_kern tmp;
  131. struct bpf_prog *fp;
  132. u16 bpf_size, bpf_len;
  133. u32 classid;
  134. int ret;
  135. if (!tb[TCA_BPF_OPS_LEN] || !tb[TCA_BPF_OPS] || !tb[TCA_BPF_CLASSID])
  136. return -EINVAL;
  137. tcf_exts_init(&exts, TCA_BPF_ACT, TCA_BPF_POLICE);
  138. ret = tcf_exts_validate(net, tp, tb, est, &exts, ovr);
  139. if (ret < 0)
  140. return ret;
  141. classid = nla_get_u32(tb[TCA_BPF_CLASSID]);
  142. bpf_len = nla_get_u16(tb[TCA_BPF_OPS_LEN]);
  143. if (bpf_len > BPF_MAXINSNS || bpf_len == 0) {
  144. ret = -EINVAL;
  145. goto errout;
  146. }
  147. bpf_size = bpf_len * sizeof(*bpf_ops);
  148. bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
  149. if (bpf_ops == NULL) {
  150. ret = -ENOMEM;
  151. goto errout;
  152. }
  153. memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size);
  154. tmp.len = bpf_len;
  155. tmp.filter = bpf_ops;
  156. ret = bpf_prog_create(&fp, &tmp);
  157. if (ret)
  158. goto errout_free;
  159. prog->bpf_len = bpf_len;
  160. prog->bpf_ops = bpf_ops;
  161. prog->filter = fp;
  162. prog->res.classid = classid;
  163. tcf_bind_filter(tp, &prog->res, base);
  164. tcf_exts_change(tp, &prog->exts, &exts);
  165. return 0;
  166. errout_free:
  167. kfree(bpf_ops);
  168. errout:
  169. tcf_exts_destroy(&exts);
  170. return ret;
  171. }
  172. static u32 cls_bpf_grab_new_handle(struct tcf_proto *tp,
  173. struct cls_bpf_head *head)
  174. {
  175. unsigned int i = 0x80000000;
  176. do {
  177. if (++head->hgen == 0x7FFFFFFF)
  178. head->hgen = 1;
  179. } while (--i > 0 && cls_bpf_get(tp, head->hgen));
  180. if (i == 0)
  181. pr_err("Insufficient number of handles\n");
  182. return i;
  183. }
  184. static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
  185. struct tcf_proto *tp, unsigned long base,
  186. u32 handle, struct nlattr **tca,
  187. unsigned long *arg, bool ovr)
  188. {
  189. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  190. struct cls_bpf_prog *oldprog = (struct cls_bpf_prog *) *arg;
  191. struct nlattr *tb[TCA_BPF_MAX + 1];
  192. struct cls_bpf_prog *prog;
  193. int ret;
  194. if (tca[TCA_OPTIONS] == NULL)
  195. return -EINVAL;
  196. ret = nla_parse_nested(tb, TCA_BPF_MAX, tca[TCA_OPTIONS], bpf_policy);
  197. if (ret < 0)
  198. return ret;
  199. prog = kzalloc(sizeof(*prog), GFP_KERNEL);
  200. if (!prog)
  201. return -ENOBUFS;
  202. tcf_exts_init(&prog->exts, TCA_BPF_ACT, TCA_BPF_POLICE);
  203. if (oldprog) {
  204. if (handle && oldprog->handle != handle) {
  205. ret = -EINVAL;
  206. goto errout;
  207. }
  208. }
  209. if (handle == 0)
  210. prog->handle = cls_bpf_grab_new_handle(tp, head);
  211. else
  212. prog->handle = handle;
  213. if (prog->handle == 0) {
  214. ret = -EINVAL;
  215. goto errout;
  216. }
  217. ret = cls_bpf_modify_existing(net, tp, prog, base, tb, tca[TCA_RATE], ovr);
  218. if (ret < 0)
  219. goto errout;
  220. if (oldprog) {
  221. list_replace_rcu(&prog->link, &oldprog->link);
  222. tcf_unbind_filter(tp, &oldprog->res);
  223. call_rcu(&oldprog->rcu, __cls_bpf_delete_prog);
  224. } else {
  225. list_add_rcu(&prog->link, &head->plist);
  226. }
  227. *arg = (unsigned long) prog;
  228. return 0;
  229. errout:
  230. kfree(prog);
  231. return ret;
  232. }
  233. static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
  234. struct sk_buff *skb, struct tcmsg *tm)
  235. {
  236. struct cls_bpf_prog *prog = (struct cls_bpf_prog *) fh;
  237. struct nlattr *nest, *nla;
  238. if (prog == NULL)
  239. return skb->len;
  240. tm->tcm_handle = prog->handle;
  241. nest = nla_nest_start(skb, TCA_OPTIONS);
  242. if (nest == NULL)
  243. goto nla_put_failure;
  244. if (nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid))
  245. goto nla_put_failure;
  246. if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_len))
  247. goto nla_put_failure;
  248. nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_len *
  249. sizeof(struct sock_filter));
  250. if (nla == NULL)
  251. goto nla_put_failure;
  252. memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
  253. if (tcf_exts_dump(skb, &prog->exts) < 0)
  254. goto nla_put_failure;
  255. nla_nest_end(skb, nest);
  256. if (tcf_exts_dump_stats(skb, &prog->exts) < 0)
  257. goto nla_put_failure;
  258. return skb->len;
  259. nla_put_failure:
  260. nla_nest_cancel(skb, nest);
  261. return -1;
  262. }
  263. static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  264. {
  265. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  266. struct cls_bpf_prog *prog;
  267. list_for_each_entry(prog, &head->plist, link) {
  268. if (arg->count < arg->skip)
  269. goto skip;
  270. if (arg->fn(tp, (unsigned long) prog, arg) < 0) {
  271. arg->stop = 1;
  272. break;
  273. }
  274. skip:
  275. arg->count++;
  276. }
  277. }
  278. static struct tcf_proto_ops cls_bpf_ops __read_mostly = {
  279. .kind = "bpf",
  280. .owner = THIS_MODULE,
  281. .classify = cls_bpf_classify,
  282. .init = cls_bpf_init,
  283. .destroy = cls_bpf_destroy,
  284. .get = cls_bpf_get,
  285. .change = cls_bpf_change,
  286. .delete = cls_bpf_delete,
  287. .walk = cls_bpf_walk,
  288. .dump = cls_bpf_dump,
  289. };
  290. static int __init cls_bpf_init_mod(void)
  291. {
  292. return register_tcf_proto_ops(&cls_bpf_ops);
  293. }
  294. static void __exit cls_bpf_exit_mod(void)
  295. {
  296. unregister_tcf_proto_ops(&cls_bpf_ops);
  297. }
  298. module_init(cls_bpf_init_mod);
  299. module_exit(cls_bpf_exit_mod);