Linux 6.10-rc3
[linux-block.git] / net / netfilter / xt_string.c
CommitLineData
d2912cb1 1// SPDX-License-Identifier: GPL-2.0-only
7567662b 2/* String matching match for iptables
601e68e1 3 *
7567662b 4 * (C) 2005 Pablo Neira Ayuso <pablo@eurodev.net>
7567662b
PNA
5 */
6
5a0e3ad6 7#include <linux/gfp.h>
7567662b
PNA
8#include <linux/init.h>
9#include <linux/module.h>
10#include <linux/kernel.h>
11#include <linux/skbuff.h>
2e4e6a17
HW
12#include <linux/netfilter/x_tables.h>
13#include <linux/netfilter/xt_string.h>
7567662b
PNA
14#include <linux/textsearch.h>
15
16MODULE_AUTHOR("Pablo Neira Ayuso <pablo@eurodev.net>");
2ae15b64 17MODULE_DESCRIPTION("Xtables: string-based matching");
7567662b 18MODULE_LICENSE("GPL");
2e4e6a17
HW
19MODULE_ALIAS("ipt_string");
20MODULE_ALIAS("ip6t_string");
1be3ac98 21MODULE_ALIAS("ebt_string");
7567662b 22
d3c5ee6d 23static bool
62fc8051 24string_mt(const struct sk_buff *skb, struct xt_action_param *par)
7567662b 25{
f7108a20 26 const struct xt_string_info *conf = par->matchinfo;
d879e19e 27 bool invert;
7567662b 28
d879e19e 29 invert = conf->u.v1.flags & XT_STRING_FLAG_INVERT;
4ad3f261 30
601e68e1 31 return (skb_find_text((struct sk_buff *)skb, conf->from_offset,
059a2440 32 conf->to_offset, conf->config)
4ad3f261 33 != UINT_MAX) ^ invert;
7567662b
PNA
34}
35
e79ec50b 36#define STRING_TEXT_PRIV(m) ((struct xt_string_info *)(m))
7567662b 37
b0f38452 38static int string_mt_check(const struct xt_mtchk_param *par)
7567662b 39{
9b4fce7a 40 struct xt_string_info *conf = par->matchinfo;
7567662b 41 struct ts_config *ts_conf;
4ad3f261 42 int flags = TS_AUTOLOAD;
7567662b 43
7567662b
PNA
44 /* Damn, can't handle this case properly with iptables... */
45 if (conf->from_offset > conf->to_offset)
bd414ee6 46 return -EINVAL;
3ab72088 47 if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0')
bd414ee6 48 return -EINVAL;
3ab72088 49 if (conf->patlen > XT_STRING_MAX_PATTERN_SIZE)
bd414ee6 50 return -EINVAL;
d879e19e
JE
51 if (conf->u.v1.flags &
52 ~(XT_STRING_FLAG_IGNORECASE | XT_STRING_FLAG_INVERT))
53 return -EINVAL;
54 if (conf->u.v1.flags & XT_STRING_FLAG_IGNORECASE)
55 flags |= TS_IGNORECASE;
7567662b 56 ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen,
4ad3f261 57 GFP_KERNEL, flags);
7567662b 58 if (IS_ERR(ts_conf))
4a5a5c73 59 return PTR_ERR(ts_conf);
7567662b
PNA
60
61 conf->config = ts_conf;
bd414ee6 62 return 0;
7567662b
PNA
63}
64
6be3d859 65static void string_mt_destroy(const struct xt_mtdtor_param *par)
7567662b 66{
6be3d859 67 textsearch_destroy(STRING_TEXT_PRIV(par->matchinfo)->config);
7567662b
PNA
68}
69
d879e19e
JE
70static struct xt_match xt_string_mt_reg __read_mostly = {
71 .name = "string",
72 .revision = 1,
73 .family = NFPROTO_UNSPEC,
74 .checkentry = string_mt_check,
75 .match = string_mt,
76 .destroy = string_mt_destroy,
77 .matchsize = sizeof(struct xt_string_info),
ec231890 78 .usersize = offsetof(struct xt_string_info, config),
d879e19e 79 .me = THIS_MODULE,
7567662b
PNA
80};
81
d3c5ee6d 82static int __init string_mt_init(void)
7567662b 83{
d879e19e 84 return xt_register_match(&xt_string_mt_reg);
7567662b
PNA
85}
86
d3c5ee6d 87static void __exit string_mt_exit(void)
7567662b 88{
d879e19e 89 xt_unregister_match(&xt_string_mt_reg);
7567662b
PNA
90}
91
d3c5ee6d
JE
92module_init(string_mt_init);
93module_exit(string_mt_exit);