1*d2912cb1SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
22e4e6a17SHarald Welte /* String matching match for iptables
32e4e6a17SHarald Welte *
42e4e6a17SHarald Welte * (C) 2005 Pablo Neira Ayuso <[email protected]>
52e4e6a17SHarald Welte */
62e4e6a17SHarald Welte
75a0e3ad6STejun Heo #include <linux/gfp.h>
82e4e6a17SHarald Welte #include <linux/init.h>
92e4e6a17SHarald Welte #include <linux/module.h>
102e4e6a17SHarald Welte #include <linux/kernel.h>
112e4e6a17SHarald Welte #include <linux/skbuff.h>
122e4e6a17SHarald Welte #include <linux/netfilter/x_tables.h>
132e4e6a17SHarald Welte #include <linux/netfilter/xt_string.h>
142e4e6a17SHarald Welte #include <linux/textsearch.h>
152e4e6a17SHarald Welte
162e4e6a17SHarald Welte MODULE_AUTHOR("Pablo Neira Ayuso <[email protected]>");
172ae15b64SJan Engelhardt MODULE_DESCRIPTION("Xtables: string-based matching");
182e4e6a17SHarald Welte MODULE_LICENSE("GPL");
192e4e6a17SHarald Welte MODULE_ALIAS("ipt_string");
202e4e6a17SHarald Welte MODULE_ALIAS("ip6t_string");
211be3ac98SBernie Harris MODULE_ALIAS("ebt_string");
222e4e6a17SHarald Welte
23d3c5ee6dSJan Engelhardt static bool
string_mt(const struct sk_buff * skb,struct xt_action_param * par)2462fc8051SJan Engelhardt string_mt(const struct sk_buff *skb, struct xt_action_param *par)
252e4e6a17SHarald Welte {
26f7108a20SJan Engelhardt const struct xt_string_info *conf = par->matchinfo;
27d879e19eSJan Engelhardt bool invert;
282e4e6a17SHarald Welte
29d879e19eSJan Engelhardt invert = conf->u.v1.flags & XT_STRING_FLAG_INVERT;
304ad3f261SJoonwoo Park
312e4e6a17SHarald Welte return (skb_find_text((struct sk_buff *)skb, conf->from_offset,
32059a2440SBojan Prtvar conf->to_offset, conf->config)
334ad3f261SJoonwoo Park != UINT_MAX) ^ invert;
342e4e6a17SHarald Welte }
352e4e6a17SHarald Welte
36e79ec50bSJan Engelhardt #define STRING_TEXT_PRIV(m) ((struct xt_string_info *)(m))
372e4e6a17SHarald Welte
string_mt_check(const struct xt_mtchk_param * par)38b0f38452SJan Engelhardt static int string_mt_check(const struct xt_mtchk_param *par)
392e4e6a17SHarald Welte {
409b4fce7aSJan Engelhardt struct xt_string_info *conf = par->matchinfo;
412e4e6a17SHarald Welte struct ts_config *ts_conf;
424ad3f261SJoonwoo Park int flags = TS_AUTOLOAD;
432e4e6a17SHarald Welte
442e4e6a17SHarald Welte /* Damn, can't handle this case properly with iptables... */
452e4e6a17SHarald Welte if (conf->from_offset > conf->to_offset)
46bd414ee6SJan Engelhardt return -EINVAL;
473ab72088SPatrick McHardy if (conf->algo[XT_STRING_MAX_ALGO_NAME_SIZE - 1] != '\0')
48bd414ee6SJan Engelhardt return -EINVAL;
493ab72088SPatrick McHardy if (conf->patlen > XT_STRING_MAX_PATTERN_SIZE)
50bd414ee6SJan Engelhardt return -EINVAL;
514ad3f261SJoonwoo Park if (conf->u.v1.flags &
524ad3f261SJoonwoo Park ~(XT_STRING_FLAG_IGNORECASE | XT_STRING_FLAG_INVERT))
53bd414ee6SJan Engelhardt return -EINVAL;
544ad3f261SJoonwoo Park if (conf->u.v1.flags & XT_STRING_FLAG_IGNORECASE)
554ad3f261SJoonwoo Park flags |= TS_IGNORECASE;
562e4e6a17SHarald Welte ts_conf = textsearch_prepare(conf->algo, conf->pattern, conf->patlen,
574ad3f261SJoonwoo Park GFP_KERNEL, flags);
582e4e6a17SHarald Welte if (IS_ERR(ts_conf))
594a5a5c73SJan Engelhardt return PTR_ERR(ts_conf);
602e4e6a17SHarald Welte
612e4e6a17SHarald Welte conf->config = ts_conf;
62bd414ee6SJan Engelhardt return 0;
632e4e6a17SHarald Welte }
642e4e6a17SHarald Welte
string_mt_destroy(const struct xt_mtdtor_param * par)656be3d859SJan Engelhardt static void string_mt_destroy(const struct xt_mtdtor_param *par)
662e4e6a17SHarald Welte {
676be3d859SJan Engelhardt textsearch_destroy(STRING_TEXT_PRIV(par->matchinfo)->config);
682e4e6a17SHarald Welte }
692e4e6a17SHarald Welte
70d879e19eSJan Engelhardt static struct xt_match xt_string_mt_reg __read_mostly = {
714470bbc7SPatrick McHardy .name = "string",
724ad3f261SJoonwoo Park .revision = 1,
7355b69e91SJan Engelhardt .family = NFPROTO_UNSPEC,
74d3c5ee6dSJan Engelhardt .checkentry = string_mt_check,
75d3c5ee6dSJan Engelhardt .match = string_mt,
76d3c5ee6dSJan Engelhardt .destroy = string_mt_destroy,
774470bbc7SPatrick McHardy .matchsize = sizeof(struct xt_string_info),
78ec231890SWillem de Bruijn .usersize = offsetof(struct xt_string_info, config),
79d879e19eSJan Engelhardt .me = THIS_MODULE,
802e4e6a17SHarald Welte };
812e4e6a17SHarald Welte
string_mt_init(void)82d3c5ee6dSJan Engelhardt static int __init string_mt_init(void)
832e4e6a17SHarald Welte {
84d879e19eSJan Engelhardt return xt_register_match(&xt_string_mt_reg);
852e4e6a17SHarald Welte }
862e4e6a17SHarald Welte
string_mt_exit(void)87d3c5ee6dSJan Engelhardt static void __exit string_mt_exit(void)
882e4e6a17SHarald Welte {
89d879e19eSJan Engelhardt xt_unregister_match(&xt_string_mt_reg);
902e4e6a17SHarald Welte }
912e4e6a17SHarald Welte
92d3c5ee6dSJan Engelhardt module_init(string_mt_init);
93d3c5ee6dSJan Engelhardt module_exit(string_mt_exit);
94