act_bpf.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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. int action, filter_res;
  35. bool at_ingress = G_TC_AT(skb->tc_verd) & AT_INGRESS;
  36. if (unlikely(!skb_mac_header_was_set(skb)))
  37. return TC_ACT_UNSPEC;
  38. spin_lock(&prog->tcf_lock);
  39. prog->tcf_tm.lastuse = jiffies;
  40. bstats_update(&prog->tcf_bstats, skb);
  41. /* Needed here for accessing maps. */
  42. rcu_read_lock();
  43. if (at_ingress) {
  44. __skb_push(skb, skb->mac_len);
  45. filter_res = BPF_PROG_RUN(prog->filter, skb);
  46. __skb_pull(skb, skb->mac_len);
  47. } else {
  48. filter_res = BPF_PROG_RUN(prog->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. prog->tcf_qstats.drops++;
  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. spin_unlock(&prog->tcf_lock);
  79. return action;
  80. }
  81. static bool tcf_bpf_is_ebpf(const struct tcf_bpf *prog)
  82. {
  83. return !prog->bpf_ops;
  84. }
  85. static int tcf_bpf_dump_bpf_info(const struct tcf_bpf *prog,
  86. struct sk_buff *skb)
  87. {
  88. struct nlattr *nla;
  89. if (nla_put_u16(skb, TCA_ACT_BPF_OPS_LEN, prog->bpf_num_ops))
  90. return -EMSGSIZE;
  91. nla = nla_reserve(skb, TCA_ACT_BPF_OPS, prog->bpf_num_ops *
  92. sizeof(struct sock_filter));
  93. if (nla == NULL)
  94. return -EMSGSIZE;
  95. memcpy(nla_data(nla), prog->bpf_ops, nla_len(nla));
  96. return 0;
  97. }
  98. static int tcf_bpf_dump_ebpf_info(const struct tcf_bpf *prog,
  99. struct sk_buff *skb)
  100. {
  101. if (nla_put_u32(skb, TCA_ACT_BPF_FD, prog->bpf_fd))
  102. return -EMSGSIZE;
  103. if (prog->bpf_name &&
  104. nla_put_string(skb, TCA_ACT_BPF_NAME, prog->bpf_name))
  105. return -EMSGSIZE;
  106. return 0;
  107. }
  108. static int tcf_bpf_dump(struct sk_buff *skb, struct tc_action *act,
  109. int bind, int ref)
  110. {
  111. unsigned char *tp = skb_tail_pointer(skb);
  112. struct tcf_bpf *prog = act->priv;
  113. struct tc_act_bpf opt = {
  114. .index = prog->tcf_index,
  115. .refcnt = prog->tcf_refcnt - ref,
  116. .bindcnt = prog->tcf_bindcnt - bind,
  117. .action = prog->tcf_action,
  118. };
  119. struct tcf_t tm;
  120. int ret;
  121. if (nla_put(skb, TCA_ACT_BPF_PARMS, sizeof(opt), &opt))
  122. goto nla_put_failure;
  123. if (tcf_bpf_is_ebpf(prog))
  124. ret = tcf_bpf_dump_ebpf_info(prog, skb);
  125. else
  126. ret = tcf_bpf_dump_bpf_info(prog, skb);
  127. if (ret)
  128. goto nla_put_failure;
  129. tm.install = jiffies_to_clock_t(jiffies - prog->tcf_tm.install);
  130. tm.lastuse = jiffies_to_clock_t(jiffies - prog->tcf_tm.lastuse);
  131. tm.expires = jiffies_to_clock_t(prog->tcf_tm.expires);
  132. if (nla_put(skb, TCA_ACT_BPF_TM, sizeof(tm), &tm))
  133. goto nla_put_failure;
  134. return skb->len;
  135. nla_put_failure:
  136. nlmsg_trim(skb, tp);
  137. return -1;
  138. }
  139. static const struct nla_policy act_bpf_policy[TCA_ACT_BPF_MAX + 1] = {
  140. [TCA_ACT_BPF_PARMS] = { .len = sizeof(struct tc_act_bpf) },
  141. [TCA_ACT_BPF_FD] = { .type = NLA_U32 },
  142. [TCA_ACT_BPF_NAME] = { .type = NLA_NUL_STRING, .len = ACT_BPF_NAME_LEN },
  143. [TCA_ACT_BPF_OPS_LEN] = { .type = NLA_U16 },
  144. [TCA_ACT_BPF_OPS] = { .type = NLA_BINARY,
  145. .len = sizeof(struct sock_filter) * BPF_MAXINSNS },
  146. };
  147. static int tcf_bpf_init_from_ops(struct nlattr **tb, struct tcf_bpf_cfg *cfg)
  148. {
  149. struct sock_filter *bpf_ops;
  150. struct sock_fprog_kern fprog_tmp;
  151. struct bpf_prog *fp;
  152. u16 bpf_size, bpf_num_ops;
  153. int ret;
  154. bpf_num_ops = nla_get_u16(tb[TCA_ACT_BPF_OPS_LEN]);
  155. if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0)
  156. return -EINVAL;
  157. bpf_size = bpf_num_ops * sizeof(*bpf_ops);
  158. if (bpf_size != nla_len(tb[TCA_ACT_BPF_OPS]))
  159. return -EINVAL;
  160. bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
  161. if (bpf_ops == NULL)
  162. return -ENOMEM;
  163. memcpy(bpf_ops, nla_data(tb[TCA_ACT_BPF_OPS]), bpf_size);
  164. fprog_tmp.len = bpf_num_ops;
  165. fprog_tmp.filter = bpf_ops;
  166. ret = bpf_prog_create(&fp, &fprog_tmp);
  167. if (ret < 0) {
  168. kfree(bpf_ops);
  169. return ret;
  170. }
  171. cfg->bpf_ops = bpf_ops;
  172. cfg->bpf_num_ops = bpf_num_ops;
  173. cfg->filter = fp;
  174. cfg->is_ebpf = false;
  175. return 0;
  176. }
  177. static int tcf_bpf_init_from_efd(struct nlattr **tb, struct tcf_bpf_cfg *cfg)
  178. {
  179. struct bpf_prog *fp;
  180. char *name = NULL;
  181. u32 bpf_fd;
  182. bpf_fd = nla_get_u32(tb[TCA_ACT_BPF_FD]);
  183. fp = bpf_prog_get(bpf_fd);
  184. if (IS_ERR(fp))
  185. return PTR_ERR(fp);
  186. if (fp->type != BPF_PROG_TYPE_SCHED_ACT) {
  187. bpf_prog_put(fp);
  188. return -EINVAL;
  189. }
  190. if (tb[TCA_ACT_BPF_NAME]) {
  191. name = kmemdup(nla_data(tb[TCA_ACT_BPF_NAME]),
  192. nla_len(tb[TCA_ACT_BPF_NAME]),
  193. GFP_KERNEL);
  194. if (!name) {
  195. bpf_prog_put(fp);
  196. return -ENOMEM;
  197. }
  198. }
  199. cfg->bpf_fd = bpf_fd;
  200. cfg->bpf_name = name;
  201. cfg->filter = fp;
  202. cfg->is_ebpf = true;
  203. return 0;
  204. }
  205. static void tcf_bpf_cfg_cleanup(const struct tcf_bpf_cfg *cfg)
  206. {
  207. if (cfg->is_ebpf)
  208. bpf_prog_put(cfg->filter);
  209. else
  210. bpf_prog_destroy(cfg->filter);
  211. kfree(cfg->bpf_ops);
  212. kfree(cfg->bpf_name);
  213. }
  214. static void tcf_bpf_prog_fill_cfg(const struct tcf_bpf *prog,
  215. struct tcf_bpf_cfg *cfg)
  216. {
  217. cfg->is_ebpf = tcf_bpf_is_ebpf(prog);
  218. cfg->filter = prog->filter;
  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 nlattr *tb[TCA_ACT_BPF_MAX + 1];
  227. struct tcf_bpf_cfg cfg, old;
  228. struct tc_act_bpf *parm;
  229. struct tcf_bpf *prog;
  230. bool is_bpf, is_ebpf;
  231. int ret;
  232. if (!nla)
  233. return -EINVAL;
  234. ret = nla_parse_nested(tb, TCA_ACT_BPF_MAX, nla, act_bpf_policy);
  235. if (ret < 0)
  236. return ret;
  237. is_bpf = tb[TCA_ACT_BPF_OPS_LEN] && tb[TCA_ACT_BPF_OPS];
  238. is_ebpf = tb[TCA_ACT_BPF_FD];
  239. if ((!is_bpf && !is_ebpf) || (is_bpf && is_ebpf) ||
  240. !tb[TCA_ACT_BPF_PARMS])
  241. return -EINVAL;
  242. parm = nla_data(tb[TCA_ACT_BPF_PARMS]);
  243. memset(&cfg, 0, sizeof(cfg));
  244. ret = is_bpf ? tcf_bpf_init_from_ops(tb, &cfg) :
  245. tcf_bpf_init_from_efd(tb, &cfg);
  246. if (ret < 0)
  247. return ret;
  248. if (!tcf_hash_check(parm->index, act, bind)) {
  249. ret = tcf_hash_create(parm->index, est, act,
  250. sizeof(*prog), bind);
  251. if (ret < 0)
  252. goto destroy_fp;
  253. ret = ACT_P_CREATED;
  254. } else {
  255. /* Don't override defaults. */
  256. if (bind)
  257. goto destroy_fp;
  258. tcf_hash_release(act, bind);
  259. if (!replace) {
  260. ret = -EEXIST;
  261. goto destroy_fp;
  262. }
  263. }
  264. prog = to_bpf(act);
  265. spin_lock_bh(&prog->tcf_lock);
  266. if (ret != ACT_P_CREATED)
  267. tcf_bpf_prog_fill_cfg(prog, &old);
  268. prog->bpf_ops = cfg.bpf_ops;
  269. prog->bpf_name = cfg.bpf_name;
  270. if (cfg.bpf_num_ops)
  271. prog->bpf_num_ops = cfg.bpf_num_ops;
  272. if (cfg.bpf_fd)
  273. prog->bpf_fd = cfg.bpf_fd;
  274. prog->tcf_action = parm->action;
  275. prog->filter = cfg.filter;
  276. spin_unlock_bh(&prog->tcf_lock);
  277. if (ret == ACT_P_CREATED)
  278. tcf_hash_insert(act);
  279. else
  280. tcf_bpf_cfg_cleanup(&old);
  281. return ret;
  282. destroy_fp:
  283. tcf_bpf_cfg_cleanup(&cfg);
  284. return ret;
  285. }
  286. static void tcf_bpf_cleanup(struct tc_action *act, int bind)
  287. {
  288. struct tcf_bpf_cfg tmp;
  289. tcf_bpf_prog_fill_cfg(act->priv, &tmp);
  290. tcf_bpf_cfg_cleanup(&tmp);
  291. }
  292. static struct tc_action_ops act_bpf_ops __read_mostly = {
  293. .kind = "bpf",
  294. .type = TCA_ACT_BPF,
  295. .owner = THIS_MODULE,
  296. .act = tcf_bpf,
  297. .dump = tcf_bpf_dump,
  298. .cleanup = tcf_bpf_cleanup,
  299. .init = tcf_bpf_init,
  300. };
  301. static int __init bpf_init_module(void)
  302. {
  303. return tcf_register_action(&act_bpf_ops, BPF_TAB_MASK);
  304. }
  305. static void __exit bpf_cleanup_module(void)
  306. {
  307. tcf_unregister_action(&act_bpf_ops);
  308. }
  309. module_init(bpf_init_module);
  310. module_exit(bpf_cleanup_module);
  311. MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
  312. MODULE_DESCRIPTION("TC BPF based action");
  313. MODULE_LICENSE("GPL v2");