act_bpf.c 4.8 KB

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