Skip to contentSkip to conversation

netfilter: TCPMSS: fix dropped packets when MSS option is unaligned

PATCH Superseded Kacper Kokot 6 participants 20 messages May 25 – Jun 21

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 netfilter: TCPMSS: fix dropped packets when MSS option is unaligned

Padding TCP options with NOPs is optional, so it is legal to send an MSS option that is not aligned to a word boundary and therefore not aligned for checksum calculation. The current TCPMSS target is not robust to this: when the MSS option is unaligned it produces an invalid checksum, and the packet is dropped.

When the changed word is not aligned, the modified bytes straddle two checksum words, and using the standard incremental update helper (which assumes alignment) produces an invalid checksum:

    | w1     | w2     |
OLD |  a  b  |  c  d  |
NEW |  a  b' |  c' d  |

Since b' and c' sit across w1 and w2, we could compute the incremental checksum in two operations by recalculating w1 and then w2:

    C' = C - w1 + w1' - w2 + w2'

But working it out:

    C' = C - w1 - w2 + w1' + w2'
       = C - (a * 2^8 + b)  - (c * 2^8 + d)
           + (a * 2^8 + b') + (c' * 2^8 + d)
       = C + 2^8 * (a - a + c' - c) + (b' - b + d - d)
       = C + 2^8 * (c' - c) + (b' - b)
       = C - (2^8 * c + b) + (2^8 * c' + b')

So an unaligned incremental checksum can be done in a single operation by byteswapping the changed bytes before passing them to the helper. This patch implements that trick for unaligned MSS option updates.

---
Reproduction script

#!/usr/bin/env python3
import argparse
from scapy.all import *

parser = argparse.ArgumentParser()
parser.add_argument("target_ip")
parser.add_argument("target_port", type=int)
args = parser.parse_args()

def try_handshake(options):

    sport = RandShort()
    ip = IP(dst=args.target_ip)
  
    syn = TCP(
      sport=sport,
      dport=args.target_port,
      flags="S",
      seq=1000,
      options=options
    )
  
    synack = sr1(ip/syn, timeout=2)
  
    print("SYNACK", synack)
    if synack and synack.haslayer(TCP) and synack[TCP].flags == 0x12:
      ack = TCP(
          sport=sport,
          dport=args.target_port,
          flags="R",
          seq=syn.seq + 1,
          ack=synack.seq + 1,
      )
  
      send(ip/ack)
      print("SYN-ACK received")
    else:
      print("No SYN-ACK received")

print("\n>>> MSS Aligned")
try_handshake([

      ('MSS', 1460),
      ("NOP", None),
      ("NOP", None),
      ('SAckOK', b''),
      ('Timestamp', (12345, 0)),
      ('WScale', 7)

])

print("\n>>> MSS Misaligned")
try_handshake([

      ("NOP", None),
      ('MSS', 1460),
      ("NOP", None),
      ('SAckOK', b''),
      ('Timestamp', (12345, 0)),
      ('WScale', 7)

])

A script to reproduce:

#!/usr/bin/env python3
import argparse
from scapy.all import *

parser = argparse.ArgumentParser()
parser.add_argument("target_ip")
parser.add_argument("target_port", type=int)
args = parser.parse_args()

mss_aligned_tcp_options = [

      ('MSS', 1460),
      ("NOP", None),
      ("NOP", None),
      ('SAckOK', b''),
      ('Timestamp', (12345, 0)),
      ('WScale', 7)

]

mss_misaligned_tcp_options = [

      ("NOP", None),
      ('MSS', 1460),
      ("NOP", None),
      ('SAckOK', b''),
      ('Timestamp', (12345, 0)),
      ('WScale', 7)

]

def try_handshake(options):

    sport = RandShort()
    ip = IP(dst=args.target_ip)

    syn = TCP(
      sport=sport,
      dport=args.target_port,
      flags="S",
      seq=1000,
      options=options
    )

    synack = sr1(ip/syn, timeout=2)

    print("SYNACK", synack)

    if synack and synack.haslayer(TCP) and synack[TCP].flags == 0x12:
      ack = TCP(
          sport=sport,
          dport=args.target_port,
          flags="R",
          seq=syn.seq + 1,
          ack=synack.seq + 1,
      )

      send(ip/ack)
      print("SYN-ACK response")
    else:
      print("No SYN-ACK received")

print("\n>>> MSS Aligned")
try_handshake(mss_aligned_tcp_options)
print("\n>>> MSS Misaligned")
try_handshake(mss_misaligned_tcp_options)

 net/netfilter/xt_TCPMSS.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)
net/netfilter/xt_TCPMSS.c +13 -1
index 80e1634bc51f..8e409858505d 100644
@@ -117,6 +117,7 @@ tcpmss_mangle_packet(struct sk_buff *skb,
 	for (i = sizeof(struct tcphdr); i <= tcp_hdrlen - TCPOLEN_MSS; i += optlen(opt, i)) {
 		if (opt[i] == TCPOPT_MSS && opt[i+1] == TCPOLEN_MSS) {
 			u_int16_t oldmss;
+			u16 csum_oldmss, csum_newmss;
 
 			oldmss = (opt[i+2] << 8) | opt[i+3];
 
@@ -130,8 +131,19 @@ tcpmss_mangle_packet(struct sk_buff *skb,
 			opt[i+2] = (newmss & 0xff00) >> 8;
 			opt[i+3] = newmss & 0x00ff;
 
+			csum_oldmss = htons(oldmss);
+			csum_newmss = htons(newmss);
+
+			/* MSS may be unaligned; fix up the incremental checksum
+			 * to avoid an invalid checksum and a dropped packet.
+			 */
+			if (((char *)&opt[i + 2] - (char *)tcph) & 0x1 != 0) {
+				csum_oldmss = swab16(csum_oldmss);
+				csum_newmss = swab16(csum_newmss);
+			}
+
 			inet_proto_csum_replace2(&tcph->check, skb,
-						 htons(oldmss), htons(newmss),
+						 csum_oldmss, csum_newmss,
 						 false);
 			return 0;
 		}
Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 5 lines · Kacper Kokot

Padding TCP options with NOPs is optional, so it is legal to send an MSS option that is not aligned to a word boundary and therefore not aligned for checksum calculation. The current TCPMSS target is not robust to this: when the MSS option is unaligned it produces an invalid checksum, and the packet is dropped.

Is this an actual, real world bug? This code is 20+ years old, all that this hints at is that they are always aligned in reality?

(Not disputing theoretical problem).

#2 lore raw
Quoted 2 lines

Is this an actual, real world bug? This code is 20+ years old, all that this hints at is that they are always aligned in reality?

No, as far as I know it's theoretical - I just stumbled on it while debugging something else.

#3 lore raw
In reply to Florian Westphal
Quoted 10 lines · Florian Westphal
Quoted 5 lines · Kacper Kokot

Padding TCP options with NOPs is optional, so it is legal to send an MSS option that is not aligned to a word boundary and therefore not aligned for checksum calculation. The current TCPMSS target is not robust to this: when the MSS option is unaligned it produces an invalid checksum, and the packet is dropped.

Is this an actual, real world bug? This code is 20+ years old, all that this hints at is that they are always aligned in reality?

AFAICS, these issues are not present in real environments as MSS option is placed at the beginning of the options block making it aligned by default usually.

I would say this is more for correctness. I wonder, if we are touching this code, we could use the opportunity to make it use get_unaligned_be16() instead.

#4 lore raw
Quoted 12 lines · On Tue, 26 May 2026 00:08:15 +0200 Fernando Fernandez Mancera…
Quoted 10 lines · Florian Westphal
Quoted 5 lines · Kacper Kokot

Padding TCP options with NOPs is optional, so it is legal to send an MSS option that is not aligned to a word boundary and therefore not aligned for checksum calculation. The current TCPMSS target is not robust to this: when the MSS option is unaligned it produces an invalid checksum, and the packet is dropped.

Is this an actual, real world bug? This code is 20+ years old, all that this hints at is that they are always aligned in reality?

Quoted 7 lines

AFAICS, these issues are not present in real environments as MSS option is placed at the beginning of the options block making it aligned by default usually.

I would say this is more for correctness. I wonder, if we are touching this code, we could use the opportunity to make it use get_unaligned_be16() instead.

gcc and clang convert x[0] << 8 | x[1] (etc) to the appropriate single instruction (and maybe byteswap) on cpu that support misaligned accesses. So there is little to gain from doing it any other way.

-- David

#5 lore raw
In reply to Kacper Kokot
kernel test robot noticed the following build warnings:

Hi Kacper,

kernel test robot noticed the following build warnings:

[auto build test WARNING on nf-next/main]
[also build test WARNING on netfilter-nf/main linus/master v6.16-rc1 next-20260525] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Kacper-Kokot/netfilter-TCPMSS-fix-dropped-packets-when-MSS-option-is-unaligned/20260526-041308
base:   https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git main
patch link:    https://lore.kernel.org/r/20260525201116.407338-2-kacper.kokot.44%40gmail.com
patch subject: [PATCH] netfilter: TCPMSS: fix dropped packets when MSS option is unaligned
config: x86_64-rhel-9.4-ltp (https://download.01.org/0day-ci/archive/20260526/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260526/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

net/netfilter/xt_TCPMSS.c: In function 'tcpmss_mangle_packet':

>> net/netfilter/xt_TCPMSS.c:140:66: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
     140 |                         if (((char *)&opt[i + 2] - (char *)tcph) & 0x1 != 0) {
         |                                                                  ^

vim +140 net/netfilter/xt_TCPMSS.c

    69	
    70	static int
    71	tcpmss_mangle_packet(struct sk_buff *skb,
    72			     const struct xt_action_param *par,
    73			     unsigned int family,
    74			     unsigned int tcphoff,
    75			     unsigned int minlen)
    76	{
    77		const struct xt_tcpmss_info *info = par->targinfo;
    78		struct tcphdr *tcph;
    79		int len, tcp_hdrlen;
    80		unsigned int i;
    81		__be16 oldval;
    82		u16 newmss;
    83		u8 *opt;
    84	
    85		/* This is a fragment, no TCP header is available */
    86		if (par->fragoff != 0)
    87			return 0;
    88	
    89		if (skb_ensure_writable(skb, skb->len))
    90			return -1;
    91	
    92		len = skb->len - tcphoff;
    93		if (len < (int)sizeof(struct tcphdr))
    94			return -1;
    95	
    96		tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
    97		tcp_hdrlen = tcph->doff * 4;
    98	
    99		if (len < tcp_hdrlen || tcp_hdrlen < sizeof(struct tcphdr))
   100			return -1;
   101	
   102		if (info->mss == XT_TCPMSS_CLAMP_PMTU) {
   103			struct net *net = xt_net(par);
   104			unsigned int in_mtu = tcpmss_reverse_mtu(net, skb, family);
   105			unsigned int min_mtu = min(dst_mtu(skb_dst(skb)), in_mtu);
   106	
   107			if (min_mtu <= minlen) {
   108				net_err_ratelimited("unknown or invalid path-MTU (%u)\n",
   109						    min_mtu);
   110				return -1;
   111			}
   112			newmss = min_mtu - minlen;
   113		} else
   114			newmss = info->mss;
   115	
   116		opt = (u_int8_t *)tcph;
   117		for (i = sizeof(struct tcphdr); i <= tcp_hdrlen - TCPOLEN_MSS; i += optlen(opt, i)) {
   118			if (opt[i] == TCPOPT_MSS && opt[i+1] == TCPOLEN_MSS) {
   119				u_int16_t oldmss;
   120				u16 csum_oldmss, csum_newmss;
   121	
   122				oldmss = (opt[i+2] << 8) | opt[i+3];
   123	
   124				/* Never increase MSS, even when setting it, as
   125				 * doing so results in problems for hosts that rely
   126				 * on MSS being set correctly.
   127				 */
   128				if (oldmss <= newmss)
   129					return 0;
   130	
   131				opt[i+2] = (newmss & 0xff00) >> 8;
   132				opt[i+3] = newmss & 0x00ff;
   133	
   134				csum_oldmss = htons(oldmss);
   135				csum_newmss = htons(newmss);
   136	
   137				/* MSS may be unaligned; fix up the incremental checksum
   138				 * to avoid an invalid checksum and a dropped packet.
   139				 */
 > 140				if (((char *)&opt[i + 2] - (char *)tcph) & 0x1 != 0) {

141 csum_oldmss = swab16(csum_oldmss);
142 csum_newmss = swab16(csum_newmss);
143 }
144
145 inet_proto_csum_replace2(&tcph->check, skb,
146 csum_oldmss, csum_newmss,
147 false);
148 return 0;
149 }
150 }
151
152 /* There is data after the header so the option can't be added
153 * without moving it, and doing so may make the SYN packet
154 * itself too large. Accept the packet unmodified instead.
155 */
156 if (len > tcp_hdrlen)
157 return 0;
158
159 /* tcph->doff has 4 bits, do not wrap it to 0 */
160 if (tcp_hdrlen >= 15 * 4)
161 return 0;
162
163 /*
164 * MSS Option not found ?! add it..
165 */
166 if (skb_tailroom(skb) < TCPOLEN_MSS) {
167 if (pskb_expand_head(skb, 0,
168 TCPOLEN_MSS - skb_tailroom(skb),
169 GFP_ATOMIC))
170 return -1;
171 tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
172 }
173
174 skb_put(skb, TCPOLEN_MSS);
175
176 /*
177 * IPv4: RFC 1122 states "If an MSS option is not received at
178 * connection setup, TCP MUST assume a default send MSS of 536".
179 * IPv6: RFC 2460 states IPv6 has a minimum MTU of 1280 and a minimum
180 * length IPv6 header of 60, ergo the default MSS value is 1220
181 * Since no MSS was provided, we must use the default values
182 */
183 if (xt_family(par) == NFPROTO_IPV4)
184 newmss = min(newmss, (u16)536);
185 else
186 newmss = min(newmss, (u16)1220);
187
188 opt = (u_int8_t *)tcph + sizeof(struct tcphdr);
189 memmove(opt + TCPOLEN_MSS, opt, len - sizeof(struct tcphdr));
190
191 inet_proto_csum_replace2(&tcph->check, skb,
192 htons(len), htons(len + TCPOLEN_MSS), true);
193 opt[0] = TCPOPT_MSS;
194 opt[1] = TCPOLEN_MSS;
195 opt[2] = (newmss & 0xff00) >> 8;
196 opt[3] = newmss & 0x00ff;
197
198 inet_proto_csum_replace4(&tcph->check, skb, 0, *((__be32 *)opt), false);
199
200 oldval = ((__be16 *)tcph)[6];
201 tcph->doff += TCPOLEN_MSS/4;
202 inet_proto_csum_replace2(&tcph->check, skb,
203 oldval, ((__be16 *)tcph)[6], false);
204 return TCPOLEN_MSS;
205 }
206

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#6 lore raw
Quoted 23 lines · On Tue, 26 May 2026 15:50:00 +0200 kernel test robot <[email protected]> wrote:

Hi Kacper,

kernel test robot noticed the following build warnings:

[auto build test WARNING on nf-next/main]
[also build test WARNING on netfilter-nf/main linus/master v6.16-rc1 next-20260525] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Kacper-Kokot/netfilter-TCPMSS-fix-dropped-packets-when-MSS-option-is-unaligned/20260526-041308
base:   https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git main
patch link:    https://lore.kernel.org/r/20260525201116.407338-2-kacper.kokot.44%40gmail.com
patch subject: [PATCH] netfilter: TCPMSS: fix dropped packets when MSS option is unaligned
config: x86_64-rhel-9.4-ltp (https://download.01.org/0day-ci/archive/20260526/[email protected]/config)
compiler: gcc-14 (Debian 14.2.0-19) 14.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260526/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

Quoted 6 lines

All warnings (new ones prefixed by >>):

net/netfilter/xt_TCPMSS.c: In function 'tcpmss_mangle_packet':

Quoted 1 line
Quoted 1 line

net/netfilter/xt_TCPMSS.c:140:66: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]

     140 |                         if (((char *)&opt[i + 2] - (char *)tcph) & 0x1 != 0) {
         |                                                                  ^

and, of course, the code works fine because 0x1 != 0 is 1.

K (or maybe R) said that with hindsight they should have corrected the priority of & and | when they added && and || and just fixed all the existing code so it still worked.

-- David

Quoted 147 lines

vim +140 net/netfilter/xt_TCPMSS.c

    69	
    70	static int
    71	tcpmss_mangle_packet(struct sk_buff *skb,
    72			     const struct xt_action_param *par,
    73			     unsigned int family,
    74			     unsigned int tcphoff,
    75			     unsigned int minlen)
    76	{
    77		const struct xt_tcpmss_info *info = par->targinfo;
    78		struct tcphdr *tcph;
    79		int len, tcp_hdrlen;
    80		unsigned int i;
    81		__be16 oldval;
    82		u16 newmss;
    83		u8 *opt;
    84	
    85		/* This is a fragment, no TCP header is available */
    86		if (par->fragoff != 0)
    87			return 0;
    88	
    89		if (skb_ensure_writable(skb, skb->len))
    90			return -1;
    91	
    92		len = skb->len - tcphoff;
    93		if (len < (int)sizeof(struct tcphdr))
    94			return -1;
    95	
    96		tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
    97		tcp_hdrlen = tcph->doff * 4;
    98	
    99		if (len < tcp_hdrlen || tcp_hdrlen < sizeof(struct tcphdr))
   100			return -1;
   101	
   102		if (info->mss == XT_TCPMSS_CLAMP_PMTU) {
   103			struct net *net = xt_net(par);
   104			unsigned int in_mtu = tcpmss_reverse_mtu(net, skb, family);
   105			unsigned int min_mtu = min(dst_mtu(skb_dst(skb)), in_mtu);
   106	
   107			if (min_mtu <= minlen) {
   108				net_err_ratelimited("unknown or invalid path-MTU (%u)\n",
   109						    min_mtu);
   110				return -1;
   111			}
   112			newmss = min_mtu - minlen;
   113		} else
   114			newmss = info->mss;
   115	
   116		opt = (u_int8_t *)tcph;
   117		for (i = sizeof(struct tcphdr); i <= tcp_hdrlen - TCPOLEN_MSS; i += optlen(opt, i)) {
   118			if (opt[i] == TCPOPT_MSS && opt[i+1] == TCPOLEN_MSS) {
   119				u_int16_t oldmss;
   120				u16 csum_oldmss, csum_newmss;
   121	
   122				oldmss = (opt[i+2] << 8) | opt[i+3];
   123	
   124				/* Never increase MSS, even when setting it, as
   125				 * doing so results in problems for hosts that rely
   126				 * on MSS being set correctly.
   127				 */
   128				if (oldmss <= newmss)
   129					return 0;
   130	
   131				opt[i+2] = (newmss & 0xff00) >> 8;
   132				opt[i+3] = newmss & 0x00ff;
   133	
   134				csum_oldmss = htons(oldmss);
   135				csum_newmss = htons(newmss);
   136	
   137				/* MSS may be unaligned; fix up the incremental checksum
   138				 * to avoid an invalid checksum and a dropped packet.
   139				 */
Quoted 1 line

140 if (((char *)&opt[i + 2] - (char *)tcph) & 0x1 != 0) {

   141					csum_oldmss = swab16(csum_oldmss);
   142					csum_newmss = swab16(csum_newmss);
   143				}
   144	
   145				inet_proto_csum_replace2(&tcph->check, skb,
   146							 csum_oldmss, csum_newmss,
   147							 false);
   148				return 0;
   149			}
   150		}
   151	
   152		/* There is data after the header so the option can't be added
   153		 * without moving it, and doing so may make the SYN packet
   154		 * itself too large. Accept the packet unmodified instead.
   155		 */
   156		if (len > tcp_hdrlen)
   157			return 0;
   158	
   159		/* tcph->doff has 4 bits, do not wrap it to 0 */
   160		if (tcp_hdrlen >= 15 * 4)
   161			return 0;
   162	
   163		/*
   164		 * MSS Option not found ?! add it..
   165		 */
   166		if (skb_tailroom(skb) < TCPOLEN_MSS) {
   167			if (pskb_expand_head(skb, 0,
   168					     TCPOLEN_MSS - skb_tailroom(skb),
   169					     GFP_ATOMIC))
   170				return -1;
   171			tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
   172		}
   173	
   174		skb_put(skb, TCPOLEN_MSS);
   175	
   176		/*
   177		 * IPv4: RFC 1122 states "If an MSS option is not received at
   178		 * connection setup, TCP MUST assume a default send MSS of 536".
   179		 * IPv6: RFC 2460 states IPv6 has a minimum MTU of 1280 and a minimum
   180		 * length IPv6 header of 60, ergo the default MSS value is 1220
   181		 * Since no MSS was provided, we must use the default values
   182		 */
   183		if (xt_family(par) == NFPROTO_IPV4)
   184			newmss = min(newmss, (u16)536);
   185		else
   186			newmss = min(newmss, (u16)1220);
   187	
   188		opt = (u_int8_t *)tcph + sizeof(struct tcphdr);
   189		memmove(opt + TCPOLEN_MSS, opt, len - sizeof(struct tcphdr));
   190	
   191		inet_proto_csum_replace2(&tcph->check, skb,
   192					 htons(len), htons(len + TCPOLEN_MSS), true);
   193		opt[0] = TCPOPT_MSS;
   194		opt[1] = TCPOLEN_MSS;
   195		opt[2] = (newmss & 0xff00) >> 8;
   196		opt[3] = newmss & 0x00ff;
   197	
   198		inet_proto_csum_replace4(&tcph->check, skb, 0, *((__be32 *)opt), false);
   199	
   200		oldval = ((__be16 *)tcph)[6];
   201		tcph->doff += TCPOLEN_MSS/4;
   202		inet_proto_csum_replace2(&tcph->check, skb,
   203					 oldval, ((__be16 *)tcph)[6], false);
   204		return TCPOLEN_MSS;
   205	}
   206	

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#7 lore raw
In reply to Kacper Kokot
kernel test robot noticed the following build warnings:

Hi Kacper,

kernel test robot noticed the following build warnings:

[auto build test WARNING on nf-next/main]
[also build test WARNING on netfilter-nf/main linus/master v6.16-rc1 next-20260526] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Kacper-Kokot/netfilter-TCPMSS-fix-dropped-packets-when-MSS-option-is-unaligned/20260526-041308
base:   https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git main
patch link:    https://lore.kernel.org/r/20260525201116.407338-2-kacper.kokot.44%40gmail.com
patch subject: [PATCH] netfilter: TCPMSS: fix dropped packets when MSS option is unaligned
config: x86_64-rhel-9.4-rust (https://download.01.org/0day-ci/archive/20260526/[email protected]/config)
compiler: clang version 20.1.8 (https://github.com/llvm/llvm-project 87f0227cb60147a26a1eeb4fb06e3b505e9c7261)
rustc: rustc 1.88.0 (6b00bc388 2025-06-23)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260526/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> net/netfilter/xt_TCPMSS.c:140:45: warning: & has lower precedence than !=; != will be evaluated first [-Wparentheses]
     140 |                         if (((char *)&opt[i + 2] - (char *)tcph) & 0x1 != 0) {
         |                                                                  ^~~~~~~~~~
   net/netfilter/xt_TCPMSS.c:140:45: note: place parentheses around the '!=' expression to silence this warning
     140 |                         if (((char *)&opt[i + 2] - (char *)tcph) & 0x1 != 0) {
         |                                                                  ^         
         |                                                                    (       )
   net/netfilter/xt_TCPMSS.c:140:45: note: place parentheses around the & expression to evaluate it first
     140 |                         if (((char *)&opt[i + 2] - (char *)tcph) & 0x1 != 0) {
         |                                                                  ^
         |                             (                                         )

1 warning generated.

vim +140 net/netfilter/xt_TCPMSS.c

    69	
    70	static int
    71	tcpmss_mangle_packet(struct sk_buff *skb,
    72			     const struct xt_action_param *par,
    73			     unsigned int family,
    74			     unsigned int tcphoff,
    75			     unsigned int minlen)
    76	{
    77		const struct xt_tcpmss_info *info = par->targinfo;
    78		struct tcphdr *tcph;
    79		int len, tcp_hdrlen;
    80		unsigned int i;
    81		__be16 oldval;
    82		u16 newmss;
    83		u8 *opt;
    84	
    85		/* This is a fragment, no TCP header is available */
    86		if (par->fragoff != 0)
    87			return 0;
    88	
    89		if (skb_ensure_writable(skb, skb->len))
    90			return -1;
    91	
    92		len = skb->len - tcphoff;
    93		if (len < (int)sizeof(struct tcphdr))
    94			return -1;
    95	
    96		tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
    97		tcp_hdrlen = tcph->doff * 4;
    98	
    99		if (len < tcp_hdrlen || tcp_hdrlen < sizeof(struct tcphdr))
   100			return -1;
   101	
   102		if (info->mss == XT_TCPMSS_CLAMP_PMTU) {
   103			struct net *net = xt_net(par);
   104			unsigned int in_mtu = tcpmss_reverse_mtu(net, skb, family);
   105			unsigned int min_mtu = min(dst_mtu(skb_dst(skb)), in_mtu);
   106	
   107			if (min_mtu <= minlen) {
   108				net_err_ratelimited("unknown or invalid path-MTU (%u)\n",
   109						    min_mtu);
   110				return -1;
   111			}
   112			newmss = min_mtu - minlen;
   113		} else
   114			newmss = info->mss;
   115	
   116		opt = (u_int8_t *)tcph;
   117		for (i = sizeof(struct tcphdr); i <= tcp_hdrlen - TCPOLEN_MSS; i += optlen(opt, i)) {
   118			if (opt[i] == TCPOPT_MSS && opt[i+1] == TCPOLEN_MSS) {
   119				u_int16_t oldmss;
   120				u16 csum_oldmss, csum_newmss;
   121	
   122				oldmss = (opt[i+2] << 8) | opt[i+3];
   123	
   124				/* Never increase MSS, even when setting it, as
   125				 * doing so results in problems for hosts that rely
   126				 * on MSS being set correctly.
   127				 */
   128				if (oldmss <= newmss)
   129					return 0;
   130	
   131				opt[i+2] = (newmss & 0xff00) >> 8;
   132				opt[i+3] = newmss & 0x00ff;
   133	
   134				csum_oldmss = htons(oldmss);
   135				csum_newmss = htons(newmss);
   136	
   137				/* MSS may be unaligned; fix up the incremental checksum
   138				 * to avoid an invalid checksum and a dropped packet.
   139				 */
 > 140				if (((char *)&opt[i + 2] - (char *)tcph) & 0x1 != 0) {

141 csum_oldmss = swab16(csum_oldmss);
142 csum_newmss = swab16(csum_newmss);
143 }
144
145 inet_proto_csum_replace2(&tcph->check, skb,
146 csum_oldmss, csum_newmss,
147 false);
148 return 0;
149 }
150 }
151
152 /* There is data after the header so the option can't be added
153 * without moving it, and doing so may make the SYN packet
154 * itself too large. Accept the packet unmodified instead.
155 */
156 if (len > tcp_hdrlen)
157 return 0;
158
159 /* tcph->doff has 4 bits, do not wrap it to 0 */
160 if (tcp_hdrlen >= 15 * 4)
161 return 0;
162
163 /*
164 * MSS Option not found ?! add it..
165 */
166 if (skb_tailroom(skb) < TCPOLEN_MSS) {
167 if (pskb_expand_head(skb, 0,
168 TCPOLEN_MSS - skb_tailroom(skb),
169 GFP_ATOMIC))
170 return -1;
171 tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
172 }
173
174 skb_put(skb, TCPOLEN_MSS);
175
176 /*
177 * IPv4: RFC 1122 states "If an MSS option is not received at
178 * connection setup, TCP MUST assume a default send MSS of 536".
179 * IPv6: RFC 2460 states IPv6 has a minimum MTU of 1280 and a minimum
180 * length IPv6 header of 60, ergo the default MSS value is 1220
181 * Since no MSS was provided, we must use the default values
182 */
183 if (xt_family(par) == NFPROTO_IPV4)
184 newmss = min(newmss, (u16)536);
185 else
186 newmss = min(newmss, (u16)1220);
187
188 opt = (u_int8_t *)tcph + sizeof(struct tcphdr);
189 memmove(opt + TCPOLEN_MSS, opt, len - sizeof(struct tcphdr));
190
191 inet_proto_csum_replace2(&tcph->check, skb,
192 htons(len), htons(len + TCPOLEN_MSS), true);
193 opt[0] = TCPOPT_MSS;
194 opt[1] = TCPOLEN_MSS;
195 opt[2] = (newmss & 0xff00) >> 8;
196 opt[3] = newmss & 0x00ff;
197
198 inet_proto_csum_replace4(&tcph->check, skb, 0, *((__be32 *)opt), false);
199
200 oldval = ((__be16 *)tcph)[6];
201 tcph->doff += TCPOLEN_MSS/4;
202 inet_proto_csum_replace2(&tcph->check, skb,
203 oldval, ((__be16 *)tcph)[6], false);
204 return TCPOLEN_MSS;
205 }
206

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#8 lore raw
Quoted 3 lines

AFAICS, these issues are not present in real environments as MSS option is placed at the beginning of the options block making it aligned by default usually.

I agree, I haven't observed it in any real environment and wouldn't expect to. I found it by reading the code and had to craft a SYN to reproduce. That said the spec permits unaligned options and the kernel shouldn't silently drop legal packets just because nobody sends them today. I can note in the v2 commit message that this is a theoretical fix.

Quoted 6 lines
Quoted 2 lines

I wonder, if we are touching this code, we could use the opportunity to make it use get_unaligned_be16() instead.

gcc and clang convert x[0] << 8 | x[1] (etc) to the appropriate single instruction (and maybe byteswap) on cpu that support misaligned accesses. So there is little to gain from doing it any other way.

Happy to go with whichever you prefer for v2.

Quoted 1 line

and, of course, the code works fine because 0x1 != 0 is 1.

Ha - accidentally correct. I'll add the parens in v2 tomorrow.

Also the reproducer I sent with v1 was clunky. Here's a better one with some results below:

#!/usr/bin/env python3
import argparse
from scapy.all import *

parser = argparse.ArgumentParser()
parser.add_argument("target_ip")
parser.add_argument("target_port", type=int)
args = parser.parse_args()

def gen_mss_syn_options(nops=0):

      return nops * [("NOP", None)] + [("MSS", 1460)]

def syn_check(opts):
sport = RandShort()
ip = IP(dst=args.target_ip)
syn = TCP(sport=sport, dport=args.target_port, flags="S",
seq=1000, options=opts)
synack = sr1(ip/syn, timeout=1, verbose=False) send(ip/TCP(sport=sport, dport=args.target_port, flags="R",
seq=syn.seq+1),
verbose=False)
return not not (synack and synack.haslayer(TCP) and
synack[TCP].flags == 0x12)

for i in range(7):

      n = 5
      ok = sum(syn_check(gen_mss_syn_options(i)) for _ in range(n))
      print(f"{i} nops + mss, {ok}/{n} probes responded")

Before:

0 nops + mss, 5/5 probes responded
1 nops + mss, 0/5 probes responded
2 nops + mss, 5/5 probes responded
3 nops + mss, 0/5 probes responded
4 nops + mss, 5/5 probes responded
5 nops + mss, 0/5 probes responded
6 nops + mss, 5/5 probes responded

After:

0 nops + mss, 5/5 probes responded
1 nops + mss, 5/5 probes responded
2 nops + mss, 5/5 probes responded
3 nops + mss, 5/5 probes responded
4 nops + mss, 5/5 probes responded
5 nops + mss, 5/5 probes responded
6 nops + mss, 5/5 probes responded

#9 lore raw
In reply to Kacper Kokot
kernel test robot noticed the following build warnings:

Hi Kacper,

kernel test robot noticed the following build warnings:

[auto build test WARNING on nf-next/main]
[also build test WARNING on netfilter-nf/main horms-ipvs/master linus/master v7.1-rc5 next-20260527] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Kacper-Kokot/netfilter-TCPMSS-fix-dropped-packets-when-MSS-option-is-unaligned/20260526-041308
base:   https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git main
patch link:    https://lore.kernel.org/r/20260525201116.407338-2-kacper.kokot.44%40gmail.com
patch subject: [PATCH] netfilter: TCPMSS: fix dropped packets when MSS option is unaligned
config: m68k-allmodconfig (https://download.01.org/0day-ci/archive/20260529/[email protected]/config)
compiler: m68k-linux-gcc (GCC) 15.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260529/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

net/netfilter/xt_TCPMSS.c: In function 'tcpmss_mangle_packet':

>> net/netfilter/xt_TCPMSS.c:140:66: warning: suggest parentheses around comparison in operand of '&' [-Wparentheses]
     140 |                         if (((char *)&opt[i + 2] - (char *)tcph) & 0x1 != 0) {
         |                                                                  ^

vim +140 net/netfilter/xt_TCPMSS.c

    69	
    70	static int
    71	tcpmss_mangle_packet(struct sk_buff *skb,
    72			     const struct xt_action_param *par,
    73			     unsigned int family,
    74			     unsigned int tcphoff,
    75			     unsigned int minlen)
    76	{
    77		const struct xt_tcpmss_info *info = par->targinfo;
    78		struct tcphdr *tcph;
    79		int len, tcp_hdrlen;
    80		unsigned int i;
    81		__be16 oldval;
    82		u16 newmss;
    83		u8 *opt;
    84	
    85		/* This is a fragment, no TCP header is available */
    86		if (par->fragoff != 0)
    87			return 0;
    88	
    89		if (skb_ensure_writable(skb, skb->len))
    90			return -1;
    91	
    92		len = skb->len - tcphoff;
    93		if (len < (int)sizeof(struct tcphdr))
    94			return -1;
    95	
    96		tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
    97		tcp_hdrlen = tcph->doff * 4;
    98	
    99		if (len < tcp_hdrlen || tcp_hdrlen < sizeof(struct tcphdr))
   100			return -1;
   101	
   102		if (info->mss == XT_TCPMSS_CLAMP_PMTU) {
   103			struct net *net = xt_net(par);
   104			unsigned int in_mtu = tcpmss_reverse_mtu(net, skb, family);
   105			unsigned int min_mtu = min(dst_mtu(skb_dst(skb)), in_mtu);
   106	
   107			if (min_mtu <= minlen) {
   108				net_err_ratelimited("unknown or invalid path-MTU (%u)\n",
   109						    min_mtu);
   110				return -1;
   111			}
   112			newmss = min_mtu - minlen;
   113		} else
   114			newmss = info->mss;
   115	
   116		opt = (u_int8_t *)tcph;
   117		for (i = sizeof(struct tcphdr); i <= tcp_hdrlen - TCPOLEN_MSS; i += optlen(opt, i)) {
   118			if (opt[i] == TCPOPT_MSS && opt[i+1] == TCPOLEN_MSS) {
   119				u_int16_t oldmss;
   120				u16 csum_oldmss, csum_newmss;
   121	
   122				oldmss = (opt[i+2] << 8) | opt[i+3];
   123	
   124				/* Never increase MSS, even when setting it, as
   125				 * doing so results in problems for hosts that rely
   126				 * on MSS being set correctly.
   127				 */
   128				if (oldmss <= newmss)
   129					return 0;
   130	
   131				opt[i+2] = (newmss & 0xff00) >> 8;
   132				opt[i+3] = newmss & 0x00ff;
   133	
   134				csum_oldmss = htons(oldmss);
   135				csum_newmss = htons(newmss);
   136	
   137				/* MSS may be unaligned; fix up the incremental checksum
   138				 * to avoid an invalid checksum and a dropped packet.
   139				 */
 > 140				if (((char *)&opt[i + 2] - (char *)tcph) & 0x1 != 0) {

141 csum_oldmss = swab16(csum_oldmss);
142 csum_newmss = swab16(csum_newmss);
143 }
144
145 inet_proto_csum_replace2(&tcph->check, skb,
146 csum_oldmss, csum_newmss,
147 false);
148 return 0;
149 }
150 }
151
152 /* There is data after the header so the option can't be added
153 * without moving it, and doing so may make the SYN packet
154 * itself too large. Accept the packet unmodified instead.
155 */
156 if (len > tcp_hdrlen)
157 return 0;
158
159 /* tcph->doff has 4 bits, do not wrap it to 0 */
160 if (tcp_hdrlen >= 15 * 4)
161 return 0;
162
163 /*
164 * MSS Option not found ?! add it..
165 */
166 if (skb_tailroom(skb) < TCPOLEN_MSS) {
167 if (pskb_expand_head(skb, 0,
168 TCPOLEN_MSS - skb_tailroom(skb),
169 GFP_ATOMIC))
170 return -1;
171 tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
172 }
173
174 skb_put(skb, TCPOLEN_MSS);
175
176 /*
177 * IPv4: RFC 1122 states "If an MSS option is not received at
178 * connection setup, TCP MUST assume a default send MSS of 536".
179 * IPv6: RFC 2460 states IPv6 has a minimum MTU of 1280 and a minimum
180 * length IPv6 header of 60, ergo the default MSS value is 1220
181 * Since no MSS was provided, we must use the default values
182 */
183 if (xt_family(par) == NFPROTO_IPV4)
184 newmss = min(newmss, (u16)536);
185 else
186 newmss = min(newmss, (u16)1220);
187
188 opt = (u_int8_t *)tcph + sizeof(struct tcphdr);
189 memmove(opt + TCPOLEN_MSS, opt, len - sizeof(struct tcphdr));
190
191 inet_proto_csum_replace2(&tcph->check, skb,
192 htons(len), htons(len + TCPOLEN_MSS), true);
193 opt[0] = TCPOPT_MSS;
194 opt[1] = TCPOLEN_MSS;
195 opt[2] = (newmss & 0xff00) >> 8;
196 opt[3] = newmss & 0x00ff;
197
198 inet_proto_csum_replace4(&tcph->check, skb, 0, *((__be32 *)opt), false);
199
200 oldval = ((__be16 *)tcph)[6];
201 tcph->doff += TCPOLEN_MSS/4;
202 inet_proto_csum_replace2(&tcph->check, skb,
203 oldval, ((__be16 *)tcph)[6], false);
204 return TCPOLEN_MSS;
205 }
206

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#10 lore raw
In reply to Kacper Kokot
kernel test robot noticed the following build warnings:

Hi Kacper,

kernel test robot noticed the following build warnings:

[auto build test WARNING on nf-next/main]
[also build test WARNING on netfilter-nf/main linus/master v7.1-rc5 next-20260527] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Kacper-Kokot/netfilter-TCPMSS-fix-dropped-packets-when-MSS-option-is-unaligned/20260526-041308
base:   https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git main
patch link:    https://lore.kernel.org/r/20260525201116.407338-2-kacper.kokot.44%40gmail.com
patch subject: [PATCH] netfilter: TCPMSS: fix dropped packets when MSS option is unaligned
config: s390-allmodconfig (https://download.01.org/0day-ci/archive/20260529/[email protected]/config)
compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260529/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

>> net/netfilter/xt_TCPMSS.c:140:45: warning: & has lower precedence than !=; != will be evaluated first [-Wparentheses]
     140 |                         if (((char *)&opt[i + 2] - (char *)tcph) & 0x1 != 0) {
         |                                                                  ^~~~~~~~~~
   net/netfilter/xt_TCPMSS.c:140:45: note: place parentheses around the '!=' expression to silence this warning
     140 |                         if (((char *)&opt[i + 2] - (char *)tcph) & 0x1 != 0) {
         |                                                                  ^         
         |                                                                    (       )
   net/netfilter/xt_TCPMSS.c:140:45: note: place parentheses around the & expression to evaluate it first
     140 |                         if (((char *)&opt[i + 2] - (char *)tcph) & 0x1 != 0) {
         |                                                                  ^
         |                             (                                         )

1 warning generated.

vim +140 net/netfilter/xt_TCPMSS.c

    69	
    70	static int
    71	tcpmss_mangle_packet(struct sk_buff *skb,
    72			     const struct xt_action_param *par,
    73			     unsigned int family,
    74			     unsigned int tcphoff,
    75			     unsigned int minlen)
    76	{
    77		const struct xt_tcpmss_info *info = par->targinfo;
    78		struct tcphdr *tcph;
    79		int len, tcp_hdrlen;
    80		unsigned int i;
    81		__be16 oldval;
    82		u16 newmss;
    83		u8 *opt;
    84	
    85		/* This is a fragment, no TCP header is available */
    86		if (par->fragoff != 0)
    87			return 0;
    88	
    89		if (skb_ensure_writable(skb, skb->len))
    90			return -1;
    91	
    92		len = skb->len - tcphoff;
    93		if (len < (int)sizeof(struct tcphdr))
    94			return -1;
    95	
    96		tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
    97		tcp_hdrlen = tcph->doff * 4;
    98	
    99		if (len < tcp_hdrlen || tcp_hdrlen < sizeof(struct tcphdr))
   100			return -1;
   101	
   102		if (info->mss == XT_TCPMSS_CLAMP_PMTU) {
   103			struct net *net = xt_net(par);
   104			unsigned int in_mtu = tcpmss_reverse_mtu(net, skb, family);
   105			unsigned int min_mtu = min(dst_mtu(skb_dst(skb)), in_mtu);
   106	
   107			if (min_mtu <= minlen) {
   108				net_err_ratelimited("unknown or invalid path-MTU (%u)\n",
   109						    min_mtu);
   110				return -1;
   111			}
   112			newmss = min_mtu - minlen;
   113		} else
   114			newmss = info->mss;
   115	
   116		opt = (u_int8_t *)tcph;
   117		for (i = sizeof(struct tcphdr); i <= tcp_hdrlen - TCPOLEN_MSS; i += optlen(opt, i)) {
   118			if (opt[i] == TCPOPT_MSS && opt[i+1] == TCPOLEN_MSS) {
   119				u_int16_t oldmss;
   120				u16 csum_oldmss, csum_newmss;
   121	
   122				oldmss = (opt[i+2] << 8) | opt[i+3];
   123	
   124				/* Never increase MSS, even when setting it, as
   125				 * doing so results in problems for hosts that rely
   126				 * on MSS being set correctly.
   127				 */
   128				if (oldmss <= newmss)
   129					return 0;
   130	
   131				opt[i+2] = (newmss & 0xff00) >> 8;
   132				opt[i+3] = newmss & 0x00ff;
   133	
   134				csum_oldmss = htons(oldmss);
   135				csum_newmss = htons(newmss);
   136	
   137				/* MSS may be unaligned; fix up the incremental checksum
   138				 * to avoid an invalid checksum and a dropped packet.
   139				 */
 > 140				if (((char *)&opt[i + 2] - (char *)tcph) & 0x1 != 0) {

141 csum_oldmss = swab16(csum_oldmss);
142 csum_newmss = swab16(csum_newmss);
143 }
144
145 inet_proto_csum_replace2(&tcph->check, skb,
146 csum_oldmss, csum_newmss,
147 false);
148 return 0;
149 }
150 }
151
152 /* There is data after the header so the option can't be added
153 * without moving it, and doing so may make the SYN packet
154 * itself too large. Accept the packet unmodified instead.
155 */
156 if (len > tcp_hdrlen)
157 return 0;
158
159 /* tcph->doff has 4 bits, do not wrap it to 0 */
160 if (tcp_hdrlen >= 15 * 4)
161 return 0;
162
163 /*
164 * MSS Option not found ?! add it..
165 */
166 if (skb_tailroom(skb) < TCPOLEN_MSS) {
167 if (pskb_expand_head(skb, 0,
168 TCPOLEN_MSS - skb_tailroom(skb),
169 GFP_ATOMIC))
170 return -1;
171 tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
172 }
173
174 skb_put(skb, TCPOLEN_MSS);
175
176 /*
177 * IPv4: RFC 1122 states "If an MSS option is not received at
178 * connection setup, TCP MUST assume a default send MSS of 536".
179 * IPv6: RFC 2460 states IPv6 has a minimum MTU of 1280 and a minimum
180 * length IPv6 header of 60, ergo the default MSS value is 1220
181 * Since no MSS was provided, we must use the default values
182 */
183 if (xt_family(par) == NFPROTO_IPV4)
184 newmss = min(newmss, (u16)536);
185 else
186 newmss = min(newmss, (u16)1220);
187
188 opt = (u_int8_t *)tcph + sizeof(struct tcphdr);
189 memmove(opt + TCPOLEN_MSS, opt, len - sizeof(struct tcphdr));
190
191 inet_proto_csum_replace2(&tcph->check, skb,
192 htons(len), htons(len + TCPOLEN_MSS), true);
193 opt[0] = TCPOPT_MSS;
194 opt[1] = TCPOLEN_MSS;
195 opt[2] = (newmss & 0xff00) >> 8;
196 opt[3] = newmss & 0x00ff;
197
198 inet_proto_csum_replace4(&tcph->check, skb, 0, *((__be32 *)opt), false);
199
200 oldval = ((__be16 *)tcph)[6];
201 tcph->doff += TCPOLEN_MSS/4;
202 inet_proto_csum_replace2(&tcph->check, skb,
203 oldval, ((__be16 *)tcph)[6], false);
204 return TCPOLEN_MSS;
205 }
206

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#11 lore raw
Quoted 36 lines · On Fri, 29 May 2026 02:11:48 +0800 kernel test robot <[email protected]> wrote:

Hi Kacper,

kernel test robot noticed the following build warnings:

[auto build test WARNING on nf-next/main]
[also build test WARNING on netfilter-nf/main linus/master v7.1-rc5 next-20260527] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Kacper-Kokot/netfilter-TCPMSS-fix-dropped-packets-when-MSS-option-is-unaligned/20260526-041308
base:   https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git main
patch link:    https://lore.kernel.org/r/20260525201116.407338-2-kacper.kokot.44%40gmail.com
patch subject: [PATCH] netfilter: TCPMSS: fix dropped packets when MSS option is unaligned
config: s390-allmodconfig (https://download.01.org/0day-ci/archive/20260529/[email protected]/config)
compiler: clang version 18.1.8 (https://github.com/llvm/llvm-project 3b5b5c1ec4a3095ab096dd780e84d7ab81f3d7ff)
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260529/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

All warnings (new ones prefixed by >>):

Quoted 1 line
Quoted 1 line

net/netfilter/xt_TCPMSS.c:140:45: warning: & has lower precedence than !=; != will be evaluated first [-Wparentheses]

     140 |                         if (((char *)&opt[i + 2] - (char *)tcph) & 0x1 != 0) {
         |                                                                  ^~~~~~~~~~
   net/netfilter/xt_TCPMSS.c:140:45: note: place parentheses around the '!=' expression to silence this warning
     140 |                         if (((char *)&opt[i + 2] - (char *)tcph) & 0x1 != 0) {
         |                                                                  ^         
         |                                                                    (       )
   net/netfilter/xt_TCPMSS.c:140:45: note: place parentheses around the & expression to evaluate it first
     140 |                         if (((char *)&opt[i + 2] - (char *)tcph) & 0x1 != 0) {
         |                                                                  ^
         |                             (                                         )
Quoted 1 line

1 warning generated.

This has been noted a lot of times.
While 'unusual' the expression is actually correct (but does need changing). Mostly because the '!= 0' isn't needed and '0x1 != 0' is 1.

-- David

Quoted 147 lines

vim +140 net/netfilter/xt_TCPMSS.c

    69	
    70	static int
    71	tcpmss_mangle_packet(struct sk_buff *skb,
    72			     const struct xt_action_param *par,
    73			     unsigned int family,
    74			     unsigned int tcphoff,
    75			     unsigned int minlen)
    76	{
    77		const struct xt_tcpmss_info *info = par->targinfo;
    78		struct tcphdr *tcph;
    79		int len, tcp_hdrlen;
    80		unsigned int i;
    81		__be16 oldval;
    82		u16 newmss;
    83		u8 *opt;
    84	
    85		/* This is a fragment, no TCP header is available */
    86		if (par->fragoff != 0)
    87			return 0;
    88	
    89		if (skb_ensure_writable(skb, skb->len))
    90			return -1;
    91	
    92		len = skb->len - tcphoff;
    93		if (len < (int)sizeof(struct tcphdr))
    94			return -1;
    95	
    96		tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
    97		tcp_hdrlen = tcph->doff * 4;
    98	
    99		if (len < tcp_hdrlen || tcp_hdrlen < sizeof(struct tcphdr))
   100			return -1;
   101	
   102		if (info->mss == XT_TCPMSS_CLAMP_PMTU) {
   103			struct net *net = xt_net(par);
   104			unsigned int in_mtu = tcpmss_reverse_mtu(net, skb, family);
   105			unsigned int min_mtu = min(dst_mtu(skb_dst(skb)), in_mtu);
   106	
   107			if (min_mtu <= minlen) {
   108				net_err_ratelimited("unknown or invalid path-MTU (%u)\n",
   109						    min_mtu);
   110				return -1;
   111			}
   112			newmss = min_mtu - minlen;
   113		} else
   114			newmss = info->mss;
   115	
   116		opt = (u_int8_t *)tcph;
   117		for (i = sizeof(struct tcphdr); i <= tcp_hdrlen - TCPOLEN_MSS; i += optlen(opt, i)) {
   118			if (opt[i] == TCPOPT_MSS && opt[i+1] == TCPOLEN_MSS) {
   119				u_int16_t oldmss;
   120				u16 csum_oldmss, csum_newmss;
   121	
   122				oldmss = (opt[i+2] << 8) | opt[i+3];
   123	
   124				/* Never increase MSS, even when setting it, as
   125				 * doing so results in problems for hosts that rely
   126				 * on MSS being set correctly.
   127				 */
   128				if (oldmss <= newmss)
   129					return 0;
   130	
   131				opt[i+2] = (newmss & 0xff00) >> 8;
   132				opt[i+3] = newmss & 0x00ff;
   133	
   134				csum_oldmss = htons(oldmss);
   135				csum_newmss = htons(newmss);
   136	
   137				/* MSS may be unaligned; fix up the incremental checksum
   138				 * to avoid an invalid checksum and a dropped packet.
   139				 */
Quoted 1 line

140 if (((char *)&opt[i + 2] - (char *)tcph) & 0x1 != 0) {

   141					csum_oldmss = swab16(csum_oldmss);
   142					csum_newmss = swab16(csum_newmss);
   143				}
   144	
   145				inet_proto_csum_replace2(&tcph->check, skb,
   146							 csum_oldmss, csum_newmss,
   147							 false);
   148				return 0;
   149			}
   150		}
   151	
   152		/* There is data after the header so the option can't be added
   153		 * without moving it, and doing so may make the SYN packet
   154		 * itself too large. Accept the packet unmodified instead.
   155		 */
   156		if (len > tcp_hdrlen)
   157			return 0;
   158	
   159		/* tcph->doff has 4 bits, do not wrap it to 0 */
   160		if (tcp_hdrlen >= 15 * 4)
   161			return 0;
   162	
   163		/*
   164		 * MSS Option not found ?! add it..
   165		 */
   166		if (skb_tailroom(skb) < TCPOLEN_MSS) {
   167			if (pskb_expand_head(skb, 0,
   168					     TCPOLEN_MSS - skb_tailroom(skb),
   169					     GFP_ATOMIC))
   170				return -1;
   171			tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
   172		}
   173	
   174		skb_put(skb, TCPOLEN_MSS);
   175	
   176		/*
   177		 * IPv4: RFC 1122 states "If an MSS option is not received at
   178		 * connection setup, TCP MUST assume a default send MSS of 536".
   179		 * IPv6: RFC 2460 states IPv6 has a minimum MTU of 1280 and a minimum
   180		 * length IPv6 header of 60, ergo the default MSS value is 1220
   181		 * Since no MSS was provided, we must use the default values
   182		 */
   183		if (xt_family(par) == NFPROTO_IPV4)
   184			newmss = min(newmss, (u16)536);
   185		else
   186			newmss = min(newmss, (u16)1220);
   187	
   188		opt = (u_int8_t *)tcph + sizeof(struct tcphdr);
   189		memmove(opt + TCPOLEN_MSS, opt, len - sizeof(struct tcphdr));
   190	
   191		inet_proto_csum_replace2(&tcph->check, skb,
   192					 htons(len), htons(len + TCPOLEN_MSS), true);
   193		opt[0] = TCPOPT_MSS;
   194		opt[1] = TCPOLEN_MSS;
   195		opt[2] = (newmss & 0xff00) >> 8;
   196		opt[3] = newmss & 0x00ff;
   197	
   198		inet_proto_csum_replace4(&tcph->check, skb, 0, *((__be32 *)opt), false);
   199	
   200		oldval = ((__be16 *)tcph)[6];
   201		tcph->doff += TCPOLEN_MSS/4;
   202		inet_proto_csum_replace2(&tcph->check, skb,
   203					 oldval, ((__be16 *)tcph)[6], false);
   204		return TCPOLEN_MSS;
   205	}
   206	

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki

#12 lore raw

PATCH v2 1/1 netfilter: TCPMSS: fix dropped packets when MSS option is unaligned

Padding TCP options with NOPs is optional, so it is legal to send an MSS option that is not aligned to a word boundary and therefore not aligned for checksum calculation. The current TCPMSS target is not robust to this: when the MSS option is unaligned it produces an invalid checksum, and the packet is dropped.

This has not been observed in any real environment. Senders place the MSS at the beginning of the options block, where it is naturally aligned, but the spec allows unaligned options and the kernel shouldn't silently drop legal packets.

When the changed word is not aligned, the modified bytes straddle two checksum words, and using the standard incremental update helper (which assumes alignment) produces an invalid checksum:

    | w1     | w2     |
OLD |  a  b  |  c  d  |
NEW |  a  b' |  c' d  |

Since b' and c' sit across w1 and w2, we could compute the incremental checksum in two operations by recalculating w1 and then w2:

    C' = C - w1 + w1' - w2 + w2'

But working it out:

    C' = C - w1 - w2 + w1' + w2'
       = C - (a * 2^8 + b)  - (c * 2^8 + d)
           + (a * 2^8 + b') + (c' * 2^8 + d)
       = C + 2^8 * (a - a + c' - c) + (b' - b + d - d)
       = C + 2^8 * (c' - c) + (b' - b)
       = C - (2^8 * c + b) + (2^8 * c' + b')

So an unaligned incremental checksum can be done in a single operation by byteswapping the changed bytes before passing them to the helper. This patch implements that trick for unaligned MSS option updates.

---
I decided to go with the get_unaligned_be16 suggestion because it's idiomatic and it produces shorter assembly on x86-64 (6 instructions vs 9). SYN processing is a cold path so I didn't look into it further.

v2:
- Use get_unaligned_be16 (Fernando's suggestion)
- Fix alignment check expression (David)
- Mention it's a theoretical bug in the commit message
- Drop cc stable, the bug is only theoretical

 net/netfilter/xt_TCPMSS.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
net/netfilter/xt_TCPMSS.c +0 -0
index 80e1634bc51f..32c87a520361 100644

@ -117,8 +117,9 @@ tcpmss_mangle_packet(struct sk_buff *skb,
for (i = sizeof(struct tcphdr); i <= tcp_hdrlen - TCPOLEN_MSS; i += optlen(opt, i)) {
if (opt[i] == TCPOPT_MSS && opt[i+1] == TCPOLEN_MSS) {
u_int16_t oldmss;
+ u16 csum_oldmss, csum_newmss;

- oldmss = (opt[i+2] << 8) | opt[i+3];
+ oldmss = get_unaligned_be16(&opt[i+2]);

/* Never increase MSS, even when setting it, as
* doing so results in problems for hosts that rely
@@ -130,8 +131,19 @@ tcpmss_mangle_packet(struct sk_buff *skb,
opt[i+2] = (newmss & 0xff00) >> 8; opt[i+3] = newmss & 0x00ff;

+ csum_oldmss = htons(oldmss);
+ csum_newmss = htons(newmss);
+
+ /* MSS may be unaligned; fix up the incremental checksum
+ * to avoid an invalid checksum and a dropped packet.
+ */
+ if (((char *)&opt[i + 2] - (char *)tcph) & 0x1) {
+ csum_oldmss = swab16(csum_oldmss);
+ csum_newmss = swab16(csum_newmss);
+ }
+
inet_proto_csum_replace2(&tcph->check, skb,
- htons(oldmss), htons(newmss),
+ csum_oldmss, csum_newmss,
false);
return 0;
}

Signature · 1 line
2.43.0
#13 lore raw

Hi,

Quoted 5 lines · Kacper Kokot

Padding TCP options with NOPs is optional, so it is legal to send an MSS option that is not aligned to a word boundary and therefore not aligned for checksum calculation. The current TCPMSS target is not robust to this: when the MSS option is unaligned it produces an invalid checksum, and the packet is dropped.

Yes, but how many stacks do this?

Quoted 1 line

This has not been observed in any real environment.

... then why is this a fix?

Quoted 3 lines

Senders place the MSS at the beginning of the options block, where it is naturally aligned, but the spec allows unaligned options and the kernel shouldn't silently drop legal packets.

This is questionably a "clean packet".

And "the kernel is not silently dropping anything, it is policy that would drop it", and there are mechanisms to track what rule is dropping this packet.

Quoted 22 lines

When the changed word is not aligned, the modified bytes straddle two checksum words, and using the standard incremental update helper (which assumes alignment) produces an invalid checksum:

    | w1     | w2     |
OLD |  a  b  |  c  d  |
NEW |  a  b' |  c' d  |

Since b' and c' sit across w1 and w2, we could compute the incremental checksum in two operations by recalculating w1 and then w2:

    C' = C - w1 + w1' - w2 + w2'

But working it out:

    C' = C - w1 - w2 + w1' + w2'
       = C - (a * 2^8 + b)  - (c * 2^8 + d)
           + (a * 2^8 + b') + (c' * 2^8 + d)
       = C + 2^8 * (a - a + c' - c) + (b' - b + d - d)
       = C + 2^8 * (c' - c) + (b' - b)
       = C - (2^8 * c + b) + (2^8 * c' + b')
Quoted 3 lines

So an unaligned incremental checksum can be done in a single operation by byteswapping the changed bytes before passing them to the helper. This patch implements that trick for unaligned MSS option updates.

If this is a fix as the title specify, there no Fixes: tag?

To me, this qualifies as an enhancement, if anything.

Maybe more correct subject is:

"netfilter: TCPMSS: handle packets with unaligned MSS option"

since this mangling unaligned MSS has never been supported this far.

Thanks

Quoted 55 lines

---
I decided to go with the get_unaligned_be16 suggestion because it's idiomatic and it produces shorter assembly on x86-64 (6 instructions vs 9). SYN processing is a cold path so I didn't look into it further.

v2:
- Use get_unaligned_be16 (Fernando's suggestion)
- Fix alignment check expression (David)
- Mention it's a theoretical bug in the commit message
- Drop cc stable, the bug is only theoretical

 net/netfilter/xt_TCPMSS.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)
net/netfilter/xt_TCPMSS.c +0 -0
index 80e1634bc51f..32c87a520361 100644

@ -117,8 +117,9 @@ tcpmss_mangle_packet(struct sk_buff *skb,

net/netfilter/xt_TCPMSS.c +14 -2
 	for (i = sizeof(struct tcphdr); i <= tcp_hdrlen - TCPOLEN_MSS; i += optlen(opt, i)) {
 		if (opt[i] == TCPOPT_MSS && opt[i+1] == TCPOLEN_MSS) {
 			u_int16_t oldmss;
+			u16 csum_oldmss, csum_newmss;
 
-			oldmss = (opt[i+2] << 8) | opt[i+3];
+			oldmss = get_unaligned_be16(&opt[i+2]);
 
 			/* Never increase MSS, even when setting it, as
 			 * doing so results in problems for hosts that rely
@@ -130,8 +131,19 @@ tcpmss_mangle_packet(struct sk_buff *skb,
 			opt[i+2] = (newmss & 0xff00) >> 8;
 			opt[i+3] = newmss & 0x00ff;
 
+			csum_oldmss = htons(oldmss);
+			csum_newmss = htons(newmss);
+
+			/* MSS may be unaligned; fix up the incremental checksum
+			 * to avoid an invalid checksum and a dropped packet.
+			 */
+			if (((char *)&opt[i + 2] - (char *)tcph) & 0x1) {
+				csum_oldmss = swab16(csum_oldmss);
+				csum_newmss = swab16(csum_newmss);
+			}
+
 			inet_proto_csum_replace2(&tcph->check, skb,
-						 htons(oldmss), htons(newmss),
+						 csum_oldmss, csum_newmss,
 						 false);
 			return 0;
 		}
Signature · 1 line
2.43.0
#14 lore raw
Quoted 4 lines
Quoted 2 lines

Padding TCP options with NOPs is optional, so it is legal to send an MSS option that is not aligned to a word boundary [...]

Yes, but how many stacks do this?

None that I'm aware of. Mainstream stacks pad everything to a word boundary and put MSS as the first option. The motivation is RFC 9293 (MUST-64) spec conformance rather than a bug seen in the wild.

Quoted 5 lines
Quoted 1 line

This has not been observed in any real environment.

... then why is this a fix?
[...]
To me, this qualifies as an enhancement, if anything.

I'll drop the "fix" and reframe this as an enhancement including your suggested subject line.

Quoted 1 line

This is questionably a "clean packet".

Fair point, I shouldn't have framed it as legitimate/clean traffic being dropped.

Quoted 2 lines

And "the kernel is not silently dropping anything, it is policy that would drop it" [...]

I'll reword the commit message to say the mangled packet ends up with an invalid checksum and could then be dropped by policy, rather than implying the kernel itself drops it.

Thanks for the review.

#15 lore raw
In reply to Kacper Kokot
Quoted 33 lines · Kacper Kokot

Padding TCP options with NOPs is optional, so it is legal to send an MSS option that is not aligned to a word boundary and therefore not aligned for checksum calculation. The current TCPMSS target is not robust to this: when the MSS option is unaligned it produces an invalid checksum, and the packet is dropped.

This has not been observed in any real environment. Senders place the MSS at the beginning of the options block, where it is naturally aligned, but the spec allows unaligned options and the kernel shouldn't silently drop legal packets.

When the changed word is not aligned, the modified bytes straddle two checksum words, and using the standard incremental update helper (which assumes alignment) produces an invalid checksum:

     | w1     | w2     |
OLD |  a  b  |  c  d  |
NEW |  a  b' |  c' d  |

Since b' and c' sit across w1 and w2, we could compute the incremental checksum in two operations by recalculating w1 and then w2:

     C' = C - w1 + w1' - w2 + w2'

But working it out:

     C' = C - w1 - w2 + w1' + w2'
        = C - (a * 2^8 + b)  - (c * 2^8 + d)
            + (a * 2^8 + b') + (c' * 2^8 + d)
        = C + 2^8 * (a - a + c' - c) + (b' - b + d - d)
        = C + 2^8 * (c' - c) + (b' - b)
        = C - (2^8 * c + b) + (2^8 * c' + b')
Quoted 6 lines

So an unaligned incremental checksum can be done in a single operation by byteswapping the changed bytes before passing them to the helper. This patch implements that trick for unaligned MSS option updates.

---

Just a couple of nits..

Please use nf-next target, like "[PATCH nf-next v3] netfilter: xt_TCPMSS: ...". Let's handle this as an enhancement.

Quoted 22 lines

I decided to go with the get_unaligned_be16 suggestion because it's idiomatic and it produces shorter assembly on x86-64 (6 instructions vs 9). SYN processing is a cold path so I didn't look into it further.

v2:
- Use get_unaligned_be16 (Fernando's suggestion)
- Fix alignment check expression (David)
- Mention it's a theoretical bug in the commit message
- Drop cc stable, the bug is only theoretical

  net/netfilter/xt_TCPMSS.c | 16 ++++++++++++++--
  1 file changed, 14 insertions(+), 2 deletions(-)
net/netfilter/xt_TCPMSS.c +0 -0
index 80e1634bc51f..32c87a520361 100644

@ -117,8 +117,9 @@ tcpmss_mangle_packet(struct sk_buff *skb,
for (i = sizeof(struct tcphdr); i <= tcp_hdrlen - TCPOLEN_MSS; i += optlen(opt, i)) {
if (opt[i] == TCPOPT_MSS && opt[i+1] == TCPOLEN_MSS) {
u_int16_t oldmss;

net/netfilter/xt_TCPMSS.c

Quoted 1 line
net/netfilter/xt_TCPMSS.c +1 -0
+			u16 csum_oldmss, csum_newmss;

Please use reversed xmas tree:

+ u16 csum_oldmss, csum_newmss;
u_int16_t oldmss;

Thanks,
Fernando.

#16 lore raw

Will target nf-next and switch the variable declaration order in v3.

Thanks for the review.

#17 lore raw

PATCH v3 1/1 netfilter: TCPMSS: handle packets with unaligned MSS option

In reply to Kacper Kokot

RFC 9293 permits TCP options to begin on any octet boundary. Padding to a word boundary with NOPs is a sender convention, not a requirement, and robust receivers must handle unaligned options (MUST-64).

The xt_TCPMSS target's incremental checksum update assumes the MSS option is word-aligned. When it's not, the modified bytes straddle two checksum words and the resulting checksum is incorrect. The mangled packet may then fail checksum validation and be dropped downstream. That said, all mainstream stacks emit a word-aligned MSS, this change is motivated by spec conformance rather than a bug observed in the wild.

Extend the checksum update to handle unaligned MSS options. When the changed word is unaligned, the modified bytes b' and c' straddle two checksum words w1 and w2:

    | w1     | w2     |
OLD |  a  b  |  c  d  |
NEW |  a  b' |  c' d  |

The two-step update C' = C - w1 + w1' - w2 + w2' reduces algebraically to a single word incremental checksum update with byteswapped operands:

    C' = C - w1 - w2 + w1' + w2'
       = C - (a * 2^8 + b)  - (c * 2^8 + d)
           + (a * 2^8 + b') + (c' * 2^8 + d)
       = C + 2^8 * (a - a + c' - c) + (b' - b + d - d)
       = C + 2^8 * (c' - c) + (b' - b)
       = C - (2^8 * c + b) + (2^8 * c' + b')

So the unaligned case adds no extra checksum operations.

---
v3:
- Reframe as enhancement, not a fix (Pablo/Fernando)
- Rename subject to xt_TCPMSS, drop "fix" wording
- Reword commit message: packet may fail checksum validation and be dropped downstream (Pablo)
- Target nf-next (Fernando)
- Use __be16 for csum_oldmss/csum_newmss (sparse warning from kernel test robot)
- Reorder local variable declarations to reverse xmas tree (Fernando)

v2:
- Use get_unaligned_be16 (Fernando's suggestion)
- Fix alignment check expression (David)
- Mention it's a theoretical bug in the commit message
- Drop cc stable, the bug is only theoretical

net/netfilter/xt_TCPMSS.c +20 -2
index 80e1634bc51f..037add799d41 100644
@@ -116,9 +116,10 @@ tcpmss_mangle_packet(struct sk_buff *skb,
 	opt = (u_int8_t *)tcph;
 	for (i = sizeof(struct tcphdr); i <= tcp_hdrlen - TCPOLEN_MSS; i += optlen(opt, i)) {
 		if (opt[i] == TCPOPT_MSS && opt[i+1] == TCPOLEN_MSS) {
+			__be16 csum_oldmss, csum_newmss;
 			u_int16_t oldmss;
 
-			oldmss = (opt[i+2] << 8) | opt[i+3];
+			oldmss = get_unaligned_be16(&opt[i + 2]);
 
 			/* Never increase MSS, even when setting it, as
 			 * doing so results in problems for hosts that rely
@@ -130,8 +131,25 @@ tcpmss_mangle_packet(struct sk_buff *skb,
 			opt[i+2] = (newmss & 0xff00) >> 8;
 			opt[i+3] = newmss & 0x00ff;
 
+			csum_oldmss = htons(oldmss);
+			csum_newmss = htons(newmss);
+
+			if (((char *)&opt[i + 2] - (char *)tcph) & 0x1) {
+				/* MSS option is unaligned: the modified bytes
+				 * straddle two checksum words. Byteswapping
+				 * the operands lets a single incremental
+				 * update produce the correct checksum delta
+				 * (see commit message for the derivation).
+				 */
+				csum_oldmss = htons(swab16(oldmss));
+				csum_newmss = htons(swab16(newmss));
+			} else {
+				csum_oldmss = htons(oldmss);
+				csum_newmss = htons(newmss);
+			}
+
 			inet_proto_csum_replace2(&tcph->check, skb,
-						 htons(oldmss), htons(newmss),
+						 csum_oldmss, csum_newmss,
 						 false);
 			return 0;
 		}
Signature · 1 line
2.43.0
#18 lore raw

Re: [PATCH nf-next v3] netfilter: TCPMSS: handle packets with unaligned MSS option

Quoted 75 lines · Kacper Kokot

RFC 9293 permits TCP options to begin on any octet boundary. Padding to a word boundary with NOPs is a sender convention, not a requirement, and robust receivers must handle unaligned options (MUST-64).

The xt_TCPMSS target's incremental checksum update assumes the MSS option is word-aligned. When it's not, the modified bytes straddle two checksum words and the resulting checksum is incorrect. The mangled packet may then fail checksum validation and be dropped downstream. That said, all mainstream stacks emit a word-aligned MSS, this change is motivated by spec conformance rather than a bug observed in the wild.

Extend the checksum update to handle unaligned MSS options. When the changed word is unaligned, the modified bytes b' and c' straddle two checksum words w1 and w2:

    | w1     | w2     |
OLD |  a  b  |  c  d  |
NEW |  a  b' |  c' d  |

The two-step update C' = C - w1 + w1' - w2 + w2' reduces algebraically to a single word incremental checksum update with byteswapped operands:

    C' = C - w1 - w2 + w1' + w2'
       = C - (a * 2^8 + b)  - (c * 2^8 + d)
           + (a * 2^8 + b') + (c' * 2^8 + d)
       = C + 2^8 * (a - a + c' - c) + (b' - b + d - d)
       = C + 2^8 * (c' - c) + (b' - b)
       = C - (2^8 * c + b) + (2^8 * c' + b')

So the unaligned case adds no extra checksum operations.

---
v3:
- Reframe as enhancement, not a fix (Pablo/Fernando)
- Rename subject to xt_TCPMSS, drop "fix" wording
- Reword commit message: packet may fail checksum validation and be dropped downstream (Pablo)
- Target nf-next (Fernando)
- Use __be16 for csum_oldmss/csum_newmss (sparse warning from kernel test robot)
- Reorder local variable declarations to reverse xmas tree (Fernando)

v2:
- Use get_unaligned_be16 (Fernando's suggestion)
- Fix alignment check expression (David)
- Mention it's a theoretical bug in the commit message
- Drop cc stable, the bug is only theoretical

net/netfilter/xt_TCPMSS.c +8 -1
index 80e1634bc51f..037add799d41 100644
@@ -116,9 +116,10 @@ tcpmss_mangle_packet(struct sk_buff *skb,
 	opt = (u_int8_t *)tcph;
 	for (i = sizeof(struct tcphdr); i <= tcp_hdrlen - TCPOLEN_MSS; i += optlen(opt, i)) {
 		if (opt[i] == TCPOPT_MSS && opt[i+1] == TCPOLEN_MSS) {
+			__be16 csum_oldmss, csum_newmss;
 			u_int16_t oldmss;
 
-			oldmss = (opt[i+2] << 8) | opt[i+3];
+			oldmss = get_unaligned_be16(&opt[i + 2]);
 
 			/* Never increase MSS, even when setting it, as
 			 * doing so results in problems for hosts that rely
@@ -130,8 +131,25 @@ tcpmss_mangle_packet(struct sk_buff *skb,
 			opt[i+2] = (newmss & 0xff00) >> 8;
 			opt[i+3] = newmss & 0x00ff;
 
+			csum_oldmss = htons(oldmss);
+			csum_newmss = htons(newmss);
+
+			if (((char *)&opt[i + 2] - (char *)tcph) & 0x1) {
+				/* MSS option is unaligned: the modified bytes
+				 * straddle two checksum words. Byteswapping

net/netfilter/xt_TCPMSS.c tcpmss_mangle_packet(struct sk_buff *skb, · line 149

Quoted 10 lines
net/netfilter/xt_TCPMSS.c +10 -0
+				 * the operands lets a single incremental
+				 * update produce the correct checksum delta
+				 * (see commit message for the derivation).
+				 */
+				csum_oldmss = htons(swab16(oldmss));
+				csum_newmss = htons(swab16(newmss));
+			} else {
+				csum_oldmss = htons(oldmss);
+				csum_newmss = htons(newmss);
+			}

After seeing this unaligned in other areas in the Netfilter tree, I am not sure it is worth to add workarounds everywhere in this codebase to deal with updates that span two 16-bits words for such a hypothetical case like this.

By now, patches that call get_unaligned_be16() for correctness are OK IMO. This is to deal with arches which cannot cope with unaligned access. This will corrupt such rare packet but that it addresses the unaligned splats.

If we start seeing real stacks which provide real unaligned access like this, maybe by then we can revisit.

So I am leaning towards a small patches to introduce get_unaligned_be16() and document that this corrupts packets with such a rare unaligned TCP option.

IIRC, x86_64 has a inet checksum function that can deal with 1-byte words, although other arches cannot do that and still need to operation with 16-bit words. Given Linux is multi-arch, this all need to stick to the 16-bit word arithmetics when mangling packets

Maybe in the future all checksum functions in every arch are updated too to deal with 1-byte word updates, and maybe real stacks pop up with such a rare packets. But by then these ugly workaround won't be needed at all.

Quoted 11 lines
net/netfilter/xt_TCPMSS.c +2 -1
+
 			inet_proto_csum_replace2(&tcph->check, skb,
-						 htons(oldmss), htons(newmss),
+						 csum_oldmss, csum_newmss,
 						 false);
 			return 0;
 		}
Signature · 1 line
2.43.0
#19 lore raw
In reply to Kacper Kokot
kernel test robot noticed the following build warnings:

Hi Kacper,

kernel test robot noticed the following build warnings:

[auto build test WARNING on nf-next/main]
[also build test WARNING on netfilter-nf/main horms-ipvs/master linus/master v7.1-rc5 next-20260528] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]

url:    https://github.com/intel-lab-lkp/linux/commits/Kacper-Kokot/netfilter-TCPMSS-fix-dropped-packets-when-MSS-option-is-unaligned/20260526-041308
base:   https://git.kernel.org/pub/scm/linux/kernel/git/netfilter/nf-next.git main
patch link:    https://lore.kernel.org/r/20260525201116.407338-2-kacper.kokot.44%40gmail.com
patch subject: [PATCH] netfilter: TCPMSS: fix dropped packets when MSS option is unaligned
config: arc-randconfig-r133-20260529 (https://download.01.org/0day-ci/archive/20260530/[email protected]/config)
compiler: arc-linux-gcc (GCC) 10.5.0
sparse: v0.6.5-rc1
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260530/[email protected]/reproduce)

If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/

sparse warnings: (new ones prefixed by >>)

>> net/netfilter/xt_TCPMSS.c:134:37: sparse: sparse: incorrect type in assignment (different base types) @@     expected unsigned short [usertype] csum_oldmss @@     got restricted __be16 [usertype] @@
   net/netfilter/xt_TCPMSS.c:134:37: sparse:     expected unsigned short [usertype] csum_oldmss
   net/netfilter/xt_TCPMSS.c:134:37: sparse:     got restricted __be16 [usertype]
>> net/netfilter/xt_TCPMSS.c:135:37: sparse: sparse: incorrect type in assignment (different base types) @@     expected unsigned short [usertype] csum_newmss @@     got restricted __be16 [usertype] @@
   net/netfilter/xt_TCPMSS.c:135:37: sparse:     expected unsigned short [usertype] csum_newmss
   net/netfilter/xt_TCPMSS.c:135:37: sparse:     got restricted __be16 [usertype]
>> net/netfilter/xt_TCPMSS.c:146:50: sparse: sparse: incorrect type in argument 3 (different base types) @@     expected restricted __be16 [usertype] from @@     got unsigned short [assigned] [usertype] csum_oldmss @@
   net/netfilter/xt_TCPMSS.c:146:50: sparse:     expected restricted __be16 [usertype] from
   net/netfilter/xt_TCPMSS.c:146:50: sparse:     got unsigned short [assigned] [usertype] csum_oldmss
>> net/netfilter/xt_TCPMSS.c:146:63: sparse: sparse: incorrect type in argument 4 (different base types) @@     expected restricted __be16 [usertype] to @@     got unsigned short [assigned] [usertype] csum_newmss @@

net/netfilter/xt_TCPMSS.c:146:63: sparse: expected restricted __be16 [usertype] to
net/netfilter/xt_TCPMSS.c:146:63: sparse: got unsigned short [assigned] [usertype] csum_newmss

vim +134 net/netfilter/xt_TCPMSS.c

    69	
    70	static int
    71	tcpmss_mangle_packet(struct sk_buff *skb,
    72			     const struct xt_action_param *par,
    73			     unsigned int family,
    74			     unsigned int tcphoff,
    75			     unsigned int minlen)
    76	{
    77		const struct xt_tcpmss_info *info = par->targinfo;
    78		struct tcphdr *tcph;
    79		int len, tcp_hdrlen;
    80		unsigned int i;
    81		__be16 oldval;
    82		u16 newmss;
    83		u8 *opt;
    84	
    85		/* This is a fragment, no TCP header is available */
    86		if (par->fragoff != 0)
    87			return 0;
    88	
    89		if (skb_ensure_writable(skb, skb->len))
    90			return -1;
    91	
    92		len = skb->len - tcphoff;
    93		if (len < (int)sizeof(struct tcphdr))
    94			return -1;
    95	
    96		tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
    97		tcp_hdrlen = tcph->doff * 4;
    98	
    99		if (len < tcp_hdrlen || tcp_hdrlen < sizeof(struct tcphdr))
   100			return -1;
   101	
   102		if (info->mss == XT_TCPMSS_CLAMP_PMTU) {
   103			struct net *net = xt_net(par);
   104			unsigned int in_mtu = tcpmss_reverse_mtu(net, skb, family);
   105			unsigned int min_mtu = min(dst_mtu(skb_dst(skb)), in_mtu);
   106	
   107			if (min_mtu <= minlen) {
   108				net_err_ratelimited("unknown or invalid path-MTU (%u)\n",
   109						    min_mtu);
   110				return -1;
   111			}
   112			newmss = min_mtu - minlen;
   113		} else
   114			newmss = info->mss;
   115	
   116		opt = (u_int8_t *)tcph;
   117		for (i = sizeof(struct tcphdr); i <= tcp_hdrlen - TCPOLEN_MSS; i += optlen(opt, i)) {
   118			if (opt[i] == TCPOPT_MSS && opt[i+1] == TCPOLEN_MSS) {
   119				u_int16_t oldmss;
   120				u16 csum_oldmss, csum_newmss;
   121	
   122				oldmss = (opt[i+2] << 8) | opt[i+3];
   123	
   124				/* Never increase MSS, even when setting it, as
   125				 * doing so results in problems for hosts that rely
   126				 * on MSS being set correctly.
   127				 */
   128				if (oldmss <= newmss)
   129					return 0;
   130	
   131				opt[i+2] = (newmss & 0xff00) >> 8;
   132				opt[i+3] = newmss & 0x00ff;
   133	
 > 134				csum_oldmss = htons(oldmss);
 > 135				csum_newmss = htons(newmss);
   136	
   137				/* MSS may be unaligned; fix up the incremental checksum
   138				 * to avoid an invalid checksum and a dropped packet.
   139				 */
   140				if (((char *)&opt[i + 2] - (char *)tcph) & 0x1 != 0) {
   141					csum_oldmss = swab16(csum_oldmss);
   142					csum_newmss = swab16(csum_newmss);
   143				}
   144	
   145				inet_proto_csum_replace2(&tcph->check, skb,
 > 146							 csum_oldmss, csum_newmss,

147 false);
148 return 0;
149 }
150 }
151
152 /* There is data after the header so the option can't be added
153 * without moving it, and doing so may make the SYN packet
154 * itself too large. Accept the packet unmodified instead.
155 */
156 if (len > tcp_hdrlen)
157 return 0;
158
159 /* tcph->doff has 4 bits, do not wrap it to 0 */
160 if (tcp_hdrlen >= 15 * 4)
161 return 0;
162
163 /*
164 * MSS Option not found ?! add it..
165 */
166 if (skb_tailroom(skb) < TCPOLEN_MSS) {
167 if (pskb_expand_head(skb, 0,
168 TCPOLEN_MSS - skb_tailroom(skb),
169 GFP_ATOMIC))
170 return -1;
171 tcph = (struct tcphdr *)(skb_network_header(skb) + tcphoff);
172 }
173
174 skb_put(skb, TCPOLEN_MSS);
175
176 /*
177 * IPv4: RFC 1122 states "If an MSS option is not received at
178 * connection setup, TCP MUST assume a default send MSS of 536".
179 * IPv6: RFC 2460 states IPv6 has a minimum MTU of 1280 and a minimum
180 * length IPv6 header of 60, ergo the default MSS value is 1220
181 * Since no MSS was provided, we must use the default values
182 */
183 if (xt_family(par) == NFPROTO_IPV4)
184 newmss = min(newmss, (u16)536);
185 else
186 newmss = min(newmss, (u16)1220);
187
188 opt = (u_int8_t *)tcph + sizeof(struct tcphdr);
189 memmove(opt + TCPOLEN_MSS, opt, len - sizeof(struct tcphdr));
190
191 inet_proto_csum_replace2(&tcph->check, skb,
192 htons(len), htons(len + TCPOLEN_MSS), true);
193 opt[0] = TCPOPT_MSS;
194 opt[1] = TCPOLEN_MSS;
195 opt[2] = (newmss & 0xff00) >> 8;
196 opt[3] = newmss & 0x00ff;
197
198 inet_proto_csum_replace4(&tcph->check, skb, 0, *((__be32 *)opt), false);
199
200 oldval = ((__be16 *)tcph)[6];
201 tcph->doff += TCPOLEN_MSS/4;
202 inet_proto_csum_replace2(&tcph->check, skb,
203 oldval, ((__be16 *)tcph)[6], false);
204 return TCPOLEN_MSS;
205 }
206

--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki