| 1 | |
| 2 | |
| 3 | * Copyright (c) 2010 Patrick McHardy <[email protected]>
|
| 4 |
|
| 5 | #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
|
| 6 | #include <linux/module.h>
|
| 7 | #include <linux/gfp.h>
|
| 8 | #include <linux/skbuff.h>
|
| 9 | #include <linux/netfilter_ipv4/ip_tables.h>
|
| 10 | #include <linux/netfilter_ipv6/ip6_tables.h>
|
| 11 | #include <linux/netfilter/x_tables.h>
|
| 12 | #include <linux/netfilter/xt_CT.h>
|
| 13 | #include <net/netfilter/nf_conntrack.h>
|
| 14 | #include <net/netfilter/nf_conntrack_l4proto.h>
|
| 15 | #include <net/netfilter/nf_conntrack_helper.h>
|
| 16 | #include <net/netfilter/nf_conntrack_ecache.h>
|
| 17 | #include <net/netfilter/nf_conntrack_timeout.h>
|
| 18 | #include <net/netfilter/nf_conntrack_zones.h>
|
| 19 | #include "nf_internals.h"
|
| 20 |
|
| 21 | static inline int xt_ct_target(struct sk_buff *skb, struct nf_conn *ct)
|
| 22 | {
|
| 23 |
|
| 24 | if (skb->_nfct != 0)
|
| 25 | return XT_CONTINUE;
|
| 26 |
|
| 27 | if (ct) {
|
| 28 | refcount_inc(&ct->ct_general.use);
|
| 29 | nf_ct_set(skb, ct, IP_CT_NEW);
|
| 30 | } else {
|
| 31 | nf_ct_set(skb, ct, IP_CT_UNTRACKED);
|
| 32 | }
|
| 33 |
|
| 34 | return XT_CONTINUE;
|
| 35 | }
|
| 36 |
|
| 37 | static unsigned int xt_ct_target_v0(struct sk_buff *skb,
|
| 38 | const struct xt_action_param *par)
|
| 39 | {
|
| 40 | const struct xt_ct_target_info *info = par->targinfo;
|
| 41 | struct nf_conn *ct = info->ct;
|
| 42 |
|
| 43 | return xt_ct_target(skb, ct);
|
| 44 | }
|
| 45 |
|
| 46 | static unsigned int xt_ct_target_v1(struct sk_buff *skb,
|
| 47 | const struct xt_action_param *par)
|
| 48 | {
|
| 49 | const struct xt_ct_target_info_v1 *info = par->targinfo;
|
| 50 | struct nf_conn *ct = info->ct;
|
| 51 |
|
| 52 | return xt_ct_target(skb, ct);
|
| 53 | }
|
| 54 |
|
| 55 | static u8 xt_ct_find_proto(const struct xt_tgchk_param *par)
|
| 56 | {
|
| 57 | if (par->family == NFPROTO_IPV4) {
|
| 58 | const struct ipt_entry *e = par->entryinfo;
|
| 59 |
|
| 60 | if (e->ip.invflags & IPT_INV_PROTO)
|
| 61 | return 0;
|
| 62 | return e->ip.proto;
|
| 63 | } else if (par->family == NFPROTO_IPV6) {
|
| 64 | const struct ip6t_entry *e = par->entryinfo;
|
| 65 |
|
| 66 | if (e->ipv6.invflags & IP6T_INV_PROTO)
|
| 67 | return 0;
|
| 68 | return e->ipv6.proto;
|
| 69 | } else
|
| 70 | return 0;
|
| 71 | }
|
| 72 |
|
| 73 | static int
|
| 74 | xt_ct_set_helper(struct nf_conn *ct, const char *helper_name,
|
| 75 | const struct xt_tgchk_param *par)
|
| 76 | {
|
| 77 | struct nf_conntrack_helper *helper;
|
| 78 | struct nf_conn_help *help;
|
| 79 | u8 proto;
|
| 80 |
|
| 81 | proto = xt_ct_find_proto(par);
|
| 82 | if (!proto) {
|
| 83 | pr_info_ratelimited("You must specify a L4 protocol and not use inversions on it\n");
|
| 84 | return -ENOENT;
|
| 85 | }
|
| 86 |
|
| 87 | helper = nf_conntrack_helper_try_module_get(helper_name, par->family,
|
| 88 | proto);
|
| 89 | if (helper == NULL) {
|
| 90 | pr_info_ratelimited("No such helper \"%s\"\n", helper_name);
|
| 91 | return -ENOENT;
|
| 92 | }
|
| 93 |
|
| 94 | help = nf_ct_helper_ext_add(ct, GFP_KERNEL);
|
| 95 | if (help == NULL) {
|
| 96 | nf_conntrack_helper_put(helper);
|
| 97 | return -ENOMEM;
|
| 98 | }
|
| 99 |
|
| 100 | rcu_assign_pointer(help->helper, helper);
|
| 101 | return 0;
|
| 102 | }
|
| 103 |
|
| 104 | static int
|
| 105 | xt_ct_set_timeout(struct nf_conn *ct, const struct xt_tgchk_param *par,
|
| 106 | const char *timeout_name)
|
| 107 | {
|
| 108 | #ifdef CONFIG_NF_CONNTRACK_TIMEOUT
|
| 109 | const struct nf_conntrack_l4proto *l4proto;
|
| 110 | u8 proto;
|
| 111 |
|
| 112 | proto = xt_ct_find_proto(par);
|
| 113 | if (!proto) {
|
| 114 | pr_info_ratelimited("You must specify a L4 protocol and not "
|
| 115 | "use inversions on it");
|
| 116 | return -EINVAL;
|
| 117 | }
|
| 118 | l4proto = nf_ct_l4proto_find(proto);
|
| 119 | return nf_ct_set_timeout(par->net, ct, par->family, l4proto->l4proto,
|
| 120 | timeout_name);
|
| 121 |
|
| 122 | #else
|
| 123 | return -EOPNOTSUPP;
|
| 124 | #endif
|
| 125 | }
|
| 126 |
|
| 127 | static u16 xt_ct_flags_to_dir(const struct xt_ct_target_info_v1 *info)
|
| 128 | {
|
| 129 | switch (info->flags & (XT_CT_ZONE_DIR_ORIG |
|
| 130 | XT_CT_ZONE_DIR_REPL)) {
|
| 131 | case XT_CT_ZONE_DIR_ORIG:
|
| 132 | return NF_CT_ZONE_DIR_ORIG;
|
| 133 | case XT_CT_ZONE_DIR_REPL:
|
| 134 | return NF_CT_ZONE_DIR_REPL;
|
| 135 | default:
|
| 136 | return NF_CT_DEFAULT_ZONE_DIR;
|
| 137 | }
|
| 138 | }
|
| 139 |
|
| 140 | static void xt_ct_put_helper(struct nf_conn_help *help)
|
| 141 | {
|
| 142 | struct nf_conntrack_helper *helper;
|
| 143 |
|
| 144 | if (!help)
|
| 145 | return;
|
| 146 |
|
| 147 | |
| 148 | * already detached (post-replacement).
|
| 149 |
|
| 150 | helper = rcu_dereference_raw(help->helper);
|
| 151 | if (helper)
|
| 152 | nf_conntrack_helper_put(helper);
|
| 153 | }
|
| 154 |
|
| 155 | static int xt_ct_tg_check(const struct xt_tgchk_param *par,
|
| 156 | struct xt_ct_target_info_v1 *info)
|
| 157 | {
|
| 158 | struct nf_conntrack_zone zone;
|
| 159 | struct nf_conn_help *help;
|
| 160 | struct nf_conn *ct;
|
| 161 | int ret = -EOPNOTSUPP;
|
| 162 |
|
| 163 | if (info->flags & XT_CT_NOTRACK) {
|
| 164 | ct = NULL;
|
| 165 | goto out;
|
| 166 | }
|
| 167 |
|
| 168 | #ifndef CONFIG_NF_CONNTRACK_ZONES
|
| 169 | if (info->zone || info->flags & (XT_CT_ZONE_DIR_ORIG |
|
| 170 | XT_CT_ZONE_DIR_REPL |
|
| 171 | XT_CT_ZONE_MARK))
|
| 172 | goto err1;
|
| 173 | #endif
|
| 174 |
|
| 175 | ret = nf_ct_netns_get(par->net, par->family);
|
| 176 | if (ret < 0)
|
| 177 | goto err1;
|
| 178 |
|
| 179 | memset(&zone, 0, sizeof(zone));
|
| 180 | zone.id = info->zone;
|
| 181 | zone.dir = xt_ct_flags_to_dir(info);
|
| 182 | if (info->flags & XT_CT_ZONE_MARK)
|
| 183 | zone.flags |= NF_CT_FLAG_MARK;
|
| 184 |
|
| 185 | ct = nf_ct_tmpl_alloc(par->net, &zone, GFP_KERNEL);
|
| 186 | if (!ct) {
|
| 187 | ret = -ENOMEM;
|
| 188 | goto err2;
|
| 189 | }
|
| 190 |
|
| 191 | if ((info->ct_events || info->exp_events) &&
|
| 192 | !nf_ct_ecache_ext_add(ct, info->ct_events, info->exp_events,
|
| 193 | GFP_KERNEL)) {
|
| 194 | ret = -EINVAL;
|
| 195 | goto err3;
|
| 196 | }
|
| 197 |
|
| 198 | if (info->helper[0]) {
|
| 199 | if (strnlen(info->helper, sizeof(info->helper)) == sizeof(info->helper)) {
|
| 200 | ret = -ENAMETOOLONG;
|
| 201 | goto err3;
|
| 202 | }
|
| 203 |
|
| 204 | ret = xt_ct_set_helper(ct, info->helper, par);
|
| 205 | if (ret < 0)
|
| 206 | goto err3;
|
| 207 | }
|
| 208 |
|
| 209 | if (info->timeout[0]) {
|
| 210 | if (strnlen(info->timeout, sizeof(info->timeout)) == sizeof(info->timeout)) {
|
| 211 | ret = -ENAMETOOLONG;
|
| 212 | goto err4;
|
| 213 | }
|
| 214 |
|
| 215 | ret = xt_ct_set_timeout(ct, par, info->timeout);
|
| 216 | if (ret < 0)
|
| 217 | goto err4;
|
| 218 | }
|
| 219 | __set_bit(IPS_CONFIRMED_BIT, &ct->status);
|
| 220 | out:
|
| 221 | info->ct = ct;
|
| 222 | return 0;
|
| 223 |
|
| 224 | err4:
|
| 225 | help = nfct_help(ct);
|
| 226 | xt_ct_put_helper(help);
|
| 227 | err3:
|
| 228 | nf_ct_tmpl_free(ct);
|
| 229 | err2:
|
| 230 | nf_ct_netns_put(par->net, par->family);
|
| 231 | err1:
|
| 232 | return ret;
|
| 233 | }
|
| 234 |
|
| 235 | static int xt_ct_tg_check_v0(const struct xt_tgchk_param *par)
|
| 236 | {
|
| 237 | struct xt_ct_target_info *info = par->targinfo;
|
| 238 | struct xt_ct_target_info_v1 info_v1 = {
|
| 239 | .flags = info->flags,
|
| 240 | .zone = info->zone,
|
| 241 | .ct_events = info->ct_events,
|
| 242 | .exp_events = info->exp_events,
|
| 243 | };
|
| 244 | int ret;
|
| 245 |
|
| 246 | if (info->flags & ~XT_CT_NOTRACK)
|
| 247 | return -EINVAL;
|
| 248 |
|
| 249 | memcpy(info_v1.helper, info->helper, sizeof(info->helper));
|
| 250 |
|
| 251 | ret = xt_ct_tg_check(par, &info_v1);
|
| 252 | if (ret < 0)
|
| 253 | return ret;
|
| 254 |
|
| 255 | info->ct = info_v1.ct;
|
| 256 |
|
| 257 | return ret;
|
| 258 | }
|
| 259 |
|
| 260 | static int xt_ct_tg_check_v1(const struct xt_tgchk_param *par)
|
| 261 | {
|
| 262 | struct xt_ct_target_info_v1 *info = par->targinfo;
|
| 263 |
|
| 264 | if (info->flags & ~XT_CT_NOTRACK)
|
| 265 | return -EINVAL;
|
| 266 |
|
| 267 | return xt_ct_tg_check(par, par->targinfo);
|
| 268 | }
|
| 269 |
|
| 270 | static int xt_ct_tg_check_v2(const struct xt_tgchk_param *par)
|
| 271 | {
|
| 272 | struct xt_ct_target_info_v1 *info = par->targinfo;
|
| 273 |
|
| 274 | if (info->flags & ~XT_CT_MASK)
|
| 275 | return -EINVAL;
|
| 276 |
|
| 277 | return xt_ct_tg_check(par, par->targinfo);
|
| 278 | }
|
| 279 |
|
| 280 | static void xt_ct_tg_destroy(const struct xt_tgdtor_param *par,
|
| 281 | struct xt_ct_target_info_v1 *info)
|
| 282 | {
|
| 283 | struct nf_conn *ct = info->ct;
|
| 284 | struct nf_conn_help *help;
|
| 285 |
|
| 286 | if (ct) {
|
| 287 | help = nfct_help(ct);
|
| 288 | xt_ct_put_helper(help);
|
| 289 |
|
| 290 | nf_ct_netns_put(par->net, par->family);
|
| 291 |
|
| 292 | nf_ct_destroy_timeout(ct);
|
| 293 | nf_ct_put(info->ct);
|
| 294 | }
|
| 295 | }
|
| 296 |
|
| 297 | static void xt_ct_tg_destroy_v0(const struct xt_tgdtor_param *par)
|
| 298 | {
|
| 299 | struct xt_ct_target_info *info = par->targinfo;
|
| 300 | struct xt_ct_target_info_v1 info_v1 = {
|
| 301 | .flags = info->flags,
|
| 302 | .zone = info->zone,
|
| 303 | .ct_events = info->ct_events,
|
| 304 | .exp_events = info->exp_events,
|
| 305 | .ct = info->ct,
|
| 306 | };
|
| 307 | memcpy(info_v1.helper, info->helper, sizeof(info->helper));
|
| 308 |
|
| 309 | xt_ct_tg_destroy(par, &info_v1);
|
| 310 | }
|
| 311 |
|
| 312 | static void xt_ct_tg_destroy_v1(const struct xt_tgdtor_param *par)
|
| 313 | {
|
| 314 | xt_ct_tg_destroy(par, par->targinfo);
|
| 315 | }
|
| 316 |
|
| 317 | static unsigned int
|
| 318 | notrack_tg(struct sk_buff *skb, const struct xt_action_param *par)
|
| 319 | {
|
| 320 |
|
| 321 | if (skb->_nfct != 0)
|
| 322 | return XT_CONTINUE;
|
| 323 |
|
| 324 | nf_ct_set(skb, NULL, IP_CT_UNTRACKED);
|
| 325 |
|
| 326 | return XT_CONTINUE;
|
| 327 | }
|
| 328 |
|
| 329 | static struct xt_target xt_ct_tg_reg[] __read_mostly = {
|
| 330 | {
|
| 331 | .name = "NOTRACK",
|
| 332 | .revision = 0,
|
| 333 | .family = NFPROTO_IPV4,
|
| 334 | .target = notrack_tg,
|
| 335 | .table = "raw",
|
| 336 | .me = THIS_MODULE,
|
| 337 | },
|
| 338 | {
|
| 339 | .name = "CT",
|
| 340 | .family = NFPROTO_IPV4,
|
| 341 | .targetsize = sizeof(struct xt_ct_target_info),
|
| 342 | .usersize = offsetof(struct xt_ct_target_info, ct),
|
| 343 | .checkentry = xt_ct_tg_check_v0,
|
| 344 | .destroy = xt_ct_tg_destroy_v0,
|
| 345 | .target = xt_ct_target_v0,
|
| 346 | .table = "raw",
|
| 347 | .me = THIS_MODULE,
|
| 348 | },
|
| 349 | {
|
| 350 | .name = "CT",
|
| 351 | .family = NFPROTO_IPV4,
|
| 352 | .revision = 1,
|
| 353 | .targetsize = sizeof(struct xt_ct_target_info_v1),
|
| 354 | .usersize = offsetof(struct xt_ct_target_info_v1, ct),
|
| 355 | .checkentry = xt_ct_tg_check_v1,
|
| 356 | .destroy = xt_ct_tg_destroy_v1,
|
| 357 | .target = xt_ct_target_v1,
|
| 358 | .table = "raw",
|
| 359 | .me = THIS_MODULE,
|
| 360 | },
|
| 361 | {
|
| 362 | .name = "CT",
|
| 363 | .family = NFPROTO_IPV4,
|
| 364 | .revision = 2,
|
| 365 | .targetsize = sizeof(struct xt_ct_target_info_v1),
|
| 366 | .usersize = offsetof(struct xt_ct_target_info_v1, ct),
|
| 367 | .checkentry = xt_ct_tg_check_v2,
|
| 368 | .destroy = xt_ct_tg_destroy_v1,
|
| 369 | .target = xt_ct_target_v1,
|
| 370 | .table = "raw",
|
| 371 | .me = THIS_MODULE,
|
| 372 | },
|
| 373 | #if IS_ENABLED(CONFIG_IP6_NF_IPTABLES)
|
| 374 | {
|
| 375 | .name = "NOTRACK",
|
| 376 | .revision = 0,
|
| 377 | .family = NFPROTO_IPV6,
|
| 378 | .target = notrack_tg,
|
| 379 | .table = "raw",
|
| 380 | .me = THIS_MODULE,
|
| 381 | },
|
| 382 | {
|
| 383 | .name = "CT",
|
| 384 | .family = NFPROTO_IPV6,
|
| 385 | .targetsize = sizeof(struct xt_ct_target_info),
|
| 386 | .usersize = offsetof(struct xt_ct_target_info, ct),
|
| 387 | .checkentry = xt_ct_tg_check_v0,
|
| 388 | .destroy = xt_ct_tg_destroy_v0,
|
| 389 | .target = xt_ct_target_v0,
|
| 390 | .table = "raw",
|
| 391 | .me = THIS_MODULE,
|
| 392 | },
|
| 393 | {
|
| 394 | .name = "CT",
|
| 395 | .family = NFPROTO_IPV6,
|
| 396 | .revision = 1,
|
| 397 | .targetsize = sizeof(struct xt_ct_target_info_v1),
|
| 398 | .usersize = offsetof(struct xt_ct_target_info_v1, ct),
|
| 399 | .checkentry = xt_ct_tg_check_v1,
|
| 400 | .destroy = xt_ct_tg_destroy_v1,
|
| 401 | .target = xt_ct_target_v1,
|
| 402 | .table = "raw",
|
| 403 | .me = THIS_MODULE,
|
| 404 | },
|
| 405 | {
|
| 406 | .name = "CT",
|
| 407 | .family = NFPROTO_IPV6,
|
| 408 | .revision = 2,
|
| 409 | .targetsize = sizeof(struct xt_ct_target_info_v1),
|
| 410 | .usersize = offsetof(struct xt_ct_target_info_v1, ct),
|
| 411 | .checkentry = xt_ct_tg_check_v2,
|
| 412 | .destroy = xt_ct_tg_destroy_v1,
|
| 413 | .target = xt_ct_target_v1,
|
| 414 | .table = "raw",
|
| 415 | .me = THIS_MODULE,
|
| 416 | },
|
| 417 | #endif
|
| 418 | };
|
| 419 |
|
| 420 | static int __init xt_ct_tg_init(void)
|
| 421 | {
|
| 422 | return xt_register_targets(xt_ct_tg_reg, ARRAY_SIZE(xt_ct_tg_reg));
|
| 423 | }
|
| 424 |
|
| 425 | static void __exit xt_ct_tg_exit(void)
|
| 426 | {
|
| 427 | xt_unregister_targets(xt_ct_tg_reg, ARRAY_SIZE(xt_ct_tg_reg));
|
| 428 | }
|
| 429 |
|
| 430 | module_init(xt_ct_tg_init);
|
| 431 | module_exit(xt_ct_tg_exit);
|
| 432 |
|
| 433 | MODULE_LICENSE("GPL");
|
| 434 | MODULE_DESCRIPTION("Xtables: connection tracking target");
|
| 435 | MODULE_ALIAS("ipt_CT");
|
| 436 | MODULE_ALIAS("ip6t_CT");
|
| 437 | MODULE_ALIAS("ipt_NOTRACK");
|
| 438 | MODULE_ALIAS("ip6t_NOTRACK");
|