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