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> |
| 11 | #include <linux/skbuff.h> |
| 12 | #include <linux/filter.h> |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame^] | 13 | #include <linux/bpf.h> |
Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 14 | |
| 15 | #include <linux/netfilter/xt_bpf.h> |
| 16 | #include <linux/netfilter/x_tables.h> |
| 17 | |
| 18 | MODULE_AUTHOR("Willem de Bruijn <willemb@google.com>"); |
| 19 | MODULE_DESCRIPTION("Xtables: BPF filter match"); |
| 20 | MODULE_LICENSE("GPL"); |
| 21 | MODULE_ALIAS("ipt_bpf"); |
| 22 | MODULE_ALIAS("ip6t_bpf"); |
| 23 | |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame^] | 24 | static int __bpf_mt_check_bytecode(struct sock_filter *insns, __u16 len, |
| 25 | struct bpf_prog **ret) |
Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 26 | { |
Daniel Borkmann | b1fcd35 | 2014-05-23 18:43:58 +0200 | [diff] [blame] | 27 | struct sock_fprog_kern program; |
Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 28 | |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame^] | 29 | program.len = len; |
| 30 | program.filter = insns; |
Daniel Borkmann | b1fcd35 | 2014-05-23 18:43:58 +0200 | [diff] [blame] | 31 | |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame^] | 32 | if (bpf_prog_create(ret, &program)) { |
Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 33 | pr_info("bpf: check failed: parse error\n"); |
| 34 | return -EINVAL; |
| 35 | } |
| 36 | |
| 37 | return 0; |
| 38 | } |
| 39 | |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame^] | 40 | static int __bpf_mt_check_fd(int fd, struct bpf_prog **ret) |
| 41 | { |
| 42 | struct bpf_prog *prog; |
| 43 | |
| 44 | prog = bpf_prog_get_type(fd, BPF_PROG_TYPE_SOCKET_FILTER); |
| 45 | if (IS_ERR(prog)) |
| 46 | return PTR_ERR(prog); |
| 47 | |
| 48 | *ret = prog; |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | static int bpf_mt_check(const struct xt_mtchk_param *par) |
| 53 | { |
| 54 | struct xt_bpf_info *info = par->matchinfo; |
| 55 | |
| 56 | return __bpf_mt_check_bytecode(info->bpf_program, |
| 57 | info->bpf_program_num_elem, |
| 58 | &info->filter); |
| 59 | } |
| 60 | |
| 61 | static int bpf_mt_check_v1(const struct xt_mtchk_param *par) |
| 62 | { |
| 63 | struct xt_bpf_info_v1 *info = par->matchinfo; |
| 64 | |
| 65 | if (info->mode == XT_BPF_MODE_BYTECODE) |
| 66 | return __bpf_mt_check_bytecode(info->bpf_program, |
| 67 | info->bpf_program_num_elem, |
| 68 | &info->filter); |
| 69 | else if (info->mode == XT_BPF_MODE_FD_PINNED || |
| 70 | info->mode == XT_BPF_MODE_FD_ELF) |
| 71 | return __bpf_mt_check_fd(info->fd, &info->filter); |
| 72 | else |
| 73 | return -EINVAL; |
| 74 | } |
| 75 | |
Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 76 | static bool bpf_mt(const struct sk_buff *skb, struct xt_action_param *par) |
| 77 | { |
| 78 | const struct xt_bpf_info *info = par->matchinfo; |
| 79 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 80 | return BPF_PROG_RUN(info->filter, skb); |
Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame^] | 83 | static bool bpf_mt_v1(const struct sk_buff *skb, struct xt_action_param *par) |
| 84 | { |
| 85 | const struct xt_bpf_info_v1 *info = par->matchinfo; |
| 86 | |
| 87 | return !!bpf_prog_run_save_cb(info->filter, (struct sk_buff *) skb); |
| 88 | } |
| 89 | |
Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 90 | static void bpf_mt_destroy(const struct xt_mtdtor_param *par) |
| 91 | { |
| 92 | const struct xt_bpf_info *info = par->matchinfo; |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame^] | 93 | |
Alexei Starovoitov | 7ae457c | 2014-07-30 20:34:16 -0700 | [diff] [blame] | 94 | bpf_prog_destroy(info->filter); |
Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 95 | } |
| 96 | |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame^] | 97 | static void bpf_mt_destroy_v1(const struct xt_mtdtor_param *par) |
| 98 | { |
| 99 | const struct xt_bpf_info_v1 *info = par->matchinfo; |
| 100 | |
| 101 | bpf_prog_destroy(info->filter); |
| 102 | } |
| 103 | |
| 104 | static struct xt_match bpf_mt_reg[] __read_mostly = { |
| 105 | { |
| 106 | .name = "bpf", |
| 107 | .revision = 0, |
| 108 | .family = NFPROTO_UNSPEC, |
| 109 | .checkentry = bpf_mt_check, |
| 110 | .match = bpf_mt, |
| 111 | .destroy = bpf_mt_destroy, |
| 112 | .matchsize = sizeof(struct xt_bpf_info), |
| 113 | .me = THIS_MODULE, |
| 114 | }, |
| 115 | { |
| 116 | .name = "bpf", |
| 117 | .revision = 1, |
| 118 | .family = NFPROTO_UNSPEC, |
| 119 | .checkentry = bpf_mt_check_v1, |
| 120 | .match = bpf_mt_v1, |
| 121 | .destroy = bpf_mt_destroy_v1, |
| 122 | .matchsize = sizeof(struct xt_bpf_info_v1), |
| 123 | .me = THIS_MODULE, |
| 124 | }, |
Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | static int __init bpf_mt_init(void) |
| 128 | { |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame^] | 129 | 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] | 130 | } |
| 131 | |
| 132 | static void __exit bpf_mt_exit(void) |
| 133 | { |
Willem de Bruijn | eb8925e | 2016-12-06 16:25:02 -0500 | [diff] [blame^] | 134 | xt_unregister_matches(bpf_mt_reg, ARRAY_SIZE(bpf_mt_reg)); |
Willem de Bruijn | e6f30c7 | 2013-01-18 07:17:30 +0000 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | module_init(bpf_mt_init); |
| 138 | module_exit(bpf_mt_exit); |