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.6 2004/09/16 23:01:34 joerg 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 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 callout_init(&ncp_timer_handle);
112 callout_reset(&ncp_timer_handle, NCP_TIMER_TICK, ncp_timer, NULL);
119 struct ncp_conn *ncp, *nncp;
120 struct thread *td = curthread; /* XXX */
123 KKASSERT(td->td_proc);
124 cred = td->td_proc->p_ucred;
126 callout_stop(&ncp_timer_handle);
127 rm_at_exit(ncp_at_exit);
128 ncp_conn_locklist(LK_EXCLUSIVE, td);
129 for (ncp = SLIST_FIRST(&conn_list); ncp; ncp = nncp) {
130 nncp = SLIST_NEXT(ncp, nc_next);
132 if (ncp_conn_lock(ncp, td, cred,NCPM_READ|NCPM_EXECUTE|NCPM_WRITE)) {
133 NCPFATAL("Can't lock connection !\n");
136 if (ncp_disconnect(ncp) != 0)
137 ncp_conn_unlock(ncp, td);
139 ncp_conn_unlocklist(td);
143 /* tick every second and check for watch dog packets and lost connections */
147 struct ncp_conn *conn;
149 if(ncp_conn_locklist(LK_SHARED | LK_NOWAIT, NULL) == 0) {
150 SLIST_FOREACH(conn, &conn_list, nc_next)
151 ncp_check_conn(conn);
152 ncp_conn_unlocklist(NULL);
154 callout_reset(&ncp_timer_handle, NCP_TIMER_TICK, ncp_timer, NULL);
158 ncp_get_bindery_object_id(struct ncp_conn *conn,
159 u_int16_t object_type, char *object_name,
160 struct ncp_bindery_object *target,
161 struct thread *td,struct ucred *cred)
166 NCP_RQ_HEAD_S(23,53,td,cred);
167 ncp_rq_word_hl(rqp, object_type);
168 ncp_rq_pstring(rqp, object_name);
169 checkbad(ncp_request(conn,rqp));
170 if (rqp->rpsize < 54) {
171 printf("ncp_rp_size %d < 54\n", rqp->rpsize);
175 target->object_id = ncp_rp_dword_hl(rqp);
176 target->object_type = ncp_rp_word_hl(rqp);
177 ncp_rp_mem(rqp,(caddr_t)target->object_name, 48);
183 ncp_read(struct ncp_conn *conn, ncp_fh *file, struct uio *uiop, struct ucred *cred) {
184 int error = 0, len = 0, retlen=0, tsiz, burstio;
187 tsiz = uiop->uio_resid;
189 burstio = (ncp_burst_enabled && tsiz > conn->buffer_size);
196 len = min(4096 - (uiop->uio_offset % 4096), tsiz);
197 len = min(len, conn->buffer_size);
198 NCP_RQ_HEAD(72,uiop->uio_td,cred);
200 ncp_rq_mem(rqp, (caddr_t)file, 6);
201 ncp_rq_dword(rqp, htonl(uiop->uio_offset));
202 ncp_rq_word(rqp, htons(len));
203 checkbad(ncp_request(conn,rqp));
204 retlen = ncp_rp_word_hl(rqp);
205 if (uiop->uio_offset & 1)
207 error = nwfs_mbuftouio(&rqp->mrp,uiop,retlen,&rqp->bpos);
211 error = ncp_burst_read(conn, file, tsiz, &len, &retlen, uiop, cred);
223 ncp_write(struct ncp_conn *conn, ncp_fh *file, struct uio *uiop, struct ucred *cred)
225 int error = 0, len, tsiz, backup;
228 if (uiop->uio_iovcnt != 1) {
229 printf("%s: can't handle iovcnt>1 !!!\n", __FUNCTION__);
232 tsiz = uiop->uio_resid;
234 len = min(4096 - (uiop->uio_offset % 4096), tsiz);
235 len = min(len, conn->buffer_size);
240 NCP_RQ_HEAD(73,uiop->uio_td,cred);
242 ncp_rq_mem(rqp, (caddr_t)file, 6);
243 ncp_rq_dword(rqp, htonl(uiop->uio_offset));
244 ncp_rq_word_hl(rqp, len);
245 nwfs_uiotombuf(uiop,&rqp->mrq,len,&rqp->bpos);
246 checkbad(ncp_request(conn,rqp));
252 uiop->uio_iov->iov_base -= backup;
253 uiop->uio_iov->iov_len += backup;
254 uiop->uio_offset -= backup;
255 uiop->uio_resid += backup;
261 uiop->uio_resid = tsiz;
263 case NWE_INSUFFICIENT_SPACE: