1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
|
/* $NetBSD: putter.c,v 1.39 2021/09/26 01:16:09 thorpej Exp $ */
/*
* Copyright (c) 2006, 2007 Antti Kantee. All Rights Reserved.
*
* Development of this software was supported by the
* Ulla Tuominen Foundation and the Finnish Cultural Foundation and the
* Research Foundation of Helsinki University of Technology
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/*
* Pass-to-Userspace TransporTER: generic kernel-user request-response
* transport interface.
*/
#include <sys/cdefs.h>
__KERNEL_RCSID(0, "$NetBSD: putter.c,v 1.39 2021/09/26 01:16:09 thorpej Exp $");
#include <sys/param.h>
#include <sys/systm.h>
#include <sys/conf.h>
#include <sys/file.h>
#include <sys/filedesc.h>
#include <sys/kmem.h>
#include <sys/poll.h>
#include <sys/stat.h>
#include <sys/socketvar.h>
#include <sys/module.h>
#include <sys/kauth.h>
#include <dev/putter/putter_sys.h>
/*
* Device routines. These are for when /dev/putter is initially
* opened before it has been cloned.
*/
dev_type_open(puttercdopen);
dev_type_close(puttercdclose);
dev_type_ioctl(puttercdioctl);
/* dev */
const struct cdevsw putter_cdevsw = {
.d_open = puttercdopen,
.d_close = puttercdclose,
.d_read = noread,
.d_write = nowrite,
.d_ioctl = noioctl,
.d_stop = nostop,
.d_tty = notty,
.d_poll = nopoll,
.d_mmap = nommap,
.d_kqfilter = nokqfilter,
.d_discard = nodiscard,
.d_flag = D_OTHER
};
/*
* Configuration data.
*
* This is static-size for now. Will be redone for devfs.
*/
#define PUTTER_CONFSIZE 16
static struct putter_config {
int pc_minor;
int (*pc_config)(int, int, int);
} putterconf[PUTTER_CONFSIZE];
static int
putter_configure(dev_t dev, int flags, int fmt, int fd)
{
struct putter_config *pc;
/* are we the catch-all node? */
if (minor(dev) == PUTTER_MINOR_WILDCARD
|| minor(dev) == PUTTER_MINOR_COMPAT)
return 0;
/* nopes? try to configure us */
for (pc = putterconf; pc->pc_config; pc++)
if (minor(dev) == pc->pc_minor)
return pc->pc_config(fd, flags, fmt);
return ENXIO;
}
int
putter_register(putter_config_fn pcfn, int minor)
{
int i;
for (i = 0; i < PUTTER_CONFSIZE; i++)
if (putterconf[i].pc_config == NULL)
break;
if (i == PUTTER_CONFSIZE)
return EBUSY;
putterconf[i].pc_minor = minor;
putterconf[i].pc_config = pcfn;
return 0;
}
/*
* putter instance structures. these are always allocated and freed
* from the context of the transport user.
*/
struct putter_instance {
pid_t pi_pid;
int pi_idx;
int pi_fd;
struct selinfo pi_sel;
void *pi_private;
struct putter_ops *pi_pop;
uint8_t *pi_curput;
size_t pi_curres;
void *pi_curopaq;
struct timespec pi_atime;
struct timespec pi_mtime;
struct timespec pi_btime;
TAILQ_ENTRY(putter_instance) pi_entries;
};
#define PUTTER_EMBRYO ((void *)-1) /* before attach */
#define PUTTER_DEAD ((void *)-2) /* after detach */
static TAILQ_HEAD(, putter_instance) putter_ilist
= TAILQ_HEAD_INITIALIZER(putter_ilist);
static int get_pi_idx(struct putter_instance *);
#ifdef DEBUG
#ifndef PUTTERDEBUG
#define PUTTERDEBUG
#endif
#endif
#ifdef PUTTERDEBUG
int putterdebug = 0;
#define DPRINTF(x) if (putterdebug > 0) printf x
#define DPRINTF_VERBOSE(x) if (putterdebug > 1) printf x
#else
#define DPRINTF(x)
#define DPRINTF_VERBOSE(x)
#endif
/*
* public init / deinit
*/
/* protects both the list and the contents of the list elements */
static kmutex_t pi_mtx;
void putterattach(void);
void
putterattach(void)
{
mutex_init(&pi_mtx, MUTEX_DEFAULT, IPL_NONE);
}
#if 0
void
putter_destroy(void)
{
mutex_destroy(&pi_mtx);
}
#endif
/*
* fd routines, for cloner
*/
static int putter_fop_read(file_t *, off_t *, struct uio *,
kauth_cred_t, int);
static int putter_fop_write(file_t *, off_t *, struct uio *,
kauth_cred_t, int);
static int putter_fop_ioctl(file_t*, u_long, void *);
static int putter_fop_poll(file_t *, int);
static int putter_fop_stat(file_t *, struct stat *);
static int putter_fop_close(file_t *);
static int putter_fop_kqfilter(file_t *, struct knote *);
static const struct fileops putter_fileops = {
.fo_name = "putter",
.fo_read = putter_fop_read,
.fo_write = putter_fop_write,
.fo_ioctl = putter_fop_ioctl,
.fo_fcntl = fnullop_fcntl,
.fo_poll = putter_fop_poll,
.fo_stat = putter_fop_stat,
.fo_close = putter_fop_close,
.fo_kqfilter = putter_fop_kqfilter,
.fo_restart = fnullop_restart,
};
static int
putter_fop_read(file_t *fp, off_t *off, struct uio *uio,
kauth_cred_t cred, int flags)
{
struct putter_instance *pi = fp->f_data;
size_t origres, moved;
int error;
KERNEL_LOCK(1, NULL);
getnanotime(&pi->pi_atime);
if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
printf("putter_fop_read: private %d not inited\n", pi->pi_idx);
KERNEL_UNLOCK_ONE(NULL);
return ENOENT;
}
if (pi->pi_curput == NULL) {
error = pi->pi_pop->pop_getout(pi->pi_private, uio->uio_resid,
fp->f_flag & O_NONBLOCK, &pi->pi_curput,
&pi->pi_curres, &pi->pi_curopaq);
if (error) {
KERNEL_UNLOCK_ONE(NULL);
return error;
}
}
origres = uio->uio_resid;
error = uiomove(pi->pi_curput, pi->pi_curres, uio);
moved = origres - uio->uio_resid;
DPRINTF(("putter_fop_read (%p): moved %zu bytes from %p, error %d\n",
pi, moved, pi->pi_curput, error));
KASSERT(pi->pi_curres >= moved);
pi->pi_curres -= moved;
pi->pi_curput += moved;
if (pi->pi_curres == 0) {
pi->pi_pop->pop_releaseout(pi->pi_private,
pi->pi_curopaq, error);
pi->pi_curput = NULL;
}
KERNEL_UNLOCK_ONE(NULL);
return error;
}
static int
putter_fop_write(file_t *fp, off_t *off, struct uio *uio,
kauth_cred_t cred, int flags)
{
struct putter_instance *pi = fp->f_data;
struct putter_hdr pth;
uint8_t *buf;
size_t frsize;
int error;
KERNEL_LOCK(1, NULL);
getnanotime(&pi->pi_mtime);
DPRINTF(("putter_fop_write (%p): writing response, resid %zu\n",
pi->pi_private, uio->uio_resid));
if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
printf("putter_fop_write: putter %d not inited\n", pi->pi_idx);
KERNEL_UNLOCK_ONE(NULL);
return ENOENT;
}
error = uiomove(&pth, sizeof(struct putter_hdr), uio);
if (error) {
KERNEL_UNLOCK_ONE(NULL);
return error;
}
/* Sorry mate, the kernel doesn't buffer. */
frsize = pth.pth_framelen - sizeof(struct putter_hdr);
if (uio->uio_resid < frsize) {
KERNEL_UNLOCK_ONE(NULL);
return EINVAL;
}
buf = kmem_alloc(frsize + sizeof(struct putter_hdr), KM_SLEEP);
memcpy(buf, &pth, sizeof(pth));
error = uiomove(buf+sizeof(struct putter_hdr), frsize, uio);
if (error == 0) {
pi->pi_pop->pop_dispatch(pi->pi_private,
(struct putter_hdr *)buf);
}
kmem_free(buf, frsize + sizeof(struct putter_hdr));
KERNEL_UNLOCK_ONE(NULL);
return error;
}
/*
* Poll query interface. The question is only if an event
* can be read from us.
*/
#define PUTTERPOLL_EVSET (POLLIN | POLLRDNORM | POLLRDBAND | POLLPRI)
static int
putter_fop_poll(file_t *fp, int events)
{
struct putter_instance *pi = fp->f_data;
int revents;
KERNEL_LOCK(1, NULL);
if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD) {
printf("putter_fop_ioctl: putter %d not inited\n", pi->pi_idx);
KERNEL_UNLOCK_ONE(NULL);
return ENOENT;
}
revents = events & (POLLOUT | POLLWRNORM | POLLWRBAND);
if ((events & PUTTERPOLL_EVSET) == 0) {
KERNEL_UNLOCK_ONE(NULL);
return revents;
}
/* check queue */
if (pi->pi_pop->pop_waitcount(pi->pi_private))
revents |= PUTTERPOLL_EVSET;
else
selrecord(curlwp, &pi->pi_sel);
KERNEL_UNLOCK_ONE(NULL);
return revents;
}
/*
* device close = forced unmount.
*
* unmounting is a frightfully complex operation to avoid races
*/
static int
putter_fop_close(file_t *fp)
{
struct putter_instance *pi = fp->f_data;
int rv;
DPRINTF(("putter_fop_close: device closed\n"));
KERNEL_LOCK(1, NULL);
restart:
mutex_enter(&pi_mtx);
/*
* First check if the driver was never born. In that case
* remove the instance from the list. If mount is attempted later,
* it will simply fail.
*/
if (pi->pi_private == PUTTER_EMBRYO) {
TAILQ_REMOVE(&putter_ilist, pi, pi_entries);
mutex_exit(&pi_mtx);
DPRINTF(("putter_fop_close: data associated with fp %p was "
"embryonic\n", fp));
goto out;
}
/*
* Next, analyze if unmount was called and the instance is dead.
* In this case we can just free the structure and go home, it
* was removed from the list by putter_rmprivate().
*/
if (pi->pi_private == PUTTER_DEAD) {
mutex_exit(&pi_mtx);
DPRINTF(("putter_fop_close: putter associated with fp %p (%d) "
"dead, freeing\n", fp, pi->pi_idx));
goto out;
}
/*
* So we have a reference. Proceed to unravel the
* underlying driver.
*/
mutex_exit(&pi_mtx);
/* hmm? suspicious locking? */
if (pi->pi_curput != NULL) {
pi->pi_pop->pop_releaseout(pi->pi_private, pi->pi_curopaq,
ENXIO);
pi->pi_curput = NULL;
}
while ((rv = pi->pi_pop->pop_close(pi->pi_private)) == ERESTART)
goto restart;
out:
KERNEL_UNLOCK_ONE(NULL);
/*
* Finally, release the instance information. It was already
* removed from the list by putter_rmprivate() and we know it's
* dead, so no need to lock.
*/
kmem_free(pi, sizeof(struct putter_instance));
return 0;
}
static int
putter_fop_stat(file_t *fp, struct stat *st)
{
struct putter_instance *pi = fp->f_data;
(void)memset(st, 0, sizeof(*st));
KERNEL_LOCK(1, NULL);
st->st_dev = makedev(cdevsw_lookup_major(&putter_cdevsw), pi->pi_idx);
st->st_atimespec = pi->pi_atime;
st->st_mtimespec = pi->pi_mtime;
st->st_ctimespec = st->st_birthtimespec = pi->pi_btime;
st->st_uid = kauth_cred_geteuid(fp->f_cred);
st->st_gid = kauth_cred_getegid(fp->f_cred);
st->st_mode = S_IFCHR;
KERNEL_UNLOCK_ONE(NULL);
return 0;
}
static int
putter_fop_ioctl(file_t *fp, u_long cmd, void *data)
{
/*
* work already done in sys_ioctl(). skip sanity checks to enable
* setting non-blocking fd on an embryotic driver.
*/
if (cmd == FIONBIO)
return 0;
return EINVAL;
}
/* kqueue stuff */
static void
filt_putterdetach(struct knote *kn)
{
struct putter_instance *pi = kn->kn_hook;
KERNEL_LOCK(1, NULL);
mutex_enter(&pi_mtx);
selremove_knote(&pi->pi_sel, kn);
mutex_exit(&pi_mtx);
KERNEL_UNLOCK_ONE(NULL);
}
static int
filt_putter(struct knote *kn, long hint)
{
struct putter_instance *pi = kn->kn_hook;
int error, rv;
KERNEL_LOCK(1, NULL);
error = 0;
mutex_enter(&pi_mtx);
if (pi->pi_private == PUTTER_EMBRYO || pi->pi_private == PUTTER_DEAD)
error = 1;
mutex_exit(&pi_mtx);
if (error) {
KERNEL_UNLOCK_ONE(NULL);
return 0;
}
kn->kn_data = pi->pi_pop->pop_waitcount(pi->pi_private);
rv = kn->kn_data != 0;
KERNEL_UNLOCK_ONE(NULL);
return rv;
}
static const struct filterops putter_filtops = {
.f_flags = FILTEROP_ISFD,
.f_attach = NULL,
.f_detach = filt_putterdetach,
.f_event = filt_putter,
};
static int
putter_fop_kqfilter(file_t *fp, struct knote *kn)
{
struct putter_instance *pi = fp->f_data;
KERNEL_LOCK(1, NULL);
switch (kn->kn_filter) {
case EVFILT_READ:
kn->kn_fop = &putter_filtops;
kn->kn_hook = pi;
mutex_enter(&pi_mtx);
selrecord_knote(&pi->pi_sel, kn);
mutex_exit(&pi_mtx);
break;
case EVFILT_WRITE:
kn->kn_fop = &seltrue_filtops;
break;
default:
KERNEL_UNLOCK_ONE(NULL);
return EINVAL;
}
KERNEL_UNLOCK_ONE(NULL);
return 0;
}
int
puttercdopen(dev_t dev, int flags, int fmt, struct lwp *l)
{
struct putter_instance *pi;
file_t *fp;
int error, fd, idx;
proc_t *p;
p = curproc;
pi = kmem_alloc(sizeof(struct putter_instance), KM_SLEEP);
mutex_enter(&pi_mtx);
idx = get_pi_idx(pi);
pi->pi_pid = p->p_pid;
pi->pi_idx = idx;
pi->pi_curput = NULL;
pi->pi_curres = 0;
pi->pi_curopaq = NULL;
getnanotime(&pi->pi_btime);
pi->pi_atime = pi->pi_mtime = pi->pi_btime;
selinit(&pi->pi_sel);
mutex_exit(&pi_mtx);
if ((error = fd_allocfile(&fp, &fd)) != 0)
goto bad1;
if ((error = putter_configure(dev, flags, fmt, fd)) != 0)
goto bad2;
DPRINTF(("puttercdopen: registered embryonic pmp for pid: %d\n",
pi->pi_pid));
error = fd_clone(fp, fd, FREAD|FWRITE, &putter_fileops, pi);
KASSERT(error == EMOVEFD);
return error;
bad2:
fd_abort(p, fp, fd);
bad1:
putter_detach(pi);
kmem_free(pi, sizeof(struct putter_instance));
return error;
}
int
puttercdclose(dev_t dev, int flags, int fmt, struct lwp *l)
{
panic("puttercdclose impossible\n");
return 0;
}
/*
* Set the private structure for the file descriptor. This is
* typically done immediately when the counterpart has knowledge
* about the private structure's address and the file descriptor
* (e.g. vfs mount routine).
*
* We only want to make sure that the caller had the right to open the
* device, we don't so much care about which context it gets in case
* the same process opened multiple (since they are equal at this point).
*/
struct putter_instance *
putter_attach(pid_t pid, int fd, void *ppriv, struct putter_ops *pop)
{
struct putter_instance *pi = NULL;
mutex_enter(&pi_mtx);
TAILQ_FOREACH(pi, &putter_ilist, pi_entries) {
if (pi->pi_pid == pid && pi->pi_private == PUTTER_EMBRYO) {
pi->pi_private = ppriv;
pi->pi_fd = fd;
pi->pi_pop = pop;
break;
}
}
mutex_exit(&pi_mtx);
DPRINTF(("putter_setprivate: pi at %p (%d/%d)\n", pi,
pi ? pi->pi_pid : 0, pi ? pi->pi_fd : 0));
return pi;
}
/*
* Remove fp <-> private mapping.
*/
void
putter_detach(struct putter_instance *pi)
{
mutex_enter(&pi_mtx);
TAILQ_REMOVE(&putter_ilist, pi, pi_entries);
pi->pi_private = PUTTER_DEAD;
mutex_exit(&pi_mtx);
seldestroy(&pi->pi_sel);
DPRINTF(("putter_nukebypmp: nuked %p\n", pi));
}
void
putter_notify(struct putter_instance *pi)
{
selnotify(&pi->pi_sel, 0, 0);
}
/* search sorted list of instances for free minor, sorted insert arg */
static int
get_pi_idx(struct putter_instance *pi_i)
{
struct putter_instance *pi;
int i;
KASSERT(mutex_owned(&pi_mtx));
i = 0;
TAILQ_FOREACH(pi, &putter_ilist, pi_entries) {
if (i != pi->pi_idx)
break;
i++;
}
pi_i->pi_private = PUTTER_EMBRYO;
if (pi == NULL)
TAILQ_INSERT_TAIL(&putter_ilist, pi_i, pi_entries);
else
TAILQ_INSERT_BEFORE(pi, pi_i, pi_entries);
return i;
}
MODULE(MODULE_CLASS_DRIVER, putter, NULL);
static int
putter_modcmd(modcmd_t cmd, void *arg)
{
#ifdef _MODULE
devmajor_t bmajor = NODEVMAJOR, cmajor = NODEVMAJOR;
switch (cmd) {
case MODULE_CMD_INIT:
putterattach();
return devsw_attach("putter", NULL, &bmajor,
&putter_cdevsw, &cmajor);
case MODULE_CMD_FINI:
return ENOTTY; /* XXX: putterdetach */
default:
return ENOTTY;
}
#else
if (cmd == MODULE_CMD_INIT)
return 0;
return ENOTTY;
#endif
}
|