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 38b73e9e3cSwangweidong #define pr_fmt(fmt) "X25: " fmt 39b73e9e3cSwangweidong 401da177e4SLinus Torvalds #include <linux/module.h> 414fc268d2SRandy Dunlap #include <linux/capability.h> 421da177e4SLinus Torvalds #include <linux/errno.h> 431da177e4SLinus Torvalds #include <linux/kernel.h> 443f07c014SIngo Molnar #include <linux/sched/signal.h> 451da177e4SLinus Torvalds #include <linux/timer.h> 461da177e4SLinus Torvalds #include <linux/string.h> 471da177e4SLinus Torvalds #include <linux/net.h> 481da177e4SLinus Torvalds #include <linux/netdevice.h> 491da177e4SLinus Torvalds #include <linux/if_arp.h> 501da177e4SLinus Torvalds #include <linux/skbuff.h> 515a0e3ad6STejun Heo #include <linux/slab.h> 521da177e4SLinus Torvalds #include <net/sock.h> 53c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h> 547c0f6ba6SLinus Torvalds #include <linux/uaccess.h> 551da177e4SLinus Torvalds #include <linux/fcntl.h> 561da177e4SLinus Torvalds #include <linux/termios.h> /* For TIOCINQ/OUTQ */ 571da177e4SLinus Torvalds #include <linux/notifier.h> 581da177e4SLinus Torvalds #include <linux/init.h> 591b06e6baSShaun Pereira #include <linux/compat.h> 60a9288525Sandrew hendry #include <linux/ctype.h> 611b06e6baSShaun Pereira 621da177e4SLinus Torvalds #include <net/x25.h> 631b06e6baSShaun Pereira #include <net/compat.h> 641da177e4SLinus Torvalds 651da177e4SLinus Torvalds int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20; 661da177e4SLinus Torvalds int sysctl_x25_call_request_timeout = X25_DEFAULT_T21; 671da177e4SLinus Torvalds int sysctl_x25_reset_request_timeout = X25_DEFAULT_T22; 681da177e4SLinus Torvalds int sysctl_x25_clear_request_timeout = X25_DEFAULT_T23; 691da177e4SLinus Torvalds int sysctl_x25_ack_holdback_timeout = X25_DEFAULT_T2; 7039e21c0dSAndrew Hendry int sysctl_x25_forward = 0; 711da177e4SLinus Torvalds 721da177e4SLinus Torvalds HLIST_HEAD(x25_list); 731da177e4SLinus Torvalds DEFINE_RWLOCK(x25_list_lock); 741da177e4SLinus Torvalds 7590ddc4f0SEric Dumazet static const struct proto_ops x25_proto_ops; 761da177e4SLinus Torvalds 77e9e6c232SJulia Lawall static const struct x25_address null_x25_address = {" "}; 781da177e4SLinus Torvalds 791b06e6baSShaun Pereira #ifdef CONFIG_COMPAT 801b06e6baSShaun Pereira struct compat_x25_subscrip_struct { 811b06e6baSShaun Pereira char device[200-sizeof(compat_ulong_t)]; 821b06e6baSShaun Pereira compat_ulong_t global_facil_mask; 831b06e6baSShaun Pereira compat_uint_t extended; 841b06e6baSShaun Pereira }; 851b06e6baSShaun Pereira #endif 861b06e6baSShaun Pereira 87f5eb917bSJohn Hughes 88f5eb917bSJohn Hughes int x25_parse_address_block(struct sk_buff *skb, 89f5eb917bSJohn Hughes struct x25_address *called_addr, 90f5eb917bSJohn Hughes struct x25_address *calling_addr) 91f5eb917bSJohn Hughes { 92f5eb917bSJohn Hughes unsigned char len; 93f5eb917bSJohn Hughes int needed; 94f5eb917bSJohn Hughes int rc; 95f5eb917bSJohn Hughes 96cb101ed2SMatthew Daley if (!pskb_may_pull(skb, 1)) { 97f5eb917bSJohn Hughes /* packet has no address block */ 98f5eb917bSJohn Hughes rc = 0; 99f5eb917bSJohn Hughes goto empty; 100f5eb917bSJohn Hughes } 101f5eb917bSJohn Hughes 102f5eb917bSJohn Hughes len = *skb->data; 103d449ba3dSMartin Schiller needed = 1 + ((len >> 4) + (len & 0x0f) + 1) / 2; 104f5eb917bSJohn Hughes 105cb101ed2SMatthew Daley if (!pskb_may_pull(skb, needed)) { 106f5eb917bSJohn Hughes /* packet is too short to hold the addresses it claims 107f5eb917bSJohn Hughes to hold */ 108f5eb917bSJohn Hughes rc = -1; 109f5eb917bSJohn Hughes goto empty; 110f5eb917bSJohn Hughes } 111f5eb917bSJohn Hughes 112f5eb917bSJohn Hughes return x25_addr_ntoa(skb->data, called_addr, calling_addr); 113f5eb917bSJohn Hughes 114f5eb917bSJohn Hughes empty: 115f5eb917bSJohn Hughes *called_addr->x25_addr = 0; 116f5eb917bSJohn Hughes *calling_addr->x25_addr = 0; 117f5eb917bSJohn Hughes 118f5eb917bSJohn Hughes return rc; 119f5eb917bSJohn Hughes } 120f5eb917bSJohn Hughes 121f5eb917bSJohn Hughes 1221da177e4SLinus Torvalds int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr, 1231da177e4SLinus Torvalds struct x25_address *calling_addr) 1241da177e4SLinus Torvalds { 1256bf1574eSEric Dumazet unsigned int called_len, calling_len; 1261da177e4SLinus Torvalds char *called, *calling; 1276bf1574eSEric Dumazet unsigned int i; 1281da177e4SLinus Torvalds 1291da177e4SLinus Torvalds called_len = (*p >> 0) & 0x0F; 1301da177e4SLinus Torvalds calling_len = (*p >> 4) & 0x0F; 1311da177e4SLinus Torvalds 1321da177e4SLinus Torvalds called = called_addr->x25_addr; 1331da177e4SLinus Torvalds calling = calling_addr->x25_addr; 1341da177e4SLinus Torvalds p++; 1351da177e4SLinus Torvalds 1361da177e4SLinus Torvalds for (i = 0; i < (called_len + calling_len); i++) { 1371da177e4SLinus Torvalds if (i < called_len) { 1381da177e4SLinus Torvalds if (i % 2 != 0) { 1391da177e4SLinus Torvalds *called++ = ((*p >> 0) & 0x0F) + '0'; 1401da177e4SLinus Torvalds p++; 1411da177e4SLinus Torvalds } else { 1421da177e4SLinus Torvalds *called++ = ((*p >> 4) & 0x0F) + '0'; 1431da177e4SLinus Torvalds } 1441da177e4SLinus Torvalds } else { 1451da177e4SLinus Torvalds if (i % 2 != 0) { 1461da177e4SLinus Torvalds *calling++ = ((*p >> 0) & 0x0F) + '0'; 1471da177e4SLinus Torvalds p++; 1481da177e4SLinus Torvalds } else { 1491da177e4SLinus Torvalds *calling++ = ((*p >> 4) & 0x0F) + '0'; 1501da177e4SLinus Torvalds } 1511da177e4SLinus Torvalds } 1521da177e4SLinus Torvalds } 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds *called = *calling = '\0'; 1551da177e4SLinus Torvalds 1561da177e4SLinus Torvalds return 1 + (called_len + calling_len + 1) / 2; 1571da177e4SLinus Torvalds } 1581da177e4SLinus Torvalds 1591da177e4SLinus Torvalds int x25_addr_aton(unsigned char *p, struct x25_address *called_addr, 1601da177e4SLinus Torvalds struct x25_address *calling_addr) 1611da177e4SLinus Torvalds { 1621da177e4SLinus Torvalds unsigned int called_len, calling_len; 1631da177e4SLinus Torvalds char *called, *calling; 1641da177e4SLinus Torvalds int i; 1651da177e4SLinus Torvalds 1661da177e4SLinus Torvalds called = called_addr->x25_addr; 1671da177e4SLinus Torvalds calling = calling_addr->x25_addr; 1681da177e4SLinus Torvalds 1691da177e4SLinus Torvalds called_len = strlen(called); 1701da177e4SLinus Torvalds calling_len = strlen(calling); 1711da177e4SLinus Torvalds 1721da177e4SLinus Torvalds *p++ = (calling_len << 4) | (called_len << 0); 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds for (i = 0; i < (called_len + calling_len); i++) { 1751da177e4SLinus Torvalds if (i < called_len) { 1761da177e4SLinus Torvalds if (i % 2 != 0) { 1771da177e4SLinus Torvalds *p |= (*called++ - '0') << 0; 1781da177e4SLinus Torvalds p++; 1791da177e4SLinus Torvalds } else { 1801da177e4SLinus Torvalds *p = 0x00; 1811da177e4SLinus Torvalds *p |= (*called++ - '0') << 4; 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds } else { 1841da177e4SLinus Torvalds if (i % 2 != 0) { 1851da177e4SLinus Torvalds *p |= (*calling++ - '0') << 0; 1861da177e4SLinus Torvalds p++; 1871da177e4SLinus Torvalds } else { 1881da177e4SLinus Torvalds *p = 0x00; 1891da177e4SLinus Torvalds *p |= (*calling++ - '0') << 4; 1901da177e4SLinus Torvalds } 1911da177e4SLinus Torvalds } 1921da177e4SLinus Torvalds } 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds return 1 + (called_len + calling_len + 1) / 2; 1951da177e4SLinus Torvalds } 1961da177e4SLinus Torvalds 1971da177e4SLinus Torvalds /* 1981da177e4SLinus Torvalds * Socket removal during an interrupt is now safe. 1991da177e4SLinus Torvalds */ 2001da177e4SLinus Torvalds static void x25_remove_socket(struct sock *sk) 2011da177e4SLinus Torvalds { 2021da177e4SLinus Torvalds write_lock_bh(&x25_list_lock); 2031da177e4SLinus Torvalds sk_del_node_init(sk); 2041da177e4SLinus Torvalds write_unlock_bh(&x25_list_lock); 2051da177e4SLinus Torvalds } 2061da177e4SLinus Torvalds 2071da177e4SLinus Torvalds /* 2081da177e4SLinus Torvalds * Kill all bound sockets on a dropped device. 2091da177e4SLinus Torvalds */ 2101da177e4SLinus Torvalds static void x25_kill_by_device(struct net_device *dev) 2111da177e4SLinus Torvalds { 2121da177e4SLinus Torvalds struct sock *s; 2131da177e4SLinus Torvalds 2141da177e4SLinus Torvalds write_lock_bh(&x25_list_lock); 2151da177e4SLinus Torvalds 216b67bfe0dSSasha Levin sk_for_each(s, &x25_list) 2171da177e4SLinus Torvalds if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev) 2181da177e4SLinus Torvalds x25_disconnect(s, ENETUNREACH, 0, 0); 2191da177e4SLinus Torvalds 2201da177e4SLinus Torvalds write_unlock_bh(&x25_list_lock); 2211da177e4SLinus Torvalds } 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds /* 2241da177e4SLinus Torvalds * Handle device status changes. 2251da177e4SLinus Torvalds */ 2261da177e4SLinus Torvalds static int x25_device_event(struct notifier_block *this, unsigned long event, 2271da177e4SLinus Torvalds void *ptr) 2281da177e4SLinus Torvalds { 229351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr); 2301da177e4SLinus Torvalds struct x25_neigh *nb; 2311da177e4SLinus Torvalds 232721499e8SYOSHIFUJI Hideaki if (!net_eq(dev_net(dev), &init_net)) 233e9dc8653SEric W. Biederman return NOTIFY_DONE; 234e9dc8653SEric W. Biederman 2351da177e4SLinus Torvalds if (dev->type == ARPHRD_X25 23629c36262SIgor Maravić #if IS_ENABLED(CONFIG_LLC) 2371da177e4SLinus Torvalds || dev->type == ARPHRD_ETHER 2381da177e4SLinus Torvalds #endif 2391da177e4SLinus Torvalds ) { 2401da177e4SLinus Torvalds switch (event) { 2411da177e4SLinus Torvalds case NETDEV_UP: 2421da177e4SLinus Torvalds x25_link_device_up(dev); 2431da177e4SLinus Torvalds break; 2441da177e4SLinus Torvalds case NETDEV_GOING_DOWN: 2451da177e4SLinus Torvalds nb = x25_get_neigh(dev); 2461da177e4SLinus Torvalds if (nb) { 2471da177e4SLinus Torvalds x25_terminate_link(nb); 2481da177e4SLinus Torvalds x25_neigh_put(nb); 2491da177e4SLinus Torvalds } 2501da177e4SLinus Torvalds break; 2511da177e4SLinus Torvalds case NETDEV_DOWN: 2521da177e4SLinus Torvalds x25_kill_by_device(dev); 2531da177e4SLinus Torvalds x25_route_device_down(dev); 2541da177e4SLinus Torvalds x25_link_device_down(dev); 2551da177e4SLinus Torvalds break; 2561da177e4SLinus Torvalds } 2571da177e4SLinus Torvalds } 2581da177e4SLinus Torvalds 2591da177e4SLinus Torvalds return NOTIFY_DONE; 2601da177e4SLinus Torvalds } 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds /* 2631da177e4SLinus Torvalds * Add a socket to the bound sockets list. 2641da177e4SLinus Torvalds */ 2651da177e4SLinus Torvalds static void x25_insert_socket(struct sock *sk) 2661da177e4SLinus Torvalds { 2671da177e4SLinus Torvalds write_lock_bh(&x25_list_lock); 2681da177e4SLinus Torvalds sk_add_node(sk, &x25_list); 2691da177e4SLinus Torvalds write_unlock_bh(&x25_list_lock); 2701da177e4SLinus Torvalds } 2711da177e4SLinus Torvalds 2721da177e4SLinus Torvalds /* 2731da177e4SLinus Torvalds * Find a socket that wants to accept the Call Request we just 2741da177e4SLinus Torvalds * received. Check the full list for an address/cud match. 2751da177e4SLinus Torvalds * If no cuds match return the next_best thing, an address match. 2761da177e4SLinus Torvalds * Note: if a listening socket has cud set it must only get calls 2771da177e4SLinus Torvalds * with matching cud. 2781da177e4SLinus Torvalds */ 279cb65d506SShaun Pereira static struct sock *x25_find_listener(struct x25_address *addr, 280cb65d506SShaun Pereira struct sk_buff *skb) 2811da177e4SLinus Torvalds { 2821da177e4SLinus Torvalds struct sock *s; 2831da177e4SLinus Torvalds struct sock *next_best; 2841da177e4SLinus Torvalds 2851da177e4SLinus Torvalds read_lock_bh(&x25_list_lock); 2861da177e4SLinus Torvalds next_best = NULL; 2871da177e4SLinus Torvalds 288b67bfe0dSSasha Levin sk_for_each(s, &x25_list) 2891da177e4SLinus Torvalds if ((!strcmp(addr->x25_addr, 2901da177e4SLinus Torvalds x25_sk(s)->source_addr.x25_addr) || 29106137619SMartin Schiller !strcmp(x25_sk(s)->source_addr.x25_addr, 2921da177e4SLinus Torvalds null_x25_address.x25_addr)) && 2931da177e4SLinus Torvalds s->sk_state == TCP_LISTEN) { 2941da177e4SLinus Torvalds /* 2951da177e4SLinus Torvalds * Found a listening socket, now check the incoming 2961da177e4SLinus Torvalds * call user data vs this sockets call user data 2971da177e4SLinus Torvalds */ 2987f81e25bSMatthew Daley if (x25_sk(s)->cudmatchlength > 0 && 2997f81e25bSMatthew Daley skb->len >= x25_sk(s)->cudmatchlength) { 300cb65d506SShaun Pereira if((memcmp(x25_sk(s)->calluserdata.cuddata, 301cb65d506SShaun Pereira skb->data, 302cb65d506SShaun Pereira x25_sk(s)->cudmatchlength)) == 0) { 3031da177e4SLinus Torvalds sock_hold(s); 3041da177e4SLinus Torvalds goto found; 3051da177e4SLinus Torvalds } 306cb65d506SShaun Pereira } else 3071da177e4SLinus Torvalds next_best = s; 3081da177e4SLinus Torvalds } 3091da177e4SLinus Torvalds if (next_best) { 3101da177e4SLinus Torvalds s = next_best; 3111da177e4SLinus Torvalds sock_hold(s); 3121da177e4SLinus Torvalds goto found; 3131da177e4SLinus Torvalds } 3141da177e4SLinus Torvalds s = NULL; 3151da177e4SLinus Torvalds found: 3161da177e4SLinus Torvalds read_unlock_bh(&x25_list_lock); 3171da177e4SLinus Torvalds return s; 3181da177e4SLinus Torvalds } 3191da177e4SLinus Torvalds 3201da177e4SLinus Torvalds /* 3211da177e4SLinus Torvalds * Find a connected X.25 socket given my LCI and neighbour. 3221da177e4SLinus Torvalds */ 3231da177e4SLinus Torvalds static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb) 3241da177e4SLinus Torvalds { 3251da177e4SLinus Torvalds struct sock *s; 3261da177e4SLinus Torvalds 327b67bfe0dSSasha Levin sk_for_each(s, &x25_list) 3281da177e4SLinus Torvalds if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) { 3291da177e4SLinus Torvalds sock_hold(s); 3301da177e4SLinus Torvalds goto found; 3311da177e4SLinus Torvalds } 3321da177e4SLinus Torvalds s = NULL; 3331da177e4SLinus Torvalds found: 3341da177e4SLinus Torvalds return s; 3351da177e4SLinus Torvalds } 3361da177e4SLinus Torvalds 3371da177e4SLinus Torvalds struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb) 3381da177e4SLinus Torvalds { 3391da177e4SLinus Torvalds struct sock *s; 3401da177e4SLinus Torvalds 3411da177e4SLinus Torvalds read_lock_bh(&x25_list_lock); 3421da177e4SLinus Torvalds s = __x25_find_socket(lci, nb); 3431da177e4SLinus Torvalds read_unlock_bh(&x25_list_lock); 3441da177e4SLinus Torvalds return s; 3451da177e4SLinus Torvalds } 3461da177e4SLinus Torvalds 3471da177e4SLinus Torvalds /* 3481da177e4SLinus Torvalds * Find a unique LCI for a given device. 3491da177e4SLinus Torvalds */ 3501da177e4SLinus Torvalds static unsigned int x25_new_lci(struct x25_neigh *nb) 3511da177e4SLinus Torvalds { 3521da177e4SLinus Torvalds unsigned int lci = 1; 3531da177e4SLinus Torvalds struct sock *sk; 3541da177e4SLinus Torvalds 355cf657d22SEric Dumazet while ((sk = x25_find_socket(lci, nb)) != NULL) { 3561da177e4SLinus Torvalds sock_put(sk); 3571da177e4SLinus Torvalds if (++lci == 4096) { 3581da177e4SLinus Torvalds lci = 0; 3591da177e4SLinus Torvalds break; 3601da177e4SLinus Torvalds } 361cf657d22SEric Dumazet cond_resched(); 3621da177e4SLinus Torvalds } 3631da177e4SLinus Torvalds 3641da177e4SLinus Torvalds return lci; 3651da177e4SLinus Torvalds } 3661da177e4SLinus Torvalds 3671da177e4SLinus Torvalds /* 3681da177e4SLinus Torvalds * Deferred destroy. 3691da177e4SLinus Torvalds */ 37014ebaf81SDavid S. Miller static void __x25_destroy_socket(struct sock *); 3711da177e4SLinus Torvalds 3721da177e4SLinus Torvalds /* 3731da177e4SLinus Torvalds * handler for deferred kills. 3741da177e4SLinus Torvalds */ 37599767f27SKees Cook static void x25_destroy_timer(struct timer_list *t) 3761da177e4SLinus Torvalds { 37799767f27SKees Cook struct sock *sk = from_timer(sk, t, sk_timer); 37899767f27SKees Cook 37999767f27SKees Cook x25_destroy_socket_from_timer(sk); 3801da177e4SLinus Torvalds } 3811da177e4SLinus Torvalds 3821da177e4SLinus Torvalds /* 3831da177e4SLinus Torvalds * This is called from user mode and the timers. Thus it protects itself 3841da177e4SLinus Torvalds * against interrupt users but doesn't worry about being called during 3851da177e4SLinus Torvalds * work. Once it is removed from the queue no interrupt or bottom half 3861da177e4SLinus Torvalds * will touch it and we are (fairly 8-) ) safe. 3871da177e4SLinus Torvalds * Not static as it's used by the timer 3881da177e4SLinus Torvalds */ 38914ebaf81SDavid S. Miller static void __x25_destroy_socket(struct sock *sk) 3901da177e4SLinus Torvalds { 3911da177e4SLinus Torvalds struct sk_buff *skb; 3921da177e4SLinus Torvalds 3931da177e4SLinus Torvalds x25_stop_heartbeat(sk); 3941da177e4SLinus Torvalds x25_stop_timer(sk); 3951da177e4SLinus Torvalds 3961da177e4SLinus Torvalds x25_remove_socket(sk); 3971da177e4SLinus Torvalds x25_clear_queues(sk); /* Flush the queues */ 3981da177e4SLinus Torvalds 3991da177e4SLinus Torvalds while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) { 4001da177e4SLinus Torvalds if (skb->sk != sk) { /* A pending connection */ 4011da177e4SLinus Torvalds /* 4021da177e4SLinus Torvalds * Queue the unaccepted socket for death 4031da177e4SLinus Torvalds */ 4042cec6b01Sandrew hendry skb->sk->sk_state = TCP_LISTEN; 4051da177e4SLinus Torvalds sock_set_flag(skb->sk, SOCK_DEAD); 4061da177e4SLinus Torvalds x25_start_heartbeat(skb->sk); 4071da177e4SLinus Torvalds x25_sk(skb->sk)->state = X25_STATE_0; 4081da177e4SLinus Torvalds } 4091da177e4SLinus Torvalds 4101da177e4SLinus Torvalds kfree_skb(skb); 4111da177e4SLinus Torvalds } 4121da177e4SLinus Torvalds 413c564039fSEric Dumazet if (sk_has_allocations(sk)) { 4141da177e4SLinus Torvalds /* Defer: outstanding buffers */ 4151da177e4SLinus Torvalds sk->sk_timer.expires = jiffies + 10 * HZ; 416841b86f3SKees Cook sk->sk_timer.function = x25_destroy_timer; 4171da177e4SLinus Torvalds add_timer(&sk->sk_timer); 4181da177e4SLinus Torvalds } else { 4191da177e4SLinus Torvalds /* drop last reference so sock_put will free */ 4201da177e4SLinus Torvalds __sock_put(sk); 4211da177e4SLinus Torvalds } 42214ebaf81SDavid S. Miller } 4231da177e4SLinus Torvalds 42414ebaf81SDavid S. Miller void x25_destroy_socket_from_timer(struct sock *sk) 42514ebaf81SDavid S. Miller { 42614ebaf81SDavid S. Miller sock_hold(sk); 42714ebaf81SDavid S. Miller bh_lock_sock(sk); 42814ebaf81SDavid S. Miller __x25_destroy_socket(sk); 42914ebaf81SDavid S. Miller bh_unlock_sock(sk); 43014ebaf81SDavid S. Miller sock_put(sk); 43114ebaf81SDavid S. Miller } 43214ebaf81SDavid S. Miller 4331da177e4SLinus Torvalds /* 4341da177e4SLinus Torvalds * Handling for system calls applied via the various interfaces to a 4351da177e4SLinus Torvalds * X.25 socket object. 4361da177e4SLinus Torvalds */ 4371da177e4SLinus Torvalds 4381da177e4SLinus Torvalds static int x25_setsockopt(struct socket *sock, int level, int optname, 439b7058842SDavid S. Miller char __user *optval, unsigned int optlen) 4401da177e4SLinus Torvalds { 4411da177e4SLinus Torvalds int opt; 4421da177e4SLinus Torvalds struct sock *sk = sock->sk; 4431da177e4SLinus Torvalds int rc = -ENOPROTOOPT; 4441da177e4SLinus Torvalds 4451da177e4SLinus Torvalds if (level != SOL_X25 || optname != X25_QBITINCL) 4461da177e4SLinus Torvalds goto out; 4471da177e4SLinus Torvalds 4481da177e4SLinus Torvalds rc = -EINVAL; 4491da177e4SLinus Torvalds if (optlen < sizeof(int)) 4501da177e4SLinus Torvalds goto out; 4511da177e4SLinus Torvalds 4521da177e4SLinus Torvalds rc = -EFAULT; 4531da177e4SLinus Torvalds if (get_user(opt, (int __user *)optval)) 4541da177e4SLinus Torvalds goto out; 4551da177e4SLinus Torvalds 456cb863ffdSandrew hendry if (opt) 457cb863ffdSandrew hendry set_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags); 458cb863ffdSandrew hendry else 459cb863ffdSandrew hendry clear_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags); 4601da177e4SLinus Torvalds rc = 0; 4611da177e4SLinus Torvalds out: 4621da177e4SLinus Torvalds return rc; 4631da177e4SLinus Torvalds } 4641da177e4SLinus Torvalds 4651da177e4SLinus Torvalds static int x25_getsockopt(struct socket *sock, int level, int optname, 4661da177e4SLinus Torvalds char __user *optval, int __user *optlen) 4671da177e4SLinus Torvalds { 4681da177e4SLinus Torvalds struct sock *sk = sock->sk; 4691da177e4SLinus Torvalds int val, len, rc = -ENOPROTOOPT; 4701da177e4SLinus Torvalds 4711da177e4SLinus Torvalds if (level != SOL_X25 || optname != X25_QBITINCL) 4721da177e4SLinus Torvalds goto out; 4731da177e4SLinus Torvalds 4741da177e4SLinus Torvalds rc = -EFAULT; 4751da177e4SLinus Torvalds if (get_user(len, optlen)) 4761da177e4SLinus Torvalds goto out; 4771da177e4SLinus Torvalds 4781da177e4SLinus Torvalds len = min_t(unsigned int, len, sizeof(int)); 4791da177e4SLinus Torvalds 4801da177e4SLinus Torvalds rc = -EINVAL; 4811da177e4SLinus Torvalds if (len < 0) 4821da177e4SLinus Torvalds goto out; 4831da177e4SLinus Torvalds 4841da177e4SLinus Torvalds rc = -EFAULT; 4851da177e4SLinus Torvalds if (put_user(len, optlen)) 4861da177e4SLinus Torvalds goto out; 4871da177e4SLinus Torvalds 488cb863ffdSandrew hendry val = test_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags); 4891da177e4SLinus Torvalds rc = copy_to_user(optval, &val, len) ? -EFAULT : 0; 4901da177e4SLinus Torvalds out: 4911da177e4SLinus Torvalds return rc; 4921da177e4SLinus Torvalds } 4931da177e4SLinus Torvalds 4941da177e4SLinus Torvalds static int x25_listen(struct socket *sock, int backlog) 4951da177e4SLinus Torvalds { 4961da177e4SLinus Torvalds struct sock *sk = sock->sk; 4971da177e4SLinus Torvalds int rc = -EOPNOTSUPP; 4981da177e4SLinus Torvalds 49925aa4efeSandrew hendry lock_sock(sk); 5001da177e4SLinus Torvalds if (sk->sk_state != TCP_LISTEN) { 5011da177e4SLinus Torvalds memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN); 5021da177e4SLinus Torvalds sk->sk_max_ack_backlog = backlog; 5031da177e4SLinus Torvalds sk->sk_state = TCP_LISTEN; 5041da177e4SLinus Torvalds rc = 0; 5051da177e4SLinus Torvalds } 50625aa4efeSandrew hendry release_sock(sk); 5071da177e4SLinus Torvalds 5081da177e4SLinus Torvalds return rc; 5091da177e4SLinus Torvalds } 5101da177e4SLinus Torvalds 5111da177e4SLinus Torvalds static struct proto x25_proto = { 5121da177e4SLinus Torvalds .name = "X25", 5131da177e4SLinus Torvalds .owner = THIS_MODULE, 5141da177e4SLinus Torvalds .obj_size = sizeof(struct x25_sock), 5151da177e4SLinus Torvalds }; 5161da177e4SLinus Torvalds 51711aa9c28SEric W. Biederman static struct sock *x25_alloc_socket(struct net *net, int kern) 5181da177e4SLinus Torvalds { 5191da177e4SLinus Torvalds struct x25_sock *x25; 52011aa9c28SEric W. Biederman struct sock *sk = sk_alloc(net, AF_X25, GFP_ATOMIC, &x25_proto, kern); 5211da177e4SLinus Torvalds 5221da177e4SLinus Torvalds if (!sk) 5231da177e4SLinus Torvalds goto out; 5241da177e4SLinus Torvalds 5251da177e4SLinus Torvalds sock_init_data(NULL, sk); 5261da177e4SLinus Torvalds 5271da177e4SLinus Torvalds x25 = x25_sk(sk); 5281da177e4SLinus Torvalds skb_queue_head_init(&x25->ack_queue); 5291da177e4SLinus Torvalds skb_queue_head_init(&x25->fragment_queue); 5301da177e4SLinus Torvalds skb_queue_head_init(&x25->interrupt_in_queue); 5311da177e4SLinus Torvalds skb_queue_head_init(&x25->interrupt_out_queue); 5321da177e4SLinus Torvalds out: 5331da177e4SLinus Torvalds return sk; 5341da177e4SLinus Torvalds } 5351da177e4SLinus Torvalds 5363f378b68SEric Paris static int x25_create(struct net *net, struct socket *sock, int protocol, 5373f378b68SEric Paris int kern) 5381da177e4SLinus Torvalds { 5391da177e4SLinus Torvalds struct sock *sk; 5401da177e4SLinus Torvalds struct x25_sock *x25; 541b18e7a06Sandrew hendry int rc = -EAFNOSUPPORT; 5421da177e4SLinus Torvalds 54309ad9bc7SOctavian Purdila if (!net_eq(net, &init_net)) 5441da177e4SLinus Torvalds goto out; 5451da177e4SLinus Torvalds 546b18e7a06Sandrew hendry rc = -ESOCKTNOSUPPORT; 547b18e7a06Sandrew hendry if (sock->type != SOCK_SEQPACKET) 548b18e7a06Sandrew hendry goto out; 549b18e7a06Sandrew hendry 550b18e7a06Sandrew hendry rc = -EINVAL; 551b18e7a06Sandrew hendry if (protocol) 552b18e7a06Sandrew hendry goto out; 553b18e7a06Sandrew hendry 554b18e7a06Sandrew hendry rc = -ENOBUFS; 55511aa9c28SEric W. Biederman if ((sk = x25_alloc_socket(net, kern)) == NULL) 5561da177e4SLinus Torvalds goto out; 5571da177e4SLinus Torvalds 5581da177e4SLinus Torvalds x25 = x25_sk(sk); 5591da177e4SLinus Torvalds 5601da177e4SLinus Torvalds sock_init_data(sock, sk); 5611da177e4SLinus Torvalds 5621da177e4SLinus Torvalds x25_init_timers(sk); 5631da177e4SLinus Torvalds 5641da177e4SLinus Torvalds sock->ops = &x25_proto_ops; 5651da177e4SLinus Torvalds sk->sk_protocol = protocol; 5661da177e4SLinus Torvalds sk->sk_backlog_rcv = x25_backlog_rcv; 5671da177e4SLinus Torvalds 5681da177e4SLinus Torvalds x25->t21 = sysctl_x25_call_request_timeout; 5691da177e4SLinus Torvalds x25->t22 = sysctl_x25_reset_request_timeout; 5701da177e4SLinus Torvalds x25->t23 = sysctl_x25_clear_request_timeout; 5711da177e4SLinus Torvalds x25->t2 = sysctl_x25_ack_holdback_timeout; 5721da177e4SLinus Torvalds x25->state = X25_STATE_0; 573cb65d506SShaun Pereira x25->cudmatchlength = 0; 57437cda787Sandrew hendry set_bit(X25_ACCPT_APPRV_FLAG, &x25->flags); /* normally no cud */ 575ebc3f64bSShaun Pereira /* on call accept */ 5761da177e4SLinus Torvalds 5771da177e4SLinus Torvalds x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE; 5781da177e4SLinus Torvalds x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE; 5791da177e4SLinus Torvalds x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE; 5801da177e4SLinus Torvalds x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE; 581ddd0451fSJohn Hughes x25->facilities.throughput = 0; /* by default don't negotiate 582ddd0451fSJohn Hughes throughput */ 5831da177e4SLinus Torvalds x25->facilities.reverse = X25_DEFAULT_REVERSE; 584a64b7b93SShaun Pereira x25->dte_facilities.calling_len = 0; 585a64b7b93SShaun Pereira x25->dte_facilities.called_len = 0; 586a64b7b93SShaun Pereira memset(x25->dte_facilities.called_ae, '\0', 587a64b7b93SShaun Pereira sizeof(x25->dte_facilities.called_ae)); 588a64b7b93SShaun Pereira memset(x25->dte_facilities.calling_ae, '\0', 589a64b7b93SShaun Pereira sizeof(x25->dte_facilities.calling_ae)); 590a64b7b93SShaun Pereira 5911da177e4SLinus Torvalds rc = 0; 5921da177e4SLinus Torvalds out: 5931da177e4SLinus Torvalds return rc; 5941da177e4SLinus Torvalds } 5951da177e4SLinus Torvalds 5961da177e4SLinus Torvalds static struct sock *x25_make_new(struct sock *osk) 5971da177e4SLinus Torvalds { 5981da177e4SLinus Torvalds struct sock *sk = NULL; 5991da177e4SLinus Torvalds struct x25_sock *x25, *ox25; 6001da177e4SLinus Torvalds 6011da177e4SLinus Torvalds if (osk->sk_type != SOCK_SEQPACKET) 6021da177e4SLinus Torvalds goto out; 6031da177e4SLinus Torvalds 60411aa9c28SEric W. Biederman if ((sk = x25_alloc_socket(sock_net(osk), 0)) == NULL) 6051da177e4SLinus Torvalds goto out; 6061da177e4SLinus Torvalds 6071da177e4SLinus Torvalds x25 = x25_sk(sk); 6081da177e4SLinus Torvalds 6091da177e4SLinus Torvalds sk->sk_type = osk->sk_type; 6101da177e4SLinus Torvalds sk->sk_priority = osk->sk_priority; 6111da177e4SLinus Torvalds sk->sk_protocol = osk->sk_protocol; 6121da177e4SLinus Torvalds sk->sk_rcvbuf = osk->sk_rcvbuf; 6131da177e4SLinus Torvalds sk->sk_sndbuf = osk->sk_sndbuf; 6141da177e4SLinus Torvalds sk->sk_state = TCP_ESTABLISHED; 6151da177e4SLinus Torvalds sk->sk_backlog_rcv = osk->sk_backlog_rcv; 616a20a8554SShaun Pereira sock_copy_flags(sk, osk); 6171da177e4SLinus Torvalds 6181da177e4SLinus Torvalds ox25 = x25_sk(osk); 6191da177e4SLinus Torvalds x25->t21 = ox25->t21; 6201da177e4SLinus Torvalds x25->t22 = ox25->t22; 6211da177e4SLinus Torvalds x25->t23 = ox25->t23; 6221da177e4SLinus Torvalds x25->t2 = ox25->t2; 623cb863ffdSandrew hendry x25->flags = ox25->flags; 6241da177e4SLinus Torvalds x25->facilities = ox25->facilities; 625a64b7b93SShaun Pereira x25->dte_facilities = ox25->dte_facilities; 626cb65d506SShaun Pereira x25->cudmatchlength = ox25->cudmatchlength; 6271da177e4SLinus Torvalds 628b7792e34Sandrew hendry clear_bit(X25_INTERRUPT_FLAG, &x25->flags); 6291da177e4SLinus Torvalds x25_init_timers(sk); 6301da177e4SLinus Torvalds out: 6311da177e4SLinus Torvalds return sk; 6321da177e4SLinus Torvalds } 6331da177e4SLinus Torvalds 6341da177e4SLinus Torvalds static int x25_release(struct socket *sock) 6351da177e4SLinus Torvalds { 6361da177e4SLinus Torvalds struct sock *sk = sock->sk; 6371da177e4SLinus Torvalds struct x25_sock *x25; 6381da177e4SLinus Torvalds 6391da177e4SLinus Torvalds if (!sk) 64077b22836SArnd Bergmann return 0; 6411da177e4SLinus Torvalds 6421da177e4SLinus Torvalds x25 = x25_sk(sk); 6431da177e4SLinus Torvalds 64477b22836SArnd Bergmann sock_hold(sk); 64577b22836SArnd Bergmann lock_sock(sk); 6461da177e4SLinus Torvalds switch (x25->state) { 6471da177e4SLinus Torvalds 6481da177e4SLinus Torvalds case X25_STATE_0: 6491da177e4SLinus Torvalds case X25_STATE_2: 6501da177e4SLinus Torvalds x25_disconnect(sk, 0, 0, 0); 65177b22836SArnd Bergmann __x25_destroy_socket(sk); 6521da177e4SLinus Torvalds goto out; 6531da177e4SLinus Torvalds 6541da177e4SLinus Torvalds case X25_STATE_1: 6551da177e4SLinus Torvalds case X25_STATE_3: 6561da177e4SLinus Torvalds case X25_STATE_4: 6571da177e4SLinus Torvalds x25_clear_queues(sk); 6581da177e4SLinus Torvalds x25_write_internal(sk, X25_CLEAR_REQUEST); 6591da177e4SLinus Torvalds x25_start_t23timer(sk); 6601da177e4SLinus Torvalds x25->state = X25_STATE_2; 6611da177e4SLinus Torvalds sk->sk_state = TCP_CLOSE; 6621da177e4SLinus Torvalds sk->sk_shutdown |= SEND_SHUTDOWN; 6631da177e4SLinus Torvalds sk->sk_state_change(sk); 6641da177e4SLinus Torvalds sock_set_flag(sk, SOCK_DEAD); 6651da177e4SLinus Torvalds sock_set_flag(sk, SOCK_DESTROY); 6661da177e4SLinus Torvalds break; 6671da177e4SLinus Torvalds } 6681da177e4SLinus Torvalds 669c751e4f8SDavid S. Miller sock_orphan(sk); 6701da177e4SLinus Torvalds out: 67177b22836SArnd Bergmann release_sock(sk); 67277b22836SArnd Bergmann sock_put(sk); 6731da177e4SLinus Torvalds return 0; 6741da177e4SLinus Torvalds } 6751da177e4SLinus Torvalds 6761da177e4SLinus Torvalds static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len) 6771da177e4SLinus Torvalds { 6781da177e4SLinus Torvalds struct sock *sk = sock->sk; 6791da177e4SLinus Torvalds struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr; 680a9288525Sandrew hendry int len, i, rc = 0; 6811da177e4SLinus Torvalds 682*797a22bdSEric Dumazet if (addr_len != sizeof(struct sockaddr_x25) || 68391774904SArnd Bergmann addr->sx25_family != AF_X25) { 68491774904SArnd Bergmann rc = -EINVAL; 68591774904SArnd Bergmann goto out; 68691774904SArnd Bergmann } 6871da177e4SLinus Torvalds 68806137619SMartin Schiller /* check for the null_x25_address */ 68906137619SMartin Schiller if (strcmp(addr->sx25_addr.x25_addr, null_x25_address.x25_addr)) { 69006137619SMartin Schiller 691a9288525Sandrew hendry len = strlen(addr->sx25_addr.x25_addr); 692a9288525Sandrew hendry for (i = 0; i < len; i++) { 693a9288525Sandrew hendry if (!isdigit(addr->sx25_addr.x25_addr[i])) { 694a9288525Sandrew hendry rc = -EINVAL; 695a9288525Sandrew hendry goto out; 696a9288525Sandrew hendry } 697a9288525Sandrew hendry } 69806137619SMartin Schiller } 699a9288525Sandrew hendry 70090c27297Sandrew hendry lock_sock(sk); 701*797a22bdSEric Dumazet if (sock_flag(sk, SOCK_ZAPPED)) { 7021da177e4SLinus Torvalds x25_sk(sk)->source_addr = addr->sx25_addr; 7031da177e4SLinus Torvalds x25_insert_socket(sk); 7041da177e4SLinus Torvalds sock_reset_flag(sk, SOCK_ZAPPED); 705*797a22bdSEric Dumazet } else { 706*797a22bdSEric Dumazet rc = -EINVAL; 707*797a22bdSEric Dumazet } 70890c27297Sandrew hendry release_sock(sk); 7091da177e4SLinus Torvalds SOCK_DEBUG(sk, "x25_bind: socket is bound\n"); 71091774904SArnd Bergmann out: 71191774904SArnd Bergmann return rc; 7121da177e4SLinus Torvalds } 7131da177e4SLinus Torvalds 7141da177e4SLinus Torvalds static int x25_wait_for_connection_establishment(struct sock *sk) 7151da177e4SLinus Torvalds { 7161da177e4SLinus Torvalds DECLARE_WAITQUEUE(wait, current); 7171da177e4SLinus Torvalds int rc; 7181da177e4SLinus Torvalds 719aa395145SEric Dumazet add_wait_queue_exclusive(sk_sleep(sk), &wait); 7201da177e4SLinus Torvalds for (;;) { 7211da177e4SLinus Torvalds __set_current_state(TASK_INTERRUPTIBLE); 7221da177e4SLinus Torvalds rc = -ERESTARTSYS; 7231da177e4SLinus Torvalds if (signal_pending(current)) 7241da177e4SLinus Torvalds break; 7251da177e4SLinus Torvalds rc = sock_error(sk); 7261da177e4SLinus Torvalds if (rc) { 7271da177e4SLinus Torvalds sk->sk_socket->state = SS_UNCONNECTED; 7281da177e4SLinus Torvalds break; 7291da177e4SLinus Torvalds } 7301da177e4SLinus Torvalds rc = 0; 7311da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED) { 7321da177e4SLinus Torvalds release_sock(sk); 7331da177e4SLinus Torvalds schedule(); 7341da177e4SLinus Torvalds lock_sock(sk); 7351da177e4SLinus Torvalds } else 7361da177e4SLinus Torvalds break; 7371da177e4SLinus Torvalds } 7381da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 739aa395145SEric Dumazet remove_wait_queue(sk_sleep(sk), &wait); 7401da177e4SLinus Torvalds return rc; 7411da177e4SLinus Torvalds } 7421da177e4SLinus Torvalds 7431da177e4SLinus Torvalds static int x25_connect(struct socket *sock, struct sockaddr *uaddr, 7441da177e4SLinus Torvalds int addr_len, int flags) 7451da177e4SLinus Torvalds { 7461da177e4SLinus Torvalds struct sock *sk = sock->sk; 7471da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk); 7481da177e4SLinus Torvalds struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr; 7491da177e4SLinus Torvalds struct x25_route *rt; 7501da177e4SLinus Torvalds int rc = 0; 7511da177e4SLinus Torvalds 7521da177e4SLinus Torvalds lock_sock(sk); 7531da177e4SLinus Torvalds if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) { 7541da177e4SLinus Torvalds sock->state = SS_CONNECTED; 7551da177e4SLinus Torvalds goto out; /* Connect completed during a ERESTARTSYS event */ 7561da177e4SLinus Torvalds } 7571da177e4SLinus Torvalds 7581da177e4SLinus Torvalds rc = -ECONNREFUSED; 7591da177e4SLinus Torvalds if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) { 7601da177e4SLinus Torvalds sock->state = SS_UNCONNECTED; 7611da177e4SLinus Torvalds goto out; 7621da177e4SLinus Torvalds } 7631da177e4SLinus Torvalds 7641da177e4SLinus Torvalds rc = -EISCONN; /* No reconnect on a seqpacket socket */ 7651da177e4SLinus Torvalds if (sk->sk_state == TCP_ESTABLISHED) 7661da177e4SLinus Torvalds goto out; 7671da177e4SLinus Torvalds 7681da177e4SLinus Torvalds sk->sk_state = TCP_CLOSE; 7691da177e4SLinus Torvalds sock->state = SS_UNCONNECTED; 7701da177e4SLinus Torvalds 7711da177e4SLinus Torvalds rc = -EINVAL; 7721da177e4SLinus Torvalds if (addr_len != sizeof(struct sockaddr_x25) || 7731da177e4SLinus Torvalds addr->sx25_family != AF_X25) 7741da177e4SLinus Torvalds goto out; 7751da177e4SLinus Torvalds 7761da177e4SLinus Torvalds rc = -ENETUNREACH; 7771da177e4SLinus Torvalds rt = x25_get_route(&addr->sx25_addr); 7781da177e4SLinus Torvalds if (!rt) 7791da177e4SLinus Torvalds goto out; 7801da177e4SLinus Torvalds 7811da177e4SLinus Torvalds x25->neighbour = x25_get_neigh(rt->dev); 7821da177e4SLinus Torvalds if (!x25->neighbour) 7831da177e4SLinus Torvalds goto out_put_route; 7841da177e4SLinus Torvalds 7851da177e4SLinus Torvalds x25_limit_facilities(&x25->facilities, x25->neighbour); 7861da177e4SLinus Torvalds 7871da177e4SLinus Torvalds x25->lci = x25_new_lci(x25->neighbour); 7881da177e4SLinus Torvalds if (!x25->lci) 7891da177e4SLinus Torvalds goto out_put_neigh; 7901da177e4SLinus Torvalds 7911da177e4SLinus Torvalds rc = -EINVAL; 7921da177e4SLinus Torvalds if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */ 7931da177e4SLinus Torvalds goto out_put_neigh; 7941da177e4SLinus Torvalds 7951da177e4SLinus Torvalds if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr)) 7961da177e4SLinus Torvalds memset(&x25->source_addr, '\0', X25_ADDR_LEN); 7971da177e4SLinus Torvalds 7981da177e4SLinus Torvalds x25->dest_addr = addr->sx25_addr; 7991da177e4SLinus Torvalds 8001da177e4SLinus Torvalds /* Move to connecting socket, start sending Connect Requests */ 8011da177e4SLinus Torvalds sock->state = SS_CONNECTING; 8021da177e4SLinus Torvalds sk->sk_state = TCP_SYN_SENT; 8031da177e4SLinus Torvalds 8041da177e4SLinus Torvalds x25->state = X25_STATE_1; 8051da177e4SLinus Torvalds 8061da177e4SLinus Torvalds x25_write_internal(sk, X25_CALL_REQUEST); 8071da177e4SLinus Torvalds 8081da177e4SLinus Torvalds x25_start_heartbeat(sk); 8091da177e4SLinus Torvalds x25_start_t21timer(sk); 8101da177e4SLinus Torvalds 8111da177e4SLinus Torvalds /* Now the loop */ 8121da177e4SLinus Torvalds rc = -EINPROGRESS; 8131da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK)) 8141da177e4SLinus Torvalds goto out_put_neigh; 8151da177e4SLinus Torvalds 8161da177e4SLinus Torvalds rc = x25_wait_for_connection_establishment(sk); 8171da177e4SLinus Torvalds if (rc) 8181da177e4SLinus Torvalds goto out_put_neigh; 8191da177e4SLinus Torvalds 8201da177e4SLinus Torvalds sock->state = SS_CONNECTED; 8211da177e4SLinus Torvalds rc = 0; 8221da177e4SLinus Torvalds out_put_neigh: 8231da177e4SLinus Torvalds if (rc) 8241da177e4SLinus Torvalds x25_neigh_put(x25->neighbour); 8251da177e4SLinus Torvalds out_put_route: 8261da177e4SLinus Torvalds x25_route_put(rt); 8271da177e4SLinus Torvalds out: 8281da177e4SLinus Torvalds release_sock(sk); 8291da177e4SLinus Torvalds return rc; 8301da177e4SLinus Torvalds } 8311da177e4SLinus Torvalds 832bac37ec8SShaun Pereira static int x25_wait_for_data(struct sock *sk, long timeout) 8331da177e4SLinus Torvalds { 8341da177e4SLinus Torvalds DECLARE_WAITQUEUE(wait, current); 8351da177e4SLinus Torvalds int rc = 0; 8361da177e4SLinus Torvalds 837aa395145SEric Dumazet add_wait_queue_exclusive(sk_sleep(sk), &wait); 8381da177e4SLinus Torvalds for (;;) { 8391da177e4SLinus Torvalds __set_current_state(TASK_INTERRUPTIBLE); 8401da177e4SLinus Torvalds if (sk->sk_shutdown & RCV_SHUTDOWN) 8411da177e4SLinus Torvalds break; 8421da177e4SLinus Torvalds rc = -ERESTARTSYS; 8431da177e4SLinus Torvalds if (signal_pending(current)) 8441da177e4SLinus Torvalds break; 8451da177e4SLinus Torvalds rc = -EAGAIN; 8461da177e4SLinus Torvalds if (!timeout) 8471da177e4SLinus Torvalds break; 8481da177e4SLinus Torvalds rc = 0; 8491da177e4SLinus Torvalds if (skb_queue_empty(&sk->sk_receive_queue)) { 8501da177e4SLinus Torvalds release_sock(sk); 8511da177e4SLinus Torvalds timeout = schedule_timeout(timeout); 8521da177e4SLinus Torvalds lock_sock(sk); 8531da177e4SLinus Torvalds } else 8541da177e4SLinus Torvalds break; 8551da177e4SLinus Torvalds } 8561da177e4SLinus Torvalds __set_current_state(TASK_RUNNING); 857aa395145SEric Dumazet remove_wait_queue(sk_sleep(sk), &wait); 8581da177e4SLinus Torvalds return rc; 8591da177e4SLinus Torvalds } 8601da177e4SLinus Torvalds 861cdfbabfbSDavid Howells static int x25_accept(struct socket *sock, struct socket *newsock, int flags, 862cdfbabfbSDavid Howells bool kern) 8631da177e4SLinus Torvalds { 8641da177e4SLinus Torvalds struct sock *sk = sock->sk; 8651da177e4SLinus Torvalds struct sock *newsk; 8661da177e4SLinus Torvalds struct sk_buff *skb; 8671da177e4SLinus Torvalds int rc = -EINVAL; 8681da177e4SLinus Torvalds 869141646ceSAndrew Hendry if (!sk) 8701da177e4SLinus Torvalds goto out; 8711da177e4SLinus Torvalds 8721da177e4SLinus Torvalds rc = -EOPNOTSUPP; 8731da177e4SLinus Torvalds if (sk->sk_type != SOCK_SEQPACKET) 8741da177e4SLinus Torvalds goto out; 8751da177e4SLinus Torvalds 8761da177e4SLinus Torvalds lock_sock(sk); 877141646ceSAndrew Hendry rc = -EINVAL; 878141646ceSAndrew Hendry if (sk->sk_state != TCP_LISTEN) 879141646ceSAndrew Hendry goto out2; 880141646ceSAndrew Hendry 8811da177e4SLinus Torvalds rc = x25_wait_for_data(sk, sk->sk_rcvtimeo); 8821da177e4SLinus Torvalds if (rc) 8831da177e4SLinus Torvalds goto out2; 8841da177e4SLinus Torvalds skb = skb_dequeue(&sk->sk_receive_queue); 8851da177e4SLinus Torvalds rc = -EINVAL; 8861da177e4SLinus Torvalds if (!skb->sk) 8871da177e4SLinus Torvalds goto out2; 8881da177e4SLinus Torvalds newsk = skb->sk; 889b61d38e0SDavid S. Miller sock_graft(newsk, newsock); 8901da177e4SLinus Torvalds 8911da177e4SLinus Torvalds /* Now attach up the new socket */ 8921da177e4SLinus Torvalds skb->sk = NULL; 8931da177e4SLinus Torvalds kfree_skb(skb); 8941da177e4SLinus Torvalds sk->sk_ack_backlog--; 8951da177e4SLinus Torvalds newsock->state = SS_CONNECTED; 8961da177e4SLinus Torvalds rc = 0; 8971da177e4SLinus Torvalds out2: 8981da177e4SLinus Torvalds release_sock(sk); 8991da177e4SLinus Torvalds out: 9001da177e4SLinus Torvalds return rc; 9011da177e4SLinus Torvalds } 9021da177e4SLinus Torvalds 9031da177e4SLinus Torvalds static int x25_getname(struct socket *sock, struct sockaddr *uaddr, 9049b2c45d4SDenys Vlasenko int peer) 9051da177e4SLinus Torvalds { 9061da177e4SLinus Torvalds struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr; 9071da177e4SLinus Torvalds struct sock *sk = sock->sk; 9081da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk); 90991774904SArnd Bergmann int rc = 0; 9101da177e4SLinus Torvalds 9111da177e4SLinus Torvalds if (peer) { 91291774904SArnd Bergmann if (sk->sk_state != TCP_ESTABLISHED) { 91391774904SArnd Bergmann rc = -ENOTCONN; 91491774904SArnd Bergmann goto out; 91591774904SArnd Bergmann } 9161da177e4SLinus Torvalds sx25->sx25_addr = x25->dest_addr; 9171da177e4SLinus Torvalds } else 9181da177e4SLinus Torvalds sx25->sx25_addr = x25->source_addr; 9191da177e4SLinus Torvalds 9201da177e4SLinus Torvalds sx25->sx25_family = AF_X25; 9219b2c45d4SDenys Vlasenko rc = sizeof(*sx25); 9221da177e4SLinus Torvalds 92391774904SArnd Bergmann out: 92491774904SArnd Bergmann return rc; 92591774904SArnd Bergmann } 92691774904SArnd Bergmann 9271da177e4SLinus Torvalds int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb, 9281da177e4SLinus Torvalds unsigned int lci) 9291da177e4SLinus Torvalds { 9301da177e4SLinus Torvalds struct sock *sk; 9311da177e4SLinus Torvalds struct sock *make; 9321da177e4SLinus Torvalds struct x25_sock *makex25; 9331da177e4SLinus Torvalds struct x25_address source_addr, dest_addr; 9341da177e4SLinus Torvalds struct x25_facilities facilities; 935a64b7b93SShaun Pereira struct x25_dte_facilities dte_facilities; 93695a9dc43SAndrew Hendry int len, addr_len, rc; 9371da177e4SLinus Torvalds 9381da177e4SLinus Torvalds /* 9391da177e4SLinus Torvalds * Remove the LCI and frame type. 9401da177e4SLinus Torvalds */ 9411da177e4SLinus Torvalds skb_pull(skb, X25_STD_MIN_LEN); 9421da177e4SLinus Torvalds 9431da177e4SLinus Torvalds /* 9441da177e4SLinus Torvalds * Extract the X.25 addresses and convert them to ASCII strings, 9451da177e4SLinus Torvalds * and remove them. 946f5eb917bSJohn Hughes * 947f5eb917bSJohn Hughes * Address block is mandatory in call request packets 9481da177e4SLinus Torvalds */ 949f5eb917bSJohn Hughes addr_len = x25_parse_address_block(skb, &source_addr, &dest_addr); 950f5eb917bSJohn Hughes if (addr_len <= 0) 951f5eb917bSJohn Hughes goto out_clear_request; 95295a9dc43SAndrew Hendry skb_pull(skb, addr_len); 9531da177e4SLinus Torvalds 9541da177e4SLinus Torvalds /* 9551da177e4SLinus Torvalds * Get the length of the facilities, skip past them for the moment 9561da177e4SLinus Torvalds * get the call user data because this is needed to determine 9571da177e4SLinus Torvalds * the correct listener 958f5eb917bSJohn Hughes * 959f5eb917bSJohn Hughes * Facilities length is mandatory in call request packets 9601da177e4SLinus Torvalds */ 961cb101ed2SMatthew Daley if (!pskb_may_pull(skb, 1)) 962f5eb917bSJohn Hughes goto out_clear_request; 9631da177e4SLinus Torvalds len = skb->data[0] + 1; 964cb101ed2SMatthew Daley if (!pskb_may_pull(skb, len)) 965f5eb917bSJohn Hughes goto out_clear_request; 9661da177e4SLinus Torvalds skb_pull(skb,len); 9671da177e4SLinus Torvalds 9681da177e4SLinus Torvalds /* 969c7fd0d48SMatthew Daley * Ensure that the amount of call user data is valid. 970c7fd0d48SMatthew Daley */ 971c7fd0d48SMatthew Daley if (skb->len > X25_MAX_CUD_LEN) 972c7fd0d48SMatthew Daley goto out_clear_request; 973c7fd0d48SMatthew Daley 974c7fd0d48SMatthew Daley /* 975cb101ed2SMatthew Daley * Get all the call user data so it can be used in 976cb101ed2SMatthew Daley * x25_find_listener and skb_copy_from_linear_data up ahead. 977cb101ed2SMatthew Daley */ 978cb101ed2SMatthew Daley if (!pskb_may_pull(skb, skb->len)) 979cb101ed2SMatthew Daley goto out_clear_request; 980cb101ed2SMatthew Daley 981cb101ed2SMatthew Daley /* 9821da177e4SLinus Torvalds * Find a listener for the particular address/cud pair. 9831da177e4SLinus Torvalds */ 984cb65d506SShaun Pereira sk = x25_find_listener(&source_addr,skb); 985cb65d506SShaun Pereira skb_push(skb,len); 9861da177e4SLinus Torvalds 98795a9dc43SAndrew Hendry if (sk != NULL && sk_acceptq_is_full(sk)) { 98895a9dc43SAndrew Hendry goto out_sock_put; 98995a9dc43SAndrew Hendry } 99095a9dc43SAndrew Hendry 9911da177e4SLinus Torvalds /* 99295a9dc43SAndrew Hendry * We dont have any listeners for this incoming call. 99395a9dc43SAndrew Hendry * Try forwarding it. 9941da177e4SLinus Torvalds */ 99595a9dc43SAndrew Hendry if (sk == NULL) { 99695a9dc43SAndrew Hendry skb_push(skb, addr_len + X25_STD_MIN_LEN); 99739e21c0dSAndrew Hendry if (sysctl_x25_forward && 99839e21c0dSAndrew Hendry x25_forward_call(&dest_addr, nb, skb, lci) > 0) 99995a9dc43SAndrew Hendry { 100095a9dc43SAndrew Hendry /* Call was forwarded, dont process it any more */ 100195a9dc43SAndrew Hendry kfree_skb(skb); 100295a9dc43SAndrew Hendry rc = 1; 100395a9dc43SAndrew Hendry goto out; 100495a9dc43SAndrew Hendry } else { 100595a9dc43SAndrew Hendry /* No listeners, can't forward, clear the call */ 10061da177e4SLinus Torvalds goto out_clear_request; 100795a9dc43SAndrew Hendry } 100895a9dc43SAndrew Hendry } 10091da177e4SLinus Torvalds 10101da177e4SLinus Torvalds /* 10111da177e4SLinus Torvalds * Try to reach a compromise on the requested facilities. 10121da177e4SLinus Torvalds */ 1013a64b7b93SShaun Pereira len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities); 1014a64b7b93SShaun Pereira if (len == -1) 10151da177e4SLinus Torvalds goto out_sock_put; 10161da177e4SLinus Torvalds 10171da177e4SLinus Torvalds /* 10181da177e4SLinus Torvalds * current neighbour/link might impose additional limits 10191da177e4SLinus Torvalds * on certain facilties 10201da177e4SLinus Torvalds */ 10211da177e4SLinus Torvalds 10221da177e4SLinus Torvalds x25_limit_facilities(&facilities, nb); 10231da177e4SLinus Torvalds 10241da177e4SLinus Torvalds /* 10251da177e4SLinus Torvalds * Try to create a new socket. 10261da177e4SLinus Torvalds */ 10271da177e4SLinus Torvalds make = x25_make_new(sk); 10281da177e4SLinus Torvalds if (!make) 10291da177e4SLinus Torvalds goto out_sock_put; 10301da177e4SLinus Torvalds 10311da177e4SLinus Torvalds /* 10321da177e4SLinus Torvalds * Remove the facilities 10331da177e4SLinus Torvalds */ 10341da177e4SLinus Torvalds skb_pull(skb, len); 10351da177e4SLinus Torvalds 10361da177e4SLinus Torvalds skb->sk = make; 10371da177e4SLinus Torvalds make->sk_state = TCP_ESTABLISHED; 10381da177e4SLinus Torvalds 10391da177e4SLinus Torvalds makex25 = x25_sk(make); 10401da177e4SLinus Torvalds makex25->lci = lci; 10411da177e4SLinus Torvalds makex25->dest_addr = dest_addr; 10421da177e4SLinus Torvalds makex25->source_addr = source_addr; 10431da177e4SLinus Torvalds makex25->neighbour = nb; 10441da177e4SLinus Torvalds makex25->facilities = facilities; 1045a64b7b93SShaun Pereira makex25->dte_facilities= dte_facilities; 10461da177e4SLinus Torvalds makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask; 1047cb65d506SShaun Pereira /* ensure no reverse facil on accept */ 1048cb65d506SShaun Pereira makex25->vc_facil_mask &= ~X25_MASK_REVERSE; 1049a64b7b93SShaun Pereira /* ensure no calling address extension on accept */ 1050a64b7b93SShaun Pereira makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE; 1051cb65d506SShaun Pereira makex25->cudmatchlength = x25_sk(sk)->cudmatchlength; 10521da177e4SLinus Torvalds 105337cda787Sandrew hendry /* Normally all calls are accepted immediately */ 105437cda787Sandrew hendry if (test_bit(X25_ACCPT_APPRV_FLAG, &makex25->flags)) { 10551da177e4SLinus Torvalds x25_write_internal(make, X25_CALL_ACCEPTED); 10561da177e4SLinus Torvalds makex25->state = X25_STATE_3; 1057ebc3f64bSShaun Pereira } 10581da177e4SLinus Torvalds 1059cb65d506SShaun Pereira /* 1060cb65d506SShaun Pereira * Incoming Call User Data. 1061cb65d506SShaun Pereira */ 1062d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, makex25->calluserdata.cuddata, skb->len); 1063cb65d506SShaun Pereira makex25->calluserdata.cudlength = skb->len; 1064cb65d506SShaun Pereira 10651da177e4SLinus Torvalds sk->sk_ack_backlog++; 10661da177e4SLinus Torvalds 10671da177e4SLinus Torvalds x25_insert_socket(make); 10681da177e4SLinus Torvalds 10691da177e4SLinus Torvalds skb_queue_head(&sk->sk_receive_queue, skb); 10701da177e4SLinus Torvalds 10711da177e4SLinus Torvalds x25_start_heartbeat(make); 10721da177e4SLinus Torvalds 10731da177e4SLinus Torvalds if (!sock_flag(sk, SOCK_DEAD)) 1074676d2369SDavid S. Miller sk->sk_data_ready(sk); 10751da177e4SLinus Torvalds rc = 1; 10761da177e4SLinus Torvalds sock_put(sk); 10771da177e4SLinus Torvalds out: 10781da177e4SLinus Torvalds return rc; 10791da177e4SLinus Torvalds out_sock_put: 10801da177e4SLinus Torvalds sock_put(sk); 10811da177e4SLinus Torvalds out_clear_request: 10821da177e4SLinus Torvalds rc = 0; 10831da177e4SLinus Torvalds x25_transmit_clear_request(nb, lci, 0x01); 10841da177e4SLinus Torvalds goto out; 10851da177e4SLinus Torvalds } 10861da177e4SLinus Torvalds 10871b784140SYing Xue static int x25_sendmsg(struct socket *sock, struct msghdr *msg, size_t len) 10881da177e4SLinus Torvalds { 10891da177e4SLinus Torvalds struct sock *sk = sock->sk; 10901da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk); 1091342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_x25 *, usx25, msg->msg_name); 10921da177e4SLinus Torvalds struct sockaddr_x25 sx25; 10931da177e4SLinus Torvalds struct sk_buff *skb; 10941da177e4SLinus Torvalds unsigned char *asmptr; 10951da177e4SLinus Torvalds int noblock = msg->msg_flags & MSG_DONTWAIT; 10961da177e4SLinus Torvalds size_t size; 10971da177e4SLinus Torvalds int qbit = 0, rc = -EINVAL; 10981da177e4SLinus Torvalds 109977b22836SArnd Bergmann lock_sock(sk); 11001da177e4SLinus Torvalds if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT)) 11011da177e4SLinus Torvalds goto out; 11021da177e4SLinus Torvalds 11031da177e4SLinus Torvalds /* we currently don't support segmented records at the user interface */ 11041da177e4SLinus Torvalds if (!(msg->msg_flags & (MSG_EOR|MSG_OOB))) 11051da177e4SLinus Torvalds goto out; 11061da177e4SLinus Torvalds 11071da177e4SLinus Torvalds rc = -EADDRNOTAVAIL; 11081da177e4SLinus Torvalds if (sock_flag(sk, SOCK_ZAPPED)) 11091da177e4SLinus Torvalds goto out; 11101da177e4SLinus Torvalds 11111da177e4SLinus Torvalds rc = -EPIPE; 11121da177e4SLinus Torvalds if (sk->sk_shutdown & SEND_SHUTDOWN) { 11131da177e4SLinus Torvalds send_sig(SIGPIPE, current, 0); 11141da177e4SLinus Torvalds goto out; 11151da177e4SLinus Torvalds } 11161da177e4SLinus Torvalds 11171da177e4SLinus Torvalds rc = -ENETUNREACH; 11181da177e4SLinus Torvalds if (!x25->neighbour) 11191da177e4SLinus Torvalds goto out; 11201da177e4SLinus Torvalds 11211da177e4SLinus Torvalds if (usx25) { 11221da177e4SLinus Torvalds rc = -EINVAL; 11231da177e4SLinus Torvalds if (msg->msg_namelen < sizeof(sx25)) 11241da177e4SLinus Torvalds goto out; 11251da177e4SLinus Torvalds memcpy(&sx25, usx25, sizeof(sx25)); 11261da177e4SLinus Torvalds rc = -EISCONN; 11271da177e4SLinus Torvalds if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr)) 11281da177e4SLinus Torvalds goto out; 11291da177e4SLinus Torvalds rc = -EINVAL; 11301da177e4SLinus Torvalds if (sx25.sx25_family != AF_X25) 11311da177e4SLinus Torvalds goto out; 11321da177e4SLinus Torvalds } else { 11331da177e4SLinus Torvalds /* 11341da177e4SLinus Torvalds * FIXME 1003.1g - if the socket is like this because 11351da177e4SLinus Torvalds * it has become closed (not started closed) we ought 11361da177e4SLinus Torvalds * to SIGPIPE, EPIPE; 11371da177e4SLinus Torvalds */ 11381da177e4SLinus Torvalds rc = -ENOTCONN; 11391da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED) 11401da177e4SLinus Torvalds goto out; 11411da177e4SLinus Torvalds 11421da177e4SLinus Torvalds sx25.sx25_family = AF_X25; 11431da177e4SLinus Torvalds sx25.sx25_addr = x25->dest_addr; 11441da177e4SLinus Torvalds } 11451da177e4SLinus Torvalds 114683e0bbcbSAlan Cox /* Sanity check the packet size */ 114783e0bbcbSAlan Cox if (len > 65535) { 114883e0bbcbSAlan Cox rc = -EMSGSIZE; 114983e0bbcbSAlan Cox goto out; 115083e0bbcbSAlan Cox } 115183e0bbcbSAlan Cox 11521da177e4SLinus Torvalds SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n"); 11531da177e4SLinus Torvalds 11541da177e4SLinus Torvalds /* Build a packet */ 11551da177e4SLinus Torvalds SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n"); 11561da177e4SLinus Torvalds 11571da177e4SLinus Torvalds if ((msg->msg_flags & MSG_OOB) && len > 32) 11581da177e4SLinus Torvalds len = 32; 11591da177e4SLinus Torvalds 11601da177e4SLinus Torvalds size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN; 11611da177e4SLinus Torvalds 116277b22836SArnd Bergmann release_sock(sk); 11631da177e4SLinus Torvalds skb = sock_alloc_send_skb(sk, size, noblock, &rc); 116477b22836SArnd Bergmann lock_sock(sk); 11651da177e4SLinus Torvalds if (!skb) 11661da177e4SLinus Torvalds goto out; 11671da177e4SLinus Torvalds X25_SKB_CB(skb)->flags = msg->msg_flags; 11681da177e4SLinus Torvalds 11691da177e4SLinus Torvalds skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN); 11701da177e4SLinus Torvalds 11711da177e4SLinus Torvalds /* 11721da177e4SLinus Torvalds * Put the data on the end 11731da177e4SLinus Torvalds */ 11741da177e4SLinus Torvalds SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n"); 11751da177e4SLinus Torvalds 1176eeeb0374SArnaldo Carvalho de Melo skb_reset_transport_header(skb); 1177eeeb0374SArnaldo Carvalho de Melo skb_put(skb, len); 11781da177e4SLinus Torvalds 11796ce8e9ceSAl Viro rc = memcpy_from_msg(skb_transport_header(skb), msg, len); 11801da177e4SLinus Torvalds if (rc) 11811da177e4SLinus Torvalds goto out_kfree_skb; 11821da177e4SLinus Torvalds 11831da177e4SLinus Torvalds /* 11841da177e4SLinus Torvalds * If the Q BIT Include socket option is in force, the first 11851da177e4SLinus Torvalds * byte of the user data is the logical value of the Q Bit. 11861da177e4SLinus Torvalds */ 1187cb863ffdSandrew hendry if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) { 1188cb101ed2SMatthew Daley if (!pskb_may_pull(skb, 1)) 1189cb101ed2SMatthew Daley goto out_kfree_skb; 1190cb101ed2SMatthew Daley 11911da177e4SLinus Torvalds qbit = skb->data[0]; 11921da177e4SLinus Torvalds skb_pull(skb, 1); 11931da177e4SLinus Torvalds } 11941da177e4SLinus Torvalds 11951da177e4SLinus Torvalds /* 11961da177e4SLinus Torvalds * Push down the X.25 header 11971da177e4SLinus Torvalds */ 11981da177e4SLinus Torvalds SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n"); 11991da177e4SLinus Torvalds 12001da177e4SLinus Torvalds if (msg->msg_flags & MSG_OOB) { 12011da177e4SLinus Torvalds if (x25->neighbour->extended) { 12021da177e4SLinus Torvalds asmptr = skb_push(skb, X25_STD_MIN_LEN); 12031da177e4SLinus Torvalds *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ; 12041da177e4SLinus Torvalds *asmptr++ = (x25->lci >> 0) & 0xFF; 12051da177e4SLinus Torvalds *asmptr++ = X25_INTERRUPT; 12061da177e4SLinus Torvalds } else { 12071da177e4SLinus Torvalds asmptr = skb_push(skb, X25_STD_MIN_LEN); 12081da177e4SLinus Torvalds *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ; 12091da177e4SLinus Torvalds *asmptr++ = (x25->lci >> 0) & 0xFF; 12101da177e4SLinus Torvalds *asmptr++ = X25_INTERRUPT; 12111da177e4SLinus Torvalds } 12121da177e4SLinus Torvalds } else { 12131da177e4SLinus Torvalds if (x25->neighbour->extended) { 12141da177e4SLinus Torvalds /* Build an Extended X.25 header */ 12151da177e4SLinus Torvalds asmptr = skb_push(skb, X25_EXT_MIN_LEN); 12161da177e4SLinus Torvalds *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ; 12171da177e4SLinus Torvalds *asmptr++ = (x25->lci >> 0) & 0xFF; 12181da177e4SLinus Torvalds *asmptr++ = X25_DATA; 12191da177e4SLinus Torvalds *asmptr++ = X25_DATA; 12201da177e4SLinus Torvalds } else { 12211da177e4SLinus Torvalds /* Build an Standard X.25 header */ 12221da177e4SLinus Torvalds asmptr = skb_push(skb, X25_STD_MIN_LEN); 12231da177e4SLinus Torvalds *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ; 12241da177e4SLinus Torvalds *asmptr++ = (x25->lci >> 0) & 0xFF; 12251da177e4SLinus Torvalds *asmptr++ = X25_DATA; 12261da177e4SLinus Torvalds } 12271da177e4SLinus Torvalds 12281da177e4SLinus Torvalds if (qbit) 12291da177e4SLinus Torvalds skb->data[0] |= X25_Q_BIT; 12301da177e4SLinus Torvalds } 12311da177e4SLinus Torvalds 12321da177e4SLinus Torvalds SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n"); 12331da177e4SLinus Torvalds SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n"); 12341da177e4SLinus Torvalds 12351da177e4SLinus Torvalds rc = -ENOTCONN; 12361da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED) 12371da177e4SLinus Torvalds goto out_kfree_skb; 12381da177e4SLinus Torvalds 12391da177e4SLinus Torvalds if (msg->msg_flags & MSG_OOB) 12401da177e4SLinus Torvalds skb_queue_tail(&x25->interrupt_out_queue, skb); 12411da177e4SLinus Torvalds else { 12428db09f26SRoel Kluin rc = x25_output(sk, skb); 12438db09f26SRoel Kluin len = rc; 12448db09f26SRoel Kluin if (rc < 0) 12451da177e4SLinus Torvalds kfree_skb(skb); 1246cb863ffdSandrew hendry else if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) 12471da177e4SLinus Torvalds len++; 12481da177e4SLinus Torvalds } 12491da177e4SLinus Torvalds 12501da177e4SLinus Torvalds x25_kick(sk); 12511da177e4SLinus Torvalds rc = len; 12521da177e4SLinus Torvalds out: 125377b22836SArnd Bergmann release_sock(sk); 12541da177e4SLinus Torvalds return rc; 12551da177e4SLinus Torvalds out_kfree_skb: 12561da177e4SLinus Torvalds kfree_skb(skb); 12571da177e4SLinus Torvalds goto out; 12581da177e4SLinus Torvalds } 12591da177e4SLinus Torvalds 12601da177e4SLinus Torvalds 12611b784140SYing Xue static int x25_recvmsg(struct socket *sock, struct msghdr *msg, size_t size, 12621da177e4SLinus Torvalds int flags) 12631da177e4SLinus Torvalds { 12641da177e4SLinus Torvalds struct sock *sk = sock->sk; 12651da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk); 1266342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_x25 *, sx25, msg->msg_name); 12671da177e4SLinus Torvalds size_t copied; 1268501e89d3SDave Jones int qbit, header_len; 12691da177e4SLinus Torvalds struct sk_buff *skb; 12701da177e4SLinus Torvalds unsigned char *asmptr; 12711da177e4SLinus Torvalds int rc = -ENOTCONN; 12721da177e4SLinus Torvalds 127377b22836SArnd Bergmann lock_sock(sk); 1274501e89d3SDave Jones 1275501e89d3SDave Jones if (x25->neighbour == NULL) 1276501e89d3SDave Jones goto out; 1277501e89d3SDave Jones 1278501e89d3SDave Jones header_len = x25->neighbour->extended ? 1279501e89d3SDave Jones X25_EXT_MIN_LEN : X25_STD_MIN_LEN; 1280501e89d3SDave Jones 12811da177e4SLinus Torvalds /* 12821da177e4SLinus Torvalds * This works for seqpacket too. The receiver has ordered the queue for 12831da177e4SLinus Torvalds * us! We do one quick check first though 12841da177e4SLinus Torvalds */ 12851da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED) 12861da177e4SLinus Torvalds goto out; 12871da177e4SLinus Torvalds 12881da177e4SLinus Torvalds if (flags & MSG_OOB) { 12891da177e4SLinus Torvalds rc = -EINVAL; 12901da177e4SLinus Torvalds if (sock_flag(sk, SOCK_URGINLINE) || 12911da177e4SLinus Torvalds !skb_peek(&x25->interrupt_in_queue)) 12921da177e4SLinus Torvalds goto out; 12931da177e4SLinus Torvalds 12941da177e4SLinus Torvalds skb = skb_dequeue(&x25->interrupt_in_queue); 12951da177e4SLinus Torvalds 1296cb101ed2SMatthew Daley if (!pskb_may_pull(skb, X25_STD_MIN_LEN)) 1297cb101ed2SMatthew Daley goto out_free_dgram; 1298cb101ed2SMatthew Daley 12991da177e4SLinus Torvalds skb_pull(skb, X25_STD_MIN_LEN); 13001da177e4SLinus Torvalds 13011da177e4SLinus Torvalds /* 13021da177e4SLinus Torvalds * No Q bit information on Interrupt data. 13031da177e4SLinus Torvalds */ 1304cb863ffdSandrew hendry if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) { 13051da177e4SLinus Torvalds asmptr = skb_push(skb, 1); 13061da177e4SLinus Torvalds *asmptr = 0x00; 13071da177e4SLinus Torvalds } 13081da177e4SLinus Torvalds 13091da177e4SLinus Torvalds msg->msg_flags |= MSG_OOB; 13101da177e4SLinus Torvalds } else { 13111da177e4SLinus Torvalds /* Now we can treat all alike */ 131277b22836SArnd Bergmann release_sock(sk); 13131da177e4SLinus Torvalds skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT, 13141da177e4SLinus Torvalds flags & MSG_DONTWAIT, &rc); 131577b22836SArnd Bergmann lock_sock(sk); 13161da177e4SLinus Torvalds if (!skb) 13171da177e4SLinus Torvalds goto out; 13181da177e4SLinus Torvalds 1319cb101ed2SMatthew Daley if (!pskb_may_pull(skb, header_len)) 1320cb101ed2SMatthew Daley goto out_free_dgram; 1321cb101ed2SMatthew Daley 13221da177e4SLinus Torvalds qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT; 13231da177e4SLinus Torvalds 1324cb101ed2SMatthew Daley skb_pull(skb, header_len); 13251da177e4SLinus Torvalds 1326cb863ffdSandrew hendry if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) { 13271da177e4SLinus Torvalds asmptr = skb_push(skb, 1); 13281da177e4SLinus Torvalds *asmptr = qbit; 13291da177e4SLinus Torvalds } 13301da177e4SLinus Torvalds } 13311da177e4SLinus Torvalds 1332badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skb); 13331da177e4SLinus Torvalds copied = skb->len; 13341da177e4SLinus Torvalds 13351da177e4SLinus Torvalds if (copied > size) { 13361da177e4SLinus Torvalds copied = size; 13371da177e4SLinus Torvalds msg->msg_flags |= MSG_TRUNC; 13381da177e4SLinus Torvalds } 13391da177e4SLinus Torvalds 13401da177e4SLinus Torvalds /* Currently, each datagram always contains a complete record */ 13411da177e4SLinus Torvalds msg->msg_flags |= MSG_EOR; 13421da177e4SLinus Torvalds 134351f3d02bSDavid S. Miller rc = skb_copy_datagram_msg(skb, 0, msg, copied); 13441da177e4SLinus Torvalds if (rc) 13451da177e4SLinus Torvalds goto out_free_dgram; 13461da177e4SLinus Torvalds 13471da177e4SLinus Torvalds if (sx25) { 13481da177e4SLinus Torvalds sx25->sx25_family = AF_X25; 13491da177e4SLinus Torvalds sx25->sx25_addr = x25->dest_addr; 1350f3d33426SHannes Frederic Sowa msg->msg_namelen = sizeof(*sx25); 13511da177e4SLinus Torvalds } 13521da177e4SLinus Torvalds 13531da177e4SLinus Torvalds x25_check_rbuf(sk); 13541da177e4SLinus Torvalds rc = copied; 13551da177e4SLinus Torvalds out_free_dgram: 13561da177e4SLinus Torvalds skb_free_datagram(sk, skb); 13571da177e4SLinus Torvalds out: 135877b22836SArnd Bergmann release_sock(sk); 13591da177e4SLinus Torvalds return rc; 13601da177e4SLinus Torvalds } 13611da177e4SLinus Torvalds 13621da177e4SLinus Torvalds 13631da177e4SLinus Torvalds static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg) 13641da177e4SLinus Torvalds { 13651da177e4SLinus Torvalds struct sock *sk = sock->sk; 13661da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk); 13671da177e4SLinus Torvalds void __user *argp = (void __user *)arg; 13681da177e4SLinus Torvalds int rc; 13691da177e4SLinus Torvalds 13701da177e4SLinus Torvalds switch (cmd) { 13711da177e4SLinus Torvalds case TIOCOUTQ: { 137270be998cSandrew hendry int amount; 137331e6d363SEric Dumazet 137470be998cSandrew hendry amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk); 13751da177e4SLinus Torvalds if (amount < 0) 13761da177e4SLinus Torvalds amount = 0; 13771da177e4SLinus Torvalds rc = put_user(amount, (unsigned int __user *)argp); 13781da177e4SLinus Torvalds break; 13791da177e4SLinus Torvalds } 13801da177e4SLinus Torvalds 13811da177e4SLinus Torvalds case TIOCINQ: { 13821da177e4SLinus Torvalds struct sk_buff *skb; 13831da177e4SLinus Torvalds int amount = 0; 13841da177e4SLinus Torvalds /* 13851da177e4SLinus Torvalds * These two are safe on a single CPU system as 13861da177e4SLinus Torvalds * only user tasks fiddle here 13871da177e4SLinus Torvalds */ 138854aafbd4Sandrew hendry lock_sock(sk); 13891da177e4SLinus Torvalds if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL) 13901da177e4SLinus Torvalds amount = skb->len; 139154aafbd4Sandrew hendry release_sock(sk); 13921da177e4SLinus Torvalds rc = put_user(amount, (unsigned int __user *)argp); 13931da177e4SLinus Torvalds break; 13941da177e4SLinus Torvalds } 13951da177e4SLinus Torvalds 13961da177e4SLinus Torvalds case SIOCGSTAMP: 13971da177e4SLinus Torvalds rc = -EINVAL; 13981da177e4SLinus Torvalds if (sk) 13991da177e4SLinus Torvalds rc = sock_get_timestamp(sk, 14001da177e4SLinus Torvalds (struct timeval __user *)argp); 14011da177e4SLinus Torvalds break; 1402ae40eb1eSEric Dumazet case SIOCGSTAMPNS: 1403ae40eb1eSEric Dumazet rc = -EINVAL; 1404ae40eb1eSEric Dumazet if (sk) 1405ae40eb1eSEric Dumazet rc = sock_get_timestampns(sk, 1406ae40eb1eSEric Dumazet (struct timespec __user *)argp); 1407ae40eb1eSEric Dumazet break; 14081da177e4SLinus Torvalds case SIOCGIFADDR: 14091da177e4SLinus Torvalds case SIOCSIFADDR: 14101da177e4SLinus Torvalds case SIOCGIFDSTADDR: 14111da177e4SLinus Torvalds case SIOCSIFDSTADDR: 14121da177e4SLinus Torvalds case SIOCGIFBRDADDR: 14131da177e4SLinus Torvalds case SIOCSIFBRDADDR: 14141da177e4SLinus Torvalds case SIOCGIFNETMASK: 14151da177e4SLinus Torvalds case SIOCSIFNETMASK: 14161da177e4SLinus Torvalds case SIOCGIFMETRIC: 14171da177e4SLinus Torvalds case SIOCSIFMETRIC: 14181da177e4SLinus Torvalds rc = -EINVAL; 14191da177e4SLinus Torvalds break; 14201da177e4SLinus Torvalds case SIOCADDRT: 14211da177e4SLinus Torvalds case SIOCDELRT: 14221da177e4SLinus Torvalds rc = -EPERM; 14231da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 14241da177e4SLinus Torvalds break; 14251da177e4SLinus Torvalds rc = x25_route_ioctl(cmd, argp); 14261da177e4SLinus Torvalds break; 14271da177e4SLinus Torvalds case SIOCX25GSUBSCRIP: 14281da177e4SLinus Torvalds rc = x25_subscr_ioctl(cmd, argp); 14291da177e4SLinus Torvalds break; 14301da177e4SLinus Torvalds case SIOCX25SSUBSCRIP: 14311da177e4SLinus Torvalds rc = -EPERM; 14321da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN)) 14331da177e4SLinus Torvalds break; 14341da177e4SLinus Torvalds rc = x25_subscr_ioctl(cmd, argp); 14351da177e4SLinus Torvalds break; 14361da177e4SLinus Torvalds case SIOCX25GFACILITIES: { 1437f90de660Sandrew hendry lock_sock(sk); 1438f90de660Sandrew hendry rc = copy_to_user(argp, &x25->facilities, 1439f90de660Sandrew hendry sizeof(x25->facilities)) 1440f90de660Sandrew hendry ? -EFAULT : 0; 1441f90de660Sandrew hendry release_sock(sk); 14421da177e4SLinus Torvalds break; 14431da177e4SLinus Torvalds } 14441da177e4SLinus Torvalds 14451da177e4SLinus Torvalds case SIOCX25SFACILITIES: { 14461da177e4SLinus Torvalds struct x25_facilities facilities; 14471da177e4SLinus Torvalds rc = -EFAULT; 1448fddc5f3eSJoe Perches if (copy_from_user(&facilities, argp, sizeof(facilities))) 14491da177e4SLinus Torvalds break; 14501da177e4SLinus Torvalds rc = -EINVAL; 1451f90de660Sandrew hendry lock_sock(sk); 14521da177e4SLinus Torvalds if (sk->sk_state != TCP_LISTEN && 14531da177e4SLinus Torvalds sk->sk_state != TCP_CLOSE) 1454f90de660Sandrew hendry goto out_fac_release; 14551da177e4SLinus Torvalds if (facilities.pacsize_in < X25_PS16 || 14561da177e4SLinus Torvalds facilities.pacsize_in > X25_PS4096) 1457f90de660Sandrew hendry goto out_fac_release; 14581da177e4SLinus Torvalds if (facilities.pacsize_out < X25_PS16 || 14591da177e4SLinus Torvalds facilities.pacsize_out > X25_PS4096) 1460f90de660Sandrew hendry goto out_fac_release; 14611da177e4SLinus Torvalds if (facilities.winsize_in < 1 || 14621da177e4SLinus Torvalds facilities.winsize_in > 127) 1463f90de660Sandrew hendry goto out_fac_release; 1464ddd0451fSJohn Hughes if (facilities.throughput) { 1465ddd0451fSJohn Hughes int out = facilities.throughput & 0xf0; 1466ddd0451fSJohn Hughes int in = facilities.throughput & 0x0f; 1467ddd0451fSJohn Hughes if (!out) 1468ddd0451fSJohn Hughes facilities.throughput |= 1469ddd0451fSJohn Hughes X25_DEFAULT_THROUGHPUT << 4; 1470ddd0451fSJohn Hughes else if (out < 0x30 || out > 0xD0) 1471f90de660Sandrew hendry goto out_fac_release; 1472ddd0451fSJohn Hughes if (!in) 1473ddd0451fSJohn Hughes facilities.throughput |= 1474ddd0451fSJohn Hughes X25_DEFAULT_THROUGHPUT; 1475ddd0451fSJohn Hughes else if (in < 0x03 || in > 0x0D) 1476f90de660Sandrew hendry goto out_fac_release; 1477ddd0451fSJohn Hughes } 1478ebc3f64bSShaun Pereira if (facilities.reverse && 147906a96b33Sroel kluin (facilities.reverse & 0x81) != 0x81) 1480f90de660Sandrew hendry goto out_fac_release; 14811da177e4SLinus Torvalds x25->facilities = facilities; 14821da177e4SLinus Torvalds rc = 0; 1483f90de660Sandrew hendry out_fac_release: 1484f90de660Sandrew hendry release_sock(sk); 14851da177e4SLinus Torvalds break; 14861da177e4SLinus Torvalds } 14871da177e4SLinus Torvalds 1488a64b7b93SShaun Pereira case SIOCX25GDTEFACILITIES: { 1489f90de660Sandrew hendry lock_sock(sk); 1490a64b7b93SShaun Pereira rc = copy_to_user(argp, &x25->dte_facilities, 1491a64b7b93SShaun Pereira sizeof(x25->dte_facilities)); 1492f90de660Sandrew hendry release_sock(sk); 1493a64b7b93SShaun Pereira if (rc) 1494a64b7b93SShaun Pereira rc = -EFAULT; 1495a64b7b93SShaun Pereira break; 1496a64b7b93SShaun Pereira } 1497a64b7b93SShaun Pereira 1498a64b7b93SShaun Pereira case SIOCX25SDTEFACILITIES: { 1499a64b7b93SShaun Pereira struct x25_dte_facilities dtefacs; 1500a64b7b93SShaun Pereira rc = -EFAULT; 1501a64b7b93SShaun Pereira if (copy_from_user(&dtefacs, argp, sizeof(dtefacs))) 1502a64b7b93SShaun Pereira break; 1503a64b7b93SShaun Pereira rc = -EINVAL; 1504f90de660Sandrew hendry lock_sock(sk); 1505a64b7b93SShaun Pereira if (sk->sk_state != TCP_LISTEN && 1506a64b7b93SShaun Pereira sk->sk_state != TCP_CLOSE) 1507f90de660Sandrew hendry goto out_dtefac_release; 1508a64b7b93SShaun Pereira if (dtefacs.calling_len > X25_MAX_AE_LEN) 1509f90de660Sandrew hendry goto out_dtefac_release; 1510a64b7b93SShaun Pereira if (dtefacs.called_len > X25_MAX_AE_LEN) 1511f90de660Sandrew hendry goto out_dtefac_release; 1512a64b7b93SShaun Pereira x25->dte_facilities = dtefacs; 1513a64b7b93SShaun Pereira rc = 0; 1514f90de660Sandrew hendry out_dtefac_release: 1515f90de660Sandrew hendry release_sock(sk); 1516a64b7b93SShaun Pereira break; 1517a64b7b93SShaun Pereira } 1518a64b7b93SShaun Pereira 15191da177e4SLinus Torvalds case SIOCX25GCALLUSERDATA: { 15205b7958dfSandrew hendry lock_sock(sk); 15215b7958dfSandrew hendry rc = copy_to_user(argp, &x25->calluserdata, 15225b7958dfSandrew hendry sizeof(x25->calluserdata)) 15235b7958dfSandrew hendry ? -EFAULT : 0; 15245b7958dfSandrew hendry release_sock(sk); 15251da177e4SLinus Torvalds break; 15261da177e4SLinus Torvalds } 15271da177e4SLinus Torvalds 15281da177e4SLinus Torvalds case SIOCX25SCALLUSERDATA: { 15291da177e4SLinus Torvalds struct x25_calluserdata calluserdata; 15301da177e4SLinus Torvalds 15311da177e4SLinus Torvalds rc = -EFAULT; 1532fddc5f3eSJoe Perches if (copy_from_user(&calluserdata, argp, sizeof(calluserdata))) 15331da177e4SLinus Torvalds break; 15341da177e4SLinus Torvalds rc = -EINVAL; 15351da177e4SLinus Torvalds if (calluserdata.cudlength > X25_MAX_CUD_LEN) 15361da177e4SLinus Torvalds break; 15375b7958dfSandrew hendry lock_sock(sk); 15381da177e4SLinus Torvalds x25->calluserdata = calluserdata; 15395b7958dfSandrew hendry release_sock(sk); 15401da177e4SLinus Torvalds rc = 0; 15411da177e4SLinus Torvalds break; 15421da177e4SLinus Torvalds } 15431da177e4SLinus Torvalds 15441da177e4SLinus Torvalds case SIOCX25GCAUSEDIAG: { 154574a7e440Sandrew hendry lock_sock(sk); 1546fddc5f3eSJoe Perches rc = copy_to_user(argp, &x25->causediag, sizeof(x25->causediag)) 154774a7e440Sandrew hendry ? -EFAULT : 0; 154874a7e440Sandrew hendry release_sock(sk); 15491da177e4SLinus Torvalds break; 15501da177e4SLinus Torvalds } 15511da177e4SLinus Torvalds 1552386e50ccSAndrew Hendry case SIOCX25SCAUSEDIAG: { 1553386e50ccSAndrew Hendry struct x25_causediag causediag; 1554386e50ccSAndrew Hendry rc = -EFAULT; 1555386e50ccSAndrew Hendry if (copy_from_user(&causediag, argp, sizeof(causediag))) 1556386e50ccSAndrew Hendry break; 155774a7e440Sandrew hendry lock_sock(sk); 1558386e50ccSAndrew Hendry x25->causediag = causediag; 155974a7e440Sandrew hendry release_sock(sk); 1560386e50ccSAndrew Hendry rc = 0; 1561386e50ccSAndrew Hendry break; 1562386e50ccSAndrew Hendry 1563386e50ccSAndrew Hendry } 1564386e50ccSAndrew Hendry 1565cb65d506SShaun Pereira case SIOCX25SCUDMATCHLEN: { 1566cb65d506SShaun Pereira struct x25_subaddr sub_addr; 1567cb65d506SShaun Pereira rc = -EINVAL; 15683f0a069aSandrew hendry lock_sock(sk); 1569cb65d506SShaun Pereira if(sk->sk_state != TCP_CLOSE) 15703f0a069aSandrew hendry goto out_cud_release; 1571cb65d506SShaun Pereira rc = -EFAULT; 1572cb65d506SShaun Pereira if (copy_from_user(&sub_addr, argp, 1573cb65d506SShaun Pereira sizeof(sub_addr))) 15743f0a069aSandrew hendry goto out_cud_release; 1575cb65d506SShaun Pereira rc = -EINVAL; 1576cb65d506SShaun Pereira if (sub_addr.cudmatchlength > X25_MAX_CUD_LEN) 15773f0a069aSandrew hendry goto out_cud_release; 1578cb65d506SShaun Pereira x25->cudmatchlength = sub_addr.cudmatchlength; 1579cb65d506SShaun Pereira rc = 0; 15803f0a069aSandrew hendry out_cud_release: 15813f0a069aSandrew hendry release_sock(sk); 1582cb65d506SShaun Pereira break; 1583cb65d506SShaun Pereira } 1584cb65d506SShaun Pereira 1585ebc3f64bSShaun Pereira case SIOCX25CALLACCPTAPPRV: { 1586ebc3f64bSShaun Pereira rc = -EINVAL; 158777b22836SArnd Bergmann lock_sock(sk); 15884ccb93ceSDave Jones if (sk->sk_state == TCP_CLOSE) { 158937cda787Sandrew hendry clear_bit(X25_ACCPT_APPRV_FLAG, &x25->flags); 1590ebc3f64bSShaun Pereira rc = 0; 15914ccb93ceSDave Jones } 15924ccb93ceSDave Jones release_sock(sk); 1593ebc3f64bSShaun Pereira break; 1594ebc3f64bSShaun Pereira } 1595ebc3f64bSShaun Pereira 1596ebc3f64bSShaun Pereira case SIOCX25SENDCALLACCPT: { 1597ebc3f64bSShaun Pereira rc = -EINVAL; 159877b22836SArnd Bergmann lock_sock(sk); 1599ebc3f64bSShaun Pereira if (sk->sk_state != TCP_ESTABLISHED) 16004ccb93ceSDave Jones goto out_sendcallaccpt_release; 160137cda787Sandrew hendry /* must call accptapprv above */ 160237cda787Sandrew hendry if (test_bit(X25_ACCPT_APPRV_FLAG, &x25->flags)) 16034ccb93ceSDave Jones goto out_sendcallaccpt_release; 1604ebc3f64bSShaun Pereira x25_write_internal(sk, X25_CALL_ACCEPTED); 1605ebc3f64bSShaun Pereira x25->state = X25_STATE_3; 1606ebc3f64bSShaun Pereira rc = 0; 16074ccb93ceSDave Jones out_sendcallaccpt_release: 16084ccb93ceSDave Jones release_sock(sk); 1609ebc3f64bSShaun Pereira break; 1610ebc3f64bSShaun Pereira } 1611ebc3f64bSShaun Pereira 16121da177e4SLinus Torvalds default: 1613b5e5fa5eSChristoph Hellwig rc = -ENOIOCTLCMD; 16141da177e4SLinus Torvalds break; 16151da177e4SLinus Torvalds } 16161da177e4SLinus Torvalds 16171da177e4SLinus Torvalds return rc; 16181da177e4SLinus Torvalds } 16191da177e4SLinus Torvalds 1620ec1b4cf7SStephen Hemminger static const struct net_proto_family x25_family_ops = { 16211da177e4SLinus Torvalds .family = AF_X25, 16221da177e4SLinus Torvalds .create = x25_create, 16231da177e4SLinus Torvalds .owner = THIS_MODULE, 16241da177e4SLinus Torvalds }; 16251da177e4SLinus Torvalds 16261b06e6baSShaun Pereira #ifdef CONFIG_COMPAT 16271b06e6baSShaun Pereira static int compat_x25_subscr_ioctl(unsigned int cmd, 16281b06e6baSShaun Pereira struct compat_x25_subscrip_struct __user *x25_subscr32) 16291b06e6baSShaun Pereira { 16301b06e6baSShaun Pereira struct compat_x25_subscrip_struct x25_subscr; 16311b06e6baSShaun Pereira struct x25_neigh *nb; 16321b06e6baSShaun Pereira struct net_device *dev; 16331b06e6baSShaun Pereira int rc = -EINVAL; 16341b06e6baSShaun Pereira 16351b06e6baSShaun Pereira rc = -EFAULT; 16361b06e6baSShaun Pereira if (copy_from_user(&x25_subscr, x25_subscr32, sizeof(*x25_subscr32))) 16371b06e6baSShaun Pereira goto out; 16381b06e6baSShaun Pereira 16391b06e6baSShaun Pereira rc = -EINVAL; 16401b06e6baSShaun Pereira dev = x25_dev_get(x25_subscr.device); 16411b06e6baSShaun Pereira if (dev == NULL) 16421b06e6baSShaun Pereira goto out; 16431b06e6baSShaun Pereira 16441b06e6baSShaun Pereira nb = x25_get_neigh(dev); 16451b06e6baSShaun Pereira if (nb == NULL) 16461b06e6baSShaun Pereira goto out_dev_put; 16471b06e6baSShaun Pereira 16481b06e6baSShaun Pereira dev_put(dev); 16491b06e6baSShaun Pereira 16501b06e6baSShaun Pereira if (cmd == SIOCX25GSUBSCRIP) { 16515595a1a5Sandrew hendry read_lock_bh(&x25_neigh_list_lock); 16521b06e6baSShaun Pereira x25_subscr.extended = nb->extended; 16531b06e6baSShaun Pereira x25_subscr.global_facil_mask = nb->global_facil_mask; 16545595a1a5Sandrew hendry read_unlock_bh(&x25_neigh_list_lock); 16551b06e6baSShaun Pereira rc = copy_to_user(x25_subscr32, &x25_subscr, 16561b06e6baSShaun Pereira sizeof(*x25_subscr32)) ? -EFAULT : 0; 16571b06e6baSShaun Pereira } else { 16581b06e6baSShaun Pereira rc = -EINVAL; 16591b06e6baSShaun Pereira if (x25_subscr.extended == 0 || x25_subscr.extended == 1) { 16601b06e6baSShaun Pereira rc = 0; 16615595a1a5Sandrew hendry write_lock_bh(&x25_neigh_list_lock); 16621b06e6baSShaun Pereira nb->extended = x25_subscr.extended; 16631b06e6baSShaun Pereira nb->global_facil_mask = x25_subscr.global_facil_mask; 16645595a1a5Sandrew hendry write_unlock_bh(&x25_neigh_list_lock); 16651b06e6baSShaun Pereira } 16661b06e6baSShaun Pereira } 16671b06e6baSShaun Pereira x25_neigh_put(nb); 16681b06e6baSShaun Pereira out: 16691b06e6baSShaun Pereira return rc; 16701b06e6baSShaun Pereira out_dev_put: 16711b06e6baSShaun Pereira dev_put(dev); 16721b06e6baSShaun Pereira goto out; 16731b06e6baSShaun Pereira } 16741b06e6baSShaun Pereira 16751b06e6baSShaun Pereira static int compat_x25_ioctl(struct socket *sock, unsigned int cmd, 16761b06e6baSShaun Pereira unsigned long arg) 16771b06e6baSShaun Pereira { 16781b06e6baSShaun Pereira void __user *argp = compat_ptr(arg); 16791b06e6baSShaun Pereira struct sock *sk = sock->sk; 16801b06e6baSShaun Pereira 16811b06e6baSShaun Pereira int rc = -ENOIOCTLCMD; 16821b06e6baSShaun Pereira 16831b06e6baSShaun Pereira switch(cmd) { 16841b06e6baSShaun Pereira case TIOCOUTQ: 16851b06e6baSShaun Pereira case TIOCINQ: 16861b06e6baSShaun Pereira rc = x25_ioctl(sock, cmd, (unsigned long)argp); 16871b06e6baSShaun Pereira break; 16881b06e6baSShaun Pereira case SIOCGSTAMP: 16891b06e6baSShaun Pereira rc = -EINVAL; 16901b06e6baSShaun Pereira if (sk) 16911b06e6baSShaun Pereira rc = compat_sock_get_timestamp(sk, 16921b06e6baSShaun Pereira (struct timeval __user*)argp); 16931b06e6baSShaun Pereira break; 1694ae40eb1eSEric Dumazet case SIOCGSTAMPNS: 1695ae40eb1eSEric Dumazet rc = -EINVAL; 1696ae40eb1eSEric Dumazet if (sk) 1697ae40eb1eSEric Dumazet rc = compat_sock_get_timestampns(sk, 1698ae40eb1eSEric Dumazet (struct timespec __user*)argp); 1699ae40eb1eSEric Dumazet break; 17001b06e6baSShaun Pereira case SIOCGIFADDR: 17011b06e6baSShaun Pereira case SIOCSIFADDR: 17021b06e6baSShaun Pereira case SIOCGIFDSTADDR: 17031b06e6baSShaun Pereira case SIOCSIFDSTADDR: 17041b06e6baSShaun Pereira case SIOCGIFBRDADDR: 17051b06e6baSShaun Pereira case SIOCSIFBRDADDR: 17061b06e6baSShaun Pereira case SIOCGIFNETMASK: 17071b06e6baSShaun Pereira case SIOCSIFNETMASK: 17081b06e6baSShaun Pereira case SIOCGIFMETRIC: 17091b06e6baSShaun Pereira case SIOCSIFMETRIC: 17101b06e6baSShaun Pereira rc = -EINVAL; 17111b06e6baSShaun Pereira break; 17121b06e6baSShaun Pereira case SIOCADDRT: 17131b06e6baSShaun Pereira case SIOCDELRT: 17141b06e6baSShaun Pereira rc = -EPERM; 17151b06e6baSShaun Pereira if (!capable(CAP_NET_ADMIN)) 17161b06e6baSShaun Pereira break; 17171b06e6baSShaun Pereira rc = x25_route_ioctl(cmd, argp); 17181b06e6baSShaun Pereira break; 17191b06e6baSShaun Pereira case SIOCX25GSUBSCRIP: 17201b06e6baSShaun Pereira rc = compat_x25_subscr_ioctl(cmd, argp); 17211b06e6baSShaun Pereira break; 17221b06e6baSShaun Pereira case SIOCX25SSUBSCRIP: 17231b06e6baSShaun Pereira rc = -EPERM; 17241b06e6baSShaun Pereira if (!capable(CAP_NET_ADMIN)) 17251b06e6baSShaun Pereira break; 17261b06e6baSShaun Pereira rc = compat_x25_subscr_ioctl(cmd, argp); 17271b06e6baSShaun Pereira break; 17281b06e6baSShaun Pereira case SIOCX25GFACILITIES: 17291b06e6baSShaun Pereira case SIOCX25SFACILITIES: 17309a6b9f2eSShaun Pereira case SIOCX25GDTEFACILITIES: 17319a6b9f2eSShaun Pereira case SIOCX25SDTEFACILITIES: 17321b06e6baSShaun Pereira case SIOCX25GCALLUSERDATA: 17331b06e6baSShaun Pereira case SIOCX25SCALLUSERDATA: 17341b06e6baSShaun Pereira case SIOCX25GCAUSEDIAG: 1735386e50ccSAndrew Hendry case SIOCX25SCAUSEDIAG: 17361b06e6baSShaun Pereira case SIOCX25SCUDMATCHLEN: 17371b06e6baSShaun Pereira case SIOCX25CALLACCPTAPPRV: 17381b06e6baSShaun Pereira case SIOCX25SENDCALLACCPT: 17391b06e6baSShaun Pereira rc = x25_ioctl(sock, cmd, (unsigned long)argp); 17401b06e6baSShaun Pereira break; 17411b06e6baSShaun Pereira default: 17421b06e6baSShaun Pereira rc = -ENOIOCTLCMD; 17431b06e6baSShaun Pereira break; 17441b06e6baSShaun Pereira } 17451b06e6baSShaun Pereira return rc; 17461b06e6baSShaun Pereira } 17471b06e6baSShaun Pereira #endif 17481b06e6baSShaun Pereira 174991774904SArnd Bergmann static const struct proto_ops x25_proto_ops = { 17501da177e4SLinus Torvalds .family = AF_X25, 17511da177e4SLinus Torvalds .owner = THIS_MODULE, 17521da177e4SLinus Torvalds .release = x25_release, 17531da177e4SLinus Torvalds .bind = x25_bind, 17541da177e4SLinus Torvalds .connect = x25_connect, 17551da177e4SLinus Torvalds .socketpair = sock_no_socketpair, 17561da177e4SLinus Torvalds .accept = x25_accept, 17571da177e4SLinus Torvalds .getname = x25_getname, 1758a11e1d43SLinus Torvalds .poll = datagram_poll, 17591da177e4SLinus Torvalds .ioctl = x25_ioctl, 17601b06e6baSShaun Pereira #ifdef CONFIG_COMPAT 17611b06e6baSShaun Pereira .compat_ioctl = compat_x25_ioctl, 17621b06e6baSShaun Pereira #endif 17631da177e4SLinus Torvalds .listen = x25_listen, 17641da177e4SLinus Torvalds .shutdown = sock_no_shutdown, 17651da177e4SLinus Torvalds .setsockopt = x25_setsockopt, 17661da177e4SLinus Torvalds .getsockopt = x25_getsockopt, 17671da177e4SLinus Torvalds .sendmsg = x25_sendmsg, 17681da177e4SLinus Torvalds .recvmsg = x25_recvmsg, 17691da177e4SLinus Torvalds .mmap = sock_no_mmap, 17701da177e4SLinus Torvalds .sendpage = sock_no_sendpage, 17711da177e4SLinus Torvalds }; 17721da177e4SLinus Torvalds 17737546dd97SStephen Hemminger static struct packet_type x25_packet_type __read_mostly = { 177409640e63SHarvey Harrison .type = cpu_to_be16(ETH_P_X25), 17751da177e4SLinus Torvalds .func = x25_lapb_receive_frame, 17761da177e4SLinus Torvalds }; 17771da177e4SLinus Torvalds 17781da177e4SLinus Torvalds static struct notifier_block x25_dev_notifier = { 17791da177e4SLinus Torvalds .notifier_call = x25_device_event, 17801da177e4SLinus Torvalds }; 17811da177e4SLinus Torvalds 17821da177e4SLinus Torvalds void x25_kill_by_neigh(struct x25_neigh *nb) 17831da177e4SLinus Torvalds { 17841da177e4SLinus Torvalds struct sock *s; 17851da177e4SLinus Torvalds 17861da177e4SLinus Torvalds write_lock_bh(&x25_list_lock); 17871da177e4SLinus Torvalds 1788b67bfe0dSSasha Levin sk_for_each(s, &x25_list) 17891da177e4SLinus Torvalds if (x25_sk(s)->neighbour == nb) 17901da177e4SLinus Torvalds x25_disconnect(s, ENETUNREACH, 0, 0); 17911da177e4SLinus Torvalds 17921da177e4SLinus Torvalds write_unlock_bh(&x25_list_lock); 179395a9dc43SAndrew Hendry 179495a9dc43SAndrew Hendry /* Remove any related forwards */ 179595a9dc43SAndrew Hendry x25_clear_forward_by_dev(nb->dev); 17961da177e4SLinus Torvalds } 17971da177e4SLinus Torvalds 17981da177e4SLinus Torvalds static int __init x25_init(void) 17991da177e4SLinus Torvalds { 180064df6d52Slinzhang int rc; 18011da177e4SLinus Torvalds 180264df6d52Slinzhang rc = proto_register(&x25_proto, 0); 180364df6d52Slinzhang if (rc) 18041da177e4SLinus Torvalds goto out; 18051da177e4SLinus Torvalds 18061fd975a0Sandrew hendry rc = sock_register(&x25_family_ops); 180764df6d52Slinzhang if (rc) 18081fd975a0Sandrew hendry goto out_proto; 18091da177e4SLinus Torvalds 18101da177e4SLinus Torvalds dev_add_pack(&x25_packet_type); 18111da177e4SLinus Torvalds 18121fd975a0Sandrew hendry rc = register_netdevice_notifier(&x25_dev_notifier); 181364df6d52Slinzhang if (rc) 18141fd975a0Sandrew hendry goto out_sock; 18151da177e4SLinus Torvalds 181664df6d52Slinzhang rc = x25_register_sysctl(); 181764df6d52Slinzhang if (rc) 181864df6d52Slinzhang goto out_dev; 181964df6d52Slinzhang 182064df6d52Slinzhang rc = x25_proc_init(); 182164df6d52Slinzhang if (rc) 182264df6d52Slinzhang goto out_sysctl; 182364df6d52Slinzhang 1824b73e9e3cSwangweidong pr_info("Linux Version 0.2\n"); 18251da177e4SLinus Torvalds 18261da177e4SLinus Torvalds out: 18271da177e4SLinus Torvalds return rc; 182864df6d52Slinzhang out_sysctl: 182964df6d52Slinzhang x25_unregister_sysctl(); 18301fd975a0Sandrew hendry out_dev: 18311fd975a0Sandrew hendry unregister_netdevice_notifier(&x25_dev_notifier); 18321fd975a0Sandrew hendry out_sock: 183364df6d52Slinzhang dev_remove_pack(&x25_packet_type); 18341fd975a0Sandrew hendry sock_unregister(AF_X25); 18351fd975a0Sandrew hendry out_proto: 18361fd975a0Sandrew hendry proto_unregister(&x25_proto); 18371fd975a0Sandrew hendry goto out; 18381da177e4SLinus Torvalds } 18391da177e4SLinus Torvalds module_init(x25_init); 18401da177e4SLinus Torvalds 18411da177e4SLinus Torvalds static void __exit x25_exit(void) 18421da177e4SLinus Torvalds { 18431da177e4SLinus Torvalds x25_proc_exit(); 18441da177e4SLinus Torvalds x25_link_free(); 18451da177e4SLinus Torvalds x25_route_free(); 18461da177e4SLinus Torvalds 18471da177e4SLinus Torvalds x25_unregister_sysctl(); 18481da177e4SLinus Torvalds 18491da177e4SLinus Torvalds unregister_netdevice_notifier(&x25_dev_notifier); 18501da177e4SLinus Torvalds 18511da177e4SLinus Torvalds dev_remove_pack(&x25_packet_type); 18521da177e4SLinus Torvalds 18531da177e4SLinus Torvalds sock_unregister(AF_X25); 18541da177e4SLinus Torvalds proto_unregister(&x25_proto); 18551da177e4SLinus Torvalds } 18561da177e4SLinus Torvalds module_exit(x25_exit); 18571da177e4SLinus Torvalds 18581da177e4SLinus Torvalds MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>"); 18591da177e4SLinus Torvalds MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol"); 18601da177e4SLinus Torvalds MODULE_LICENSE("GPL"); 18611da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_X25); 1862