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
2019
2120MODULE_LICENSE ("GPL" );
2221MODULE_AUTHOR ("Marc Boucher <marc@mbsi.ca>" );
22+ MODULE_AUTHOR ("Jan Engelhardt <jengelh@computergmbh.de>" );
2323MODULE_DESCRIPTION ("Xtables: connection tracking state match" );
2424MODULE_ALIAS ("ipt_conntrack" );
2525MODULE_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+
169207static bool
170208conntrack_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