Re: [PATCH net v2] net: netfilter: Fix port selection of FTP for NF_NAT_RANGE_PROTO_SPECIFIED

From: Jan Engelhardt
Date: Tue Sep 07 2021 - 11:17:44 EST



On Tuesday 2021-09-07 15:54, Florian Westphal wrote:
>> - /* Try to get same port: if not, try to change it. */
>> - for (port = ntohs(exp->saved_proto.tcp.port); port != 0; port++) {
>> - int ret;
>> + if (htons(nat->range_info.min_proto.all) == 0 ||
>> + htons(nat->range_info.max_proto.all) == 0) {
>
>Either use if (nat->range_info.min_proto.all || ...
>
>or use ntohs(). I will leave it up to you if you prefer
>ntohs(nat->range_info.min_proto.all) == 0 or
>nat->range_info.min_proto.all == ntohs(0).

If one has the option, one should always prefer to put htons/htonl on
the side with the constant literal;
Propagation of constants and compile-time evaluation is the target.

That works for some other functions as well (e.g.
strlen("fixedstring")).