cls_bpf.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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_head *head = rtnl_dereference(tp->root);
  91. struct cls_bpf_prog *prog, *todel = (struct cls_bpf_prog *) arg;
  92. list_for_each_entry(prog, &head->plist, link) {
  93. if (prog == todel) {
  94. list_del_rcu(&prog->link);
  95. tcf_unbind_filter(tp, &prog->res);
  96. call_rcu(&prog->rcu, __cls_bpf_delete_prog);
  97. return 0;
  98. }
  99. }
  100. return -ENOENT;
  101. }
  102. static void cls_bpf_destroy(struct tcf_proto *tp)
  103. {
  104. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  105. struct cls_bpf_prog *prog, *tmp;
  106. list_for_each_entry_safe(prog, tmp, &head->plist, link) {
  107. list_del_rcu(&prog->link);
  108. tcf_unbind_filter(tp, &prog->res);
  109. call_rcu(&prog->rcu, __cls_bpf_delete_prog);
  110. }
  111. RCU_INIT_POINTER(tp->root, NULL);
  112. kfree_rcu(head, rcu);
  113. }
  114. static unsigned long cls_bpf_get(struct tcf_proto *tp, u32 handle)
  115. {
  116. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  117. struct cls_bpf_prog *prog;
  118. unsigned long ret = 0UL;
  119. if (head == NULL)
  120. return 0UL;
  121. list_for_each_entry_rcu(prog, &head->plist, link) {
  122. if (prog->handle == handle) {
  123. ret = (unsigned long) prog;
  124. break;
  125. }
  126. }
  127. return ret;
  128. }
  129. static void cls_bpf_put(struct tcf_proto *tp, unsigned long f)
  130. {
  131. }
  132. static int cls_bpf_modify_existing(struct net *net, struct tcf_proto *tp,
  133. struct cls_bpf_prog *prog,
  134. unsigned long base, struct nlattr **tb,
  135. struct nlattr *est, bool ovr)
  136. {
  137. struct sock_filter *bpf_ops;
  138. struct tcf_exts exts;
  139. struct sock_fprog_kern tmp;
  140. struct bpf_prog *fp;
  141. u16 bpf_size, bpf_len;
  142. u32 classid;
  143. int ret;
  144. if (!tb[TCA_BPF_OPS_LEN] || !tb[TCA_BPF_OPS] || !tb[TCA_BPF_CLASSID])
  145. return -EINVAL;
  146. tcf_exts_init(&exts, TCA_BPF_ACT, TCA_BPF_POLICE);
  147. ret = tcf_exts_validate(net, tp, tb, est, &exts, ovr);
  148. if (ret < 0)
  149. return ret;
  150. classid = nla_get_u32(tb[TCA_BPF_CLASSID]);
  151. bpf_len = nla_get_u16(tb[TCA_BPF_OPS_LEN]);
  152. if (bpf_len > BPF_MAXINSNS || bpf_len == 0) {
  153. ret = -EINVAL;
  154. goto errout;
  155. }
  156. bpf_size = bpf_len * sizeof(*bpf_ops);
  157. bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
  158. if (bpf_ops == NULL) {
  159. ret = -ENOMEM;
  160. goto errout;
  161. }
  162. memcpy(bpf_ops, nla_data(tb[TCA_BPF_OPS]), bpf_size);
  163. tmp.len = bpf_len;
  164. tmp.filter = bpf_ops;
  165. ret = bpf_prog_create(&fp, &tmp);
  166. if (ret)
  167. goto errout_free;
  168. prog->bpf_len = bpf_len;
  169. prog->bpf_ops = bpf_ops;
  170. prog->filter = fp;
  171. prog->res.classid = classid;
  172. tcf_bind_filter(tp, &prog->res, base);
  173. tcf_exts_change(tp, &prog->exts, &exts);
  174. return 0;
  175. errout_free:
  176. kfree(bpf_ops);
  177. errout:
  178. tcf_exts_destroy(&exts);
  179. return ret;
  180. }
  181. static u32 cls_bpf_grab_new_handle(struct tcf_proto *tp,
  182. struct cls_bpf_head *head)
  183. {
  184. unsigned int i = 0x80000000;
  185. do {
  186. if (++head->hgen == 0x7FFFFFFF)
  187. head->hgen = 1;
  188. } while (--i > 0 && cls_bpf_get(tp, head->hgen));
  189. if (i == 0)
  190. pr_err("Insufficient number of handles\n");
  191. return i;
  192. }
  193. static int cls_bpf_change(struct net *net, struct sk_buff *in_skb,
  194. struct tcf_proto *tp, unsigned long base,
  195. u32 handle, struct nlattr **tca,
  196. unsigned long *arg, bool ovr)
  197. {
  198. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  199. struct cls_bpf_prog *oldprog = (struct cls_bpf_prog *) *arg;
  200. struct nlattr *tb[TCA_BPF_MAX + 1];
  201. struct cls_bpf_prog *prog;
  202. int ret;
  203. if (tca[TCA_OPTIONS] == NULL)
  204. return -EINVAL;
  205. ret = nla_parse_nested(tb, TCA_BPF_MAX, tca[TCA_OPTIONS], bpf_policy);
  206. if (ret < 0)
  207. return ret;
  208. prog = kzalloc(sizeof(*prog), GFP_KERNEL);
  209. if (!prog)
  210. return -ENOBUFS;
  211. tcf_exts_init(&prog->exts, TCA_BPF_ACT, TCA_BPF_POLICE);
  212. if (oldprog) {
  213. if (handle && oldprog->handle != handle) {
  214. ret = -EINVAL;
  215. goto errout;
  216. }
  217. }
  218. if (handle == 0)
  219. prog->handle = cls_bpf_grab_new_handle(tp, head);
  220. else
  221. prog->handle = handle;
  222. if (prog->handle == 0) {
  223. ret = -EINVAL;
  224. goto errout;
  225. }
  226. ret = cls_bpf_modify_existing(net, tp, prog, base, tb, tca[TCA_RATE], ovr);
  227. if (ret < 0)
  228. goto errout;
  229. if (oldprog) {
  230. list_replace_rcu(&prog->link, &oldprog->link);
  231. tcf_unbind_filter(tp, &oldprog->res);
  232. call_rcu(&oldprog->rcu, __cls_bpf_delete_prog);
  233. } else {
  234. list_add_rcu(&prog->link, &head->plist);
  235. }
  236. *arg = (unsigned long) prog;
  237. return 0;
  238. errout:
  239. kfree(prog);
  240. return ret;
  241. }
  242. static int cls_bpf_dump(struct net *net, struct tcf_proto *tp, unsigned long fh,
  243. struct sk_buff *skb, struct tcmsg *tm)
  244. {
  245. struct cls_bpf_prog *prog = (struct cls_bpf_prog *) fh;
  246. struct nlattr *nest, *nla;
  247. if (prog == NULL)
  248. return skb->len;
  249. tm->tcm_handle = prog->handle;
  250. nest = nla_nest_start(skb, TCA_OPTIONS);
  251. if (nest == NULL)
  252. goto nla_put_failure;
  253. if (nla_put_u32(skb, TCA_BPF_CLASSID, prog->res.classid))
  254. goto nla_put_failure;
  255. if (nla_put_u16(skb, TCA_BPF_OPS_LEN, prog->bpf_len))
  256. goto nla_put_failure;
  257. nla = nla_reserve(skb, TCA_BPF_OPS, prog->bpf_len *
  258. sizeof(struct sock_filter));
  259. if (nla == NULL)
  260. goto nla_put_failure;
  261. memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
  262. if (tcf_exts_dump(skb, &prog->exts) < 0)
  263. goto nla_put_failure;
  264. nla_nest_end(skb, nest);
  265. if (tcf_exts_dump_stats(skb, &prog->exts) < 0)
  266. goto nla_put_failure;
  267. return skb->len;
  268. nla_put_failure:
  269. nla_nest_cancel(skb, nest);
  270. return -1;
  271. }
  272. static void cls_bpf_walk(struct tcf_proto *tp, struct tcf_walker *arg)
  273. {
  274. struct cls_bpf_head *head = rtnl_dereference(tp->root);
  275. struct cls_bpf_prog *prog;
  276. list_for_each_entry_rcu(prog, &head->plist, link) {
  277. if (arg->count < arg->skip)
  278. goto skip;
  279. if (arg->fn(tp, (unsigned long) prog, arg) < 0) {
  280. arg->stop = 1;
  281. break;
  282. }
  283. skip:
  284. arg->count++;
  285. }
  286. }
  287. static struct tcf_proto_ops cls_bpf_ops __read_mostly = {
  288. .kind = "bpf",
  289. .owner = THIS_MODULE,
  290. .classify = cls_bpf_classify,
  291. .init = cls_bpf_init,
  292. .destroy = cls_bpf_destroy,
  293. .get = cls_bpf_get,
  294. .put = cls_bpf_put,
  295. .change = cls_bpf_change,
  296. .delete = cls_bpf_delete,
  297. .walk = cls_bpf_walk,
  298. .dump = cls_bpf_dump,
  299. };
  300. static int __init cls_bpf_init_mod(void)
  301. {
  302. return register_tcf_proto_ops(&cls_bpf_ops);
  303. }
  304. static void __exit cls_bpf_exit_mod(void)
  305. {
  306. unregister_tcf_proto_ops(&cls_bpf_ops);
  307. }
  308. module_init(cls_bpf_init_mod);
  309. module_exit(cls_bpf_exit_mod);