lwt_bpf.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399
  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. rcu_read_lock();
  46. bpf_compute_data_pointers(skb);
  47. ret = bpf_prog_run_save_cb(lwt->prog, skb);
  48. rcu_read_unlock();
  49. switch (ret) {
  50. case BPF_OK:
  51. break;
  52. case BPF_REDIRECT:
  53. if (unlikely(!can_redirect)) {
  54. pr_warn_once("Illegal redirect return code in prog %s\n",
  55. lwt->name ? : "<unknown>");
  56. ret = BPF_OK;
  57. } else {
  58. ret = skb_do_redirect(skb);
  59. if (ret == 0)
  60. ret = BPF_REDIRECT;
  61. }
  62. break;
  63. case BPF_DROP:
  64. kfree_skb(skb);
  65. ret = -EPERM;
  66. break;
  67. default:
  68. pr_warn_once("bpf-lwt: Illegal return value %u, expect packet loss\n", ret);
  69. kfree_skb(skb);
  70. ret = -EINVAL;
  71. break;
  72. }
  73. preempt_enable();
  74. return ret;
  75. }
  76. static int bpf_input(struct sk_buff *skb)
  77. {
  78. struct dst_entry *dst = skb_dst(skb);
  79. struct bpf_lwt *bpf;
  80. int ret;
  81. bpf = bpf_lwt_lwtunnel(dst->lwtstate);
  82. if (bpf->in.prog) {
  83. ret = run_lwt_bpf(skb, &bpf->in, dst, NO_REDIRECT);
  84. if (ret < 0)
  85. return ret;
  86. }
  87. if (unlikely(!dst->lwtstate->orig_input)) {
  88. pr_warn_once("orig_input not set on dst for prog %s\n",
  89. bpf->out.name);
  90. kfree_skb(skb);
  91. return -EINVAL;
  92. }
  93. return dst->lwtstate->orig_input(skb);
  94. }
  95. static int bpf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
  96. {
  97. struct dst_entry *dst = skb_dst(skb);
  98. struct bpf_lwt *bpf;
  99. int ret;
  100. bpf = bpf_lwt_lwtunnel(dst->lwtstate);
  101. if (bpf->out.prog) {
  102. ret = run_lwt_bpf(skb, &bpf->out, dst, NO_REDIRECT);
  103. if (ret < 0)
  104. return ret;
  105. }
  106. if (unlikely(!dst->lwtstate->orig_output)) {
  107. pr_warn_once("orig_output not set on dst for prog %s\n",
  108. bpf->out.name);
  109. kfree_skb(skb);
  110. return -EINVAL;
  111. }
  112. return dst->lwtstate->orig_output(net, sk, skb);
  113. }
  114. static int xmit_check_hhlen(struct sk_buff *skb)
  115. {
  116. int hh_len = skb_dst(skb)->dev->hard_header_len;
  117. if (skb_headroom(skb) < hh_len) {
  118. int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
  119. if (pskb_expand_head(skb, nhead, 0, GFP_ATOMIC))
  120. return -ENOMEM;
  121. }
  122. return 0;
  123. }
  124. static int bpf_xmit(struct sk_buff *skb)
  125. {
  126. struct dst_entry *dst = skb_dst(skb);
  127. struct bpf_lwt *bpf;
  128. bpf = bpf_lwt_lwtunnel(dst->lwtstate);
  129. if (bpf->xmit.prog) {
  130. int ret;
  131. ret = run_lwt_bpf(skb, &bpf->xmit, dst, CAN_REDIRECT);
  132. switch (ret) {
  133. case BPF_OK:
  134. /* If the header was expanded, headroom might be too
  135. * small for L2 header to come, expand as needed.
  136. */
  137. ret = xmit_check_hhlen(skb);
  138. if (unlikely(ret))
  139. return ret;
  140. return LWTUNNEL_XMIT_CONTINUE;
  141. case BPF_REDIRECT:
  142. return LWTUNNEL_XMIT_DONE;
  143. default:
  144. return ret;
  145. }
  146. }
  147. return LWTUNNEL_XMIT_CONTINUE;
  148. }
  149. static void bpf_lwt_prog_destroy(struct bpf_lwt_prog *prog)
  150. {
  151. if (prog->prog)
  152. bpf_prog_put(prog->prog);
  153. kfree(prog->name);
  154. }
  155. static void bpf_destroy_state(struct lwtunnel_state *lwt)
  156. {
  157. struct bpf_lwt *bpf = bpf_lwt_lwtunnel(lwt);
  158. bpf_lwt_prog_destroy(&bpf->in);
  159. bpf_lwt_prog_destroy(&bpf->out);
  160. bpf_lwt_prog_destroy(&bpf->xmit);
  161. }
  162. static const struct nla_policy bpf_prog_policy[LWT_BPF_PROG_MAX + 1] = {
  163. [LWT_BPF_PROG_FD] = { .type = NLA_U32, },
  164. [LWT_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
  165. .len = MAX_PROG_NAME },
  166. };
  167. static int bpf_parse_prog(struct nlattr *attr, struct bpf_lwt_prog *prog,
  168. enum bpf_prog_type type)
  169. {
  170. struct nlattr *tb[LWT_BPF_PROG_MAX + 1];
  171. struct bpf_prog *p;
  172. int ret;
  173. u32 fd;
  174. ret = nla_parse_nested(tb, LWT_BPF_PROG_MAX, attr, bpf_prog_policy,
  175. NULL);
  176. if (ret < 0)
  177. return ret;
  178. if (!tb[LWT_BPF_PROG_FD] || !tb[LWT_BPF_PROG_NAME])
  179. return -EINVAL;
  180. prog->name = nla_memdup(tb[LWT_BPF_PROG_NAME], GFP_KERNEL);
  181. if (!prog->name)
  182. return -ENOMEM;
  183. fd = nla_get_u32(tb[LWT_BPF_PROG_FD]);
  184. p = bpf_prog_get_type(fd, type);
  185. if (IS_ERR(p))
  186. return PTR_ERR(p);
  187. prog->prog = p;
  188. return 0;
  189. }
  190. static const struct nla_policy bpf_nl_policy[LWT_BPF_MAX + 1] = {
  191. [LWT_BPF_IN] = { .type = NLA_NESTED, },
  192. [LWT_BPF_OUT] = { .type = NLA_NESTED, },
  193. [LWT_BPF_XMIT] = { .type = NLA_NESTED, },
  194. [LWT_BPF_XMIT_HEADROOM] = { .type = NLA_U32 },
  195. };
  196. static int bpf_build_state(struct nlattr *nla,
  197. unsigned int family, const void *cfg,
  198. struct lwtunnel_state **ts,
  199. struct netlink_ext_ack *extack)
  200. {
  201. struct nlattr *tb[LWT_BPF_MAX + 1];
  202. struct lwtunnel_state *newts;
  203. struct bpf_lwt *bpf;
  204. int ret;
  205. if (family != AF_INET && family != AF_INET6)
  206. return -EAFNOSUPPORT;
  207. ret = nla_parse_nested(tb, LWT_BPF_MAX, nla, bpf_nl_policy, extack);
  208. if (ret < 0)
  209. return ret;
  210. if (!tb[LWT_BPF_IN] && !tb[LWT_BPF_OUT] && !tb[LWT_BPF_XMIT])
  211. return -EINVAL;
  212. newts = lwtunnel_state_alloc(sizeof(*bpf));
  213. if (!newts)
  214. return -ENOMEM;
  215. newts->type = LWTUNNEL_ENCAP_BPF;
  216. bpf = bpf_lwt_lwtunnel(newts);
  217. if (tb[LWT_BPF_IN]) {
  218. newts->flags |= LWTUNNEL_STATE_INPUT_REDIRECT;
  219. ret = bpf_parse_prog(tb[LWT_BPF_IN], &bpf->in,
  220. BPF_PROG_TYPE_LWT_IN);
  221. if (ret < 0)
  222. goto errout;
  223. }
  224. if (tb[LWT_BPF_OUT]) {
  225. newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT;
  226. ret = bpf_parse_prog(tb[LWT_BPF_OUT], &bpf->out,
  227. BPF_PROG_TYPE_LWT_OUT);
  228. if (ret < 0)
  229. goto errout;
  230. }
  231. if (tb[LWT_BPF_XMIT]) {
  232. newts->flags |= LWTUNNEL_STATE_XMIT_REDIRECT;
  233. ret = bpf_parse_prog(tb[LWT_BPF_XMIT], &bpf->xmit,
  234. BPF_PROG_TYPE_LWT_XMIT);
  235. if (ret < 0)
  236. goto errout;
  237. }
  238. if (tb[LWT_BPF_XMIT_HEADROOM]) {
  239. u32 headroom = nla_get_u32(tb[LWT_BPF_XMIT_HEADROOM]);
  240. if (headroom > LWT_BPF_MAX_HEADROOM) {
  241. ret = -ERANGE;
  242. goto errout;
  243. }
  244. newts->headroom = headroom;
  245. }
  246. bpf->family = family;
  247. *ts = newts;
  248. return 0;
  249. errout:
  250. bpf_destroy_state(newts);
  251. kfree(newts);
  252. return ret;
  253. }
  254. static int bpf_fill_lwt_prog(struct sk_buff *skb, int attr,
  255. struct bpf_lwt_prog *prog)
  256. {
  257. struct nlattr *nest;
  258. if (!prog->prog)
  259. return 0;
  260. nest = nla_nest_start(skb, attr);
  261. if (!nest)
  262. return -EMSGSIZE;
  263. if (prog->name &&
  264. nla_put_string(skb, LWT_BPF_PROG_NAME, prog->name))
  265. return -EMSGSIZE;
  266. return nla_nest_end(skb, nest);
  267. }
  268. static int bpf_fill_encap_info(struct sk_buff *skb, struct lwtunnel_state *lwt)
  269. {
  270. struct bpf_lwt *bpf = bpf_lwt_lwtunnel(lwt);
  271. if (bpf_fill_lwt_prog(skb, LWT_BPF_IN, &bpf->in) < 0 ||
  272. bpf_fill_lwt_prog(skb, LWT_BPF_OUT, &bpf->out) < 0 ||
  273. bpf_fill_lwt_prog(skb, LWT_BPF_XMIT, &bpf->xmit) < 0)
  274. return -EMSGSIZE;
  275. return 0;
  276. }
  277. static int bpf_encap_nlsize(struct lwtunnel_state *lwtstate)
  278. {
  279. int nest_len = nla_total_size(sizeof(struct nlattr)) +
  280. nla_total_size(MAX_PROG_NAME) + /* LWT_BPF_PROG_NAME */
  281. 0;
  282. return nest_len + /* LWT_BPF_IN */
  283. nest_len + /* LWT_BPF_OUT */
  284. nest_len + /* LWT_BPF_XMIT */
  285. 0;
  286. }
  287. static int bpf_lwt_prog_cmp(struct bpf_lwt_prog *a, struct bpf_lwt_prog *b)
  288. {
  289. /* FIXME:
  290. * The LWT state is currently rebuilt for delete requests which
  291. * results in a new bpf_prog instance. Comparing names for now.
  292. */
  293. if (!a->name && !b->name)
  294. return 0;
  295. if (!a->name || !b->name)
  296. return 1;
  297. return strcmp(a->name, b->name);
  298. }
  299. static int bpf_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
  300. {
  301. struct bpf_lwt *a_bpf = bpf_lwt_lwtunnel(a);
  302. struct bpf_lwt *b_bpf = bpf_lwt_lwtunnel(b);
  303. return bpf_lwt_prog_cmp(&a_bpf->in, &b_bpf->in) ||
  304. bpf_lwt_prog_cmp(&a_bpf->out, &b_bpf->out) ||
  305. bpf_lwt_prog_cmp(&a_bpf->xmit, &b_bpf->xmit);
  306. }
  307. static const struct lwtunnel_encap_ops bpf_encap_ops = {
  308. .build_state = bpf_build_state,
  309. .destroy_state = bpf_destroy_state,
  310. .input = bpf_input,
  311. .output = bpf_output,
  312. .xmit = bpf_xmit,
  313. .fill_encap = bpf_fill_encap_info,
  314. .get_encap_size = bpf_encap_nlsize,
  315. .cmp_encap = bpf_encap_cmp,
  316. .owner = THIS_MODULE,
  317. };
  318. static int __init bpf_lwt_init(void)
  319. {
  320. return lwtunnel_encap_add_ops(&bpf_encap_ops, LWTUNNEL_ENCAP_BPF);
  321. }
  322. subsys_initcall(bpf_lwt_init)