1 /* Copyright (c) 2016 Thomas Graf <tgraf@tgraf.ch>
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.
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.
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/skbuff.h>
16 #include <linux/types.h>
17 #include <linux/bpf.h>
18 #include <net/lwtunnel.h>
21 struct bpf_prog *prog;
26 struct bpf_lwt_prog in;
27 struct bpf_lwt_prog out;
28 struct bpf_lwt_prog xmit;
32 #define MAX_PROG_NAME 256
34 static inline struct bpf_lwt *bpf_lwt_lwtunnel(struct lwtunnel_state *lwt)
36 return (struct bpf_lwt *)lwt->data;
39 #define NO_REDIRECT false
40 #define CAN_REDIRECT true
42 static int run_lwt_bpf(struct sk_buff *skb, struct bpf_lwt_prog *lwt,
43 struct dst_entry *dst, bool can_redirect)
47 /* Preempt disable and BH disable are needed to protect per-cpu
48 * redirect_info between BPF prog and skb_do_redirect().
52 bpf_compute_data_pointers(skb);
53 ret = bpf_prog_run_save_cb(lwt->prog, skb);
60 if (unlikely(!can_redirect)) {
61 pr_warn_once("Illegal redirect return code in prog %s\n",
62 lwt->name ? : "<unknown>");
65 skb_reset_mac_header(skb);
66 ret = skb_do_redirect(skb);
78 pr_warn_once("bpf-lwt: Illegal return value %u, expect packet loss\n", ret);
90 static int bpf_input(struct sk_buff *skb)
92 struct dst_entry *dst = skb_dst(skb);
96 bpf = bpf_lwt_lwtunnel(dst->lwtstate);
98 ret = run_lwt_bpf(skb, &bpf->in, dst, NO_REDIRECT);
103 if (unlikely(!dst->lwtstate->orig_input)) {
104 pr_warn_once("orig_input not set on dst for prog %s\n",
110 return dst->lwtstate->orig_input(skb);
113 static int bpf_output(struct net *net, struct sock *sk, struct sk_buff *skb)
115 struct dst_entry *dst = skb_dst(skb);
119 bpf = bpf_lwt_lwtunnel(dst->lwtstate);
121 ret = run_lwt_bpf(skb, &bpf->out, dst, NO_REDIRECT);
126 if (unlikely(!dst->lwtstate->orig_output)) {
127 pr_warn_once("orig_output not set on dst for prog %s\n",
133 return dst->lwtstate->orig_output(net, sk, skb);
136 static int xmit_check_hhlen(struct sk_buff *skb)
138 int hh_len = skb_dst(skb)->dev->hard_header_len;
140 if (skb_headroom(skb) < hh_len) {
141 int nhead = HH_DATA_ALIGN(hh_len - skb_headroom(skb));
143 if (pskb_expand_head(skb, nhead, 0, GFP_ATOMIC))
150 static int bpf_xmit(struct sk_buff *skb)
152 struct dst_entry *dst = skb_dst(skb);
155 bpf = bpf_lwt_lwtunnel(dst->lwtstate);
156 if (bpf->xmit.prog) {
159 ret = run_lwt_bpf(skb, &bpf->xmit, dst, CAN_REDIRECT);
162 /* If the header was expanded, headroom might be too
163 * small for L2 header to come, expand as needed.
165 ret = xmit_check_hhlen(skb);
169 return LWTUNNEL_XMIT_CONTINUE;
171 return LWTUNNEL_XMIT_DONE;
177 return LWTUNNEL_XMIT_CONTINUE;
180 static void bpf_lwt_prog_destroy(struct bpf_lwt_prog *prog)
183 bpf_prog_put(prog->prog);
188 static void bpf_destroy_state(struct lwtunnel_state *lwt)
190 struct bpf_lwt *bpf = bpf_lwt_lwtunnel(lwt);
192 bpf_lwt_prog_destroy(&bpf->in);
193 bpf_lwt_prog_destroy(&bpf->out);
194 bpf_lwt_prog_destroy(&bpf->xmit);
197 static const struct nla_policy bpf_prog_policy[LWT_BPF_PROG_MAX + 1] = {
198 [LWT_BPF_PROG_FD] = { .type = NLA_U32, },
199 [LWT_BPF_PROG_NAME] = { .type = NLA_NUL_STRING,
200 .len = MAX_PROG_NAME },
203 static int bpf_parse_prog(struct nlattr *attr, struct bpf_lwt_prog *prog,
204 enum bpf_prog_type type)
206 struct nlattr *tb[LWT_BPF_PROG_MAX + 1];
211 ret = nla_parse_nested(tb, LWT_BPF_PROG_MAX, attr, bpf_prog_policy,
216 if (!tb[LWT_BPF_PROG_FD] || !tb[LWT_BPF_PROG_NAME])
219 prog->name = nla_memdup(tb[LWT_BPF_PROG_NAME], GFP_ATOMIC);
223 fd = nla_get_u32(tb[LWT_BPF_PROG_FD]);
224 p = bpf_prog_get_type(fd, type);
233 static const struct nla_policy bpf_nl_policy[LWT_BPF_MAX + 1] = {
234 [LWT_BPF_IN] = { .type = NLA_NESTED, },
235 [LWT_BPF_OUT] = { .type = NLA_NESTED, },
236 [LWT_BPF_XMIT] = { .type = NLA_NESTED, },
237 [LWT_BPF_XMIT_HEADROOM] = { .type = NLA_U32 },
240 static int bpf_build_state(struct nlattr *nla,
241 unsigned int family, const void *cfg,
242 struct lwtunnel_state **ts,
243 struct netlink_ext_ack *extack)
245 struct nlattr *tb[LWT_BPF_MAX + 1];
246 struct lwtunnel_state *newts;
250 if (family != AF_INET && family != AF_INET6)
251 return -EAFNOSUPPORT;
253 ret = nla_parse_nested(tb, LWT_BPF_MAX, nla, bpf_nl_policy, extack);
257 if (!tb[LWT_BPF_IN] && !tb[LWT_BPF_OUT] && !tb[LWT_BPF_XMIT])
260 newts = lwtunnel_state_alloc(sizeof(*bpf));
264 newts->type = LWTUNNEL_ENCAP_BPF;
265 bpf = bpf_lwt_lwtunnel(newts);
267 if (tb[LWT_BPF_IN]) {
268 newts->flags |= LWTUNNEL_STATE_INPUT_REDIRECT;
269 ret = bpf_parse_prog(tb[LWT_BPF_IN], &bpf->in,
270 BPF_PROG_TYPE_LWT_IN);
275 if (tb[LWT_BPF_OUT]) {
276 newts->flags |= LWTUNNEL_STATE_OUTPUT_REDIRECT;
277 ret = bpf_parse_prog(tb[LWT_BPF_OUT], &bpf->out,
278 BPF_PROG_TYPE_LWT_OUT);
283 if (tb[LWT_BPF_XMIT]) {
284 newts->flags |= LWTUNNEL_STATE_XMIT_REDIRECT;
285 ret = bpf_parse_prog(tb[LWT_BPF_XMIT], &bpf->xmit,
286 BPF_PROG_TYPE_LWT_XMIT);
291 if (tb[LWT_BPF_XMIT_HEADROOM]) {
292 u32 headroom = nla_get_u32(tb[LWT_BPF_XMIT_HEADROOM]);
294 if (headroom > LWT_BPF_MAX_HEADROOM) {
299 newts->headroom = headroom;
302 bpf->family = family;
308 bpf_destroy_state(newts);
313 static int bpf_fill_lwt_prog(struct sk_buff *skb, int attr,
314 struct bpf_lwt_prog *prog)
321 nest = nla_nest_start(skb, attr);
326 nla_put_string(skb, LWT_BPF_PROG_NAME, prog->name))
329 return nla_nest_end(skb, nest);
332 static int bpf_fill_encap_info(struct sk_buff *skb, struct lwtunnel_state *lwt)
334 struct bpf_lwt *bpf = bpf_lwt_lwtunnel(lwt);
336 if (bpf_fill_lwt_prog(skb, LWT_BPF_IN, &bpf->in) < 0 ||
337 bpf_fill_lwt_prog(skb, LWT_BPF_OUT, &bpf->out) < 0 ||
338 bpf_fill_lwt_prog(skb, LWT_BPF_XMIT, &bpf->xmit) < 0)
344 static int bpf_encap_nlsize(struct lwtunnel_state *lwtstate)
346 int nest_len = nla_total_size(sizeof(struct nlattr)) +
347 nla_total_size(MAX_PROG_NAME) + /* LWT_BPF_PROG_NAME */
350 return nest_len + /* LWT_BPF_IN */
351 nest_len + /* LWT_BPF_OUT */
352 nest_len + /* LWT_BPF_XMIT */
356 static int bpf_lwt_prog_cmp(struct bpf_lwt_prog *a, struct bpf_lwt_prog *b)
359 * The LWT state is currently rebuilt for delete requests which
360 * results in a new bpf_prog instance. Comparing names for now.
362 if (!a->name && !b->name)
365 if (!a->name || !b->name)
368 return strcmp(a->name, b->name);
371 static int bpf_encap_cmp(struct lwtunnel_state *a, struct lwtunnel_state *b)
373 struct bpf_lwt *a_bpf = bpf_lwt_lwtunnel(a);
374 struct bpf_lwt *b_bpf = bpf_lwt_lwtunnel(b);
376 return bpf_lwt_prog_cmp(&a_bpf->in, &b_bpf->in) ||
377 bpf_lwt_prog_cmp(&a_bpf->out, &b_bpf->out) ||
378 bpf_lwt_prog_cmp(&a_bpf->xmit, &b_bpf->xmit);
381 static const struct lwtunnel_encap_ops bpf_encap_ops = {
382 .build_state = bpf_build_state,
383 .destroy_state = bpf_destroy_state,
385 .output = bpf_output,
387 .fill_encap = bpf_fill_encap_info,
388 .get_encap_size = bpf_encap_nlsize,
389 .cmp_encap = bpf_encap_cmp,
390 .owner = THIS_MODULE,
393 static int __init bpf_lwt_init(void)
395 return lwtunnel_encap_add_ops(&bpf_encap_ops, LWTUNNEL_ENCAP_BPF);
398 subsys_initcall(bpf_lwt_init)