Blame view
net/netfilter/xt_CT.c
4.1 KB
84f3bb9ae netfilter: xtable... |
1 2 3 4 5 6 7 |
/* * Copyright (c) 2010 Patrick McHardy <kaber@trash.net> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License version 2 as * published by the Free Software Foundation. */ |
a7fed7620 netfilter: xt_CT:... |
8 |
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt |
84f3bb9ae netfilter: xtable... |
9 |
#include <linux/module.h> |
5a0e3ad6a include cleanup: ... |
10 |
#include <linux/gfp.h> |
84f3bb9ae netfilter: xtable... |
11 |
#include <linux/skbuff.h> |
84f3bb9ae netfilter: xtable... |
12 13 14 15 16 17 18 |
#include <linux/netfilter_ipv4/ip_tables.h> #include <linux/netfilter_ipv6/ip6_tables.h> #include <linux/netfilter/x_tables.h> #include <linux/netfilter/xt_CT.h> #include <net/netfilter/nf_conntrack.h> #include <net/netfilter/nf_conntrack_helper.h> #include <net/netfilter/nf_conntrack_ecache.h> |
5d0aa2ccd netfilter: nf_con... |
19 |
#include <net/netfilter/nf_conntrack_zones.h> |
84f3bb9ae netfilter: xtable... |
20 21 |
static unsigned int xt_ct_target(struct sk_buff *skb, |
4b560b447 netfilter: xtable... |
22 |
const struct xt_action_param *par) |
84f3bb9ae netfilter: xtable... |
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
{ const struct xt_ct_target_info *info = par->targinfo; struct nf_conn *ct = info->ct; /* Previously seen (loopback)? Ignore. */ if (skb->nfct != NULL) return XT_CONTINUE; atomic_inc(&ct->ct_general.use); skb->nfct = &ct->ct_general; skb->nfctinfo = IP_CT_NEW; return XT_CONTINUE; } static u8 xt_ct_find_proto(const struct xt_tgchk_param *par) { |
076f7839d netfilter: xt_CT:... |
40 |
if (par->family == NFPROTO_IPV4) { |
84f3bb9ae netfilter: xtable... |
41 42 43 44 45 |
const struct ipt_entry *e = par->entryinfo; if (e->ip.invflags & IPT_INV_PROTO) return 0; return e->ip.proto; |
076f7839d netfilter: xt_CT:... |
46 |
} else if (par->family == NFPROTO_IPV6) { |
84f3bb9ae netfilter: xtable... |
47 48 49 50 51 52 53 54 |
const struct ip6t_entry *e = par->entryinfo; if (e->ipv6.invflags & IP6T_INV_PROTO) return 0; return e->ipv6.proto; } else return 0; } |
135367b8f netfilter: xtable... |
55 |
static int xt_ct_tg_check(const struct xt_tgchk_param *par) |
84f3bb9ae netfilter: xtable... |
56 57 58 59 60 |
{ struct xt_ct_target_info *info = par->targinfo; struct nf_conntrack_tuple t; struct nf_conn_help *help; struct nf_conn *ct; |
4a5a5c73b netfilter: xtable... |
61 |
int ret = 0; |
84f3bb9ae netfilter: xtable... |
62 |
u8 proto; |
3d058d7bc netfilter: rework... |
63 64 |
if (info->flags & ~(XT_CT_NOTRACK | XT_CT_USERSPACE_HELPER)) return -EOPNOTSUPP; |
84f3bb9ae netfilter: xtable... |
65 66 |
if (info->flags & XT_CT_NOTRACK) { |
5bfddbd46 netfilter: nf_con... |
67 |
ct = nf_ct_untracked_get(); |
84f3bb9ae netfilter: xtable... |
68 69 70 |
atomic_inc(&ct->ct_general.use); goto out; } |
5d0aa2ccd netfilter: nf_con... |
71 72 73 74 |
#ifndef CONFIG_NF_CONNTRACK_ZONES if (info->zone) goto err1; #endif |
4a5a5c73b netfilter: xtable... |
75 76 |
ret = nf_ct_l3proto_try_module_get(par->family); if (ret < 0) |
84f3bb9ae netfilter: xtable... |
77 78 79 |
goto err1; memset(&t, 0, sizeof(t)); |
5d0aa2ccd netfilter: nf_con... |
80 |
ct = nf_conntrack_alloc(par->net, info->zone, &t, &t, GFP_KERNEL); |
4a5a5c73b netfilter: xtable... |
81 |
ret = PTR_ERR(ct); |
84f3bb9ae netfilter: xtable... |
82 83 |
if (IS_ERR(ct)) goto err2; |
4a5a5c73b netfilter: xtable... |
84 |
ret = 0; |
84f3bb9ae netfilter: xtable... |
85 86 87 88 |
if ((info->ct_events || info->exp_events) && !nf_ct_ecache_ext_add(ct, info->ct_events, info->exp_events, GFP_KERNEL)) goto err3; |
3d058d7bc netfilter: rework... |
89 90 91 |
if (info->flags & XT_CT_USERSPACE_HELPER) { __set_bit(IPS_USERSPACE_HELPER_BIT, &ct->status); } else if (info->helper[0]) { |
4a5a5c73b netfilter: xtable... |
92 |
ret = -ENOENT; |
84f3bb9ae netfilter: xtable... |
93 |
proto = xt_ct_find_proto(par); |
a7fed7620 netfilter: xt_CT:... |
94 95 96 97 |
if (!proto) { pr_info("You must specify a L4 protocol, " "and not use inversions on it. "); |
84f3bb9ae netfilter: xtable... |
98 |
goto err3; |
a7fed7620 netfilter: xt_CT:... |
99 |
} |
84f3bb9ae netfilter: xtable... |
100 |
|
4a5a5c73b netfilter: xtable... |
101 |
ret = -ENOMEM; |
84f3bb9ae netfilter: xtable... |
102 103 104 |
help = nf_ct_helper_ext_add(ct, GFP_KERNEL); if (help == NULL) goto err3; |
4a5a5c73b netfilter: xtable... |
105 |
ret = -ENOENT; |
84f3bb9ae netfilter: xtable... |
106 107 108 |
help->helper = nf_conntrack_helper_try_module_get(info->helper, par->family, proto); |
a7fed7620 netfilter: xt_CT:... |
109 110 111 |
if (help->helper == NULL) { pr_info("No such helper \"%s\" ", info->helper); |
84f3bb9ae netfilter: xtable... |
112 |
goto err3; |
a7fed7620 netfilter: xt_CT:... |
113 |
} |
84f3bb9ae netfilter: xtable... |
114 115 116 117 118 119 |
} __set_bit(IPS_TEMPLATE_BIT, &ct->status); __set_bit(IPS_CONFIRMED_BIT, &ct->status); out: info->ct = ct; |
d6b00a534 netfilter: xtable... |
120 |
return 0; |
84f3bb9ae netfilter: xtable... |
121 122 123 124 125 126 |
err3: nf_conntrack_free(ct); err2: nf_ct_l3proto_module_put(par->family); err1: |
4a5a5c73b netfilter: xtable... |
127 |
return ret; |
84f3bb9ae netfilter: xtable... |
128 129 130 131 132 133 134 |
} static void xt_ct_tg_destroy(const struct xt_tgdtor_param *par) { struct xt_ct_target_info *info = par->targinfo; struct nf_conn *ct = info->ct; struct nf_conn_help *help; |
5bfddbd46 netfilter: nf_con... |
135 |
if (!nf_ct_is_untracked(ct)) { |
84f3bb9ae netfilter: xtable... |
136 137 138 139 140 141 142 143 144 145 146 147 |
help = nfct_help(ct); if (help) module_put(help->helper->me); nf_ct_l3proto_module_put(par->family); } nf_ct_put(info->ct); } static struct xt_target xt_ct_tg __read_mostly = { .name = "CT", .family = NFPROTO_UNSPEC, |
7d5f7ed80 netfilter: xtable... |
148 |
.targetsize = sizeof(struct xt_ct_target_info), |
84f3bb9ae netfilter: xtable... |
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
.checkentry = xt_ct_tg_check, .destroy = xt_ct_tg_destroy, .target = xt_ct_target, .table = "raw", .me = THIS_MODULE, }; static int __init xt_ct_tg_init(void) { return xt_register_target(&xt_ct_tg); } static void __exit xt_ct_tg_exit(void) { xt_unregister_target(&xt_ct_tg); } module_init(xt_ct_tg_init); module_exit(xt_ct_tg_exit); MODULE_LICENSE("GPL"); MODULE_DESCRIPTION("Xtables: connection tracking target"); MODULE_ALIAS("ipt_CT"); MODULE_ALIAS("ip6t_CT"); |