11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * X.25 Packet Layer release 002 31da177e4SLinus Torvalds * 41da177e4SLinus Torvalds * This is ALPHA test software. This code may break your machine, 51da177e4SLinus Torvalds * randomly fail to work with new releases, misbehave and/or generally 61da177e4SLinus Torvalds * screw up. It might even work. 71da177e4SLinus Torvalds * 81da177e4SLinus Torvalds * This code REQUIRES 2.1.15 or higher 91da177e4SLinus Torvalds * 101da177e4SLinus Torvalds * This module: 111da177e4SLinus Torvalds * This module is free software; you can redistribute it and/or 121da177e4SLinus Torvalds * modify it under the terms of the GNU General Public License 131da177e4SLinus Torvalds * as published by the Free Software Foundation; either version 141da177e4SLinus Torvalds * 2 of the License, or (at your option) any later version. 151da177e4SLinus Torvalds * 161da177e4SLinus Torvalds * History 171da177e4SLinus Torvalds * X.25 001 Jonathan Naylor Started coding. 181da177e4SLinus Torvalds * X.25 002 Jonathan Naylor Centralised disconnect handling. 191da177e4SLinus Torvalds * New timer architecture. 201da177e4SLinus Torvalds * 2000-03-11 Henner Eisen MSG_EOR handling more POSIX compliant. 211da177e4SLinus Torvalds * 2000-03-22 Daniela Squassoni Allowed disabling/enabling of 221da177e4SLinus Torvalds * facilities negotiation and increased 231da177e4SLinus Torvalds * the throughput upper limit. 241da177e4SLinus Torvalds * 2000-08-27 Arnaldo C. Melo s/suser/capable/ + micro cleanups 251da177e4SLinus Torvalds * 2000-09-04 Henner Eisen Set sock->state in x25_accept(). 261da177e4SLinus Torvalds * Fixed x25_output() related skb leakage. 271da177e4SLinus Torvalds * 2000-10-02 Henner Eisen Made x25_kick() single threaded per socket. 281da177e4SLinus Torvalds * 2000-10-27 Henner Eisen MSG_DONTWAIT for fragment allocation. 291da177e4SLinus Torvalds * 2000-11-14 Henner Eisen Closing datalink from NETDEV_GOING_DOWN 301da177e4SLinus Torvalds * 2002-10-06 Arnaldo C. Melo Get rid of cli/sti, move proc stuff to 311da177e4SLinus Torvalds * x25_proc.c, using seq_file 32cb65d506SShaun Pereira * 2005-04-02 Shaun Pereira Selective sub address matching 33cb65d506SShaun Pereira * with call user data 34ebc3f64bSShaun Pereira * 2005-04-15 Shaun Pereira Fast select with no restriction on 35ebc3f64bSShaun Pereira * response 361da177e4SLinus Torvalds */ 371da177e4SLinus Torvalds 381da177e4SLinus Torvalds #include <linux/config.h> 391da177e4SLinus Torvalds #include <linux/module.h> 404fc268d2SRandy Dunlap #include <linux/capability.h> 411da177e4SLinus Torvalds #include <linux/errno.h> 421da177e4SLinus Torvalds #include <linux/kernel.h> 431da177e4SLinus Torvalds #include <linux/sched.h> 441da177e4SLinus Torvalds #include <linux/timer.h> 451da177e4SLinus Torvalds #include <linux/string.h> 461da177e4SLinus Torvalds #include <linux/net.h> 471da177e4SLinus Torvalds #include <linux/netdevice.h> 481da177e4SLinus Torvalds #include <linux/if_arp.h> 491da177e4SLinus Torvalds #include <linux/skbuff.h> 501da177e4SLinus Torvalds #include <net/sock.h> 51c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h> 521da177e4SLinus Torvalds #include <asm/uaccess.h> 531da177e4SLinus Torvalds #include <linux/fcntl.h> 541da177e4SLinus Torvalds #include <linux/termios.h> /* For TIOCINQ/OUTQ */ 551da177e4SLinus Torvalds #include <linux/notifier.h> 561da177e4SLinus Torvalds #include <linux/init.h> 571b06e6baSShaun Pereira #include <linux/compat.h> 581b06e6baSShaun Pereira 591da177e4SLinus Torvalds #include <net/x25.h> 601b06e6baSShaun Pereira #include <net/compat.h> 611da177e4SLinus Torvalds 621da177e4SLinus Torvalds int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20; 631da177e4SLinus Torvalds int sysctl_x25_call_request_timeout = X25_DEFAULT_T21; 641da177e4SLinus Torvalds int sysctl_x25_reset_request_timeout = X25_DEFAULT_T22; 651da177e4SLinus Torvalds int sysctl_x25_clear_request_timeout = X25_DEFAULT_T23; 661da177e4SLinus Torvalds int sysctl_x25_ack_holdback_timeout = X25_DEFAULT_T2; 671da177e4SLinus Torvalds 681da177e4SLinus Torvalds HLIST_HEAD(x25_list); 691da177e4SLinus Torvalds DEFINE_RWLOCK(x25_list_lock); 701da177e4SLinus Torvalds 7190ddc4f0SEric Dumazet static const struct proto_ops x25_proto_ops; 721da177e4SLinus Torvalds 731da177e4SLinus Torvalds static struct x25_address null_x25_address = {" "}; 741da177e4SLinus Torvalds 751b06e6baSShaun Pereira #ifdef CONFIG_COMPAT 761b06e6baSShaun Pereira struct compat_x25_subscrip_struct { 771b06e6baSShaun Pereira char device[200-sizeof(compat_ulong_t)]; 781b06e6baSShaun Pereira compat_ulong_t global_facil_mask; 791b06e6baSShaun Pereira compat_uint_t extended; 801b06e6baSShaun Pereira }; 811b06e6baSShaun Pereira #endif 821b06e6baSShaun Pereira 831da177e4SLinus Torvalds int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr, 841da177e4SLinus Torvalds struct x25_address *calling_addr) 851da177e4SLinus Torvalds { 861da177e4SLinus Torvalds int called_len, calling_len; 871da177e4SLinus Torvalds char *called, *calling; 881da177e4SLinus Torvalds int i; 891da177e4SLinus Torvalds 901da177e4SLinus Torvalds called_len = (*p >> 0) & 0x0F; 911da177e4SLinus Torvalds calling_len = (*p >> 4) & 0x0F; 921da177e4SLinus Torvalds 931da177e4SLinus Torvalds called = called_addr->x25_addr; 941da177e4SLinus Torvalds calling = calling_addr->x25_addr; 951da177e4SLinus Torvalds p++; 961da177e4SLinus Torvalds 971da177e4SLinus Torvalds for (i = 0; i < (called_len + calling_len); i++) { 981da177e4SLinus Torvalds if (i < called_len) { 991da177e4SLinus Torvalds if (i % 2 != 0) { 1001da177e4SLinus Torvalds *called++ = ((*p >> 0) & 0x0F) + '0'; 1011da177e4SLinus Torvalds p++; 1021da177e4SLinus Torvalds } else { 1031da177e4SLinus Torvalds *called++ = ((*p >> 4) & 0x0F) + '0'; 1041da177e4SLinus Torvalds } 1051da177e4SLinus Torvalds } else { 1061da177e4SLinus Torvalds if (i % 2 != 0) { 1071da177e4SLinus Torvalds *calling++ = ((*p >> 0) & 0x0F) + '0'; 1081da177e4SLinus Torvalds p++; 1091da177e4SLinus Torvalds } else { 1101da177e4SLinus Torvalds *calling++ = ((*p >> 4) & 0x0F) + '0'; 1111da177e4SLinus Torvalds } 1121da177e4SLinus Torvalds } 1131da177e4SLinus Torvalds } 1141da177e4SLinus Torvalds 1151da177e4SLinus Torvalds *called = *calling = '\0'; 1161da177e4SLinus Torvalds 1171da177e4SLinus Torvalds return 1 + (called_len + calling_len + 1) / 2; 1181da177e4SLinus Torvalds } 1191da177e4SLinus Torvalds 1201da177e4SLinus Torvalds int x25_addr_aton(unsigned char *p, struct x25_address *called_addr, 1211da177e4SLinus Torvalds struct x25_address *calling_addr) 1221da177e4SLinus Torvalds { 1231da177e4SLinus Torvalds unsigned int called_len, calling_len; 1241da177e4SLinus Torvalds char *called, *calling; 1251da177e4SLinus Torvalds int i; 1261da177e4SLinus Torvalds 1271da177e4SLinus Torvalds called = called_addr->x25_addr; 1281da177e4SLinus Torvalds calling = calling_addr->x25_addr; 1291da177e4SLinus Torvalds 1301da177e4SLinus Torvalds called_len = strlen(called); 1311da177e4SLinus Torvalds calling_len = strlen(calling); 1321da177e4SLinus Torvalds 1331da177e4SLinus Torvalds *p++ = (calling_len << 4) | (called_len << 0); 1341da177e4SLinus Torvalds 1351da177e4SLinus Torvalds for (i = 0; i < (called_len + calling_len); i++) { 1361da177e4SLinus Torvalds if (i < called_len) { 1371da177e4SLinus Torvalds if (i % 2 != 0) { 1381da177e4SLinus Torvalds *p |= (*called++ - '0') << 0; 1391da177e4SLinus Torvalds p++; 1401da177e4SLinus Torvalds } else { 1411da177e4SLinus Torvalds *p = 0x00; 1421da177e4SLinus Torvalds *p |= (*called++ - '0') << 4; 1431da177e4SLinus Torvalds } 1441da177e4SLinus Torvalds } else { 1451da177e4SLinus Torvalds if (i % 2 != 0) { 1461da177e4SLinus Torvalds *p |= (*calling++ - '0') << 0; 1471da177e4SLinus Torvalds p++; 1481da177e4SLinus Torvalds } else { 1491da177e4SLinus Torvalds *p = 0x00; 1501da177e4SLinus Torvalds *p |= (*calling++ - '0') << 4; 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds } 1541da177e4SLinus Torvalds 1551da177e4SLinus Torvalds return 1 + (called_len + calling_len + 1) / 2; 1561da177e4SLinus Torvalds } 1571da177e4SLinus Torvalds 1581da177e4SLinus Torvalds /* 1591da177e4SLinus Torvalds * Socket removal during an interrupt is now safe. 1601da177e4SLinus Torvalds */ 1611da177e4SLinus Torvalds static void x25_remove_socket(struct sock *sk) 1621da177e4SLinus Torvalds { 1631da177e4SLinus Torvalds write_lock_bh(&x25_list_lock); 1641da177e4SLinus Torvalds sk_del_node_init(sk); 1651da177e4SLinus Torvalds write_unlock_bh(&x25_list_lock); 1661da177e4SLinus Torvalds } 1671da177e4SLinus Torvalds 1681da177e4SLinus Torvalds /* 1691da177e4SLinus Torvalds * Kill all bound sockets on a dropped device. 1701da177e4SLinus Torvalds */ 1711da177e4SLinus Torvalds static void x25_kill_by_device(struct net_device *dev) 1721da177e4SLinus Torvalds { 1731da177e4SLinus Torvalds struct sock *s; 1741da177e4SLinus Torvalds struct hlist_node *node; 1751da177e4SLinus Torvalds 1761da177e4SLinus Torvalds write_lock_bh(&x25_list_lock); 1771da177e4SLinus Torvalds 1781da177e4SLinus Torvalds sk_for_each(s, node, &x25_list) 1791da177e4SLinus Torvalds if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev) 1801da177e4SLinus Torvalds x25_disconnect(s, ENETUNREACH, 0, 0); 1811da177e4SLinus Torvalds 1821da177e4SLinus Torvalds write_unlock_bh(&x25_list_lock); 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds /* 1861da177e4SLinus Torvalds * Handle device status changes. 1871da177e4SLinus Torvalds */ 1881da177e4SLinus Torvalds static int x25_device_event(struct notifier_block *this, unsigned long event, 1891da177e4SLinus Torvalds void *ptr) 1901da177e4SLinus Torvalds { 1911da177e4SLinus Torvalds struct net_device *dev = ptr; 1921da177e4SLinus Torvalds struct x25_neigh *nb; 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds if (dev->type == ARPHRD_X25 1951da177e4SLinus Torvalds #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE) 1961da177e4SLinus Torvalds || dev->type == ARPHRD_ETHER 1971da177e4SLinus Torvalds #endif 1981da177e4SLinus Torvalds ) { 1991da177e4SLinus Torvalds switch (event) { 2001da177e4SLinus Torvalds case NETDEV_UP: 2011da177e4SLinus Torvalds x25_link_device_up(dev); 2021da177e4SLinus Torvalds break; 2031da177e4SLinus Torvalds case NETDEV_GOING_DOWN: 2041da177e4SLinus Torvalds nb = x25_get_neigh(dev); 2051da177e4SLinus Torvalds if (nb) { 2061da177e4SLinus Torvalds x25_terminate_link(nb); 2071da177e4SLinus Torvalds x25_neigh_put(nb); 2081da177e4SLinus Torvalds } 2091da177e4SLinus Torvalds break; 2101da177e4SLinus Torvalds case NETDEV_DOWN: 2111da177e4SLinus Torvalds x25_kill_by_device(dev); 2121da177e4SLinus Torvalds x25_route_device_down(dev); 2131da177e4SLinus Torvalds x25_link_device_down(dev); 2141da177e4SLinus Torvalds break; 2151da177e4SLinus Torvalds } 2161da177e4SLinus Torvalds } 2171da177e4SLinus Torvalds 2181da177e4SLinus Torvalds return NOTIFY_DONE; 2191da177e4SLinus Torvalds } 2201da177e4SLinus Torvalds 2211da177e4SLinus Torvalds /* 2221da177e4SLinus Torvalds * Add a socket to the bound sockets list. 2231da177e4SLinus Torvalds */ 2241da177e4SLinus Torvalds static void x25_insert_socket(struct sock *sk) 2251da177e4SLinus Torvalds { 2261da177e4SLinus Torvalds write_lock_bh(&x25_list_lock); 2271da177e4SLinus Torvalds sk_add_node(sk, &x25_list); 2281da177e4SLinus Torvalds write_unlock_bh(&x25_list_lock); 2291da177e4SLinus Torvalds } 2301da177e4SLinus Torvalds 2311da177e4SLinus Torvalds /* 2321da177e4SLinus Torvalds * Find a socket that wants to accept the Call Request we just 2331da177e4SLinus Torvalds * received. Check the full list for an address/cud match. 2341da177e4SLinus Torvalds * If no cuds match return the next_best thing, an address match. 2351da177e4SLinus Torvalds * Note: if a listening socket has cud set it must only get calls 2361da177e4SLinus Torvalds * with matching cud. 2371da177e4SLinus Torvalds */ 238cb65d506SShaun Pereira static struct sock *x25_find_listener(struct x25_address *addr, 239cb65d506SShaun Pereira struct sk_buff *skb) 2401da177e4SLinus Torvalds { 2411da177e4SLinus Torvalds struct sock *s; 2421da177e4SLinus Torvalds struct sock *next_best; 2431da177e4SLinus Torvalds struct hlist_node *node; 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds read_lock_bh(&x25_list_lock); 2461da177e4SLinus Torvalds next_best = NULL; 2471da177e4SLinus Torvalds 2481da177e4SLinus Torvalds sk_for_each(s, node, &x25_list) 2491da177e4SLinus Torvalds if ((!strcmp(addr->x25_addr, 2501da177e4SLinus Torvalds x25_sk(s)->source_addr.x25_addr) || 2511da177e4SLinus Torvalds !strcmp(addr->x25_addr, 2521da177e4SLinus Torvalds null_x25_address.x25_addr)) && 2531da177e4SLinus Torvalds s->sk_state == TCP_LISTEN) { 2541da177e4SLinus Torvalds /* 2551da177e4SLinus Torvalds * Found a listening socket, now check the incoming 2561da177e4SLinus Torvalds * call user data vs this sockets call user data 2571da177e4SLinus Torvalds */ 258cb65d506SShaun Pereira if(skb->len > 0 && x25_sk(s)->cudmatchlength > 0) { 259cb65d506SShaun Pereira if((memcmp(x25_sk(s)->calluserdata.cuddata, 260cb65d506SShaun Pereira skb->data, 261cb65d506SShaun Pereira x25_sk(s)->cudmatchlength)) == 0) { 2621da177e4SLinus Torvalds sock_hold(s); 2631da177e4SLinus Torvalds goto found; 2641da177e4SLinus Torvalds } 265cb65d506SShaun Pereira } else 2661da177e4SLinus Torvalds next_best = s; 2671da177e4SLinus Torvalds } 2681da177e4SLinus Torvalds if (next_best) { 2691da177e4SLinus Torvalds s = next_best; 2701da177e4SLinus Torvalds sock_hold(s); 2711da177e4SLinus Torvalds goto found; 2721da177e4SLinus Torvalds } 2731da177e4SLinus Torvalds s = NULL; 2741da177e4SLinus Torvalds found: 2751da177e4SLinus Torvalds read_unlock_bh(&x25_list_lock); 2761da177e4SLinus Torvalds return s; 2771da177e4SLinus Torvalds } 2781da177e4SLinus Torvalds 2791da177e4SLinus Torvalds /* 2801da177e4SLinus Torvalds * Find a connected X.25 socket given my LCI and neighbour. 2811da177e4SLinus Torvalds */ 2821da177e4SLinus Torvalds static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb) 2831da177e4SLinus Torvalds { 2841da177e4SLinus Torvalds struct sock *s; 2851da177e4SLinus Torvalds struct hlist_node *node; 2861da177e4SLinus Torvalds 2871da177e4SLinus Torvalds sk_for_each(s, node, &x25_list) 2881da177e4SLinus Torvalds if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) { 2891da177e4SLinus Torvalds sock_hold(s); 2901da177e4SLinus Torvalds goto found; 2911da177e4SLinus Torvalds } 2921da177e4SLinus Torvalds s = NULL; 2931da177e4SLinus Torvalds found: 2941da177e4SLinus Torvalds return s; 2951da177e4SLinus Torvalds } 2961da177e4SLinus Torvalds 2971da177e4SLinus Torvalds struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb) 2981da177e4SLinus Torvalds { 2991da177e4SLinus Torvalds struct sock *s; 3001da177e4SLinus Torvalds 3011da177e4SLinus Torvalds read_lock_bh(&x25_list_lock); 3021da177e4SLinus Torvalds s = __x25_find_socket(lci, nb); 3031da177e4SLinus Torvalds read_unlock_bh(&x25_list_lock); 3041da177e4SLinus Torvalds return s; 3051da177e4SLinus Torvalds } 3061da177e4SLinus Torvalds 3071da177e4SLinus Torvalds /* 3081da177e4SLinus Torvalds * Find a unique LCI for a given device. 3091da177e4SLinus Torvalds */ 3101da177e4SLinus Torvalds static unsigned int x25_new_lci(struct x25_neigh *nb) 3111da177e4SLinus Torvalds { 3121da177e4SLinus Torvalds unsigned int lci = 1; 3131da177e4SLinus Torvalds struct sock *sk; 3141da177e4SLinus Torvalds 3151da177e4SLinus Torvalds read_lock_bh(&x25_list_lock); 3161da177e4SLinus Torvalds 3171da177e4SLinus Torvalds while ((sk = __x25_find_socket(lci, nb)) != NULL) { 3181da177e4SLinus Torvalds sock_put(sk); 3191da177e4SLinus Torvalds if (++lci == 4096) { 3201da177e4SLinus Torvalds lci = 0; 3211da177e4SLinus Torvalds break; 3221da177e4SLinus Torvalds } 3231da177e4SLinus Torvalds } 3241da177e4SLinus Torvalds 3251da177e4SLinus Torvalds read_unlock_bh(&x25_list_lock); 3261da177e4SLinus Torvalds return lci; 3271da177e4SLinus Torvalds } 3281da177e4SLinus Torvalds 3291da177e4SLinus Torvalds /* 3301da177e4SLinus Torvalds * Deferred destroy. 3311da177e4SLinus Torvalds */ 3321da177e4SLinus Torvalds void x25_destroy_socket(struct sock *); 3331da177e4SLinus Torvalds 3341da177e4SLinus Torvalds /* 3351da177e4SLinus Torvalds * handler for deferred kills. 3361da177e4SLinus Torvalds */ 3371da177e4SLinus Torvalds static void x25_destroy_timer(unsigned long data) 3381da177e4SLinus Torvalds { 3391da177e4SLinus Torvalds x25_destroy_socket((struct sock *)data); 3401da177e4SLinus Torvalds } 3411da177e4SLinus Torvalds 3421da177e4SLinus Torvalds /* 3431da177e4SLinus Torvalds * This is called from user mode and the timers. Thus it protects itself 3441da177e4SLinus Torvalds * against interrupt users but doesn't worry about being called during 3451da177e4SLinus Torvalds * work. Once it is removed from the queue no interrupt or bottom half 3461da177e4SLinus Torvalds * will touch it and we are (fairly 8-) ) safe. 3471da177e4SLinus Torvalds * Not static as it's used by the timer 3481da177e4SLinus Torvalds */ 3491da177e4SLinus Torvalds void x25_destroy_socket(struct sock *sk) 3501da177e4SLinus Torvalds { 3511da177e4SLinus Torvalds struct sk_buff *skb; 3521da177e4SLinus Torvalds 3531da177e4SLinus Torvalds sock_hold(sk); 3541da177e4SLinus Torvalds lock_sock(sk); 3551da177e4SLinus Torvalds x25_stop_heartbeat(sk); 3561da177e4SLinus Torvalds x25_stop_timer(sk); 3571da177e4SLinus Torvalds 3581da177e4SLinus Torvalds x25_remove_socket(sk); 3591da177e4SLinus Torvalds x25_clear_queues(sk); /* Flush the queues */ 3601da177e4SLinus Torvalds 3611da177e4SLinus Torvalds while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) { 3621da177e4SLinus Torvalds if (skb->sk != sk) { /* A pending connection */ 3631da177e4SLinus Torvalds /* 3641da177e4SLinus Torvalds * Queue the unaccepted socket for death 3651da177e4SLinus Torvalds */ 3661da177e4SLinus Torvalds sock_set_flag(skb->sk, SOCK_DEAD); 3671da177e4SLinus Torvalds x25_start_heartbeat(skb->sk); 3681da177e4SLinus Torvalds x25_sk(skb->sk)->state = X25_STATE_0; 3691da177e4SLinus Torvalds } 3701da177e4SLinus Torvalds 3711da177e4SLinus Torvalds kfree_skb(skb); 3721da177e4SLinus Torvalds } 3731da177e4SLinus Torvalds 3741da177e4SLinus Torvalds if (atomic_read(&sk->sk_wmem_alloc) || 3751da177e4SLinus Torvalds atomic_read(&sk->sk_rmem_alloc)) { 3761da177e4SLinus Torvalds /* Defer: outstanding buffers */ 3771da177e4SLinus Torvalds sk->sk_timer.expires = jiffies + 10 * HZ; 3781da177e4SLinus Torvalds sk->sk_timer.function = x25_destroy_timer; 3791da177e4SLinus Torvalds sk->sk_timer.data = (unsigned long)sk; 3801da177e4SLinus Torvalds add_timer(&sk->sk_timer); 3811da177e4SLinus Torvalds } else { 3821da177e4SLinus Torvalds /* drop last reference so sock_put will free */ 3831da177e4SLinus Torvalds __sock_put(sk); 3841da177e4SLinus Torvalds } 3851da177e4SLinus Torvalds 3861da177e4SLinus Torvalds release_sock(sk); 3871da177e4SLinus Torvalds sock_put(sk); 3881da177e4SLinus Torvalds } 3891da177e4SLinus Torvalds 3901da177e4SLinus Torvalds /* 3911da177e4SLinus Torvalds * Handling for system calls applied via the various interfaces to a 3921da177e4SLinus Torvalds * X.25 socket object. 3931da177e4SLinus Torvalds */ 3941da177e4SLinus Torvalds 3951da177e4SLinus Torvalds static int x25_setsockopt(struct socket *sock, int level, int optname, 3961da177e4SLinus Torvalds char __user *optval, int optlen) 3971da177e4SLinus Torvalds { 3981da177e4SLinus Torvalds int opt; 3991da177e4SLinus Torvalds struct sock *sk = sock->sk; 4001da177e4SLinus Torvalds int rc = -ENOPROTOOPT; 4011da177e4SLinus Torvalds 4021da177e4SLinus Torvalds if (level != SOL_X25 || optname != X25_QBITINCL) 4031da177e4SLinus Torvalds goto out; 4041da177e4SLinus Torvalds 4051da177e4SLinus Torvalds rc = -EINVAL; 4061da177e4SLinus Torvalds if (optlen < sizeof(int)) 4071da177e4SLinus Torvalds goto out; 4081da177e4SLinus Torvalds 4091da177e4SLinus Torvalds rc = -EFAULT; 4101da177e4SLinus Torvalds if (get_user(opt, (int __user *)optval)) 4111da177e4SLinus Torvalds goto out; 4121da177e4SLinus Torvalds 4131da177e4SLinus Torvalds x25_sk(sk)->qbitincl = !!opt; 4141da177e4SLinus Torvalds rc = 0; 4151da177e4SLinus Torvalds out: 4161da177e4SLinus Torvalds return rc; 4171da177e4SLinus Torvalds } 4181da177e4SLinus Torvalds 4191da177e4SLinus Torvalds static int x25_getsockopt(struct socket *sock, int level, int optname, 4201da177e4SLinus Torvalds char __user *optval, int __user *optlen) 4211da177e4SLinus Torvalds { 4221da177e4SLinus Torvalds struct sock *sk = sock->sk; 4231da177e4SLinus Torvalds int val, len, rc = -ENOPROTOOPT; 4241da177e4SLinus Torvalds 4251da177e4SLinus Torvalds if (level != SOL_X25 || optname != X25_QBITINCL) 4261da177e4SLinus Torvalds goto out; 4271da177e4SLinus Torvalds 4281da177e4SLinus Torvalds rc = -EFAULT; 4291da177e4SLinus Torvalds if (get_user(len, optlen)) 4301da177e4SLinus Torvalds goto out; 4311da177e4SLinus Torvalds 4321da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(int)); 4331da177e4SLinus Torvalds 4341da177e4SLinus Torvalds rc = -EINVAL; 4351da177e4SLinus Torvalds if (len < 0) 4361da177e4SLinus Torvalds goto out; 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds rc = -EFAULT; 4391da177e4SLinus Torvalds if (put_user(len, optlen)) 4401da177e4SLinus Torvalds goto out; 4411da177e4SLinus Torvalds 4421da177e4SLinus Torvalds val = x25_sk(sk)->qbitincl; 4431da177e4SLinus Torvalds rc = copy_to_user(optval, &val, len) ? -EFAULT : 0; 4441da177e4SLinus Torvalds out: 4451da177e4SLinus Torvalds return rc; 4461da177e4SLinus Torvalds } 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds static int x25_listen(struct socket *sock, int backlog) 4491da177e4SLinus Torvalds { 4501da177e4SLinus Torvalds struct sock *sk = sock->sk; 4511da177e4SLinus Torvalds int rc = -EOPNOTSUPP; 4521da177e4SLinus Torvalds 4531da177e4SLinus Torvalds if (sk->sk_state != TCP_LISTEN) { 4541da177e4SLinus Torvalds memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN); 4551da177e4SLinus Torvalds sk->sk_max_ack_backlog = backlog; 4561da177e4SLinus Torvalds sk->sk_state = TCP_LISTEN; 4571da177e4SLinus Torvalds rc = 0; 4581da177e4SLinus Torvalds } 4591da177e4SLinus Torvalds 4601da177e4SLinus Torvalds return rc; 4611da177e4SLinus Torvalds } 4621da177e4SLinus Torvalds 4631da177e4SLinus Torvalds static struct proto x25_proto = { 4641da177e4SLinus Torvalds .name = "X25", 4651da177e4SLinus Torvalds .owner = THIS_MODULE, 4661da177e4SLinus Torvalds .obj_size = sizeof(struct x25_sock), 4671da177e4SLinus Torvalds }; 4681da177e4SLinus Torvalds 4691da177e4SLinus Torvalds static struct sock *x25_alloc_socket(void) 4701da177e4SLinus Torvalds { 4711da177e4SLinus Torvalds struct x25_sock *x25; 4721da177e4SLinus Torvalds struct sock *sk = sk_alloc(AF_X25, GFP_ATOMIC, &x25_proto, 1); 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds if (!sk) 4751da177e4SLinus Torvalds goto out; 4761da177e4SLinus Torvalds 4771da177e4SLinus Torvalds sock_init_data(NULL, sk); 4781da177e4SLinus Torvalds 4791da177e4SLinus Torvalds x25 = x25_sk(sk); 4801da177e4SLinus Torvalds skb_queue_head_init(&x25->ack_queue); 4811da177e4SLinus Torvalds skb_queue_head_init(&x25->fragment_queue); 4821da177e4SLinus Torvalds skb_queue_head_init(&x25->interrupt_in_queue); 4831da177e4SLinus Torvalds skb_queue_head_init(&x25->interrupt_out_queue); 4841da177e4SLinus Torvalds out: 4851da177e4SLinus Torvalds return sk; 4861da177e4SLinus Torvalds } 4871da177e4SLinus Torvalds 4881da177e4SLinus Torvalds void x25_init_timers(struct sock *sk); 4891da177e4SLinus Torvalds 4901da177e4SLinus Torvalds static int x25_create(struct socket *sock, int protocol) 4911da177e4SLinus Torvalds { 4921da177e4SLinus Torvalds struct sock *sk; 4931da177e4SLinus Torvalds struct x25_sock *x25; 4941da177e4SLinus Torvalds int rc = -ESOCKTNOSUPPORT; 4951da177e4SLinus Torvalds 4961da177e4SLinus Torvalds if (sock->type != SOCK_SEQPACKET || protocol) 4971da177e4SLinus Torvalds goto out; 4981da177e4SLinus Torvalds 4991da177e4SLinus Torvalds rc = -ENOMEM; 5001da177e4SLinus Torvalds if ((sk = x25_alloc_socket()) == NULL) 5011da177e4SLinus Torvalds goto out; 5021da177e4SLinus Torvalds 5031da177e4SLinus Torvalds x25 = x25_sk(sk); 5041da177e4SLinus Torvalds 5051da177e4SLinus Torvalds sock_init_data(sock, sk); 5061da177e4SLinus Torvalds 5071da177e4SLinus Torvalds x25_init_timers(sk); 5081da177e4SLinus Torvalds 5091da177e4SLinus Torvalds sock->ops = &x25_proto_ops; 5101da177e4SLinus Torvalds sk->sk_protocol = protocol; 5111da177e4SLinus Torvalds sk->sk_backlog_rcv = x25_backlog_rcv; 5121da177e4SLinus Torvalds 5131da177e4SLinus Torvalds x25->t21 = sysctl_x25_call_request_timeout; 5141da177e4SLinus Torvalds x25->t22 = sysctl_x25_reset_request_timeout; 5151da177e4SLinus Torvalds x25->t23 = sysctl_x25_clear_request_timeout; 5161da177e4SLinus Torvalds x25->t2 = sysctl_x25_ack_holdback_timeout; 5171da177e4SLinus Torvalds x25->state = X25_STATE_0; 518cb65d506SShaun Pereira x25->cudmatchlength = 0; 519ebc3f64bSShaun Pereira x25->accptapprv = X25_DENY_ACCPT_APPRV; /* normally no cud */ 520ebc3f64bSShaun Pereira /* on call accept */ 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE; 5231da177e4SLinus Torvalds x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE; 5241da177e4SLinus Torvalds x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE; 5251da177e4SLinus Torvalds x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE; 5261da177e4SLinus Torvalds x25->facilities.throughput = X25_DEFAULT_THROUGHPUT; 5271da177e4SLinus Torvalds x25->facilities.reverse = X25_DEFAULT_REVERSE; 528*a64b7b93SShaun Pereira x25->dte_facilities.calling_len = 0; 529*a64b7b93SShaun Pereira x25->dte_facilities.called_len = 0; 530*a64b7b93SShaun Pereira memset(x25->dte_facilities.called_ae, '\0', 531*a64b7b93SShaun Pereira sizeof(x25->dte_facilities.called_ae)); 532*a64b7b93SShaun Pereira memset(x25->dte_facilities.calling_ae, '\0', 533*a64b7b93SShaun Pereira sizeof(x25->dte_facilities.calling_ae)); 534*a64b7b93SShaun Pereira 5351da177e4SLinus Torvalds rc = 0; 5361da177e4SLinus Torvalds out: 5371da177e4SLinus Torvalds return rc; 5381da177e4SLinus Torvalds } 5391da177e4SLinus Torvalds 5401da177e4SLinus Torvalds static struct sock *x25_make_new(struct sock *osk) 5411da177e4SLinus Torvalds { 5421da177e4SLinus Torvalds struct sock *sk = NULL; 5431da177e4SLinus Torvalds struct x25_sock *x25, *ox25; 5441da177e4SLinus Torvalds 5451da177e4SLinus Torvalds if (osk->sk_type != SOCK_SEQPACKET) 5461da177e4SLinus Torvalds goto out; 5471da177e4SLinus Torvalds 5481da177e4SLinus Torvalds if ((sk = x25_alloc_socket()) == NULL) 5491da177e4SLinus Torvalds goto out; 5501da177e4SLinus Torvalds 5511da177e4SLinus Torvalds x25 = x25_sk(sk); 5521da177e4SLinus Torvalds 5531da177e4SLinus Torvalds sk->sk_type = osk->sk_type; 5541da177e4SLinus Torvalds sk->sk_socket = osk->sk_socket; 5551da177e4SLinus Torvalds sk->sk_priority = osk->sk_priority; 5561da177e4SLinus Torvalds sk->sk_protocol = osk->sk_protocol; 5571da177e4SLinus Torvalds sk->sk_rcvbuf = osk->sk_rcvbuf; 5581da177e4SLinus Torvalds sk->sk_sndbuf = osk->sk_sndbuf; 5591da177e4SLinus Torvalds sk->sk_state = TCP_ESTABLISHED; 5601da177e4SLinus Torvalds sk->sk_sleep = osk->sk_sleep; 5611da177e4SLinus Torvalds sk->sk_backlog_rcv = osk->sk_backlog_rcv; 562a20a8554SShaun Pereira sock_copy_flags(sk, osk); 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds ox25 = x25_sk(osk); 5651da177e4SLinus Torvalds x25->t21 = ox25->t21; 5661da177e4SLinus Torvalds x25->t22 = ox25->t22; 5671da177e4SLinus Torvalds x25->t23 = ox25->t23; 5681da177e4SLinus Torvalds x25->t2 = ox25->t2; 5691da177e4SLinus Torvalds x25->facilities = ox25->facilities; 5701da177e4SLinus Torvalds x25->qbitincl = ox25->qbitincl; 571*a64b7b93SShaun Pereira x25->dte_facilities = ox25->dte_facilities; 572cb65d506SShaun Pereira x25->cudmatchlength = ox25->cudmatchlength; 573ebc3f64bSShaun Pereira x25->accptapprv = ox25->accptapprv; 5741da177e4SLinus Torvalds 5751da177e4SLinus Torvalds x25_init_timers(sk); 5761da177e4SLinus Torvalds out: 5771da177e4SLinus Torvalds return sk; 5781da177e4SLinus Torvalds } 5791da177e4SLinus Torvalds 5801da177e4SLinus Torvalds static int x25_release(struct socket *sock) 5811da177e4SLinus Torvalds { 5821da177e4SLinus Torvalds struct sock *sk = sock->sk; 5831da177e4SLinus Torvalds struct x25_sock *x25; 5841da177e4SLinus Torvalds 5851da177e4SLinus Torvalds if (!sk) 5861da177e4SLinus Torvalds goto out; 5871da177e4SLinus Torvalds 5881da177e4SLinus Torvalds x25 = x25_sk(sk); 5891da177e4SLinus Torvalds 5901da177e4SLinus Torvalds switch (x25->state) { 5911da177e4SLinus Torvalds 5921da177e4SLinus Torvalds case X25_STATE_0: 5931da177e4SLinus Torvalds case X25_STATE_2: 5941da177e4SLinus Torvalds x25_disconnect(sk, 0, 0, 0); 5951da177e4SLinus Torvalds x25_destroy_socket(sk); 5961da177e4SLinus Torvalds goto out; 5971da177e4SLinus Torvalds 5981da177e4SLinus Torvalds case X25_STATE_1: 5991da177e4SLinus Torvalds case X25_STATE_3: 6001da177e4SLinus Torvalds case X25_STATE_4: 6011da177e4SLinus Torvalds x25_clear_queues(sk); 6021da177e4SLinus Torvalds x25_write_internal(sk, X25_CLEAR_REQUEST); 6031da177e4SLinus Torvalds x25_start_t23timer(sk); 6041da177e4SLinus Torvalds x25->state = X25_STATE_2; 6051da177e4SLinus Torvalds sk->sk_state = TCP_CLOSE; 6061da177e4SLinus Torvalds sk->sk_shutdown |= SEND_SHUTDOWN; 6071da177e4SLinus Torvalds sk->sk_state_change(sk); 6081da177e4SLinus Torvalds sock_set_flag(sk, SOCK_DEAD); 6091da177e4SLinus Torvalds sock_set_flag(sk, SOCK_DESTROY); 6101da177e4SLinus Torvalds break; 6111da177e4SLinus Torvalds } 6121da177e4SLinus Torvalds 6131da177e4SLinus Torvalds sock->sk = NULL; 6141da177e4SLinus Torvalds sk->sk_socket = NULL; /* Not used, but we should do this */ 6151da177e4SLinus Torvalds out: 6161da177e4SLinus Torvalds return 0; 6171da177e4SLinus Torvalds } 6181da177e4SLinus Torvalds 6191da177e4SLinus Torvalds static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 6201da177e4SLinus Torvalds { 6211da177e4SLinus Torvalds struct sock *sk = sock->sk; 6221da177e4SLinus Torvalds struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr; 6231da177e4SLinus Torvalds 6241da177e4SLinus Torvalds if (!sock_flag(sk, SOCK_ZAPPED) || 6251da177e4SLinus Torvalds addr_len != sizeof(struct sockaddr_x25) || 6261da177e4SLinus Torvalds addr->sx25_family != AF_X25) 6271da177e4SLinus Torvalds return -EINVAL; 6281da177e4SLinus Torvalds 6291da177e4SLinus Torvalds x25_sk(sk)->source_addr = addr->sx25_addr; 6301da177e4SLinus Torvalds x25_insert_socket(sk); 6311da177e4SLinus Torvalds sock_reset_flag(sk, SOCK_ZAPPED); 6321da177e4SLinus Torvalds SOCK_DEBUG(sk, "x25_bind: socket is bound\n"); 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds return 0; 6351da177e4SLinus Torvalds } 6361da177e4SLinus Torvalds 6371da177e4SLinus Torvalds static int x25_wait_for_connection_establishment(struct sock *sk) 6381da177e4SLinus Torvalds { 6391da177e4SLinus Torvalds DECLARE_WAITQUEUE(wait, current); 6401da177e4SLinus Torvalds int rc; 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds add_wait_queue_exclusive(sk->sk_sleep, &wait); 6431da177e4SLinus Torvalds for (;;) { 6441da177e4SLinus Torvalds __set_current_state(TASK_INTERRUPTIBLE); 6451da177e4SLinus Torvalds rc = -ERESTARTSYS; 6461da177e4SLinus Torvalds if (signal_pending(current)) 6471da177e4SLinus Torvalds break; 6481da177e4SLinus Torvalds rc = sock_error(sk); 6491da177e4SLinus Torvalds if (rc) { 6501da177e4SLinus Torvalds sk->sk_socket->state = SS_UNCONNECTED; 6511da177e4SLinus Torvalds break; 6521da177e4SLinus Torvalds } 6531da177e4SLinus Torvalds rc = 0; 6541da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED) { 6551da177e4SLinus Torvalds release_sock(sk); 6561da177e4SLinus Torvalds schedule(); 6571da177e4SLinus Torvalds lock_sock(sk); 6581da177e4SLinus Torvalds } else 6591da177e4SLinus Torvalds break; 6601da177e4SLinus Torvalds } 6611da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 6621da177e4SLinus Torvalds remove_wait_queue(sk->sk_sleep, &wait); 6631da177e4SLinus Torvalds return rc; 6641da177e4SLinus Torvalds } 6651da177e4SLinus Torvalds 6661da177e4SLinus Torvalds static int x25_connect(struct socket *sock, struct sockaddr *uaddr, 6671da177e4SLinus Torvalds int addr_len, int flags) 6681da177e4SLinus Torvalds { 6691da177e4SLinus Torvalds struct sock *sk = sock->sk; 6701da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk); 6711da177e4SLinus Torvalds struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr; 6721da177e4SLinus Torvalds struct x25_route *rt; 6731da177e4SLinus Torvalds int rc = 0; 6741da177e4SLinus Torvalds 6751da177e4SLinus Torvalds lock_sock(sk); 6761da177e4SLinus Torvalds if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) { 6771da177e4SLinus Torvalds sock->state = SS_CONNECTED; 6781da177e4SLinus Torvalds goto out; /* Connect completed during a ERESTARTSYS event */ 6791da177e4SLinus Torvalds } 6801da177e4SLinus Torvalds 6811da177e4SLinus Torvalds rc = -ECONNREFUSED; 6821da177e4SLinus Torvalds if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) { 6831da177e4SLinus Torvalds sock->state = SS_UNCONNECTED; 6841da177e4SLinus Torvalds goto out; 6851da177e4SLinus Torvalds } 6861da177e4SLinus Torvalds 6871da177e4SLinus Torvalds rc = -EISCONN; /* No reconnect on a seqpacket socket */ 6881da177e4SLinus Torvalds if (sk->sk_state == TCP_ESTABLISHED) 6891da177e4SLinus Torvalds goto out; 6901da177e4SLinus Torvalds 6911da177e4SLinus Torvalds sk->sk_state = TCP_CLOSE; 6921da177e4SLinus Torvalds sock->state = SS_UNCONNECTED; 6931da177e4SLinus Torvalds 6941da177e4SLinus Torvalds rc = -EINVAL; 6951da177e4SLinus Torvalds if (addr_len != sizeof(struct sockaddr_x25) || 6961da177e4SLinus Torvalds addr->sx25_family != AF_X25) 6971da177e4SLinus Torvalds goto out; 6981da177e4SLinus Torvalds 6991da177e4SLinus Torvalds rc = -ENETUNREACH; 7001da177e4SLinus Torvalds rt = x25_get_route(&addr->sx25_addr); 7011da177e4SLinus Torvalds if (!rt) 7021da177e4SLinus Torvalds goto out; 7031da177e4SLinus Torvalds 7041da177e4SLinus Torvalds x25->neighbour = x25_get_neigh(rt->dev); 7051da177e4SLinus Torvalds if (!x25->neighbour) 7061da177e4SLinus Torvalds goto out_put_route; 7071da177e4SLinus Torvalds 7081da177e4SLinus Torvalds x25_limit_facilities(&x25->facilities, x25->neighbour); 7091da177e4SLinus Torvalds 7101da177e4SLinus Torvalds x25->lci = x25_new_lci(x25->neighbour); 7111da177e4SLinus Torvalds if (!x25->lci) 7121da177e4SLinus Torvalds goto out_put_neigh; 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds rc = -EINVAL; 7151da177e4SLinus Torvalds if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */ 7161da177e4SLinus Torvalds goto out_put_neigh; 7171da177e4SLinus Torvalds 7181da177e4SLinus Torvalds if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr)) 7191da177e4SLinus Torvalds memset(&x25->source_addr, '\0', X25_ADDR_LEN); 7201da177e4SLinus Torvalds 7211da177e4SLinus Torvalds x25->dest_addr = addr->sx25_addr; 7221da177e4SLinus Torvalds 7231da177e4SLinus Torvalds /* Move to connecting socket, start sending Connect Requests */ 7241da177e4SLinus Torvalds sock->state = SS_CONNECTING; 7251da177e4SLinus Torvalds sk->sk_state = TCP_SYN_SENT; 7261da177e4SLinus Torvalds 7271da177e4SLinus Torvalds x25->state = X25_STATE_1; 7281da177e4SLinus Torvalds 7291da177e4SLinus Torvalds x25_write_internal(sk, X25_CALL_REQUEST); 7301da177e4SLinus Torvalds 7311da177e4SLinus Torvalds x25_start_heartbeat(sk); 7321da177e4SLinus Torvalds x25_start_t21timer(sk); 7331da177e4SLinus Torvalds 7341da177e4SLinus Torvalds /* Now the loop */ 7351da177e4SLinus Torvalds rc = -EINPROGRESS; 7361da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) 7371da177e4SLinus Torvalds goto out_put_neigh; 7381da177e4SLinus Torvalds 7391da177e4SLinus Torvalds rc = x25_wait_for_connection_establishment(sk); 7401da177e4SLinus Torvalds if (rc) 7411da177e4SLinus Torvalds goto out_put_neigh; 7421da177e4SLinus Torvalds 7431da177e4SLinus Torvalds sock->state = SS_CONNECTED; 7441da177e4SLinus Torvalds rc = 0; 7451da177e4SLinus Torvalds out_put_neigh: 7461da177e4SLinus Torvalds if (rc) 7471da177e4SLinus Torvalds x25_neigh_put(x25->neighbour); 7481da177e4SLinus Torvalds out_put_route: 7491da177e4SLinus Torvalds x25_route_put(rt); 7501da177e4SLinus Torvalds out: 7511da177e4SLinus Torvalds release_sock(sk); 7521da177e4SLinus Torvalds return rc; 7531da177e4SLinus Torvalds } 7541da177e4SLinus Torvalds 755bac37ec8SShaun Pereira static int x25_wait_for_data(struct sock *sk, long timeout) 7561da177e4SLinus Torvalds { 7571da177e4SLinus Torvalds DECLARE_WAITQUEUE(wait, current); 7581da177e4SLinus Torvalds int rc = 0; 7591da177e4SLinus Torvalds 7601da177e4SLinus Torvalds add_wait_queue_exclusive(sk->sk_sleep, &wait); 7611da177e4SLinus Torvalds for (;;) { 7621da177e4SLinus Torvalds __set_current_state(TASK_INTERRUPTIBLE); 7631da177e4SLinus Torvalds if (sk->sk_shutdown & RCV_SHUTDOWN) 7641da177e4SLinus Torvalds break; 7651da177e4SLinus Torvalds rc = -ERESTARTSYS; 7661da177e4SLinus Torvalds if (signal_pending(current)) 7671da177e4SLinus Torvalds break; 7681da177e4SLinus Torvalds rc = -EAGAIN; 7691da177e4SLinus Torvalds if (!timeout) 7701da177e4SLinus Torvalds break; 7711da177e4SLinus Torvalds rc = 0; 7721da177e4SLinus Torvalds if (skb_queue_empty(&sk->sk_receive_queue)) { 7731da177e4SLinus Torvalds release_sock(sk); 7741da177e4SLinus Torvalds timeout = schedule_timeout(timeout); 7751da177e4SLinus Torvalds lock_sock(sk); 7761da177e4SLinus Torvalds } else 7771da177e4SLinus Torvalds break; 7781da177e4SLinus Torvalds } 7791da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 7801da177e4SLinus Torvalds remove_wait_queue(sk->sk_sleep, &wait); 7811da177e4SLinus Torvalds return rc; 7821da177e4SLinus Torvalds } 7831da177e4SLinus Torvalds 7841da177e4SLinus Torvalds static int x25_accept(struct socket *sock, struct socket *newsock, int flags) 7851da177e4SLinus Torvalds { 7861da177e4SLinus Torvalds struct sock *sk = sock->sk; 7871da177e4SLinus Torvalds struct sock *newsk; 7881da177e4SLinus Torvalds struct sk_buff *skb; 7891da177e4SLinus Torvalds int rc = -EINVAL; 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds if (!sk || sk->sk_state != TCP_LISTEN) 7921da177e4SLinus Torvalds goto out; 7931da177e4SLinus Torvalds 7941da177e4SLinus Torvalds rc = -EOPNOTSUPP; 7951da177e4SLinus Torvalds if (sk->sk_type != SOCK_SEQPACKET) 7961da177e4SLinus Torvalds goto out; 7971da177e4SLinus Torvalds 7981da177e4SLinus Torvalds lock_sock(sk); 7991da177e4SLinus Torvalds rc = x25_wait_for_data(sk, sk->sk_rcvtimeo); 8001da177e4SLinus Torvalds if (rc) 8011da177e4SLinus Torvalds goto out2; 8021da177e4SLinus Torvalds skb = skb_dequeue(&sk->sk_receive_queue); 8031da177e4SLinus Torvalds rc = -EINVAL; 8041da177e4SLinus Torvalds if (!skb->sk) 8051da177e4SLinus Torvalds goto out2; 8061da177e4SLinus Torvalds newsk = skb->sk; 8071da177e4SLinus Torvalds newsk->sk_socket = newsock; 8081da177e4SLinus Torvalds newsk->sk_sleep = &newsock->wait; 8091da177e4SLinus Torvalds 8101da177e4SLinus Torvalds /* Now attach up the new socket */ 8111da177e4SLinus Torvalds skb->sk = NULL; 8121da177e4SLinus Torvalds kfree_skb(skb); 8131da177e4SLinus Torvalds sk->sk_ack_backlog--; 8141da177e4SLinus Torvalds newsock->sk = newsk; 8151da177e4SLinus Torvalds newsock->state = SS_CONNECTED; 8161da177e4SLinus Torvalds rc = 0; 8171da177e4SLinus Torvalds out2: 8181da177e4SLinus Torvalds release_sock(sk); 8191da177e4SLinus Torvalds out: 8201da177e4SLinus Torvalds return rc; 8211da177e4SLinus Torvalds } 8221da177e4SLinus Torvalds 8231da177e4SLinus Torvalds static int x25_getname(struct socket *sock, struct sockaddr *uaddr, 8241da177e4SLinus Torvalds int *uaddr_len, int peer) 8251da177e4SLinus Torvalds { 8261da177e4SLinus Torvalds struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr; 8271da177e4SLinus Torvalds struct sock *sk = sock->sk; 8281da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk); 8291da177e4SLinus Torvalds 8301da177e4SLinus Torvalds if (peer) { 8311da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED) 8321da177e4SLinus Torvalds return -ENOTCONN; 8331da177e4SLinus Torvalds sx25->sx25_addr = x25->dest_addr; 8341da177e4SLinus Torvalds } else 8351da177e4SLinus Torvalds sx25->sx25_addr = x25->source_addr; 8361da177e4SLinus Torvalds 8371da177e4SLinus Torvalds sx25->sx25_family = AF_X25; 8381da177e4SLinus Torvalds *uaddr_len = sizeof(*sx25); 8391da177e4SLinus Torvalds 8401da177e4SLinus Torvalds return 0; 8411da177e4SLinus Torvalds } 8421da177e4SLinus Torvalds 8431da177e4SLinus Torvalds int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb, 8441da177e4SLinus Torvalds unsigned int lci) 8451da177e4SLinus Torvalds { 8461da177e4SLinus Torvalds struct sock *sk; 8471da177e4SLinus Torvalds struct sock *make; 8481da177e4SLinus Torvalds struct x25_sock *makex25; 8491da177e4SLinus Torvalds struct x25_address source_addr, dest_addr; 8501da177e4SLinus Torvalds struct x25_facilities facilities; 851*a64b7b93SShaun Pereira struct x25_dte_facilities dte_facilities; 8521da177e4SLinus Torvalds int len, rc; 8531da177e4SLinus Torvalds 8541da177e4SLinus Torvalds /* 8551da177e4SLinus Torvalds * Remove the LCI and frame type. 8561da177e4SLinus Torvalds */ 8571da177e4SLinus Torvalds skb_pull(skb, X25_STD_MIN_LEN); 8581da177e4SLinus Torvalds 8591da177e4SLinus Torvalds /* 8601da177e4SLinus Torvalds * Extract the X.25 addresses and convert them to ASCII strings, 8611da177e4SLinus Torvalds * and remove them. 8621da177e4SLinus Torvalds */ 8631da177e4SLinus Torvalds skb_pull(skb, x25_addr_ntoa(skb->data, &source_addr, &dest_addr)); 8641da177e4SLinus Torvalds 8651da177e4SLinus Torvalds /* 8661da177e4SLinus Torvalds * Get the length of the facilities, skip past them for the moment 8671da177e4SLinus Torvalds * get the call user data because this is needed to determine 8681da177e4SLinus Torvalds * the correct listener 8691da177e4SLinus Torvalds */ 8701da177e4SLinus Torvalds len = skb->data[0] + 1; 8711da177e4SLinus Torvalds skb_pull(skb,len); 8721da177e4SLinus Torvalds 8731da177e4SLinus Torvalds /* 8741da177e4SLinus Torvalds * Find a listener for the particular address/cud pair. 8751da177e4SLinus Torvalds */ 876cb65d506SShaun Pereira sk = x25_find_listener(&source_addr,skb); 877cb65d506SShaun Pereira skb_push(skb,len); 8781da177e4SLinus Torvalds 8791da177e4SLinus Torvalds /* 8801da177e4SLinus Torvalds * We can't accept the Call Request. 8811da177e4SLinus Torvalds */ 8821da177e4SLinus Torvalds if (sk == NULL || sk_acceptq_is_full(sk)) 8831da177e4SLinus Torvalds goto out_clear_request; 8841da177e4SLinus Torvalds 8851da177e4SLinus Torvalds /* 8861da177e4SLinus Torvalds * Try to reach a compromise on the requested facilities. 8871da177e4SLinus Torvalds */ 888*a64b7b93SShaun Pereira len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities); 889*a64b7b93SShaun Pereira if (len == -1) 8901da177e4SLinus Torvalds goto out_sock_put; 8911da177e4SLinus Torvalds 8921da177e4SLinus Torvalds /* 8931da177e4SLinus Torvalds * current neighbour/link might impose additional limits 8941da177e4SLinus Torvalds * on certain facilties 8951da177e4SLinus Torvalds */ 8961da177e4SLinus Torvalds 8971da177e4SLinus Torvalds x25_limit_facilities(&facilities, nb); 8981da177e4SLinus Torvalds 8991da177e4SLinus Torvalds /* 9001da177e4SLinus Torvalds * Try to create a new socket. 9011da177e4SLinus Torvalds */ 9021da177e4SLinus Torvalds make = x25_make_new(sk); 9031da177e4SLinus Torvalds if (!make) 9041da177e4SLinus Torvalds goto out_sock_put; 9051da177e4SLinus Torvalds 9061da177e4SLinus Torvalds /* 9071da177e4SLinus Torvalds * Remove the facilities 9081da177e4SLinus Torvalds */ 9091da177e4SLinus Torvalds skb_pull(skb, len); 9101da177e4SLinus Torvalds 9111da177e4SLinus Torvalds skb->sk = make; 9121da177e4SLinus Torvalds make->sk_state = TCP_ESTABLISHED; 9131da177e4SLinus Torvalds 9141da177e4SLinus Torvalds makex25 = x25_sk(make); 9151da177e4SLinus Torvalds makex25->lci = lci; 9161da177e4SLinus Torvalds makex25->dest_addr = dest_addr; 9171da177e4SLinus Torvalds makex25->source_addr = source_addr; 9181da177e4SLinus Torvalds makex25->neighbour = nb; 9191da177e4SLinus Torvalds makex25->facilities = facilities; 920*a64b7b93SShaun Pereira makex25->dte_facilities= dte_facilities; 9211da177e4SLinus Torvalds makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask; 922cb65d506SShaun Pereira /* ensure no reverse facil on accept */ 923cb65d506SShaun Pereira makex25->vc_facil_mask &= ~X25_MASK_REVERSE; 924*a64b7b93SShaun Pereira /* ensure no calling address extension on accept */ 925*a64b7b93SShaun Pereira makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE; 926cb65d506SShaun Pereira makex25->cudmatchlength = x25_sk(sk)->cudmatchlength; 9271da177e4SLinus Torvalds 928ebc3f64bSShaun Pereira /* Normally all calls are accepted immediatly */ 929ebc3f64bSShaun Pereira if(makex25->accptapprv & X25_DENY_ACCPT_APPRV) { 9301da177e4SLinus Torvalds x25_write_internal(make, X25_CALL_ACCEPTED); 9311da177e4SLinus Torvalds makex25->state = X25_STATE_3; 932ebc3f64bSShaun Pereira } 9331da177e4SLinus Torvalds 934cb65d506SShaun Pereira /* 935cb65d506SShaun Pereira * Incoming Call User Data. 936cb65d506SShaun Pereira */ 937cb65d506SShaun Pereira if (skb->len >= 0) { 938cb65d506SShaun Pereira memcpy(makex25->calluserdata.cuddata, skb->data, skb->len); 939cb65d506SShaun Pereira makex25->calluserdata.cudlength = skb->len; 940cb65d506SShaun Pereira } 941cb65d506SShaun Pereira 9421da177e4SLinus Torvalds sk->sk_ack_backlog++; 9431da177e4SLinus Torvalds 9441da177e4SLinus Torvalds x25_insert_socket(make); 9451da177e4SLinus Torvalds 9461da177e4SLinus Torvalds skb_queue_head(&sk->sk_receive_queue, skb); 9471da177e4SLinus Torvalds 9481da177e4SLinus Torvalds x25_start_heartbeat(make); 9491da177e4SLinus Torvalds 9501da177e4SLinus Torvalds if (!sock_flag(sk, SOCK_DEAD)) 9511da177e4SLinus Torvalds sk->sk_data_ready(sk, skb->len); 9521da177e4SLinus Torvalds rc = 1; 9531da177e4SLinus Torvalds sock_put(sk); 9541da177e4SLinus Torvalds out: 9551da177e4SLinus Torvalds return rc; 9561da177e4SLinus Torvalds out_sock_put: 9571da177e4SLinus Torvalds sock_put(sk); 9581da177e4SLinus Torvalds out_clear_request: 9591da177e4SLinus Torvalds rc = 0; 9601da177e4SLinus Torvalds x25_transmit_clear_request(nb, lci, 0x01); 9611da177e4SLinus Torvalds goto out; 9621da177e4SLinus Torvalds } 9631da177e4SLinus Torvalds 9641da177e4SLinus Torvalds static int x25_sendmsg(struct kiocb *iocb, struct socket *sock, 9651da177e4SLinus Torvalds struct msghdr *msg, size_t len) 9661da177e4SLinus Torvalds { 9671da177e4SLinus Torvalds struct sock *sk = sock->sk; 9681da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk); 9691da177e4SLinus Torvalds struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name; 9701da177e4SLinus Torvalds struct sockaddr_x25 sx25; 9711da177e4SLinus Torvalds struct sk_buff *skb; 9721da177e4SLinus Torvalds unsigned char *asmptr; 9731da177e4SLinus Torvalds int noblock = msg->msg_flags & MSG_DONTWAIT; 9741da177e4SLinus Torvalds size_t size; 9751da177e4SLinus Torvalds int qbit = 0, rc = -EINVAL; 9761da177e4SLinus Torvalds 9771da177e4SLinus Torvalds if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT)) 9781da177e4SLinus Torvalds goto out; 9791da177e4SLinus Torvalds 9801da177e4SLinus Torvalds /* we currently don't support segmented records at the user interface */ 9811da177e4SLinus Torvalds if (!(msg->msg_flags & (MSG_EOR|MSG_OOB))) 9821da177e4SLinus Torvalds goto out; 9831da177e4SLinus Torvalds 9841da177e4SLinus Torvalds rc = -EADDRNOTAVAIL; 9851da177e4SLinus Torvalds if (sock_flag(sk, SOCK_ZAPPED)) 9861da177e4SLinus Torvalds goto out; 9871da177e4SLinus Torvalds 9881da177e4SLinus Torvalds rc = -EPIPE; 9891da177e4SLinus Torvalds if (sk->sk_shutdown & SEND_SHUTDOWN) { 9901da177e4SLinus Torvalds send_sig(SIGPIPE, current, 0); 9911da177e4SLinus Torvalds goto out; 9921da177e4SLinus Torvalds } 9931da177e4SLinus Torvalds 9941da177e4SLinus Torvalds rc = -ENETUNREACH; 9951da177e4SLinus Torvalds if (!x25->neighbour) 9961da177e4SLinus Torvalds goto out; 9971da177e4SLinus Torvalds 9981da177e4SLinus Torvalds if (usx25) { 9991da177e4SLinus Torvalds rc = -EINVAL; 10001da177e4SLinus Torvalds if (msg->msg_namelen < sizeof(sx25)) 10011da177e4SLinus Torvalds goto out; 10021da177e4SLinus Torvalds memcpy(&sx25, usx25, sizeof(sx25)); 10031da177e4SLinus Torvalds rc = -EISCONN; 10041da177e4SLinus Torvalds if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr)) 10051da177e4SLinus Torvalds goto out; 10061da177e4SLinus Torvalds rc = -EINVAL; 10071da177e4SLinus Torvalds if (sx25.sx25_family != AF_X25) 10081da177e4SLinus Torvalds goto out; 10091da177e4SLinus Torvalds } else { 10101da177e4SLinus Torvalds /* 10111da177e4SLinus Torvalds * FIXME 1003.1g - if the socket is like this because 10121da177e4SLinus Torvalds * it has become closed (not started closed) we ought 10131da177e4SLinus Torvalds * to SIGPIPE, EPIPE; 10141da177e4SLinus Torvalds */ 10151da177e4SLinus Torvalds rc = -ENOTCONN; 10161da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED) 10171da177e4SLinus Torvalds goto out; 10181da177e4SLinus Torvalds 10191da177e4SLinus Torvalds sx25.sx25_family = AF_X25; 10201da177e4SLinus Torvalds sx25.sx25_addr = x25->dest_addr; 10211da177e4SLinus Torvalds } 10221da177e4SLinus Torvalds 10231da177e4SLinus Torvalds SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n"); 10241da177e4SLinus Torvalds 10251da177e4SLinus Torvalds /* Build a packet */ 10261da177e4SLinus Torvalds SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n"); 10271da177e4SLinus Torvalds 10281da177e4SLinus Torvalds if ((msg->msg_flags & MSG_OOB) && len > 32) 10291da177e4SLinus Torvalds len = 32; 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN; 10321da177e4SLinus Torvalds 10331da177e4SLinus Torvalds skb = sock_alloc_send_skb(sk, size, noblock, &rc); 10341da177e4SLinus Torvalds if (!skb) 10351da177e4SLinus Torvalds goto out; 10361da177e4SLinus Torvalds X25_SKB_CB(skb)->flags = msg->msg_flags; 10371da177e4SLinus Torvalds 10381da177e4SLinus Torvalds skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN); 10391da177e4SLinus Torvalds 10401da177e4SLinus Torvalds /* 10411da177e4SLinus Torvalds * Put the data on the end 10421da177e4SLinus Torvalds */ 10431da177e4SLinus Torvalds SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n"); 10441da177e4SLinus Torvalds 10451da177e4SLinus Torvalds asmptr = skb->h.raw = skb_put(skb, len); 10461da177e4SLinus Torvalds 10471da177e4SLinus Torvalds rc = memcpy_fromiovec(asmptr, msg->msg_iov, len); 10481da177e4SLinus Torvalds if (rc) 10491da177e4SLinus Torvalds goto out_kfree_skb; 10501da177e4SLinus Torvalds 10511da177e4SLinus Torvalds /* 10521da177e4SLinus Torvalds * If the Q BIT Include socket option is in force, the first 10531da177e4SLinus Torvalds * byte of the user data is the logical value of the Q Bit. 10541da177e4SLinus Torvalds */ 10551da177e4SLinus Torvalds if (x25->qbitincl) { 10561da177e4SLinus Torvalds qbit = skb->data[0]; 10571da177e4SLinus Torvalds skb_pull(skb, 1); 10581da177e4SLinus Torvalds } 10591da177e4SLinus Torvalds 10601da177e4SLinus Torvalds /* 10611da177e4SLinus Torvalds * Push down the X.25 header 10621da177e4SLinus Torvalds */ 10631da177e4SLinus Torvalds SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n"); 10641da177e4SLinus Torvalds 10651da177e4SLinus Torvalds if (msg->msg_flags & MSG_OOB) { 10661da177e4SLinus Torvalds if (x25->neighbour->extended) { 10671da177e4SLinus Torvalds asmptr = skb_push(skb, X25_STD_MIN_LEN); 10681da177e4SLinus Torvalds *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ; 10691da177e4SLinus Torvalds *asmptr++ = (x25->lci >> 0) & 0xFF; 10701da177e4SLinus Torvalds *asmptr++ = X25_INTERRUPT; 10711da177e4SLinus Torvalds } else { 10721da177e4SLinus Torvalds asmptr = skb_push(skb, X25_STD_MIN_LEN); 10731da177e4SLinus Torvalds *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ; 10741da177e4SLinus Torvalds *asmptr++ = (x25->lci >> 0) & 0xFF; 10751da177e4SLinus Torvalds *asmptr++ = X25_INTERRUPT; 10761da177e4SLinus Torvalds } 10771da177e4SLinus Torvalds } else { 10781da177e4SLinus Torvalds if (x25->neighbour->extended) { 10791da177e4SLinus Torvalds /* Build an Extended X.25 header */ 10801da177e4SLinus Torvalds asmptr = skb_push(skb, X25_EXT_MIN_LEN); 10811da177e4SLinus Torvalds *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ; 10821da177e4SLinus Torvalds *asmptr++ = (x25->lci >> 0) & 0xFF; 10831da177e4SLinus Torvalds *asmptr++ = X25_DATA; 10841da177e4SLinus Torvalds *asmptr++ = X25_DATA; 10851da177e4SLinus Torvalds } else { 10861da177e4SLinus Torvalds /* Build an Standard X.25 header */ 10871da177e4SLinus Torvalds asmptr = skb_push(skb, X25_STD_MIN_LEN); 10881da177e4SLinus Torvalds *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ; 10891da177e4SLinus Torvalds *asmptr++ = (x25->lci >> 0) & 0xFF; 10901da177e4SLinus Torvalds *asmptr++ = X25_DATA; 10911da177e4SLinus Torvalds } 10921da177e4SLinus Torvalds 10931da177e4SLinus Torvalds if (qbit) 10941da177e4SLinus Torvalds skb->data[0] |= X25_Q_BIT; 10951da177e4SLinus Torvalds } 10961da177e4SLinus Torvalds 10971da177e4SLinus Torvalds SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n"); 10981da177e4SLinus Torvalds SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n"); 10991da177e4SLinus Torvalds 11001da177e4SLinus Torvalds rc = -ENOTCONN; 11011da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED) 11021da177e4SLinus Torvalds goto out_kfree_skb; 11031da177e4SLinus Torvalds 11041da177e4SLinus Torvalds if (msg->msg_flags & MSG_OOB) 11051da177e4SLinus Torvalds skb_queue_tail(&x25->interrupt_out_queue, skb); 11061da177e4SLinus Torvalds else { 11071da177e4SLinus Torvalds len = x25_output(sk, skb); 11081da177e4SLinus Torvalds if (len < 0) 11091da177e4SLinus Torvalds kfree_skb(skb); 11101da177e4SLinus Torvalds else if (x25->qbitincl) 11111da177e4SLinus Torvalds len++; 11121da177e4SLinus Torvalds } 11131da177e4SLinus Torvalds 11141da177e4SLinus Torvalds /* 11151da177e4SLinus Torvalds * lock_sock() is currently only used to serialize this x25_kick() 11161da177e4SLinus Torvalds * against input-driven x25_kick() calls. It currently only blocks 11171da177e4SLinus Torvalds * incoming packets for this socket and does not protect against 11181da177e4SLinus Torvalds * any other socket state changes and is not called from anywhere 11191da177e4SLinus Torvalds * else. As x25_kick() cannot block and as long as all socket 11201da177e4SLinus Torvalds * operations are BKL-wrapped, we don't need take to care about 11211da177e4SLinus Torvalds * purging the backlog queue in x25_release(). 11221da177e4SLinus Torvalds * 11231da177e4SLinus Torvalds * Using lock_sock() to protect all socket operations entirely 11241da177e4SLinus Torvalds * (and making the whole x25 stack SMP aware) unfortunately would 11251da177e4SLinus Torvalds * require major changes to {send,recv}msg and skb allocation methods. 11261da177e4SLinus Torvalds * -> 2.5 ;) 11271da177e4SLinus Torvalds */ 11281da177e4SLinus Torvalds lock_sock(sk); 11291da177e4SLinus Torvalds x25_kick(sk); 11301da177e4SLinus Torvalds release_sock(sk); 11311da177e4SLinus Torvalds rc = len; 11321da177e4SLinus Torvalds out: 11331da177e4SLinus Torvalds return rc; 11341da177e4SLinus Torvalds out_kfree_skb: 11351da177e4SLinus Torvalds kfree_skb(skb); 11361da177e4SLinus Torvalds goto out; 11371da177e4SLinus Torvalds } 11381da177e4SLinus Torvalds 11391da177e4SLinus Torvalds 11401da177e4SLinus Torvalds static int x25_recvmsg(struct kiocb *iocb, struct socket *sock, 11411da177e4SLinus Torvalds struct msghdr *msg, size_t size, 11421da177e4SLinus Torvalds int flags) 11431da177e4SLinus Torvalds { 11441da177e4SLinus Torvalds struct sock *sk = sock->sk; 11451da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk); 11461da177e4SLinus Torvalds struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name; 11471da177e4SLinus Torvalds size_t copied; 11481da177e4SLinus Torvalds int qbit; 11491da177e4SLinus Torvalds struct sk_buff *skb; 11501da177e4SLinus Torvalds unsigned char *asmptr; 11511da177e4SLinus Torvalds int rc = -ENOTCONN; 11521da177e4SLinus Torvalds 11531da177e4SLinus Torvalds /* 11541da177e4SLinus Torvalds * This works for seqpacket too. The receiver has ordered the queue for 11551da177e4SLinus Torvalds * us! We do one quick check first though 11561da177e4SLinus Torvalds */ 11571da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED) 11581da177e4SLinus Torvalds goto out; 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds if (flags & MSG_OOB) { 11611da177e4SLinus Torvalds rc = -EINVAL; 11621da177e4SLinus Torvalds if (sock_flag(sk, SOCK_URGINLINE) || 11631da177e4SLinus Torvalds !skb_peek(&x25->interrupt_in_queue)) 11641da177e4SLinus Torvalds goto out; 11651da177e4SLinus Torvalds 11661da177e4SLinus Torvalds skb = skb_dequeue(&x25->interrupt_in_queue); 11671da177e4SLinus Torvalds 11681da177e4SLinus Torvalds skb_pull(skb, X25_STD_MIN_LEN); 11691da177e4SLinus Torvalds 11701da177e4SLinus Torvalds /* 11711da177e4SLinus Torvalds * No Q bit information on Interrupt data. 11721da177e4SLinus Torvalds */ 11731da177e4SLinus Torvalds if (x25->qbitincl) { 11741da177e4SLinus Torvalds asmptr = skb_push(skb, 1); 11751da177e4SLinus Torvalds *asmptr = 0x00; 11761da177e4SLinus Torvalds } 11771da177e4SLinus Torvalds 11781da177e4SLinus Torvalds msg->msg_flags |= MSG_OOB; 11791da177e4SLinus Torvalds } else { 11801da177e4SLinus Torvalds /* Now we can treat all alike */ 11811da177e4SLinus Torvalds skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, 11821da177e4SLinus Torvalds flags & MSG_DONTWAIT, &rc); 11831da177e4SLinus Torvalds if (!skb) 11841da177e4SLinus Torvalds goto out; 11851da177e4SLinus Torvalds 11861da177e4SLinus Torvalds qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT; 11871da177e4SLinus Torvalds 11881da177e4SLinus Torvalds skb_pull(skb, x25->neighbour->extended ? 11891da177e4SLinus Torvalds X25_EXT_MIN_LEN : X25_STD_MIN_LEN); 11901da177e4SLinus Torvalds 11911da177e4SLinus Torvalds if (x25->qbitincl) { 11921da177e4SLinus Torvalds asmptr = skb_push(skb, 1); 11931da177e4SLinus Torvalds *asmptr = qbit; 11941da177e4SLinus Torvalds } 11951da177e4SLinus Torvalds } 11961da177e4SLinus Torvalds 11971da177e4SLinus Torvalds skb->h.raw = skb->data; 11981da177e4SLinus Torvalds 11991da177e4SLinus Torvalds copied = skb->len; 12001da177e4SLinus Torvalds 12011da177e4SLinus Torvalds if (copied > size) { 12021da177e4SLinus Torvalds copied = size; 12031da177e4SLinus Torvalds msg->msg_flags |= MSG_TRUNC; 12041da177e4SLinus Torvalds } 12051da177e4SLinus Torvalds 12061da177e4SLinus Torvalds /* Currently, each datagram always contains a complete record */ 12071da177e4SLinus Torvalds msg->msg_flags |= MSG_EOR; 12081da177e4SLinus Torvalds 12091da177e4SLinus Torvalds rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied); 12101da177e4SLinus Torvalds if (rc) 12111da177e4SLinus Torvalds goto out_free_dgram; 12121da177e4SLinus Torvalds 12131da177e4SLinus Torvalds if (sx25) { 12141da177e4SLinus Torvalds sx25->sx25_family = AF_X25; 12151da177e4SLinus Torvalds sx25->sx25_addr = x25->dest_addr; 12161da177e4SLinus Torvalds } 12171da177e4SLinus Torvalds 12181da177e4SLinus Torvalds msg->msg_namelen = sizeof(struct sockaddr_x25); 12191da177e4SLinus Torvalds 12201da177e4SLinus Torvalds lock_sock(sk); 12211da177e4SLinus Torvalds x25_check_rbuf(sk); 12221da177e4SLinus Torvalds release_sock(sk); 12231da177e4SLinus Torvalds rc = copied; 12241da177e4SLinus Torvalds out_free_dgram: 12251da177e4SLinus Torvalds skb_free_datagram(sk, skb); 12261da177e4SLinus Torvalds out: 12271da177e4SLinus Torvalds return rc; 12281da177e4SLinus Torvalds } 12291da177e4SLinus Torvalds 12301da177e4SLinus Torvalds 12311da177e4SLinus Torvalds static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 12321da177e4SLinus Torvalds { 12331da177e4SLinus Torvalds struct sock *sk = sock->sk; 12341da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk); 12351da177e4SLinus Torvalds void __user *argp = (void __user *)arg; 12361da177e4SLinus Torvalds int rc; 12371da177e4SLinus Torvalds 12381da177e4SLinus Torvalds switch (cmd) { 12391da177e4SLinus Torvalds case TIOCOUTQ: { 12401da177e4SLinus Torvalds int amount = sk->sk_sndbuf - 12411da177e4SLinus Torvalds atomic_read(&sk->sk_wmem_alloc); 12421da177e4SLinus Torvalds if (amount < 0) 12431da177e4SLinus Torvalds amount = 0; 12441da177e4SLinus Torvalds rc = put_user(amount, (unsigned int __user *)argp); 12451da177e4SLinus Torvalds break; 12461da177e4SLinus Torvalds } 12471da177e4SLinus Torvalds 12481da177e4SLinus Torvalds case TIOCINQ: { 12491da177e4SLinus Torvalds struct sk_buff *skb; 12501da177e4SLinus Torvalds int amount = 0; 12511da177e4SLinus Torvalds /* 12521da177e4SLinus Torvalds * These two are safe on a single CPU system as 12531da177e4SLinus Torvalds * only user tasks fiddle here 12541da177e4SLinus Torvalds */ 12551da177e4SLinus Torvalds if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) 12561da177e4SLinus Torvalds amount = skb->len; 12571da177e4SLinus Torvalds rc = put_user(amount, (unsigned int __user *)argp); 12581da177e4SLinus Torvalds break; 12591da177e4SLinus Torvalds } 12601da177e4SLinus Torvalds 12611da177e4SLinus Torvalds case SIOCGSTAMP: 12621da177e4SLinus Torvalds rc = -EINVAL; 12631da177e4SLinus Torvalds if (sk) 12641da177e4SLinus Torvalds rc = sock_get_timestamp(sk, 12651da177e4SLinus Torvalds (struct timeval __user *)argp); 12661da177e4SLinus Torvalds break; 12671da177e4SLinus Torvalds case SIOCGIFADDR: 12681da177e4SLinus Torvalds case SIOCSIFADDR: 12691da177e4SLinus Torvalds case SIOCGIFDSTADDR: 12701da177e4SLinus Torvalds case SIOCSIFDSTADDR: 12711da177e4SLinus Torvalds case SIOCGIFBRDADDR: 12721da177e4SLinus Torvalds case SIOCSIFBRDADDR: 12731da177e4SLinus Torvalds case SIOCGIFNETMASK: 12741da177e4SLinus Torvalds case SIOCSIFNETMASK: 12751da177e4SLinus Torvalds case SIOCGIFMETRIC: 12761da177e4SLinus Torvalds case SIOCSIFMETRIC: 12771da177e4SLinus Torvalds rc = -EINVAL; 12781da177e4SLinus Torvalds break; 12791da177e4SLinus Torvalds case SIOCADDRT: 12801da177e4SLinus Torvalds case SIOCDELRT: 12811da177e4SLinus Torvalds rc = -EPERM; 12821da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 12831da177e4SLinus Torvalds break; 12841da177e4SLinus Torvalds rc = x25_route_ioctl(cmd, argp); 12851da177e4SLinus Torvalds break; 12861da177e4SLinus Torvalds case SIOCX25GSUBSCRIP: 12871da177e4SLinus Torvalds rc = x25_subscr_ioctl(cmd, argp); 12881da177e4SLinus Torvalds break; 12891da177e4SLinus Torvalds case SIOCX25SSUBSCRIP: 12901da177e4SLinus Torvalds rc = -EPERM; 12911da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 12921da177e4SLinus Torvalds break; 12931da177e4SLinus Torvalds rc = x25_subscr_ioctl(cmd, argp); 12941da177e4SLinus Torvalds break; 12951da177e4SLinus Torvalds case SIOCX25GFACILITIES: { 12961da177e4SLinus Torvalds struct x25_facilities fac = x25->facilities; 12971da177e4SLinus Torvalds rc = copy_to_user(argp, &fac, 12981da177e4SLinus Torvalds sizeof(fac)) ? -EFAULT : 0; 12991da177e4SLinus Torvalds break; 13001da177e4SLinus Torvalds } 13011da177e4SLinus Torvalds 13021da177e4SLinus Torvalds case SIOCX25SFACILITIES: { 13031da177e4SLinus Torvalds struct x25_facilities facilities; 13041da177e4SLinus Torvalds rc = -EFAULT; 13051da177e4SLinus Torvalds if (copy_from_user(&facilities, argp, 13061da177e4SLinus Torvalds sizeof(facilities))) 13071da177e4SLinus Torvalds break; 13081da177e4SLinus Torvalds rc = -EINVAL; 13091da177e4SLinus Torvalds if (sk->sk_state != TCP_LISTEN && 13101da177e4SLinus Torvalds sk->sk_state != TCP_CLOSE) 13111da177e4SLinus Torvalds break; 13121da177e4SLinus Torvalds if (facilities.pacsize_in < X25_PS16 || 13131da177e4SLinus Torvalds facilities.pacsize_in > X25_PS4096) 13141da177e4SLinus Torvalds break; 13151da177e4SLinus Torvalds if (facilities.pacsize_out < X25_PS16 || 13161da177e4SLinus Torvalds facilities.pacsize_out > X25_PS4096) 13171da177e4SLinus Torvalds break; 13181da177e4SLinus Torvalds if (facilities.winsize_in < 1 || 13191da177e4SLinus Torvalds facilities.winsize_in > 127) 13201da177e4SLinus Torvalds break; 13211da177e4SLinus Torvalds if (facilities.throughput < 0x03 || 13221da177e4SLinus Torvalds facilities.throughput > 0xDD) 13231da177e4SLinus Torvalds break; 1324ebc3f64bSShaun Pereira if (facilities.reverse && 1325ebc3f64bSShaun Pereira (facilities.reverse | 0x81)!= 0x81) 13261da177e4SLinus Torvalds break; 13271da177e4SLinus Torvalds x25->facilities = facilities; 13281da177e4SLinus Torvalds rc = 0; 13291da177e4SLinus Torvalds break; 13301da177e4SLinus Torvalds } 13311da177e4SLinus Torvalds 1332*a64b7b93SShaun Pereira case SIOCX25GDTEFACILITIES: { 1333*a64b7b93SShaun Pereira rc = copy_to_user(argp, &x25->dte_facilities, 1334*a64b7b93SShaun Pereira sizeof(x25->dte_facilities)); 1335*a64b7b93SShaun Pereira if (rc) 1336*a64b7b93SShaun Pereira rc = -EFAULT; 1337*a64b7b93SShaun Pereira break; 1338*a64b7b93SShaun Pereira } 1339*a64b7b93SShaun Pereira 1340*a64b7b93SShaun Pereira case SIOCX25SDTEFACILITIES: { 1341*a64b7b93SShaun Pereira struct x25_dte_facilities dtefacs; 1342*a64b7b93SShaun Pereira rc = -EFAULT; 1343*a64b7b93SShaun Pereira if (copy_from_user(&dtefacs, argp, sizeof(dtefacs))) 1344*a64b7b93SShaun Pereira break; 1345*a64b7b93SShaun Pereira rc = -EINVAL; 1346*a64b7b93SShaun Pereira if (sk->sk_state != TCP_LISTEN && 1347*a64b7b93SShaun Pereira sk->sk_state != TCP_CLOSE) 1348*a64b7b93SShaun Pereira break; 1349*a64b7b93SShaun Pereira if (dtefacs.calling_len > X25_MAX_AE_LEN) 1350*a64b7b93SShaun Pereira break; 1351*a64b7b93SShaun Pereira if (dtefacs.calling_ae == NULL) 1352*a64b7b93SShaun Pereira break; 1353*a64b7b93SShaun Pereira if (dtefacs.called_len > X25_MAX_AE_LEN) 1354*a64b7b93SShaun Pereira break; 1355*a64b7b93SShaun Pereira if (dtefacs.called_ae == NULL) 1356*a64b7b93SShaun Pereira break; 1357*a64b7b93SShaun Pereira x25->dte_facilities = dtefacs; 1358*a64b7b93SShaun Pereira rc = 0; 1359*a64b7b93SShaun Pereira break; 1360*a64b7b93SShaun Pereira } 1361*a64b7b93SShaun Pereira 13621da177e4SLinus Torvalds case SIOCX25GCALLUSERDATA: { 13631da177e4SLinus Torvalds struct x25_calluserdata cud = x25->calluserdata; 13641da177e4SLinus Torvalds rc = copy_to_user(argp, &cud, 13651da177e4SLinus Torvalds sizeof(cud)) ? -EFAULT : 0; 13661da177e4SLinus Torvalds break; 13671da177e4SLinus Torvalds } 13681da177e4SLinus Torvalds 13691da177e4SLinus Torvalds case SIOCX25SCALLUSERDATA: { 13701da177e4SLinus Torvalds struct x25_calluserdata calluserdata; 13711da177e4SLinus Torvalds 13721da177e4SLinus Torvalds rc = -EFAULT; 13731da177e4SLinus Torvalds if (copy_from_user(&calluserdata, argp, 13741da177e4SLinus Torvalds sizeof(calluserdata))) 13751da177e4SLinus Torvalds break; 13761da177e4SLinus Torvalds rc = -EINVAL; 13771da177e4SLinus Torvalds if (calluserdata.cudlength > X25_MAX_CUD_LEN) 13781da177e4SLinus Torvalds break; 13791da177e4SLinus Torvalds x25->calluserdata = calluserdata; 13801da177e4SLinus Torvalds rc = 0; 13811da177e4SLinus Torvalds break; 13821da177e4SLinus Torvalds } 13831da177e4SLinus Torvalds 13841da177e4SLinus Torvalds case SIOCX25GCAUSEDIAG: { 13851da177e4SLinus Torvalds struct x25_causediag causediag; 13861da177e4SLinus Torvalds causediag = x25->causediag; 13871da177e4SLinus Torvalds rc = copy_to_user(argp, &causediag, 13881da177e4SLinus Torvalds sizeof(causediag)) ? -EFAULT : 0; 13891da177e4SLinus Torvalds break; 13901da177e4SLinus Torvalds } 13911da177e4SLinus Torvalds 1392cb65d506SShaun Pereira case SIOCX25SCUDMATCHLEN: { 1393cb65d506SShaun Pereira struct x25_subaddr sub_addr; 1394cb65d506SShaun Pereira rc = -EINVAL; 1395cb65d506SShaun Pereira if(sk->sk_state != TCP_CLOSE) 1396cb65d506SShaun Pereira break; 1397cb65d506SShaun Pereira rc = -EFAULT; 1398cb65d506SShaun Pereira if (copy_from_user(&sub_addr, argp, 1399cb65d506SShaun Pereira sizeof(sub_addr))) 1400cb65d506SShaun Pereira break; 1401cb65d506SShaun Pereira rc = -EINVAL; 1402cb65d506SShaun Pereira if(sub_addr.cudmatchlength > X25_MAX_CUD_LEN) 1403cb65d506SShaun Pereira break; 1404cb65d506SShaun Pereira x25->cudmatchlength = sub_addr.cudmatchlength; 1405cb65d506SShaun Pereira rc = 0; 1406cb65d506SShaun Pereira break; 1407cb65d506SShaun Pereira } 1408cb65d506SShaun Pereira 1409ebc3f64bSShaun Pereira case SIOCX25CALLACCPTAPPRV: { 1410ebc3f64bSShaun Pereira rc = -EINVAL; 1411ebc3f64bSShaun Pereira if (sk->sk_state != TCP_CLOSE) 1412ebc3f64bSShaun Pereira break; 1413ebc3f64bSShaun Pereira x25->accptapprv = X25_ALLOW_ACCPT_APPRV; 1414ebc3f64bSShaun Pereira rc = 0; 1415ebc3f64bSShaun Pereira break; 1416ebc3f64bSShaun Pereira } 1417ebc3f64bSShaun Pereira 1418ebc3f64bSShaun Pereira case SIOCX25SENDCALLACCPT: { 1419ebc3f64bSShaun Pereira rc = -EINVAL; 1420ebc3f64bSShaun Pereira if (sk->sk_state != TCP_ESTABLISHED) 1421ebc3f64bSShaun Pereira break; 1422ebc3f64bSShaun Pereira if (x25->accptapprv) /* must call accptapprv above */ 1423ebc3f64bSShaun Pereira break; 1424ebc3f64bSShaun Pereira x25_write_internal(sk, X25_CALL_ACCEPTED); 1425ebc3f64bSShaun Pereira x25->state = X25_STATE_3; 1426ebc3f64bSShaun Pereira rc = 0; 1427ebc3f64bSShaun Pereira break; 1428ebc3f64bSShaun Pereira } 1429ebc3f64bSShaun Pereira 14301da177e4SLinus Torvalds default: 1431b5e5fa5eSChristoph Hellwig rc = -ENOIOCTLCMD; 14321da177e4SLinus Torvalds break; 14331da177e4SLinus Torvalds } 14341da177e4SLinus Torvalds 14351da177e4SLinus Torvalds return rc; 14361da177e4SLinus Torvalds } 14371da177e4SLinus Torvalds 14381da177e4SLinus Torvalds static struct net_proto_family x25_family_ops = { 14391da177e4SLinus Torvalds .family = AF_X25, 14401da177e4SLinus Torvalds .create = x25_create, 14411da177e4SLinus Torvalds .owner = THIS_MODULE, 14421da177e4SLinus Torvalds }; 14431da177e4SLinus Torvalds 14441b06e6baSShaun Pereira #ifdef CONFIG_COMPAT 14451b06e6baSShaun Pereira static int compat_x25_subscr_ioctl(unsigned int cmd, 14461b06e6baSShaun Pereira struct compat_x25_subscrip_struct __user *x25_subscr32) 14471b06e6baSShaun Pereira { 14481b06e6baSShaun Pereira struct compat_x25_subscrip_struct x25_subscr; 14491b06e6baSShaun Pereira struct x25_neigh *nb; 14501b06e6baSShaun Pereira struct net_device *dev; 14511b06e6baSShaun Pereira int rc = -EINVAL; 14521b06e6baSShaun Pereira 14531b06e6baSShaun Pereira rc = -EFAULT; 14541b06e6baSShaun Pereira if (copy_from_user(&x25_subscr, x25_subscr32, sizeof(*x25_subscr32))) 14551b06e6baSShaun Pereira goto out; 14561b06e6baSShaun Pereira 14571b06e6baSShaun Pereira rc = -EINVAL; 14581b06e6baSShaun Pereira dev = x25_dev_get(x25_subscr.device); 14591b06e6baSShaun Pereira if (dev == NULL) 14601b06e6baSShaun Pereira goto out; 14611b06e6baSShaun Pereira 14621b06e6baSShaun Pereira nb = x25_get_neigh(dev); 14631b06e6baSShaun Pereira if (nb == NULL) 14641b06e6baSShaun Pereira goto out_dev_put; 14651b06e6baSShaun Pereira 14661b06e6baSShaun Pereira dev_put(dev); 14671b06e6baSShaun Pereira 14681b06e6baSShaun Pereira if (cmd == SIOCX25GSUBSCRIP) { 14691b06e6baSShaun Pereira x25_subscr.extended = nb->extended; 14701b06e6baSShaun Pereira x25_subscr.global_facil_mask = nb->global_facil_mask; 14711b06e6baSShaun Pereira rc = copy_to_user(x25_subscr32, &x25_subscr, 14721b06e6baSShaun Pereira sizeof(*x25_subscr32)) ? -EFAULT : 0; 14731b06e6baSShaun Pereira } else { 14741b06e6baSShaun Pereira rc = -EINVAL; 14751b06e6baSShaun Pereira if (x25_subscr.extended == 0 || x25_subscr.extended == 1) { 14761b06e6baSShaun Pereira rc = 0; 14771b06e6baSShaun Pereira nb->extended = x25_subscr.extended; 14781b06e6baSShaun Pereira nb->global_facil_mask = x25_subscr.global_facil_mask; 14791b06e6baSShaun Pereira } 14801b06e6baSShaun Pereira } 14811b06e6baSShaun Pereira x25_neigh_put(nb); 14821b06e6baSShaun Pereira out: 14831b06e6baSShaun Pereira return rc; 14841b06e6baSShaun Pereira out_dev_put: 14851b06e6baSShaun Pereira dev_put(dev); 14861b06e6baSShaun Pereira goto out; 14871b06e6baSShaun Pereira } 14881b06e6baSShaun Pereira 14891b06e6baSShaun Pereira static int compat_x25_ioctl(struct socket *sock, unsigned int cmd, 14901b06e6baSShaun Pereira unsigned long arg) 14911b06e6baSShaun Pereira { 14921b06e6baSShaun Pereira void __user *argp = compat_ptr(arg); 14931b06e6baSShaun Pereira struct sock *sk = sock->sk; 14941b06e6baSShaun Pereira 14951b06e6baSShaun Pereira int rc = -ENOIOCTLCMD; 14961b06e6baSShaun Pereira 14971b06e6baSShaun Pereira switch(cmd) { 14981b06e6baSShaun Pereira case TIOCOUTQ: 14991b06e6baSShaun Pereira case TIOCINQ: 15001b06e6baSShaun Pereira rc = x25_ioctl(sock, cmd, (unsigned long)argp); 15011b06e6baSShaun Pereira break; 15021b06e6baSShaun Pereira case SIOCGSTAMP: 15031b06e6baSShaun Pereira rc = -EINVAL; 15041b06e6baSShaun Pereira if (sk) 15051b06e6baSShaun Pereira rc = compat_sock_get_timestamp(sk, 15061b06e6baSShaun Pereira (struct timeval __user*)argp); 15071b06e6baSShaun Pereira break; 15081b06e6baSShaun Pereira case SIOCGIFADDR: 15091b06e6baSShaun Pereira case SIOCSIFADDR: 15101b06e6baSShaun Pereira case SIOCGIFDSTADDR: 15111b06e6baSShaun Pereira case SIOCSIFDSTADDR: 15121b06e6baSShaun Pereira case SIOCGIFBRDADDR: 15131b06e6baSShaun Pereira case SIOCSIFBRDADDR: 15141b06e6baSShaun Pereira case SIOCGIFNETMASK: 15151b06e6baSShaun Pereira case SIOCSIFNETMASK: 15161b06e6baSShaun Pereira case SIOCGIFMETRIC: 15171b06e6baSShaun Pereira case SIOCSIFMETRIC: 15181b06e6baSShaun Pereira rc = -EINVAL; 15191b06e6baSShaun Pereira break; 15201b06e6baSShaun Pereira case SIOCADDRT: 15211b06e6baSShaun Pereira case SIOCDELRT: 15221b06e6baSShaun Pereira rc = -EPERM; 15231b06e6baSShaun Pereira if (!capable(CAP_NET_ADMIN)) 15241b06e6baSShaun Pereira break; 15251b06e6baSShaun Pereira rc = x25_route_ioctl(cmd, argp); 15261b06e6baSShaun Pereira break; 15271b06e6baSShaun Pereira case SIOCX25GSUBSCRIP: 15281b06e6baSShaun Pereira rc = compat_x25_subscr_ioctl(cmd, argp); 15291b06e6baSShaun Pereira break; 15301b06e6baSShaun Pereira case SIOCX25SSUBSCRIP: 15311b06e6baSShaun Pereira rc = -EPERM; 15321b06e6baSShaun Pereira if (!capable(CAP_NET_ADMIN)) 15331b06e6baSShaun Pereira break; 15341b06e6baSShaun Pereira rc = compat_x25_subscr_ioctl(cmd, argp); 15351b06e6baSShaun Pereira break; 15361b06e6baSShaun Pereira case SIOCX25GFACILITIES: 15371b06e6baSShaun Pereira case SIOCX25SFACILITIES: 15381b06e6baSShaun Pereira case SIOCX25GCALLUSERDATA: 15391b06e6baSShaun Pereira case SIOCX25SCALLUSERDATA: 15401b06e6baSShaun Pereira case SIOCX25GCAUSEDIAG: 15411b06e6baSShaun Pereira case SIOCX25SCUDMATCHLEN: 15421b06e6baSShaun Pereira case SIOCX25CALLACCPTAPPRV: 15431b06e6baSShaun Pereira case SIOCX25SENDCALLACCPT: 15441b06e6baSShaun Pereira rc = x25_ioctl(sock, cmd, (unsigned long)argp); 15451b06e6baSShaun Pereira break; 15461b06e6baSShaun Pereira default: 15471b06e6baSShaun Pereira rc = -ENOIOCTLCMD; 15481b06e6baSShaun Pereira break; 15491b06e6baSShaun Pereira } 15501b06e6baSShaun Pereira return rc; 15511b06e6baSShaun Pereira } 15521b06e6baSShaun Pereira #endif 15531b06e6baSShaun Pereira 155490ddc4f0SEric Dumazet static const struct proto_ops SOCKOPS_WRAPPED(x25_proto_ops) = { 15551da177e4SLinus Torvalds .family = AF_X25, 15561da177e4SLinus Torvalds .owner = THIS_MODULE, 15571da177e4SLinus Torvalds .release = x25_release, 15581da177e4SLinus Torvalds .bind = x25_bind, 15591da177e4SLinus Torvalds .connect = x25_connect, 15601da177e4SLinus Torvalds .socketpair = sock_no_socketpair, 15611da177e4SLinus Torvalds .accept = x25_accept, 15621da177e4SLinus Torvalds .getname = x25_getname, 15631da177e4SLinus Torvalds .poll = datagram_poll, 15641da177e4SLinus Torvalds .ioctl = x25_ioctl, 15651b06e6baSShaun Pereira #ifdef CONFIG_COMPAT 15661b06e6baSShaun Pereira .compat_ioctl = compat_x25_ioctl, 15671b06e6baSShaun Pereira #endif 15681da177e4SLinus Torvalds .listen = x25_listen, 15691da177e4SLinus Torvalds .shutdown = sock_no_shutdown, 15701da177e4SLinus Torvalds .setsockopt = x25_setsockopt, 15711da177e4SLinus Torvalds .getsockopt = x25_getsockopt, 15721da177e4SLinus Torvalds .sendmsg = x25_sendmsg, 15731da177e4SLinus Torvalds .recvmsg = x25_recvmsg, 15741da177e4SLinus Torvalds .mmap = sock_no_mmap, 15751da177e4SLinus Torvalds .sendpage = sock_no_sendpage, 15761da177e4SLinus Torvalds }; 15771da177e4SLinus Torvalds 15781da177e4SLinus Torvalds #include <linux/smp_lock.h> 15791da177e4SLinus Torvalds SOCKOPS_WRAP(x25_proto, AF_X25); 15801da177e4SLinus Torvalds 15811da177e4SLinus Torvalds static struct packet_type x25_packet_type = { 15821da177e4SLinus Torvalds .type = __constant_htons(ETH_P_X25), 15831da177e4SLinus Torvalds .func = x25_lapb_receive_frame, 15841da177e4SLinus Torvalds }; 15851da177e4SLinus Torvalds 15861da177e4SLinus Torvalds static struct notifier_block x25_dev_notifier = { 15871da177e4SLinus Torvalds .notifier_call = x25_device_event, 15881da177e4SLinus Torvalds }; 15891da177e4SLinus Torvalds 15901da177e4SLinus Torvalds void x25_kill_by_neigh(struct x25_neigh *nb) 15911da177e4SLinus Torvalds { 15921da177e4SLinus Torvalds struct sock *s; 15931da177e4SLinus Torvalds struct hlist_node *node; 15941da177e4SLinus Torvalds 15951da177e4SLinus Torvalds write_lock_bh(&x25_list_lock); 15961da177e4SLinus Torvalds 15971da177e4SLinus Torvalds sk_for_each(s, node, &x25_list) 15981da177e4SLinus Torvalds if (x25_sk(s)->neighbour == nb) 15991da177e4SLinus Torvalds x25_disconnect(s, ENETUNREACH, 0, 0); 16001da177e4SLinus Torvalds 16011da177e4SLinus Torvalds write_unlock_bh(&x25_list_lock); 16021da177e4SLinus Torvalds } 16031da177e4SLinus Torvalds 16041da177e4SLinus Torvalds static int __init x25_init(void) 16051da177e4SLinus Torvalds { 16061da177e4SLinus Torvalds int rc = proto_register(&x25_proto, 0); 16071da177e4SLinus Torvalds 16081da177e4SLinus Torvalds if (rc != 0) 16091da177e4SLinus Torvalds goto out; 16101da177e4SLinus Torvalds 16111da177e4SLinus Torvalds sock_register(&x25_family_ops); 16121da177e4SLinus Torvalds 16131da177e4SLinus Torvalds dev_add_pack(&x25_packet_type); 16141da177e4SLinus Torvalds 16151da177e4SLinus Torvalds register_netdevice_notifier(&x25_dev_notifier); 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds printk(KERN_INFO "X.25 for Linux. Version 0.2 for Linux 2.1.15\n"); 16181da177e4SLinus Torvalds 16191da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 16201da177e4SLinus Torvalds x25_register_sysctl(); 16211da177e4SLinus Torvalds #endif 16221da177e4SLinus Torvalds x25_proc_init(); 16231da177e4SLinus Torvalds out: 16241da177e4SLinus Torvalds return rc; 16251da177e4SLinus Torvalds } 16261da177e4SLinus Torvalds module_init(x25_init); 16271da177e4SLinus Torvalds 16281da177e4SLinus Torvalds static void __exit x25_exit(void) 16291da177e4SLinus Torvalds { 16301da177e4SLinus Torvalds x25_proc_exit(); 16311da177e4SLinus Torvalds x25_link_free(); 16321da177e4SLinus Torvalds x25_route_free(); 16331da177e4SLinus Torvalds 16341da177e4SLinus Torvalds #ifdef CONFIG_SYSCTL 16351da177e4SLinus Torvalds x25_unregister_sysctl(); 16361da177e4SLinus Torvalds #endif 16371da177e4SLinus Torvalds 16381da177e4SLinus Torvalds unregister_netdevice_notifier(&x25_dev_notifier); 16391da177e4SLinus Torvalds 16401da177e4SLinus Torvalds dev_remove_pack(&x25_packet_type); 16411da177e4SLinus Torvalds 16421da177e4SLinus Torvalds sock_unregister(AF_X25); 16431da177e4SLinus Torvalds proto_unregister(&x25_proto); 16441da177e4SLinus Torvalds } 16451da177e4SLinus Torvalds module_exit(x25_exit); 16461da177e4SLinus Torvalds 16471da177e4SLinus Torvalds MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>"); 16481da177e4SLinus Torvalds MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol"); 16491da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 16501da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_X25); 1651