act_bpf.c 9.7 KB

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