cls_bpf.c 12 KB

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