2 * Copyright (c) 1999, Boris Popov
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by Boris Popov.
16 * 4. Neither the name of the author nor the names of any co-contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * $FreeBSD: src/sys/netncp/ncp_subr.c,v 1.2.2.1 2001/02/22 08:54:11 bp Exp $
33 * $DragonFly: src/sys/netproto/ncp/ncp_subr.c,v 1.2 2003/06/17 04:28:53 dillon Exp $
35 #include <sys/param.h>
36 #include <sys/errno.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/malloc.h>
45 #include <netncp/ncp.h>
46 #include <netncp/ncp_conn.h>
47 #include <netncp/ncp_sock.h>
48 #include <netncp/ncp_subr.h>
49 #include <netncp/ncp_rq.h>
50 #include <netncp/ncp_ncp.h>
51 #include <netncp/nwerror.h>
53 int ncp_debuglevel = 0;
55 struct callout_handle ncp_timer_handle;
57 static void ncp_at_exit(struct proc *p);
58 static void ncp_timer(void *arg);
61 * duplicate string from user space. It should be very-very slow.
64 ncp_str_dup(char *s) {
69 if (copyin(p, &bt, 1)) return NULL;
73 MALLOC(p, char*, len, M_NCPDATA, M_WAITOK);
80 ncp_at_exit(struct proc *p) {
81 struct ncp_conn *ncp, *nncp;
83 if (ncp_conn_putprochandles(p) == 0) return;
85 ncp_conn_locklist(LK_EXCLUSIVE, p);
86 for (ncp = SLIST_FIRST(&conn_list); ncp; ncp = nncp) {
87 nncp = SLIST_NEXT(ncp, nc_next);
88 if (ncp->ref_cnt != 0) continue;
89 if (ncp_conn_lock(ncp, p, p->p_ucred,NCPM_READ|NCPM_EXECUTE|NCPM_WRITE))
91 if (ncp_disconnect(ncp) != 0)
92 ncp_conn_unlock(ncp,p);
94 ncp_conn_unlocklist(p);
101 if (at_exit(ncp_at_exit)) {
102 NCPFATAL("can't register at_exit handler\n");
105 ncp_timer_handle = timeout(ncp_timer,NULL,NCP_TIMER_TICK);
111 struct ncp_conn *ncp, *nncp;
112 struct proc *p = curproc;
114 untimeout(ncp_timer,NULL,ncp_timer_handle);
115 rm_at_exit(ncp_at_exit);
116 ncp_conn_locklist(LK_EXCLUSIVE, p);
117 for (ncp = SLIST_FIRST(&conn_list); ncp; ncp = nncp) {
118 nncp = SLIST_NEXT(ncp, nc_next);
120 if (ncp_conn_lock(ncp, p, p->p_ucred,NCPM_READ|NCPM_EXECUTE|NCPM_WRITE)) {
121 NCPFATAL("Can't lock connection !\n");
124 if (ncp_disconnect(ncp) != 0)
125 ncp_conn_unlock(ncp,p);
127 ncp_conn_unlocklist(p);
131 /* tick every second and check for watch dog packets and lost connections */
133 ncp_timer(void *arg){
134 struct ncp_conn *conn;
136 if(ncp_conn_locklist(LK_SHARED | LK_NOWAIT, NULL) == 0) {
137 SLIST_FOREACH(conn, &conn_list, nc_next)
138 ncp_check_conn(conn);
139 ncp_conn_unlocklist(NULL);
141 ncp_timer_handle = timeout(ncp_timer,NULL,NCP_TIMER_TICK);
145 ncp_get_bindery_object_id(struct ncp_conn *conn,
146 u_int16_t object_type, char *object_name,
147 struct ncp_bindery_object *target,
148 struct proc *p,struct ucred *cred)
153 NCP_RQ_HEAD_S(23,53,p,cred);
154 ncp_rq_word_hl(rqp, object_type);
155 ncp_rq_pstring(rqp, object_name);
156 checkbad(ncp_request(conn,rqp));
157 if (rqp->rpsize < 54) {
158 printf("ncp_rp_size %d < 54\n", rqp->rpsize);
162 target->object_id = ncp_rp_dword_hl(rqp);
163 target->object_type = ncp_rp_word_hl(rqp);
164 ncp_rp_mem(rqp,(caddr_t)target->object_name, 48);
170 ncp_read(struct ncp_conn *conn, ncp_fh *file, struct uio *uiop, struct ucred *cred) {
171 int error = 0, len = 0, retlen=0, tsiz, burstio;
174 tsiz = uiop->uio_resid;
176 burstio = (ncp_burst_enabled && tsiz > conn->buffer_size);
183 len = min(4096 - (uiop->uio_offset % 4096), tsiz);
184 len = min(len, conn->buffer_size);
185 NCP_RQ_HEAD(72,uiop->uio_procp,cred);
187 ncp_rq_mem(rqp, (caddr_t)file, 6);
188 ncp_rq_dword(rqp, htonl(uiop->uio_offset));
189 ncp_rq_word(rqp, htons(len));
190 checkbad(ncp_request(conn,rqp));
191 retlen = ncp_rp_word_hl(rqp);
192 if (uiop->uio_offset & 1)
194 error = nwfs_mbuftouio(&rqp->mrp,uiop,retlen,&rqp->bpos);
198 error = ncp_burst_read(conn, file, tsiz, &len, &retlen, uiop, cred);
210 ncp_write(struct ncp_conn *conn, ncp_fh *file, struct uio *uiop, struct ucred *cred)
212 int error = 0, len, tsiz, backup;
215 if (uiop->uio_iovcnt != 1) {
216 printf("%s: can't handle iovcnt>1 !!!\n", __FUNCTION__);
219 tsiz = uiop->uio_resid;
221 len = min(4096 - (uiop->uio_offset % 4096), tsiz);
222 len = min(len, conn->buffer_size);
227 NCP_RQ_HEAD(73,uiop->uio_procp,cred);
229 ncp_rq_mem(rqp, (caddr_t)file, 6);
230 ncp_rq_dword(rqp, htonl(uiop->uio_offset));
231 ncp_rq_word_hl(rqp, len);
232 nwfs_uiotombuf(uiop,&rqp->mrq,len,&rqp->bpos);
233 checkbad(ncp_request(conn,rqp));
239 uiop->uio_iov->iov_base -= backup;
240 uiop->uio_iov->iov_len += backup;
241 uiop->uio_offset -= backup;
242 uiop->uio_resid += backup;
248 uiop->uio_resid = tsiz;
250 case NWE_INSUFFICIENT_SPACE: