Steve French | 929be90 | 2021-06-18 00:31:49 -0500 | [diff] [blame] | 1 | // SPDX-License-Identifier: LGPL-2.1 |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 2 | /* |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 3 | * |
| 4 | * SMB/CIFS session setup handling routines |
| 5 | * |
Steve French | d185cda | 2009-04-30 17:45:10 +0000 | [diff] [blame] | 6 | * Copyright (c) International Business Machines Corp., 2006, 2009 |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 7 | * Author(s): Steve French (sfrench@us.ibm.com) |
| 8 | * |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
| 11 | #include "cifspdu.h" |
| 12 | #include "cifsglob.h" |
| 13 | #include "cifsproto.h" |
| 14 | #include "cifs_unicode.h" |
| 15 | #include "cifs_debug.h" |
| 16 | #include "ntlmssp.h" |
| 17 | #include "nterr.h" |
Steve French | 9c53588 | 2006-06-01 05:09:10 +0000 | [diff] [blame] | 18 | #include <linux/utsname.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 19 | #include <linux/slab.h> |
Steve French | 52d0053 | 2022-01-19 22:00:29 -0600 | [diff] [blame] | 20 | #include <linux/version.h> |
| 21 | #include "cifsfs.h" |
Steve French | 2442421 | 2007-11-16 23:37:35 +0000 | [diff] [blame] | 22 | #include "cifs_spnego.h" |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 23 | #include "smb2proto.h" |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 24 | #include "fs_context.h" |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 25 | |
Ronnie Sahlberg | 387ec58 | 2020-12-14 16:40:20 +1000 | [diff] [blame] | 26 | static int |
| 27 | cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses, |
| 28 | struct cifs_server_iface *iface); |
| 29 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 30 | bool |
| 31 | is_server_using_iface(struct TCP_Server_Info *server, |
| 32 | struct cifs_server_iface *iface) |
| 33 | { |
| 34 | struct sockaddr_in *i4 = (struct sockaddr_in *)&iface->sockaddr; |
| 35 | struct sockaddr_in6 *i6 = (struct sockaddr_in6 *)&iface->sockaddr; |
| 36 | struct sockaddr_in *s4 = (struct sockaddr_in *)&server->dstaddr; |
| 37 | struct sockaddr_in6 *s6 = (struct sockaddr_in6 *)&server->dstaddr; |
| 38 | |
| 39 | if (server->dstaddr.ss_family != iface->sockaddr.ss_family) |
| 40 | return false; |
| 41 | if (server->dstaddr.ss_family == AF_INET) { |
| 42 | if (s4->sin_addr.s_addr != i4->sin_addr.s_addr) |
| 43 | return false; |
| 44 | } else if (server->dstaddr.ss_family == AF_INET6) { |
| 45 | if (memcmp(&s6->sin6_addr, &i6->sin6_addr, |
| 46 | sizeof(i6->sin6_addr)) != 0) |
| 47 | return false; |
| 48 | } else { |
| 49 | /* unknown family.. */ |
| 50 | return false; |
| 51 | } |
| 52 | return true; |
| 53 | } |
| 54 | |
| 55 | bool is_ses_using_iface(struct cifs_ses *ses, struct cifs_server_iface *iface) |
| 56 | { |
| 57 | int i; |
| 58 | |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 59 | spin_lock(&ses->chan_lock); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 60 | for (i = 0; i < ses->chan_count; i++) { |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 61 | if (is_server_using_iface(ses->chans[i].server, iface)) { |
| 62 | spin_unlock(&ses->chan_lock); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 63 | return true; |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 64 | } |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 65 | } |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 66 | spin_unlock(&ses->chan_lock); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 67 | return false; |
| 68 | } |
| 69 | |
Shyam Prasad N | 88b024f | 2021-11-19 14:16:57 +0000 | [diff] [blame] | 70 | /* channel helper functions. assumed that chan_lock is held by caller. */ |
| 71 | |
Shyam Prasad N | d1a931c | 2021-07-19 12:46:53 +0000 | [diff] [blame] | 72 | unsigned int |
| 73 | cifs_ses_get_chan_index(struct cifs_ses *ses, |
| 74 | struct TCP_Server_Info *server) |
| 75 | { |
| 76 | unsigned int i; |
| 77 | |
| 78 | for (i = 0; i < ses->chan_count; i++) { |
| 79 | if (ses->chans[i].server == server) |
| 80 | return i; |
| 81 | } |
| 82 | |
| 83 | /* If we didn't find the channel, it is likely a bug */ |
| 84 | WARN_ON(1); |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | cifs_chan_set_need_reconnect(struct cifs_ses *ses, |
| 90 | struct TCP_Server_Info *server) |
| 91 | { |
| 92 | unsigned int chan_index = cifs_ses_get_chan_index(ses, server); |
| 93 | |
| 94 | set_bit(chan_index, &ses->chans_need_reconnect); |
| 95 | cifs_dbg(FYI, "Set reconnect bitmask for chan %u; now 0x%lx\n", |
| 96 | chan_index, ses->chans_need_reconnect); |
| 97 | } |
| 98 | |
| 99 | void |
| 100 | cifs_chan_clear_need_reconnect(struct cifs_ses *ses, |
| 101 | struct TCP_Server_Info *server) |
| 102 | { |
| 103 | unsigned int chan_index = cifs_ses_get_chan_index(ses, server); |
| 104 | |
| 105 | clear_bit(chan_index, &ses->chans_need_reconnect); |
| 106 | cifs_dbg(FYI, "Cleared reconnect bitmask for chan %u; now 0x%lx\n", |
| 107 | chan_index, ses->chans_need_reconnect); |
| 108 | } |
| 109 | |
| 110 | bool |
| 111 | cifs_chan_needs_reconnect(struct cifs_ses *ses, |
| 112 | struct TCP_Server_Info *server) |
| 113 | { |
| 114 | unsigned int chan_index = cifs_ses_get_chan_index(ses, server); |
| 115 | |
| 116 | return CIFS_CHAN_NEEDS_RECONNECT(ses, chan_index); |
| 117 | } |
| 118 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 119 | /* returns number of channels added */ |
Ronnie Sahlberg | 387ec58 | 2020-12-14 16:40:20 +1000 | [diff] [blame] | 120 | int cifs_try_adding_channels(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses) |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 121 | { |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 122 | int old_chan_count, new_chan_count; |
| 123 | int left; |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 124 | int i = 0; |
| 125 | int rc = 0; |
Aurelien Aptel | 65a37a3 | 2019-11-20 17:15:59 +0100 | [diff] [blame] | 126 | int tries = 0; |
Aurelien Aptel | 9a7d5a9 | 2019-12-04 16:14:54 +0100 | [diff] [blame] | 127 | struct cifs_server_iface *ifaces = NULL; |
| 128 | size_t iface_count; |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 129 | |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 130 | if (ses->server->dialect < SMB30_PROT_ID) { |
| 131 | cifs_dbg(VFS, "multichannel is not supported on this protocol version, use 3.0 or above\n"); |
| 132 | return 0; |
| 133 | } |
| 134 | |
| 135 | spin_lock(&ses->chan_lock); |
| 136 | |
| 137 | new_chan_count = old_chan_count = ses->chan_count; |
| 138 | left = ses->chan_max - ses->chan_count; |
| 139 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 140 | if (left <= 0) { |
Shyam Prasad N | 88b024f | 2021-11-19 14:16:57 +0000 | [diff] [blame] | 141 | spin_unlock(&ses->chan_lock); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 142 | cifs_dbg(FYI, |
| 143 | "ses already at max_channels (%zu), nothing to open\n", |
| 144 | ses->chan_max); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 145 | return 0; |
| 146 | } |
| 147 | |
Steve French | 9c2dc11 | 2021-05-07 20:00:41 -0500 | [diff] [blame] | 148 | if (!(ses->server->capabilities & SMB2_GLOBAL_CAP_MULTI_CHANNEL)) { |
Steve French | 9c2dc11 | 2021-05-07 20:00:41 -0500 | [diff] [blame] | 149 | ses->chan_max = 1; |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 150 | spin_unlock(&ses->chan_lock); |
Steve French | 0226487 | 2021-11-15 21:00:08 -0600 | [diff] [blame] | 151 | cifs_dbg(VFS, "server %s does not support multichannel\n", ses->server->hostname); |
Steve French | 9c2dc11 | 2021-05-07 20:00:41 -0500 | [diff] [blame] | 152 | return 0; |
| 153 | } |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 154 | spin_unlock(&ses->chan_lock); |
Steve French | 9c2dc11 | 2021-05-07 20:00:41 -0500 | [diff] [blame] | 155 | |
Aurelien Aptel | 65a37a3 | 2019-11-20 17:15:59 +0100 | [diff] [blame] | 156 | /* |
Aurelien Aptel | 9a7d5a9 | 2019-12-04 16:14:54 +0100 | [diff] [blame] | 157 | * Make a copy of the iface list at the time and use that |
| 158 | * instead so as to not hold the iface spinlock for opening |
| 159 | * channels |
| 160 | */ |
| 161 | spin_lock(&ses->iface_lock); |
| 162 | iface_count = ses->iface_count; |
| 163 | if (iface_count <= 0) { |
| 164 | spin_unlock(&ses->iface_lock); |
Aurelien Aptel | 343a1b7 | 2020-02-06 10:19:11 +0100 | [diff] [blame] | 165 | cifs_dbg(VFS, "no iface list available to open channels\n"); |
Aurelien Aptel | 9a7d5a9 | 2019-12-04 16:14:54 +0100 | [diff] [blame] | 166 | return 0; |
| 167 | } |
| 168 | ifaces = kmemdup(ses->iface_list, iface_count*sizeof(*ifaces), |
| 169 | GFP_ATOMIC); |
| 170 | if (!ifaces) { |
| 171 | spin_unlock(&ses->iface_lock); |
| 172 | return 0; |
| 173 | } |
| 174 | spin_unlock(&ses->iface_lock); |
| 175 | |
| 176 | /* |
Aurelien Aptel | 65a37a3 | 2019-11-20 17:15:59 +0100 | [diff] [blame] | 177 | * Keep connecting to same, fastest, iface for all channels as |
| 178 | * long as its RSS. Try next fastest one if not RSS or channel |
| 179 | * creation fails. |
| 180 | */ |
| 181 | while (left > 0) { |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 182 | struct cifs_server_iface *iface; |
| 183 | |
Aurelien Aptel | 65a37a3 | 2019-11-20 17:15:59 +0100 | [diff] [blame] | 184 | tries++; |
| 185 | if (tries > 3*ses->chan_max) { |
Steve French | bbbf9ea | 2020-05-30 17:29:50 -0500 | [diff] [blame] | 186 | cifs_dbg(FYI, "too many channel open attempts (%d channels left to open)\n", |
Aurelien Aptel | 65a37a3 | 2019-11-20 17:15:59 +0100 | [diff] [blame] | 187 | left); |
| 188 | break; |
| 189 | } |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 190 | |
Aurelien Aptel | 9a7d5a9 | 2019-12-04 16:14:54 +0100 | [diff] [blame] | 191 | iface = &ifaces[i]; |
Aurelien Aptel | 65a37a3 | 2019-11-20 17:15:59 +0100 | [diff] [blame] | 192 | if (is_ses_using_iface(ses, iface) && !iface->rss_capable) { |
Aurelien Aptel | 9a7d5a9 | 2019-12-04 16:14:54 +0100 | [diff] [blame] | 193 | i = (i+1) % iface_count; |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 194 | continue; |
| 195 | } |
| 196 | |
Ronnie Sahlberg | 387ec58 | 2020-12-14 16:40:20 +1000 | [diff] [blame] | 197 | rc = cifs_ses_add_channel(cifs_sb, ses, iface); |
Aurelien Aptel | 65a37a3 | 2019-11-20 17:15:59 +0100 | [diff] [blame] | 198 | if (rc) { |
| 199 | cifs_dbg(FYI, "failed to open extra channel on iface#%d rc=%d\n", |
| 200 | i, rc); |
Aurelien Aptel | 9a7d5a9 | 2019-12-04 16:14:54 +0100 | [diff] [blame] | 201 | i = (i+1) % iface_count; |
Aurelien Aptel | 65a37a3 | 2019-11-20 17:15:59 +0100 | [diff] [blame] | 202 | continue; |
| 203 | } |
| 204 | |
| 205 | cifs_dbg(FYI, "successfully opened new channel on iface#%d\n", |
| 206 | i); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 207 | left--; |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 208 | new_chan_count++; |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 209 | } |
| 210 | |
Aurelien Aptel | 9a7d5a9 | 2019-12-04 16:14:54 +0100 | [diff] [blame] | 211 | kfree(ifaces); |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 212 | return new_chan_count - old_chan_count; |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 213 | } |
| 214 | |
Aurelien Aptel | 2f58967 | 2020-04-24 16:55:31 +0200 | [diff] [blame] | 215 | /* |
| 216 | * If server is a channel of ses, return the corresponding enclosing |
| 217 | * cifs_chan otherwise return NULL. |
| 218 | */ |
| 219 | struct cifs_chan * |
| 220 | cifs_ses_find_chan(struct cifs_ses *ses, struct TCP_Server_Info *server) |
| 221 | { |
| 222 | int i; |
| 223 | |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 224 | spin_lock(&ses->chan_lock); |
Aurelien Aptel | 2f58967 | 2020-04-24 16:55:31 +0200 | [diff] [blame] | 225 | for (i = 0; i < ses->chan_count; i++) { |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 226 | if (ses->chans[i].server == server) { |
| 227 | spin_unlock(&ses->chan_lock); |
Aurelien Aptel | 2f58967 | 2020-04-24 16:55:31 +0200 | [diff] [blame] | 228 | return &ses->chans[i]; |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 229 | } |
Aurelien Aptel | 2f58967 | 2020-04-24 16:55:31 +0200 | [diff] [blame] | 230 | } |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 231 | spin_unlock(&ses->chan_lock); |
Aurelien Aptel | 2f58967 | 2020-04-24 16:55:31 +0200 | [diff] [blame] | 232 | return NULL; |
| 233 | } |
| 234 | |
Ronnie Sahlberg | 387ec58 | 2020-12-14 16:40:20 +1000 | [diff] [blame] | 235 | static int |
| 236 | cifs_ses_add_channel(struct cifs_sb_info *cifs_sb, struct cifs_ses *ses, |
| 237 | struct cifs_server_iface *iface) |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 238 | { |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 239 | struct TCP_Server_Info *chan_server; |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 240 | struct cifs_chan *chan; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 241 | struct smb3_fs_context ctx = {NULL}; |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 242 | static const char unc_fmt[] = "\\%s\\foo"; |
| 243 | char unc[sizeof(unc_fmt)+SERVER_NAME_LEN_WITH_NULL] = {0}; |
| 244 | struct sockaddr_in *ipv4 = (struct sockaddr_in *)&iface->sockaddr; |
| 245 | struct sockaddr_in6 *ipv6 = (struct sockaddr_in6 *)&iface->sockaddr; |
| 246 | int rc; |
| 247 | unsigned int xid = get_xid(); |
| 248 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 249 | if (iface->sockaddr.ss_family == AF_INET) |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 250 | cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI4)\n", |
| 251 | ses, iface->speed, iface->rdma_capable ? "yes" : "no", |
| 252 | &ipv4->sin_addr); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 253 | else |
Aurelien Aptel | 5e53895 | 2021-05-28 16:32:48 +0200 | [diff] [blame] | 254 | cifs_dbg(FYI, "adding channel to ses %p (speed:%zu bps rdma:%s ip:%pI6)\n", |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 255 | ses, iface->speed, iface->rdma_capable ? "yes" : "no", |
| 256 | &ipv6->sin6_addr); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 257 | |
| 258 | /* |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 259 | * Setup a ctx with mostly the same info as the existing |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 260 | * session and overwrite it with the requested iface data. |
| 261 | * |
| 262 | * We need to setup at least the fields used for negprot and |
| 263 | * sesssetup. |
| 264 | * |
Ronnie Sahlberg | 24e0a1e | 2020-12-10 00:06:02 -0600 | [diff] [blame] | 265 | * We only need the ctx here, so we can reuse memory from |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 266 | * the session and server without caring about memory |
| 267 | * management. |
| 268 | */ |
| 269 | |
| 270 | /* Always make new connection for now (TODO?) */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 271 | ctx.nosharesock = true; |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 272 | |
| 273 | /* Auth */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 274 | ctx.domainauto = ses->domainAuto; |
| 275 | ctx.domainname = ses->domainName; |
Shyam Prasad N | 5112d80 | 2021-11-19 13:04:11 +0000 | [diff] [blame] | 276 | ctx.server_hostname = ses->server->hostname; |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 277 | ctx.username = ses->user_name; |
| 278 | ctx.password = ses->password; |
| 279 | ctx.sectype = ses->sectype; |
| 280 | ctx.sign = ses->sign; |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 281 | |
| 282 | /* UNC and paths */ |
| 283 | /* XXX: Use ses->server->hostname? */ |
Steve French | b438fcf | 2021-02-20 19:24:11 -0600 | [diff] [blame] | 284 | sprintf(unc, unc_fmt, ses->ip_addr); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 285 | ctx.UNC = unc; |
| 286 | ctx.prepath = ""; |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 287 | |
Steve French | bbbf9ea | 2020-05-30 17:29:50 -0500 | [diff] [blame] | 288 | /* Reuse same version as master connection */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 289 | ctx.vals = ses->server->vals; |
| 290 | ctx.ops = ses->server->ops; |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 291 | |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 292 | ctx.noblocksnd = ses->server->noblocksnd; |
| 293 | ctx.noautotune = ses->server->noautotune; |
| 294 | ctx.sockopt_tcp_nodelay = ses->server->tcp_nodelay; |
| 295 | ctx.echo_interval = ses->server->echo_interval / HZ; |
Aurelien Aptel | a249cc8 | 2021-03-04 17:42:21 +0000 | [diff] [blame] | 296 | ctx.max_credits = ses->server->max_credits; |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 297 | |
| 298 | /* |
| 299 | * This will be used for encoding/decoding user/domain/pw |
| 300 | * during sess setup auth. |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 301 | */ |
Ronnie Sahlberg | 387ec58 | 2020-12-14 16:40:20 +1000 | [diff] [blame] | 302 | ctx.local_nls = cifs_sb->local_nls; |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 303 | |
| 304 | /* Use RDMA if possible */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 305 | ctx.rdma = iface->rdma_capable; |
| 306 | memcpy(&ctx.dstaddr, &iface->sockaddr, sizeof(struct sockaddr_storage)); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 307 | |
| 308 | /* reuse master con client guid */ |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 309 | memcpy(&ctx.client_guid, ses->server->client_guid, |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 310 | SMB2_CLIENT_GUID_SIZE); |
Ronnie Sahlberg | 3fa1c6d | 2020-12-09 23:07:12 -0600 | [diff] [blame] | 311 | ctx.use_client_guid = true; |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 312 | |
Shyam Prasad N | 0f2b305 | 2021-07-19 11:26:24 +0000 | [diff] [blame] | 313 | chan_server = cifs_get_tcp_session(&ctx, ses->server); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 314 | |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 315 | spin_lock(&ses->chan_lock); |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 316 | chan = &ses->chans[ses->chan_count]; |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 317 | chan->server = chan_server; |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 318 | if (IS_ERR(chan->server)) { |
| 319 | rc = PTR_ERR(chan->server); |
| 320 | chan->server = NULL; |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 321 | spin_unlock(&ses->chan_lock); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 322 | goto out; |
| 323 | } |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 324 | ses->chan_count++; |
| 325 | atomic_set(&ses->chan_seq, 0); |
| 326 | |
| 327 | /* Mark this channel as needing connect/setup */ |
| 328 | cifs_chan_set_need_reconnect(ses, chan->server); |
| 329 | |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 330 | spin_unlock(&ses->chan_lock); |
| 331 | |
Shyam Prasad N | 73f9bfb | 2021-07-19 17:37:52 +0000 | [diff] [blame] | 332 | mutex_lock(&ses->session_mutex); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 333 | /* |
| 334 | * We need to allocate the server crypto now as we will need |
| 335 | * to sign packets before we generate the channel signing key |
| 336 | * (we sign with the session key) |
| 337 | */ |
| 338 | rc = smb311_crypto_shash_allocate(chan->server); |
| 339 | if (rc) { |
| 340 | cifs_dbg(VFS, "%s: crypto alloc failed\n", __func__); |
Shyam Prasad N | 73f9bfb | 2021-07-19 17:37:52 +0000 | [diff] [blame] | 341 | mutex_unlock(&ses->session_mutex); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 342 | goto out; |
| 343 | } |
| 344 | |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 345 | rc = cifs_negotiate_protocol(xid, ses, chan->server); |
| 346 | if (!rc) |
| 347 | rc = cifs_setup_session(xid, ses, chan->server, cifs_sb->local_nls); |
Shyam Prasad N | 724244c | 2021-07-19 10:54:46 +0000 | [diff] [blame] | 348 | |
Shyam Prasad N | 73f9bfb | 2021-07-19 17:37:52 +0000 | [diff] [blame] | 349 | mutex_unlock(&ses->session_mutex); |
| 350 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 351 | out: |
Shyam Prasad N | d1a931c | 2021-07-19 12:46:53 +0000 | [diff] [blame] | 352 | if (rc && chan->server) { |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 353 | spin_lock(&ses->chan_lock); |
Shyam Prasad N | d1a931c | 2021-07-19 12:46:53 +0000 | [diff] [blame] | 354 | /* we rely on all bits beyond chan_count to be clear */ |
| 355 | cifs_chan_clear_need_reconnect(ses, chan->server); |
| 356 | ses->chan_count--; |
Shyam Prasad N | 2e0fa29 | 2021-07-19 14:04:11 +0000 | [diff] [blame] | 357 | /* |
| 358 | * chan_count should never reach 0 as at least the primary |
| 359 | * channel is always allocated |
| 360 | */ |
| 361 | WARN_ON(ses->chan_count < 1); |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 362 | spin_unlock(&ses->chan_lock); |
Shyam Prasad N | d1a931c | 2021-07-19 12:46:53 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 365 | if (rc && chan->server) |
| 366 | cifs_put_tcp_session(chan->server, 0); |
Aurelien Aptel | d70e9fa | 2019-09-20 06:31:10 +0200 | [diff] [blame] | 367 | |
| 368 | return rc; |
| 369 | } |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 370 | |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 371 | static __u32 cifs_ssetup_hdr(struct cifs_ses *ses, |
| 372 | struct TCP_Server_Info *server, |
| 373 | SESSION_SETUP_ANDX *pSMB) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 374 | { |
| 375 | __u32 capabilities = 0; |
| 376 | |
| 377 | /* init fields common to all four types of SessSetup */ |
Steve French | eca6acf | 2009-02-20 05:43:09 +0000 | [diff] [blame] | 378 | /* Note that offsets for first seven fields in req struct are same */ |
| 379 | /* in CIFS Specs so does not matter which of 3 forms of struct */ |
| 380 | /* that we use in next few lines */ |
| 381 | /* Note that header is initialized to zero in header_assemble */ |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 382 | pSMB->req.AndXCommand = 0xFF; |
Jeff Layton | c974bef | 2011-10-11 06:41:32 -0400 | [diff] [blame] | 383 | pSMB->req.MaxBufferSize = cpu_to_le16(min_t(u32, |
| 384 | CIFSMaxBufSize + MAX_CIFS_HDR_SIZE - 4, |
| 385 | USHRT_MAX)); |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 386 | pSMB->req.MaxMpxCount = cpu_to_le16(server->maxReq); |
Fabian Frederick | bc09d14 | 2014-12-10 15:41:15 -0800 | [diff] [blame] | 387 | pSMB->req.VcNumber = cpu_to_le16(1); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 388 | |
| 389 | /* Now no need to set SMBFLG_CASELESS or obsolete CANONICAL PATH */ |
| 390 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 391 | /* BB verify whether signing required on neg or just on auth frame |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 392 | (and NTLM case) */ |
| 393 | |
| 394 | capabilities = CAP_LARGE_FILES | CAP_NT_SMBS | CAP_LEVEL_II_OPLOCKS | |
| 395 | CAP_LARGE_WRITE_X | CAP_LARGE_READ_X; |
| 396 | |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 397 | if (server->sign) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 398 | pSMB->req.hdr.Flags2 |= SMBFLG2_SECURITY_SIGNATURE; |
| 399 | |
| 400 | if (ses->capabilities & CAP_UNICODE) { |
| 401 | pSMB->req.hdr.Flags2 |= SMBFLG2_UNICODE; |
| 402 | capabilities |= CAP_UNICODE; |
| 403 | } |
| 404 | if (ses->capabilities & CAP_STATUS32) { |
| 405 | pSMB->req.hdr.Flags2 |= SMBFLG2_ERR_STATUS; |
| 406 | capabilities |= CAP_STATUS32; |
| 407 | } |
| 408 | if (ses->capabilities & CAP_DFS) { |
| 409 | pSMB->req.hdr.Flags2 |= SMBFLG2_DFS; |
| 410 | capabilities |= CAP_DFS; |
| 411 | } |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 412 | if (ses->capabilities & CAP_UNIX) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 413 | capabilities |= CAP_UNIX; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 414 | |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 415 | return capabilities; |
| 416 | } |
| 417 | |
Jeff Layton | 0d3a01f | 2007-10-16 17:32:19 +0000 | [diff] [blame] | 418 | static void |
| 419 | unicode_oslm_strings(char **pbcc_area, const struct nls_table *nls_cp) |
| 420 | { |
| 421 | char *bcc_ptr = *pbcc_area; |
| 422 | int bytes_ret = 0; |
| 423 | |
| 424 | /* Copy OS version */ |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 425 | bytes_ret = cifs_strtoUTF16((__le16 *)bcc_ptr, "Linux version ", 32, |
| 426 | nls_cp); |
Jeff Layton | 0d3a01f | 2007-10-16 17:32:19 +0000 | [diff] [blame] | 427 | bcc_ptr += 2 * bytes_ret; |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 428 | bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, init_utsname()->release, |
| 429 | 32, nls_cp); |
Jeff Layton | 0d3a01f | 2007-10-16 17:32:19 +0000 | [diff] [blame] | 430 | bcc_ptr += 2 * bytes_ret; |
| 431 | bcc_ptr += 2; /* trailing null */ |
| 432 | |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 433 | bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, CIFS_NETWORK_OPSYS, |
| 434 | 32, nls_cp); |
Jeff Layton | 0d3a01f | 2007-10-16 17:32:19 +0000 | [diff] [blame] | 435 | bcc_ptr += 2 * bytes_ret; |
| 436 | bcc_ptr += 2; /* trailing null */ |
| 437 | |
| 438 | *pbcc_area = bcc_ptr; |
| 439 | } |
| 440 | |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 441 | static void unicode_domain_string(char **pbcc_area, struct cifs_ses *ses, |
Jeff Layton | 0d3a01f | 2007-10-16 17:32:19 +0000 | [diff] [blame] | 442 | const struct nls_table *nls_cp) |
| 443 | { |
| 444 | char *bcc_ptr = *pbcc_area; |
| 445 | int bytes_ret = 0; |
| 446 | |
| 447 | /* copy domain */ |
| 448 | if (ses->domainName == NULL) { |
| 449 | /* Sending null domain better than using a bogus domain name (as |
| 450 | we did briefly in 2.6.18) since server will use its default */ |
| 451 | *bcc_ptr = 0; |
| 452 | *(bcc_ptr+1) = 0; |
| 453 | bytes_ret = 0; |
| 454 | } else |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 455 | bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->domainName, |
Chen Gang | 057d633 | 2013-07-19 09:01:36 +0800 | [diff] [blame] | 456 | CIFS_MAX_DOMAINNAME_LEN, nls_cp); |
Jeff Layton | 0d3a01f | 2007-10-16 17:32:19 +0000 | [diff] [blame] | 457 | bcc_ptr += 2 * bytes_ret; |
| 458 | bcc_ptr += 2; /* account for null terminator */ |
| 459 | |
| 460 | *pbcc_area = bcc_ptr; |
| 461 | } |
| 462 | |
| 463 | |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 464 | static void unicode_ssetup_strings(char **pbcc_area, struct cifs_ses *ses, |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 465 | const struct nls_table *nls_cp) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 466 | { |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 467 | char *bcc_ptr = *pbcc_area; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 468 | int bytes_ret = 0; |
| 469 | |
| 470 | /* BB FIXME add check that strings total less |
| 471 | than 335 or will need to send them as arrays */ |
| 472 | |
Steve French | 0223cf0 | 2006-06-27 19:50:57 +0000 | [diff] [blame] | 473 | /* unicode strings, must be word aligned before the call */ |
| 474 | /* if ((long) bcc_ptr % 2) { |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 475 | *bcc_ptr = 0; |
| 476 | bcc_ptr++; |
Steve French | 0223cf0 | 2006-06-27 19:50:57 +0000 | [diff] [blame] | 477 | } */ |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 478 | /* copy user */ |
Steve French | 8727c8a | 2011-02-25 01:11:56 -0600 | [diff] [blame] | 479 | if (ses->user_name == NULL) { |
Steve French | 6e659c6 | 2006-11-08 23:10:46 +0000 | [diff] [blame] | 480 | /* null user mount */ |
| 481 | *bcc_ptr = 0; |
| 482 | *(bcc_ptr+1) = 0; |
Steve French | 301a6a3 | 2010-02-06 07:08:53 +0000 | [diff] [blame] | 483 | } else { |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 484 | bytes_ret = cifs_strtoUTF16((__le16 *) bcc_ptr, ses->user_name, |
Scott Lovenberg | 8c3a2b4 | 2013-08-09 08:47:17 -0400 | [diff] [blame] | 485 | CIFS_MAX_USERNAME_LEN, nls_cp); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 486 | } |
| 487 | bcc_ptr += 2 * bytes_ret; |
| 488 | bcc_ptr += 2; /* account for null termination */ |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 489 | |
Jeff Layton | 0d3a01f | 2007-10-16 17:32:19 +0000 | [diff] [blame] | 490 | unicode_domain_string(&bcc_ptr, ses, nls_cp); |
| 491 | unicode_oslm_strings(&bcc_ptr, nls_cp); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 492 | |
| 493 | *pbcc_area = bcc_ptr; |
| 494 | } |
| 495 | |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 496 | static void ascii_ssetup_strings(char **pbcc_area, struct cifs_ses *ses, |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 497 | const struct nls_table *nls_cp) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 498 | { |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 499 | char *bcc_ptr = *pbcc_area; |
Ronnie Sahlberg | 340625e | 2019-08-27 09:30:14 +1000 | [diff] [blame] | 500 | int len; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 501 | |
| 502 | /* copy user */ |
| 503 | /* BB what about null user mounts - check that we do this BB */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 504 | /* copy user */ |
Shirish Pargaonkar | de47a41 | 2012-02-02 15:28:28 -0600 | [diff] [blame] | 505 | if (ses->user_name != NULL) { |
Ronnie Sahlberg | 340625e | 2019-08-27 09:30:14 +1000 | [diff] [blame] | 506 | len = strscpy(bcc_ptr, ses->user_name, CIFS_MAX_USERNAME_LEN); |
| 507 | if (WARN_ON_ONCE(len < 0)) |
| 508 | len = CIFS_MAX_USERNAME_LEN - 1; |
| 509 | bcc_ptr += len; |
Shirish Pargaonkar | de47a41 | 2012-02-02 15:28:28 -0600 | [diff] [blame] | 510 | } |
Steve French | 8727c8a | 2011-02-25 01:11:56 -0600 | [diff] [blame] | 511 | /* else null user mount */ |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 512 | *bcc_ptr = 0; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 513 | bcc_ptr++; /* account for null termination */ |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 514 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 515 | /* copy domain */ |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 516 | if (ses->domainName != NULL) { |
Ronnie Sahlberg | 340625e | 2019-08-27 09:30:14 +1000 | [diff] [blame] | 517 | len = strscpy(bcc_ptr, ses->domainName, CIFS_MAX_DOMAINNAME_LEN); |
| 518 | if (WARN_ON_ONCE(len < 0)) |
| 519 | len = CIFS_MAX_DOMAINNAME_LEN - 1; |
| 520 | bcc_ptr += len; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 521 | } /* else we will send a null domain name |
Steve French | 6e659c6 | 2006-11-08 23:10:46 +0000 | [diff] [blame] | 522 | so the server will default to its own domain */ |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 523 | *bcc_ptr = 0; |
| 524 | bcc_ptr++; |
| 525 | |
| 526 | /* BB check for overflow here */ |
| 527 | |
| 528 | strcpy(bcc_ptr, "Linux version "); |
| 529 | bcc_ptr += strlen("Linux version "); |
Serge E. Hallyn | 96b644b | 2006-10-02 02:18:13 -0700 | [diff] [blame] | 530 | strcpy(bcc_ptr, init_utsname()->release); |
| 531 | bcc_ptr += strlen(init_utsname()->release) + 1; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 532 | |
| 533 | strcpy(bcc_ptr, CIFS_NETWORK_OPSYS); |
| 534 | bcc_ptr += strlen(CIFS_NETWORK_OPSYS) + 1; |
| 535 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 536 | *pbcc_area = bcc_ptr; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Jeff Layton | 5914079 | 2009-04-30 07:16:21 -0400 | [diff] [blame] | 539 | static void |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 540 | decode_unicode_ssetup(char **pbcc_area, int bleft, struct cifs_ses *ses, |
Jeff Layton | 5914079 | 2009-04-30 07:16:21 -0400 | [diff] [blame] | 541 | const struct nls_table *nls_cp) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 542 | { |
Jeff Layton | 5914079 | 2009-04-30 07:16:21 -0400 | [diff] [blame] | 543 | int len; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 544 | char *data = *pbcc_area; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 545 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 546 | cifs_dbg(FYI, "bleft %d\n", bleft); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 547 | |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 548 | kfree(ses->serverOS); |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 549 | ses->serverOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 550 | cifs_dbg(FYI, "serverOS=%s\n", ses->serverOS); |
Jeff Layton | 5914079 | 2009-04-30 07:16:21 -0400 | [diff] [blame] | 551 | len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2; |
| 552 | data += len; |
| 553 | bleft -= len; |
| 554 | if (bleft <= 0) |
| 555 | return; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 556 | |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 557 | kfree(ses->serverNOS); |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 558 | ses->serverNOS = cifs_strndup_from_utf16(data, bleft, true, nls_cp); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 559 | cifs_dbg(FYI, "serverNOS=%s\n", ses->serverNOS); |
Jeff Layton | 5914079 | 2009-04-30 07:16:21 -0400 | [diff] [blame] | 560 | len = (UniStrnlen((wchar_t *) data, bleft / 2) * 2) + 2; |
| 561 | data += len; |
| 562 | bleft -= len; |
| 563 | if (bleft <= 0) |
| 564 | return; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 565 | |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 566 | kfree(ses->serverDomain); |
Steve French | acbbb76 | 2012-01-18 22:32:33 -0600 | [diff] [blame] | 567 | ses->serverDomain = cifs_strndup_from_utf16(data, bleft, true, nls_cp); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 568 | cifs_dbg(FYI, "serverDomain=%s\n", ses->serverDomain); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 569 | |
Jeff Layton | 5914079 | 2009-04-30 07:16:21 -0400 | [diff] [blame] | 570 | return; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 571 | } |
| 572 | |
Jeff Layton | 7d06645 | 2013-05-24 07:41:00 -0400 | [diff] [blame] | 573 | static void decode_ascii_ssetup(char **pbcc_area, __u16 bleft, |
| 574 | struct cifs_ses *ses, |
| 575 | const struct nls_table *nls_cp) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 576 | { |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 577 | int len; |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 578 | char *bcc_ptr = *pbcc_area; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 579 | |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 580 | cifs_dbg(FYI, "decode sessetup ascii. bleft %d\n", bleft); |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 581 | |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 582 | len = strnlen(bcc_ptr, bleft); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 583 | if (len >= bleft) |
Jeff Layton | 7d06645 | 2013-05-24 07:41:00 -0400 | [diff] [blame] | 584 | return; |
Steve French | 50c2f75 | 2007-07-13 00:33:32 +0000 | [diff] [blame] | 585 | |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 586 | kfree(ses->serverOS); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 587 | |
Ronnie Sahlberg | 340625e | 2019-08-27 09:30:14 +1000 | [diff] [blame] | 588 | ses->serverOS = kmalloc(len + 1, GFP_KERNEL); |
Namjae Jeon | 27b7edc | 2014-08-20 19:39:28 +0900 | [diff] [blame] | 589 | if (ses->serverOS) { |
Ronnie Sahlberg | 340625e | 2019-08-27 09:30:14 +1000 | [diff] [blame] | 590 | memcpy(ses->serverOS, bcc_ptr, len); |
| 591 | ses->serverOS[len] = 0; |
Namjae Jeon | 27b7edc | 2014-08-20 19:39:28 +0900 | [diff] [blame] | 592 | if (strncmp(ses->serverOS, "OS/2", 4) == 0) |
| 593 | cifs_dbg(FYI, "OS/2 server\n"); |
| 594 | } |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 595 | |
| 596 | bcc_ptr += len + 1; |
| 597 | bleft -= len + 1; |
| 598 | |
| 599 | len = strnlen(bcc_ptr, bleft); |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 600 | if (len >= bleft) |
Jeff Layton | 7d06645 | 2013-05-24 07:41:00 -0400 | [diff] [blame] | 601 | return; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 602 | |
Steve French | 26f5736 | 2007-08-30 22:09:15 +0000 | [diff] [blame] | 603 | kfree(ses->serverNOS); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 604 | |
Ronnie Sahlberg | 340625e | 2019-08-27 09:30:14 +1000 | [diff] [blame] | 605 | ses->serverNOS = kmalloc(len + 1, GFP_KERNEL); |
| 606 | if (ses->serverNOS) { |
| 607 | memcpy(ses->serverNOS, bcc_ptr, len); |
| 608 | ses->serverNOS[len] = 0; |
| 609 | } |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 610 | |
| 611 | bcc_ptr += len + 1; |
| 612 | bleft -= len + 1; |
| 613 | |
Steve French | 790fe57 | 2007-07-07 19:25:05 +0000 | [diff] [blame] | 614 | len = strnlen(bcc_ptr, bleft); |
| 615 | if (len > bleft) |
Jeff Layton | 7d06645 | 2013-05-24 07:41:00 -0400 | [diff] [blame] | 616 | return; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 617 | |
Steve French | 9ac00b7 | 2006-09-30 04:13:17 +0000 | [diff] [blame] | 618 | /* No domain field in LANMAN case. Domain is |
| 619 | returned by old servers in the SMB negprot response */ |
| 620 | /* BB For newer servers which do not support Unicode, |
| 621 | but thus do return domain here we could add parsing |
| 622 | for it later, but it is not very important */ |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 623 | cifs_dbg(FYI, "ascii: bytes left %d\n", bleft); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 624 | } |
| 625 | |
Pavel Shilovsky | 5478f9b | 2011-12-27 16:22:00 +0400 | [diff] [blame] | 626 | int decode_ntlmssp_challenge(char *bcc_ptr, int blob_len, |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 627 | struct cifs_ses *ses) |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 628 | { |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 629 | unsigned int tioffset; /* challenge message target info area */ |
| 630 | unsigned int tilen; /* challenge message target info area length */ |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 631 | CHALLENGE_MESSAGE *pblob = (CHALLENGE_MESSAGE *)bcc_ptr; |
Paulo Alcantara | 9de0737 | 2021-12-07 22:51:04 -0300 | [diff] [blame] | 632 | __u32 server_flags; |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 633 | |
| 634 | if (blob_len < sizeof(CHALLENGE_MESSAGE)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 635 | cifs_dbg(VFS, "challenge blob len %d too small\n", blob_len); |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 636 | return -EINVAL; |
| 637 | } |
| 638 | |
| 639 | if (memcmp(pblob->Signature, "NTLMSSP", 8)) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 640 | cifs_dbg(VFS, "blob signature incorrect %s\n", |
| 641 | pblob->Signature); |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 642 | return -EINVAL; |
| 643 | } |
| 644 | if (pblob->MessageType != NtLmChallenge) { |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 645 | cifs_dbg(VFS, "Incorrect message type %d\n", |
| 646 | pblob->MessageType); |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 647 | return -EINVAL; |
| 648 | } |
| 649 | |
Paulo Alcantara | 9de0737 | 2021-12-07 22:51:04 -0300 | [diff] [blame] | 650 | server_flags = le32_to_cpu(pblob->NegotiateFlags); |
| 651 | cifs_dbg(FYI, "%s: negotiate=0x%08x challenge=0x%08x\n", __func__, |
| 652 | ses->ntlmssp->client_flags, server_flags); |
| 653 | |
| 654 | if ((ses->ntlmssp->client_flags & (NTLMSSP_NEGOTIATE_SEAL | NTLMSSP_NEGOTIATE_SIGN)) && |
| 655 | (!(server_flags & NTLMSSP_NEGOTIATE_56) && !(server_flags & NTLMSSP_NEGOTIATE_128))) { |
| 656 | cifs_dbg(VFS, "%s: requested signing/encryption but server did not return either 56-bit or 128-bit session key size\n", |
| 657 | __func__); |
| 658 | return -EINVAL; |
| 659 | } |
| 660 | if (!(server_flags & NTLMSSP_NEGOTIATE_NTLM) && !(server_flags & NTLMSSP_NEGOTIATE_EXTENDED_SEC)) { |
| 661 | cifs_dbg(VFS, "%s: server does not seem to support either NTLMv1 or NTLMv2\n", __func__); |
| 662 | return -EINVAL; |
| 663 | } |
| 664 | if (ses->server->sign && !(server_flags & NTLMSSP_NEGOTIATE_SIGN)) { |
| 665 | cifs_dbg(VFS, "%s: forced packet signing but server does not seem to support it\n", |
| 666 | __func__); |
| 667 | return -EOPNOTSUPP; |
| 668 | } |
| 669 | if ((ses->ntlmssp->client_flags & NTLMSSP_NEGOTIATE_KEY_XCH) && |
| 670 | !(server_flags & NTLMSSP_NEGOTIATE_KEY_XCH)) |
| 671 | pr_warn_once("%s: authentication has been weakened as server does not support key exchange\n", |
| 672 | __func__); |
| 673 | |
| 674 | ses->ntlmssp->server_flags = server_flags; |
| 675 | |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 676 | memcpy(ses->ntlmssp->cryptkey, pblob->Challenge, CIFS_CRYPTO_KEY_SIZE); |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 677 | /* In particular we can examine sign flags */ |
| 678 | /* BB spec says that if AvId field of MsvAvTimestamp is populated then |
| 679 | we must set the MIC field of the AUTHENTICATE_MESSAGE */ |
Paulo Alcantara | 9de0737 | 2021-12-07 22:51:04 -0300 | [diff] [blame] | 680 | |
Steve French | 5443d13 | 2011-03-13 05:08:25 +0000 | [diff] [blame] | 681 | tioffset = le32_to_cpu(pblob->TargetInfoArray.BufferOffset); |
| 682 | tilen = le16_to_cpu(pblob->TargetInfoArray.Length); |
Dan Carpenter | 4991a5f | 2012-01-31 11:52:01 +0300 | [diff] [blame] | 683 | if (tioffset > blob_len || tioffset + tilen > blob_len) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 684 | cifs_dbg(VFS, "tioffset + tilen too high %u + %u\n", |
| 685 | tioffset, tilen); |
Dan Carpenter | 4991a5f | 2012-01-31 11:52:01 +0300 | [diff] [blame] | 686 | return -EINVAL; |
| 687 | } |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 688 | if (tilen) { |
Silviu-Mihai Popescu | f7f7c18 | 2013-03-11 18:22:32 +0200 | [diff] [blame] | 689 | ses->auth_key.response = kmemdup(bcc_ptr + tioffset, tilen, |
| 690 | GFP_KERNEL); |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 691 | if (!ses->auth_key.response) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 692 | cifs_dbg(VFS, "Challenge target info alloc failure\n"); |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 693 | return -ENOMEM; |
| 694 | } |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 695 | ses->auth_key.len = tilen; |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 696 | } |
| 697 | |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 698 | return 0; |
| 699 | } |
| 700 | |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 701 | static int size_of_ntlmssp_blob(struct cifs_ses *ses, int base_size) |
| 702 | { |
| 703 | int sz = base_size + ses->auth_key.len |
| 704 | - CIFS_SESS_KEY_SIZE + CIFS_CPHTXT_SIZE + 2; |
| 705 | |
| 706 | if (ses->domainName) |
| 707 | sz += sizeof(__le16) * strnlen(ses->domainName, CIFS_MAX_DOMAINNAME_LEN); |
| 708 | else |
| 709 | sz += sizeof(__le16); |
| 710 | |
| 711 | if (ses->user_name) |
| 712 | sz += sizeof(__le16) * strnlen(ses->user_name, CIFS_MAX_USERNAME_LEN); |
| 713 | else |
| 714 | sz += sizeof(__le16); |
| 715 | |
Ryan Bair | d3b331f | 2021-12-22 11:04:05 -0500 | [diff] [blame] | 716 | if (ses->workstation_name) |
| 717 | sz += sizeof(__le16) * strnlen(ses->workstation_name, |
| 718 | CIFS_MAX_WORKSTATION_LEN); |
| 719 | else |
| 720 | sz += sizeof(__le16); |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 721 | |
| 722 | return sz; |
| 723 | } |
| 724 | |
| 725 | static inline void cifs_security_buffer_from_str(SECURITY_BUFFER *pbuf, |
| 726 | char *str_value, |
| 727 | int str_length, |
| 728 | unsigned char *pstart, |
| 729 | unsigned char **pcur, |
| 730 | const struct nls_table *nls_cp) |
| 731 | { |
| 732 | unsigned char *tmp = pstart; |
| 733 | int len; |
| 734 | |
| 735 | if (!pbuf) |
| 736 | return; |
| 737 | |
| 738 | if (!pcur) |
| 739 | pcur = &tmp; |
| 740 | |
| 741 | if (!str_value) { |
| 742 | pbuf->BufferOffset = cpu_to_le32(*pcur - pstart); |
| 743 | pbuf->Length = 0; |
| 744 | pbuf->MaximumLength = 0; |
| 745 | *pcur += sizeof(__le16); |
| 746 | } else { |
| 747 | len = cifs_strtoUTF16((__le16 *)*pcur, |
| 748 | str_value, |
| 749 | str_length, |
| 750 | nls_cp); |
| 751 | len *= sizeof(__le16); |
| 752 | pbuf->BufferOffset = cpu_to_le32(*pcur - pstart); |
| 753 | pbuf->Length = cpu_to_le16(len); |
| 754 | pbuf->MaximumLength = cpu_to_le16(len); |
| 755 | *pcur += len; |
| 756 | } |
| 757 | } |
| 758 | |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 759 | /* BB Move to ntlmssp.c eventually */ |
| 760 | |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 761 | int build_ntlmssp_negotiate_blob(unsigned char **pbuffer, |
| 762 | u16 *buflen, |
| 763 | struct cifs_ses *ses, |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 764 | struct TCP_Server_Info *server, |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 765 | const struct nls_table *nls_cp) |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 766 | { |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 767 | int rc = 0; |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 768 | NEGOTIATE_MESSAGE *sec_blob; |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 769 | __u32 flags; |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 770 | unsigned char *tmp; |
| 771 | int len; |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 772 | |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 773 | len = size_of_ntlmssp_blob(ses, sizeof(NEGOTIATE_MESSAGE)); |
| 774 | *pbuffer = kmalloc(len, GFP_KERNEL); |
| 775 | if (!*pbuffer) { |
| 776 | rc = -ENOMEM; |
| 777 | cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc); |
| 778 | *buflen = 0; |
| 779 | goto setup_ntlm_neg_ret; |
| 780 | } |
| 781 | sec_blob = (NEGOTIATE_MESSAGE *)*pbuffer; |
| 782 | |
| 783 | memset(*pbuffer, 0, sizeof(NEGOTIATE_MESSAGE)); |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 784 | memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8); |
| 785 | sec_blob->MessageType = NtLmNegotiate; |
| 786 | |
| 787 | /* BB is NTLMV2 session security format easier to use here? */ |
| 788 | flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET | |
| 789 | NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE | |
Pavel Shilovsky | cabfb36 | 2016-11-07 18:20:50 -0800 | [diff] [blame] | 790 | NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC | |
Paulo Alcantara | 9de0737 | 2021-12-07 22:51:04 -0300 | [diff] [blame] | 791 | NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL | |
| 792 | NTLMSSP_NEGOTIATE_SIGN; |
Aurelien Aptel | f6a6bf7 | 2019-09-20 06:22:14 +0200 | [diff] [blame] | 793 | if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess) |
Pavel Shilovsky | cabfb36 | 2016-11-07 18:20:50 -0800 | [diff] [blame] | 794 | flags |= NTLMSSP_NEGOTIATE_KEY_XCH; |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 795 | |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 796 | tmp = *pbuffer + sizeof(NEGOTIATE_MESSAGE); |
Paulo Alcantara | 9de0737 | 2021-12-07 22:51:04 -0300 | [diff] [blame] | 797 | ses->ntlmssp->client_flags = flags; |
Shirish Pargaonkar | df8fbc24 | 2010-12-11 14:19:22 -0600 | [diff] [blame] | 798 | sec_blob->NegotiateFlags = cpu_to_le32(flags); |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 799 | |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 800 | /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */ |
| 801 | cifs_security_buffer_from_str(&sec_blob->DomainName, |
| 802 | NULL, |
| 803 | CIFS_MAX_DOMAINNAME_LEN, |
| 804 | *pbuffer, &tmp, |
| 805 | nls_cp); |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 806 | |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 807 | cifs_security_buffer_from_str(&sec_blob->WorkstationName, |
| 808 | NULL, |
| 809 | CIFS_MAX_WORKSTATION_LEN, |
| 810 | *pbuffer, &tmp, |
| 811 | nls_cp); |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 812 | |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 813 | *buflen = tmp - *pbuffer; |
| 814 | setup_ntlm_neg_ret: |
| 815 | return rc; |
Jerome Marchand | b8da344 | 2016-05-26 11:52:25 +0200 | [diff] [blame] | 816 | } |
| 817 | |
Steve French | 52d0053 | 2022-01-19 22:00:29 -0600 | [diff] [blame] | 818 | /* |
| 819 | * Build ntlmssp blob with additional fields, such as version, |
| 820 | * supported by modern servers. For safety limit to SMB3 or later |
| 821 | * See notes in MS-NLMP Section 2.2.2.1 e.g. |
| 822 | */ |
| 823 | int build_ntlmssp_smb3_negotiate_blob(unsigned char **pbuffer, |
| 824 | u16 *buflen, |
| 825 | struct cifs_ses *ses, |
| 826 | struct TCP_Server_Info *server, |
| 827 | const struct nls_table *nls_cp) |
| 828 | { |
| 829 | int rc = 0; |
| 830 | struct negotiate_message *sec_blob; |
| 831 | __u32 flags; |
| 832 | unsigned char *tmp; |
| 833 | int len; |
| 834 | |
| 835 | len = size_of_ntlmssp_blob(ses, sizeof(struct negotiate_message)); |
| 836 | *pbuffer = kmalloc(len, GFP_KERNEL); |
| 837 | if (!*pbuffer) { |
| 838 | rc = -ENOMEM; |
| 839 | cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc); |
| 840 | *buflen = 0; |
| 841 | goto setup_ntlm_smb3_neg_ret; |
| 842 | } |
| 843 | sec_blob = (struct negotiate_message *)*pbuffer; |
| 844 | |
| 845 | memset(*pbuffer, 0, sizeof(struct negotiate_message)); |
| 846 | memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8); |
| 847 | sec_blob->MessageType = NtLmNegotiate; |
| 848 | |
| 849 | /* BB is NTLMV2 session security format easier to use here? */ |
| 850 | flags = NTLMSSP_NEGOTIATE_56 | NTLMSSP_REQUEST_TARGET | |
| 851 | NTLMSSP_NEGOTIATE_128 | NTLMSSP_NEGOTIATE_UNICODE | |
| 852 | NTLMSSP_NEGOTIATE_NTLM | NTLMSSP_NEGOTIATE_EXTENDED_SEC | |
| 853 | NTLMSSP_NEGOTIATE_ALWAYS_SIGN | NTLMSSP_NEGOTIATE_SEAL | |
| 854 | NTLMSSP_NEGOTIATE_SIGN | NTLMSSP_NEGOTIATE_VERSION; |
| 855 | if (!server->session_estab || ses->ntlmssp->sesskey_per_smbsess) |
| 856 | flags |= NTLMSSP_NEGOTIATE_KEY_XCH; |
| 857 | |
| 858 | sec_blob->Version.ProductMajorVersion = LINUX_VERSION_MAJOR; |
| 859 | sec_blob->Version.ProductMinorVersion = LINUX_VERSION_PATCHLEVEL; |
| 860 | sec_blob->Version.ProductBuild = cpu_to_le16(SMB3_PRODUCT_BUILD); |
| 861 | sec_blob->Version.NTLMRevisionCurrent = NTLMSSP_REVISION_W2K3; |
| 862 | |
| 863 | tmp = *pbuffer + sizeof(struct negotiate_message); |
| 864 | ses->ntlmssp->client_flags = flags; |
| 865 | sec_blob->NegotiateFlags = cpu_to_le32(flags); |
| 866 | |
| 867 | /* these fields should be null in negotiate phase MS-NLMP 3.1.5.1.1 */ |
| 868 | cifs_security_buffer_from_str(&sec_blob->DomainName, |
| 869 | NULL, |
| 870 | CIFS_MAX_DOMAINNAME_LEN, |
| 871 | *pbuffer, &tmp, |
| 872 | nls_cp); |
| 873 | |
| 874 | cifs_security_buffer_from_str(&sec_blob->WorkstationName, |
| 875 | NULL, |
| 876 | CIFS_MAX_WORKSTATION_LEN, |
| 877 | *pbuffer, &tmp, |
| 878 | nls_cp); |
| 879 | |
| 880 | *buflen = tmp - *pbuffer; |
| 881 | setup_ntlm_smb3_neg_ret: |
| 882 | return rc; |
| 883 | } |
| 884 | |
| 885 | |
Jerome Marchand | b8da344 | 2016-05-26 11:52:25 +0200 | [diff] [blame] | 886 | int build_ntlmssp_auth_blob(unsigned char **pbuffer, |
Shirish Pargaonkar | 89f150f | 2010-10-19 11:47:52 -0500 | [diff] [blame] | 887 | u16 *buflen, |
Steve French | 96daf2b | 2011-05-27 04:34:02 +0000 | [diff] [blame] | 888 | struct cifs_ses *ses, |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 889 | struct TCP_Server_Info *server, |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 890 | const struct nls_table *nls_cp) |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 891 | { |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 892 | int rc; |
Jerome Marchand | b8da344 | 2016-05-26 11:52:25 +0200 | [diff] [blame] | 893 | AUTHENTICATE_MESSAGE *sec_blob; |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 894 | __u32 flags; |
| 895 | unsigned char *tmp; |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 896 | int len; |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 897 | |
Jerome Marchand | b8da344 | 2016-05-26 11:52:25 +0200 | [diff] [blame] | 898 | rc = setup_ntlmv2_rsp(ses, nls_cp); |
| 899 | if (rc) { |
| 900 | cifs_dbg(VFS, "Error %d during NTLMSSP authentication\n", rc); |
| 901 | *buflen = 0; |
| 902 | goto setup_ntlmv2_ret; |
| 903 | } |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 904 | |
| 905 | len = size_of_ntlmssp_blob(ses, sizeof(AUTHENTICATE_MESSAGE)); |
| 906 | *pbuffer = kmalloc(len, GFP_KERNEL); |
Nicholas Mc Guire | 126c97f | 2018-08-23 12:24:02 +0200 | [diff] [blame] | 907 | if (!*pbuffer) { |
| 908 | rc = -ENOMEM; |
| 909 | cifs_dbg(VFS, "Error %d during NTLMSSP allocation\n", rc); |
| 910 | *buflen = 0; |
| 911 | goto setup_ntlmv2_ret; |
| 912 | } |
Jerome Marchand | b8da344 | 2016-05-26 11:52:25 +0200 | [diff] [blame] | 913 | sec_blob = (AUTHENTICATE_MESSAGE *)*pbuffer; |
| 914 | |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 915 | memcpy(sec_blob->Signature, NTLMSSP_SIGNATURE, 8); |
| 916 | sec_blob->MessageType = NtLmAuthenticate; |
| 917 | |
Paulo Alcantara | 9de0737 | 2021-12-07 22:51:04 -0300 | [diff] [blame] | 918 | flags = ses->ntlmssp->server_flags | NTLMSSP_REQUEST_TARGET | |
| 919 | NTLMSSP_NEGOTIATE_TARGET_INFO | NTLMSSP_NEGOTIATE_WORKSTATION_SUPPLIED; |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 920 | |
Jerome Marchand | b8da344 | 2016-05-26 11:52:25 +0200 | [diff] [blame] | 921 | tmp = *pbuffer + sizeof(AUTHENTICATE_MESSAGE); |
Shirish Pargaonkar | df8fbc24 | 2010-12-11 14:19:22 -0600 | [diff] [blame] | 922 | sec_blob->NegotiateFlags = cpu_to_le32(flags); |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 923 | |
| 924 | sec_blob->LmChallengeResponse.BufferOffset = |
| 925 | cpu_to_le32(sizeof(AUTHENTICATE_MESSAGE)); |
| 926 | sec_blob->LmChallengeResponse.Length = 0; |
| 927 | sec_blob->LmChallengeResponse.MaximumLength = 0; |
| 928 | |
Jerome Marchand | b8da344 | 2016-05-26 11:52:25 +0200 | [diff] [blame] | 929 | sec_blob->NtChallengeResponse.BufferOffset = |
| 930 | cpu_to_le32(tmp - *pbuffer); |
Stefan Metzmacher | cfda35d | 2016-05-03 10:52:30 +0200 | [diff] [blame] | 931 | if (ses->user_name != NULL) { |
Stefan Metzmacher | cfda35d | 2016-05-03 10:52:30 +0200 | [diff] [blame] | 932 | memcpy(tmp, ses->auth_key.response + CIFS_SESS_KEY_SIZE, |
| 933 | ses->auth_key.len - CIFS_SESS_KEY_SIZE); |
| 934 | tmp += ses->auth_key.len - CIFS_SESS_KEY_SIZE; |
Steve French | c8e56f1 | 2010-09-08 21:10:58 +0000 | [diff] [blame] | 935 | |
Stefan Metzmacher | cfda35d | 2016-05-03 10:52:30 +0200 | [diff] [blame] | 936 | sec_blob->NtChallengeResponse.Length = |
| 937 | cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE); |
| 938 | sec_blob->NtChallengeResponse.MaximumLength = |
| 939 | cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE); |
| 940 | } else { |
| 941 | /* |
| 942 | * don't send an NT Response for anonymous access |
| 943 | */ |
| 944 | sec_blob->NtChallengeResponse.Length = 0; |
| 945 | sec_blob->NtChallengeResponse.MaximumLength = 0; |
| 946 | } |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 947 | |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 948 | cifs_security_buffer_from_str(&sec_blob->DomainName, |
| 949 | ses->domainName, |
| 950 | CIFS_MAX_DOMAINNAME_LEN, |
| 951 | *pbuffer, &tmp, |
| 952 | nls_cp); |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 953 | |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 954 | cifs_security_buffer_from_str(&sec_blob->UserName, |
| 955 | ses->user_name, |
| 956 | CIFS_MAX_USERNAME_LEN, |
| 957 | *pbuffer, &tmp, |
| 958 | nls_cp); |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 959 | |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 960 | cifs_security_buffer_from_str(&sec_blob->WorkstationName, |
| 961 | ses->workstation_name, |
| 962 | CIFS_MAX_WORKSTATION_LEN, |
| 963 | *pbuffer, &tmp, |
| 964 | nls_cp); |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 965 | |
Paulo Alcantara | 9de0737 | 2021-12-07 22:51:04 -0300 | [diff] [blame] | 966 | if ((ses->ntlmssp->server_flags & NTLMSSP_NEGOTIATE_KEY_XCH) && |
| 967 | (!ses->server->session_estab || ses->ntlmssp->sesskey_per_smbsess) && |
| 968 | !calc_seckey(ses)) { |
Shirish Pargaonkar | d3686d5 | 2010-10-28 09:53:07 -0500 | [diff] [blame] | 969 | memcpy(tmp, ses->ntlmssp->ciphertext, CIFS_CPHTXT_SIZE); |
Jerome Marchand | b8da344 | 2016-05-26 11:52:25 +0200 | [diff] [blame] | 970 | sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer); |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 971 | sec_blob->SessionKey.Length = cpu_to_le16(CIFS_CPHTXT_SIZE); |
| 972 | sec_blob->SessionKey.MaximumLength = |
| 973 | cpu_to_le16(CIFS_CPHTXT_SIZE); |
| 974 | tmp += CIFS_CPHTXT_SIZE; |
| 975 | } else { |
Jerome Marchand | b8da344 | 2016-05-26 11:52:25 +0200 | [diff] [blame] | 976 | sec_blob->SessionKey.BufferOffset = cpu_to_le32(tmp - *pbuffer); |
Shirish Pargaonkar | d2b9152 | 2010-10-21 14:25:08 -0500 | [diff] [blame] | 977 | sec_blob->SessionKey.Length = 0; |
| 978 | sec_blob->SessionKey.MaximumLength = 0; |
| 979 | } |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 980 | |
Jerome Marchand | b8da344 | 2016-05-26 11:52:25 +0200 | [diff] [blame] | 981 | *buflen = tmp - *pbuffer; |
Shirish Pargaonkar | 2b149f1 | 2010-09-18 22:02:18 -0500 | [diff] [blame] | 982 | setup_ntlmv2_ret: |
Shirish Pargaonkar | 89f150f | 2010-10-19 11:47:52 -0500 | [diff] [blame] | 983 | return rc; |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 984 | } |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 985 | |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 986 | enum securityEnum |
Sachin Prabhu | ef65aae | 2017-01-18 15:35:57 +0530 | [diff] [blame] | 987 | cifs_select_sectype(struct TCP_Server_Info *server, enum securityEnum requested) |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 988 | { |
| 989 | switch (server->negflavor) { |
| 990 | case CIFS_NEGFLAVOR_EXTENDED: |
| 991 | switch (requested) { |
| 992 | case Kerberos: |
| 993 | case RawNTLMSSP: |
| 994 | return requested; |
| 995 | case Unspecified: |
| 996 | if (server->sec_ntlmssp && |
| 997 | (global_secflags & CIFSSEC_MAY_NTLMSSP)) |
| 998 | return RawNTLMSSP; |
| 999 | if ((server->sec_kerberos || server->sec_mskerberos) && |
| 1000 | (global_secflags & CIFSSEC_MAY_KRB5)) |
| 1001 | return Kerberos; |
Gustavo A. R. Silva | df561f66 | 2020-08-23 17:36:59 -0500 | [diff] [blame] | 1002 | fallthrough; |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 1003 | default: |
| 1004 | return Unspecified; |
| 1005 | } |
| 1006 | case CIFS_NEGFLAVOR_UNENCAP: |
| 1007 | switch (requested) { |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 1008 | case NTLMv2: |
| 1009 | return requested; |
| 1010 | case Unspecified: |
| 1011 | if (global_secflags & CIFSSEC_MAY_NTLMV2) |
| 1012 | return NTLMv2; |
Gustavo A. R. Silva | 21ac58f | 2020-11-20 12:24:14 -0600 | [diff] [blame] | 1013 | break; |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 1014 | default: |
Sachin Prabhu | dde2356 | 2013-09-27 18:35:42 +0100 | [diff] [blame] | 1015 | break; |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 1016 | } |
Ronnie Sahlberg | 76a3c92 | 2021-08-19 20:34:58 +1000 | [diff] [blame] | 1017 | fallthrough; |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 1018 | default: |
| 1019 | return Unspecified; |
| 1020 | } |
| 1021 | } |
| 1022 | |
Sachin Prabhu | 80a0e63 | 2014-06-16 15:35:25 +0100 | [diff] [blame] | 1023 | struct sess_data { |
| 1024 | unsigned int xid; |
| 1025 | struct cifs_ses *ses; |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1026 | struct TCP_Server_Info *server; |
Sachin Prabhu | 80a0e63 | 2014-06-16 15:35:25 +0100 | [diff] [blame] | 1027 | struct nls_table *nls_cp; |
| 1028 | void (*func)(struct sess_data *); |
| 1029 | int result; |
| 1030 | |
| 1031 | /* we will send the SMB in three pieces: |
| 1032 | * a fixed length beginning part, an optional |
| 1033 | * SPNEGO blob (which can be zero length), and a |
| 1034 | * last part which will include the strings |
| 1035 | * and rest of bcc area. This allows us to avoid |
| 1036 | * a large buffer 17K allocation |
| 1037 | */ |
| 1038 | int buf0_type; |
| 1039 | struct kvec iov[3]; |
| 1040 | }; |
| 1041 | |
| 1042 | static int |
| 1043 | sess_alloc_buffer(struct sess_data *sess_data, int wct) |
| 1044 | { |
| 1045 | int rc; |
| 1046 | struct cifs_ses *ses = sess_data->ses; |
| 1047 | struct smb_hdr *smb_buf; |
| 1048 | |
| 1049 | rc = small_smb_init_no_tc(SMB_COM_SESSION_SETUP_ANDX, wct, ses, |
| 1050 | (void **)&smb_buf); |
| 1051 | |
| 1052 | if (rc) |
| 1053 | return rc; |
| 1054 | |
| 1055 | sess_data->iov[0].iov_base = (char *)smb_buf; |
| 1056 | sess_data->iov[0].iov_len = be32_to_cpu(smb_buf->smb_buf_length) + 4; |
| 1057 | /* |
| 1058 | * This variable will be used to clear the buffer |
| 1059 | * allocated above in case of any error in the calling function. |
| 1060 | */ |
| 1061 | sess_data->buf0_type = CIFS_SMALL_BUFFER; |
| 1062 | |
| 1063 | /* 2000 big enough to fit max user, domain, NOS name etc. */ |
| 1064 | sess_data->iov[2].iov_base = kmalloc(2000, GFP_KERNEL); |
| 1065 | if (!sess_data->iov[2].iov_base) { |
| 1066 | rc = -ENOMEM; |
| 1067 | goto out_free_smb_buf; |
| 1068 | } |
| 1069 | |
| 1070 | return 0; |
| 1071 | |
| 1072 | out_free_smb_buf: |
Ding Hui | d72c741 | 2021-08-17 22:55:10 +0800 | [diff] [blame] | 1073 | cifs_small_buf_release(smb_buf); |
Sachin Prabhu | 80a0e63 | 2014-06-16 15:35:25 +0100 | [diff] [blame] | 1074 | sess_data->iov[0].iov_base = NULL; |
| 1075 | sess_data->iov[0].iov_len = 0; |
| 1076 | sess_data->buf0_type = CIFS_NO_BUFFER; |
| 1077 | return rc; |
| 1078 | } |
| 1079 | |
| 1080 | static void |
| 1081 | sess_free_buffer(struct sess_data *sess_data) |
| 1082 | { |
| 1083 | |
| 1084 | free_rsp_buf(sess_data->buf0_type, sess_data->iov[0].iov_base); |
| 1085 | sess_data->buf0_type = CIFS_NO_BUFFER; |
| 1086 | kfree(sess_data->iov[2].iov_base); |
| 1087 | } |
| 1088 | |
| 1089 | static int |
| 1090 | sess_establish_session(struct sess_data *sess_data) |
| 1091 | { |
| 1092 | struct cifs_ses *ses = sess_data->ses; |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1093 | struct TCP_Server_Info *server = sess_data->server; |
Sachin Prabhu | 80a0e63 | 2014-06-16 15:35:25 +0100 | [diff] [blame] | 1094 | |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1095 | mutex_lock(&server->srv_mutex); |
| 1096 | if (!server->session_estab) { |
| 1097 | if (server->sign) { |
| 1098 | server->session_key.response = |
Sachin Prabhu | 80a0e63 | 2014-06-16 15:35:25 +0100 | [diff] [blame] | 1099 | kmemdup(ses->auth_key.response, |
| 1100 | ses->auth_key.len, GFP_KERNEL); |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1101 | if (!server->session_key.response) { |
| 1102 | mutex_unlock(&server->srv_mutex); |
Sachin Prabhu | 80a0e63 | 2014-06-16 15:35:25 +0100 | [diff] [blame] | 1103 | return -ENOMEM; |
| 1104 | } |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1105 | server->session_key.len = |
Sachin Prabhu | 80a0e63 | 2014-06-16 15:35:25 +0100 | [diff] [blame] | 1106 | ses->auth_key.len; |
| 1107 | } |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1108 | server->sequence_number = 0x2; |
| 1109 | server->session_estab = true; |
Sachin Prabhu | 80a0e63 | 2014-06-16 15:35:25 +0100 | [diff] [blame] | 1110 | } |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1111 | mutex_unlock(&server->srv_mutex); |
Sachin Prabhu | 80a0e63 | 2014-06-16 15:35:25 +0100 | [diff] [blame] | 1112 | |
| 1113 | cifs_dbg(FYI, "CIFS session established successfully\n"); |
Sachin Prabhu | 80a0e63 | 2014-06-16 15:35:25 +0100 | [diff] [blame] | 1114 | return 0; |
| 1115 | } |
| 1116 | |
| 1117 | static int |
| 1118 | sess_sendreceive(struct sess_data *sess_data) |
| 1119 | { |
| 1120 | int rc; |
| 1121 | struct smb_hdr *smb_buf = (struct smb_hdr *) sess_data->iov[0].iov_base; |
| 1122 | __u16 count; |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 1123 | struct kvec rsp_iov = { NULL, 0 }; |
Sachin Prabhu | 80a0e63 | 2014-06-16 15:35:25 +0100 | [diff] [blame] | 1124 | |
| 1125 | count = sess_data->iov[1].iov_len + sess_data->iov[2].iov_len; |
Qinglang Miao | 1a0e7f7 | 2020-07-25 16:56:01 +0800 | [diff] [blame] | 1126 | be32_add_cpu(&smb_buf->smb_buf_length, count); |
Sachin Prabhu | 80a0e63 | 2014-06-16 15:35:25 +0100 | [diff] [blame] | 1127 | put_bcc(count, smb_buf); |
| 1128 | |
| 1129 | rc = SendReceive2(sess_data->xid, sess_data->ses, |
| 1130 | sess_data->iov, 3 /* num_iovecs */, |
| 1131 | &sess_data->buf0_type, |
Pavel Shilovsky | da502f7 | 2016-10-25 11:38:47 -0700 | [diff] [blame] | 1132 | CIFS_LOG_ERROR, &rsp_iov); |
| 1133 | cifs_small_buf_release(sess_data->iov[0].iov_base); |
| 1134 | memcpy(&sess_data->iov[0], &rsp_iov, sizeof(struct kvec)); |
Sachin Prabhu | 80a0e63 | 2014-06-16 15:35:25 +0100 | [diff] [blame] | 1135 | |
| 1136 | return rc; |
| 1137 | } |
| 1138 | |
Sachin Prabhu | 583cf7a | 2014-06-16 15:35:26 +0100 | [diff] [blame] | 1139 | static void |
| 1140 | sess_auth_ntlmv2(struct sess_data *sess_data) |
| 1141 | { |
| 1142 | int rc = 0; |
| 1143 | struct smb_hdr *smb_buf; |
| 1144 | SESSION_SETUP_ANDX *pSMB; |
| 1145 | char *bcc_ptr; |
| 1146 | struct cifs_ses *ses = sess_data->ses; |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1147 | struct TCP_Server_Info *server = sess_data->server; |
Sachin Prabhu | 583cf7a | 2014-06-16 15:35:26 +0100 | [diff] [blame] | 1148 | __u32 capabilities; |
| 1149 | __u16 bytes_remaining; |
| 1150 | |
| 1151 | /* old style NTLM sessionsetup */ |
| 1152 | /* wct = 13 */ |
| 1153 | rc = sess_alloc_buffer(sess_data, 13); |
| 1154 | if (rc) |
| 1155 | goto out; |
| 1156 | |
| 1157 | pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base; |
| 1158 | bcc_ptr = sess_data->iov[2].iov_base; |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1159 | capabilities = cifs_ssetup_hdr(ses, server, pSMB); |
Sachin Prabhu | 583cf7a | 2014-06-16 15:35:26 +0100 | [diff] [blame] | 1160 | |
| 1161 | pSMB->req_no_secext.Capabilities = cpu_to_le32(capabilities); |
| 1162 | |
| 1163 | /* LM2 password would be here if we supported it */ |
| 1164 | pSMB->req_no_secext.CaseInsensitivePasswordLength = 0; |
| 1165 | |
Stefan Metzmacher | 1a967d6 | 2016-05-03 10:52:30 +0200 | [diff] [blame] | 1166 | if (ses->user_name != NULL) { |
| 1167 | /* calculate nlmv2 response and session key */ |
| 1168 | rc = setup_ntlmv2_rsp(ses, sess_data->nls_cp); |
| 1169 | if (rc) { |
| 1170 | cifs_dbg(VFS, "Error %d during NTLMv2 authentication\n", rc); |
| 1171 | goto out; |
| 1172 | } |
| 1173 | |
| 1174 | memcpy(bcc_ptr, ses->auth_key.response + CIFS_SESS_KEY_SIZE, |
| 1175 | ses->auth_key.len - CIFS_SESS_KEY_SIZE); |
| 1176 | bcc_ptr += ses->auth_key.len - CIFS_SESS_KEY_SIZE; |
| 1177 | |
| 1178 | /* set case sensitive password length after tilen may get |
| 1179 | * assigned, tilen is 0 otherwise. |
| 1180 | */ |
| 1181 | pSMB->req_no_secext.CaseSensitivePasswordLength = |
| 1182 | cpu_to_le16(ses->auth_key.len - CIFS_SESS_KEY_SIZE); |
| 1183 | } else { |
| 1184 | pSMB->req_no_secext.CaseSensitivePasswordLength = 0; |
Sachin Prabhu | 583cf7a | 2014-06-16 15:35:26 +0100 | [diff] [blame] | 1185 | } |
| 1186 | |
Sachin Prabhu | 583cf7a | 2014-06-16 15:35:26 +0100 | [diff] [blame] | 1187 | if (ses->capabilities & CAP_UNICODE) { |
| 1188 | if (sess_data->iov[0].iov_len % 2) { |
| 1189 | *bcc_ptr = 0; |
| 1190 | bcc_ptr++; |
| 1191 | } |
| 1192 | unicode_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp); |
| 1193 | } else { |
| 1194 | ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp); |
| 1195 | } |
| 1196 | |
| 1197 | |
| 1198 | sess_data->iov[2].iov_len = (long) bcc_ptr - |
| 1199 | (long) sess_data->iov[2].iov_base; |
| 1200 | |
| 1201 | rc = sess_sendreceive(sess_data); |
| 1202 | if (rc) |
| 1203 | goto out; |
| 1204 | |
| 1205 | pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base; |
| 1206 | smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base; |
| 1207 | |
| 1208 | if (smb_buf->WordCount != 3) { |
| 1209 | rc = -EIO; |
| 1210 | cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount); |
| 1211 | goto out; |
| 1212 | } |
| 1213 | |
| 1214 | if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN) |
| 1215 | cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */ |
| 1216 | |
| 1217 | ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */ |
| 1218 | cifs_dbg(FYI, "UID = %llu\n", ses->Suid); |
| 1219 | |
| 1220 | bytes_remaining = get_bcc(smb_buf); |
| 1221 | bcc_ptr = pByteArea(smb_buf); |
| 1222 | |
| 1223 | /* BB check if Unicode and decode strings */ |
| 1224 | if (bytes_remaining == 0) { |
| 1225 | /* no string area to decode, do nothing */ |
| 1226 | } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) { |
| 1227 | /* unicode string area must be word-aligned */ |
| 1228 | if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) { |
| 1229 | ++bcc_ptr; |
| 1230 | --bytes_remaining; |
| 1231 | } |
| 1232 | decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses, |
| 1233 | sess_data->nls_cp); |
| 1234 | } else { |
| 1235 | decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses, |
| 1236 | sess_data->nls_cp); |
| 1237 | } |
| 1238 | |
| 1239 | rc = sess_establish_session(sess_data); |
| 1240 | out: |
| 1241 | sess_data->result = rc; |
| 1242 | sess_data->func = NULL; |
| 1243 | sess_free_buffer(sess_data); |
| 1244 | kfree(ses->auth_key.response); |
| 1245 | ses->auth_key.response = NULL; |
| 1246 | } |
| 1247 | |
Sachin Prabhu | ee03c64 | 2014-06-16 15:35:27 +0100 | [diff] [blame] | 1248 | #ifdef CONFIG_CIFS_UPCALL |
| 1249 | static void |
| 1250 | sess_auth_kerberos(struct sess_data *sess_data) |
| 1251 | { |
| 1252 | int rc = 0; |
| 1253 | struct smb_hdr *smb_buf; |
| 1254 | SESSION_SETUP_ANDX *pSMB; |
| 1255 | char *bcc_ptr; |
| 1256 | struct cifs_ses *ses = sess_data->ses; |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1257 | struct TCP_Server_Info *server = sess_data->server; |
Sachin Prabhu | ee03c64 | 2014-06-16 15:35:27 +0100 | [diff] [blame] | 1258 | __u32 capabilities; |
| 1259 | __u16 bytes_remaining; |
| 1260 | struct key *spnego_key = NULL; |
| 1261 | struct cifs_spnego_msg *msg; |
| 1262 | u16 blob_len; |
| 1263 | |
| 1264 | /* extended security */ |
| 1265 | /* wct = 12 */ |
| 1266 | rc = sess_alloc_buffer(sess_data, 12); |
| 1267 | if (rc) |
| 1268 | goto out; |
| 1269 | |
| 1270 | pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base; |
| 1271 | bcc_ptr = sess_data->iov[2].iov_base; |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1272 | capabilities = cifs_ssetup_hdr(ses, server, pSMB); |
Sachin Prabhu | ee03c64 | 2014-06-16 15:35:27 +0100 | [diff] [blame] | 1273 | |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1274 | spnego_key = cifs_get_spnego_key(ses, server); |
Sachin Prabhu | ee03c64 | 2014-06-16 15:35:27 +0100 | [diff] [blame] | 1275 | if (IS_ERR(spnego_key)) { |
| 1276 | rc = PTR_ERR(spnego_key); |
| 1277 | spnego_key = NULL; |
| 1278 | goto out; |
| 1279 | } |
| 1280 | |
David Howells | 146aa8b | 2015-10-21 14:04:48 +0100 | [diff] [blame] | 1281 | msg = spnego_key->payload.data[0]; |
Sachin Prabhu | ee03c64 | 2014-06-16 15:35:27 +0100 | [diff] [blame] | 1282 | /* |
| 1283 | * check version field to make sure that cifs.upcall is |
| 1284 | * sending us a response in an expected form |
| 1285 | */ |
| 1286 | if (msg->version != CIFS_SPNEGO_UPCALL_VERSION) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 1287 | cifs_dbg(VFS, "incorrect version of cifs.upcall (expected %d but got %d)\n", |
| 1288 | CIFS_SPNEGO_UPCALL_VERSION, msg->version); |
Sachin Prabhu | ee03c64 | 2014-06-16 15:35:27 +0100 | [diff] [blame] | 1289 | rc = -EKEYREJECTED; |
| 1290 | goto out_put_spnego_key; |
| 1291 | } |
| 1292 | |
| 1293 | ses->auth_key.response = kmemdup(msg->data, msg->sesskey_len, |
| 1294 | GFP_KERNEL); |
| 1295 | if (!ses->auth_key.response) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 1296 | cifs_dbg(VFS, "Kerberos can't allocate (%u bytes) memory\n", |
| 1297 | msg->sesskey_len); |
Sachin Prabhu | ee03c64 | 2014-06-16 15:35:27 +0100 | [diff] [blame] | 1298 | rc = -ENOMEM; |
| 1299 | goto out_put_spnego_key; |
| 1300 | } |
| 1301 | ses->auth_key.len = msg->sesskey_len; |
| 1302 | |
| 1303 | pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC; |
| 1304 | capabilities |= CAP_EXTENDED_SECURITY; |
| 1305 | pSMB->req.Capabilities = cpu_to_le32(capabilities); |
| 1306 | sess_data->iov[1].iov_base = msg->data + msg->sesskey_len; |
| 1307 | sess_data->iov[1].iov_len = msg->secblob_len; |
| 1308 | pSMB->req.SecurityBlobLength = cpu_to_le16(sess_data->iov[1].iov_len); |
| 1309 | |
| 1310 | if (ses->capabilities & CAP_UNICODE) { |
| 1311 | /* unicode strings must be word aligned */ |
| 1312 | if ((sess_data->iov[0].iov_len |
| 1313 | + sess_data->iov[1].iov_len) % 2) { |
| 1314 | *bcc_ptr = 0; |
| 1315 | bcc_ptr++; |
| 1316 | } |
| 1317 | unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp); |
| 1318 | unicode_domain_string(&bcc_ptr, ses, sess_data->nls_cp); |
| 1319 | } else { |
| 1320 | /* BB: is this right? */ |
| 1321 | ascii_ssetup_strings(&bcc_ptr, ses, sess_data->nls_cp); |
| 1322 | } |
| 1323 | |
| 1324 | sess_data->iov[2].iov_len = (long) bcc_ptr - |
| 1325 | (long) sess_data->iov[2].iov_base; |
| 1326 | |
| 1327 | rc = sess_sendreceive(sess_data); |
| 1328 | if (rc) |
| 1329 | goto out_put_spnego_key; |
| 1330 | |
| 1331 | pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base; |
| 1332 | smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base; |
| 1333 | |
| 1334 | if (smb_buf->WordCount != 4) { |
| 1335 | rc = -EIO; |
| 1336 | cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount); |
| 1337 | goto out_put_spnego_key; |
| 1338 | } |
| 1339 | |
| 1340 | if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN) |
| 1341 | cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */ |
| 1342 | |
| 1343 | ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */ |
| 1344 | cifs_dbg(FYI, "UID = %llu\n", ses->Suid); |
| 1345 | |
| 1346 | bytes_remaining = get_bcc(smb_buf); |
| 1347 | bcc_ptr = pByteArea(smb_buf); |
| 1348 | |
| 1349 | blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength); |
| 1350 | if (blob_len > bytes_remaining) { |
| 1351 | cifs_dbg(VFS, "bad security blob length %d\n", |
| 1352 | blob_len); |
| 1353 | rc = -EINVAL; |
| 1354 | goto out_put_spnego_key; |
| 1355 | } |
| 1356 | bcc_ptr += blob_len; |
| 1357 | bytes_remaining -= blob_len; |
| 1358 | |
| 1359 | /* BB check if Unicode and decode strings */ |
| 1360 | if (bytes_remaining == 0) { |
| 1361 | /* no string area to decode, do nothing */ |
| 1362 | } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) { |
| 1363 | /* unicode string area must be word-aligned */ |
| 1364 | if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) { |
| 1365 | ++bcc_ptr; |
| 1366 | --bytes_remaining; |
| 1367 | } |
| 1368 | decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses, |
| 1369 | sess_data->nls_cp); |
| 1370 | } else { |
| 1371 | decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses, |
| 1372 | sess_data->nls_cp); |
| 1373 | } |
| 1374 | |
| 1375 | rc = sess_establish_session(sess_data); |
| 1376 | out_put_spnego_key: |
| 1377 | key_invalidate(spnego_key); |
| 1378 | key_put(spnego_key); |
| 1379 | out: |
| 1380 | sess_data->result = rc; |
| 1381 | sess_data->func = NULL; |
| 1382 | sess_free_buffer(sess_data); |
| 1383 | kfree(ses->auth_key.response); |
| 1384 | ses->auth_key.response = NULL; |
| 1385 | } |
| 1386 | |
Sachin Prabhu | ee03c64 | 2014-06-16 15:35:27 +0100 | [diff] [blame] | 1387 | #endif /* ! CONFIG_CIFS_UPCALL */ |
Sachin Prabhu | 583cf7a | 2014-06-16 15:35:26 +0100 | [diff] [blame] | 1388 | |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1389 | /* |
| 1390 | * The required kvec buffers have to be allocated before calling this |
| 1391 | * function. |
| 1392 | */ |
| 1393 | static int |
| 1394 | _sess_auth_rawntlmssp_assemble_req(struct sess_data *sess_data) |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 1395 | { |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 1396 | SESSION_SETUP_ANDX *pSMB; |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1397 | struct cifs_ses *ses = sess_data->ses; |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1398 | struct TCP_Server_Info *server = sess_data->server; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 1399 | __u32 capabilities; |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1400 | char *bcc_ptr; |
Steve French | 254e55e | 2006-06-04 05:53:15 +0000 | [diff] [blame] | 1401 | |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1402 | pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base; |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1403 | |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1404 | capabilities = cifs_ssetup_hdr(ses, server, pSMB); |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1405 | if ((pSMB->req.hdr.Flags2 & SMBFLG2_UNICODE) == 0) { |
| 1406 | cifs_dbg(VFS, "NTLMSSP requires Unicode support\n"); |
| 1407 | return -ENOSYS; |
Jeff Layton | 3534b85 | 2013-05-24 07:41:01 -0400 | [diff] [blame] | 1408 | } |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 1409 | |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1410 | pSMB->req.hdr.Flags2 |= SMBFLG2_EXT_SEC; |
| 1411 | capabilities |= CAP_EXTENDED_SECURITY; |
| 1412 | pSMB->req.Capabilities |= cpu_to_le32(capabilities); |
| 1413 | |
| 1414 | bcc_ptr = sess_data->iov[2].iov_base; |
| 1415 | /* unicode strings must be word aligned */ |
| 1416 | if ((sess_data->iov[0].iov_len + sess_data->iov[1].iov_len) % 2) { |
| 1417 | *bcc_ptr = 0; |
| 1418 | bcc_ptr++; |
| 1419 | } |
| 1420 | unicode_oslm_strings(&bcc_ptr, sess_data->nls_cp); |
| 1421 | |
| 1422 | sess_data->iov[2].iov_len = (long) bcc_ptr - |
| 1423 | (long) sess_data->iov[2].iov_base; |
| 1424 | |
| 1425 | return 0; |
| 1426 | } |
| 1427 | |
| 1428 | static void |
| 1429 | sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data); |
| 1430 | |
| 1431 | static void |
| 1432 | sess_auth_rawntlmssp_negotiate(struct sess_data *sess_data) |
| 1433 | { |
| 1434 | int rc; |
| 1435 | struct smb_hdr *smb_buf; |
| 1436 | SESSION_SETUP_ANDX *pSMB; |
| 1437 | struct cifs_ses *ses = sess_data->ses; |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1438 | struct TCP_Server_Info *server = sess_data->server; |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1439 | __u16 bytes_remaining; |
| 1440 | char *bcc_ptr; |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 1441 | unsigned char *ntlmsspblob = NULL; |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1442 | u16 blob_len; |
| 1443 | |
| 1444 | cifs_dbg(FYI, "rawntlmssp session setup negotiate phase\n"); |
| 1445 | |
| 1446 | /* |
| 1447 | * if memory allocation is successful, caller of this function |
| 1448 | * frees it. |
| 1449 | */ |
| 1450 | ses->ntlmssp = kmalloc(sizeof(struct ntlmssp_auth), GFP_KERNEL); |
| 1451 | if (!ses->ntlmssp) { |
| 1452 | rc = -ENOMEM; |
| 1453 | goto out; |
| 1454 | } |
| 1455 | ses->ntlmssp->sesskey_per_smbsess = false; |
| 1456 | |
| 1457 | /* wct = 12 */ |
| 1458 | rc = sess_alloc_buffer(sess_data, 12); |
| 1459 | if (rc) |
| 1460 | goto out; |
| 1461 | |
| 1462 | pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base; |
| 1463 | |
| 1464 | /* Build security blob before we assemble the request */ |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 1465 | rc = build_ntlmssp_negotiate_blob(&ntlmsspblob, |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1466 | &blob_len, ses, server, |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 1467 | sess_data->nls_cp); |
| 1468 | if (rc) |
Shyam Prasad N | e3548aa | 2022-01-17 00:20:47 -0600 | [diff] [blame] | 1469 | goto out_free_ntlmsspblob; |
Shyam Prasad N | 49bd49f | 2021-11-05 19:03:57 +0000 | [diff] [blame] | 1470 | |
| 1471 | sess_data->iov[1].iov_len = blob_len; |
| 1472 | sess_data->iov[1].iov_base = ntlmsspblob; |
| 1473 | pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len); |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1474 | |
| 1475 | rc = _sess_auth_rawntlmssp_assemble_req(sess_data); |
| 1476 | if (rc) |
Shyam Prasad N | e3548aa | 2022-01-17 00:20:47 -0600 | [diff] [blame] | 1477 | goto out_free_ntlmsspblob; |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1478 | |
| 1479 | rc = sess_sendreceive(sess_data); |
| 1480 | |
| 1481 | pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base; |
| 1482 | smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base; |
| 1483 | |
| 1484 | /* If true, rc here is expected and not an error */ |
| 1485 | if (sess_data->buf0_type != CIFS_NO_BUFFER && |
| 1486 | smb_buf->Status.CifsError == |
| 1487 | cpu_to_le32(NT_STATUS_MORE_PROCESSING_REQUIRED)) |
| 1488 | rc = 0; |
| 1489 | |
| 1490 | if (rc) |
Shyam Prasad N | e3548aa | 2022-01-17 00:20:47 -0600 | [diff] [blame] | 1491 | goto out_free_ntlmsspblob; |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1492 | |
| 1493 | cifs_dbg(FYI, "rawntlmssp session setup challenge phase\n"); |
| 1494 | |
| 1495 | if (smb_buf->WordCount != 4) { |
| 1496 | rc = -EIO; |
| 1497 | cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount); |
Shyam Prasad N | e3548aa | 2022-01-17 00:20:47 -0600 | [diff] [blame] | 1498 | goto out_free_ntlmsspblob; |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1499 | } |
| 1500 | |
| 1501 | ses->Suid = smb_buf->Uid; /* UID left in wire format (le) */ |
| 1502 | cifs_dbg(FYI, "UID = %llu\n", ses->Suid); |
| 1503 | |
| 1504 | bytes_remaining = get_bcc(smb_buf); |
| 1505 | bcc_ptr = pByteArea(smb_buf); |
| 1506 | |
| 1507 | blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength); |
| 1508 | if (blob_len > bytes_remaining) { |
| 1509 | cifs_dbg(VFS, "bad security blob length %d\n", |
| 1510 | blob_len); |
| 1511 | rc = -EINVAL; |
Shyam Prasad N | e3548aa | 2022-01-17 00:20:47 -0600 | [diff] [blame] | 1512 | goto out_free_ntlmsspblob; |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1513 | } |
| 1514 | |
| 1515 | rc = decode_ntlmssp_challenge(bcc_ptr, blob_len, ses); |
Shyam Prasad N | e3548aa | 2022-01-17 00:20:47 -0600 | [diff] [blame] | 1516 | |
| 1517 | out_free_ntlmsspblob: |
| 1518 | kfree(ntlmsspblob); |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1519 | out: |
| 1520 | sess_free_buffer(sess_data); |
| 1521 | |
| 1522 | if (!rc) { |
| 1523 | sess_data->func = sess_auth_rawntlmssp_authenticate; |
| 1524 | return; |
| 1525 | } |
| 1526 | |
| 1527 | /* Else error. Cleanup */ |
| 1528 | kfree(ses->auth_key.response); |
| 1529 | ses->auth_key.response = NULL; |
| 1530 | kfree(ses->ntlmssp); |
| 1531 | ses->ntlmssp = NULL; |
| 1532 | |
| 1533 | sess_data->func = NULL; |
| 1534 | sess_data->result = rc; |
| 1535 | } |
| 1536 | |
| 1537 | static void |
| 1538 | sess_auth_rawntlmssp_authenticate(struct sess_data *sess_data) |
| 1539 | { |
| 1540 | int rc; |
| 1541 | struct smb_hdr *smb_buf; |
| 1542 | SESSION_SETUP_ANDX *pSMB; |
| 1543 | struct cifs_ses *ses = sess_data->ses; |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1544 | struct TCP_Server_Info *server = sess_data->server; |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1545 | __u16 bytes_remaining; |
| 1546 | char *bcc_ptr; |
Jerome Marchand | b8da344 | 2016-05-26 11:52:25 +0200 | [diff] [blame] | 1547 | unsigned char *ntlmsspblob = NULL; |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1548 | u16 blob_len; |
| 1549 | |
| 1550 | cifs_dbg(FYI, "rawntlmssp session setup authenticate phase\n"); |
| 1551 | |
| 1552 | /* wct = 12 */ |
| 1553 | rc = sess_alloc_buffer(sess_data, 12); |
| 1554 | if (rc) |
| 1555 | goto out; |
| 1556 | |
| 1557 | /* Build security blob before we assemble the request */ |
| 1558 | pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base; |
| 1559 | smb_buf = (struct smb_hdr *)pSMB; |
Jerome Marchand | b8da344 | 2016-05-26 11:52:25 +0200 | [diff] [blame] | 1560 | rc = build_ntlmssp_auth_blob(&ntlmsspblob, |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1561 | &blob_len, ses, server, |
| 1562 | sess_data->nls_cp); |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1563 | if (rc) |
| 1564 | goto out_free_ntlmsspblob; |
| 1565 | sess_data->iov[1].iov_len = blob_len; |
| 1566 | sess_data->iov[1].iov_base = ntlmsspblob; |
| 1567 | pSMB->req.SecurityBlobLength = cpu_to_le16(blob_len); |
| 1568 | /* |
| 1569 | * Make sure that we tell the server that we are using |
| 1570 | * the uid that it just gave us back on the response |
| 1571 | * (challenge) |
| 1572 | */ |
| 1573 | smb_buf->Uid = ses->Suid; |
| 1574 | |
| 1575 | rc = _sess_auth_rawntlmssp_assemble_req(sess_data); |
| 1576 | if (rc) |
| 1577 | goto out_free_ntlmsspblob; |
| 1578 | |
| 1579 | rc = sess_sendreceive(sess_data); |
| 1580 | if (rc) |
| 1581 | goto out_free_ntlmsspblob; |
| 1582 | |
| 1583 | pSMB = (SESSION_SETUP_ANDX *)sess_data->iov[0].iov_base; |
| 1584 | smb_buf = (struct smb_hdr *)sess_data->iov[0].iov_base; |
| 1585 | if (smb_buf->WordCount != 4) { |
| 1586 | rc = -EIO; |
| 1587 | cifs_dbg(VFS, "bad word count %d\n", smb_buf->WordCount); |
| 1588 | goto out_free_ntlmsspblob; |
| 1589 | } |
| 1590 | |
| 1591 | if (le16_to_cpu(pSMB->resp.Action) & GUEST_LOGIN) |
| 1592 | cifs_dbg(FYI, "Guest login\n"); /* BB mark SesInfo struct? */ |
| 1593 | |
Sachin Prabhu | ee9bbf4 | 2014-12-03 12:26:36 +0000 | [diff] [blame] | 1594 | if (ses->Suid != smb_buf->Uid) { |
| 1595 | ses->Suid = smb_buf->Uid; |
| 1596 | cifs_dbg(FYI, "UID changed! new UID = %llu\n", ses->Suid); |
| 1597 | } |
| 1598 | |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1599 | bytes_remaining = get_bcc(smb_buf); |
| 1600 | bcc_ptr = pByteArea(smb_buf); |
| 1601 | blob_len = le16_to_cpu(pSMB->resp.SecurityBlobLength); |
| 1602 | if (blob_len > bytes_remaining) { |
| 1603 | cifs_dbg(VFS, "bad security blob length %d\n", |
| 1604 | blob_len); |
| 1605 | rc = -EINVAL; |
| 1606 | goto out_free_ntlmsspblob; |
| 1607 | } |
| 1608 | bcc_ptr += blob_len; |
| 1609 | bytes_remaining -= blob_len; |
| 1610 | |
| 1611 | |
| 1612 | /* BB check if Unicode and decode strings */ |
| 1613 | if (bytes_remaining == 0) { |
| 1614 | /* no string area to decode, do nothing */ |
| 1615 | } else if (smb_buf->Flags2 & SMBFLG2_UNICODE) { |
| 1616 | /* unicode string area must be word-aligned */ |
| 1617 | if (((unsigned long) bcc_ptr - (unsigned long) smb_buf) % 2) { |
| 1618 | ++bcc_ptr; |
| 1619 | --bytes_remaining; |
| 1620 | } |
| 1621 | decode_unicode_ssetup(&bcc_ptr, bytes_remaining, ses, |
| 1622 | sess_data->nls_cp); |
| 1623 | } else { |
| 1624 | decode_ascii_ssetup(&bcc_ptr, bytes_remaining, ses, |
| 1625 | sess_data->nls_cp); |
| 1626 | } |
| 1627 | |
| 1628 | out_free_ntlmsspblob: |
| 1629 | kfree(ntlmsspblob); |
| 1630 | out: |
| 1631 | sess_free_buffer(sess_data); |
| 1632 | |
Yang Li | 74ce613 | 2022-01-17 09:11:30 +0800 | [diff] [blame] | 1633 | if (!rc) |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1634 | rc = sess_establish_session(sess_data); |
| 1635 | |
| 1636 | /* Cleanup */ |
| 1637 | kfree(ses->auth_key.response); |
| 1638 | ses->auth_key.response = NULL; |
| 1639 | kfree(ses->ntlmssp); |
| 1640 | ses->ntlmssp = NULL; |
| 1641 | |
| 1642 | sess_data->func = NULL; |
| 1643 | sess_data->result = rc; |
| 1644 | } |
| 1645 | |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1646 | static int select_sec(struct sess_data *sess_data) |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1647 | { |
| 1648 | int type; |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1649 | struct cifs_ses *ses = sess_data->ses; |
| 1650 | struct TCP_Server_Info *server = sess_data->server; |
Sachin Prabhu | 80a0e63 | 2014-06-16 15:35:25 +0100 | [diff] [blame] | 1651 | |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1652 | type = cifs_select_sectype(server, ses->sectype); |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1653 | cifs_dbg(FYI, "sess setup type %d\n", type); |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 1654 | if (type == Unspecified) { |
Joe Perches | a0a3036 | 2020-04-14 22:42:53 -0700 | [diff] [blame] | 1655 | cifs_dbg(VFS, "Unable to select appropriate authentication method!\n"); |
Jeff Layton | 3f61822 | 2013-06-12 19:52:14 -0500 | [diff] [blame] | 1656 | return -EINVAL; |
| 1657 | } |
| 1658 | |
Sachin Prabhu | 80a0e63 | 2014-06-16 15:35:25 +0100 | [diff] [blame] | 1659 | switch (type) { |
Sachin Prabhu | 583cf7a | 2014-06-16 15:35:26 +0100 | [diff] [blame] | 1660 | case NTLMv2: |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1661 | sess_data->func = sess_auth_ntlmv2; |
| 1662 | break; |
Sachin Prabhu | ee03c64 | 2014-06-16 15:35:27 +0100 | [diff] [blame] | 1663 | case Kerberos: |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1664 | #ifdef CONFIG_CIFS_UPCALL |
| 1665 | sess_data->func = sess_auth_kerberos; |
| 1666 | break; |
| 1667 | #else |
| 1668 | cifs_dbg(VFS, "Kerberos negotiated but upcall support disabled!\n"); |
| 1669 | return -ENOSYS; |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1670 | #endif /* CONFIG_CIFS_UPCALL */ |
| 1671 | case RawNTLMSSP: |
| 1672 | sess_data->func = sess_auth_rawntlmssp_negotiate; |
| 1673 | break; |
Sachin Prabhu | 80a0e63 | 2014-06-16 15:35:25 +0100 | [diff] [blame] | 1674 | default: |
Joe Perches | f96637b | 2013-05-04 22:12:25 -0500 | [diff] [blame] | 1675 | cifs_dbg(VFS, "secType %d not supported!\n", type); |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1676 | return -ENOSYS; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 1677 | } |
| 1678 | |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1679 | return 0; |
| 1680 | } |
Steve French | 2442421 | 2007-11-16 23:37:35 +0000 | [diff] [blame] | 1681 | |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1682 | int CIFS_SessSetup(const unsigned int xid, struct cifs_ses *ses, |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1683 | struct TCP_Server_Info *server, |
| 1684 | const struct nls_table *nls_cp) |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1685 | { |
| 1686 | int rc = 0; |
| 1687 | struct sess_data *sess_data; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 1688 | |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1689 | if (ses == NULL) { |
| 1690 | WARN(1, "%s: ses == NULL!", __func__); |
| 1691 | return -EINVAL; |
Steve French | 0b3cc858 | 2009-05-04 08:37:12 +0000 | [diff] [blame] | 1692 | } |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1693 | |
| 1694 | sess_data = kzalloc(sizeof(struct sess_data), GFP_KERNEL); |
| 1695 | if (!sess_data) |
| 1696 | return -ENOMEM; |
| 1697 | |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1698 | sess_data->xid = xid; |
| 1699 | sess_data->ses = ses; |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1700 | sess_data->server = server; |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1701 | sess_data->buf0_type = CIFS_NO_BUFFER; |
| 1702 | sess_data->nls_cp = (struct nls_table *) nls_cp; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 1703 | |
Shyam Prasad N | f486ef8 | 2021-07-19 13:54:16 +0000 | [diff] [blame] | 1704 | rc = select_sec(sess_data); |
| 1705 | if (rc) |
| 1706 | goto out; |
| 1707 | |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1708 | while (sess_data->func) |
| 1709 | sess_data->func(sess_data); |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 1710 | |
Sachin Prabhu | cc87c47 | 2014-06-16 15:35:28 +0100 | [diff] [blame] | 1711 | /* Store result before we free sess_data */ |
| 1712 | rc = sess_data->result; |
Sachin Prabhu | 80a0e63 | 2014-06-16 15:35:25 +0100 | [diff] [blame] | 1713 | |
| 1714 | out: |
Sachin Prabhu | 80a0e63 | 2014-06-16 15:35:25 +0100 | [diff] [blame] | 1715 | kfree(sess_data); |
| 1716 | return rc; |
Steve French | 3979877 | 2006-05-31 22:40:51 +0000 | [diff] [blame] | 1717 | } |