act_bpf.c 7.8 KB

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