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.10 2006/12/22 23:57:54 swildner 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.
64 ncp_str_dup(const char *s)
71 for (src = s; ;src++) {
72 if (copyin(src, &bt, 1)) return NULL;
76 dst = kmalloc(len, M_NCPDATA, M_WAITOK);
83 ncp_at_exit(struct thread *td)
85 struct ncp_conn *ncp, *nncp;
88 KKASSERT(td->td_proc);
89 cred = td->td_proc->p_ucred;
91 if (ncp_conn_putprochandles(td) == 0) return;
93 ncp_conn_locklist(LK_EXCLUSIVE, td);
94 for (ncp = SLIST_FIRST(&conn_list); ncp; ncp = nncp) {
95 nncp = SLIST_NEXT(ncp, nc_next);
96 if (ncp->ref_cnt != 0) continue;
97 if (ncp_conn_lock(ncp, td, cred, NCPM_READ|NCPM_EXECUTE|NCPM_WRITE))
99 if (ncp_disconnect(ncp) != 0)
100 ncp_conn_unlock(ncp, td);
102 ncp_conn_unlocklist(td);
109 if (at_exit(ncp_at_exit)) {
110 NCPFATAL("can't register at_exit handler\n");
113 callout_init(&ncp_timer_handle);
114 callout_reset(&ncp_timer_handle, NCP_TIMER_TICK, ncp_timer, NULL);
121 struct ncp_conn *ncp, *nncp;
122 struct thread *td = curthread; /* XXX */
125 KKASSERT(td->td_proc);
126 cred = td->td_proc->p_ucred;
128 callout_stop(&ncp_timer_handle);
129 rm_at_exit(ncp_at_exit);
130 ncp_conn_locklist(LK_EXCLUSIVE, td);
131 for (ncp = SLIST_FIRST(&conn_list); ncp; ncp = nncp) {
132 nncp = SLIST_NEXT(ncp, nc_next);
134 if (ncp_conn_lock(ncp, td, cred,NCPM_READ|NCPM_EXECUTE|NCPM_WRITE)) {
135 NCPFATAL("Can't lock connection !\n");
138 if (ncp_disconnect(ncp) != 0)
139 ncp_conn_unlock(ncp, td);
141 ncp_conn_unlocklist(td);
145 /* tick every second and check for watch dog packets and lost connections */
149 struct ncp_conn *conn;
151 if(ncp_conn_locklist(LK_SHARED | LK_NOWAIT, NULL) == 0) {
152 SLIST_FOREACH(conn, &conn_list, nc_next)
153 ncp_check_conn(conn);
154 ncp_conn_unlocklist(NULL);
156 callout_reset(&ncp_timer_handle, NCP_TIMER_TICK, ncp_timer, NULL);
160 ncp_get_bindery_object_id(struct ncp_conn *conn,
161 u_int16_t object_type, char *object_name,
162 struct ncp_bindery_object *target,
163 struct thread *td,struct ucred *cred)
168 NCP_RQ_HEAD_S(23,53,td,cred);
169 ncp_rq_word_hl(rqp, object_type);
170 ncp_rq_pstring(rqp, object_name);
171 checkbad(ncp_request(conn,rqp));
172 if (rqp->rpsize < 54) {
173 kprintf("ncp_rp_size %d < 54\n", rqp->rpsize);
177 target->object_id = ncp_rp_dword_hl(rqp);
178 target->object_type = ncp_rp_word_hl(rqp);
179 ncp_rp_mem(rqp,(caddr_t)target->object_name, 48);
185 ncp_read(struct ncp_conn *conn, ncp_fh *file, struct uio *uiop, struct ucred *cred) {
186 int error = 0, len = 0, retlen=0, tsiz, burstio;
189 tsiz = uiop->uio_resid;
191 burstio = (ncp_burst_enabled && tsiz > conn->buffer_size);
198 len = min(4096 - (uiop->uio_offset % 4096), tsiz);
199 len = min(len, conn->buffer_size);
200 NCP_RQ_HEAD(72,uiop->uio_td,cred);
202 ncp_rq_mem(rqp, (caddr_t)file, 6);
203 ncp_rq_dword(rqp, htonl(uiop->uio_offset));
204 ncp_rq_word(rqp, htons(len));
205 checkbad(ncp_request(conn,rqp));
206 retlen = ncp_rp_word_hl(rqp);
207 if (uiop->uio_offset & 1)
209 error = nwfs_mbuftouio(&rqp->mrp,uiop,retlen,&rqp->bpos);
213 error = ncp_burst_read(conn, file, tsiz, &len, &retlen, uiop, cred);
225 ncp_write(struct ncp_conn *conn, ncp_fh *file, struct uio *uiop, struct ucred *cred)
227 int error = 0, len, tsiz, backup;
230 if (uiop->uio_iovcnt != 1) {
231 kprintf("%s: can't handle iovcnt>1 !!!\n", __func__);
234 tsiz = uiop->uio_resid;
236 len = min(4096 - (uiop->uio_offset % 4096), tsiz);
237 len = min(len, conn->buffer_size);
239 kprintf("gotcha!\n");
242 NCP_RQ_HEAD(73,uiop->uio_td,cred);
244 ncp_rq_mem(rqp, (caddr_t)file, 6);
245 ncp_rq_dword(rqp, htonl(uiop->uio_offset));
246 ncp_rq_word_hl(rqp, len);
247 nwfs_uiotombuf(uiop,&rqp->mrq,len,&rqp->bpos);
248 checkbad(ncp_request(conn,rqp));
254 uiop->uio_iov->iov_base -= backup;
255 uiop->uio_iov->iov_len += backup;
256 uiop->uio_offset -= backup;
257 uiop->uio_resid += backup;
263 uiop->uio_resid = tsiz;
265 case NWE_INSUFFICIENT_SPACE: