11da177e4SLinus Torvalds /* net/atm/common.c - ATM sockets (common part for PVC and SVC) */ 21da177e4SLinus Torvalds 31da177e4SLinus Torvalds /* Written 1995-2000 by Werner Almesberger, EPFL LRC/ICA */ 41da177e4SLinus Torvalds 599824461SJoe Perches #define pr_fmt(fmt) KBUILD_MODNAME ":%s: " fmt, __func__ 61da177e4SLinus Torvalds 71da177e4SLinus Torvalds #include <linux/module.h> 81da177e4SLinus Torvalds #include <linux/kmod.h> 91da177e4SLinus Torvalds #include <linux/net.h> /* struct socket, struct proto_ops */ 101da177e4SLinus Torvalds #include <linux/atm.h> /* ATM stuff */ 111da177e4SLinus Torvalds #include <linux/atmdev.h> 121da177e4SLinus Torvalds #include <linux/socket.h> /* SOL_SOCKET */ 131da177e4SLinus Torvalds #include <linux/errno.h> /* error codes */ 141da177e4SLinus Torvalds #include <linux/capability.h> 15e49332bdSJesper Juhl #include <linux/mm.h> 161da177e4SLinus Torvalds #include <linux/sched.h> 171da177e4SLinus Torvalds #include <linux/time.h> /* struct timeval */ 181da177e4SLinus Torvalds #include <linux/skbuff.h> 191da177e4SLinus Torvalds #include <linux/bitops.h> 201da177e4SLinus Torvalds #include <linux/init.h> 215a0e3ad6STejun Heo #include <linux/slab.h> 221da177e4SLinus Torvalds #include <net/sock.h> /* struct sock */ 23a8147d73SJoe Perches #include <linux/uaccess.h> 24a8147d73SJoe Perches #include <linux/poll.h> 251da177e4SLinus Torvalds 2660063497SArun Sharma #include <linux/atomic.h> 271da177e4SLinus Torvalds 281da177e4SLinus Torvalds #include "resources.h" /* atm_find_dev */ 291da177e4SLinus Torvalds #include "common.h" /* prototypes */ 301da177e4SLinus Torvalds #include "protocols.h" /* atm_init_<transport> */ 311da177e4SLinus Torvalds #include "addr.h" /* address registry */ 321da177e4SLinus Torvalds #include "signaling.h" /* for WAITING and sigd_attach */ 331da177e4SLinus Torvalds 341da177e4SLinus Torvalds struct hlist_head vcc_hash[VCC_HTABLE_SIZE]; 35a8147d73SJoe Perches EXPORT_SYMBOL(vcc_hash); 36a8147d73SJoe Perches 371da177e4SLinus Torvalds DEFINE_RWLOCK(vcc_sklist_lock); 38a8147d73SJoe Perches EXPORT_SYMBOL(vcc_sklist_lock); 391da177e4SLinus Torvalds 407313bb8fSKarl Hiramoto static ATOMIC_NOTIFIER_HEAD(atm_dev_notify_chain); 417313bb8fSKarl Hiramoto 421da177e4SLinus Torvalds static void __vcc_insert_socket(struct sock *sk) 431da177e4SLinus Torvalds { 441da177e4SLinus Torvalds struct atm_vcc *vcc = atm_sk(sk); 45a8147d73SJoe Perches struct hlist_head *head = &vcc_hash[vcc->vci & (VCC_HTABLE_SIZE - 1)]; 4681c3d547SEric Dumazet sk->sk_hash = vcc->vci & (VCC_HTABLE_SIZE - 1); 471da177e4SLinus Torvalds sk_add_node(sk, head); 481da177e4SLinus Torvalds } 491da177e4SLinus Torvalds 501da177e4SLinus Torvalds void vcc_insert_socket(struct sock *sk) 511da177e4SLinus Torvalds { 521da177e4SLinus Torvalds write_lock_irq(&vcc_sklist_lock); 531da177e4SLinus Torvalds __vcc_insert_socket(sk); 541da177e4SLinus Torvalds write_unlock_irq(&vcc_sklist_lock); 551da177e4SLinus Torvalds } 56a8147d73SJoe Perches EXPORT_SYMBOL(vcc_insert_socket); 571da177e4SLinus Torvalds 581da177e4SLinus Torvalds static void vcc_remove_socket(struct sock *sk) 591da177e4SLinus Torvalds { 601da177e4SLinus Torvalds write_lock_irq(&vcc_sklist_lock); 611da177e4SLinus Torvalds sk_del_node_init(sk); 621da177e4SLinus Torvalds write_unlock_irq(&vcc_sklist_lock); 631da177e4SLinus Torvalds } 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds static struct sk_buff *alloc_tx(struct atm_vcc *vcc, unsigned int size) 661da177e4SLinus Torvalds { 671da177e4SLinus Torvalds struct sk_buff *skb; 681da177e4SLinus Torvalds struct sock *sk = sk_atm(vcc); 691da177e4SLinus Torvalds 7081e2a3d5SEric Dumazet if (sk_wmem_alloc_get(sk) && !atm_may_send(vcc, size)) { 7152240062SStephen Hemminger pr_debug("Sorry: wmem_alloc = %d, size = %d, sndbuf = %d\n", 7299824461SJoe Perches sk_wmem_alloc_get(sk), size, sk->sk_sndbuf); 731da177e4SLinus Torvalds return NULL; 741da177e4SLinus Torvalds } 7581e2a3d5SEric Dumazet while (!(skb = alloc_skb(size, GFP_KERNEL))) 7681e2a3d5SEric Dumazet schedule(); 77a8147d73SJoe Perches pr_debug("%d += %d\n", sk_wmem_alloc_get(sk), skb->truesize); 781da177e4SLinus Torvalds atomic_add(skb->truesize, &sk->sk_wmem_alloc); 791da177e4SLinus Torvalds return skb; 801da177e4SLinus Torvalds } 811da177e4SLinus Torvalds 821da177e4SLinus Torvalds static void vcc_sock_destruct(struct sock *sk) 831da177e4SLinus Torvalds { 841da177e4SLinus Torvalds if (atomic_read(&sk->sk_rmem_alloc)) 85a8147d73SJoe Perches printk(KERN_DEBUG "%s: rmem leakage (%d bytes) detected.\n", 86a8147d73SJoe Perches __func__, atomic_read(&sk->sk_rmem_alloc)); 871da177e4SLinus Torvalds 881da177e4SLinus Torvalds if (atomic_read(&sk->sk_wmem_alloc)) 89a8147d73SJoe Perches printk(KERN_DEBUG "%s: wmem leakage (%d bytes) detected.\n", 90a8147d73SJoe Perches __func__, atomic_read(&sk->sk_wmem_alloc)); 911da177e4SLinus Torvalds } 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds static void vcc_def_wakeup(struct sock *sk) 941da177e4SLinus Torvalds { 9543815482SEric Dumazet struct socket_wq *wq; 9643815482SEric Dumazet 9743815482SEric Dumazet rcu_read_lock(); 9843815482SEric Dumazet wq = rcu_dereference(sk->sk_wq); 9943815482SEric Dumazet if (wq_has_sleeper(wq)) 10043815482SEric Dumazet wake_up(&wq->wait); 10143815482SEric Dumazet rcu_read_unlock(); 1021da177e4SLinus Torvalds } 1031da177e4SLinus Torvalds 1041da177e4SLinus Torvalds static inline int vcc_writable(struct sock *sk) 1051da177e4SLinus Torvalds { 1061da177e4SLinus Torvalds struct atm_vcc *vcc = atm_sk(sk); 1071da177e4SLinus Torvalds 1081da177e4SLinus Torvalds return (vcc->qos.txtp.max_sdu + 1091da177e4SLinus Torvalds atomic_read(&sk->sk_wmem_alloc)) <= sk->sk_sndbuf; 1101da177e4SLinus Torvalds } 1111da177e4SLinus Torvalds 1121da177e4SLinus Torvalds static void vcc_write_space(struct sock *sk) 1131da177e4SLinus Torvalds { 11443815482SEric Dumazet struct socket_wq *wq; 11543815482SEric Dumazet 11643815482SEric Dumazet rcu_read_lock(); 1171da177e4SLinus Torvalds 1181da177e4SLinus Torvalds if (vcc_writable(sk)) { 11943815482SEric Dumazet wq = rcu_dereference(sk->sk_wq); 12043815482SEric Dumazet if (wq_has_sleeper(wq)) 12143815482SEric Dumazet wake_up_interruptible(&wq->wait); 1221da177e4SLinus Torvalds 1238d8ad9d7SPavel Emelyanov sk_wake_async(sk, SOCK_WAKE_SPACE, POLL_OUT); 1241da177e4SLinus Torvalds } 1251da177e4SLinus Torvalds 12643815482SEric Dumazet rcu_read_unlock(); 1271da177e4SLinus Torvalds } 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds static struct proto vcc_proto = { 1301da177e4SLinus Torvalds .name = "VCC", 1311da177e4SLinus Torvalds .owner = THIS_MODULE, 1321da177e4SLinus Torvalds .obj_size = sizeof(struct atm_vcc), 1331da177e4SLinus Torvalds }; 1341da177e4SLinus Torvalds 1351b8d7ae4SEric W. Biederman int vcc_create(struct net *net, struct socket *sock, int protocol, int family) 1361da177e4SLinus Torvalds { 1371da177e4SLinus Torvalds struct sock *sk; 1381da177e4SLinus Torvalds struct atm_vcc *vcc; 1391da177e4SLinus Torvalds 1401da177e4SLinus Torvalds sock->sk = NULL; 1411da177e4SLinus Torvalds if (sock->type == SOCK_STREAM) 1421da177e4SLinus Torvalds return -EINVAL; 1436257ff21SPavel Emelyanov sk = sk_alloc(net, family, GFP_KERNEL, &vcc_proto); 1441da177e4SLinus Torvalds if (!sk) 1451da177e4SLinus Torvalds return -ENOMEM; 1461da177e4SLinus Torvalds sock_init_data(sock, sk); 1471da177e4SLinus Torvalds sk->sk_state_change = vcc_def_wakeup; 1481da177e4SLinus Torvalds sk->sk_write_space = vcc_write_space; 1491da177e4SLinus Torvalds 1501da177e4SLinus Torvalds vcc = atm_sk(sk); 1511da177e4SLinus Torvalds vcc->dev = NULL; 1521da177e4SLinus Torvalds memset(&vcc->local, 0, sizeof(struct sockaddr_atmsvc)); 1531da177e4SLinus Torvalds memset(&vcc->remote, 0, sizeof(struct sockaddr_atmsvc)); 1541da177e4SLinus Torvalds vcc->qos.txtp.max_sdu = 1 << 16; /* for meta VCs */ 15581e2a3d5SEric Dumazet atomic_set(&sk->sk_wmem_alloc, 1); 1561da177e4SLinus Torvalds atomic_set(&sk->sk_rmem_alloc, 0); 1571da177e4SLinus Torvalds vcc->push = NULL; 1581da177e4SLinus Torvalds vcc->pop = NULL; 1591da177e4SLinus Torvalds vcc->push_oam = NULL; 1601da177e4SLinus Torvalds vcc->vpi = vcc->vci = 0; /* no VCI/VPI yet */ 1611da177e4SLinus Torvalds vcc->atm_options = vcc->aal_options = 0; 1621da177e4SLinus Torvalds sk->sk_destruct = vcc_sock_destruct; 1631da177e4SLinus Torvalds return 0; 1641da177e4SLinus Torvalds } 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds static void vcc_destroy_socket(struct sock *sk) 1671da177e4SLinus Torvalds { 1681da177e4SLinus Torvalds struct atm_vcc *vcc = atm_sk(sk); 1691da177e4SLinus Torvalds struct sk_buff *skb; 1701da177e4SLinus Torvalds 1711da177e4SLinus Torvalds set_bit(ATM_VF_CLOSE, &vcc->flags); 1721da177e4SLinus Torvalds clear_bit(ATM_VF_READY, &vcc->flags); 1731da177e4SLinus Torvalds if (vcc->dev) { 1741da177e4SLinus Torvalds if (vcc->dev->ops->close) 1751da177e4SLinus Torvalds vcc->dev->ops->close(vcc); 1761da177e4SLinus Torvalds if (vcc->push) 1771da177e4SLinus Torvalds vcc->push(vcc, NULL); /* atmarpd has no push */ 1781da177e4SLinus Torvalds 1791da177e4SLinus Torvalds while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) { 1801da177e4SLinus Torvalds atm_return(vcc, skb->truesize); 1811da177e4SLinus Torvalds kfree_skb(skb); 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds 1841da177e4SLinus Torvalds module_put(vcc->dev->ops->owner); 1851da177e4SLinus Torvalds atm_dev_put(vcc->dev); 1861da177e4SLinus Torvalds } 1879301e320SChas Williams 1889301e320SChas Williams vcc_remove_socket(sk); 1891da177e4SLinus Torvalds } 1901da177e4SLinus Torvalds 1911da177e4SLinus Torvalds int vcc_release(struct socket *sock) 1921da177e4SLinus Torvalds { 1931da177e4SLinus Torvalds struct sock *sk = sock->sk; 1941da177e4SLinus Torvalds 1951da177e4SLinus Torvalds if (sk) { 1961da177e4SLinus Torvalds lock_sock(sk); 1971da177e4SLinus Torvalds vcc_destroy_socket(sock->sk); 1981da177e4SLinus Torvalds release_sock(sk); 1991da177e4SLinus Torvalds sock_put(sk); 2001da177e4SLinus Torvalds } 2011da177e4SLinus Torvalds 2021da177e4SLinus Torvalds return 0; 2031da177e4SLinus Torvalds } 2041da177e4SLinus Torvalds 2051da177e4SLinus Torvalds void vcc_release_async(struct atm_vcc *vcc, int reply) 2061da177e4SLinus Torvalds { 2071da177e4SLinus Torvalds struct sock *sk = sk_atm(vcc); 2081da177e4SLinus Torvalds 2091da177e4SLinus Torvalds set_bit(ATM_VF_CLOSE, &vcc->flags); 2101da177e4SLinus Torvalds sk->sk_shutdown |= RCV_SHUTDOWN; 2111da177e4SLinus Torvalds sk->sk_err = -reply; 2121da177e4SLinus Torvalds clear_bit(ATM_VF_WAITING, &vcc->flags); 2131da177e4SLinus Torvalds sk->sk_state_change(sk); 2141da177e4SLinus Torvalds } 2151da177e4SLinus Torvalds EXPORT_SYMBOL(vcc_release_async); 2161da177e4SLinus Torvalds 2174e55f578SJorge Boncompte [DTI2] void vcc_process_recv_queue(struct atm_vcc *vcc) 2184e55f578SJorge Boncompte [DTI2] { 2194e55f578SJorge Boncompte [DTI2] struct sk_buff_head queue, *rq; 2204e55f578SJorge Boncompte [DTI2] struct sk_buff *skb, *tmp; 2214e55f578SJorge Boncompte [DTI2] unsigned long flags; 2224e55f578SJorge Boncompte [DTI2] 2234e55f578SJorge Boncompte [DTI2] __skb_queue_head_init(&queue); 2244e55f578SJorge Boncompte [DTI2] rq = &sk_atm(vcc)->sk_receive_queue; 2254e55f578SJorge Boncompte [DTI2] 2264e55f578SJorge Boncompte [DTI2] spin_lock_irqsave(&rq->lock, flags); 2274e55f578SJorge Boncompte [DTI2] skb_queue_splice_init(rq, &queue); 2284e55f578SJorge Boncompte [DTI2] spin_unlock_irqrestore(&rq->lock, flags); 2294e55f578SJorge Boncompte [DTI2] 2304e55f578SJorge Boncompte [DTI2] skb_queue_walk_safe(&queue, skb, tmp) { 2314e55f578SJorge Boncompte [DTI2] __skb_unlink(skb, &queue); 2324e55f578SJorge Boncompte [DTI2] vcc->push(vcc, skb); 2334e55f578SJorge Boncompte [DTI2] } 2344e55f578SJorge Boncompte [DTI2] } 2354e55f578SJorge Boncompte [DTI2] EXPORT_SYMBOL(vcc_process_recv_queue); 2364e55f578SJorge Boncompte [DTI2] 2377313bb8fSKarl Hiramoto void atm_dev_signal_change(struct atm_dev *dev, char signal) 2387313bb8fSKarl Hiramoto { 2397313bb8fSKarl Hiramoto pr_debug("%s signal=%d dev=%p number=%d dev->signal=%d\n", 2407313bb8fSKarl Hiramoto __func__, signal, dev, dev->number, dev->signal); 2417313bb8fSKarl Hiramoto 2427313bb8fSKarl Hiramoto /* atm driver sending invalid signal */ 2437313bb8fSKarl Hiramoto WARN_ON(signal < ATM_PHY_SIG_LOST || signal > ATM_PHY_SIG_FOUND); 2447313bb8fSKarl Hiramoto 2457313bb8fSKarl Hiramoto if (dev->signal == signal) 2467313bb8fSKarl Hiramoto return; /* no change */ 2477313bb8fSKarl Hiramoto 2487313bb8fSKarl Hiramoto dev->signal = signal; 2497313bb8fSKarl Hiramoto 2507313bb8fSKarl Hiramoto atomic_notifier_call_chain(&atm_dev_notify_chain, signal, dev); 2517313bb8fSKarl Hiramoto } 2527313bb8fSKarl Hiramoto EXPORT_SYMBOL(atm_dev_signal_change); 2531da177e4SLinus Torvalds 25464bf69ddSStanislaw Gruszka void atm_dev_release_vccs(struct atm_dev *dev) 25564bf69ddSStanislaw Gruszka { 25664bf69ddSStanislaw Gruszka int i; 25764bf69ddSStanislaw Gruszka 25864bf69ddSStanislaw Gruszka write_lock_irq(&vcc_sklist_lock); 25964bf69ddSStanislaw Gruszka for (i = 0; i < VCC_HTABLE_SIZE; i++) { 26064bf69ddSStanislaw Gruszka struct hlist_head *head = &vcc_hash[i]; 26164bf69ddSStanislaw Gruszka struct hlist_node *node, *tmp; 26264bf69ddSStanislaw Gruszka struct sock *s; 26364bf69ddSStanislaw Gruszka struct atm_vcc *vcc; 26464bf69ddSStanislaw Gruszka 26564bf69ddSStanislaw Gruszka sk_for_each_safe(s, node, tmp, head) { 26664bf69ddSStanislaw Gruszka vcc = atm_sk(s); 26764bf69ddSStanislaw Gruszka if (vcc->dev == dev) { 26864bf69ddSStanislaw Gruszka vcc_release_async(vcc, -EPIPE); 26964bf69ddSStanislaw Gruszka sk_del_node_init(s); 27064bf69ddSStanislaw Gruszka } 27164bf69ddSStanislaw Gruszka } 27264bf69ddSStanislaw Gruszka } 27364bf69ddSStanislaw Gruszka write_unlock_irq(&vcc_sklist_lock); 27464bf69ddSStanislaw Gruszka } 275c031235bSPhilip A. Prindeville EXPORT_SYMBOL(atm_dev_release_vccs); 27664bf69ddSStanislaw Gruszka 2771da177e4SLinus Torvalds static int adjust_tp(struct atm_trafprm *tp, unsigned char aal) 2781da177e4SLinus Torvalds { 2791da177e4SLinus Torvalds int max_sdu; 2801da177e4SLinus Torvalds 281a8147d73SJoe Perches if (!tp->traffic_class) 282a8147d73SJoe Perches return 0; 2831da177e4SLinus Torvalds switch (aal) { 2841da177e4SLinus Torvalds case ATM_AAL0: 2851da177e4SLinus Torvalds max_sdu = ATM_CELL_SIZE-1; 2861da177e4SLinus Torvalds break; 2871da177e4SLinus Torvalds case ATM_AAL34: 2881da177e4SLinus Torvalds max_sdu = ATM_MAX_AAL34_PDU; 2891da177e4SLinus Torvalds break; 2901da177e4SLinus Torvalds default: 29199824461SJoe Perches pr_warning("AAL problems ... (%d)\n", aal); 2921da177e4SLinus Torvalds /* fall through */ 2931da177e4SLinus Torvalds case ATM_AAL5: 2941da177e4SLinus Torvalds max_sdu = ATM_MAX_AAL5_PDU; 2951da177e4SLinus Torvalds } 296a8147d73SJoe Perches if (!tp->max_sdu) 297a8147d73SJoe Perches tp->max_sdu = max_sdu; 298a8147d73SJoe Perches else if (tp->max_sdu > max_sdu) 299a8147d73SJoe Perches return -EINVAL; 300a8147d73SJoe Perches if (!tp->max_cdv) 301a8147d73SJoe Perches tp->max_cdv = ATM_MAX_CDV; 3021da177e4SLinus Torvalds return 0; 3031da177e4SLinus Torvalds } 3041da177e4SLinus Torvalds 30561c33e01SMitchell Blank Jr static int check_ci(const struct atm_vcc *vcc, short vpi, int vci) 3061da177e4SLinus Torvalds { 307a8147d73SJoe Perches struct hlist_head *head = &vcc_hash[vci & (VCC_HTABLE_SIZE - 1)]; 3081da177e4SLinus Torvalds struct hlist_node *node; 3091da177e4SLinus Torvalds struct sock *s; 3101da177e4SLinus Torvalds struct atm_vcc *walk; 3111da177e4SLinus Torvalds 3121da177e4SLinus Torvalds sk_for_each(s, node, head) { 3131da177e4SLinus Torvalds walk = atm_sk(s); 3141da177e4SLinus Torvalds if (walk->dev != vcc->dev) 3151da177e4SLinus Torvalds continue; 3161da177e4SLinus Torvalds if (test_bit(ATM_VF_ADDR, &walk->flags) && walk->vpi == vpi && 3171da177e4SLinus Torvalds walk->vci == vci && ((walk->qos.txtp.traffic_class != 3181da177e4SLinus Torvalds ATM_NONE && vcc->qos.txtp.traffic_class != ATM_NONE) || 3191da177e4SLinus Torvalds (walk->qos.rxtp.traffic_class != ATM_NONE && 3201da177e4SLinus Torvalds vcc->qos.rxtp.traffic_class != ATM_NONE))) 3211da177e4SLinus Torvalds return -EADDRINUSE; 3221da177e4SLinus Torvalds } 3231da177e4SLinus Torvalds 3241da177e4SLinus Torvalds /* allow VCCs with same VPI/VCI iff they don't collide on 3251da177e4SLinus Torvalds TX/RX (but we may refuse such sharing for other reasons, 3261da177e4SLinus Torvalds e.g. if protocol requires to have both channels) */ 3271da177e4SLinus Torvalds 3281da177e4SLinus Torvalds return 0; 3291da177e4SLinus Torvalds } 3301da177e4SLinus Torvalds 33161c33e01SMitchell Blank Jr static int find_ci(const struct atm_vcc *vcc, short *vpi, int *vci) 3321da177e4SLinus Torvalds { 3331da177e4SLinus Torvalds static short p; /* poor man's per-device cache */ 3341da177e4SLinus Torvalds static int c; 3351da177e4SLinus Torvalds short old_p; 3361da177e4SLinus Torvalds int old_c; 3371da177e4SLinus Torvalds int err; 3381da177e4SLinus Torvalds 3391da177e4SLinus Torvalds if (*vpi != ATM_VPI_ANY && *vci != ATM_VCI_ANY) { 3401da177e4SLinus Torvalds err = check_ci(vcc, *vpi, *vci); 3411da177e4SLinus Torvalds return err; 3421da177e4SLinus Torvalds } 3431da177e4SLinus Torvalds /* last scan may have left values out of bounds for current device */ 3441da177e4SLinus Torvalds if (*vpi != ATM_VPI_ANY) 3451da177e4SLinus Torvalds p = *vpi; 3461da177e4SLinus Torvalds else if (p >= 1 << vcc->dev->ci_range.vpi_bits) 3471da177e4SLinus Torvalds p = 0; 3481da177e4SLinus Torvalds if (*vci != ATM_VCI_ANY) 3491da177e4SLinus Torvalds c = *vci; 3501da177e4SLinus Torvalds else if (c < ATM_NOT_RSV_VCI || c >= 1 << vcc->dev->ci_range.vci_bits) 3511da177e4SLinus Torvalds c = ATM_NOT_RSV_VCI; 3521da177e4SLinus Torvalds old_p = p; 3531da177e4SLinus Torvalds old_c = c; 3541da177e4SLinus Torvalds do { 3551da177e4SLinus Torvalds if (!check_ci(vcc, p, c)) { 3561da177e4SLinus Torvalds *vpi = p; 3571da177e4SLinus Torvalds *vci = c; 3581da177e4SLinus Torvalds return 0; 3591da177e4SLinus Torvalds } 3601da177e4SLinus Torvalds if (*vci == ATM_VCI_ANY) { 3611da177e4SLinus Torvalds c++; 3621da177e4SLinus Torvalds if (c >= 1 << vcc->dev->ci_range.vci_bits) 3631da177e4SLinus Torvalds c = ATM_NOT_RSV_VCI; 3641da177e4SLinus Torvalds } 3651da177e4SLinus Torvalds if ((c == ATM_NOT_RSV_VCI || *vci != ATM_VCI_ANY) && 3661da177e4SLinus Torvalds *vpi == ATM_VPI_ANY) { 3671da177e4SLinus Torvalds p++; 368a8147d73SJoe Perches if (p >= 1 << vcc->dev->ci_range.vpi_bits) 369a8147d73SJoe Perches p = 0; 3701da177e4SLinus Torvalds } 371a8147d73SJoe Perches } while (old_p != p || old_c != c); 3721da177e4SLinus Torvalds return -EADDRINUSE; 3731da177e4SLinus Torvalds } 3741da177e4SLinus Torvalds 3751da177e4SLinus Torvalds static int __vcc_connect(struct atm_vcc *vcc, struct atm_dev *dev, short vpi, 3761da177e4SLinus Torvalds int vci) 3771da177e4SLinus Torvalds { 3781da177e4SLinus Torvalds struct sock *sk = sk_atm(vcc); 3791da177e4SLinus Torvalds int error; 3801da177e4SLinus Torvalds 3811da177e4SLinus Torvalds if ((vpi != ATM_VPI_UNSPEC && vpi != ATM_VPI_ANY && 3821da177e4SLinus Torvalds vpi >> dev->ci_range.vpi_bits) || (vci != ATM_VCI_UNSPEC && 3831da177e4SLinus Torvalds vci != ATM_VCI_ANY && vci >> dev->ci_range.vci_bits)) 3841da177e4SLinus Torvalds return -EINVAL; 3851da177e4SLinus Torvalds if (vci > 0 && vci < ATM_NOT_RSV_VCI && !capable(CAP_NET_BIND_SERVICE)) 3861da177e4SLinus Torvalds return -EPERM; 38764bf69ddSStanislaw Gruszka error = -ENODEV; 3881da177e4SLinus Torvalds if (!try_module_get(dev->ops->owner)) 38964bf69ddSStanislaw Gruszka return error; 3901da177e4SLinus Torvalds vcc->dev = dev; 3911da177e4SLinus Torvalds write_lock_irq(&vcc_sklist_lock); 39264bf69ddSStanislaw Gruszka if (test_bit(ATM_DF_REMOVED, &dev->flags) || 39364bf69ddSStanislaw Gruszka (error = find_ci(vcc, &vpi, &vci))) { 3941da177e4SLinus Torvalds write_unlock_irq(&vcc_sklist_lock); 3951da177e4SLinus Torvalds goto fail_module_put; 3961da177e4SLinus Torvalds } 3971da177e4SLinus Torvalds vcc->vpi = vpi; 3981da177e4SLinus Torvalds vcc->vci = vci; 3991da177e4SLinus Torvalds __vcc_insert_socket(sk); 4001da177e4SLinus Torvalds write_unlock_irq(&vcc_sklist_lock); 4011da177e4SLinus Torvalds switch (vcc->qos.aal) { 4021da177e4SLinus Torvalds case ATM_AAL0: 4031da177e4SLinus Torvalds error = atm_init_aal0(vcc); 4041da177e4SLinus Torvalds vcc->stats = &dev->stats.aal0; 4051da177e4SLinus Torvalds break; 4061da177e4SLinus Torvalds case ATM_AAL34: 4071da177e4SLinus Torvalds error = atm_init_aal34(vcc); 4081da177e4SLinus Torvalds vcc->stats = &dev->stats.aal34; 4091da177e4SLinus Torvalds break; 4101da177e4SLinus Torvalds case ATM_NO_AAL: 4111da177e4SLinus Torvalds /* ATM_AAL5 is also used in the "0 for default" case */ 4121da177e4SLinus Torvalds vcc->qos.aal = ATM_AAL5; 4131da177e4SLinus Torvalds /* fall through */ 4141da177e4SLinus Torvalds case ATM_AAL5: 4151da177e4SLinus Torvalds error = atm_init_aal5(vcc); 4161da177e4SLinus Torvalds vcc->stats = &dev->stats.aal5; 4171da177e4SLinus Torvalds break; 4181da177e4SLinus Torvalds default: 4191da177e4SLinus Torvalds error = -EPROTOTYPE; 4201da177e4SLinus Torvalds } 421a8147d73SJoe Perches if (!error) 422a8147d73SJoe Perches error = adjust_tp(&vcc->qos.txtp, vcc->qos.aal); 423a8147d73SJoe Perches if (!error) 424a8147d73SJoe Perches error = adjust_tp(&vcc->qos.rxtp, vcc->qos.aal); 4251da177e4SLinus Torvalds if (error) 4261da177e4SLinus Torvalds goto fail; 42752240062SStephen Hemminger pr_debug("VCC %d.%d, AAL %d\n", vpi, vci, vcc->qos.aal); 42899824461SJoe Perches pr_debug(" TX: %d, PCR %d..%d, SDU %d\n", 42999824461SJoe Perches vcc->qos.txtp.traffic_class, 43099824461SJoe Perches vcc->qos.txtp.min_pcr, 43199824461SJoe Perches vcc->qos.txtp.max_pcr, 43299824461SJoe Perches vcc->qos.txtp.max_sdu); 43399824461SJoe Perches pr_debug(" RX: %d, PCR %d..%d, SDU %d\n", 43499824461SJoe Perches vcc->qos.rxtp.traffic_class, 43599824461SJoe Perches vcc->qos.rxtp.min_pcr, 43699824461SJoe Perches vcc->qos.rxtp.max_pcr, 43799824461SJoe Perches vcc->qos.rxtp.max_sdu); 4381da177e4SLinus Torvalds 4391da177e4SLinus Torvalds if (dev->ops->open) { 440a8147d73SJoe Perches error = dev->ops->open(vcc); 441a8147d73SJoe Perches if (error) 4421da177e4SLinus Torvalds goto fail; 4431da177e4SLinus Torvalds } 4441da177e4SLinus Torvalds return 0; 4451da177e4SLinus Torvalds 4461da177e4SLinus Torvalds fail: 4471da177e4SLinus Torvalds vcc_remove_socket(sk); 4481da177e4SLinus Torvalds fail_module_put: 4491da177e4SLinus Torvalds module_put(dev->ops->owner); 4501da177e4SLinus Torvalds /* ensure we get dev module ref count correct */ 4511da177e4SLinus Torvalds vcc->dev = NULL; 4521da177e4SLinus Torvalds return error; 4531da177e4SLinus Torvalds } 4541da177e4SLinus Torvalds 4551da177e4SLinus Torvalds int vcc_connect(struct socket *sock, int itf, short vpi, int vci) 4561da177e4SLinus Torvalds { 4571da177e4SLinus Torvalds struct atm_dev *dev; 4581da177e4SLinus Torvalds struct atm_vcc *vcc = ATM_SD(sock); 4591da177e4SLinus Torvalds int error; 4601da177e4SLinus Torvalds 46199824461SJoe Perches pr_debug("(vpi %d, vci %d)\n", vpi, vci); 4621da177e4SLinus Torvalds if (sock->state == SS_CONNECTED) 4631da177e4SLinus Torvalds return -EISCONN; 4641da177e4SLinus Torvalds if (sock->state != SS_UNCONNECTED) 4651da177e4SLinus Torvalds return -EINVAL; 4661da177e4SLinus Torvalds if (!(vpi || vci)) 4671da177e4SLinus Torvalds return -EINVAL; 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds if (vpi != ATM_VPI_UNSPEC && vci != ATM_VCI_UNSPEC) 4701da177e4SLinus Torvalds clear_bit(ATM_VF_PARTIAL, &vcc->flags); 4711da177e4SLinus Torvalds else 4721da177e4SLinus Torvalds if (test_bit(ATM_VF_PARTIAL, &vcc->flags)) 4731da177e4SLinus Torvalds return -EINVAL; 47499824461SJoe Perches pr_debug("(TX: cl %d,bw %d-%d,sdu %d; " 4751da177e4SLinus Torvalds "RX: cl %d,bw %d-%d,sdu %d,AAL %s%d)\n", 4761da177e4SLinus Torvalds vcc->qos.txtp.traffic_class, vcc->qos.txtp.min_pcr, 4771da177e4SLinus Torvalds vcc->qos.txtp.max_pcr, vcc->qos.txtp.max_sdu, 4781da177e4SLinus Torvalds vcc->qos.rxtp.traffic_class, vcc->qos.rxtp.min_pcr, 4791da177e4SLinus Torvalds vcc->qos.rxtp.max_pcr, vcc->qos.rxtp.max_sdu, 48099824461SJoe Perches vcc->qos.aal == ATM_AAL5 ? "" : 48199824461SJoe Perches vcc->qos.aal == ATM_AAL0 ? "" : " ??? code ", 48299824461SJoe Perches vcc->qos.aal == ATM_AAL0 ? 0 : vcc->qos.aal); 4831da177e4SLinus Torvalds if (!test_bit(ATM_VF_HASQOS, &vcc->flags)) 4841da177e4SLinus Torvalds return -EBADFD; 4851da177e4SLinus Torvalds if (vcc->qos.txtp.traffic_class == ATM_ANYCLASS || 4861da177e4SLinus Torvalds vcc->qos.rxtp.traffic_class == ATM_ANYCLASS) 4871da177e4SLinus Torvalds return -EINVAL; 488c9933d08SMitchell Blank Jr if (likely(itf != ATM_ITF_ANY)) { 489a8147d73SJoe Perches dev = try_then_request_module(atm_dev_lookup(itf), 490a8147d73SJoe Perches "atm-device-%d", itf); 491c9933d08SMitchell Blank Jr } else { 492c9933d08SMitchell Blank Jr dev = NULL; 49357b47a53SIngo Molnar mutex_lock(&atm_dev_mutex); 494c9933d08SMitchell Blank Jr if (!list_empty(&atm_devs)) { 495a8147d73SJoe Perches dev = list_entry(atm_devs.next, 496a8147d73SJoe Perches struct atm_dev, dev_list); 497c9933d08SMitchell Blank Jr atm_dev_hold(dev); 498c9933d08SMitchell Blank Jr } 49957b47a53SIngo Molnar mutex_unlock(&atm_dev_mutex); 500c9933d08SMitchell Blank Jr } 5011da177e4SLinus Torvalds if (!dev) 5021da177e4SLinus Torvalds return -ENODEV; 5031da177e4SLinus Torvalds error = __vcc_connect(vcc, dev, vpi, vci); 5041da177e4SLinus Torvalds if (error) { 5051da177e4SLinus Torvalds atm_dev_put(dev); 5061da177e4SLinus Torvalds return error; 5071da177e4SLinus Torvalds } 5081da177e4SLinus Torvalds if (vpi == ATM_VPI_UNSPEC || vci == ATM_VCI_UNSPEC) 5091da177e4SLinus Torvalds set_bit(ATM_VF_PARTIAL, &vcc->flags); 5101da177e4SLinus Torvalds if (test_bit(ATM_VF_READY, &ATM_SD(sock)->flags)) 5111da177e4SLinus Torvalds sock->state = SS_CONNECTED; 5121da177e4SLinus Torvalds return 0; 5131da177e4SLinus Torvalds } 5141da177e4SLinus Torvalds 5151da177e4SLinus Torvalds int vcc_recvmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *msg, 5161da177e4SLinus Torvalds size_t size, int flags) 5171da177e4SLinus Torvalds { 5181da177e4SLinus Torvalds struct sock *sk = sock->sk; 5191da177e4SLinus Torvalds struct atm_vcc *vcc; 5201da177e4SLinus Torvalds struct sk_buff *skb; 5211da177e4SLinus Torvalds int copied, error = -EINVAL; 5221da177e4SLinus Torvalds 5231da177e4SLinus Torvalds if (sock->state != SS_CONNECTED) 5241da177e4SLinus Torvalds return -ENOTCONN; 52540ba8499SJorge Boncompte [DTI2] 52640ba8499SJorge Boncompte [DTI2] /* only handle MSG_DONTWAIT and MSG_PEEK */ 52740ba8499SJorge Boncompte [DTI2] if (flags & ~(MSG_DONTWAIT | MSG_PEEK)) 5281da177e4SLinus Torvalds return -EOPNOTSUPP; 52940ba8499SJorge Boncompte [DTI2] 5301da177e4SLinus Torvalds vcc = ATM_SD(sock); 5311da177e4SLinus Torvalds if (test_bit(ATM_VF_RELEASED, &vcc->flags) || 5321da177e4SLinus Torvalds test_bit(ATM_VF_CLOSE, &vcc->flags) || 5331da177e4SLinus Torvalds !test_bit(ATM_VF_READY, &vcc->flags)) 5341da177e4SLinus Torvalds return 0; 5351da177e4SLinus Torvalds 5361da177e4SLinus Torvalds skb = skb_recv_datagram(sk, flags, flags & MSG_DONTWAIT, &error); 5371da177e4SLinus Torvalds if (!skb) 5381da177e4SLinus Torvalds return error; 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds copied = skb->len; 5411da177e4SLinus Torvalds if (copied > size) { 5421da177e4SLinus Torvalds copied = size; 5431da177e4SLinus Torvalds msg->msg_flags |= MSG_TRUNC; 5441da177e4SLinus Torvalds } 5451da177e4SLinus Torvalds 5461da177e4SLinus Torvalds error = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); 5471da177e4SLinus Torvalds if (error) 5481da177e4SLinus Torvalds return error; 5493b885787SNeil Horman sock_recv_ts_and_drops(msg, sk, skb); 55040ba8499SJorge Boncompte [DTI2] 55140ba8499SJorge Boncompte [DTI2] if (!(flags & MSG_PEEK)) { 55240ba8499SJorge Boncompte [DTI2] pr_debug("%d -= %d\n", atomic_read(&sk->sk_rmem_alloc), 55340ba8499SJorge Boncompte [DTI2] skb->truesize); 5541da177e4SLinus Torvalds atm_return(vcc, skb->truesize); 55540ba8499SJorge Boncompte [DTI2] } 55640ba8499SJorge Boncompte [DTI2] 5571da177e4SLinus Torvalds skb_free_datagram(sk, skb); 5581da177e4SLinus Torvalds return copied; 5591da177e4SLinus Torvalds } 5601da177e4SLinus Torvalds 5611da177e4SLinus Torvalds int vcc_sendmsg(struct kiocb *iocb, struct socket *sock, struct msghdr *m, 5621da177e4SLinus Torvalds size_t total_len) 5631da177e4SLinus Torvalds { 5641da177e4SLinus Torvalds struct sock *sk = sock->sk; 5651da177e4SLinus Torvalds DEFINE_WAIT(wait); 5661da177e4SLinus Torvalds struct atm_vcc *vcc; 5671da177e4SLinus Torvalds struct sk_buff *skb; 5681da177e4SLinus Torvalds int eff, error; 5691da177e4SLinus Torvalds const void __user *buff; 5701da177e4SLinus Torvalds int size; 5711da177e4SLinus Torvalds 5721da177e4SLinus Torvalds lock_sock(sk); 5731da177e4SLinus Torvalds if (sock->state != SS_CONNECTED) { 5741da177e4SLinus Torvalds error = -ENOTCONN; 5751da177e4SLinus Torvalds goto out; 5761da177e4SLinus Torvalds } 5771da177e4SLinus Torvalds if (m->msg_name) { 5781da177e4SLinus Torvalds error = -EISCONN; 5791da177e4SLinus Torvalds goto out; 5801da177e4SLinus Torvalds } 5811da177e4SLinus Torvalds if (m->msg_iovlen != 1) { 5821da177e4SLinus Torvalds error = -ENOSYS; /* fix this later @@@ */ 5831da177e4SLinus Torvalds goto out; 5841da177e4SLinus Torvalds } 5851da177e4SLinus Torvalds buff = m->msg_iov->iov_base; 5861da177e4SLinus Torvalds size = m->msg_iov->iov_len; 5871da177e4SLinus Torvalds vcc = ATM_SD(sock); 5881da177e4SLinus Torvalds if (test_bit(ATM_VF_RELEASED, &vcc->flags) || 5891da177e4SLinus Torvalds test_bit(ATM_VF_CLOSE, &vcc->flags) || 5901da177e4SLinus Torvalds !test_bit(ATM_VF_READY, &vcc->flags)) { 5911da177e4SLinus Torvalds error = -EPIPE; 5921da177e4SLinus Torvalds send_sig(SIGPIPE, current, 0); 5931da177e4SLinus Torvalds goto out; 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds if (!size) { 5961da177e4SLinus Torvalds error = 0; 5971da177e4SLinus Torvalds goto out; 5981da177e4SLinus Torvalds } 5991da177e4SLinus Torvalds if (size < 0 || size > vcc->qos.txtp.max_sdu) { 6001da177e4SLinus Torvalds error = -EMSGSIZE; 6011da177e4SLinus Torvalds goto out; 6021da177e4SLinus Torvalds } 603e49332bdSJesper Juhl 6041da177e4SLinus Torvalds eff = (size+3) & ~3; /* align to word boundary */ 605aa395145SEric Dumazet prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 6061da177e4SLinus Torvalds error = 0; 6071da177e4SLinus Torvalds while (!(skb = alloc_tx(vcc, eff))) { 6081da177e4SLinus Torvalds if (m->msg_flags & MSG_DONTWAIT) { 6091da177e4SLinus Torvalds error = -EAGAIN; 6101da177e4SLinus Torvalds break; 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds schedule(); 6131da177e4SLinus Torvalds if (signal_pending(current)) { 6141da177e4SLinus Torvalds error = -ERESTARTSYS; 6151da177e4SLinus Torvalds break; 6161da177e4SLinus Torvalds } 6171da177e4SLinus Torvalds if (test_bit(ATM_VF_RELEASED, &vcc->flags) || 6181da177e4SLinus Torvalds test_bit(ATM_VF_CLOSE, &vcc->flags) || 6191da177e4SLinus Torvalds !test_bit(ATM_VF_READY, &vcc->flags)) { 6201da177e4SLinus Torvalds error = -EPIPE; 6211da177e4SLinus Torvalds send_sig(SIGPIPE, current, 0); 6221da177e4SLinus Torvalds break; 6231da177e4SLinus Torvalds } 624aa395145SEric Dumazet prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE); 6251da177e4SLinus Torvalds } 626aa395145SEric Dumazet finish_wait(sk_sleep(sk), &wait); 6271da177e4SLinus Torvalds if (error) 6281da177e4SLinus Torvalds goto out; 6291da177e4SLinus Torvalds skb->dev = NULL; /* for paths shared with net_device interfaces */ 6301da177e4SLinus Torvalds ATM_SKB(skb)->atm_options = vcc->atm_options; 6311da177e4SLinus Torvalds if (copy_from_user(skb_put(skb, size), buff, size)) { 6321da177e4SLinus Torvalds kfree_skb(skb); 6331da177e4SLinus Torvalds error = -EFAULT; 6341da177e4SLinus Torvalds goto out; 6351da177e4SLinus Torvalds } 636a8147d73SJoe Perches if (eff != size) 637a8147d73SJoe Perches memset(skb->data + size, 0, eff-size); 6381da177e4SLinus Torvalds error = vcc->dev->ops->send(vcc, skb); 6391da177e4SLinus Torvalds error = error ? error : size; 6401da177e4SLinus Torvalds out: 6411da177e4SLinus Torvalds release_sock(sk); 6421da177e4SLinus Torvalds return error; 6431da177e4SLinus Torvalds } 6441da177e4SLinus Torvalds 6451da177e4SLinus Torvalds unsigned int vcc_poll(struct file *file, struct socket *sock, poll_table *wait) 6461da177e4SLinus Torvalds { 6471da177e4SLinus Torvalds struct sock *sk = sock->sk; 6481da177e4SLinus Torvalds struct atm_vcc *vcc; 6491da177e4SLinus Torvalds unsigned int mask; 6501da177e4SLinus Torvalds 651aa395145SEric Dumazet sock_poll_wait(file, sk_sleep(sk), wait); 6521da177e4SLinus Torvalds mask = 0; 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds vcc = ATM_SD(sock); 6551da177e4SLinus Torvalds 6561da177e4SLinus Torvalds /* exceptional events */ 6571da177e4SLinus Torvalds if (sk->sk_err) 6581da177e4SLinus Torvalds mask = POLLERR; 6591da177e4SLinus Torvalds 6601da177e4SLinus Torvalds if (test_bit(ATM_VF_RELEASED, &vcc->flags) || 6611da177e4SLinus Torvalds test_bit(ATM_VF_CLOSE, &vcc->flags)) 6621da177e4SLinus Torvalds mask |= POLLHUP; 6631da177e4SLinus Torvalds 6641da177e4SLinus Torvalds /* readable? */ 6651da177e4SLinus Torvalds if (!skb_queue_empty(&sk->sk_receive_queue)) 6661da177e4SLinus Torvalds mask |= POLLIN | POLLRDNORM; 6671da177e4SLinus Torvalds 6681da177e4SLinus Torvalds /* writable? */ 6691da177e4SLinus Torvalds if (sock->state == SS_CONNECTING && 6701da177e4SLinus Torvalds test_bit(ATM_VF_WAITING, &vcc->flags)) 6711da177e4SLinus Torvalds return mask; 6721da177e4SLinus Torvalds 6731da177e4SLinus Torvalds if (vcc->qos.txtp.traffic_class != ATM_NONE && 6741da177e4SLinus Torvalds vcc_writable(sk)) 6751da177e4SLinus Torvalds mask |= POLLOUT | POLLWRNORM | POLLWRBAND; 6761da177e4SLinus Torvalds 6771da177e4SLinus Torvalds return mask; 6781da177e4SLinus Torvalds } 6791da177e4SLinus Torvalds 6801da177e4SLinus Torvalds static int atm_change_qos(struct atm_vcc *vcc, struct atm_qos *qos) 6811da177e4SLinus Torvalds { 6821da177e4SLinus Torvalds int error; 6831da177e4SLinus Torvalds 6841da177e4SLinus Torvalds /* 6851da177e4SLinus Torvalds * Don't let the QoS change the already connected AAL type nor the 6861da177e4SLinus Torvalds * traffic class. 6871da177e4SLinus Torvalds */ 6881da177e4SLinus Torvalds if (qos->aal != vcc->qos.aal || 6891da177e4SLinus Torvalds qos->rxtp.traffic_class != vcc->qos.rxtp.traffic_class || 6901da177e4SLinus Torvalds qos->txtp.traffic_class != vcc->qos.txtp.traffic_class) 6911da177e4SLinus Torvalds return -EINVAL; 6921da177e4SLinus Torvalds error = adjust_tp(&qos->txtp, qos->aal); 693a8147d73SJoe Perches if (!error) 694a8147d73SJoe Perches error = adjust_tp(&qos->rxtp, qos->aal); 695a8147d73SJoe Perches if (error) 696a8147d73SJoe Perches return error; 697a8147d73SJoe Perches if (!vcc->dev->ops->change_qos) 698a8147d73SJoe Perches return -EOPNOTSUPP; 6991da177e4SLinus Torvalds if (sk_atm(vcc)->sk_family == AF_ATMPVC) 7001da177e4SLinus Torvalds return vcc->dev->ops->change_qos(vcc, qos, ATM_MF_SET); 7011da177e4SLinus Torvalds return svc_change_qos(vcc, qos); 7021da177e4SLinus Torvalds } 7031da177e4SLinus Torvalds 70461c33e01SMitchell Blank Jr static int check_tp(const struct atm_trafprm *tp) 7051da177e4SLinus Torvalds { 7061da177e4SLinus Torvalds /* @@@ Should be merged with adjust_tp */ 707a8147d73SJoe Perches if (!tp->traffic_class || tp->traffic_class == ATM_ANYCLASS) 708a8147d73SJoe Perches return 0; 7091da177e4SLinus Torvalds if (tp->traffic_class != ATM_UBR && !tp->min_pcr && !tp->pcr && 710a8147d73SJoe Perches !tp->max_pcr) 711a8147d73SJoe Perches return -EINVAL; 712a8147d73SJoe Perches if (tp->min_pcr == ATM_MAX_PCR) 713a8147d73SJoe Perches return -EINVAL; 7141da177e4SLinus Torvalds if (tp->min_pcr && tp->max_pcr && tp->max_pcr != ATM_MAX_PCR && 715a8147d73SJoe Perches tp->min_pcr > tp->max_pcr) 716a8147d73SJoe Perches return -EINVAL; 7171da177e4SLinus Torvalds /* 7181da177e4SLinus Torvalds * We allow pcr to be outside [min_pcr,max_pcr], because later 7191da177e4SLinus Torvalds * adjustment may still push it in the valid range. 7201da177e4SLinus Torvalds */ 7211da177e4SLinus Torvalds return 0; 7221da177e4SLinus Torvalds } 7231da177e4SLinus Torvalds 72461c33e01SMitchell Blank Jr static int check_qos(const struct atm_qos *qos) 7251da177e4SLinus Torvalds { 7261da177e4SLinus Torvalds int error; 7271da177e4SLinus Torvalds 7281da177e4SLinus Torvalds if (!qos->txtp.traffic_class && !qos->rxtp.traffic_class) 7291da177e4SLinus Torvalds return -EINVAL; 7301da177e4SLinus Torvalds if (qos->txtp.traffic_class != qos->rxtp.traffic_class && 7311da177e4SLinus Torvalds qos->txtp.traffic_class && qos->rxtp.traffic_class && 7321da177e4SLinus Torvalds qos->txtp.traffic_class != ATM_ANYCLASS && 733a8147d73SJoe Perches qos->rxtp.traffic_class != ATM_ANYCLASS) 734a8147d73SJoe Perches return -EINVAL; 7351da177e4SLinus Torvalds error = check_tp(&qos->txtp); 736a8147d73SJoe Perches if (error) 737a8147d73SJoe Perches return error; 7381da177e4SLinus Torvalds return check_tp(&qos->rxtp); 7391da177e4SLinus Torvalds } 7401da177e4SLinus Torvalds 7411da177e4SLinus Torvalds int vcc_setsockopt(struct socket *sock, int level, int optname, 742b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 7431da177e4SLinus Torvalds { 7441da177e4SLinus Torvalds struct atm_vcc *vcc; 7451da177e4SLinus Torvalds unsigned long value; 7461da177e4SLinus Torvalds int error; 7471da177e4SLinus Torvalds 7481da177e4SLinus Torvalds if (__SO_LEVEL_MATCH(optname, level) && optlen != __SO_SIZE(optname)) 7491da177e4SLinus Torvalds return -EINVAL; 7501da177e4SLinus Torvalds 7511da177e4SLinus Torvalds vcc = ATM_SD(sock); 7521da177e4SLinus Torvalds switch (optname) { 7531da177e4SLinus Torvalds case SO_ATMQOS: 7541da177e4SLinus Torvalds { 7551da177e4SLinus Torvalds struct atm_qos qos; 7561da177e4SLinus Torvalds 7571da177e4SLinus Torvalds if (copy_from_user(&qos, optval, sizeof(qos))) 7581da177e4SLinus Torvalds return -EFAULT; 7591da177e4SLinus Torvalds error = check_qos(&qos); 760a8147d73SJoe Perches if (error) 761a8147d73SJoe Perches return error; 7621da177e4SLinus Torvalds if (sock->state == SS_CONNECTED) 7631da177e4SLinus Torvalds return atm_change_qos(vcc, &qos); 7641da177e4SLinus Torvalds if (sock->state != SS_UNCONNECTED) 7651da177e4SLinus Torvalds return -EBADFD; 7661da177e4SLinus Torvalds vcc->qos = qos; 7671da177e4SLinus Torvalds set_bit(ATM_VF_HASQOS, &vcc->flags); 7681da177e4SLinus Torvalds return 0; 7691da177e4SLinus Torvalds } 7701da177e4SLinus Torvalds case SO_SETCLP: 7711da177e4SLinus Torvalds if (get_user(value, (unsigned long __user *)optval)) 7721da177e4SLinus Torvalds return -EFAULT; 773a8147d73SJoe Perches if (value) 774a8147d73SJoe Perches vcc->atm_options |= ATM_ATMOPT_CLP; 775a8147d73SJoe Perches else 776a8147d73SJoe Perches vcc->atm_options &= ~ATM_ATMOPT_CLP; 7771da177e4SLinus Torvalds return 0; 7781da177e4SLinus Torvalds default: 779a8147d73SJoe Perches if (level == SOL_SOCKET) 780a8147d73SJoe Perches return -EINVAL; 7811da177e4SLinus Torvalds break; 7821da177e4SLinus Torvalds } 783a8147d73SJoe Perches if (!vcc->dev || !vcc->dev->ops->setsockopt) 784a8147d73SJoe Perches return -EINVAL; 7851da177e4SLinus Torvalds return vcc->dev->ops->setsockopt(vcc, level, optname, optval, optlen); 7861da177e4SLinus Torvalds } 7871da177e4SLinus Torvalds 7881da177e4SLinus Torvalds int vcc_getsockopt(struct socket *sock, int level, int optname, 7891da177e4SLinus Torvalds char __user *optval, int __user *optlen) 7901da177e4SLinus Torvalds { 7911da177e4SLinus Torvalds struct atm_vcc *vcc; 7921da177e4SLinus Torvalds int len; 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds if (get_user(len, optlen)) 7951da177e4SLinus Torvalds return -EFAULT; 7961da177e4SLinus Torvalds if (__SO_LEVEL_MATCH(optname, level) && len != __SO_SIZE(optname)) 7971da177e4SLinus Torvalds return -EINVAL; 7981da177e4SLinus Torvalds 7991da177e4SLinus Torvalds vcc = ATM_SD(sock); 8001da177e4SLinus Torvalds switch (optname) { 8011da177e4SLinus Torvalds case SO_ATMQOS: 8021da177e4SLinus Torvalds if (!test_bit(ATM_VF_HASQOS, &vcc->flags)) 8031da177e4SLinus Torvalds return -EINVAL; 804a8147d73SJoe Perches return copy_to_user(optval, &vcc->qos, sizeof(vcc->qos)) 805a8147d73SJoe Perches ? -EFAULT : 0; 8061da177e4SLinus Torvalds case SO_SETCLP: 807a8147d73SJoe Perches return put_user(vcc->atm_options & ATM_ATMOPT_CLP ? 1 : 0, 808a8147d73SJoe Perches (unsigned long __user *)optval) ? -EFAULT : 0; 8091da177e4SLinus Torvalds case SO_ATMPVC: 8101da177e4SLinus Torvalds { 8111da177e4SLinus Torvalds struct sockaddr_atmpvc pvc; 8121da177e4SLinus Torvalds 813a8147d73SJoe Perches if (!vcc->dev || !test_bit(ATM_VF_ADDR, &vcc->flags)) 8141da177e4SLinus Torvalds return -ENOTCONN; 815*e862f1a9SMathias Krause memset(&pvc, 0, sizeof(pvc)); 8161da177e4SLinus Torvalds pvc.sap_family = AF_ATMPVC; 8171da177e4SLinus Torvalds pvc.sap_addr.itf = vcc->dev->number; 8181da177e4SLinus Torvalds pvc.sap_addr.vpi = vcc->vpi; 8191da177e4SLinus Torvalds pvc.sap_addr.vci = vcc->vci; 820a8147d73SJoe Perches return copy_to_user(optval, &pvc, sizeof(pvc)) ? -EFAULT : 0; 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds default: 823a8147d73SJoe Perches if (level == SOL_SOCKET) 824a8147d73SJoe Perches return -EINVAL; 8251da177e4SLinus Torvalds break; 8261da177e4SLinus Torvalds } 827a8147d73SJoe Perches if (!vcc->dev || !vcc->dev->ops->getsockopt) 828a8147d73SJoe Perches return -EINVAL; 8291da177e4SLinus Torvalds return vcc->dev->ops->getsockopt(vcc, level, optname, optval, len); 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds 8327313bb8fSKarl Hiramoto int register_atmdevice_notifier(struct notifier_block *nb) 8337313bb8fSKarl Hiramoto { 8347313bb8fSKarl Hiramoto return atomic_notifier_chain_register(&atm_dev_notify_chain, nb); 8357313bb8fSKarl Hiramoto } 8367313bb8fSKarl Hiramoto EXPORT_SYMBOL_GPL(register_atmdevice_notifier); 8377313bb8fSKarl Hiramoto 8387313bb8fSKarl Hiramoto void unregister_atmdevice_notifier(struct notifier_block *nb) 8397313bb8fSKarl Hiramoto { 8407313bb8fSKarl Hiramoto atomic_notifier_chain_unregister(&atm_dev_notify_chain, nb); 8417313bb8fSKarl Hiramoto } 8427313bb8fSKarl Hiramoto EXPORT_SYMBOL_GPL(unregister_atmdevice_notifier); 8437313bb8fSKarl Hiramoto 8441da177e4SLinus Torvalds static int __init atm_init(void) 8451da177e4SLinus Torvalds { 8461da177e4SLinus Torvalds int error; 8471da177e4SLinus Torvalds 848a8147d73SJoe Perches error = proto_register(&vcc_proto, 0); 849a8147d73SJoe Perches if (error < 0) 8501da177e4SLinus Torvalds goto out; 851a8147d73SJoe Perches error = atmpvc_init(); 852a8147d73SJoe Perches if (error < 0) { 85399824461SJoe Perches pr_err("atmpvc_init() failed with %d\n", error); 8541da177e4SLinus Torvalds goto out_unregister_vcc_proto; 8551da177e4SLinus Torvalds } 856a8147d73SJoe Perches error = atmsvc_init(); 857a8147d73SJoe Perches if (error < 0) { 85899824461SJoe Perches pr_err("atmsvc_init() failed with %d\n", error); 8591da177e4SLinus Torvalds goto out_atmpvc_exit; 8601da177e4SLinus Torvalds } 861a8147d73SJoe Perches error = atm_proc_init(); 862a8147d73SJoe Perches if (error < 0) { 86399824461SJoe Perches pr_err("atm_proc_init() failed with %d\n", error); 8641da177e4SLinus Torvalds goto out_atmsvc_exit; 8651da177e4SLinus Torvalds } 866a8147d73SJoe Perches error = atm_sysfs_init(); 867a8147d73SJoe Perches if (error < 0) { 86899824461SJoe Perches pr_err("atm_sysfs_init() failed with %d\n", error); 869656d98b0SRoman Kagan goto out_atmproc_exit; 870656d98b0SRoman Kagan } 8711da177e4SLinus Torvalds out: 8721da177e4SLinus Torvalds return error; 873656d98b0SRoman Kagan out_atmproc_exit: 874656d98b0SRoman Kagan atm_proc_exit(); 8751da177e4SLinus Torvalds out_atmsvc_exit: 8761da177e4SLinus Torvalds atmsvc_exit(); 8771da177e4SLinus Torvalds out_atmpvc_exit: 8781da177e4SLinus Torvalds atmsvc_exit(); 8791da177e4SLinus Torvalds out_unregister_vcc_proto: 8801da177e4SLinus Torvalds proto_unregister(&vcc_proto); 8811da177e4SLinus Torvalds goto out; 8821da177e4SLinus Torvalds } 8831da177e4SLinus Torvalds 8841da177e4SLinus Torvalds static void __exit atm_exit(void) 8851da177e4SLinus Torvalds { 8861da177e4SLinus Torvalds atm_proc_exit(); 887656d98b0SRoman Kagan atm_sysfs_exit(); 8881da177e4SLinus Torvalds atmsvc_exit(); 8891da177e4SLinus Torvalds atmpvc_exit(); 8901da177e4SLinus Torvalds proto_unregister(&vcc_proto); 8911da177e4SLinus Torvalds } 8921da177e4SLinus Torvalds 89384ff602eSDaniel Walker subsys_initcall(atm_init); 89484ff602eSDaniel Walker 8951da177e4SLinus Torvalds module_exit(atm_exit); 8961da177e4SLinus Torvalds 8971da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 8981da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_ATMPVC); 8991da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_ATMSVC); 900