2
* Copyright (C) 2004, 2005 Mike Wray <mike.wray@hp.com>
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by the
6
* Free Software Foundation; either version 2 of the License, or (at your
7
* option) any later version.
9
* This program is distributed in the hope that it will be useful, but
10
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* You should have received a copy of the GNU General Public License along
15
* with this program; if not, write to the Free software Foundation, Inc.,
16
* 59 Temple Place, suite 330, Boston, MA 02111-1307 USA
19
#include <linux/config.h>
20
#include <linux/module.h>
22
#include <linux/types.h>
23
#include <linux/kernel.h>
24
#include <linux/errno.h>
26
#include <asm/uaccess.h>
28
#include <linux/slab.h>
30
#include <linux/proc_fs.h>
31
#include <linux/string.h>
33
#include <linux/net.h>
35
#include <linux/inet.h>
36
#include <linux/netdevice.h>
43
#include "vnet_eval.h"
44
#include "vnet_forward.h"
47
#include "kernel_stream.h"
48
#include "mem_stream.h"
49
#include "sys_string.h"
51
#include "sxpr_parser.h"
53
#define MODULE_NAME "VNET"
60
* Kernel interface to files in /proc.
61
* todo: Add a sysfs interface using kobject.
64
#define PROC_ROOT "/proc/"
65
#define PROC_ROOT_LEN 6
66
#define MODULE_ROOT PROC_ROOT "vnet"
76
typedef struct proc_dir_entry ProcEntry;
77
typedef struct inode Inode;
78
typedef struct file File;
80
static int proc_open_fn(struct inode *inode, File *file);
81
//static ssize_t proc_read_fn(File *file, char *buffer, size_t count, loff_t *offset);
82
//static ssize_t proc_write_fn(File *file, const char *buffer, size_t count, loff_t *offset) ;
83
//static int proc_flush_fn(File *file);
84
static loff_t proc_lseek_fn(File * file, loff_t offset, int orig);
85
static int proc_ioctl_fn(struct inode *inode, File *file, unsigned opcode, unsigned long arg);
86
//static int proc_release_fn(struct inode *inode, File *file);
88
static int ProcEntry_has_name(ProcEntry *entry, const char *name, int namelen){
89
dprintf("> name=%.*s entry=%.*s\n", namelen, name, entry->namelen, entry->name);
90
if(!entry || !entry->low_ino) return FALSE;
91
if(entry->namelen != namelen) return FALSE;
92
return memcmp(name, entry->name, namelen) == 0;
95
// Set f->f_error on error?
96
// Does interface stop r/w on first error?
97
// Is release called after an error?
100
static int proc_get_parser(File *file, Parser **val){
102
Parser *parser = NULL;
103
parser = file->private_data;
105
parser = Parser_new();
110
file->private_data = parser;
117
static int proc_open_fn(Inode *inode, File *file){
119
// Return errcode or 0 on success.
120
// Can stuff data in file->private_data (void*).
122
//ProcEntry *entry = (ProcEntry *)inode->u.generic_ip;
123
//file->private_data = NULL;
124
//file->f_dentry->d_ino is inode.
125
// Check for user privilege - deny otherwise.
129
file->private_data = NULL;
133
static ssize_t proc_read_fn(File *file, char *buffer,
134
size_t count, loff_t *offset){
136
// Copy data to user buffer, increment offset by count, return count.
139
//if(copy_to_user(buffer, data, count)){
147
static ssize_t proc_write_fn(File *file, const char *buffer,
148
size_t count, loff_t *offset) {
155
static int proc_flush_fn(File *file){
157
int writing = (file->f_flags & O_ACCMODE) == O_WRONLY;
158
int f_count = atomic_read(&file->f_count);
159
if (writing && f_count == 1) {
160
ProcEntry *pentry = (ProcEntry *)file->f_dentry->d_inode->u.generic_ip;
169
/** Offset from start. */
171
/** Offset from current position. */
173
/** Offset from size of file. */
176
#endif /* !SEEK_SET */
178
static loff_t proc_lseek_fn(File * file, loff_t offset, int from){
185
offset += file->f_pos;
192
if(offset < 0) return -EINVAL;
193
file->f_pos = offset;
197
static int proc_ioctl_fn(Inode *inode, File *file,
198
unsigned opcode, unsigned long arg){
204
static ssize_t proc_policy_write_fn(File *file, const char *buffer,
205
size_t count, loff_t *offset) {
207
// Copy data into kernel space from buffer.
208
// Increment offset by count, return count (or code).
211
Parser *parser = NULL;
213
err = proc_get_parser(file, &parser);
215
data = allocate(count);
220
err = copy_from_user(data, buffer, count);
223
err = Parser_input(parser, data, count);
226
err = (err < 0 ? err : count);
230
static int proc_policy_release_fn(Inode *inode, File *file){
232
// Cleanup file->private_data, return errcode.
234
Parser *parser = NULL;
238
err = proc_get_parser(file, &parser);
240
err = Parser_input(parser, NULL, 0);
243
for(l = obj; CONSP(l); l = CDR(l)){
244
err = vnet_eval(CAR(l), iostdout, NULL);
249
file->private_data = NULL;
250
dprintf("< err=%d\n", err);
254
static int proc_io_open(Inode *inode, File *file, IOStream **val){
256
IOStream *io = mem_stream_new();
261
file->private_data = io;
263
*val = (err ? NULL: io);
267
static ssize_t proc_io_read_fn(File *file, char *buffer,
268
size_t count, loff_t *offset){
270
// Copy data to user buffer, increment offset by count, return count.
272
char kbuf[1024] = {};
273
int kbuf_n = sizeof(kbuf);
276
IOStream *io = file->private_data;
285
k = IOStream_read(io, kbuf, k);
287
if(copy_to_user(ubuf, kbuf, k)){
296
return (err ? err : n);
299
static int proc_io_release_fn(Inode *inode, File *file){
302
IOStream *io = file->private_data;
303
if(io) IOStream_close(io);
304
dprintf("< err=%d\n", err);
308
static int proc_vnets_open_fn(Inode *inode, File *file){
311
if(proc_io_open(inode, file, &io)) goto exit;
317
static int proc_vifs_open_fn(Inode *inode, File *file){
320
if(proc_io_open(inode, file, &io)) goto exit;
326
static int proc_peers_open_fn(Inode *inode, File *file){
329
if(proc_io_open(inode, file, &io)) goto exit;
335
static int proc_varp_open_fn(Inode *inode, File *file){
338
if(proc_io_open(inode, file, &io)) goto exit;
344
static struct file_operations proc_policy_ops = {
347
write: proc_policy_write_fn,
348
//flush: proc_flush_fn,
349
llseek: proc_lseek_fn,
350
ioctl: proc_ioctl_fn,
351
release: proc_policy_release_fn,
354
static struct file_operations proc_vnets_ops = {
355
open: proc_vnets_open_fn,
356
read: proc_io_read_fn,
357
release: proc_io_release_fn,
360
static struct file_operations proc_vifs_ops = {
361
open: proc_vifs_open_fn,
362
read: proc_io_read_fn,
363
release: proc_io_release_fn,
366
static struct file_operations proc_peers_ops = {
367
open: proc_peers_open_fn,
368
read: proc_io_read_fn,
369
release: proc_io_release_fn,
372
static struct file_operations proc_varp_ops = {
373
open: proc_varp_open_fn,
374
read: proc_io_read_fn,
375
release: proc_io_release_fn,
378
static ProcEntry *proc_fs_root = &proc_root;
380
static int proc_path_init(const char *path, const char **rest){
388
if(strncmp(PROC_ROOT, path, PROC_ROOT_LEN)){
391
path += PROC_ROOT_LEN;
399
/** Parse a path relative to `dir'. If dir is null or the proc root
400
* the path is relative to "/proc/", and the leading "/proc/" may be
404
static ProcEntry * ProcFS_lookup(const char *path, ProcEntry *dir){
405
const char *pathptr = path, *next = NULL;
406
ProcEntry *entry, *result = NULL;
409
if(dir && (dir != proc_fs_root)){
412
if(proc_path_init(path, &pathptr)) goto exit;
413
entry = proc_fs_root;
415
if(!pathptr || !*pathptr) goto exit;
417
next = strchr(pathptr, '/');
418
pathlen = (next ? next - pathptr : strlen(pathptr));
419
for(entry = entry->subdir; entry ; entry = entry->next) {
420
if(ProcEntry_has_name(entry, pathptr, pathlen)) break;
433
static ProcEntry *ProcFS_register(const char *name, ProcEntry *dir,
434
int val, struct file_operations *ops){
438
entry = create_proc_entry(name, mode, dir);
440
entry->proc_fops = ops;
441
entry->data = (void*)val; // Whatever data we need.
446
static ProcEntry *ProcFS_mkdir(const char *name, ProcEntry *parent){
447
ProcEntry *entry = NULL;
448
entry = ProcFS_lookup(name, parent);
451
if(proc_path_init(name, &path)) goto exit;
452
entry = proc_mkdir(path, parent);
458
static void ProcFS_remove(const char *name, ProcEntry *parent){
459
remove_proc_entry(name, parent);
462
static void ProcFS_rmrec_entry(ProcEntry *entry){
464
// Don't want to remove /proc itself!
465
if(entry->parent == entry) return;
466
while(entry->subdir){
467
ProcFS_rmrec_entry(entry->subdir);
469
dprintf("> remove %s\n", entry->name);
470
ProcFS_remove(entry->name, entry->parent);
474
static void ProcFS_rmrec(const char *name, ProcEntry *parent){
477
dprintf("> name=%s\n", name);
478
entry = ProcFS_lookup(name, parent);
480
ProcFS_rmrec_entry(entry);
485
void __init ProcFS_init(void){
486
ProcEntry *root_entry;
487
ProcEntry *policy_entry;
488
ProcEntry *vnets_entry;
489
ProcEntry *vifs_entry;
490
ProcEntry *peers_entry;
491
ProcEntry *varp_entry;
494
root_entry = ProcFS_mkdir(MODULE_ROOT, NULL);
495
if(!root_entry) goto exit;
496
policy_entry = ProcFS_register("policy", root_entry, VNET_POLICY, &proc_policy_ops);
497
vnets_entry = ProcFS_register("vnets", root_entry, VNET_VNETS, &proc_vnets_ops);
498
vifs_entry = ProcFS_register("vifs", root_entry, VNET_VIFS, &proc_vifs_ops);
499
peers_entry = ProcFS_register("peers", root_entry, VNET_PEERS, &proc_peers_ops);
500
varp_entry = ProcFS_register("varp", root_entry, VNET_VARP, &proc_varp_ops);
505
void __exit ProcFS_exit(void){
507
ProcFS_rmrec(MODULE_ROOT, NULL);