Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 1 | /* |
| 2 | * (C) 2007 Patrick McHardy <kaber@trash.net> |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License version 2 as |
| 6 | * published by the Free Software Foundation. |
| 7 | */ |
| 8 | #include <linux/module.h> |
| 9 | #include <linux/skbuff.h> |
| 10 | #include <linux/gen_stats.h> |
| 11 | |
| 12 | #include <linux/netfilter/x_tables.h> |
| 13 | #include <linux/netfilter/xt_rateest.h> |
| 14 | #include <net/netfilter/xt_rateest.h> |
| 15 | |
| 16 | |
Jan Engelhardt | f7108a20 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 17 | static bool |
Jan Engelhardt | 62fc805 | 2009-07-07 20:42:08 +0200 | [diff] [blame] | 18 | xt_rateest_mt(const struct sk_buff *skb, struct xt_action_param *par) |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 19 | { |
Jan Engelhardt | f7108a20 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 20 | const struct xt_rateest_match_info *info = par->matchinfo; |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 21 | struct gnet_stats_rate_est64 sample = {0}; |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 22 | u_int32_t bps1, bps2, pps1, pps2; |
| 23 | bool ret = true; |
| 24 | |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 25 | gen_estimator_read(&info->est1->rate_est, &sample); |
| 26 | |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 27 | if (info->flags & XT_RATEEST_MATCH_DELTA) { |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 28 | bps1 = info->bps1 >= sample.bps ? info->bps1 - sample.bps : 0; |
| 29 | pps1 = info->pps1 >= sample.pps ? info->pps1 - sample.pps : 0; |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 30 | } else { |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 31 | bps1 = sample.bps; |
| 32 | pps1 = sample.pps; |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 33 | } |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 34 | |
| 35 | if (info->flags & XT_RATEEST_MATCH_ABS) { |
| 36 | bps2 = info->bps2; |
| 37 | pps2 = info->pps2; |
| 38 | } else { |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 39 | gen_estimator_read(&info->est2->rate_est, &sample); |
| 40 | |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 41 | if (info->flags & XT_RATEEST_MATCH_DELTA) { |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 42 | bps2 = info->bps2 >= sample.bps ? info->bps2 - sample.bps : 0; |
| 43 | pps2 = info->pps2 >= sample.pps ? info->pps2 - sample.pps : 0; |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 44 | } else { |
Eric Dumazet | 1c0d32f | 2016-12-04 09:48:16 -0800 | [diff] [blame] | 45 | bps2 = sample.bps; |
| 46 | pps2 = sample.pps; |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 47 | } |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | switch (info->mode) { |
| 51 | case XT_RATEEST_MATCH_LT: |
| 52 | if (info->flags & XT_RATEEST_MATCH_BPS) |
| 53 | ret &= bps1 < bps2; |
| 54 | if (info->flags & XT_RATEEST_MATCH_PPS) |
| 55 | ret &= pps1 < pps2; |
| 56 | break; |
| 57 | case XT_RATEEST_MATCH_GT: |
| 58 | if (info->flags & XT_RATEEST_MATCH_BPS) |
| 59 | ret &= bps1 > bps2; |
| 60 | if (info->flags & XT_RATEEST_MATCH_PPS) |
| 61 | ret &= pps1 > pps2; |
| 62 | break; |
| 63 | case XT_RATEEST_MATCH_EQ: |
| 64 | if (info->flags & XT_RATEEST_MATCH_BPS) |
| 65 | ret &= bps1 == bps2; |
| 66 | if (info->flags & XT_RATEEST_MATCH_PPS) |
Patrick McHardy | 4d900f9 | 2009-06-22 14:17:12 +0200 | [diff] [blame] | 67 | ret &= pps1 == pps2; |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 68 | break; |
| 69 | } |
| 70 | |
| 71 | ret ^= info->flags & XT_RATEEST_MATCH_INVERT ? true : false; |
| 72 | return ret; |
| 73 | } |
| 74 | |
Jan Engelhardt | b0f3845 | 2010-03-19 17:16:42 +0100 | [diff] [blame] | 75 | static int xt_rateest_mt_checkentry(const struct xt_mtchk_param *par) |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 76 | { |
Jan Engelhardt | 9b4fce7 | 2008-10-08 11:35:18 +0200 | [diff] [blame] | 77 | struct xt_rateest_match_info *info = par->matchinfo; |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 78 | struct xt_rateest *est1, *est2; |
Eric Dumazet | 00fe1ae | 2011-07-29 16:24:46 +0200 | [diff] [blame] | 79 | int ret = -EINVAL; |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 80 | |
| 81 | if (hweight32(info->flags & (XT_RATEEST_MATCH_ABS | |
| 82 | XT_RATEEST_MATCH_REL)) != 1) |
| 83 | goto err1; |
| 84 | |
| 85 | if (!(info->flags & (XT_RATEEST_MATCH_BPS | XT_RATEEST_MATCH_PPS))) |
| 86 | goto err1; |
| 87 | |
| 88 | switch (info->mode) { |
| 89 | case XT_RATEEST_MATCH_EQ: |
| 90 | case XT_RATEEST_MATCH_LT: |
| 91 | case XT_RATEEST_MATCH_GT: |
| 92 | break; |
| 93 | default: |
| 94 | goto err1; |
| 95 | } |
| 96 | |
Jan Engelhardt | 4a5a5c7 | 2010-03-19 17:32:59 +0100 | [diff] [blame] | 97 | ret = -ENOENT; |
Cong Wang | 3427b2a | 2018-03-01 18:58:38 -0800 | [diff] [blame] | 98 | est1 = xt_rateest_lookup(par->net, info->name1); |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 99 | if (!est1) |
| 100 | goto err1; |
| 101 | |
Eric Dumazet | 00fe1ae | 2011-07-29 16:24:46 +0200 | [diff] [blame] | 102 | est2 = NULL; |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 103 | if (info->flags & XT_RATEEST_MATCH_REL) { |
Cong Wang | 3427b2a | 2018-03-01 18:58:38 -0800 | [diff] [blame] | 104 | est2 = xt_rateest_lookup(par->net, info->name2); |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 105 | if (!est2) |
| 106 | goto err2; |
Eric Dumazet | 00fe1ae | 2011-07-29 16:24:46 +0200 | [diff] [blame] | 107 | } |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 108 | |
| 109 | info->est1 = est1; |
| 110 | info->est2 = est2; |
Jan Engelhardt | bd414ee | 2010-03-23 16:35:56 +0100 | [diff] [blame] | 111 | return 0; |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 112 | |
| 113 | err2: |
Cong Wang | 3427b2a | 2018-03-01 18:58:38 -0800 | [diff] [blame] | 114 | xt_rateest_put(par->net, est1); |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 115 | err1: |
Eric Dumazet | 00fe1ae | 2011-07-29 16:24:46 +0200 | [diff] [blame] | 116 | return ret; |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 117 | } |
| 118 | |
Jan Engelhardt | 6be3d85 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 119 | static void xt_rateest_mt_destroy(const struct xt_mtdtor_param *par) |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 120 | { |
Jan Engelhardt | 6be3d85 | 2008-10-08 11:35:19 +0200 | [diff] [blame] | 121 | struct xt_rateest_match_info *info = par->matchinfo; |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 122 | |
Cong Wang | 3427b2a | 2018-03-01 18:58:38 -0800 | [diff] [blame] | 123 | xt_rateest_put(par->net, info->est1); |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 124 | if (info->est2) |
Cong Wang | 3427b2a | 2018-03-01 18:58:38 -0800 | [diff] [blame] | 125 | xt_rateest_put(par->net, info->est2); |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 126 | } |
| 127 | |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 128 | static struct xt_match xt_rateest_mt_reg __read_mostly = { |
| 129 | .name = "rateest", |
| 130 | .revision = 0, |
| 131 | .family = NFPROTO_UNSPEC, |
| 132 | .match = xt_rateest_mt, |
| 133 | .checkentry = xt_rateest_mt_checkentry, |
| 134 | .destroy = xt_rateest_mt_destroy, |
| 135 | .matchsize = sizeof(struct xt_rateest_match_info), |
Willem de Bruijn | ec23189 | 2017-01-02 17:19:46 -0500 | [diff] [blame] | 136 | .usersize = offsetof(struct xt_rateest_match_info, est1), |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 137 | .me = THIS_MODULE, |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | static int __init xt_rateest_mt_init(void) |
| 141 | { |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 142 | return xt_register_match(&xt_rateest_mt_reg); |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | static void __exit xt_rateest_mt_fini(void) |
| 146 | { |
Jan Engelhardt | 55b69e9 | 2008-10-08 11:35:01 +0200 | [diff] [blame] | 147 | xt_unregister_match(&xt_rateest_mt_reg); |
Patrick McHardy | 50c164a | 2007-12-04 13:02:19 +0100 | [diff] [blame] | 148 | } |
| 149 | |
| 150 | MODULE_AUTHOR("Patrick McHardy <kaber@trash.net>"); |
| 151 | MODULE_LICENSE("GPL"); |
| 152 | MODULE_DESCRIPTION("xtables rate estimator match"); |
| 153 | MODULE_ALIAS("ipt_rateest"); |
| 154 | MODULE_ALIAS("ip6t_rateest"); |
| 155 | module_init(xt_rateest_mt_init); |
| 156 | module_exit(xt_rateest_mt_fini); |