cls_bpf.c 8.1 KB

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