^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /* net/atm/resources.c - Statically allocated resources */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) /* Fixes
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Arnaldo Carvalho de Melo <acme@conectiva.com.br>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) * 2002/01 - don't free the whole struct sock on sk->destruct time,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) * use the default destruct function initialized by sock_init_data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/ctype.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/atmdev.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/sonet.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/kernel.h> /* for barrier */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/bitops.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/capability.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) #include <net/sock.h> /* for struct sock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include "common.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include "resources.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #include "addr.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) LIST_HEAD(atm_devs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) DEFINE_MUTEX(atm_dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static struct atm_dev *__alloc_atm_dev(const char *type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) struct atm_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) dev = kzalloc(sizeof(*dev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) dev->type = type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) dev->signal = ATM_PHY_SIG_UNKNOWN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) dev->link_rate = ATM_OC3_PCR;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) spin_lock_init(&dev->lock);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) INIT_LIST_HEAD(&dev->local);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) INIT_LIST_HEAD(&dev->lecs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) static struct atm_dev *__atm_dev_lookup(int number)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) struct atm_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) struct list_head *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) list_for_each(p, &atm_devs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) dev = list_entry(p, struct atm_dev, dev_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) if (dev->number == number) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) atm_dev_hold(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) struct atm_dev *atm_dev_lookup(int number)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) struct atm_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) mutex_lock(&atm_dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) dev = __atm_dev_lookup(number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) mutex_unlock(&atm_dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) EXPORT_SYMBOL(atm_dev_lookup);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) struct atm_dev *atm_dev_register(const char *type, struct device *parent,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) const struct atmdev_ops *ops, int number,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) unsigned long *flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) struct atm_dev *dev, *inuse;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) dev = __alloc_atm_dev(type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) if (!dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) pr_err("no space for dev %s\n", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) mutex_lock(&atm_dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (number != -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) inuse = __atm_dev_lookup(number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (inuse) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) atm_dev_put(inuse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) mutex_unlock(&atm_dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) dev->number = number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) dev->number = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) while ((inuse = __atm_dev_lookup(dev->number))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) atm_dev_put(inuse);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) dev->number++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) dev->ops = ops;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) if (flags)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) dev->flags = *flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) memset(&dev->flags, 0, sizeof(dev->flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) memset(&dev->stats, 0, sizeof(dev->stats));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) refcount_set(&dev->refcnt, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (atm_proc_dev_register(dev) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) pr_err("atm_proc_dev_register failed for dev %s\n", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) if (atm_register_sysfs(dev, parent) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) pr_err("atm_register_sysfs failed for dev %s\n", type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) atm_proc_dev_deregister(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) goto out_fail;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) list_add_tail(&dev->dev_list, &atm_devs);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) mutex_unlock(&atm_dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) return dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) out_fail:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) kfree(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) EXPORT_SYMBOL(atm_dev_register);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) void atm_dev_deregister(struct atm_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) BUG_ON(test_bit(ATM_DF_REMOVED, &dev->flags));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) set_bit(ATM_DF_REMOVED, &dev->flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) * if we remove current device from atm_devs list, new device
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) * with same number can appear, such we need deregister proc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) * release async all vccs and remove them from vccs list too
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) mutex_lock(&atm_dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) list_del(&dev->dev_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) mutex_unlock(&atm_dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) atm_dev_release_vccs(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) atm_unregister_sysfs(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) atm_proc_dev_deregister(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) atm_dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) EXPORT_SYMBOL(atm_dev_deregister);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) static void copy_aal_stats(struct k_atm_aal_stats *from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) struct atm_aal_stats *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) #define __HANDLE_ITEM(i) to->i = atomic_read(&from->i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) __AAL_STAT_ITEMS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) #undef __HANDLE_ITEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) static void subtract_aal_stats(struct k_atm_aal_stats *from,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) struct atm_aal_stats *to)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) #define __HANDLE_ITEM(i) atomic_sub(to->i, &from->i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) __AAL_STAT_ITEMS
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) #undef __HANDLE_ITEM
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) static int fetch_stats(struct atm_dev *dev, struct atm_dev_stats __user *arg,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) int zero)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) struct atm_dev_stats tmp;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) int error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) copy_aal_stats(&dev->stats.aal0, &tmp.aal0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) copy_aal_stats(&dev->stats.aal34, &tmp.aal34);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) copy_aal_stats(&dev->stats.aal5, &tmp.aal5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) if (arg)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) error = copy_to_user(arg, &tmp, sizeof(tmp));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (zero && !error) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) subtract_aal_stats(&dev->stats.aal0, &tmp.aal0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) subtract_aal_stats(&dev->stats.aal34, &tmp.aal34);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) subtract_aal_stats(&dev->stats.aal5, &tmp.aal5);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) return error ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) int atm_getnames(void __user *buf, int __user *iobuf_len)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) int error, len, size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) struct atm_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) struct list_head *p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) int *tmp_buf, *tmp_p;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (get_user(len, iobuf_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) mutex_lock(&atm_dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) list_for_each(p, &atm_devs)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) size += sizeof(int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (size > len) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) mutex_unlock(&atm_dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) return -E2BIG;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) tmp_buf = kmalloc(size, GFP_ATOMIC);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) if (!tmp_buf) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) mutex_unlock(&atm_dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) tmp_p = tmp_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) list_for_each(p, &atm_devs) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) dev = list_entry(p, struct atm_dev, dev_list);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) *tmp_p++ = dev->number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) mutex_unlock(&atm_dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) error = ((copy_to_user(buf, tmp_buf, size)) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) put_user(size, iobuf_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) kfree(tmp_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) int atm_dev_ioctl(unsigned int cmd, void __user *buf, int __user *sioc_len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) int number, int compat)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) int error, len, size = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) struct atm_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if (get_user(len, sioc_len))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) return -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) dev = try_then_request_module(atm_dev_lookup(number), "atm-device-%d",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (!dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) switch (cmd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) case ATM_GETTYPE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) size = strlen(dev->type) + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) if (copy_to_user(buf, dev->type, size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) error = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) case ATM_GETESI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) size = ESI_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) if (copy_to_user(buf, dev->esi, size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) error = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) case ATM_SETESI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) for (i = 0; i < ESI_LEN; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (dev->esi[i]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) error = -EEXIST;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) case ATM_SETESIF:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) unsigned char esi[ESI_LEN];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) if (!capable(CAP_NET_ADMIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) error = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) if (copy_from_user(esi, buf, ESI_LEN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) error = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) memcpy(dev->esi, esi, ESI_LEN);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) error = ESI_LEN;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) case ATM_GETSTATZ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (!capable(CAP_NET_ADMIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) error = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) case ATM_GETSTAT:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293) size = sizeof(struct atm_dev_stats);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) error = fetch_stats(dev, buf, cmd == ATM_GETSTATZ);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) if (error)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) case ATM_GETCIRANGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) size = sizeof(struct atm_cirange);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) if (copy_to_user(buf, &dev->ci_range, size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) error = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) case ATM_GETLINKRATE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) size = sizeof(int);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (copy_to_user(buf, &dev->link_rate, size)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) error = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) case ATM_RSTADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (!capable(CAP_NET_ADMIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) error = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) atm_reset_addr(dev, ATM_ADDR_LOCAL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) case ATM_ADDADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320) case ATM_DELADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) case ATM_ADDLECSADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) case ATM_DELLECSADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) struct sockaddr_atmsvc addr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) if (!capable(CAP_NET_ADMIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) error = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (copy_from_user(&addr, buf, sizeof(addr))) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) error = -EFAULT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) if (cmd == ATM_ADDADDR || cmd == ATM_ADDLECSADDR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336) error = atm_add_addr(dev, &addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) (cmd == ATM_ADDADDR ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338) ATM_ADDR_LOCAL : ATM_ADDR_LECS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) error = atm_del_addr(dev, &addr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) (cmd == ATM_DELADDR ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) ATM_ADDR_LOCAL : ATM_ADDR_LECS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) case ATM_GETADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) case ATM_GETLECSADDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) error = atm_get_addr(dev, buf, len,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) (cmd == ATM_GETADDR ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349) ATM_ADDR_LOCAL : ATM_ADDR_LECS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (error < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352) size = error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) /* may return 0, but later on size == 0 means "don't
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) write the length" */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355) error = put_user(size, sioc_len) ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357) case ATM_SETLOOP:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) if (__ATM_LM_XTRMT((int) (unsigned long) buf) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) __ATM_LM_XTLOC((int) (unsigned long) buf) >
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) __ATM_LM_XTRMT((int) (unsigned long) buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) case ATM_SETCIRANGE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) case SONET_GETSTATZ:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) case SONET_SETDIAG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368) case SONET_CLRDIAG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) case SONET_SETFRAMING:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) if (!capable(CAP_NET_ADMIN)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) error = -EPERM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) if (IS_ENABLED(CONFIG_COMPAT) && compat) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) #ifdef CONFIG_COMPAT
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378) if (!dev->ops->compat_ioctl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382) size = dev->ops->compat_ioctl(dev, cmd, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (!dev->ops->ioctl) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) error = -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389) size = dev->ops->ioctl(dev, cmd, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) if (size < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392) error = (size == -ENOIOCTLCMD ? -ENOTTY : size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) goto done;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) if (size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398) error = put_user(size, sioc_len) ? -EFAULT : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) error = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) done:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) atm_dev_put(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) return error;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) void *atm_dev_seq_start(struct seq_file *seq, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) mutex_lock(&atm_dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) return seq_list_start_head(&atm_devs, *pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) void atm_dev_seq_stop(struct seq_file *seq, void *v)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) mutex_unlock(&atm_dev_mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 415) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 416)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 417) void *atm_dev_seq_next(struct seq_file *seq, void *v, loff_t *pos)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 418) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 419) return seq_list_next(v, &atm_devs, pos);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 420) }