Skip to content

Commit b416499

Browse files
Jan Engelhardtdavem330
authored andcommitted
[NETFILTER]: xt_conntrack: add port and direction matching
Extend the xt_conntrack match revision 1 by port matching (all four {orig,repl}{src,dst}) and by packet direction matching. Signed-off-by: Jan Engelhardt <jengelh@computergmbh.de> Signed-off-by: Patrick McHardy <kaber@trash.net> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 41d0cde commit b416499

2 files changed

Lines changed: 63 additions & 17 deletions

File tree

‎include/linux/netfilter/xt_conntrack.h‎

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,6 @@
66
#define _XT_CONNTRACK_H
77

88
#include <linux/netfilter/nf_conntrack_tuple_common.h>
9-
#ifdef __KERNEL__
10-
# include <linux/in.h>
11-
#endif
129

1310
#define XT_CONNTRACK_STATE_BIT(ctinfo) (1 << ((ctinfo)%IP_CT_IS_REPLY+1))
1411
#define XT_CONNTRACK_STATE_INVALID (1 << 0)
@@ -18,14 +15,21 @@
1815
#define XT_CONNTRACK_STATE_UNTRACKED (1 << (IP_CT_NUMBER + 3))
1916

2017
/* flags, invflags: */
21-
#define XT_CONNTRACK_STATE 0x01
22-
#define XT_CONNTRACK_PROTO 0x02
23-
#define XT_CONNTRACK_ORIGSRC 0x04
24-
#define XT_CONNTRACK_ORIGDST 0x08
25-
#define XT_CONNTRACK_REPLSRC 0x10
26-
#define XT_CONNTRACK_REPLDST 0x20
27-
#define XT_CONNTRACK_STATUS 0x40
28-
#define XT_CONNTRACK_EXPIRES 0x80
18+
enum {
19+
XT_CONNTRACK_STATE = 1 << 0,
20+
XT_CONNTRACK_PROTO = 1 << 1,
21+
XT_CONNTRACK_ORIGSRC = 1 << 2,
22+
XT_CONNTRACK_ORIGDST = 1 << 3,
23+
XT_CONNTRACK_REPLSRC = 1 << 4,
24+
XT_CONNTRACK_REPLDST = 1 << 5,
25+
XT_CONNTRACK_STATUS = 1 << 6,
26+
XT_CONNTRACK_EXPIRES = 1 << 7,
27+
XT_CONNTRACK_ORIGSRC_PORT = 1 << 8,
28+
XT_CONNTRACK_ORIGDST_PORT = 1 << 9,
29+
XT_CONNTRACK_REPLSRC_PORT = 1 << 10,
30+
XT_CONNTRACK_REPLDST_PORT = 1 << 11,
31+
XT_CONNTRACK_DIRECTION = 1 << 12,
32+
};
2933

3034
/* This is exposed to userspace, so remains frozen in time. */
3135
struct ip_conntrack_old_tuple
@@ -70,8 +74,10 @@ struct xt_conntrack_mtinfo1 {
7074
union nf_inet_addr repldst_addr, repldst_mask;
7175
u_int32_t expires_min, expires_max;
7276
u_int16_t l4proto;
77+
__be16 origsrc_port, origdst_port;
78+
__be16 replsrc_port, repldst_port;
79+
u_int16_t match_flags, invert_flags;
7380
u_int8_t state_mask, status_mask;
74-
u_int8_t match_flags, invert_flags;
7581
};
7682

7783
#endif /*_XT_CONNTRACK_H*/

‎net/netfilter/xt_conntrack.c‎

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
*
55
* (C) 2001 Marc Boucher (marc@mbsi.ca).
66
* Copyright © CC Computer Consultants GmbH, 2007 - 2008
7-
* Jan Engelhardt <jengelh@computergmbh.de>
87
*
98
* This program is free software; you can redistribute it and/or modify
109
* it under the terms of the GNU General Public License version 2 as
@@ -20,6 +19,7 @@
2019

2120
MODULE_LICENSE("GPL");
2221
MODULE_AUTHOR("Marc Boucher <marc@mbsi.ca>");
22+
MODULE_AUTHOR("Jan Engelhardt <jengelh@computergmbh.de>");
2323
MODULE_DESCRIPTION("Xtables: connection tracking state match");
2424
MODULE_ALIAS("ipt_conntrack");
2525
MODULE_ALIAS("ip6t_conntrack");
@@ -166,6 +166,44 @@ conntrack_mt_repldst(const struct nf_conn *ct,
166166
&info->repldst_addr, &info->repldst_mask, family);
167167
}
168168

169+
static inline bool
170+
ct_proto_port_check(const struct xt_conntrack_mtinfo1 *info,
171+
const struct nf_conn *ct)
172+
{
173+
const struct nf_conntrack_tuple *tuple;
174+
175+
tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
176+
if ((info->match_flags & XT_CONNTRACK_PROTO) &&
177+
(tuple->dst.protonum == info->l4proto) ^
178+
!(info->invert_flags & XT_CONNTRACK_PROTO))
179+
return false;
180+
181+
/* Shortcut to match all recognized protocols by using ->src.all. */
182+
if ((info->match_flags & XT_CONNTRACK_ORIGSRC_PORT) &&
183+
(tuple->src.u.all == info->origsrc_port) ^
184+
!(info->invert_flags & XT_CONNTRACK_ORIGSRC_PORT))
185+
return false;
186+
187+
if ((info->match_flags & XT_CONNTRACK_ORIGDST_PORT) &&
188+
(tuple->dst.u.all == info->origdst_port) ^
189+
!(info->invert_flags & XT_CONNTRACK_ORIGDST_PORT))
190+
return false;
191+
192+
tuple = &ct->tuplehash[IP_CT_DIR_REPLY].tuple;
193+
194+
if ((info->match_flags & XT_CONNTRACK_REPLSRC_PORT) &&
195+
(tuple->src.u.all == info->replsrc_port) ^
196+
!(info->invert_flags & XT_CONNTRACK_REPLSRC_PORT))
197+
return false;
198+
199+
if ((info->match_flags & XT_CONNTRACK_REPLDST_PORT) &&
200+
(tuple->dst.u.all == info->repldst_port) ^
201+
!(info->invert_flags & XT_CONNTRACK_REPLDST_PORT))
202+
return false;
203+
204+
return true;
205+
}
206+
169207
static bool
170208
conntrack_mt(const struct sk_buff *skb, const struct net_device *in,
171209
const struct net_device *out, const struct xt_match *match,
@@ -200,10 +238,9 @@ conntrack_mt(const struct sk_buff *skb, const struct net_device *in,
200238

201239
if (ct == NULL)
202240
return info->match_flags & XT_CONNTRACK_STATE;
203-
204-
if ((info->match_flags & XT_CONNTRACK_PROTO) &&
205-
((ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple.dst.protonum ==
206-
info->l4proto) ^ !(info->invert_flags & XT_CONNTRACK_PROTO)))
241+
if ((info->match_flags & XT_CONNTRACK_DIRECTION) &&
242+
(CTINFO2DIR(ctinfo) == IP_CT_DIR_ORIGINAL) ^
243+
!!(info->invert_flags & XT_CONNTRACK_DIRECTION))
207244
return false;
208245

209246
if (info->match_flags & XT_CONNTRACK_ORIGSRC)
@@ -226,6 +263,9 @@ conntrack_mt(const struct sk_buff *skb, const struct net_device *in,
226263
!(info->invert_flags & XT_CONNTRACK_REPLDST))
227264
return false;
228265

266+
if (!ct_proto_port_check(info, ct))
267+
return false;
268+
229269
if ((info->match_flags & XT_CONNTRACK_STATUS) &&
230270
(!!(info->status_mask & ct->status) ^
231271
!(info->invert_flags & XT_CONNTRACK_STATUS)))

0 commit comments

Comments
 (0)