lwt_bpf.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /* Copyright (c) 2016 Thomas Graf <tgraf@tgraf.ch>
  2. *
  3. * This program is free software; you can redistribute it and/or
  4. * modify it under the terms of version 2 of the GNU General Public
  5. * License as published by the Free Software Foundation.
  6. *
  7. * This program is distributed in the hope that it will be useful, but
  8. * WITHOUT ANY WARRANTY; without even the implied warranty of
  9. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. * General Public License for more details.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #include <linux/skbuff.h>
  15. #include <linux/types.h>
  16. #include <linux/bpf.h>
  17. #include <net/lwtunnel.h>
  18. struct bpf_lwt_prog {
  19. struct bpf_prog *prog;
  20. char *name;
  21. };
  22. struct bpf_lwt {
  23. struct bpf_lwt_prog in;
  24. struct bpf_lwt_prog out;
  25. struct bpf_lwt_prog xmit;
  26. int family;
  27. };
  28. #define MAX_PROG_NAME 256
  29. static inline struct bpf_lwt *bpf_lwt_lwtunnel(struct lwtunnel_state *lwt)
  30. {
  31. return (struct bpf_lwt *)lwt->data;
  32. }
  33. #define NO_REDIRECT false
  34. #define CAN_REDIRECT true
  35. static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt,
  36. struct dst_entry *dst, bool can_redirect)
  37. {
  38. int ret;
  39. /* Preempt disable is needed to protect per-cpu redirect_info between
  40. * BPF prog and skb_do_redirect(). The call_rcu in bpf_prog_put() and
  41. * access to maps strictly require a rcu_read_lock() for protection,
  42. * mixing with BH RCU lock doesn't work.
  43. */
  44. preempt_disable();
  45. bpf_compute_data_pointers(skb);
  46. ret = bpf_prog_run_save_cb(lwt->prog, skb);
  47. switch (ret) {
  48. case BPF_OK:
  49. break;
  50. case BPF_REDIRECT:
  51. if (unlikely(!can_redirect)) {
  52. pr_warn_once("Illegal redirect return code in prog %s\n",
  53. lwt->name ? : "<unknown>");
  54. ret = BPF_OK;
  55. } else {
  56. ret = skb_do_redirect(skb);
  57. if (ret == 0)
  58. ret = BPF_REDIRECT;
  59. }
  60. break;
  61. case BPF_DROP:
  62. kfree_skb(skb);
  63. ret = -EPERM;
  64. break;
  65. default:
  66. pr_warn_once("bpf-lwt: Illegal return value %u, expect packet loss\n", ret);
  67. kfree_skb(skb);
  68. ret = -EINVAL;
  69. break;
  70. }
  71. preempt_enable();
  72. return ret;
  73. }
  74. static int bpf_input(struct sk_buff *skb)
  75. {
  76. struct dst_entry *dst = skb_dst(skb);
  77. struct bpf_lwt *bpf;
  78. int ret;
  79. bpf = bpf_lwt_lwtunnel(dst->lwtstate);
  80. if (bpf->in.prog) {
  81. ret = run_lwt_bpf(skb, &bpf->in, dst, NO_REDIRECT);
  82. if (ret < 0)
  83. return ret;
  84. }
  85. if (unlikely(!dst->lwtstate->orig_input)) {
  86. pr_warn_once("orig_input not set on dst for prog %s\n",
  87. bpf->out.name);
  88. kfree_skb(skb);
  89. return -EINVAL;
  90. }
  91. return dst->lwtstate->orig_input(skb);
  92. }
  93. static int bpf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  94. {
  95. struct dst_entry *dst = skb_dst(skb);
  96. struct bpf_lwt *bpf;
  97. int ret;
  98. bpf = bpf_lwt_lwtunnel(dst->lwtstate);
  99. if (bpf->out.prog) {
  100. ret = run_lwt_bpf(skb, &bpf->out, dst, NO_REDIRECT);
  101. if (ret < 0)
  102. return ret;
  103. }
  104. if (unlikely(!dst->lwtstate->orig_output)) {
  105. pr_warn_once("orig_output not set on dst for prog %s\n",
  106. bpf->out.name);
  107. kfree_skb(skb);
  108. return -EINVAL;
  109. }
  110. return dst->lwtstate->orig_output(net, sk, skb);
  111. }
  112. static int xmit_check_hhlen(struct sk_buff *skb)
  113. {
  114. int hh_len = skb_dst(skb)->dev->hard_header_len;
  115. if (skb_headroom(skb) < hh_len) {
  116. int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
  117. if (pskb_expand_head(skb, nhead, 0, GFP_ATOMIC))
  118. return -ENOMEM;
  119. }
  120. return 0;
  121. }
  122. static int bpf_xmit(struct sk_buff *skb)
  123. {
  124. struct dst_entry *dst = skb_dst(skb);
  125. struct bpf_lwt *bpf;
  126. bpf = bpf_lwt_lwtunnel(dst->lwtstate);
  127. if (bpf->xmit.prog) {
  128. int ret;
  129. ret = run_lwt_bpf(skb, &bpf->xmit, dst, CAN_REDIRECT);
  130. switch (ret) {
  131. case BPF_OK:
  132. /* If the header was expanded, headroom might be too
  133. * small for L2 header to come, expand as needed.
  134. */
  135. ret = xmit_check_hhlen(skb);
  136. if (unlikely(ret))
  137. return ret;
  138. return LWTUNNEL_XMIT_CONTINUE;
  139. case BPF_REDIRECT:
  140. return LWTUNNEL_XMIT_DONE;
  141. default:
  142. return ret;
  143. }
  144. }
  145. return LWTUNNEL_XMIT_CONTINUE;
  146. }
  147. static void bpf_lwt_prog_destroy(struct bpf_lwt_prog *prog)
  148. {
  149. if (prog->prog)
  150. bpf_prog_put(prog->prog);
  151. kfree(prog->name);
  152. }
  153. static void bpf_destroy_state(struct lwtunnel_state *lwt)
  154. {
  155. struct bpf_lwt *bpf = bpf_lwt_lwtunnel(lwt);
  156. bpf_lwt_prog_destroy(&bpf->in);
  157. bpf_lwt_prog_destroy(&bpf->out);
  158. bpf_lwt_prog_destroy(&bpf->xmit);
  159. }
  160. static const struct nla_policy bpf_prog_policy[LWT_BPF_PROG_MAX + 1] = {
  161. [LWT_BPF_PROG_FD] = { .type = NLA_U32, },
  162. [LWT_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
  163. .len = MAX_PROG_NAME },
  164. };
  165. static int bpf_parse_prog(struct nlattr *attr, struct bpf_lwt_prog *prog,
  166. enum bpf_prog_type type)
  167. {
  168. struct nlattr *tb[LWT_BPF_PROG_MAX + 1];
  169. struct bpf_prog *p;
  170. int ret;
  171. u32 fd;
  172. ret = nla_parse_nested(tb, LWT_BPF_PROG_MAX, attr, bpf_prog_policy,
  173. NULL);
  174. if (ret < 0)
  175. return ret;
  176. if (!tb[LWT_BPF_PROG_FD] || !tb[LWT_BPF_PROG_NAME])
  177. return -EINVAL;
  178. prog->name = nla_memdup(tb[LWT_BPF_PROG_NAME], GFP_ATOMIC);
  179. if (!prog->name)
  180. return -ENOMEM;
  181. fd = nla_get_u32(tb[LWT_BPF_PROG_FD]);
  182. p = bpf_prog_get_type(fd, type);
  183. if (IS_ERR(p))
  184. return PTR_ERR(p);
  185. prog->prog = p;
  186. return 0;
  187. }
  188. static const struct nla_policy bpf_nl_policy[LWT_BPF_MAX + 1] = {
  189. [LWT_BPF_IN] = { .type = NLA_NESTED, },
  190. [LWT_BPF_OUT] = { .type = NLA_NESTED, },
  191. [LWT_BPF_XMIT] = { .type = NLA_NESTED, },
  192. [LWT_BPF_XMIT_HEADROOM] = { .type = NLA_U32 },
  193. };
  194. static int bpf_build_state(struct nlattr *nla,
  195. unsigned int family, const void *cfg,
  196. struct lwtunnel_state **ts,
  197. struct netlink_ext_ack *extack)
  198. {
  199. struct nlattr *tb[LWT_BPF_MAX + 1];
  200. struct lwtunnel_state *newts;
  201. struct bpf_lwt *bpf;
  202. int ret;
  203. if (family != AF_INET && family != AF_INET6)
  204. return -EAFNOSUPPORT;
  205. ret = nla_parse_nested(tb, LWT_BPF_MAX, nla, bpf_nl_policy, extack);
  206. if (ret < 0)
  207. return ret;
  208. if (!tb[LWT_BPF_IN] && !tb[LWT_BPF_OUT] && !tb[LWT_BPF_XMIT])
  209. return -EINVAL;
  210. newts = lwtunnel_state_alloc(sizeof(*bpf));
  211. if (!newts)
  212. return -ENOMEM;
  213. newts->type = LWTUNNEL_ENCAP_BPF;
  214. bpf = bpf_lwt_lwtunnel(newts);
  215. if (tb[LWT_BPF_IN]) {
  216. newts->flags |= LWTUNNEL_STATE_INPUT_REDIRECT;
  217. ret = bpf_parse_prog(tb[LWT_BPF_IN], &bpf->in,
  218. BPF_PROG_TYPE_LWT_IN);
  219. if (ret < 0)
  220. goto errout;
  221. }
  222. if (tb[LWT_BPF_OUT]) {
  223. newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT;
  224. ret = bpf_parse_prog(tb[LWT_BPF_OUT], &bpf->out,
  225. BPF_PROG_TYPE_LWT_OUT);
  226. if (ret < 0)
  227. goto errout;
  228. }
  229. if (tb[LWT_BPF_XMIT]) {
  230. newts->flags |= LWTUNNEL_STATE_XMIT_REDIRECT;
  231. ret = bpf_parse_prog(tb[LWT_BPF_XMIT], &bpf->xmit,
  232. BPF_PROG_TYPE_LWT_XMIT);
  233. if (ret < 0)
  234. goto errout;
  235. }
  236. if (tb[LWT_BPF_XMIT_HEADROOM]) {
  237. u32 headroom = nla_get_u32(tb[LWT_BPF_XMIT_HEADROOM]);
  238. if (headroom > LWT_BPF_MAX_HEADROOM) {
  239. ret = -ERANGE;
  240. goto errout;
  241. }
  242. newts->headroom = headroom;
  243. }
  244. bpf->family = family;
  245. *ts = newts;
  246. return 0;
  247. errout:
  248. bpf_destroy_state(newts);
  249. kfree(newts);
  250. return ret;
  251. }
  252. static int bpf_fill_lwt_prog(struct sk_buff *skb, int attr,
  253. struct bpf_lwt_prog *prog)
  254. {
  255. struct nlattr *nest;
  256. if (!prog->prog)
  257. return 0;
  258. nest = nla_nest_start(skb, attr);
  259. if (!nest)
  260. return -EMSGSIZE;
  261. if (prog->name &&
  262. nla_put_string(skb, LWT_BPF_PROG_NAME, prog->name))
  263. return -EMSGSIZE;
  264. return nla_nest_end(skb, nest);
  265. }
  266. static int bpf_fill_encap_info(struct sk_buff *skb, struct lwtunnel_state *lwt)
  267. {
  268. struct bpf_lwt *bpf = bpf_lwt_lwtunnel(lwt);
  269. if (bpf_fill_lwt_prog(skb, LWT_BPF_IN, &bpf->in) < 0 ||
  270. bpf_fill_lwt_prog(skb, LWT_BPF_OUT, &bpf->out) < 0 ||
  271. bpf_fill_lwt_prog(skb, LWT_BPF_XMIT, &bpf->xmit) < 0)
  272. return -EMSGSIZE;
  273. return 0;
  274. }
  275. static int bpf_encap_nlsize(struct lwtunnel_state *lwtstate)
  276. {
  277. int nest_len = nla_total_size(sizeof(struct nlattr)) +
  278. nla_total_size(MAX_PROG_NAME) + /* LWT_BPF_PROG_NAME */
  279. 0;
  280. return nest_len + /* LWT_BPF_IN */
  281. nest_len + /* LWT_BPF_OUT */
  282. nest_len + /* LWT_BPF_XMIT */
  283. 0;
  284. }
  285. static int bpf_lwt_prog_cmp(struct bpf_lwt_prog *a, struct bpf_lwt_prog *b)
  286. {
  287. /* FIXME:
  288. * The LWT state is currently rebuilt for delete requests which
  289. * results in a new bpf_prog instance. Comparing names for now.
  290. */
  291. if (!a->name && !b->name)
  292. return 0;
  293. if (!a->name || !b->name)
  294. return 1;
  295. return strcmp(a->name, b->name);
  296. }
  297. static int bpf_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
  298. {
  299. struct bpf_lwt *a_bpf = bpf_lwt_lwtunnel(a);
  300. struct bpf_lwt *b_bpf = bpf_lwt_lwtunnel(b);
  301. return bpf_lwt_prog_cmp(&a_bpf->in, &b_bpf->in) ||
  302. bpf_lwt_prog_cmp(&a_bpf->out, &b_bpf->out) ||
  303. bpf_lwt_prog_cmp(&a_bpf->xmit, &b_bpf->xmit);
  304. }
  305. static const struct lwtunnel_encap_ops bpf_encap_ops = {
  306. .build_state = bpf_build_state,
  307. .destroy_state = bpf_destroy_state,
  308. .input = bpf_input,
  309. .output = bpf_output,
  310. .xmit = bpf_xmit,
  311. .fill_encap = bpf_fill_encap_info,
  312. .get_encap_size = bpf_encap_nlsize,
  313. .cmp_encap = bpf_encap_cmp,
  314. .owner = THIS_MODULE,
  315. };
  316. static int __init bpf_lwt_init(void)
  317. {
  318. return lwtunnel_encap_add_ops(&bpf_encap_ops, LWTUNNEL_ENCAP_BPF);
  319. }
  320. subsys_initcall(bpf_lwt_init)