act_bpf.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 <net/netlink.h>
  16. #include <net/pkt_sched.h>
  17. #include <linux/tc_act/tc_bpf.h>
  18. #include <net/tc_act/tc_bpf.h>
  19. #define BPF_TAB_MASK 15
  20. static int tcf_bpf(struct sk_buff *skb, const struct tc_action *a,
  21. struct tcf_result *res)
  22. {
  23. struct tcf_bpf *b = a->priv;
  24. int action, filter_res;
  25. spin_lock(&b->tcf_lock);
  26. b->tcf_tm.lastuse = jiffies;
  27. bstats_update(&b->tcf_bstats, skb);
  28. filter_res = BPF_PROG_RUN(b->filter, skb);
  29. /* A BPF program may overwrite the default action opcode.
  30. * Similarly as in cls_bpf, if filter_res == -1 we use the
  31. * default action specified from tc.
  32. *
  33. * In case a different well-known TC_ACT opcode has been
  34. * returned, it will overwrite the default one.
  35. *
  36. * For everything else that is unkown, TC_ACT_UNSPEC is
  37. * returned.
  38. */
  39. switch (filter_res) {
  40. case TC_ACT_PIPE:
  41. case TC_ACT_RECLASSIFY:
  42. case TC_ACT_OK:
  43. action = filter_res;
  44. break;
  45. case TC_ACT_SHOT:
  46. action = filter_res;
  47. b->tcf_qstats.drops++;
  48. break;
  49. case TC_ACT_UNSPEC:
  50. action = b->tcf_action;
  51. break;
  52. default:
  53. action = TC_ACT_UNSPEC;
  54. break;
  55. }
  56. spin_unlock(&b->tcf_lock);
  57. return action;
  58. }
  59. static int tcf_bpf_dump(struct sk_buff *skb, struct tc_action *a,
  60. int bind, int ref)
  61. {
  62. unsigned char *tp = skb_tail_pointer(skb);
  63. struct tcf_bpf *b = a->priv;
  64. struct tc_act_bpf opt = {
  65. .index = b->tcf_index,
  66. .refcnt = b->tcf_refcnt - ref,
  67. .bindcnt = b->tcf_bindcnt - bind,
  68. .action = b->tcf_action,
  69. };
  70. struct tcf_t t;
  71. struct nlattr *nla;
  72. if (nla_put(skb, TCA_ACT_BPF_PARMS, sizeof(opt), &opt))
  73. goto nla_put_failure;
  74. if (nla_put_u16(skb, TCA_ACT_BPF_OPS_LEN, b->bpf_num_ops))
  75. goto nla_put_failure;
  76. nla = nla_reserve(skb, TCA_ACT_BPF_OPS, b->bpf_num_ops *
  77. sizeof(struct sock_filter));
  78. if (!nla)
  79. goto nla_put_failure;
  80. memcpy(nla_data(nla), b->bpf_ops, nla_len(nla));
  81. t.install = jiffies_to_clock_t(jiffies - b->tcf_tm.install);
  82. t.lastuse = jiffies_to_clock_t(jiffies - b->tcf_tm.lastuse);
  83. t.expires = jiffies_to_clock_t(b->tcf_tm.expires);
  84. if (nla_put(skb, TCA_ACT_BPF_TM, sizeof(t), &t))
  85. goto nla_put_failure;
  86. return skb->len;
  87. nla_put_failure:
  88. nlmsg_trim(skb, tp);
  89. return -1;
  90. }
  91. static const struct nla_policy act_bpf_policy[TCA_ACT_BPF_MAX + 1] = {
  92. [TCA_ACT_BPF_PARMS] = { .len = sizeof(struct tc_act_bpf) },
  93. [TCA_ACT_BPF_OPS_LEN] = { .type = NLA_U16 },
  94. [TCA_ACT_BPF_OPS] = { .type = NLA_BINARY,
  95. .len = sizeof(struct sock_filter) * BPF_MAXINSNS },
  96. };
  97. static int tcf_bpf_init(struct net *net, struct nlattr *nla,
  98. struct nlattr *est, struct tc_action *a,
  99. int ovr, int bind)
  100. {
  101. struct nlattr *tb[TCA_ACT_BPF_MAX + 1];
  102. struct tc_act_bpf *parm;
  103. struct tcf_bpf *b;
  104. u16 bpf_size, bpf_num_ops;
  105. struct sock_filter *bpf_ops;
  106. struct sock_fprog_kern tmp;
  107. struct bpf_prog *fp;
  108. int ret;
  109. if (!nla)
  110. return -EINVAL;
  111. ret = nla_parse_nested(tb, TCA_ACT_BPF_MAX, nla, act_bpf_policy);
  112. if (ret < 0)
  113. return ret;
  114. if (!tb[TCA_ACT_BPF_PARMS] ||
  115. !tb[TCA_ACT_BPF_OPS_LEN] || !tb[TCA_ACT_BPF_OPS])
  116. return -EINVAL;
  117. parm = nla_data(tb[TCA_ACT_BPF_PARMS]);
  118. bpf_num_ops = nla_get_u16(tb[TCA_ACT_BPF_OPS_LEN]);
  119. if (bpf_num_ops > BPF_MAXINSNS || bpf_num_ops == 0)
  120. return -EINVAL;
  121. bpf_size = bpf_num_ops * sizeof(*bpf_ops);
  122. if (bpf_size != nla_len(tb[TCA_ACT_BPF_OPS]))
  123. return -EINVAL;
  124. bpf_ops = kzalloc(bpf_size, GFP_KERNEL);
  125. if (!bpf_ops)
  126. return -ENOMEM;
  127. memcpy(bpf_ops, nla_data(tb[TCA_ACT_BPF_OPS]), bpf_size);
  128. tmp.len = bpf_num_ops;
  129. tmp.filter = bpf_ops;
  130. ret = bpf_prog_create(&fp, &tmp);
  131. if (ret)
  132. goto free_bpf_ops;
  133. if (!tcf_hash_check(parm->index, a, bind)) {
  134. ret = tcf_hash_create(parm->index, est, a, sizeof(*b), bind);
  135. if (ret)
  136. goto destroy_fp;
  137. ret = ACT_P_CREATED;
  138. } else {
  139. if (bind)
  140. goto destroy_fp;
  141. tcf_hash_release(a, bind);
  142. if (!ovr) {
  143. ret = -EEXIST;
  144. goto destroy_fp;
  145. }
  146. }
  147. b = to_bpf(a);
  148. spin_lock_bh(&b->tcf_lock);
  149. b->tcf_action = parm->action;
  150. b->bpf_num_ops = bpf_num_ops;
  151. b->bpf_ops = bpf_ops;
  152. b->filter = fp;
  153. spin_unlock_bh(&b->tcf_lock);
  154. if (ret == ACT_P_CREATED)
  155. tcf_hash_insert(a);
  156. return ret;
  157. destroy_fp:
  158. bpf_prog_destroy(fp);
  159. free_bpf_ops:
  160. kfree(bpf_ops);
  161. return ret;
  162. }
  163. static void tcf_bpf_cleanup(struct tc_action *a, int bind)
  164. {
  165. struct tcf_bpf *b = a->priv;
  166. bpf_prog_destroy(b->filter);
  167. }
  168. static struct tc_action_ops act_bpf_ops = {
  169. .kind = "bpf",
  170. .type = TCA_ACT_BPF,
  171. .owner = THIS_MODULE,
  172. .act = tcf_bpf,
  173. .dump = tcf_bpf_dump,
  174. .cleanup = tcf_bpf_cleanup,
  175. .init = tcf_bpf_init,
  176. };
  177. static int __init bpf_init_module(void)
  178. {
  179. return tcf_register_action(&act_bpf_ops, BPF_TAB_MASK);
  180. }
  181. static void __exit bpf_cleanup_module(void)
  182. {
  183. tcf_unregister_action(&act_bpf_ops);
  184. }
  185. module_init(bpf_init_module);
  186. module_exit(bpf_cleanup_module);
  187. MODULE_AUTHOR("Jiri Pirko <jiri@resnulli.us>");
  188. MODULE_DESCRIPTION("TC BPF based action");
  189. MODULE_LICENSE("GPL v2");