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.4 2003/08/07 21:17:38 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>
53 int ncp_debuglevel = 0;
55 struct callout_handle ncp_timer_handle;
57 static void ncp_at_exit(struct thread *td);
58 static void ncp_timer(void *arg);
61 * duplicate string from user space. It should be very-very slow.
70 if (copyin(p, &bt, 1)) return NULL;
74 MALLOC(p, char*, len, M_NCPDATA, M_WAITOK);
81 ncp_at_exit(struct thread *td)
83 struct ncp_conn *ncp, *nncp;
86 KKASSERT(td->td_proc);
87 cred = td->td_proc->p_ucred;
89 if (ncp_conn_putprochandles(td) == 0) return;
91 ncp_conn_locklist(LK_EXCLUSIVE, td);
92 for (ncp = SLIST_FIRST(&conn_list); ncp; ncp = nncp) {
93 nncp = SLIST_NEXT(ncp, nc_next);
94 if (ncp->ref_cnt != 0) continue;
95 if (ncp_conn_lock(ncp, td, cred, NCPM_READ|NCPM_EXECUTE|NCPM_WRITE))
97 if (ncp_disconnect(ncp) != 0)
98 ncp_conn_unlock(ncp, td);
100 ncp_conn_unlocklist(td);
107 if (at_exit(ncp_at_exit)) {
108 NCPFATAL("can't register at_exit handler\n");
111 ncp_timer_handle = timeout(ncp_timer,NULL,NCP_TIMER_TICK);
118 struct ncp_conn *ncp, *nncp;
119 struct thread *td = curthread; /* XXX */
122 KKASSERT(td->td_proc);
123 cred = td->td_proc->p_ucred;
125 untimeout(ncp_timer,NULL,ncp_timer_handle);
126 rm_at_exit(ncp_at_exit);
127 ncp_conn_locklist(LK_EXCLUSIVE, td);
128 for (ncp = SLIST_FIRST(&conn_list); ncp; ncp = nncp) {
129 nncp = SLIST_NEXT(ncp, nc_next);
131 if (ncp_conn_lock(ncp, td, cred,NCPM_READ|NCPM_EXECUTE|NCPM_WRITE)) {
132 NCPFATAL("Can't lock connection !\n");
135 if (ncp_disconnect(ncp) != 0)
136 ncp_conn_unlock(ncp, td);
138 ncp_conn_unlocklist(td);
142 /* tick every second and check for watch dog packets and lost connections */
144 ncp_timer(void *arg){
145 struct ncp_conn *conn;
147 if(ncp_conn_locklist(LK_SHARED | LK_NOWAIT, NULL) == 0) {
148 SLIST_FOREACH(conn, &conn_list, nc_next)
149 ncp_check_conn(conn);
150 ncp_conn_unlocklist(NULL);
152 ncp_timer_handle = timeout(ncp_timer,NULL,NCP_TIMER_TICK);
156 ncp_get_bindery_object_id(struct ncp_conn *conn,
157 u_int16_t object_type, char *object_name,
158 struct ncp_bindery_object *target,
159 struct thread *td,struct ucred *cred)
164 NCP_RQ_HEAD_S(23,53,td,cred);
165 ncp_rq_word_hl(rqp, object_type);
166 ncp_rq_pstring(rqp, object_name);
167 checkbad(ncp_request(conn,rqp));
168 if (rqp->rpsize < 54) {
169 printf("ncp_rp_size %d < 54\n", rqp->rpsize);
173 target->object_id = ncp_rp_dword_hl(rqp);
174 target->object_type = ncp_rp_word_hl(rqp);
175 ncp_rp_mem(rqp,(caddr_t)target->object_name, 48);
181 ncp_read(struct ncp_conn *conn, ncp_fh *file, struct uio *uiop, struct ucred *cred) {
182 int error = 0, len = 0, retlen=0, tsiz, burstio;
185 tsiz = uiop->uio_resid;
187 burstio = (ncp_burst_enabled && tsiz > conn->buffer_size);
194 len = min(4096 - (uiop->uio_offset % 4096), tsiz);
195 len = min(len, conn->buffer_size);
196 NCP_RQ_HEAD(72,uiop->uio_td,cred);
198 ncp_rq_mem(rqp, (caddr_t)file, 6);
199 ncp_rq_dword(rqp, htonl(uiop->uio_offset));
200 ncp_rq_word(rqp, htons(len));
201 checkbad(ncp_request(conn,rqp));
202 retlen = ncp_rp_word_hl(rqp);
203 if (uiop->uio_offset & 1)
205 error = nwfs_mbuftouio(&rqp->mrp,uiop,retlen,&rqp->bpos);
209 error = ncp_burst_read(conn, file, tsiz, &len, &retlen, uiop, cred);
221 ncp_write(struct ncp_conn *conn, ncp_fh *file, struct uio *uiop, struct ucred *cred)
223 int error = 0, len, tsiz, backup;
226 if (uiop->uio_iovcnt != 1) {
227 printf("%s: can't handle iovcnt>1 !!!\n", __FUNCTION__);
230 tsiz = uiop->uio_resid;
232 len = min(4096 - (uiop->uio_offset % 4096), tsiz);
233 len = min(len, conn->buffer_size);
238 NCP_RQ_HEAD(73,uiop->uio_td,cred);
240 ncp_rq_mem(rqp, (caddr_t)file, 6);
241 ncp_rq_dword(rqp, htonl(uiop->uio_offset));
242 ncp_rq_word_hl(rqp, len);
243 nwfs_uiotombuf(uiop,&rqp->mrq,len,&rqp->bpos);
244 checkbad(ncp_request(conn,rqp));
250 uiop->uio_iov->iov_base -= backup;
251 uiop->uio_iov->iov_len += backup;
252 uiop->uio_offset -= backup;
253 uiop->uio_resid += backup;
259 uiop->uio_resid = tsiz;
261 case NWE_INSUFFICIENT_SPACE: