Linux Audio

Check our new training course

Embedded Linux Audio

Check our new training course
with Creative Commons CC-BY-SA
lecture materials

Bootlin logo

Elixir Cross Referencer

Loading...
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
/*
 *  sock.c
 *
 *  Copyright (C) 1995, 1996 by Paal-Kr. Engstad and Volker Lendecke
 *  Copyright (C) 1997 by Volker Lendecke
 *
 *  Please add a note about your changes to smbfs in the ChangeLog file.
 */

#include <linux/fs.h>
#include <linux/time.h>
#include <linux/errno.h>
#include <linux/socket.h>
#include <linux/fcntl.h>
#include <linux/file.h>
#include <linux/in.h>
#include <linux/net.h>
#include <linux/tcp.h>
#include <linux/mm.h>
#include <linux/netdevice.h>
#include <linux/smp_lock.h>
#include <linux/workqueue.h>
#include <net/scm.h>
#include <net/ip.h>

#include <linux/smb_fs.h>
#include <linux/smb.h>
#include <linux/smbno.h>

#include <asm/uaccess.h>
#include <asm/ioctls.h>

#include "smb_debug.h"
#include "proto.h"
#include "request.h"


static int
_recvfrom(struct socket *socket, unsigned char *ubuf, int size, unsigned flags)
{
	struct iovec iov;
	struct msghdr msg;
	struct kiocb iocb;
	struct sock_iocb *si;
	mm_segment_t fs;

	fs = get_fs();
	set_fs(get_ds());
	flags |= MSG_DONTWAIT | MSG_NOSIGNAL;

	msg.msg_flags = flags;
	msg.msg_name = NULL;
	msg.msg_namelen = 0;
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;
	msg.msg_control = NULL;
	iov.iov_base = ubuf;
	iov.iov_len = size;

	init_sync_kiocb(&iocb, NULL);
	si = kiocb_to_siocb(&iocb);
	si->sock = socket;
	si->scm = &si->async_scm;
	si->msg = &msg;
	si->size = size;
	si->flags = flags;

	memset(si->scm, 0, sizeof(*si->scm));

	size = socket->ops->recvmsg(&iocb, socket, &msg, size, flags, si->scm);
	if (size >= 0)
		scm_recv(socket, &msg, si->scm, flags);
	if (-EIOCBQUEUED == size)
		size = wait_on_sync_kiocb(&iocb);

	set_fs(fs);
	return size;
}

/*
 * Return the server this socket belongs to
 */
static struct smb_sb_info *
server_from_socket(struct socket *socket)
{
	return socket->sk->user_data;
}

/*
 * Called when there is data on the socket.
 */
void
smb_data_ready(struct sock *sk, int len)
{
	struct smb_sb_info *server = server_from_socket(sk->socket);
	void (*data_ready)(struct sock *, int) = server->data_ready;

	data_ready(sk, len);
	VERBOSE("(%p, %d)\n", sk, len);
	smbiod_wake_up();
}

int
smb_valid_socket(struct inode * inode)
{
	return (inode && S_ISSOCK(inode->i_mode) && 
		SOCKET_I(inode)->type == SOCK_STREAM);
}

static struct socket *
server_sock(struct smb_sb_info *server)
{
	struct file *file;

	if (server && (file = server->sock_file))
	{
#ifdef SMBFS_PARANOIA
		if (!smb_valid_socket(file->f_dentry->d_inode))
			PARANOIA("bad socket!\n");
#endif
		return SOCKET_I(file->f_dentry->d_inode);
	}
	return NULL;
}

void
smb_close_socket(struct smb_sb_info *server)
{
	struct file * file = server->sock_file;

	if (file) {
		struct socket *sock = server_sock(server);

		VERBOSE("closing socket %p\n", sock);
		sock->sk->data_ready = server->data_ready;
		server->sock_file = NULL;
		fput(file);
	}
}

static int
smb_get_length(struct socket *socket, unsigned char *header)
{
	int result;

	result = _recvfrom(socket, header, 4, MSG_PEEK);
	if (result == -EAGAIN)
		return -ENODATA;
	if (result < 0) {
		PARANOIA("recv error = %d\n", -result);
		return result;
	}
	if (result < 4)
		return -ENODATA;

	switch (header[0]) {
	case 0x00:
	case 0x82:
		break;

	case 0x85:
		DEBUG1("Got SESSION KEEP ALIVE\n");
		_recvfrom(socket, header, 4, 0);	/* read away */
		return -ENODATA;

	default:
		PARANOIA("Invalid NBT packet, code=%x\n", header[0]);
		return -EIO;
	}

	/* The length in the RFC NB header is the raw data length */
	return smb_len(header);
}

int
smb_recv_available(struct smb_sb_info *server)
{
	mm_segment_t oldfs;
	int avail, err;
	struct socket *sock = server_sock(server);

	oldfs = get_fs();
	set_fs(get_ds());
	err = sock->ops->ioctl(sock, SIOCINQ, (unsigned long) &avail);
	set_fs(oldfs);
	return (err >= 0) ? avail : err;
}

/*
 * Adjust the iovec to move on 'n' bytes (from nfs/sunrpc)
 */
static int
smb_move_iov(struct msghdr *msg, struct iovec *niv, unsigned amount)
{
	struct iovec *iv = msg->msg_iov;
	int i;
	int len;

	/*
	 *	Eat any sent iovecs
	 */
	while (iv->iov_len <= amount) {
		amount -= iv->iov_len;
		iv++;
		msg->msg_iovlen--;
	}

	/*
	 *	And chew down the partial one
	 */
	niv[0].iov_len = iv->iov_len-amount;
	niv[0].iov_base =((unsigned char *)iv->iov_base)+amount;
	iv++;

	len = niv[0].iov_len;

	/*
	 *	And copy any others
	 */
	for (i = 1; i < msg->msg_iovlen; i++) {
		niv[i] = *iv++;
		len += niv[i].iov_len;
	}

	msg->msg_iov = niv;
	return len;
}

/*
 * smb_receive_header
 * Only called by the smbiod thread.
 */
int
smb_receive_header(struct smb_sb_info *server)
{
	struct socket *sock;
	int result = 0;
	unsigned char peek_buf[4];

	result = -EIO; 
	sock = server_sock(server);
	if (!sock)
		goto out;
	if (sock->sk->state != TCP_ESTABLISHED)
		goto out;

	if (!server->smb_read) {
		result = smb_get_length(sock, peek_buf);
		if (result < 0) {
			if (result == -ENODATA)
				result = 0;
			goto out;
		}
		server->smb_len = result + 4;

		if (server->smb_len < SMB_HEADER_LEN) {
			PARANOIA("short packet: %d\n", result);
			server->rstate = SMB_RECV_DROP;
			result = -EIO;
			goto out;
		}
		if (server->smb_len > SMB_MAX_PACKET_SIZE) {
			PARANOIA("long packet: %d\n", result);
			server->rstate = SMB_RECV_DROP;
			result = -EIO;
			goto out;
		}
	}

	result = _recvfrom(sock, server->header + server->smb_read,
			   SMB_HEADER_LEN - server->smb_read, 0);
	VERBOSE("_recvfrom: %d\n", result);
	if (result < 0) {
		VERBOSE("receive error: %d\n", result);
		goto out;
	}
	server->smb_read += result;

	if (server->smb_read == SMB_HEADER_LEN)
		server->rstate = SMB_RECV_HCOMPLETE;
out:
	return result;
}

static char drop_buffer[PAGE_SIZE];

/*
 * smb_receive_drop - read and throw away the data
 * Only called by the smbiod thread.
 *
 * FIXME: we are in the kernel, could we just tell the socket that we want
 * to drop stuff from the buffer?
 */
int
smb_receive_drop(struct smb_sb_info *server)
{
	struct socket *sock;
	unsigned int flags;
	struct iovec iov;
	struct msghdr msg;
	struct kiocb iocb;
	struct sock_iocb *si;
	mm_segment_t fs;
	int rlen = smb_len(server->header) - server->smb_read + 4;
	int result = -EIO;

	sock = server_sock(server);
	if (!sock)
		goto out;
	if (sock->sk->state != TCP_ESTABLISHED)
		goto out;

	fs = get_fs();
	set_fs(get_ds());

	flags = MSG_DONTWAIT | MSG_NOSIGNAL;
	iov.iov_base = drop_buffer;
	iov.iov_len = PAGE_SIZE;
	msg.msg_flags = flags;
	msg.msg_name = NULL;
	msg.msg_namelen = 0;
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;
	msg.msg_control = NULL;

	if (rlen > PAGE_SIZE)
		rlen = PAGE_SIZE;

	init_sync_kiocb(&iocb, NULL);
	si = kiocb_to_siocb(&iocb);
	si->sock = sock;
	si->scm = &si->async_scm;
	si->msg = &msg;
	si->size = rlen;
	si->flags = flags;

	memset(si->scm, 0, sizeof(*si->scm));

	result = sock->ops->recvmsg(&iocb, sock, &msg, rlen, flags, si->scm);
	if (result >= 0)
		scm_recv(sock, &msg, si->scm, flags);
	if (-EIOCBQUEUED == result)
		result = wait_on_sync_kiocb(&iocb);

	set_fs(fs);

	VERBOSE("read: %d\n", result);
	if (result < 0) {
		VERBOSE("receive error: %d\n", result);
		goto out;
	}
	server->smb_read += result;

	if (server->smb_read >= server->smb_len)
		server->rstate = SMB_RECV_END;

out:
	return result;
}

/*
 * smb_receive
 * Only called by the smbiod thread.
 */
int
smb_receive(struct smb_sb_info *server, struct smb_request *req)
{
	struct socket *sock;
	unsigned int flags;
	struct iovec iov[4];
	struct msghdr msg;
	struct kiocb iocb;
	struct sock_iocb *si;
	mm_segment_t fs;
	int rlen;
	int result = -EIO;

	sock = server_sock(server);
	if (!sock)
		goto out;
	if (sock->sk->state != TCP_ESTABLISHED)
		goto out;

	fs = get_fs();
	set_fs(get_ds());

	flags = MSG_DONTWAIT | MSG_NOSIGNAL;;
	msg.msg_flags = flags;
	msg.msg_name = NULL;
	msg.msg_namelen = 0;
	msg.msg_iov = req->rq_iov;
	msg.msg_iovlen = req->rq_iovlen;
	msg.msg_control = NULL;

	/* Dont repeat bytes and count available bufferspace */
	rlen = smb_move_iov(&msg, iov, req->rq_bytes_recvd);
	if (req->rq_rlen < rlen)
		rlen = req->rq_rlen;

	init_sync_kiocb(&iocb, NULL);
	si = kiocb_to_siocb(&iocb);
	si->sock = sock;
	si->scm = &si->async_scm;
	si->msg = &msg;
	si->size = rlen;
	si->flags = flags;

	memset(si->scm, 0, sizeof(*si->scm));

	result = sock->ops->recvmsg(&iocb, sock, &msg, rlen, flags, si->scm);
	if (result >= 0)
		scm_recv(sock, &msg, si->scm, flags);
	if (-EIOCBQUEUED == result)
		result = wait_on_sync_kiocb(&iocb);

	set_fs(fs);

	VERBOSE("read: %d\n", result);
	if (result < 0) {
		VERBOSE("receive error: %d\n", result);
		goto out;
	}
	req->rq_bytes_recvd += result;
	server->smb_read += result;

out:
	return result;
}

/*
 * Try to send a SMB request. This may return after sending only parts of the
 * request. SMB_REQ_TRANSMITTED will be set if a request was fully sent.
 *
 * Parts of this was taken from xprt_sendmsg from net/sunrpc/xprt.c
 */
int
smb_send_request(struct smb_request *req)
{
	mm_segment_t fs;
	struct smb_sb_info *server = req->rq_server;
	struct socket *sock;
	struct kiocb iocb;
	struct sock_iocb *si;
	struct msghdr msg;
        int slen = req->rq_slen - req->rq_bytes_sent;
	int result = -EIO;
	struct iovec iov[4];

	sock = server_sock(server);
	if (!sock)
		goto out;
	if (sock->sk->state != TCP_ESTABLISHED)
		goto out;

	msg.msg_name = NULL;
	msg.msg_namelen = 0;
	msg.msg_control = NULL;
	msg.msg_controllen = 0;
	msg.msg_iov = req->rq_iov;
	msg.msg_iovlen = req->rq_iovlen;
	msg.msg_flags = MSG_NOSIGNAL | MSG_DONTWAIT;

	/* Dont repeat bytes */
	if (req->rq_bytes_sent)
		smb_move_iov(&msg, iov, req->rq_bytes_sent);

	init_sync_kiocb(&iocb, NULL);
	si = kiocb_to_siocb(&iocb);
	si->scm = &si->async_scm;
	si->sock = sock;
	si->msg = &msg;
	si->size = slen;

	fs = get_fs();
	set_fs(get_ds());
	result = scm_send(sock, &msg, si->scm);
	if (result >= 0) {
		result = sock->ops->sendmsg(&iocb, sock, &msg, slen, si->scm);
		if (-EIOCBQUEUED != result)
			scm_destroy(si->scm);
	}
	if (-EIOCBQUEUED == result)
		result = wait_on_sync_kiocb(&iocb);
	set_fs(fs);

	if (result >= 0) {
		req->rq_bytes_sent += result;
		if (req->rq_bytes_sent >= req->rq_slen)
			req->rq_flags |= SMB_REQ_TRANSMITTED;
	}
out:
	return result;
}