act_bpf.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Copyright (c) 2015 Jiri Pirko <jiri@resnulli.us>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. */
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/kernel.h>
  12. #include <linux/skbuff.h>
  13. #include <linux/rtnetlink.h>
  14. #include <linux/filter.h>
  15. #include <linux/bpf.h>
  16. #include <net/netlink.h>
  17. #include <net/pkt_sched.h>
  18. #include <linux/tc_act/tc_bpf.h>
  19. #include <net/tc_act/tc_bpf.h>
  20. #define BPF_TAB_MASK 15
  21. #define ACT_BPF_NAME_LEN 256
  22. struct tcf_bpf_cfg {
  23. struct bpf_prog *filter;
  24. struct sock_filter *bpf_ops;
  25. const char *bpf_name;
  26. u32 bpf_fd;
  27. u16 bpf_num_ops;
  28. bool is_ebpf;
  29. };
  30. static int bpf_net_id;
  31. static struct tc_action_ops act_bpf_ops;
  32. static int tcf_bpf(struct sk_buff *skb, const struct tc_action *act,
  33. struct tcf_result *res)
  34. {
  35. bool at_ingress = skb_at_tc_ingress(skb);
  36. struct tcf_bpf *prog = to_bpf(act);
  37. struct bpf_prog *filter;
  38. int action, filter_res;
  39. tcf_lastuse_update(&prog->tcf_tm);
  40. bstats_cpu_update(this_cpu_ptr(prog->common.cpu_bstats), skb);
  41. rcu_read_lock();
  42. filter = rcu_dereference(prog->filter);
  43. if (at_ingress) {
  44. __skb_push(skb, skb->mac_len);
  45. bpf_compute_data_end(skb);
  46. filter_res = BPF_PROG_RUN(filter, skb);
  47. __skb_pull(skb, skb->mac_len);
  48. } else {
  49. bpf_compute_data_end(skb);
  50. filter_res = BPF_PROG_RUN(filter, skb);
  51. }
  52. rcu_read_unlock();
  53. /* A BPF program may overwrite the default action opcode.
  54. * Similarly as in cls_bpf, if filter_res == -1 we use the
  55. * default action specified from tc.
  56. *
  57. * In case a different well-known TC_ACT opcode has been
  58. * returned, it will overwrite the default one.
  59. *
  60. * For everything else that is unkown, TC_ACT_UNSPEC is
  61. * returned.
  62. */
  63. switch (filter_res) {
  64. case TC_ACT_PIPE:
  65. case TC_ACT_RECLASSIFY:
  66. case TC_ACT_OK:
  67. case TC_ACT_REDIRECT:
  68. action = filter_res;
  69. break;
  70. case TC_ACT_SHOT:
  71. action = filter_res;
  72. qstats_drop_inc(this_cpu_ptr(prog->common.cpu_qstats));
  73. break;
  74. case TC_ACT_UNSPEC:
  75. action = prog->tcf_action;
  76. break;
  77. default:
  78. action = TC_ACT_UNSPEC;
  79. break;
  80. }
  81. return action;
  82. }
  83. static bool tcf_bpf_is_ebpf(const struct tcf_bpf *prog)
  84. {
  85. return !prog->bpf_ops;
  86. }
  87. static int tcf_bpf_dump_bpf_info(const struct tcf_bpf *prog,
  88. struct sk_buff *skb)
  89. {
  90. struct nlattr *nla;
  91. if (nla_put_u16(skb, TCA_ACT_BPF_OPS_LEN, prog->bpf_num_ops))
  92. return -EMSGSIZE;
  93. nla = nla_reserve(skb, TCA_ACT_BPF_OPS, prog->bpf_num_ops *
  94. sizeof(struct sock_filter));
  95. if (nla == NULL)
  96. return -EMSGSIZE;
  97. memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
  98. return 0;
  99. }
  100. static int tcf_bpf_dump_ebpf_info(const struct tcf_bpf *prog,
  101. struct sk_buff *skb)
  102. {
  103. if (nla_put_u32(skb, TCA_ACT_BPF_FD, prog->bpf_fd))
  104. return -EMSGSIZE;
  105. if (prog->bpf_name &&
  106. nla_put_string(skb, TCA_ACT_BPF_NAME, prog->bpf_name))
  107. return -EMSGSIZE;
  108. return 0;
  109. }
  110. static int tcf_bpf_dump(struct sk_buff *skb, struct tc_action *act,
  111. int bind, int ref)
  112. {
  113. unsigned char *tp = skb_tail_pointer(skb);
  114. struct tcf_bpf *prog = to_bpf(act);
  115. struct tc_act_bpf opt = {
  116. .index = prog->tcf_index,
  117. .refcnt = prog->tcf_refcnt - ref,
  118. .bindcnt = prog->tcf_bindcnt - bind,
  119. .action = prog->tcf_action,
  120. };
  121. struct tcf_t tm;
  122. int ret;
  123. if (nla_put(skb, TCA_ACT_BPF_PARMS, sizeof(opt), &opt))
  124. goto nla_put_failure;
  125. if (tcf_bpf_is_ebpf(prog))
  126. ret = tcf_bpf_dump_ebpf_info(prog, skb);
  127. else
  128. ret = tcf_bpf_dump_bpf_info(prog, skb);
  129. if (ret)
  130. goto nla_put_failure;
  131. tcf_tm_dump(&tm, &prog->tcf_tm);
  132. if (nla_put_64bit(skb, TCA_ACT_BPF_TM, sizeof(tm), &tm,
  133. TCA_ACT_BPF_PAD))
  134. goto nla_put_failure;
  135. return skb->len;
  136. nla_put_failure:
  137. nlmsg_trim(skb, tp);
  138. return -1;
  139. }
  140. static const struct nla_policy act_bpf_policy[TCA_ACT_BPF_MAX + 1] = {
  141. [TCA_ACT_BPF_PARMS] = { .len = sizeof(struct tc_act_bpf) },
  142. [TCA_ACT_BPF_FD] = { .type = NLA_U32 },
  143. [TCA_ACT_BPF_NAME] = { .type = NLA_NUL_STRING,
  144. .len = ACT_BPF_NAME_LEN },
  145. [TCA_ACT_BPF_OPS_LEN] = { .type = NLA_U16 },
  146. [TCA_ACT_BPF_OPS] = { .type = NLA_BINARY,
  147. .len = sizeof(struct sock_filter) * BPF_MAXINSNS },
  148. };
  149. static int tcf_bpf_init_from_ops(struct nlattr **tb, struct tcf_bpf_cfg *cfg)
  150. {
  151. struct sock_filter *bpf_ops;
  152. struct sock_fprog_kern fprog_tmp;
  153. struct bpf_prog *fp;
  154. u16 bpf_size, bpf_num_ops;
  155. int ret;
  156. bpf_num_ops = nla_get_u16(tb[TCA_ACT_BPF_OPS_LEN]);
  157. if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0)
  158. return -EINVAL;
  159. bpf_size = bpf_num_ops * sizeof(*bpf_ops);
  160. if (bpf_size != nla_len(tb[TCA_ACT_BPF_OPS]))
  161. return -EINVAL;
  162. bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
  163. if (bpf_ops == NULL)
  164. return -ENOMEM;
  165. memcpy(bpf_ops, nla_data(tb[TCA_ACT_BPF_OPS]), bpf_size);
  166. fprog_tmp.len = bpf_num_ops;
  167. fprog_tmp.filter = bpf_ops;
  168. ret = bpf_prog_create(&fp, &fprog_tmp);
  169. if (ret < 0) {
  170. kfree(bpf_ops);
  171. return ret;
  172. }
  173. cfg->bpf_ops = bpf_ops;
  174. cfg->bpf_num_ops = bpf_num_ops;
  175. cfg->filter = fp;
  176. cfg->is_ebpf = false;
  177. return 0;
  178. }
  179. static int tcf_bpf_init_from_efd(struct nlattr **tb, struct tcf_bpf_cfg *cfg)
  180. {
  181. struct bpf_prog *fp;
  182. char *name = NULL;
  183. u32 bpf_fd;
  184. bpf_fd = nla_get_u32(tb[TCA_ACT_BPF_FD]);
  185. fp = bpf_prog_get_type(bpf_fd, BPF_PROG_TYPE_SCHED_ACT);
  186. if (IS_ERR(fp))
  187. return PTR_ERR(fp);
  188. if (tb[TCA_ACT_BPF_NAME]) {
  189. name = kmemdup(nla_data(tb[TCA_ACT_BPF_NAME]),
  190. nla_len(tb[TCA_ACT_BPF_NAME]),
  191. GFP_KERNEL);
  192. if (!name) {
  193. bpf_prog_put(fp);
  194. return -ENOMEM;
  195. }
  196. }
  197. cfg->bpf_fd = bpf_fd;
  198. cfg->bpf_name = name;
  199. cfg->filter = fp;
  200. cfg->is_ebpf = true;
  201. return 0;
  202. }
  203. static void tcf_bpf_cfg_cleanup(const struct tcf_bpf_cfg *cfg)
  204. {
  205. struct bpf_prog *filter = cfg->filter;
  206. if (filter) {
  207. if (cfg->is_ebpf)
  208. bpf_prog_put(filter);
  209. else
  210. bpf_prog_destroy(filter);
  211. }
  212. kfree(cfg->bpf_ops);
  213. kfree(cfg->bpf_name);
  214. }
  215. static void tcf_bpf_prog_fill_cfg(const struct tcf_bpf *prog,
  216. struct tcf_bpf_cfg *cfg)
  217. {
  218. cfg->is_ebpf = tcf_bpf_is_ebpf(prog);
  219. /* updates to prog->filter are prevented, since it's called either
  220. * with rtnl lock or during final cleanup in rcu callback
  221. */
  222. cfg->filter = rcu_dereference_protected(prog->filter, 1);
  223. cfg->bpf_ops = prog->bpf_ops;
  224. cfg->bpf_name = prog->bpf_name;
  225. }
  226. static int tcf_bpf_init(struct net *net, struct nlattr *nla,
  227. struct nlattr *est, struct tc_action **act,
  228. int replace, int bind)
  229. {
  230. struct tc_action_net *tn = net_generic(net, bpf_net_id);
  231. struct nlattr *tb[TCA_ACT_BPF_MAX + 1];
  232. struct tcf_bpf_cfg cfg, old;
  233. struct tc_act_bpf *parm;
  234. struct tcf_bpf *prog;
  235. bool is_bpf, is_ebpf;
  236. int ret, res = 0;
  237. if (!nla)
  238. return -EINVAL;
  239. ret = nla_parse_nested(tb, TCA_ACT_BPF_MAX, nla, act_bpf_policy);
  240. if (ret < 0)
  241. return ret;
  242. if (!tb[TCA_ACT_BPF_PARMS])
  243. return -EINVAL;
  244. parm = nla_data(tb[TCA_ACT_BPF_PARMS]);
  245. if (!tcf_hash_check(tn, parm->index, act, bind)) {
  246. ret = tcf_hash_create(tn, parm->index, est, act,
  247. &act_bpf_ops, bind, true);
  248. if (ret < 0)
  249. return ret;
  250. res = ACT_P_CREATED;
  251. } else {
  252. /* Don't override defaults. */
  253. if (bind)
  254. return 0;
  255. tcf_hash_release(*act, bind);
  256. if (!replace)
  257. return -EEXIST;
  258. }
  259. is_bpf = tb[TCA_ACT_BPF_OPS_LEN] && tb[TCA_ACT_BPF_OPS];
  260. is_ebpf = tb[TCA_ACT_BPF_FD];
  261. if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf)) {
  262. ret = -EINVAL;
  263. goto out;
  264. }
  265. memset(&cfg, 0, sizeof(cfg));
  266. ret = is_bpf ? tcf_bpf_init_from_ops(tb, &cfg) :
  267. tcf_bpf_init_from_efd(tb, &cfg);
  268. if (ret < 0)
  269. goto out;
  270. prog = to_bpf(*act);
  271. ASSERT_RTNL();
  272. if (res != ACT_P_CREATED)
  273. tcf_bpf_prog_fill_cfg(prog, &old);
  274. prog->bpf_ops = cfg.bpf_ops;
  275. prog->bpf_name = cfg.bpf_name;
  276. if (cfg.bpf_num_ops)
  277. prog->bpf_num_ops = cfg.bpf_num_ops;
  278. if (cfg.bpf_fd)
  279. prog->bpf_fd = cfg.bpf_fd;
  280. prog->tcf_action = parm->action;
  281. rcu_assign_pointer(prog->filter, cfg.filter);
  282. if (res == ACT_P_CREATED) {
  283. tcf_hash_insert(tn, *act);
  284. } else {
  285. /* make sure the program being replaced is no longer executing */
  286. synchronize_rcu();
  287. tcf_bpf_cfg_cleanup(&old);
  288. }
  289. return res;
  290. out:
  291. if (res == ACT_P_CREATED)
  292. tcf_hash_cleanup(*act, est);
  293. return ret;
  294. }
  295. static void tcf_bpf_cleanup(struct tc_action *act, int bind)
  296. {
  297. struct tcf_bpf_cfg tmp;
  298. tcf_bpf_prog_fill_cfg(to_bpf(act), &tmp);
  299. tcf_bpf_cfg_cleanup(&tmp);
  300. }
  301. static int tcf_bpf_walker(struct net *net, struct sk_buff *skb,
  302. struct netlink_callback *cb, int type,
  303. const struct tc_action_ops *ops)
  304. {
  305. struct tc_action_net *tn = net_generic(net, bpf_net_id);
  306. return tcf_generic_walker(tn, skb, cb, type, ops);
  307. }
  308. static int tcf_bpf_search(struct net *net, struct tc_action **a, u32 index)
  309. {
  310. struct tc_action_net *tn = net_generic(net, bpf_net_id);
  311. return tcf_hash_search(tn, a, index);
  312. }
  313. static struct tc_action_ops act_bpf_ops __read_mostly = {
  314. .kind = "bpf",
  315. .type = TCA_ACT_BPF,
  316. .owner = THIS_MODULE,
  317. .act = tcf_bpf,
  318. .dump = tcf_bpf_dump,
  319. .cleanup = tcf_bpf_cleanup,
  320. .init = tcf_bpf_init,
  321. .walk = tcf_bpf_walker,
  322. .lookup = tcf_bpf_search,
  323. .size = sizeof(struct tcf_bpf),
  324. };
  325. static __net_init int bpf_init_net(struct net *net)
  326. {
  327. struct tc_action_net *tn = net_generic(net, bpf_net_id);
  328. return tc_action_net_init(tn, &act_bpf_ops, BPF_TAB_MASK);
  329. }
  330. static void __net_exit bpf_exit_net(struct net *net)
  331. {
  332. struct tc_action_net *tn = net_generic(net, bpf_net_id);
  333. tc_action_net_exit(tn);
  334. }
  335. static struct pernet_operations bpf_net_ops = {
  336. .init = bpf_init_net,
  337. .exit = bpf_exit_net,
  338. .id = &bpf_net_id,
  339. .size = sizeof(struct tc_action_net),
  340. };
  341. static int __init bpf_init_module(void)
  342. {
  343. return tcf_register_action(&act_bpf_ops, &bpf_net_ops);
  344. }
  345. static void __exit bpf_cleanup_module(void)
  346. {
  347. tcf_unregister_action(&act_bpf_ops, &bpf_net_ops);
  348. }
  349. module_init(bpf_init_module);
  350. module_exit(bpf_cleanup_module);
  351. MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
  352. MODULE_DESCRIPTION("TC BPF based action");
  353. MODULE_LICENSE("GPL v2");