Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 1 | /* Xtables module to match packets using a BPF filter. |
| 2 | * Copyright 2013 Google Inc. |
| 3 | * Written by Willem de Bruijn <willemb@google.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License version 2 as |
| 7 | * published by the Free Software Foundation. |
| 8 | */ |
| 9 | |
| 10 | #include <linux/module.h> |
Shmulik Ladkani | 7e3c72f | 2017-10-09 15:27:15 +0300 | [diff] [blame] | 11 | #include <linux/syscalls.h> |
Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 12 | #include <linux/skbuff.h> |
| 13 | #include <linux/filter.h> |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame] | 14 | #include <linux/bpf.h> |
Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 15 | |
| 16 | #include <linux/netfilter/xt_bpf.h> |
| 17 | #include <linux/netfilter/x_tables.h> |
| 18 | |
| 19 | MODULE_AUTHOR("Willem de Bruijn <willemb@google.com>"); |
| 20 | MODULE_DESCRIPTION("Xtables: BPF filter match"); |
| 21 | MODULE_LICENSE("GPL"); |
| 22 | MODULE_ALIAS("ipt_bpf"); |
| 23 | MODULE_ALIAS("ip6t_bpf"); |
| 24 | |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame] | 25 | static int __bpf_mt_check_bytecode(struct sock_filter *insns, __u16 len, |
| 26 | struct bpf_prog **ret) |
Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 27 | { |
Daniel Borkmann | b1fcd35 | 2014-05-23 18:43:58 +0200 | [diff] [blame] | 28 | struct sock_fprog_kern program; |
Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 29 | |
Jann Horn | 5244700 | 2017-12-01 01:46:07 +0100 | [diff] [blame] | 30 | if (len > XT_BPF_MAX_NUM_INSTR) |
| 31 | return -EINVAL; |
| 32 | |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame] | 33 | program.len = len; |
| 34 | program.filter = insns; |
Daniel Borkmann | b1fcd35 | 2014-05-23 18:43:58 +0200 | [diff] [blame] | 35 | |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame] | 36 | if (bpf_prog_create(ret, &program)) { |
Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 37 | pr_info("bpf: check failed: parse error\n"); |
| 38 | return -EINVAL; |
| 39 | } |
| 40 | |
| 41 | return 0; |
| 42 | } |
| 43 | |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame] | 44 | static int __bpf_mt_check_fd(int fd, struct bpf_prog **ret) |
| 45 | { |
| 46 | struct bpf_prog *prog; |
| 47 | |
| 48 | prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER); |
| 49 | if (IS_ERR(prog)) |
| 50 | return PTR_ERR(prog); |
| 51 | |
| 52 | *ret = prog; |
| 53 | return 0; |
| 54 | } |
| 55 | |
Shmulik Ladkani | 7e3c72f | 2017-10-09 15:27:15 +0300 | [diff] [blame] | 56 | static int __bpf_mt_check_path(const char *path, struct bpf_prog **ret) |
| 57 | { |
Jann Horn | 5244700 | 2017-12-01 01:46:07 +0100 | [diff] [blame] | 58 | if (strnlen(path, XT_BPF_PATH_MAX) == XT_BPF_PATH_MAX) |
| 59 | return -EINVAL; |
| 60 | |
Al Viro | 7dc12f7 | 2017-12-02 20:20:38 -0500 | [diff] [blame] | 61 | *ret = bpf_prog_get_type_path(path, BPF_PROG_TYPE_SOCKET_FILTER); |
| 62 | return PTR_ERR_OR_ZERO(*ret); |
Shmulik Ladkani | 7e3c72f | 2017-10-09 15:27:15 +0300 | [diff] [blame] | 63 | |
Shmulik Ladkani | 7e3c72f | 2017-10-09 15:27:15 +0300 | [diff] [blame] | 64 | } |
| 65 | |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame] | 66 | static int bpf_mt_check(const struct xt_mtchk_param *par) |
| 67 | { |
| 68 | struct xt_bpf_info *info = par->matchinfo; |
| 69 | |
| 70 | return __bpf_mt_check_bytecode(info->bpf_program, |
| 71 | info->bpf_program_num_elem, |
| 72 | &info->filter); |
| 73 | } |
| 74 | |
| 75 | static int bpf_mt_check_v1(const struct xt_mtchk_param *par) |
| 76 | { |
| 77 | struct xt_bpf_info_v1 *info = par->matchinfo; |
| 78 | |
| 79 | if (info->mode == XT_BPF_MODE_BYTECODE) |
| 80 | return __bpf_mt_check_bytecode(info->bpf_program, |
| 81 | info->bpf_program_num_elem, |
| 82 | &info->filter); |
Shmulik Ladkani | 7e3c72f | 2017-10-09 15:27:15 +0300 | [diff] [blame] | 83 | else if (info->mode == XT_BPF_MODE_FD_ELF) |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame] | 84 | return __bpf_mt_check_fd(info->fd, &info->filter); |
Shmulik Ladkani | 7e3c72f | 2017-10-09 15:27:15 +0300 | [diff] [blame] | 85 | else if (info->mode == XT_BPF_MODE_PATH_PINNED) |
| 86 | return __bpf_mt_check_path(info->path, &info->filter); |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame] | 87 | else |
| 88 | return -EINVAL; |
| 89 | } |
| 90 | |
Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 91 | static bool bpf_mt(const struct sk_buff *skb, struct xt_action_param *par) |
| 92 | { |
| 93 | const struct xt_bpf_info *info = par->matchinfo; |
| 94 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 95 | return BPF_PROG_RUN(info->filter, skb); |
Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 96 | } |
| 97 | |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame] | 98 | static bool bpf_mt_v1(const struct sk_buff *skb, struct xt_action_param *par) |
| 99 | { |
| 100 | const struct xt_bpf_info_v1 *info = par->matchinfo; |
| 101 | |
| 102 | return !!bpf_prog_run_save_cb(info->filter, (struct sk_buff *) skb); |
| 103 | } |
| 104 | |
Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 105 | static void bpf_mt_destroy(const struct xt_mtdtor_param *par) |
| 106 | { |
| 107 | const struct xt_bpf_info *info = par->matchinfo; |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame] | 108 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 109 | bpf_prog_destroy(info->filter); |
Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame] | 112 | static void bpf_mt_destroy_v1(const struct xt_mtdtor_param *par) |
| 113 | { |
| 114 | const struct xt_bpf_info_v1 *info = par->matchinfo; |
| 115 | |
| 116 | bpf_prog_destroy(info->filter); |
| 117 | } |
| 118 | |
| 119 | static struct xt_match bpf_mt_reg[] __read_mostly = { |
| 120 | { |
| 121 | .name = "bpf", |
| 122 | .revision = 0, |
| 123 | .family = NFPROTO_UNSPEC, |
| 124 | .checkentry = bpf_mt_check, |
| 125 | .match = bpf_mt, |
| 126 | .destroy = bpf_mt_destroy, |
| 127 | .matchsize = sizeof(struct xt_bpf_info), |
| 128 | .me = THIS_MODULE, |
| 129 | }, |
| 130 | { |
| 131 | .name = "bpf", |
| 132 | .revision = 1, |
| 133 | .family = NFPROTO_UNSPEC, |
| 134 | .checkentry = bpf_mt_check_v1, |
| 135 | .match = bpf_mt_v1, |
| 136 | .destroy = bpf_mt_destroy_v1, |
| 137 | .matchsize = sizeof(struct xt_bpf_info_v1), |
| 138 | .me = THIS_MODULE, |
| 139 | }, |
Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | static int __init bpf_mt_init(void) |
| 143 | { |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame] | 144 | return xt_register_matches(bpf_mt_reg, ARRAY_SIZE(bpf_mt_reg)); |
Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | static void __exit bpf_mt_exit(void) |
| 148 | { |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame] | 149 | xt_unregister_matches(bpf_mt_reg, ARRAY_SIZE(bpf_mt_reg)); |
Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | module_init(bpf_mt_init); |
| 153 | module_exit(bpf_mt_exit); |