@@ -112,6 +112,54 @@ ct_proto_port_check(const struct xt_conntrack_mtinfo2 *info,
112112 return true;
113113}
114114
115+ static inline bool
116+ port_match(u16 min, u16 max, u16 port, bool invert)
117+ {
118+ return (port >= min && port <= max) ^ invert;
119+ }
120+
121+ static inline bool
122+ ct_proto_port_check_v3(const struct xt_conntrack_mtinfo3 *info,
123+ const struct nf_conn *ct)
124+ {
125+ const struct nf_conntrack_tuple *tuple;
126+
127+ tuple = &ct->tuplehash[IP_CT_DIR_ORIGINAL].tuple;
128+ if ((info->match_flags & XT_CONNTRACK_PROTO) &&
129+ (nf_ct_protonum(ct) == info->l4proto) ^
130+ !(info->invert_flags & XT_CONNTRACK_PROTO))
131+ return false;
132+
133+ /* Shortcut to match all recognized protocols by using ->src.all. */
134+ if ((info->match_flags & XT_CONNTRACK_ORIGSRC_PORT) &&
135+ !port_match(info->origsrc_port, info->origsrc_port_high,
136+ ntohs(tuple->src.u.all),
137+ info->invert_flags & XT_CONNTRACK_ORIGSRC_PORT))
138+ return false;
139+
140+ if ((info->match_flags & XT_CONNTRACK_ORIGDST_PORT) &&
141+ !port_match(info->origdst_port, info->origdst_port_high,
142+ ntohs(tuple->dst.u.all),
143+ info->invert_flags & XT_CONNTRACK_ORIGDST_PORT))
144+ return false;
145+
146+ tuple = &ct->tuplehash[IP_CT_DIR_REPLY].tuple;
147+
148+ if ((info->match_flags & XT_CONNTRACK_REPLSRC_PORT) &&
149+ !port_match(info->replsrc_port, info->replsrc_port_high,
150+ ntohs(tuple->src.u.all),
151+ info->invert_flags & XT_CONNTRACK_REPLSRC_PORT))
152+ return false;
153+
154+ if ((info->match_flags & XT_CONNTRACK_REPLDST_PORT) &&
155+ !port_match(info->repldst_port, info->repldst_port_high,
156+ ntohs(tuple->dst.u.all),
157+ info->invert_flags & XT_CONNTRACK_REPLDST_PORT))
158+ return false;
159+
160+ return true;
161+ }
162+
115163static bool
116164conntrack_mt(const struct sk_buff *skb, struct xt_action_param *par,
117165 u16 state_mask, u16 status_mask)
@@ -170,8 +218,13 @@ conntrack_mt(const struct sk_buff *skb, struct xt_action_param *par,
170218 !(info->invert_flags & XT_CONNTRACK_REPLDST))
171219 return false;
172220
173- if (!ct_proto_port_check(info, ct))
174- return false;
221+ if (par->match->revision != 3) {
222+ if (!ct_proto_port_check(info, ct))
223+ return false;
224+ } else {
225+ if (!ct_proto_port_check_v3(par->matchinfo, ct))
226+ return false;
227+ }
175228
176229 if ((info->match_flags & XT_CONNTRACK_STATUS) &&
177230 (!!(status_mask & ct->status) ^
@@ -207,6 +260,14 @@ conntrack_mt_v2(const struct sk_buff *skb, struct xt_action_param *par)
207260 return conntrack_mt(skb, par, info->state_mask, info->status_mask);
208261}
209262
263+ static bool
264+ conntrack_mt_v3(const struct sk_buff *skb, struct xt_action_param *par)
265+ {
266+ const struct xt_conntrack_mtinfo3 *info = par->matchinfo;
267+
268+ return conntrack_mt(skb, par, info->state_mask, info->status_mask);
269+ }
270+
210271static int conntrack_mt_check(const struct xt_mtchk_param *par)
211272{
212273 int ret;
@@ -244,6 +305,16 @@ static struct xt_match conntrack_mt_reg[] __read_mostly = {
244305 .destroy = conntrack_mt_destroy,
245306 .me = THIS_MODULE,
246307 },
308+ {
309+ .name = "conntrack",
310+ .revision = 3,
311+ .family = NFPROTO_UNSPEC,
312+ .matchsize = sizeof(struct xt_conntrack_mtinfo3),
313+ .match = conntrack_mt_v3,
314+ .checkentry = conntrack_mt_check,
315+ .destroy = conntrack_mt_destroy,
316+ .me = THIS_MODULE,
317+ },
247318};
248319
249320static int __init conntrack_mt_init(void)
0 commit comments