blob: 122aa8b0147b7360a65a90648603b45fd555d84e [file] [log] [blame]
Jan Engelhardt96e32272008-01-14 23:39:13 -08001/*
2 * xt_connmark - Netfilter module to match connection mark values
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
Jan Engelhardt96e32272008-01-14 23:39:13 -08004 * Copyright (C) 2002,2004 MARA Systems AB <http://www.marasystems.com>
5 * by Henrik Nordstrom <hno@marasystems.com>
6 * Copyright © CC Computer Consultants GmbH, 2007 - 2008
7 * Jan Engelhardt <jengelh@computergmbh.de>
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 */
23
24#include <linux/module.h>
25#include <linux/skbuff.h>
Patrick McHardy587aa642007-03-14 16:37:25 -070026#include <net/netfilter/nf_conntrack.h>
27#include <linux/netfilter/x_tables.h>
28#include <linux/netfilter/xt_connmark.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029
Robert P. J. Day3a4fa0a2007-10-19 23:10:43 +020030MODULE_AUTHOR("Henrik Nordstrom <hno@marasystems.com>");
Jan Engelhardt2ae15b62008-01-14 23:42:28 -080031MODULE_DESCRIPTION("Xtables: connection mark match");
Linus Torvalds1da177e2005-04-16 15:20:36 -070032MODULE_LICENSE("GPL");
Harald Welte2e4e6a12006-01-12 13:30:04 -080033MODULE_ALIAS("ipt_connmark");
Jan Engelhardt73aaf932007-10-11 14:36:40 -070034MODULE_ALIAS("ip6t_connmark");
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Jan Engelhardt1d93a9c2007-07-07 22:15:35 -070036static bool
Jan Engelhardtf7108a22008-10-08 11:35:18 +020037connmark_mt(const struct sk_buff *skb, const struct xt_match_param *par)
Linus Torvalds1da177e2005-04-16 15:20:36 -070038{
Jan Engelhardtf7108a22008-10-08 11:35:18 +020039 const struct xt_connmark_mtinfo1 *info = par->matchinfo;
Jan Engelhardt96e32272008-01-14 23:39:13 -080040 enum ip_conntrack_info ctinfo;
41 const struct nf_conn *ct;
42
43 ct = nf_ct_get(skb, &ctinfo);
44 if (ct == NULL)
45 return false;
46
47 return ((ct->mark & info->mask) == info->mark) ^ info->invert;
48}
49
Jan Engelhardt9b4fce72008-10-08 11:35:18 +020050static bool connmark_mt_check(const struct xt_mtchk_param *par)
Jan Engelhardt96e32272008-01-14 23:39:13 -080051{
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +020052 if (nf_ct_l3proto_try_module_get(par->family) < 0) {
Jan Engelhardt96e32272008-01-14 23:39:13 -080053 printk(KERN_WARNING "cannot load conntrack support for "
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +020054 "proto=%u\n", par->family);
Jan Engelhardt96e32272008-01-14 23:39:13 -080055 return false;
56 }
57 return true;
58}
59
Jan Engelhardt6be3d852008-10-08 11:35:19 +020060static void connmark_mt_destroy(const struct xt_mtdtor_param *par)
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080061{
Jan Engelhardt92f3b2b2008-10-08 11:35:20 +020062 nf_ct_l3proto_module_put(par->family);
Pablo Neira Ayusob9f78f92006-03-22 13:56:08 -080063}
64
Jan Engelhardt84899a22009-06-12 18:50:33 +020065static struct xt_match connmark_mt_reg __read_mostly = {
66 .name = "connmark",
67 .revision = 1,
68 .family = NFPROTO_UNSPEC,
69 .checkentry = connmark_mt_check,
70 .match = connmark_mt,
71 .matchsize = sizeof(struct xt_connmark_mtinfo1),
72 .destroy = connmark_mt_destroy,
73 .me = THIS_MODULE,
Patrick McHardy5d04bff2006-03-20 18:01:58 -080074};
Harald Welte2e4e6a12006-01-12 13:30:04 -080075
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080076static int __init connmark_mt_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077{
Jan Engelhardt84899a22009-06-12 18:50:33 +020078 return xt_register_match(&connmark_mt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
80
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080081static void __exit connmark_mt_exit(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
Jan Engelhardt84899a22009-06-12 18:50:33 +020083 xt_unregister_match(&connmark_mt_reg);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
Jan Engelhardtd3c5ee62007-12-04 23:24:03 -080086module_init(connmark_mt_init);
87module_exit(connmark_mt_exit);