summaryrefslogtreecommitdiffstats
path: root/sys/netncp/ncp_rq.c
blob: 8cef7d2a74f42a37a9d2f8c948471299f6b76bf0 (plain)
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
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
/*
 * Copyright (c) 1999, Boris Popov
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. All advertising materials mentioning features or use of this software
 *    must display the following acknowledgement:
 *    This product includes software developed by Boris Popov.
 * 4. Neither the name of the author nor the names of any co-contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * Routines to prepare request and fetch reply
 *
 * $FreeBSD$
 */ 
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/errno.h>
#include <sys/mbuf.h>
#include <sys/uio.h>

#include <netncp/ncp.h>
#include <netncp/ncp_conn.h>
#include <netncp/ncp_rq.h>
#include <netncp/ncp_subr.h>
#include <netncp/ncp_ncp.h>
#include <netncp/ncp_nls.h>

static struct mbuf* m_getm(struct mbuf *top, int len);

int
ncp_rq_head(struct ncp_rq *rqp, u_int32_t ptype, u_int8_t fn,struct proc *p,
    struct ucred *cred)
{
	struct mbuf *m;
	struct ncp_rqhdr *rq;
	struct ncp_bursthdr *brq;
	caddr_t pstart;

	bzero(rqp, sizeof(*rqp));
	rqp->p = p;
	rqp->cred = cred;
	m = m_gethdr(M_WAIT, MT_DATA);
	if (m == NULL) 
		return ENOBUFS;		/* if M_WAIT ? */
	m->m_pkthdr.rcvif = NULL;
	rqp->rq = rqp->mrq = m;
	rqp->rp = NULL;
	switch(ptype) {
	    case NCP_PACKET_BURST:
		MH_ALIGN(m, sizeof(*brq) + 24);
		m->m_len = sizeof(*brq);
		brq = mtod(m, struct ncp_bursthdr *);
		brq->bh_type = ptype;
		brq->bh_streamtype = 0x2;
		pstart = (caddr_t)brq;
		break;
	    default:
		MH_ALIGN(m, sizeof(*rq) + 2);	/* possible len field in some functions */
		m->m_len = sizeof(*rq);
		rq = mtod(m, struct ncp_rqhdr *);
		rq->type = ptype;
		rq->seq = 0;	/* filled later */
		rq->fn = fn;
		pstart = (caddr_t)rq;
		break;
	}
	rqp->bpos = pstart + m->m_len;
	return 0;
}

int
ncp_rq_done(struct ncp_rq *rqp) {
	m_freem(rqp->rq);
	rqp->rq=NULL;
	if (rqp->rp) m_freem(rqp->rp);
	rqp->rp=NULL;
	return (0);
}

static struct mbuf*
m_getm(struct mbuf *top, int len) {
	int rest = len, mlen;
	struct mbuf *m=0,*mp;
	
	mp = top;
	while (rest > 0) {
/*	NCPSDEBUG("%d\n",rest);*/
		m = m_get(M_WAIT, MT_DATA);
		if (rest > MINCLSIZE) {
			MCLGET(m,M_WAIT);
			mlen = ( (m->m_flags & M_EXT) == 0) ? MLEN : MCLBYTES;
		} else { 
			mlen = MLEN;
		}
		m->m_len = 0/*min(mlen,rest)*/;
		mp->m_next = m;
		mp = m;
		rest -= mlen;
	}
	return top;
}

/*
 * Routines to fill the request
 */
static caddr_t ncp_mchecksize(struct ncp_rq *rqp, int size);
#define	NCP_RQADD(t)	((t*)(ncp_mchecksize(rqp,sizeof(t))))

caddr_t
ncp_mchecksize(struct ncp_rq *rqp, int size) {
	caddr_t bpos1;

	if (size>MLEN)
		panic("ncp_mchecksize\n");
	if (M_TRAILINGSPACE(rqp->mrq)<(size)) {
		struct mbuf *m;
		m = m_get(M_WAIT, MT_DATA);
		m->m_len = 0;
		rqp->bpos = mtod(m, caddr_t);
		rqp->mrq->m_next = m;
		rqp->mrq = m;
	}
	rqp->mrq->m_len += size;
	bpos1 = rqp->bpos;
	rqp->bpos += size;
	return bpos1;
}

void
ncp_rq_byte(struct ncp_rq *rqp,u_int8_t x) {
	*NCP_RQADD(u_int8_t)=x;
}

void
ncp_rq_word_hl(struct ncp_rq *rqp, u_int16_t x) {
	setwbe(NCP_RQADD(u_int16_t), 0, x);
}

void
ncp_rq_word_lh(struct ncp_rq *rqp, u_int16_t x) {
	setwle(NCP_RQADD(u_int16_t), 0, x);
}

void
ncp_rq_dword_lh(struct ncp_rq *rqp, u_int32_t x) {
	setdle(NCP_RQADD(u_int32_t), 0, x);
}

void
ncp_rq_pathstring(struct ncp_rq *rqp, int size, char *name, struct ncp_nlstables *nt) {
	struct mbuf *m;
	int cplen;

	ncp_rq_byte(rqp, size);
	m = rqp->mrq;
	cplen = min(size, M_TRAILINGSPACE(m));
	if (cplen) {
		ncp_pathcopy(name, rqp->bpos, cplen, nt);
		size -= cplen;
		name += cplen;
		m->m_len += cplen;
	}
	if (size) {
		m_getm(m, size);
		while (size > 0){
			m = m->m_next;
			cplen = min(size, M_TRAILINGSPACE(m));
			ncp_pathcopy(name, mtod(m, caddr_t) + m->m_len, cplen, nt);
			size -= cplen;
			name += cplen;
			m->m_len += cplen;
		}
	}
	rqp->bpos = mtod(m,caddr_t) + m->m_len;
	rqp->mrq = m;
	return;
}

int
ncp_rq_putanymem(struct ncp_rq *rqp, caddr_t source, int size, int type) {
	struct mbuf *m;
	int cplen, error;

	m = rqp->mrq;
	cplen = min(size, M_TRAILINGSPACE(m));
	if (cplen) {
		if (type==1) {
			error = copyin(source, rqp->bpos, cplen);
			if (error) return error;
		} else
			bcopy(source, rqp->bpos, cplen);
		size -= cplen;
		source += cplen;
		m->m_len += cplen;
	}
	if (size) {
		m_getm(m, size);
		while (size > 0){
			m = m->m_next;
			cplen = min(size, M_TRAILINGSPACE(m));
			if (type==1) {
				error = copyin(source, mtod(m, caddr_t) + m->m_len, cplen);
				if (error) return error;
			} else
				bcopy(source, mtod(m, caddr_t) + m->m_len, cplen);
			size -= cplen;
			source += cplen;
			m->m_len += cplen;
		}
	}
	rqp->bpos = mtod(m,caddr_t) + m->m_len;
	rqp->mrq = m;
	return 0;
}

int
ncp_rq_mbuf(struct ncp_rq *rqp, struct mbuf *m, int size) {

	rqp->mrq->m_next = m;
	m->m_next = NULL;
	if (size != M_COPYALL) m->m_len = size;
	rqp->bpos = mtod(m,caddr_t) + m->m_len;
	rqp->mrq = m;
	return 0;
}

void 
ncp_rq_pstring(struct ncp_rq *rqp, char *s) {
	int len = strlen(s);
	if (len > 255) {
		nwfs_printf("string too long: %s\n", s);
		len = 255;
	}
	ncp_rq_byte(rqp, len);
	ncp_rq_mem(rqp, s, len);
	return;
}

void 
ncp_rq_dbase_path(struct ncp_rq *rqp, u_int8_t vol_num, u_int32_t dir_base,
                    int namelen, u_char *path, struct ncp_nlstables *nt)
{
	int complen;

	ncp_rq_byte(rqp, vol_num);
	ncp_rq_dword(rqp, dir_base);
	ncp_rq_byte(rqp, 1);	/* with dirbase */
	if (path != NULL && path[0]) {
		if (namelen < 0) {
			namelen = *path++;
			ncp_rq_byte(rqp, namelen);
			for(; namelen; namelen--) {
				complen = *path++;
				ncp_rq_byte(rqp, complen);
				ncp_rq_mem(rqp, path, complen);
				path += complen;
			}
		} else {
			ncp_rq_byte(rqp, 1);	/* 1 component */
			ncp_rq_pathstring(rqp, namelen, path, nt);
		}
	} else {
		ncp_rq_byte(rqp, 0);
		ncp_rq_byte(rqp, 0);
	}
}
/*
 * fetch reply routines
 */
#define ncp_mspaceleft	(mtod(rqp->mrp,caddr_t)+rqp->mrp->m_len-rqp->bpos)

u_int8_t
ncp_rp_byte(struct ncp_rq *rqp) {
	if (rqp->mrp == NULL) return 0;
	if (ncp_mspaceleft < 1) {
		rqp->mrp = rqp->mrp->m_next;
		if (rqp->mrp == NULL) return 0;
		rqp->bpos = mtod(rqp->mrp, caddr_t);
	}
	rqp->bpos += 1;
	return rqp->bpos[-1];
}

u_int16_t
ncp_rp_word_lh(struct ncp_rq *rqp) {
	caddr_t prev = rqp->bpos;
	u_int16_t t;

	if (rqp->mrp == NULL) return 0;
	if (ncp_mspaceleft >= 2) {
		rqp->bpos += 2;
		return getwle(prev,0);
	}
	t = *((u_int8_t*)(rqp->bpos));
	rqp->mrp = rqp->mrp->m_next;
	if (rqp->mrp == NULL) return 0;
	((u_int8_t *)&t)[1] = *((u_int8_t*)(rqp->bpos = mtod(rqp->mrp, caddr_t)));
	rqp->bpos += 2;
	return t;
}

u_int16_t
ncp_rp_word_hl(struct ncp_rq *rqp) {
	return (ntohs(ncp_rp_word_lh(rqp)));
}

u_int32_t
ncp_rp_dword_hl(struct ncp_rq *rqp) {
	int togo, rest;
	caddr_t prev = rqp->bpos;
	u_int32_t t;

	if (rqp->mrp == NULL) return 0;
	rest = ncp_mspaceleft;
	if (rest >= 4) {
		rqp->bpos += 4;
		return getdbe(prev,0);
	}
	togo = 0;
	while (rest--) {
		((u_int8_t *)&t)[togo++] = *((u_int8_t*)(prev++));
	}
	rqp->mrp = rqp->mrp->m_next;
	if (rqp->mrp == NULL) return 0;
	prev = mtod(rqp->mrp, caddr_t);
	rqp->bpos = prev + 4 - togo;	/* XXX possible low than togo bytes in next mbuf */
	while (togo < 4) {
		((u_int8_t *)&t)[togo++] = *((u_int8_t*)(prev++));
	}
	return getdbe(&t,0);
}

u_int32_t
ncp_rp_dword_lh(struct ncp_rq *rqp) {
	int rest, togo;
	caddr_t prev = rqp->bpos;
	u_int32_t t;

	if (rqp->mrp == NULL) return 0;
	rest = ncp_mspaceleft;
	if (rest >= 4) {
		rqp->bpos += 4;
		return getdle(prev,0);
	}
	togo = 0;
	while (rest--) {
		((u_int8_t *)&t)[togo++] = *((u_int8_t*)(prev++));
	}
	rqp->mrp = rqp->mrp->m_next;
	if (rqp->mrp == NULL) return 0;
	prev = mtod(rqp->mrp, caddr_t);
	rqp->bpos = prev + 4 - togo;	/* XXX possible low than togo bytes in next mbuf */
	while (togo < 4) {
		((u_int8_t *)&t)[togo++] = *((u_int8_t*)(prev++));
	}
	return getdle(&t,0);
}
void
ncp_rp_mem(struct ncp_rq *rqp,caddr_t target, int size) {
	register struct mbuf *m=rqp->mrp;
	register unsigned count;
	
	while (size > 0) {
		if (m==0) {	/* should be panic */
			printf("ncp_rp_mem: incomplete copy\n");
			return;
		}
		count = mtod(m,caddr_t)+m->m_len-rqp->bpos;
		if (count == 0) {
			m=m->m_next;
			rqp->bpos=mtod(m,caddr_t);
			continue;
		}
		count = min(count,size);
		bcopy(rqp->bpos, target, count);
		size -= count;
		target += count;
		rqp->bpos += count;
	}
	rqp->mrp=m;
	return;
}

int
ncp_rp_usermem(struct ncp_rq *rqp,caddr_t target, int size) {
	register struct mbuf *m=rqp->mrp;
	register unsigned count;
	int error;
	
	while (size>0) {
		if (m==0) {	/* should be panic */
			printf("ncp_rp_mem: incomplete copy\n");
			return EFAULT;
		}
		count = mtod(m,caddr_t)+m->m_len-rqp->bpos;
		if (count == 0) {
			m=m->m_next;
			rqp->bpos=mtod(m,caddr_t);
			continue;
		}
		count = min(count,size);
		error=copyout(rqp->bpos, target, count);
		if (error) return error;
		size -= count;
		target += count;
		rqp->bpos += count;
	}
	rqp->mrp=m;
	return 0;
}

struct mbuf*
ncp_rp_mbuf(struct ncp_rq *rqp, int size) {
	register struct mbuf *m=rqp->mrp, *rm;
	register unsigned count;
	
	rm = m_copym(m, rqp->bpos - mtod(m,caddr_t), size, M_WAIT);
	while (size > 0) {
		if (m == 0) {
			printf("ncp_rp_mbuf: can't advance\n");
			return rm;
		}
		count = mtod(m,caddr_t)+ m->m_len - rqp->bpos;
		if (count == 0) {
			m = m->m_next;
			rqp->bpos = mtod(m, caddr_t);
			continue;
		}
		count = min(count, size);
		size -= count;
		rqp->bpos += count;
	}
	rqp->mrp=m;
	return rm;
}

int
nwfs_mbuftouio(mrep, uiop, siz, dpos)
	struct mbuf **mrep;
	register struct uio *uiop;
	int siz;
	caddr_t *dpos;
{
	register char *mbufcp, *uiocp;
	register int xfer, left, len;
	register struct mbuf *mp;
	long uiosiz;
	int error = 0;

	mp = *mrep;
	if (!mp) return 0;
	mbufcp = *dpos;
	len = mtod(mp, caddr_t)+mp->m_len-mbufcp;
	while (siz > 0) {
		if (uiop->uio_iovcnt <= 0 || uiop->uio_iov == NULL)
			return (EFBIG);
		left = uiop->uio_iov->iov_len;
		uiocp = uiop->uio_iov->iov_base;
		if (left > siz)
			left = siz;
		uiosiz = left;
		while (left > 0) {
			while (len == 0) {
				mp = mp->m_next;
				if (mp == NULL)
					return (EBADRPC);
				mbufcp = mtod(mp, caddr_t);
				len = mp->m_len;
			}
			xfer = (left > len) ? len : left;
#ifdef notdef
			/* Not Yet.. */
			if (uiop->uio_iov->iov_op != NULL)
				(*(uiop->uio_iov->iov_op))
				(mbufcp, uiocp, xfer);
			else
#endif
			if (uiop->uio_segflg == UIO_SYSSPACE)
				bcopy(mbufcp, uiocp, xfer);
			else
				copyout(mbufcp, uiocp, xfer);
			left -= xfer;
			len -= xfer;
			mbufcp += xfer;
			uiocp += xfer;
			uiop->uio_offset += xfer;
			uiop->uio_resid -= xfer;
		}
		if (uiop->uio_iov->iov_len <= siz) {
			uiop->uio_iovcnt--;
			uiop->uio_iov++;
		} else {
			uiop->uio_iov->iov_base += uiosiz;
			uiop->uio_iov->iov_len -= uiosiz;
		}
		siz -= uiosiz;
	}
	*dpos = mbufcp;
	*mrep = mp;
	return (error);
}
/*
 * copies a uio scatter/gather list to an mbuf chain.
 * NOTE: can ony handle iovcnt == 1
 */
int
nwfs_uiotombuf(uiop, mq, siz, bpos)
	register struct uio *uiop;
	struct mbuf **mq;
	int siz;
	caddr_t *bpos;
{
	register char *uiocp;
	register struct mbuf *mp, *mp2;
	register int xfer, left, mlen;
	int uiosiz, clflg;

#ifdef DIAGNOSTIC
	if (uiop->uio_iovcnt != 1)
		panic("nfsm_uiotombuf: iovcnt != 1");
#endif

	if (siz > MLEN)		/* or should it >= MCLBYTES ?? */
		clflg = 1;
	else
		clflg = 0;
	mp = mp2 = *mq;
	while (siz > 0) {
		left = uiop->uio_iov->iov_len;
		uiocp = uiop->uio_iov->iov_base;
		if (left > siz)
			left = siz;
		uiosiz = left;
		while (left > 0) {
			mlen = M_TRAILINGSPACE(mp);
			if (mlen == 0) {
				MGET(mp, M_WAIT, MT_DATA);
				if (clflg)
					MCLGET(mp, M_WAIT);
				mp->m_len = 0;
				mp2->m_next = mp;
				mp2 = mp;
				mlen = M_TRAILINGSPACE(mp);
			}
			xfer = (left > mlen) ? mlen : left;
#ifdef notdef
			/* Not Yet.. */
			if (uiop->uio_iov->iov_op != NULL)
				(*(uiop->uio_iov->iov_op))
				(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
			else
#endif
			if (uiop->uio_segflg == UIO_SYSSPACE)
				bcopy(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
			else
				copyin(uiocp, mtod(mp, caddr_t)+mp->m_len, xfer);
			mp->m_len += xfer;
			left -= xfer;
			uiocp += xfer;
			uiop->uio_offset += xfer;
			uiop->uio_resid -= xfer;
		}
		uiop->uio_iov->iov_base += uiosiz;
		uiop->uio_iov->iov_len -= uiosiz;
		siz -= uiosiz;
	}
	*bpos = mtod(mp, caddr_t)+mp->m_len;
	*mq = mp;
	return (0);
}
OpenPOWER on IntegriCloud