Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /* iptables module to match on related connections */
0003 /*
0004  * (C) 2001 Martin Josefsson <gandalf@wlug.westbo.se>
0005  */
0006 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
0007 #include <linux/module.h>
0008 #include <linux/skbuff.h>
0009 #include <linux/netfilter.h>
0010 #include <net/netfilter/nf_conntrack.h>
0011 #include <net/netfilter/nf_conntrack_core.h>
0012 #include <net/netfilter/nf_conntrack_helper.h>
0013 #include <linux/netfilter/x_tables.h>
0014 #include <linux/netfilter/xt_helper.h>
0015 
0016 MODULE_LICENSE("GPL");
0017 MODULE_AUTHOR("Martin Josefsson <gandalf@netfilter.org>");
0018 MODULE_DESCRIPTION("Xtables: Related connection matching");
0019 MODULE_ALIAS("ipt_helper");
0020 MODULE_ALIAS("ip6t_helper");
0021 
0022 
0023 static bool
0024 helper_mt(const struct sk_buff *skb, struct xt_action_param *par)
0025 {
0026     const struct xt_helper_info *info = par->matchinfo;
0027     const struct nf_conn *ct;
0028     const struct nf_conn_help *master_help;
0029     const struct nf_conntrack_helper *helper;
0030     enum ip_conntrack_info ctinfo;
0031     bool ret = info->invert;
0032 
0033     ct = nf_ct_get(skb, &ctinfo);
0034     if (!ct || !ct->master)
0035         return ret;
0036 
0037     master_help = nfct_help(ct->master);
0038     if (!master_help)
0039         return ret;
0040 
0041     /* rcu_read_lock()ed by nf_hook_thresh */
0042     helper = rcu_dereference(master_help->helper);
0043     if (!helper)
0044         return ret;
0045 
0046     if (info->name[0] == '\0')
0047         ret = !ret;
0048     else
0049         ret ^= !strncmp(helper->name, info->name,
0050                 strlen(helper->name));
0051     return ret;
0052 }
0053 
0054 static int helper_mt_check(const struct xt_mtchk_param *par)
0055 {
0056     struct xt_helper_info *info = par->matchinfo;
0057     int ret;
0058 
0059     ret = nf_ct_netns_get(par->net, par->family);
0060     if (ret < 0) {
0061         pr_info_ratelimited("cannot load conntrack support for proto=%u\n",
0062                     par->family);
0063         return ret;
0064     }
0065     info->name[sizeof(info->name) - 1] = '\0';
0066     return 0;
0067 }
0068 
0069 static void helper_mt_destroy(const struct xt_mtdtor_param *par)
0070 {
0071     nf_ct_netns_put(par->net, par->family);
0072 }
0073 
0074 static struct xt_match helper_mt_reg __read_mostly = {
0075     .name       = "helper",
0076     .revision   = 0,
0077     .family     = NFPROTO_UNSPEC,
0078     .checkentry = helper_mt_check,
0079     .match      = helper_mt,
0080     .destroy    = helper_mt_destroy,
0081     .matchsize  = sizeof(struct xt_helper_info),
0082     .me         = THIS_MODULE,
0083 };
0084 
0085 static int __init helper_mt_init(void)
0086 {
0087     return xt_register_match(&helper_mt_reg);
0088 }
0089 
0090 static void __exit helper_mt_exit(void)
0091 {
0092     xt_unregister_match(&helper_mt_reg);
0093 }
0094 
0095 module_init(helper_mt_init);
0096 module_exit(helper_mt_exit);