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))) |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 53 | return; /* no memory */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 54 | strcpy(ib->info_start, cp); /* set output string */ |
| 55 | ib->next = NULL; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 56 | spin_lock_irqsave(&divert_info_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 */ |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 74 | spin_unlock_irqrestore(&divert_info_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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 |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 84 | isdn_divert_read(struct file *file, char __user *buf, size_t count, loff_t *off) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 85 | { |
| 86 | struct divert_info *inf; |
| 87 | int len; |
| 88 | |
Arnd Bergmann | c11da83 | 2014-02-26 12:01:54 +0100 | [diff] [blame] | 89 | if (!(inf = *((struct divert_info **) file->private_data))) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 90 | if (file->f_flags & O_NONBLOCK) |
| 91 | return -EAGAIN; |
Arnd Bergmann | c11da83 | 2014-02-26 12:01:54 +0100 | [diff] [blame] | 92 | wait_event_interruptible(rd_queue, (inf = |
| 93 | *((struct divert_info **) file->private_data))); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | } |
Arnd Bergmann | c11da83 | 2014-02-26 12:01:54 +0100 | [diff] [blame] | 95 | if (!inf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | return (0); |
| 97 | |
| 98 | inf->usage_cnt--; /* new usage count */ |
| 99 | file->private_data = &inf->next; /* next structure */ |
| 100 | if ((len = strlen(inf->info_start)) <= count) { |
| 101 | if (copy_to_user(buf, inf->info_start, len)) |
| 102 | return -EFAULT; |
| 103 | *off += len; |
| 104 | return (len); |
| 105 | } |
| 106 | return (0); |
| 107 | } /* isdn_divert_read */ |
| 108 | |
| 109 | /**********************************/ |
| 110 | /* deflection device write routine */ |
| 111 | /**********************************/ |
| 112 | static ssize_t |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 113 | isdn_divert_write(struct file *file, const char __user *buf, size_t count, loff_t *off) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 114 | { |
| 115 | return (-ENODEV); |
| 116 | } /* isdn_divert_write */ |
| 117 | |
| 118 | |
| 119 | /***************************************/ |
| 120 | /* select routines for various kernels */ |
| 121 | /***************************************/ |
| 122 | static unsigned int |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 123 | isdn_divert_poll(struct file *file, poll_table *wait) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 124 | { |
| 125 | unsigned int mask = 0; |
| 126 | |
| 127 | poll_wait(file, &(rd_queue), wait); |
| 128 | /* mask = POLLOUT | POLLWRNORM; */ |
| 129 | if (*((struct divert_info **) file->private_data)) { |
| 130 | mask |= POLLIN | POLLRDNORM; |
| 131 | } |
| 132 | return mask; |
| 133 | } /* isdn_divert_poll */ |
| 134 | |
| 135 | /****************/ |
| 136 | /* Open routine */ |
| 137 | /****************/ |
| 138 | static int |
| 139 | isdn_divert_open(struct inode *ino, struct file *filep) |
| 140 | { |
| 141 | unsigned long flags; |
| 142 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 143 | spin_lock_irqsave(&divert_info_lock, flags); |
| 144 | if_used++; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 145 | if (divert_info_head) |
| 146 | filep->private_data = &(divert_info_tail->next); |
| 147 | else |
| 148 | filep->private_data = &divert_info_head; |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 149 | spin_unlock_irqrestore(&divert_info_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | /* start_divert(); */ |
| 151 | return nonseekable_open(ino, filep); |
| 152 | } /* isdn_divert_open */ |
| 153 | |
| 154 | /*******************/ |
| 155 | /* close routine */ |
| 156 | /*******************/ |
| 157 | static int |
| 158 | isdn_divert_close(struct inode *ino, struct file *filep) |
| 159 | { |
| 160 | struct divert_info *inf; |
| 161 | unsigned long flags; |
| 162 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 163 | spin_lock_irqsave(&divert_info_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | if_used--; |
| 165 | inf = *((struct divert_info **) filep->private_data); |
| 166 | while (inf) { |
| 167 | inf->usage_cnt--; |
| 168 | inf = inf->next; |
| 169 | } |
| 170 | if (if_used <= 0) |
| 171 | while (divert_info_head) { |
| 172 | inf = divert_info_head; |
| 173 | divert_info_head = divert_info_head->next; |
| 174 | kfree(inf); |
| 175 | } |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 176 | spin_unlock_irqrestore(&divert_info_lock, flags); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 177 | return (0); |
| 178 | } /* isdn_divert_close */ |
| 179 | |
| 180 | /*********/ |
| 181 | /* IOCTL */ |
| 182 | /*********/ |
Frederic Weisbecker | d79b6f4 | 2010-03-30 07:27:50 +0200 | [diff] [blame] | 183 | 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] | 184 | { |
| 185 | divert_ioctl dioctl; |
| 186 | int i; |
| 187 | unsigned long flags; |
| 188 | divert_rule *rulep; |
| 189 | char *cp; |
| 190 | |
| 191 | if (copy_from_user(&dioctl, (void __user *) arg, sizeof(dioctl))) |
| 192 | return -EFAULT; |
| 193 | |
| 194 | switch (cmd) { |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 195 | case IIOCGETVER: |
| 196 | dioctl.drv_version = DIVERT_IIOC_VERSION; /* set version */ |
| 197 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 199 | case IIOCGETDRV: |
| 200 | if ((dioctl.getid.drvid = divert_if.name_to_drv(dioctl.getid.drvnam)) < 0) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | return (-EINVAL); |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 202 | break; |
| 203 | |
| 204 | case IIOCGETNAM: |
| 205 | cp = divert_if.drv_to_name(dioctl.getid.drvid); |
| 206 | if (!cp) |
| 207 | return (-EINVAL); |
| 208 | if (!*cp) |
| 209 | return (-EINVAL); |
| 210 | strcpy(dioctl.getid.drvnam, cp); |
| 211 | break; |
| 212 | |
| 213 | case IIOCGETRULE: |
| 214 | if (!(rulep = getruleptr(dioctl.getsetrule.ruleidx))) |
| 215 | return (-EINVAL); |
| 216 | dioctl.getsetrule.rule = *rulep; /* copy data */ |
| 217 | break; |
| 218 | |
| 219 | case IIOCMODRULE: |
| 220 | if (!(rulep = getruleptr(dioctl.getsetrule.ruleidx))) |
| 221 | return (-EINVAL); |
| 222 | spin_lock_irqsave(&divert_lock, flags); |
| 223 | *rulep = dioctl.getsetrule.rule; /* copy data */ |
| 224 | spin_unlock_irqrestore(&divert_lock, flags); |
| 225 | return (0); /* no copy required */ |
| 226 | break; |
| 227 | |
| 228 | case IIOCINSRULE: |
| 229 | return (insertrule(dioctl.getsetrule.ruleidx, &dioctl.getsetrule.rule)); |
| 230 | break; |
| 231 | |
| 232 | case IIOCDELRULE: |
| 233 | return (deleterule(dioctl.getsetrule.ruleidx)); |
| 234 | break; |
| 235 | |
| 236 | case IIOCDODFACT: |
| 237 | return (deflect_extern_action(dioctl.fwd_ctrl.subcmd, |
| 238 | dioctl.fwd_ctrl.callid, |
| 239 | dioctl.fwd_ctrl.to_nr)); |
| 240 | |
| 241 | case IIOCDOCFACT: |
| 242 | case IIOCDOCFDIS: |
| 243 | case IIOCDOCFINT: |
| 244 | if (!divert_if.drv_to_name(dioctl.cf_ctrl.drvid)) |
| 245 | return (-EINVAL); /* invalid driver */ |
| 246 | if (strnlen(dioctl.cf_ctrl.msn, sizeof(dioctl.cf_ctrl.msn)) == |
| 247 | sizeof(dioctl.cf_ctrl.msn)) |
| 248 | return -EINVAL; |
| 249 | if (strnlen(dioctl.cf_ctrl.fwd_nr, sizeof(dioctl.cf_ctrl.fwd_nr)) == |
| 250 | sizeof(dioctl.cf_ctrl.fwd_nr)) |
| 251 | return -EINVAL; |
| 252 | if ((i = cf_command(dioctl.cf_ctrl.drvid, |
| 253 | (cmd == IIOCDOCFACT) ? 1 : (cmd == IIOCDOCFDIS) ? 0 : 2, |
| 254 | dioctl.cf_ctrl.cfproc, |
| 255 | dioctl.cf_ctrl.msn, |
| 256 | dioctl.cf_ctrl.service, |
| 257 | dioctl.cf_ctrl.fwd_nr, |
| 258 | &dioctl.cf_ctrl.procid))) |
| 259 | return (i); |
| 260 | break; |
| 261 | |
| 262 | default: |
| 263 | return (-EINVAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 264 | } /* switch cmd */ |
| 265 | return copy_to_user((void __user *)arg, &dioctl, sizeof(dioctl)) ? -EFAULT : 0; |
| 266 | } /* isdn_divert_ioctl */ |
| 267 | |
Frederic Weisbecker | d79b6f4 | 2010-03-30 07:27:50 +0200 | [diff] [blame] | 268 | static long isdn_divert_ioctl(struct file *file, uint cmd, ulong arg) |
| 269 | { |
| 270 | long ret; |
| 271 | |
Arnd Bergmann | 76a6492 | 2010-07-11 11:18:53 +0000 | [diff] [blame] | 272 | mutex_lock(&isdn_divert_mutex); |
Frederic Weisbecker | d79b6f4 | 2010-03-30 07:27:50 +0200 | [diff] [blame] | 273 | ret = isdn_divert_ioctl_unlocked(file, cmd, arg); |
Arnd Bergmann | 76a6492 | 2010-07-11 11:18:53 +0000 | [diff] [blame] | 274 | mutex_unlock(&isdn_divert_mutex); |
Frederic Weisbecker | d79b6f4 | 2010-03-30 07:27:50 +0200 | [diff] [blame] | 275 | |
| 276 | return ret; |
| 277 | } |
| 278 | |
Arjan van de Ven | 2b8693c | 2007-02-12 00:55:32 -0800 | [diff] [blame] | 279 | static const struct file_operations isdn_fops = |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | { |
| 281 | .owner = THIS_MODULE, |
| 282 | .llseek = no_llseek, |
| 283 | .read = isdn_divert_read, |
| 284 | .write = isdn_divert_write, |
| 285 | .poll = isdn_divert_poll, |
Frederic Weisbecker | d79b6f4 | 2010-03-30 07:27:50 +0200 | [diff] [blame] | 286 | .unlocked_ioctl = isdn_divert_ioctl, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 287 | .open = isdn_divert_open, |
Joe Perches | 475be4d | 2012-02-19 19:52:38 -0800 | [diff] [blame] | 288 | .release = isdn_divert_close, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 289 | }; |
| 290 | |
| 291 | /****************************/ |
| 292 | /* isdn subdir in /proc/net */ |
| 293 | /****************************/ |
| 294 | static struct proc_dir_entry *isdn_proc_entry = NULL; |
| 295 | static struct proc_dir_entry *isdn_divert_entry = NULL; |
| 296 | #endif /* CONFIG_PROC_FS */ |
| 297 | |
| 298 | /***************************************************************************/ |
| 299 | /* divert_dev_init must be called before the proc filesystem may be used */ |
| 300 | /***************************************************************************/ |
| 301 | int |
| 302 | divert_dev_init(void) |
| 303 | { |
| 304 | |
| 305 | init_waitqueue_head(&rd_queue); |
| 306 | |
| 307 | #ifdef CONFIG_PROC_FS |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 308 | isdn_proc_entry = proc_mkdir("isdn", init_net.proc_net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | if (!isdn_proc_entry) |
| 310 | return (-1); |
Denis V. Lunev | ac41cfd | 2008-04-29 01:02:30 -0700 | [diff] [blame] | 311 | isdn_divert_entry = proc_create("divert", S_IFREG | S_IRUGO, |
| 312 | isdn_proc_entry, &isdn_fops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 313 | if (!isdn_divert_entry) { |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 314 | remove_proc_entry("isdn", init_net.proc_net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 315 | return (-1); |
| 316 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 317 | #endif /* CONFIG_PROC_FS */ |
| 318 | |
| 319 | return (0); |
| 320 | } /* divert_dev_init */ |
| 321 | |
| 322 | /***************************************************************************/ |
| 323 | /* divert_dev_deinit must be called before leaving isdn when included as */ |
| 324 | /* a module. */ |
| 325 | /***************************************************************************/ |
| 326 | int |
| 327 | divert_dev_deinit(void) |
| 328 | { |
| 329 | |
| 330 | #ifdef CONFIG_PROC_FS |
| 331 | remove_proc_entry("divert", isdn_proc_entry); |
Eric W. Biederman | 457c4cb | 2007-09-12 12:01:34 +0200 | [diff] [blame] | 332 | remove_proc_entry("isdn", init_net.proc_net); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 333 | #endif /* CONFIG_PROC_FS */ |
| 334 | |
| 335 | return (0); |
| 336 | } /* divert_dev_deinit */ |