| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* $Id: divert_procfs.c,v 1.11.6.2 2001/09/23 22:24:36 kai Exp $ | 
 | 2 |  * | 
 | 3 |  * Filesystem handling for the diversion supplementary services. | 
 | 4 |  * | 
 | 5 |  * Copyright 1998       by Werner Cornelius (werner@isdn4linux.de) | 
 | 6 |  * | 
 | 7 |  * This software may be used and distributed according to the terms | 
 | 8 |  * of the GNU General Public License, incorporated herein by reference. | 
 | 9 |  * | 
 | 10 |  */ | 
 | 11 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | #include <linux/module.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include <linux/poll.h> | 
| Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 14 | #include <linux/slab.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 15 | #ifdef CONFIG_PROC_FS | 
 | 16 | #include <linux/proc_fs.h> | 
 | 17 | #else | 
 | 18 | #include <linux/fs.h> | 
 | 19 | #endif | 
| Alexey Dobriyan | a99bbaf | 2009-10-04 16:11:37 +0400 | [diff] [blame] | 20 | #include <linux/sched.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 21 | #include <linux/isdnif.h> | 
| Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 22 | #include <net/net_namespace.h> | 
| Arnd Bergmann | 76a6492 | 2010-07-11 11:18:53 +0000 | [diff] [blame] | 23 | #include <linux/mutex.h> | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | #include "isdn_divert.h" | 
 | 25 |  | 
 | 26 |  | 
 | 27 | /*********************************/ | 
 | 28 | /* Variables for interface queue */ | 
 | 29 | /*********************************/ | 
 | 30 | ulong if_used = 0;		/* number of interface users */ | 
| Arnd Bergmann | 76a6492 | 2010-07-11 11:18:53 +0000 | [diff] [blame] | 31 | static DEFINE_MUTEX(isdn_divert_mutex); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | static struct divert_info *divert_info_head = NULL;	/* head of queue */ | 
 | 33 | static struct divert_info *divert_info_tail = NULL;	/* pointer to last entry */ | 
 | 34 | static DEFINE_SPINLOCK(divert_info_lock);/* lock for queue */ | 
 | 35 | static wait_queue_head_t rd_queue; | 
 | 36 |  | 
 | 37 | /*********************************/ | 
 | 38 | /* put an info buffer into queue */ | 
 | 39 | /*********************************/ | 
 | 40 | void | 
 | 41 | put_info_buffer(char *cp) | 
 | 42 | { | 
 | 43 | 	struct divert_info *ib; | 
 | 44 | 	unsigned long flags; | 
 | 45 |  | 
 | 46 | 	if (if_used <= 0) | 
 | 47 | 		return; | 
 | 48 | 	if (!cp) | 
 | 49 | 		return; | 
 | 50 | 	if (!*cp) | 
 | 51 | 		return; | 
| Robert P. J. Day | 5cbded5 | 2006-12-13 00:35:56 -0800 | [diff] [blame] | 52 | 	if (!(ib = kmalloc(sizeof(struct divert_info) + strlen(cp), GFP_ATOMIC))) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | 		 return;	/* no memory */ | 
 | 54 | 	strcpy(ib->info_start, cp);	/* set output string */ | 
 | 55 | 	ib->next = NULL; | 
 | 56 | 	spin_lock_irqsave( &divert_info_lock, flags ); | 
 | 57 | 	ib->usage_cnt = if_used; | 
 | 58 | 	if (!divert_info_head) | 
 | 59 | 		divert_info_head = ib;	/* new head */ | 
 | 60 | 	else | 
 | 61 | 		divert_info_tail->next = ib;	/* follows existing messages */ | 
 | 62 | 	divert_info_tail = ib;	/* new tail */ | 
 | 63 |  | 
 | 64 | 	/* delete old entrys */ | 
 | 65 | 	while (divert_info_head->next) { | 
 | 66 | 		if ((divert_info_head->usage_cnt <= 0) && | 
 | 67 | 		    (divert_info_head->next->usage_cnt <= 0)) { | 
 | 68 | 			ib = divert_info_head; | 
 | 69 | 			divert_info_head = divert_info_head->next; | 
 | 70 | 			kfree(ib); | 
 | 71 | 		} else | 
 | 72 | 			break; | 
 | 73 | 	}			/* divert_info_head->next */ | 
 | 74 | 	spin_unlock_irqrestore( &divert_info_lock, flags ); | 
 | 75 | 	wake_up_interruptible(&(rd_queue)); | 
 | 76 | }				/* put_info_buffer */ | 
 | 77 |  | 
| Charlie Shepherd | 732781d | 2007-07-31 00:39:45 -0700 | [diff] [blame] | 78 | #ifdef CONFIG_PROC_FS | 
 | 79 |  | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | /**********************************/ | 
 | 81 | /* deflection device read routine */ | 
 | 82 | /**********************************/ | 
 | 83 | static ssize_t | 
 | 84 | isdn_divert_read(struct file *file, char __user *buf, size_t count, loff_t * off) | 
 | 85 | { | 
 | 86 | 	struct divert_info *inf; | 
 | 87 | 	int len; | 
 | 88 |  | 
 | 89 | 	if (!*((struct divert_info **) file->private_data)) { | 
 | 90 | 		if (file->f_flags & O_NONBLOCK) | 
 | 91 | 			return -EAGAIN; | 
 | 92 | 		interruptible_sleep_on(&(rd_queue)); | 
 | 93 | 	} | 
 | 94 | 	if (!(inf = *((struct divert_info **) file->private_data))) | 
 | 95 | 		return (0); | 
 | 96 |  | 
 | 97 | 	inf->usage_cnt--;	/* new usage count */ | 
 | 98 | 	file->private_data = &inf->next;	/* next structure */ | 
 | 99 | 	if ((len = strlen(inf->info_start)) <= count) { | 
 | 100 | 		if (copy_to_user(buf, inf->info_start, len)) | 
 | 101 | 			return -EFAULT; | 
 | 102 | 		*off += len; | 
 | 103 | 		return (len); | 
 | 104 | 	} | 
 | 105 | 	return (0); | 
 | 106 | }				/* isdn_divert_read */ | 
 | 107 |  | 
 | 108 | /**********************************/ | 
 | 109 | /* deflection device write routine */ | 
 | 110 | /**********************************/ | 
 | 111 | static ssize_t | 
 | 112 | isdn_divert_write(struct file *file, const char __user *buf, size_t count, loff_t * off) | 
 | 113 | { | 
 | 114 | 	return (-ENODEV); | 
 | 115 | }				/* isdn_divert_write */ | 
 | 116 |  | 
 | 117 |  | 
 | 118 | /***************************************/ | 
 | 119 | /* select routines for various kernels */ | 
 | 120 | /***************************************/ | 
 | 121 | static unsigned int | 
 | 122 | isdn_divert_poll(struct file *file, poll_table * wait) | 
 | 123 | { | 
 | 124 | 	unsigned int mask = 0; | 
 | 125 |  | 
 | 126 | 	poll_wait(file, &(rd_queue), wait); | 
 | 127 | 	/* mask = POLLOUT | POLLWRNORM; */ | 
 | 128 | 	if (*((struct divert_info **) file->private_data)) { | 
 | 129 | 		mask |= POLLIN | POLLRDNORM; | 
 | 130 | 	} | 
 | 131 | 	return mask; | 
 | 132 | }				/* isdn_divert_poll */ | 
 | 133 |  | 
 | 134 | /****************/ | 
 | 135 | /* Open routine */ | 
 | 136 | /****************/ | 
 | 137 | static int | 
 | 138 | isdn_divert_open(struct inode *ino, struct file *filep) | 
 | 139 | { | 
 | 140 | 	unsigned long flags; | 
 | 141 |  | 
 | 142 | 	spin_lock_irqsave( &divert_info_lock, flags ); | 
 | 143 |  	if_used++; | 
 | 144 | 	if (divert_info_head) | 
 | 145 | 		filep->private_data = &(divert_info_tail->next); | 
 | 146 | 	else | 
 | 147 | 		filep->private_data = &divert_info_head; | 
 | 148 | 	spin_unlock_irqrestore( &divert_info_lock, flags ); | 
 | 149 | 	/*  start_divert(); */ | 
 | 150 | 	return nonseekable_open(ino, filep); | 
 | 151 | }				/* isdn_divert_open */ | 
 | 152 |  | 
 | 153 | /*******************/ | 
 | 154 | /* close routine   */ | 
 | 155 | /*******************/ | 
 | 156 | static int | 
 | 157 | isdn_divert_close(struct inode *ino, struct file *filep) | 
 | 158 | { | 
 | 159 | 	struct divert_info *inf; | 
 | 160 | 	unsigned long flags; | 
 | 161 |  | 
 | 162 | 	spin_lock_irqsave( &divert_info_lock, flags ); | 
 | 163 | 	if_used--; | 
 | 164 | 	inf = *((struct divert_info **) filep->private_data); | 
 | 165 | 	while (inf) { | 
 | 166 | 		inf->usage_cnt--; | 
 | 167 | 		inf = inf->next; | 
 | 168 | 	} | 
 | 169 | 	if (if_used <= 0) | 
 | 170 | 		while (divert_info_head) { | 
 | 171 | 			inf = divert_info_head; | 
 | 172 | 			divert_info_head = divert_info_head->next; | 
 | 173 | 			kfree(inf); | 
 | 174 | 		} | 
 | 175 | 	spin_unlock_irqrestore( &divert_info_lock, flags ); | 
 | 176 | 	return (0); | 
 | 177 | }				/* isdn_divert_close */ | 
 | 178 |  | 
 | 179 | /*********/ | 
 | 180 | /* IOCTL */ | 
 | 181 | /*********/ | 
| Frederic Weisbecker | d79b6f4 | 2010-03-30 07:27:50 +0200 | [diff] [blame] | 182 | static int isdn_divert_ioctl_unlocked(struct file *file, uint cmd, ulong arg) | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | { | 
 | 184 | 	divert_ioctl dioctl; | 
 | 185 | 	int i; | 
 | 186 | 	unsigned long flags; | 
 | 187 | 	divert_rule *rulep; | 
 | 188 | 	char *cp; | 
 | 189 |  | 
 | 190 | 	if (copy_from_user(&dioctl, (void __user *) arg, sizeof(dioctl))) | 
 | 191 | 		return -EFAULT; | 
 | 192 |  | 
 | 193 | 	switch (cmd) { | 
 | 194 | 		case IIOCGETVER: | 
 | 195 | 			dioctl.drv_version = DIVERT_IIOC_VERSION;	/* set version */ | 
 | 196 | 			break; | 
 | 197 |  | 
 | 198 | 		case IIOCGETDRV: | 
 | 199 | 			if ((dioctl.getid.drvid = divert_if.name_to_drv(dioctl.getid.drvnam)) < 0) | 
 | 200 | 				return (-EINVAL); | 
 | 201 | 			break; | 
 | 202 |  | 
 | 203 | 		case IIOCGETNAM: | 
 | 204 | 			cp = divert_if.drv_to_name(dioctl.getid.drvid); | 
 | 205 | 			if (!cp) | 
 | 206 | 				return (-EINVAL); | 
 | 207 | 			if (!*cp) | 
 | 208 | 				return (-EINVAL); | 
 | 209 | 			strcpy(dioctl.getid.drvnam, cp); | 
 | 210 | 			break; | 
 | 211 |  | 
 | 212 | 		case IIOCGETRULE: | 
 | 213 | 			if (!(rulep = getruleptr(dioctl.getsetrule.ruleidx))) | 
 | 214 | 				return (-EINVAL); | 
 | 215 | 			dioctl.getsetrule.rule = *rulep;	/* copy data */ | 
 | 216 | 			break; | 
 | 217 |  | 
 | 218 | 		case IIOCMODRULE: | 
 | 219 | 			if (!(rulep = getruleptr(dioctl.getsetrule.ruleidx))) | 
 | 220 | 				return (-EINVAL); | 
 | 221 |             spin_lock_irqsave(&divert_lock, flags); | 
 | 222 | 			*rulep = dioctl.getsetrule.rule;	/* copy data */ | 
 | 223 | 			spin_unlock_irqrestore(&divert_lock, flags); | 
 | 224 | 			return (0);	/* no copy required */ | 
 | 225 | 			break; | 
 | 226 |  | 
 | 227 | 		case IIOCINSRULE: | 
 | 228 | 			return (insertrule(dioctl.getsetrule.ruleidx, &dioctl.getsetrule.rule)); | 
 | 229 | 			break; | 
 | 230 |  | 
 | 231 | 		case IIOCDELRULE: | 
 | 232 | 			return (deleterule(dioctl.getsetrule.ruleidx)); | 
 | 233 | 			break; | 
 | 234 |  | 
 | 235 | 		case IIOCDODFACT: | 
 | 236 | 			return (deflect_extern_action(dioctl.fwd_ctrl.subcmd, | 
 | 237 | 						  dioctl.fwd_ctrl.callid, | 
 | 238 | 						 dioctl.fwd_ctrl.to_nr)); | 
 | 239 |  | 
 | 240 | 		case IIOCDOCFACT: | 
 | 241 | 		case IIOCDOCFDIS: | 
 | 242 | 		case IIOCDOCFINT: | 
 | 243 | 			if (!divert_if.drv_to_name(dioctl.cf_ctrl.drvid)) | 
 | 244 | 				return (-EINVAL);	/* invalid driver */ | 
 | 245 | 			if ((i = cf_command(dioctl.cf_ctrl.drvid, | 
 | 246 | 					    (cmd == IIOCDOCFACT) ? 1 : (cmd == IIOCDOCFDIS) ? 0 : 2, | 
 | 247 | 					    dioctl.cf_ctrl.cfproc, | 
 | 248 | 					    dioctl.cf_ctrl.msn, | 
 | 249 | 					    dioctl.cf_ctrl.service, | 
 | 250 | 					    dioctl.cf_ctrl.fwd_nr, | 
 | 251 | 					    &dioctl.cf_ctrl.procid))) | 
 | 252 | 				return (i); | 
 | 253 | 			break; | 
 | 254 |  | 
 | 255 | 		default: | 
 | 256 | 			return (-EINVAL); | 
 | 257 | 	}			/* switch cmd */ | 
 | 258 | 	return copy_to_user((void __user *)arg, &dioctl, sizeof(dioctl)) ? -EFAULT : 0; | 
 | 259 | }				/* isdn_divert_ioctl */ | 
 | 260 |  | 
| Frederic Weisbecker | d79b6f4 | 2010-03-30 07:27:50 +0200 | [diff] [blame] | 261 | static long isdn_divert_ioctl(struct file *file, uint cmd, ulong arg) | 
 | 262 | { | 
 | 263 | 	long ret; | 
 | 264 |  | 
| Arnd Bergmann | 76a6492 | 2010-07-11 11:18:53 +0000 | [diff] [blame] | 265 | 	mutex_lock(&isdn_divert_mutex); | 
| Frederic Weisbecker | d79b6f4 | 2010-03-30 07:27:50 +0200 | [diff] [blame] | 266 | 	ret = isdn_divert_ioctl_unlocked(file, cmd, arg); | 
| Arnd Bergmann | 76a6492 | 2010-07-11 11:18:53 +0000 | [diff] [blame] | 267 | 	mutex_unlock(&isdn_divert_mutex); | 
| Frederic Weisbecker | d79b6f4 | 2010-03-30 07:27:50 +0200 | [diff] [blame] | 268 |  | 
 | 269 | 	return ret; | 
 | 270 | } | 
 | 271 |  | 
| Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 272 | static const struct file_operations isdn_fops = | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | { | 
 | 274 | 	.owner          = THIS_MODULE, | 
 | 275 | 	.llseek         = no_llseek, | 
 | 276 | 	.read           = isdn_divert_read, | 
 | 277 | 	.write          = isdn_divert_write, | 
 | 278 | 	.poll           = isdn_divert_poll, | 
| Frederic Weisbecker | d79b6f4 | 2010-03-30 07:27:50 +0200 | [diff] [blame] | 279 | 	.unlocked_ioctl = isdn_divert_ioctl, | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | 	.open           = isdn_divert_open, | 
 | 281 | 	.release        = isdn_divert_close,                                       | 
 | 282 | }; | 
 | 283 |  | 
 | 284 | /****************************/ | 
 | 285 | /* isdn subdir in /proc/net */ | 
 | 286 | /****************************/ | 
 | 287 | static struct proc_dir_entry *isdn_proc_entry = NULL; | 
 | 288 | static struct proc_dir_entry *isdn_divert_entry = NULL; | 
 | 289 | #endif	/* CONFIG_PROC_FS */ | 
 | 290 |  | 
 | 291 | /***************************************************************************/ | 
 | 292 | /* divert_dev_init must be called before the proc filesystem may be used   */ | 
 | 293 | /***************************************************************************/ | 
 | 294 | int | 
 | 295 | divert_dev_init(void) | 
 | 296 | { | 
 | 297 |  | 
 | 298 | 	init_waitqueue_head(&rd_queue); | 
 | 299 |  | 
 | 300 | #ifdef CONFIG_PROC_FS | 
| Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 301 | 	isdn_proc_entry = proc_mkdir("isdn", init_net.proc_net); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | 	if (!isdn_proc_entry) | 
 | 303 | 		return (-1); | 
| Denis V. Lunev | ac41cfd | 2008-04-29 01:02:30 -0700 | [diff] [blame] | 304 | 	isdn_divert_entry = proc_create("divert", S_IFREG | S_IRUGO, | 
 | 305 | 					isdn_proc_entry, &isdn_fops); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 306 | 	if (!isdn_divert_entry) { | 
| Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 307 | 		remove_proc_entry("isdn", init_net.proc_net); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 308 | 		return (-1); | 
 | 309 | 	} | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 310 | #endif	/* CONFIG_PROC_FS */ | 
 | 311 |  | 
 | 312 | 	return (0); | 
 | 313 | }				/* divert_dev_init */ | 
 | 314 |  | 
 | 315 | /***************************************************************************/ | 
 | 316 | /* divert_dev_deinit must be called before leaving isdn when included as   */ | 
 | 317 | /* a module.                                                               */ | 
 | 318 | /***************************************************************************/ | 
 | 319 | int | 
 | 320 | divert_dev_deinit(void) | 
 | 321 | { | 
 | 322 |  | 
 | 323 | #ifdef CONFIG_PROC_FS | 
 | 324 | 	remove_proc_entry("divert", isdn_proc_entry); | 
| Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 325 | 	remove_proc_entry("isdn", init_net.proc_net); | 
| Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 326 | #endif	/* CONFIG_PROC_FS */ | 
 | 327 |  | 
 | 328 | 	return (0); | 
 | 329 | }				/* divert_dev_deinit */ |