act_bpf.c 8.7 KB

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