cls_bpf.c 11 KB

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