{"id":2229685,"url":"http://patchwork.ozlabs.org/api/1.1/patches/2229685/?format=json","web_url":"http://patchwork.ozlabs.org/project/linux-cifs-client/patch/20260428155542.226234-3-henrique.carvalho@suse.com/","project":{"id":12,"url":"http://patchwork.ozlabs.org/api/1.1/projects/12/?format=json","name":"Linux CIFS Client","link_name":"linux-cifs-client","list_id":"linux-cifs.vger.kernel.org","list_email":"linux-cifs@vger.kernel.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20260428155542.226234-3-henrique.carvalho@suse.com>","date":"2026-04-28T15:55:40","name":"[v2,02/11] smb: client: refactor negotiate context assembly","commit_ref":null,"pull_url":null,"state":"new","archived":false,"hash":"ae326e05389f737f5f7d1c5ff624121357bca9ba","submitter":{"id":89563,"url":"http://patchwork.ozlabs.org/api/1.1/people/89563/?format=json","name":"Henrique Carvalho","email":"henrique.carvalho@suse.com"},"delegate":null,"mbox":"http://patchwork.ozlabs.org/project/linux-cifs-client/patch/20260428155542.226234-3-henrique.carvalho@suse.com/mbox/","series":[{"id":501886,"url":"http://patchwork.ozlabs.org/api/1.1/series/501886/?format=json","web_url":"http://patchwork.ozlabs.org/project/linux-cifs-client/list/?series=501886","date":"2026-04-28T15:55:41","name":"smb: implement SMB over QUIC","version":2,"mbox":"http://patchwork.ozlabs.org/series/501886/mbox/"}],"comments":"http://patchwork.ozlabs.org/api/patches/2229685/comments/","check":"pending","checks":"http://patchwork.ozlabs.org/api/patches/2229685/checks/","tags":{},"headers":{"Return-Path":"\n <linux-cifs+bounces-11228-incoming=patchwork.ozlabs.org@vger.kernel.org>","X-Original-To":["incoming@patchwork.ozlabs.org","linux-cifs@vger.kernel.org"],"Delivered-To":"patchwork-incoming@legolas.ozlabs.org","Authentication-Results":["legolas.ozlabs.org;\n\tdkim=pass (2048-bit key;\n unprotected) header.d=suse.com header.i=@suse.com header.a=rsa-sha256\n header.s=google header.b=coQOvt62;\n\tdkim-atps=neutral","legolas.ozlabs.org;\n spf=pass (sender SPF authorized) smtp.mailfrom=vger.kernel.org\n (client-ip=2600:3c0a:e001:db::12fc:5321; helo=sea.lore.kernel.org;\n envelope-from=linux-cifs+bounces-11228-incoming=patchwork.ozlabs.org@vger.kernel.org;\n receiver=patchwork.ozlabs.org)","smtp.subspace.kernel.org;\n\tdkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com\n header.b=\"coQOvt62\"","smtp.subspace.kernel.org;\n arc=none smtp.client-ip=209.85.128.53","smtp.subspace.kernel.org;\n dmarc=pass (p=quarantine dis=none) header.from=suse.com","smtp.subspace.kernel.org;\n spf=pass smtp.mailfrom=suse.com"],"Received":["from sea.lore.kernel.org (sea.lore.kernel.org\n [IPv6:2600:3c0a:e001:db::12fc:5321])\n\t(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)\n\t key-exchange x25519)\n\t(No client certificate requested)\n\tby legolas.ozlabs.org (Postfix) with ESMTPS id 4g4lRs5Jvxz1yHv\n\tfor <incoming@patchwork.ozlabs.org>; Wed, 29 Apr 2026 01:56:57 +1000 (AEST)","from smtp.subspace.kernel.org (conduit.subspace.kernel.org\n [100.90.174.1])\n\tby sea.lore.kernel.org (Postfix) with ESMTP id 85F4F306705B\n\tfor <incoming@patchwork.ozlabs.org>; Tue, 28 Apr 2026 15:56:05 +0000 (UTC)","from localhost.localdomain (localhost.localdomain [127.0.0.1])\n\tby smtp.subspace.kernel.org (Postfix) with ESMTP id D99E73B38A6;\n\tTue, 28 Apr 2026 15:56:03 +0000 (UTC)","from mail-wm1-f53.google.com (mail-wm1-f53.google.com\n [209.85.128.53])\n\t(using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits))\n\t(No client certificate requested)\n\tby smtp.subspace.kernel.org (Postfix) with ESMTPS id 1545B3C3C03\n\tfor <linux-cifs@vger.kernel.org>; Tue, 28 Apr 2026 15:56:01 +0000 (UTC)","by mail-wm1-f53.google.com with SMTP id\n 5b1f17b1804b1-488b150559bso91545985e9.1\n        for <linux-cifs@vger.kernel.org>;\n Tue, 28 Apr 2026 08:56:01 -0700 (PDT)","from precision ([2a01:4b00:c007:bb00:be9d:a3c4:18b1:4a25])\n        by smtp.gmail.com with ESMTPSA id\n 5b1f17b1804b1-48a775f56fcsm23475685e9.25.2026.04.28.08.55.59\n        (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256);\n        Tue, 28 Apr 2026 08:55:59 -0700 (PDT)"],"ARC-Seal":"i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116;\n\tt=1777391763; cv=none;\n b=XkBpkeW1tEw401G16ZgMX4vboNrYbOYFQAof6uV5cYuHBeSu83U0eXXplswMfbFd+Fsgmuot0L1J+SpciLC5WRgNAwaWOxpJTSrCdDKDobz3cCmhDsjFdnEFTDkeOac3OGRrBQAMPozN/sczJnD0ChC7ryPiMQ+A0VU46cF4AxU=","ARC-Message-Signature":"i=1; a=rsa-sha256; d=subspace.kernel.org;\n\ts=arc-20240116; t=1777391763; c=relaxed/simple;\n\tbh=lQ5DpuALbgTsXOKgwn74AyoacGYenQNBVH9tON40P7o=;\n\th=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:\n\t MIME-Version;\n b=FlyoBa+9+ZUu42q6WYtsjlQTCjSJ+64sXM9Sb/kn5x8W/JDugBiK/hh9NXoXxHYjYuIUSsGW8q+JBtfYFV+fL6ImT3nAHS6YufiKfCG7eolDevI2lbNZhRqw5cIBzTDqTrT4WU+1gv9/3hew3MOeWlyi7/+Ocon9gecQhJeXDac=","ARC-Authentication-Results":"i=1; smtp.subspace.kernel.org;\n dmarc=pass (p=quarantine dis=none) header.from=suse.com;\n spf=pass smtp.mailfrom=suse.com;\n dkim=pass (2048-bit key) header.d=suse.com header.i=@suse.com\n header.b=coQOvt62; arc=none smtp.client-ip=209.85.128.53","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n        d=suse.com; s=google; t=1777391760; x=1777996560;\n darn=vger.kernel.org;\n        h=content-transfer-encoding:mime-version:references:in-reply-to\n         :message-id:date:subject:cc:to:from:from:to:cc:subject:date\n         :message-id:reply-to;\n        bh=ycsCVSVbXnO73EBGsa9NkUp/8I4UNQARemQ6U5oYXqA=;\n        b=coQOvt62SFvhOkRoe9RXDRqvX+nU6A+7SWcF2VqKb0PsDoeAAdl70+ynTtXtByTGh9\n         fvv+wq9B8Ge084X1ECpM+eV/5o7Cuf3y6Z3bxtYTdBHD95v3WUz9vSvPKqNQEa0CF/Mr\n         n6Xr1CHCUVSpd/ruS64ln+VZdb3AoR5iCycbcMmpzgjz0Wwj24/QQxQzG67izNLC6Zmw\n         qSuWLrndsp/kaa9sjQhr4uPPG3ysB14QfFQALfq3DWn2QH6U5EPpXJELVIMIM62y2/mq\n         EVpiX2/oDLbn53QSR5QgveG4hWy52O/ILpHLPO3cJIGyhZKAMH5isRIj8ynZyuENDsX5\n         VB4g==","X-Google-DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/relaxed;\n        d=1e100.net; s=20251104; t=1777391760; x=1777996560;\n        h=content-transfer-encoding:mime-version:references:in-reply-to\n         :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from\n         :to:cc:subject:date:message-id:reply-to;\n        bh=ycsCVSVbXnO73EBGsa9NkUp/8I4UNQARemQ6U5oYXqA=;\n        b=f4h1+wL9HPKHzqvoacmufMnzjWZb3+WWEOTqKt5i5TXZ5Y2urJXa4hxxTZz0MROG4p\n         xYFOftUx4vaPdt99Et2O00mVJJeEz2XyuVnjNjGadL+a/yvcK9siMH07TEEu83h/Js2R\n         cWh1sXYqfloLzcwGbdu33DU8zIUnrPLqlsLXON7lyc/IFR+8lfPt5zKVuPy2Rnsm0kCD\n         c/mQ7gbc++ADkyYuenu3VOq5oUbGoHF6ZTTXgrHLY938H47wKzCCcJs6ge0HE9/U0hX0\n         cKKRiUK2wPi0XrBX0Nv2NvvqezY+NRUhWIHHgCSO91nEC5LG7higCUiX8StLjGTi6Yrc\n         2uaA==","X-Gm-Message-State":"AOJu0YyPNcCXiWJeiQeOYg0HNQHE17StY2Q0vjZJYse/XetXJ/1fItC0\n\t+yiaG9QSOUsIaUddkqwAteg98e0dL0j4vNmm216gMTO/QOAJI5LxK0/ze2bTQ4Qf5zzw2TD4SVH\n\th6dBi2W8xhQ==","X-Gm-Gg":"AeBDiev0Z1YiwM0LCJQyAoiQpVO1CfxsLTt0/ciLy3eTvGK9I3Yf32802Ar0WTzUCQC\n\t2gRcomX7JlUJsdtf08TOoJ3nlr5psrKAY4t0GnldEIyRT0jqKcPwC5iAomSftm25oQu1gp1oeSX\n\t/4lgYhunIzWTKxx7Aab3ntvbl1AoFgzkxGFN2RjQStVcJfxrDmyleyoIiPa9DfFXvySifXkQacQ\n\tjy0gzJZGIgvrIc+MzXQrqeresO3aGjcUqf3gL3bFvbaqVOeZwHBIbCExkIY39S4gBmqUL0AX3GC\n\tThCMEQp7h6hPLtcm86H7GFDZw8af1ynf2BhBtuPk5k6A/++AJ92OuF5btmMrYiOW6a8FEcdguPV\n\tqnAOcmKcSevjpsvM7uqndiREE7oVvJWo6G5SaAMvVq/MiUgD4FVSWX2+t5aVT7FSWPvGO43EIdd\n\t0K9s/Yx4PzWdQkheyqspnDdoLvPolh+67IRPD7Or1fBHcb","X-Received":"by 2002:a05:600c:1d8c:b0:48a:568f:ae82 with SMTP id\n 5b1f17b1804b1-48a77adfebfmr65313715e9.10.1777391760215;\n        Tue, 28 Apr 2026 08:56:00 -0700 (PDT)","From":"Henrique Carvalho <henrique.carvalho@suse.com>","To":"linux-cifs@vger.kernel.org","Cc":"sfrench@samba.org,\n\tlinkinjeon@kernel.org,\n\tmetze@samba.org,\n\tpc@manguebit.com,\n\tronniesahlberg@gmail.com,\n\tsprasad@microsoft.com,\n\ttom@talpey.com,\n\tbharathsm@microsoft.com,\n\tematsumiya@suse.de,\n\tHenrique Carvalho <henrique.carvalho@suse.com>","Subject":"[PATCH v2 02/11] smb: client: refactor negotiate context assembly","Date":"Tue, 28 Apr 2026 12:55:40 -0300","Message-ID":"<20260428155542.226234-3-henrique.carvalho@suse.com>","X-Mailer":"git-send-email 2.53.0","In-Reply-To":"<20260428155542.226234-1-henrique.carvalho@suse.com>","References":"<20260428155542.226234-1-henrique.carvalho@suse.com>","Precedence":"bulk","X-Mailing-List":"linux-cifs@vger.kernel.org","List-Id":"<linux-cifs.vger.kernel.org>","List-Subscribe":"<mailto:linux-cifs+subscribe@vger.kernel.org>","List-Unsubscribe":"<mailto:linux-cifs+unsubscribe@vger.kernel.org>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit"},"content":"Introduce struct neg_ctx, smb2_neg_context_len() and add_context() to\nhandle alignment, pointer advancement, and counter increment in one\nplace, eliminating repetitive code.\n\nAlso fix missing ALIGN() for posix negotiate context.\n\nSigned-off-by: Henrique Carvalho <henrique.carvalho@suse.com>\n---\n fs/smb/client/smb2pdu.c | 106 +++++++++++++++++++++-------------------\n 1 file changed, 56 insertions(+), 50 deletions(-)","diff":"diff --git a/fs/smb/client/smb2pdu.c b/fs/smb/client/smb2pdu.c\nindex cddb1ae04e83..bac88a612d5d 100644\n--- a/fs/smb/client/smb2pdu.c\n+++ b/fs/smb/client/smb2pdu.c\n@@ -618,7 +618,15 @@ static int smb2_ioctl_req_init(u32 opcode, struct cifs_tcon *tcon,\n \n /* For explanation of negotiate contexts see MS-SMB2 section 2.2.3.1 */\n \n-static void\n+static inline unsigned int\n+smb2_neg_context_len(const void *p)\n+{\n+\tconst struct smb2_neg_context *ctx = p;\n+\n+\treturn sizeof(*ctx) + le16_to_cpu(ctx->DataLength);\n+}\n+\n+static unsigned int\n build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)\n {\n \tpneg_ctxt->ContextType = SMB2_PREAUTH_INTEGRITY_CAPABILITIES;\n@@ -627,9 +635,10 @@ build_preauth_ctxt(struct smb2_preauth_neg_context *pneg_ctxt)\n \tpneg_ctxt->SaltLength = cpu_to_le16(SMB311_SALT_SIZE);\n \tget_random_bytes(pneg_ctxt->Salt, SMB311_SALT_SIZE);\n \tpneg_ctxt->HashAlgorithms = SMB2_PREAUTH_INTEGRITY_SHA512;\n+\treturn smb2_neg_context_len(pneg_ctxt);\n }\n \n-static void\n+static unsigned int\n build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)\n {\n \tpneg_ctxt->ContextType = SMB2_COMPRESSION_CAPABILITIES;\n@@ -640,13 +649,13 @@ build_compression_ctxt(struct smb2_compression_capabilities_context *pneg_ctxt)\n \tpneg_ctxt->CompressionAlgorithms[0] = SMB3_COMPRESS_LZ77;\n \tpneg_ctxt->CompressionAlgorithms[1] = SMB3_COMPRESS_LZ77_HUFF;\n \tpneg_ctxt->CompressionAlgorithms[2] = SMB3_COMPRESS_LZNT1;\n+\treturn smb2_neg_context_len(pneg_ctxt);\n }\n \n static unsigned int\n build_signing_ctxt(struct smb2_signing_capabilities *pneg_ctxt)\n {\n-\tunsigned int ctxt_len = sizeof(struct smb2_signing_capabilities);\n-\tunsigned short num_algs = 1; /* number of signing algorithms sent */\n+\tconst unsigned short num_algs = 1; /* number of signing algorithms sent */\n \n \tpneg_ctxt->ContextType = SMB2_SIGNING_CAPABILITIES;\n \t/*\n@@ -658,13 +667,11 @@ build_signing_ctxt(struct smb2_signing_capabilities *pneg_ctxt)\n \tpneg_ctxt->SigningAlgorithmCount = cpu_to_le16(num_algs);\n \tpneg_ctxt->SigningAlgorithms[0] = cpu_to_le16(SIGNING_ALG_AES_CMAC);\n \n-\tctxt_len += sizeof(__le16) * num_algs;\n-\tctxt_len = ALIGN(ctxt_len, 8);\n-\treturn ctxt_len;\n+\treturn smb2_neg_context_len(pneg_ctxt);\n \t/* TBD add SIGNING_ALG_AES_GMAC and/or SIGNING_ALG_HMAC_SHA256 */\n }\n \n-static void\n+static unsigned int\n build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)\n {\n \tpneg_ctxt->ContextType = SMB2_ENCRYPTION_CAPABILITIES;\n@@ -684,6 +691,7 @@ build_encrypt_ctxt(struct smb2_encryption_neg_context *pneg_ctxt)\n \t\tpneg_ctxt->Ciphers[0] = SMB2_ENCRYPTION_AES128_GCM;\n \t\tpneg_ctxt->Ciphers[1] = SMB2_ENCRYPTION_AES128_CCM;\n \t}\n+\treturn smb2_neg_context_len(pneg_ctxt);\n }\n \n static unsigned int\n@@ -695,11 +703,11 @@ build_netname_ctxt(struct smb2_netname_neg_context *pneg_ctxt, char *hostname)\n \n \t/* copy up to max of first 100 bytes of server name to NetName field */\n \tpneg_ctxt->DataLength = cpu_to_le16(2 * cifs_strtoUTF16(pneg_ctxt->NetName, hostname, 100, cp));\n-\t/* context size is DataLength + minimal smb2_neg_context */\n-\treturn ALIGN(le16_to_cpu(pneg_ctxt->DataLength) + sizeof(struct smb2_neg_context), 8);\n+\n+\treturn smb2_neg_context_len(pneg_ctxt);\n }\n \n-static void\n+static unsigned int\n build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)\n {\n \tpneg_ctxt->ContextType = SMB2_POSIX_EXTENSIONS_AVAILABLE;\n@@ -721,15 +729,31 @@ build_posix_ctxt(struct smb2_posix_neg_context *pneg_ctxt)\n \tpneg_ctxt->Name[13] = 0x8B;\n \tpneg_ctxt->Name[14] = 0xCD;\n \tpneg_ctxt->Name[15] = 0x7C;\n+\treturn smb2_neg_context_len(pneg_ctxt);\n+}\n+\n+struct neg_ctx {\n+\tchar *pos;\n+\tunsigned int len;\n+\tunsigned int cnt;\n+};\n+\n+static inline void\n+add_context(struct neg_ctx *nc, unsigned int ctx_len)\n+{\n+\tunsigned int aligned_len = ALIGN(ctx_len, 8);\n+\n+\tnc->pos += aligned_len;\n+\tnc->len += aligned_len;\n+\tnc->cnt++;\n }\n \n static void\n assemble_neg_contexts(struct smb2_negotiate_req *req,\n \t\t      struct TCP_Server_Info *server, unsigned int *total_len)\n {\n-\tunsigned int ctxt_len, neg_context_count;\n+\tstruct neg_ctx nc;\n \tstruct TCP_Server_Info *pserver;\n-\tchar *pneg_ctxt;\n \tchar *hostname;\n \n \tif (*total_len > 200) {\n@@ -744,18 +768,15 @@ assemble_neg_contexts(struct smb2_negotiate_req *req,\n \t */\n \t*total_len = ALIGN(*total_len, 8);\n \n-\tpneg_ctxt = (*total_len) + (char *)req;\n \treq->NegotiateContextOffset = cpu_to_le32(*total_len);\n \n-\tbuild_preauth_ctxt((struct smb2_preauth_neg_context *)pneg_ctxt);\n-\tctxt_len = ALIGN(sizeof(struct smb2_preauth_neg_context), 8);\n-\t*total_len += ctxt_len;\n-\tpneg_ctxt += ctxt_len;\n+\tnc.pos = (*total_len) + (char *)req;\n+\tnc.len = *total_len;\n+\tnc.cnt = 0;\n+\n+\tadd_context(&nc, build_preauth_ctxt((struct smb2_preauth_neg_context *)nc.pos));\n \n-\tbuild_encrypt_ctxt((struct smb2_encryption_neg_context *)pneg_ctxt);\n-\tctxt_len = ALIGN(sizeof(struct smb2_encryption_neg_context), 8);\n-\t*total_len += ctxt_len;\n-\tpneg_ctxt += ctxt_len;\n+\tadd_context(&nc, build_encrypt_ctxt((struct smb2_encryption_neg_context *)nc.pos));\n \n \t/*\n \t * secondary channels don't have the hostname field populated\n@@ -764,41 +785,26 @@ assemble_neg_contexts(struct smb2_negotiate_req *req,\n \tpserver = SERVER_IS_CHAN(server) ? server->primary_server : server;\n \tcifs_server_lock(pserver);\n \thostname = pserver->hostname;\n-\tif (hostname && (hostname[0] != 0)) {\n-\t\tctxt_len = build_netname_ctxt((struct smb2_netname_neg_context *)pneg_ctxt,\n-\t\t\t\t\t      hostname);\n-\t\t*total_len += ctxt_len;\n-\t\tpneg_ctxt += ctxt_len;\n-\t\tneg_context_count = 3;\n-\t} else\n-\t\tneg_context_count = 2;\n+\tif (hostname && (hostname[0] != 0))\n+\t\tadd_context(&nc,\n+\t\t\t    build_netname_ctxt((struct smb2_netname_neg_context *)nc.pos,\n+\t\t\t\t\t       hostname));\n \tcifs_server_unlock(pserver);\n \n-\tbuild_posix_ctxt((struct smb2_posix_neg_context *)pneg_ctxt);\n-\t*total_len += sizeof(struct smb2_posix_neg_context);\n-\tpneg_ctxt += sizeof(struct smb2_posix_neg_context);\n-\tneg_context_count++;\n+\tadd_context(&nc, build_posix_ctxt((struct smb2_posix_neg_context *)nc.pos));\n \n-\tif (server->compression.requested) {\n-\t\tbuild_compression_ctxt((struct smb2_compression_capabilities_context *)\n-\t\t\t\tpneg_ctxt);\n-\t\tctxt_len = ALIGN(sizeof(struct smb2_compression_capabilities_context), 8);\n-\t\t*total_len += ctxt_len;\n-\t\tpneg_ctxt += ctxt_len;\n-\t\tneg_context_count++;\n-\t}\n+\tif (server->compression.requested)\n+\t\tadd_context(&nc,\n+\t\t\t    build_compression_ctxt(\n+\t\t\t\t    (struct smb2_compression_capabilities_context *)nc.pos));\n \n-\tif (enable_negotiate_signing) {\n-\t\tctxt_len = build_signing_ctxt((struct smb2_signing_capabilities *)\n-\t\t\t\tpneg_ctxt);\n-\t\t*total_len += ctxt_len;\n-\t\tpneg_ctxt += ctxt_len;\n-\t\tneg_context_count++;\n-\t}\n+\tif (enable_negotiate_signing)\n+\t\tadd_context(&nc, build_signing_ctxt((struct smb2_signing_capabilities *)nc.pos));\n \n \t/* check for and add transport_capabilities and signing capabilities */\n-\treq->NegotiateContextCount = cpu_to_le16(neg_context_count);\n+\treq->NegotiateContextCount = cpu_to_le16(nc.cnt);\n \n+\t*total_len = nc.len;\n }\n \n /* If invalid preauth context warn but use what we requested, SHA-512 */\n","prefixes":["v2","02/11"]}