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
31 * $FreeBSD: src/sys/netncp/ncp_conn.c,v 1.3.2.5 2001/02/22 08:54:11 bp Exp $
35 #include <sys/param.h>
36 #include <sys/systm.h>
37 #include <sys/kernel.h>
38 #include <sys/malloc.h>
42 #include <sys/sysctl.h>
48 SLIST_HEAD(ncp_handle_head,ncp_handle);
50 int ncp_burst_enabled = 1;
52 struct ncp_conn_head conn_list={NULL};
53 static int ncp_conn_cnt = 0;
54 static int ncp_next_ref = 1;
55 static struct lock listlock;
57 struct ncp_handle_head lhlist={NULL};
58 static int ncp_next_handle = 1;
59 static struct lock lhlock;
61 static int ncp_sysctl_connstat(SYSCTL_HANDLER_ARGS);
62 static int ncp_conn_lock_any(struct ncp_conn *conn, struct thread *td,
65 SYSCTL_DECL(_net_ncp);
66 SYSCTL_INT (_net_ncp, OID_AUTO, burst_enabled, CTLFLAG_RD, &ncp_burst_enabled, 0, "");
67 SYSCTL_INT (_net_ncp, OID_AUTO, conn_cnt, CTLFLAG_RD, &ncp_conn_cnt, 0, "");
68 SYSCTL_PROC(_net_ncp, OID_AUTO, conn_stat, CTLFLAG_RD|CTLTYPE_OPAQUE,
69 NULL, 0, ncp_sysctl_connstat, "S,connstat", "Connections list");
71 MALLOC_DEFINE(M_NCPDATA, "NCP data", "NCP private data");
75 lockinit(&listlock, "ncpll", 0, 0);
76 lockinit(&lhlock, "ncplh", 0, 0);
81 ncp_conn_locklist(int flags, struct thread *td)
83 return lockmgr(&listlock, flags | LK_CANRECURSE);
87 ncp_conn_unlocklist(struct thread *td)
89 lockmgr(&listlock, LK_RELEASE);
93 ncp_conn_access(struct ncp_conn *conn, struct ucred *cred, mode_t mode)
97 if (cred == NOCRED || ncp_suser(cred) == 0 ||
98 cred->cr_uid == conn->nc_owner->cr_uid)
101 if (!groupmember(conn->nc_group, cred))
103 error = (conn->li.access_mode & mode) == mode ? 0 : EACCES;
108 * NOTE: The lockmgr() call is protected by nc_lwant
111 ncp_conn_lock_any(struct ncp_conn *conn, struct thread *td, struct ucred *cred)
115 if (conn->nc_id == 0)
117 error = lockmgr(&conn->nc_lock, LK_EXCLUSIVE | LK_CANRECURSE);
118 if (error == ERESTART)
120 error = ncp_chkintr(conn, td);
122 lockmgr(&conn->nc_lock, LK_RELEASE);
126 if (conn->nc_id == 0) {
127 lockmgr(&conn->nc_lock, LK_RELEASE);
130 conn->td = td; /* who currently operates */
136 * NOTE: The lockmgr() call should be protected by nc_lwant, but it
137 * is unclear whether callers have coded this correctly.
140 ncp_conn_lock(struct ncp_conn *conn, struct thread *td,
141 struct ucred *cred, int mode)
145 error = ncp_conn_access(conn, cred, mode);
148 return ncp_conn_lock_any(conn, td, cred);
152 * Lock conn but unlock connlist
155 ncp_conn_lock2(struct ncp_conn *conn, struct thread *td,
156 struct ucred *cred, int mode)
160 error = ncp_conn_access(conn, cred, mode);
162 ncp_conn_unlocklist(td);
166 ncp_conn_unlocklist(td);
167 error = ncp_conn_lock_any(conn, td, cred);
169 if (conn->nc_lwant == 0) {
170 wakeup(&conn->nc_lwant);
176 ncp_conn_unlock(struct ncp_conn *conn, struct thread *td) {
178 * note, that LK_RELASE will do wakeup() instead of wakeup_one().
179 * this will do a little overhead
181 lockmgr(&conn->nc_lock, LK_RELEASE);
185 ncp_conn_assert_locked(struct ncp_conn *conn, const char *checker,
188 if (lockstatus(&conn->nc_lock, NULL) == LK_EXCLUSIVE)
190 kprintf("%s: connection isn't locked!\n", checker);
195 * create, fill with defaults and return in locked state
198 ncp_conn_alloc(struct thread *td, struct ucred *cred, struct ncp_conn **conn)
201 struct ncp_conn *ncp;
203 ncp = kmalloc(sizeof(struct ncp_conn), M_NCPDATA, M_WAITOK | M_ZERO);
205 lockinit(&ncp->nc_lock, "ncplck", 0, 0);
207 ncp->nc_id = ncp_next_ref++;
208 ncp->nc_owner = cred;
210 ncp->connid = 0xFFFF;
211 ncp_conn_lock_any(ncp, td, ncp->nc_owner);
213 ncp_conn_locklist(LK_EXCLUSIVE, td);
214 SLIST_INSERT_HEAD(&conn_list,ncp,nc_next);
215 ncp_conn_unlocklist(td);
220 * Remove the connection, on entry it must be locked
223 ncp_conn_free(struct ncp_conn *ncp)
226 struct ncp_conn *ncp1;
229 NCPFATAL("conn==NULL !\n");
232 if (ncp->nc_id == 0) {
233 kprintf("already!!!!\n");
236 error = ncp_conn_assert_locked(ncp, __func__, ncp->td);
240 NCPFATAL("there are %d references left\n",ncp->ref_cnt);
245 * Mark conn as died and wait for other process. This also
246 * interlocks against other ncp_conn_free() ops.
249 while (ncp->nc_lwant) {
250 ncp_conn_unlock(ncp, ncp->td);
251 kprintf("lwant = %d\n", ncp->nc_lwant);
252 tsleep(&ncp->nc_lwant, 0, "ncpdr", 2*hz);
253 lockmgr(&ncp->nc_lock, LK_EXCLUSIVE);
255 ncp_conn_locklist(LK_EXCLUSIVE, ncp->td);
256 SLIST_FOREACH(ncp1, &conn_list, nc_next) {
260 KKASSERT(ncp1 == ncp);
261 SLIST_REMOVE(&conn_list, ncp, ncp_conn, nc_next);
263 ncp_conn_unlocklist(ncp->td);
264 if (ncp->li.user) kfree(ncp->li.user, M_NCPDATA);
265 if (ncp->li.password) kfree(ncp->li.password, M_NCPDATA);
266 crfree(ncp->nc_owner);
267 kfree(ncp, M_NCPDATA);
272 * Lookup connection by handle, return a locked conn descriptor
275 ncp_conn_getbyref(int ref,struct thread *td,struct ucred *cred, int mode, struct ncp_conn **connpp){
276 struct ncp_conn *ncp;
279 ncp_conn_locklist(LK_SHARED, td);
280 SLIST_FOREACH(ncp, &conn_list, nc_next)
281 if (ncp->nc_id == ref) break;
283 ncp_conn_unlocklist(td);
286 error = ncp_conn_lock2(ncp, td, cred, mode);
292 * find attached, but not logged in connection to specified server
295 ncp_conn_getattached(struct ncp_conn_args *li,struct thread *td,struct ucred *cred,int mode, struct ncp_conn **connpp){
296 struct ncp_conn *ncp, *ncp2=NULL;
299 ncp_conn_locklist(LK_SHARED, td);
300 SLIST_FOREACH(ncp, &conn_list, nc_next) {
301 if ((ncp->flags & NCPFL_LOGGED) != 0 ||
302 strcmp(ncp->li.server,li->server) != 0 ||
303 ncp->li.addr.addr.sa_len != li->addr.addr.sa_len ||
304 bcmp(&ncp->li.addr.addr,&ncp->li.addr.addr,li->addr.addr.sa_len) != 0)
306 if (ncp_suser(cred) == 0 ||
307 cred->cr_uid == ncp->nc_owner->cr_uid)
309 error = ncp_conn_access(ncp,cred,mode);
310 if (!error && ncp2 == NULL)
313 if (ncp == NULL) ncp = ncp2;
315 ncp_conn_unlocklist(td);
318 error = ncp_conn_lock2(ncp,td,cred,mode);
325 * Lookup connection by server/user pair, return a locked conn descriptor.
326 * if li is NULL or server/user pair incomplete, try to select best connection
328 * Connection selected in next order:
329 * 1. Try to search conn with ucred owner, if li is NULL also find a primary
330 * 2. If 1. fails try to get first suitable shared connection
331 * 3. If 2. fails then nothing can help to poor ucred owner
335 ncp_conn_getbyli(struct ncp_conn_args *li, struct thread *td,
336 struct ucred *cred, int mode, struct ncp_conn **connpp)
338 struct ncp_conn *ncp, *ncp2=NULL;
339 int error=0, partial, haveserv;
341 partial = (li == NULL || li->server[0] == 0 || li->user == NULL);
342 haveserv = (li && li->server[0]);
343 ncp_conn_locklist(LK_SHARED, td);
344 SLIST_FOREACH(ncp, &conn_list, nc_next) {
346 if (cred->cr_uid == ncp->nc_owner->cr_uid) {
348 if (strcmp(ncp->li.server,li->server) == 0)
351 if (ncp->flags & NCPFL_PRIMARY)
358 if (strcmp(ncp->li.server,li->server) != 0 ||
359 ncp->li.user == NULL ||
360 strcmp(ncp->li.user,li->user) != 0)
362 if (cred->cr_uid == ncp->nc_owner->cr_uid)
364 if (ncp_suser(cred) == 0)
367 error = ncp_conn_access(ncp,cred,mode);
368 if (!error && ncp2 == NULL)
371 if (ncp == NULL) ncp = ncp2;
373 ncp_conn_unlocklist(td);
376 error = ncp_conn_lock2(ncp,td,cred,mode);
383 * Set primary connection flag, since it have sence only for an owner,
384 * only owner can modify this flag.
385 * connection expected to be locked.
388 ncp_conn_setprimary(struct ncp_conn *conn, int on){
389 struct ncp_conn *ncp=NULL;
391 if (conn->ucred->cr_uid != conn->nc_owner->cr_uid)
393 ncp_conn_locklist(LK_SHARED, conn->td);
394 SLIST_FOREACH(ncp, &conn_list, nc_next) {
395 if (conn->ucred->cr_uid == ncp->nc_owner->cr_uid)
396 ncp->flags &= ~NCPFL_PRIMARY;
398 ncp_conn_unlocklist(conn->td);
400 conn->flags |= NCPFL_PRIMARY;
404 * Lease conn to given proc, returning unique handle
405 * problem: how locks should be applied ?
408 ncp_conn_gethandle(struct ncp_conn *conn, struct thread *td, struct ncp_handle **handle){
409 struct ncp_handle *refp;
411 lockmgr(&lhlock, LK_EXCLUSIVE);
412 SLIST_FOREACH(refp, &lhlist, nh_next)
413 if (refp->nh_conn == conn && td == refp->nh_td) break;
418 lockmgr(&lhlock, LK_RELEASE);
421 refp = kmalloc(sizeof(struct ncp_handle), M_NCPDATA,
423 SLIST_INSERT_HEAD(&lhlist,refp,nh_next);
426 refp->nh_conn = conn;
427 refp->nh_id = ncp_next_handle++;
430 lockmgr(&lhlock, LK_RELEASE);
434 * release reference, if force - ignore refcount
437 ncp_conn_puthandle(struct ncp_handle *handle, struct thread *td, int force) {
438 struct ncp_handle *refp = handle;
440 lockmgr(&lhlock, LK_EXCLUSIVE);
442 refp->nh_conn->ref_cnt--;
444 refp->nh_conn->ref_cnt -= refp->nh_ref;
447 if (refp->nh_ref == 0) {
448 SLIST_REMOVE(&lhlist, refp, ncp_handle, nh_next);
449 kfree(refp, M_NCPDATA);
451 lockmgr(&lhlock, LK_RELEASE);
458 ncp_conn_findhandle(int connHandle, struct thread *td, struct ncp_handle **handle) {
459 struct ncp_handle *refp;
461 lockmgr(&lhlock, LK_SHARED);
462 SLIST_FOREACH(refp, &lhlist, nh_next)
463 if (refp->nh_td == td && refp->nh_id == connHandle) break;
464 lockmgr(&lhlock, LK_RELEASE);
472 * Clear handles associated with specified process
475 ncp_conn_putprochandles(struct thread *td) {
476 struct ncp_handle *hp, *nhp;
479 lockmgr(&lhlock, LK_EXCLUSIVE);
480 for (hp = SLIST_FIRST(&lhlist); hp; hp = nhp) {
481 nhp = SLIST_NEXT(hp, nh_next);
482 if (hp->nh_td != td) continue;
484 hp->nh_conn->ref_cnt -= hp->nh_ref;
485 SLIST_REMOVE(&lhlist, hp, ncp_handle, nh_next);
486 kfree(hp, M_NCPDATA);
488 lockmgr(&lhlock, LK_RELEASE);
492 * remove references in all possible connections,
493 * XXX - possible problem is a locked list.
496 ncp_conn_list_rm_ref(pid_t pid) {
497 struct ncp_conn *ncp;
499 ncp_conn_locklist(LK_SHARED, NULL);
500 SLIST_FOREACH(ncp, &conn_list, nc_next) {
501 ncp_conn_rm_ref(ncp,pid,1);
503 ncp_conn_unlocklist(NULL);
508 ncp_conn_getinfo(struct ncp_conn *ncp, struct ncp_conn_stat *ncs) {
509 bzero(ncs,sizeof(*ncs));
511 ncs->li.user = ncs->user;
513 strcpy(ncs->user, ncp->li.user);
514 ncs->li.password = NULL;
515 ncs->connRef = ncp->nc_id;
516 ncs->ref_cnt = ncp->ref_cnt;
517 ncs->connid = ncp->connid;
518 ncs->owner = ncp->nc_owner->cr_uid;
519 ncs->group = ncp->nc_group;
520 ncs->flags = ncp->flags;
521 ncs->buffer_size = ncp->buffer_size;
526 ncp_sysctl_connstat(SYSCTL_HANDLER_ARGS) {
528 struct ncp_conn_stat ncs;
529 struct ncp_conn *ncp;
530 /* struct ucred *cred = req->p->p_ucred;*/
533 ncp_conn_locklist(LK_SHARED, req->td);
534 error = SYSCTL_OUT(req, &ncp_conn_cnt, sizeof(ncp_conn_cnt));
535 SLIST_FOREACH(ncp, &conn_list, nc_next) {
537 /* I can't do conn_lock while list is locked */
540 ncp_conn_getinfo(ncp, &ncs);
542 bzero(&ncs,sizeof(ncs));
543 ncs.connRef = ncp->nc_id;
544 strcpy(ncs.li.server,"***");
547 error = SYSCTL_OUT(req, &ncs, sizeof(ncs));
549 ncp_conn_unlocklist(req->td);