1ee5d8f4dSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds * X.25 Packet Layer release 002
41da177e4SLinus Torvalds *
51da177e4SLinus Torvalds * This is ALPHA test software. This code may break your machine,
61da177e4SLinus Torvalds * randomly fail to work with new releases, misbehave and/or generally
71da177e4SLinus Torvalds * screw up. It might even work.
81da177e4SLinus Torvalds *
91da177e4SLinus Torvalds * This code REQUIRES 2.1.15 or higher
101da177e4SLinus Torvalds *
111da177e4SLinus Torvalds * History
121da177e4SLinus Torvalds * X.25 001 Jonathan Naylor Started coding.
131da177e4SLinus Torvalds * X.25 002 Jonathan Naylor Centralised disconnect handling.
141da177e4SLinus Torvalds * New timer architecture.
151da177e4SLinus Torvalds * 2000-03-11 Henner Eisen MSG_EOR handling more POSIX compliant.
161da177e4SLinus Torvalds * 2000-03-22 Daniela Squassoni Allowed disabling/enabling of
171da177e4SLinus Torvalds * facilities negotiation and increased
181da177e4SLinus Torvalds * the throughput upper limit.
191da177e4SLinus Torvalds * 2000-08-27 Arnaldo C. Melo s/suser/capable/ + micro cleanups
201da177e4SLinus Torvalds * 2000-09-04 Henner Eisen Set sock->state in x25_accept().
211da177e4SLinus Torvalds * Fixed x25_output() related skb leakage.
221da177e4SLinus Torvalds * 2000-10-02 Henner Eisen Made x25_kick() single threaded per socket.
231da177e4SLinus Torvalds * 2000-10-27 Henner Eisen MSG_DONTWAIT for fragment allocation.
241da177e4SLinus Torvalds * 2000-11-14 Henner Eisen Closing datalink from NETDEV_GOING_DOWN
251da177e4SLinus Torvalds * 2002-10-06 Arnaldo C. Melo Get rid of cli/sti, move proc stuff to
261da177e4SLinus Torvalds * x25_proc.c, using seq_file
27cb65d506SShaun Pereira * 2005-04-02 Shaun Pereira Selective sub address matching
28cb65d506SShaun Pereira * with call user data
29ebc3f64bSShaun Pereira * 2005-04-15 Shaun Pereira Fast select with no restriction on
30ebc3f64bSShaun Pereira * response
311da177e4SLinus Torvalds */
321da177e4SLinus Torvalds
33b73e9e3cSwangweidong #define pr_fmt(fmt) "X25: " fmt
34b73e9e3cSwangweidong
351da177e4SLinus Torvalds #include <linux/module.h>
364fc268d2SRandy Dunlap #include <linux/capability.h>
371da177e4SLinus Torvalds #include <linux/errno.h>
381da177e4SLinus Torvalds #include <linux/kernel.h>
393f07c014SIngo Molnar #include <linux/sched/signal.h>
401da177e4SLinus Torvalds #include <linux/timer.h>
411da177e4SLinus Torvalds #include <linux/string.h>
421da177e4SLinus Torvalds #include <linux/net.h>
431da177e4SLinus Torvalds #include <linux/netdevice.h>
441da177e4SLinus Torvalds #include <linux/if_arp.h>
451da177e4SLinus Torvalds #include <linux/skbuff.h>
465a0e3ad6STejun Heo #include <linux/slab.h>
471da177e4SLinus Torvalds #include <net/sock.h>
48c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h>
497c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
501da177e4SLinus Torvalds #include <linux/fcntl.h>
511da177e4SLinus Torvalds #include <linux/termios.h> /* For TIOCINQ/OUTQ */
521da177e4SLinus Torvalds #include <linux/notifier.h>
531da177e4SLinus Torvalds #include <linux/init.h>
541b06e6baSShaun Pereira #include <linux/compat.h>
55a9288525Sandrew hendry #include <linux/ctype.h>
561b06e6baSShaun Pereira
571da177e4SLinus Torvalds #include <net/x25.h>
581b06e6baSShaun Pereira #include <net/compat.h>
591da177e4SLinus Torvalds
601da177e4SLinus Torvalds int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
611da177e4SLinus Torvalds int sysctl_x25_call_request_timeout = X25_DEFAULT_T21;
621da177e4SLinus Torvalds int sysctl_x25_reset_request_timeout = X25_DEFAULT_T22;
631da177e4SLinus Torvalds int sysctl_x25_clear_request_timeout = X25_DEFAULT_T23;
641da177e4SLinus Torvalds int sysctl_x25_ack_holdback_timeout = X25_DEFAULT_T2;
6539e21c0dSAndrew Hendry int sysctl_x25_forward = 0;
661da177e4SLinus Torvalds
671da177e4SLinus Torvalds HLIST_HEAD(x25_list);
681da177e4SLinus Torvalds DEFINE_RWLOCK(x25_list_lock);
691da177e4SLinus Torvalds
7090ddc4f0SEric Dumazet static const struct proto_ops x25_proto_ops;
711da177e4SLinus Torvalds
72e9e6c232SJulia Lawall static const struct x25_address null_x25_address = {" "};
731da177e4SLinus Torvalds
741b06e6baSShaun Pereira #ifdef CONFIG_COMPAT
751b06e6baSShaun Pereira struct compat_x25_subscrip_struct {
761b06e6baSShaun Pereira char device[200-sizeof(compat_ulong_t)];
771b06e6baSShaun Pereira compat_ulong_t global_facil_mask;
781b06e6baSShaun Pereira compat_uint_t extended;
791b06e6baSShaun Pereira };
801b06e6baSShaun Pereira #endif
811b06e6baSShaun Pereira
82f5eb917bSJohn Hughes
x25_parse_address_block(struct sk_buff * skb,struct x25_address * called_addr,struct x25_address * calling_addr)83f5eb917bSJohn Hughes int x25_parse_address_block(struct sk_buff *skb,
84f5eb917bSJohn Hughes struct x25_address *called_addr,
85f5eb917bSJohn Hughes struct x25_address *calling_addr)
86f5eb917bSJohn Hughes {
87f5eb917bSJohn Hughes unsigned char len;
88f5eb917bSJohn Hughes int needed;
89f5eb917bSJohn Hughes int rc;
90f5eb917bSJohn Hughes
91cb101ed2SMatthew Daley if (!pskb_may_pull(skb, 1)) {
92f5eb917bSJohn Hughes /* packet has no address block */
93f5eb917bSJohn Hughes rc = 0;
94f5eb917bSJohn Hughes goto empty;
95f5eb917bSJohn Hughes }
96f5eb917bSJohn Hughes
97f5eb917bSJohn Hughes len = *skb->data;
98d449ba3dSMartin Schiller needed = 1 + ((len >> 4) + (len & 0x0f) + 1) / 2;
99f5eb917bSJohn Hughes
100cb101ed2SMatthew Daley if (!pskb_may_pull(skb, needed)) {
101f5eb917bSJohn Hughes /* packet is too short to hold the addresses it claims
102f5eb917bSJohn Hughes to hold */
103f5eb917bSJohn Hughes rc = -1;
104f5eb917bSJohn Hughes goto empty;
105f5eb917bSJohn Hughes }
106f5eb917bSJohn Hughes
107f5eb917bSJohn Hughes return x25_addr_ntoa(skb->data, called_addr, calling_addr);
108f5eb917bSJohn Hughes
109f5eb917bSJohn Hughes empty:
110f5eb917bSJohn Hughes *called_addr->x25_addr = 0;
111f5eb917bSJohn Hughes *calling_addr->x25_addr = 0;
112f5eb917bSJohn Hughes
113f5eb917bSJohn Hughes return rc;
114f5eb917bSJohn Hughes }
115f5eb917bSJohn Hughes
116f5eb917bSJohn Hughes
x25_addr_ntoa(unsigned char * p,struct x25_address * called_addr,struct x25_address * calling_addr)1171da177e4SLinus Torvalds int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr,
1181da177e4SLinus Torvalds struct x25_address *calling_addr)
1191da177e4SLinus Torvalds {
1206bf1574eSEric Dumazet unsigned int called_len, calling_len;
1211da177e4SLinus Torvalds char *called, *calling;
1226bf1574eSEric Dumazet unsigned int i;
1231da177e4SLinus Torvalds
1241da177e4SLinus Torvalds called_len = (*p >> 0) & 0x0F;
1251da177e4SLinus Torvalds calling_len = (*p >> 4) & 0x0F;
1261da177e4SLinus Torvalds
1271da177e4SLinus Torvalds called = called_addr->x25_addr;
1281da177e4SLinus Torvalds calling = calling_addr->x25_addr;
1291da177e4SLinus Torvalds p++;
1301da177e4SLinus Torvalds
1311da177e4SLinus Torvalds for (i = 0; i < (called_len + calling_len); i++) {
1321da177e4SLinus Torvalds if (i < called_len) {
1331da177e4SLinus Torvalds if (i % 2 != 0) {
1341da177e4SLinus Torvalds *called++ = ((*p >> 0) & 0x0F) + '0';
1351da177e4SLinus Torvalds p++;
1361da177e4SLinus Torvalds } else {
1371da177e4SLinus Torvalds *called++ = ((*p >> 4) & 0x0F) + '0';
1381da177e4SLinus Torvalds }
1391da177e4SLinus Torvalds } else {
1401da177e4SLinus Torvalds if (i % 2 != 0) {
1411da177e4SLinus Torvalds *calling++ = ((*p >> 0) & 0x0F) + '0';
1421da177e4SLinus Torvalds p++;
1431da177e4SLinus Torvalds } else {
1441da177e4SLinus Torvalds *calling++ = ((*p >> 4) & 0x0F) + '0';
1451da177e4SLinus Torvalds }
1461da177e4SLinus Torvalds }
1471da177e4SLinus Torvalds }
1481da177e4SLinus Torvalds
1491da177e4SLinus Torvalds *called = *calling = '\0';
1501da177e4SLinus Torvalds
1511da177e4SLinus Torvalds return 1 + (called_len + calling_len + 1) / 2;
1521da177e4SLinus Torvalds }
1531da177e4SLinus Torvalds
x25_addr_aton(unsigned char * p,struct x25_address * called_addr,struct x25_address * calling_addr)1541da177e4SLinus Torvalds int x25_addr_aton(unsigned char *p, struct x25_address *called_addr,
1551da177e4SLinus Torvalds struct x25_address *calling_addr)
1561da177e4SLinus Torvalds {
1571da177e4SLinus Torvalds unsigned int called_len, calling_len;
1581da177e4SLinus Torvalds char *called, *calling;
1591da177e4SLinus Torvalds int i;
1601da177e4SLinus Torvalds
1611da177e4SLinus Torvalds called = called_addr->x25_addr;
1621da177e4SLinus Torvalds calling = calling_addr->x25_addr;
1631da177e4SLinus Torvalds
1641da177e4SLinus Torvalds called_len = strlen(called);
1651da177e4SLinus Torvalds calling_len = strlen(calling);
1661da177e4SLinus Torvalds
1671da177e4SLinus Torvalds *p++ = (calling_len << 4) | (called_len << 0);
1681da177e4SLinus Torvalds
1691da177e4SLinus Torvalds for (i = 0; i < (called_len + calling_len); i++) {
1701da177e4SLinus Torvalds if (i < called_len) {
1711da177e4SLinus Torvalds if (i % 2 != 0) {
1721da177e4SLinus Torvalds *p |= (*called++ - '0') << 0;
1731da177e4SLinus Torvalds p++;
1741da177e4SLinus Torvalds } else {
1751da177e4SLinus Torvalds *p = 0x00;
1761da177e4SLinus Torvalds *p |= (*called++ - '0') << 4;
1771da177e4SLinus Torvalds }
1781da177e4SLinus Torvalds } else {
1791da177e4SLinus Torvalds if (i % 2 != 0) {
1801da177e4SLinus Torvalds *p |= (*calling++ - '0') << 0;
1811da177e4SLinus Torvalds p++;
1821da177e4SLinus Torvalds } else {
1831da177e4SLinus Torvalds *p = 0x00;
1841da177e4SLinus Torvalds *p |= (*calling++ - '0') << 4;
1851da177e4SLinus Torvalds }
1861da177e4SLinus Torvalds }
1871da177e4SLinus Torvalds }
1881da177e4SLinus Torvalds
1891da177e4SLinus Torvalds return 1 + (called_len + calling_len + 1) / 2;
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds
1921da177e4SLinus Torvalds /*
1931da177e4SLinus Torvalds * Socket removal during an interrupt is now safe.
1941da177e4SLinus Torvalds */
x25_remove_socket(struct sock * sk)1951da177e4SLinus Torvalds static void x25_remove_socket(struct sock *sk)
1961da177e4SLinus Torvalds {
1971da177e4SLinus Torvalds write_lock_bh(&x25_list_lock);
1981da177e4SLinus Torvalds sk_del_node_init(sk);
1991da177e4SLinus Torvalds write_unlock_bh(&x25_list_lock);
2001da177e4SLinus Torvalds }
2011da177e4SLinus Torvalds
2021da177e4SLinus Torvalds /*
2031da177e4SLinus Torvalds * Handle device status changes.
2041da177e4SLinus Torvalds */
x25_device_event(struct notifier_block * this,unsigned long event,void * ptr)2051da177e4SLinus Torvalds static int x25_device_event(struct notifier_block *this, unsigned long event,
2061da177e4SLinus Torvalds void *ptr)
2071da177e4SLinus Torvalds {
208351638e7SJiri Pirko struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2091da177e4SLinus Torvalds struct x25_neigh *nb;
2101da177e4SLinus Torvalds
211721499e8SYOSHIFUJI Hideaki if (!net_eq(dev_net(dev), &init_net))
212e9dc8653SEric W. Biederman return NOTIFY_DONE;
213e9dc8653SEric W. Biederman
21413458ffeSXie He if (dev->type == ARPHRD_X25) {
2151da177e4SLinus Torvalds switch (event) {
2167eed751bSMartin Schiller case NETDEV_REGISTER:
2177eed751bSMartin Schiller case NETDEV_POST_TYPE_CHANGE:
2181da177e4SLinus Torvalds x25_link_device_up(dev);
2191da177e4SLinus Torvalds break;
2207eed751bSMartin Schiller case NETDEV_DOWN:
2211da177e4SLinus Torvalds nb = x25_get_neigh(dev);
2221da177e4SLinus Torvalds if (nb) {
2237eed751bSMartin Schiller x25_link_terminated(nb);
2241da177e4SLinus Torvalds x25_neigh_put(nb);
2251da177e4SLinus Torvalds }
2261da177e4SLinus Torvalds x25_route_device_down(dev);
2277eed751bSMartin Schiller break;
2287eed751bSMartin Schiller case NETDEV_PRE_TYPE_CHANGE:
2297eed751bSMartin Schiller case NETDEV_UNREGISTER:
2301da177e4SLinus Torvalds x25_link_device_down(dev);
2311da177e4SLinus Torvalds break;
2327eed751bSMartin Schiller case NETDEV_CHANGE:
2337eed751bSMartin Schiller if (!netif_carrier_ok(dev)) {
2347eed751bSMartin Schiller nb = x25_get_neigh(dev);
2357eed751bSMartin Schiller if (nb) {
2367eed751bSMartin Schiller x25_link_terminated(nb);
2377eed751bSMartin Schiller x25_neigh_put(nb);
2387eed751bSMartin Schiller }
2397eed751bSMartin Schiller }
2407eed751bSMartin Schiller break;
2411da177e4SLinus Torvalds }
2421da177e4SLinus Torvalds }
2431da177e4SLinus Torvalds
2441da177e4SLinus Torvalds return NOTIFY_DONE;
2451da177e4SLinus Torvalds }
2461da177e4SLinus Torvalds
2471da177e4SLinus Torvalds /*
2481da177e4SLinus Torvalds * Add a socket to the bound sockets list.
2491da177e4SLinus Torvalds */
x25_insert_socket(struct sock * sk)2501da177e4SLinus Torvalds static void x25_insert_socket(struct sock *sk)
2511da177e4SLinus Torvalds {
2521da177e4SLinus Torvalds write_lock_bh(&x25_list_lock);
2531da177e4SLinus Torvalds sk_add_node(sk, &x25_list);
2541da177e4SLinus Torvalds write_unlock_bh(&x25_list_lock);
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds
2571da177e4SLinus Torvalds /*
2581da177e4SLinus Torvalds * Find a socket that wants to accept the Call Request we just
2591da177e4SLinus Torvalds * received. Check the full list for an address/cud match.
2601da177e4SLinus Torvalds * If no cuds match return the next_best thing, an address match.
2611da177e4SLinus Torvalds * Note: if a listening socket has cud set it must only get calls
2621da177e4SLinus Torvalds * with matching cud.
2631da177e4SLinus Torvalds */
x25_find_listener(struct x25_address * addr,struct sk_buff * skb)264cb65d506SShaun Pereira static struct sock *x25_find_listener(struct x25_address *addr,
265cb65d506SShaun Pereira struct sk_buff *skb)
2661da177e4SLinus Torvalds {
2671da177e4SLinus Torvalds struct sock *s;
2681da177e4SLinus Torvalds struct sock *next_best;
2691da177e4SLinus Torvalds
2701da177e4SLinus Torvalds read_lock_bh(&x25_list_lock);
2711da177e4SLinus Torvalds next_best = NULL;
2721da177e4SLinus Torvalds
273b67bfe0dSSasha Levin sk_for_each(s, &x25_list)
2741da177e4SLinus Torvalds if ((!strcmp(addr->x25_addr,
2751da177e4SLinus Torvalds x25_sk(s)->source_addr.x25_addr) ||
27606137619SMartin Schiller !strcmp(x25_sk(s)->source_addr.x25_addr,
2771da177e4SLinus Torvalds null_x25_address.x25_addr)) &&
2781da177e4SLinus Torvalds s->sk_state == TCP_LISTEN) {
2791da177e4SLinus Torvalds /*
2801da177e4SLinus Torvalds * Found a listening socket, now check the incoming
2811da177e4SLinus Torvalds * call user data vs this sockets call user data
2821da177e4SLinus Torvalds */
2837f81e25bSMatthew Daley if (x25_sk(s)->cudmatchlength > 0 &&
2847f81e25bSMatthew Daley skb->len >= x25_sk(s)->cudmatchlength) {
285cb65d506SShaun Pereira if((memcmp(x25_sk(s)->calluserdata.cuddata,
286cb65d506SShaun Pereira skb->data,
287cb65d506SShaun Pereira x25_sk(s)->cudmatchlength)) == 0) {
2881da177e4SLinus Torvalds sock_hold(s);
2891da177e4SLinus Torvalds goto found;
2901da177e4SLinus Torvalds }
291cb65d506SShaun Pereira } else
2921da177e4SLinus Torvalds next_best = s;
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds if (next_best) {
2951da177e4SLinus Torvalds s = next_best;
2961da177e4SLinus Torvalds sock_hold(s);
2971da177e4SLinus Torvalds goto found;
2981da177e4SLinus Torvalds }
2991da177e4SLinus Torvalds s = NULL;
3001da177e4SLinus Torvalds found:
3011da177e4SLinus Torvalds read_unlock_bh(&x25_list_lock);
3021da177e4SLinus Torvalds return s;
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds
3051da177e4SLinus Torvalds /*
3061da177e4SLinus Torvalds * Find a connected X.25 socket given my LCI and neighbour.
3071da177e4SLinus Torvalds */
__x25_find_socket(unsigned int lci,struct x25_neigh * nb)3081da177e4SLinus Torvalds static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
3091da177e4SLinus Torvalds {
3101da177e4SLinus Torvalds struct sock *s;
3111da177e4SLinus Torvalds
312b67bfe0dSSasha Levin sk_for_each(s, &x25_list)
3131da177e4SLinus Torvalds if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
3141da177e4SLinus Torvalds sock_hold(s);
3151da177e4SLinus Torvalds goto found;
3161da177e4SLinus Torvalds }
3171da177e4SLinus Torvalds s = NULL;
3181da177e4SLinus Torvalds found:
3191da177e4SLinus Torvalds return s;
3201da177e4SLinus Torvalds }
3211da177e4SLinus Torvalds
x25_find_socket(unsigned int lci,struct x25_neigh * nb)3221da177e4SLinus Torvalds struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
3231da177e4SLinus Torvalds {
3241da177e4SLinus Torvalds struct sock *s;
3251da177e4SLinus Torvalds
3261da177e4SLinus Torvalds read_lock_bh(&x25_list_lock);
3271da177e4SLinus Torvalds s = __x25_find_socket(lci, nb);
3281da177e4SLinus Torvalds read_unlock_bh(&x25_list_lock);
3291da177e4SLinus Torvalds return s;
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds
3321da177e4SLinus Torvalds /*
3331da177e4SLinus Torvalds * Find a unique LCI for a given device.
3341da177e4SLinus Torvalds */
x25_new_lci(struct x25_neigh * nb)3351da177e4SLinus Torvalds static unsigned int x25_new_lci(struct x25_neigh *nb)
3361da177e4SLinus Torvalds {
3371da177e4SLinus Torvalds unsigned int lci = 1;
3381da177e4SLinus Torvalds struct sock *sk;
3391da177e4SLinus Torvalds
340cf657d22SEric Dumazet while ((sk = x25_find_socket(lci, nb)) != NULL) {
3411da177e4SLinus Torvalds sock_put(sk);
3421da177e4SLinus Torvalds if (++lci == 4096) {
3431da177e4SLinus Torvalds lci = 0;
3441da177e4SLinus Torvalds break;
3451da177e4SLinus Torvalds }
346cf657d22SEric Dumazet cond_resched();
3471da177e4SLinus Torvalds }
3481da177e4SLinus Torvalds
3491da177e4SLinus Torvalds return lci;
3501da177e4SLinus Torvalds }
3511da177e4SLinus Torvalds
3521da177e4SLinus Torvalds /*
3531da177e4SLinus Torvalds * Deferred destroy.
3541da177e4SLinus Torvalds */
35514ebaf81SDavid S. Miller static void __x25_destroy_socket(struct sock *);
3561da177e4SLinus Torvalds
3571da177e4SLinus Torvalds /*
3581da177e4SLinus Torvalds * handler for deferred kills.
3591da177e4SLinus Torvalds */
x25_destroy_timer(struct timer_list * t)36099767f27SKees Cook static void x25_destroy_timer(struct timer_list *t)
3611da177e4SLinus Torvalds {
36299767f27SKees Cook struct sock *sk = from_timer(sk, t, sk_timer);
36399767f27SKees Cook
36499767f27SKees Cook x25_destroy_socket_from_timer(sk);
3651da177e4SLinus Torvalds }
3661da177e4SLinus Torvalds
3671da177e4SLinus Torvalds /*
3681da177e4SLinus Torvalds * This is called from user mode and the timers. Thus it protects itself
369db67f249Sgushengxian * against interrupting users but doesn't worry about being called during
3701da177e4SLinus Torvalds * work. Once it is removed from the queue no interrupt or bottom half
3711da177e4SLinus Torvalds * will touch it and we are (fairly 8-) ) safe.
3721da177e4SLinus Torvalds * Not static as it's used by the timer
3731da177e4SLinus Torvalds */
__x25_destroy_socket(struct sock * sk)37414ebaf81SDavid S. Miller static void __x25_destroy_socket(struct sock *sk)
3751da177e4SLinus Torvalds {
3761da177e4SLinus Torvalds struct sk_buff *skb;
3771da177e4SLinus Torvalds
3781da177e4SLinus Torvalds x25_stop_heartbeat(sk);
3791da177e4SLinus Torvalds x25_stop_timer(sk);
3801da177e4SLinus Torvalds
3811da177e4SLinus Torvalds x25_remove_socket(sk);
3821da177e4SLinus Torvalds x25_clear_queues(sk); /* Flush the queues */
3831da177e4SLinus Torvalds
3841da177e4SLinus Torvalds while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
3851da177e4SLinus Torvalds if (skb->sk != sk) { /* A pending connection */
3861da177e4SLinus Torvalds /*
3871da177e4SLinus Torvalds * Queue the unaccepted socket for death
3881da177e4SLinus Torvalds */
3892cec6b01Sandrew hendry skb->sk->sk_state = TCP_LISTEN;
3901da177e4SLinus Torvalds sock_set_flag(skb->sk, SOCK_DEAD);
3911da177e4SLinus Torvalds x25_start_heartbeat(skb->sk);
3921da177e4SLinus Torvalds x25_sk(skb->sk)->state = X25_STATE_0;
3931da177e4SLinus Torvalds }
3941da177e4SLinus Torvalds
3951da177e4SLinus Torvalds kfree_skb(skb);
3961da177e4SLinus Torvalds }
3971da177e4SLinus Torvalds
398c564039fSEric Dumazet if (sk_has_allocations(sk)) {
3991da177e4SLinus Torvalds /* Defer: outstanding buffers */
4001da177e4SLinus Torvalds sk->sk_timer.expires = jiffies + 10 * HZ;
401841b86f3SKees Cook sk->sk_timer.function = x25_destroy_timer;
4021da177e4SLinus Torvalds add_timer(&sk->sk_timer);
4031da177e4SLinus Torvalds } else {
4041da177e4SLinus Torvalds /* drop last reference so sock_put will free */
4051da177e4SLinus Torvalds __sock_put(sk);
4061da177e4SLinus Torvalds }
40714ebaf81SDavid S. Miller }
4081da177e4SLinus Torvalds
x25_destroy_socket_from_timer(struct sock * sk)40914ebaf81SDavid S. Miller void x25_destroy_socket_from_timer(struct sock *sk)
41014ebaf81SDavid S. Miller {
41114ebaf81SDavid S. Miller sock_hold(sk);
41214ebaf81SDavid S. Miller bh_lock_sock(sk);
41314ebaf81SDavid S. Miller __x25_destroy_socket(sk);
41414ebaf81SDavid S. Miller bh_unlock_sock(sk);
41514ebaf81SDavid S. Miller sock_put(sk);
41614ebaf81SDavid S. Miller }
41714ebaf81SDavid S. Miller
4181da177e4SLinus Torvalds /*
4191da177e4SLinus Torvalds * Handling for system calls applied via the various interfaces to a
4201da177e4SLinus Torvalds * X.25 socket object.
4211da177e4SLinus Torvalds */
4221da177e4SLinus Torvalds
x25_setsockopt(struct socket * sock,int level,int optname,sockptr_t optval,unsigned int optlen)4231da177e4SLinus Torvalds static int x25_setsockopt(struct socket *sock, int level, int optname,
424a7b75c5aSChristoph Hellwig sockptr_t optval, unsigned int optlen)
4251da177e4SLinus Torvalds {
4261da177e4SLinus Torvalds int opt;
4271da177e4SLinus Torvalds struct sock *sk = sock->sk;
4281da177e4SLinus Torvalds int rc = -ENOPROTOOPT;
4291da177e4SLinus Torvalds
4301da177e4SLinus Torvalds if (level != SOL_X25 || optname != X25_QBITINCL)
4311da177e4SLinus Torvalds goto out;
4321da177e4SLinus Torvalds
4331da177e4SLinus Torvalds rc = -EINVAL;
4341da177e4SLinus Torvalds if (optlen < sizeof(int))
4351da177e4SLinus Torvalds goto out;
4361da177e4SLinus Torvalds
4371da177e4SLinus Torvalds rc = -EFAULT;
438a7b75c5aSChristoph Hellwig if (copy_from_sockptr(&opt, optval, sizeof(int)))
4391da177e4SLinus Torvalds goto out;
4401da177e4SLinus Torvalds
441cb863ffdSandrew hendry if (opt)
442cb863ffdSandrew hendry set_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
443cb863ffdSandrew hendry else
444cb863ffdSandrew hendry clear_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
4451da177e4SLinus Torvalds rc = 0;
4461da177e4SLinus Torvalds out:
4471da177e4SLinus Torvalds return rc;
4481da177e4SLinus Torvalds }
4491da177e4SLinus Torvalds
x25_getsockopt(struct socket * sock,int level,int optname,char __user * optval,int __user * optlen)4501da177e4SLinus Torvalds static int x25_getsockopt(struct socket *sock, int level, int optname,
4511da177e4SLinus Torvalds char __user *optval, int __user *optlen)
4521da177e4SLinus Torvalds {
4531da177e4SLinus Torvalds struct sock *sk = sock->sk;
4541da177e4SLinus Torvalds int val, len, rc = -ENOPROTOOPT;
4551da177e4SLinus Torvalds
4561da177e4SLinus Torvalds if (level != SOL_X25 || optname != X25_QBITINCL)
4571da177e4SLinus Torvalds goto out;
4581da177e4SLinus Torvalds
4591da177e4SLinus Torvalds rc = -EFAULT;
4601da177e4SLinus Torvalds if (get_user(len, optlen))
4611da177e4SLinus Torvalds goto out;
4621da177e4SLinus Torvalds
4631da177e4SLinus Torvalds rc = -EINVAL;
4641da177e4SLinus Torvalds if (len < 0)
4651da177e4SLinus Torvalds goto out;
4661da177e4SLinus Torvalds
467*138cca05SGavrilov Ilia len = min_t(unsigned int, len, sizeof(int));
468*138cca05SGavrilov Ilia
4691da177e4SLinus Torvalds rc = -EFAULT;
4701da177e4SLinus Torvalds if (put_user(len, optlen))
4711da177e4SLinus Torvalds goto out;
4721da177e4SLinus Torvalds
473cb863ffdSandrew hendry val = test_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
4741da177e4SLinus Torvalds rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
4751da177e4SLinus Torvalds out:
4761da177e4SLinus Torvalds return rc;
4771da177e4SLinus Torvalds }
4781da177e4SLinus Torvalds
x25_listen(struct socket * sock,int backlog)4791da177e4SLinus Torvalds static int x25_listen(struct socket *sock, int backlog)
4801da177e4SLinus Torvalds {
4811da177e4SLinus Torvalds struct sock *sk = sock->sk;
4821da177e4SLinus Torvalds int rc = -EOPNOTSUPP;
4831da177e4SLinus Torvalds
48425aa4efeSandrew hendry lock_sock(sk);
485f2b0b521SHyunwoo Kim if (sock->state != SS_UNCONNECTED) {
486f2b0b521SHyunwoo Kim rc = -EINVAL;
487f2b0b521SHyunwoo Kim release_sock(sk);
488f2b0b521SHyunwoo Kim return rc;
489f2b0b521SHyunwoo Kim }
490f2b0b521SHyunwoo Kim
4911da177e4SLinus Torvalds if (sk->sk_state != TCP_LISTEN) {
4921da177e4SLinus Torvalds memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
4931da177e4SLinus Torvalds sk->sk_max_ack_backlog = backlog;
4941da177e4SLinus Torvalds sk->sk_state = TCP_LISTEN;
4951da177e4SLinus Torvalds rc = 0;
4961da177e4SLinus Torvalds }
49725aa4efeSandrew hendry release_sock(sk);
4981da177e4SLinus Torvalds
4991da177e4SLinus Torvalds return rc;
5001da177e4SLinus Torvalds }
5011da177e4SLinus Torvalds
5021da177e4SLinus Torvalds static struct proto x25_proto = {
5031da177e4SLinus Torvalds .name = "X25",
5041da177e4SLinus Torvalds .owner = THIS_MODULE,
5051da177e4SLinus Torvalds .obj_size = sizeof(struct x25_sock),
5061da177e4SLinus Torvalds };
5071da177e4SLinus Torvalds
x25_alloc_socket(struct net * net,int kern)50811aa9c28SEric W. Biederman static struct sock *x25_alloc_socket(struct net *net, int kern)
5091da177e4SLinus Torvalds {
5101da177e4SLinus Torvalds struct x25_sock *x25;
51111aa9c28SEric W. Biederman struct sock *sk = sk_alloc(net, AF_X25, GFP_ATOMIC, &x25_proto, kern);
5121da177e4SLinus Torvalds
5131da177e4SLinus Torvalds if (!sk)
5141da177e4SLinus Torvalds goto out;
5151da177e4SLinus Torvalds
5161da177e4SLinus Torvalds sock_init_data(NULL, sk);
5171da177e4SLinus Torvalds
5181da177e4SLinus Torvalds x25 = x25_sk(sk);
5191da177e4SLinus Torvalds skb_queue_head_init(&x25->ack_queue);
5201da177e4SLinus Torvalds skb_queue_head_init(&x25->fragment_queue);
5211da177e4SLinus Torvalds skb_queue_head_init(&x25->interrupt_in_queue);
5221da177e4SLinus Torvalds skb_queue_head_init(&x25->interrupt_out_queue);
5231da177e4SLinus Torvalds out:
5241da177e4SLinus Torvalds return sk;
5251da177e4SLinus Torvalds }
5261da177e4SLinus Torvalds
x25_create(struct net * net,struct socket * sock,int protocol,int kern)5273f378b68SEric Paris static int x25_create(struct net *net, struct socket *sock, int protocol,
5283f378b68SEric Paris int kern)
5291da177e4SLinus Torvalds {
5301da177e4SLinus Torvalds struct sock *sk;
5311da177e4SLinus Torvalds struct x25_sock *x25;
532b18e7a06Sandrew hendry int rc = -EAFNOSUPPORT;
5331da177e4SLinus Torvalds
53409ad9bc7SOctavian Purdila if (!net_eq(net, &init_net))
5351da177e4SLinus Torvalds goto out;
5361da177e4SLinus Torvalds
537b18e7a06Sandrew hendry rc = -ESOCKTNOSUPPORT;
538b18e7a06Sandrew hendry if (sock->type != SOCK_SEQPACKET)
539b18e7a06Sandrew hendry goto out;
540b18e7a06Sandrew hendry
541b18e7a06Sandrew hendry rc = -EINVAL;
542b18e7a06Sandrew hendry if (protocol)
543b18e7a06Sandrew hendry goto out;
544b18e7a06Sandrew hendry
545d7736958SZheng Yongjun rc = -ENOMEM;
54611aa9c28SEric W. Biederman if ((sk = x25_alloc_socket(net, kern)) == NULL)
5471da177e4SLinus Torvalds goto out;
5481da177e4SLinus Torvalds
5491da177e4SLinus Torvalds x25 = x25_sk(sk);
5501da177e4SLinus Torvalds
5511da177e4SLinus Torvalds sock_init_data(sock, sk);
5521da177e4SLinus Torvalds
5531da177e4SLinus Torvalds x25_init_timers(sk);
5541da177e4SLinus Torvalds
5551da177e4SLinus Torvalds sock->ops = &x25_proto_ops;
5561da177e4SLinus Torvalds sk->sk_protocol = protocol;
5571da177e4SLinus Torvalds sk->sk_backlog_rcv = x25_backlog_rcv;
5581da177e4SLinus Torvalds
5591da177e4SLinus Torvalds x25->t21 = sysctl_x25_call_request_timeout;
5601da177e4SLinus Torvalds x25->t22 = sysctl_x25_reset_request_timeout;
5611da177e4SLinus Torvalds x25->t23 = sysctl_x25_clear_request_timeout;
5621da177e4SLinus Torvalds x25->t2 = sysctl_x25_ack_holdback_timeout;
5631da177e4SLinus Torvalds x25->state = X25_STATE_0;
564cb65d506SShaun Pereira x25->cudmatchlength = 0;
56537cda787Sandrew hendry set_bit(X25_ACCPT_APPRV_FLAG, &x25->flags); /* normally no cud */
566ebc3f64bSShaun Pereira /* on call accept */
5671da177e4SLinus Torvalds
5681da177e4SLinus Torvalds x25->facilities.winsize_in = X25_DEFAULT_WINDOW_SIZE;
5691da177e4SLinus Torvalds x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
5701da177e4SLinus Torvalds x25->facilities.pacsize_in = X25_DEFAULT_PACKET_SIZE;
5711da177e4SLinus Torvalds x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
572ddd0451fSJohn Hughes x25->facilities.throughput = 0; /* by default don't negotiate
573ddd0451fSJohn Hughes throughput */
5741da177e4SLinus Torvalds x25->facilities.reverse = X25_DEFAULT_REVERSE;
575a64b7b93SShaun Pereira x25->dte_facilities.calling_len = 0;
576a64b7b93SShaun Pereira x25->dte_facilities.called_len = 0;
577a64b7b93SShaun Pereira memset(x25->dte_facilities.called_ae, '\0',
578a64b7b93SShaun Pereira sizeof(x25->dte_facilities.called_ae));
579a64b7b93SShaun Pereira memset(x25->dte_facilities.calling_ae, '\0',
580a64b7b93SShaun Pereira sizeof(x25->dte_facilities.calling_ae));
581a64b7b93SShaun Pereira
5821da177e4SLinus Torvalds rc = 0;
5831da177e4SLinus Torvalds out:
5841da177e4SLinus Torvalds return rc;
5851da177e4SLinus Torvalds }
5861da177e4SLinus Torvalds
x25_make_new(struct sock * osk)5871da177e4SLinus Torvalds static struct sock *x25_make_new(struct sock *osk)
5881da177e4SLinus Torvalds {
5891da177e4SLinus Torvalds struct sock *sk = NULL;
5901da177e4SLinus Torvalds struct x25_sock *x25, *ox25;
5911da177e4SLinus Torvalds
5921da177e4SLinus Torvalds if (osk->sk_type != SOCK_SEQPACKET)
5931da177e4SLinus Torvalds goto out;
5941da177e4SLinus Torvalds
59511aa9c28SEric W. Biederman if ((sk = x25_alloc_socket(sock_net(osk), 0)) == NULL)
5961da177e4SLinus Torvalds goto out;
5971da177e4SLinus Torvalds
5981da177e4SLinus Torvalds x25 = x25_sk(sk);
5991da177e4SLinus Torvalds
6001da177e4SLinus Torvalds sk->sk_type = osk->sk_type;
6011da177e4SLinus Torvalds sk->sk_priority = osk->sk_priority;
6021da177e4SLinus Torvalds sk->sk_protocol = osk->sk_protocol;
6031da177e4SLinus Torvalds sk->sk_rcvbuf = osk->sk_rcvbuf;
6041da177e4SLinus Torvalds sk->sk_sndbuf = osk->sk_sndbuf;
6051da177e4SLinus Torvalds sk->sk_state = TCP_ESTABLISHED;
6061da177e4SLinus Torvalds sk->sk_backlog_rcv = osk->sk_backlog_rcv;
607a20a8554SShaun Pereira sock_copy_flags(sk, osk);
6081da177e4SLinus Torvalds
6091da177e4SLinus Torvalds ox25 = x25_sk(osk);
6101da177e4SLinus Torvalds x25->t21 = ox25->t21;
6111da177e4SLinus Torvalds x25->t22 = ox25->t22;
6121da177e4SLinus Torvalds x25->t23 = ox25->t23;
6131da177e4SLinus Torvalds x25->t2 = ox25->t2;
614cb863ffdSandrew hendry x25->flags = ox25->flags;
6151da177e4SLinus Torvalds x25->facilities = ox25->facilities;
616a64b7b93SShaun Pereira x25->dte_facilities = ox25->dte_facilities;
617cb65d506SShaun Pereira x25->cudmatchlength = ox25->cudmatchlength;
6181da177e4SLinus Torvalds
619b7792e34Sandrew hendry clear_bit(X25_INTERRUPT_FLAG, &x25->flags);
6201da177e4SLinus Torvalds x25_init_timers(sk);
6211da177e4SLinus Torvalds out:
6221da177e4SLinus Torvalds return sk;
6231da177e4SLinus Torvalds }
6241da177e4SLinus Torvalds
x25_release(struct socket * sock)6251da177e4SLinus Torvalds static int x25_release(struct socket *sock)
6261da177e4SLinus Torvalds {
6271da177e4SLinus Torvalds struct sock *sk = sock->sk;
6281da177e4SLinus Torvalds struct x25_sock *x25;
6291da177e4SLinus Torvalds
6301da177e4SLinus Torvalds if (!sk)
63177b22836SArnd Bergmann return 0;
6321da177e4SLinus Torvalds
6331da177e4SLinus Torvalds x25 = x25_sk(sk);
6341da177e4SLinus Torvalds
63577b22836SArnd Bergmann sock_hold(sk);
63677b22836SArnd Bergmann lock_sock(sk);
6371da177e4SLinus Torvalds switch (x25->state) {
6381da177e4SLinus Torvalds
6391da177e4SLinus Torvalds case X25_STATE_0:
6401da177e4SLinus Torvalds case X25_STATE_2:
6411da177e4SLinus Torvalds x25_disconnect(sk, 0, 0, 0);
64277b22836SArnd Bergmann __x25_destroy_socket(sk);
6431da177e4SLinus Torvalds goto out;
6441da177e4SLinus Torvalds
6451da177e4SLinus Torvalds case X25_STATE_1:
6461da177e4SLinus Torvalds case X25_STATE_3:
6471da177e4SLinus Torvalds case X25_STATE_4:
6481da177e4SLinus Torvalds x25_clear_queues(sk);
6491da177e4SLinus Torvalds x25_write_internal(sk, X25_CLEAR_REQUEST);
6501da177e4SLinus Torvalds x25_start_t23timer(sk);
6511da177e4SLinus Torvalds x25->state = X25_STATE_2;
6521da177e4SLinus Torvalds sk->sk_state = TCP_CLOSE;
6531da177e4SLinus Torvalds sk->sk_shutdown |= SEND_SHUTDOWN;
6541da177e4SLinus Torvalds sk->sk_state_change(sk);
6551da177e4SLinus Torvalds sock_set_flag(sk, SOCK_DEAD);
6561da177e4SLinus Torvalds sock_set_flag(sk, SOCK_DESTROY);
6571da177e4SLinus Torvalds break;
658f8fc57e8SMartin Schiller
659f8fc57e8SMartin Schiller case X25_STATE_5:
660f8fc57e8SMartin Schiller x25_write_internal(sk, X25_CLEAR_REQUEST);
661f8fc57e8SMartin Schiller x25_disconnect(sk, 0, 0, 0);
662f8fc57e8SMartin Schiller __x25_destroy_socket(sk);
663f8fc57e8SMartin Schiller goto out;
6641da177e4SLinus Torvalds }
6651da177e4SLinus Torvalds
666c751e4f8SDavid S. Miller sock_orphan(sk);
6671da177e4SLinus Torvalds out:
66877b22836SArnd Bergmann release_sock(sk);
66977b22836SArnd Bergmann sock_put(sk);
6701da177e4SLinus Torvalds return 0;
6711da177e4SLinus Torvalds }
6721da177e4SLinus Torvalds
x25_bind(struct socket * sock,struct sockaddr * uaddr,int addr_len)6731da177e4SLinus Torvalds static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
6741da177e4SLinus Torvalds {
6751da177e4SLinus Torvalds struct sock *sk = sock->sk;
6761da177e4SLinus Torvalds struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
677a9288525Sandrew hendry int len, i, rc = 0;
6781da177e4SLinus Torvalds
679797a22bdSEric Dumazet if (addr_len != sizeof(struct sockaddr_x25) ||
6806ee50c8eSDan Carpenter addr->sx25_family != AF_X25 ||
6816ee50c8eSDan Carpenter strnlen(addr->sx25_addr.x25_addr, X25_ADDR_LEN) == X25_ADDR_LEN) {
68291774904SArnd Bergmann rc = -EINVAL;
68391774904SArnd Bergmann goto out;
68491774904SArnd Bergmann }
6851da177e4SLinus Torvalds
68606137619SMartin Schiller /* check for the null_x25_address */
68706137619SMartin Schiller if (strcmp(addr->sx25_addr.x25_addr, null_x25_address.x25_addr)) {
68806137619SMartin Schiller
689a9288525Sandrew hendry len = strlen(addr->sx25_addr.x25_addr);
690a9288525Sandrew hendry for (i = 0; i < len; i++) {
691a9288525Sandrew hendry if (!isdigit(addr->sx25_addr.x25_addr[i])) {
692a9288525Sandrew hendry rc = -EINVAL;
693a9288525Sandrew hendry goto out;
694a9288525Sandrew hendry }
695a9288525Sandrew hendry }
69606137619SMartin Schiller }
697a9288525Sandrew hendry
69890c27297Sandrew hendry lock_sock(sk);
699797a22bdSEric Dumazet if (sock_flag(sk, SOCK_ZAPPED)) {
7001da177e4SLinus Torvalds x25_sk(sk)->source_addr = addr->sx25_addr;
7011da177e4SLinus Torvalds x25_insert_socket(sk);
7021da177e4SLinus Torvalds sock_reset_flag(sk, SOCK_ZAPPED);
703797a22bdSEric Dumazet } else {
704797a22bdSEric Dumazet rc = -EINVAL;
705797a22bdSEric Dumazet }
70690c27297Sandrew hendry release_sock(sk);
7071da177e4SLinus Torvalds SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
70891774904SArnd Bergmann out:
70991774904SArnd Bergmann return rc;
7101da177e4SLinus Torvalds }
7111da177e4SLinus Torvalds
x25_wait_for_connection_establishment(struct sock * sk)7121da177e4SLinus Torvalds static int x25_wait_for_connection_establishment(struct sock *sk)
7131da177e4SLinus Torvalds {
7141da177e4SLinus Torvalds DECLARE_WAITQUEUE(wait, current);
7151da177e4SLinus Torvalds int rc;
7161da177e4SLinus Torvalds
717aa395145SEric Dumazet add_wait_queue_exclusive(sk_sleep(sk), &wait);
7181da177e4SLinus Torvalds for (;;) {
7191da177e4SLinus Torvalds __set_current_state(TASK_INTERRUPTIBLE);
7201da177e4SLinus Torvalds rc = -ERESTARTSYS;
7211da177e4SLinus Torvalds if (signal_pending(current))
7221da177e4SLinus Torvalds break;
7231da177e4SLinus Torvalds rc = sock_error(sk);
7241da177e4SLinus Torvalds if (rc) {
7251da177e4SLinus Torvalds sk->sk_socket->state = SS_UNCONNECTED;
7261da177e4SLinus Torvalds break;
7271da177e4SLinus Torvalds }
728944e594cSMartin Schiller rc = -ENOTCONN;
729944e594cSMartin Schiller if (sk->sk_state == TCP_CLOSE) {
730944e594cSMartin Schiller sk->sk_socket->state = SS_UNCONNECTED;
731944e594cSMartin Schiller break;
732944e594cSMartin Schiller }
7331da177e4SLinus Torvalds rc = 0;
7341da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED) {
7351da177e4SLinus Torvalds release_sock(sk);
7361da177e4SLinus Torvalds schedule();
7371da177e4SLinus Torvalds lock_sock(sk);
7381da177e4SLinus Torvalds } else
7391da177e4SLinus Torvalds break;
7401da177e4SLinus Torvalds }
7411da177e4SLinus Torvalds __set_current_state(TASK_RUNNING);
742aa395145SEric Dumazet remove_wait_queue(sk_sleep(sk), &wait);
7431da177e4SLinus Torvalds return rc;
7441da177e4SLinus Torvalds }
7451da177e4SLinus Torvalds
x25_connect(struct socket * sock,struct sockaddr * uaddr,int addr_len,int flags)7461da177e4SLinus Torvalds static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
7471da177e4SLinus Torvalds int addr_len, int flags)
7481da177e4SLinus Torvalds {
7491da177e4SLinus Torvalds struct sock *sk = sock->sk;
7501da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk);
7511da177e4SLinus Torvalds struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
7521da177e4SLinus Torvalds struct x25_route *rt;
7531da177e4SLinus Torvalds int rc = 0;
7541da177e4SLinus Torvalds
7551da177e4SLinus Torvalds lock_sock(sk);
7561da177e4SLinus Torvalds if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
7571da177e4SLinus Torvalds sock->state = SS_CONNECTED;
7581da177e4SLinus Torvalds goto out; /* Connect completed during a ERESTARTSYS event */
7591da177e4SLinus Torvalds }
7601da177e4SLinus Torvalds
7611da177e4SLinus Torvalds rc = -ECONNREFUSED;
7621da177e4SLinus Torvalds if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
7631da177e4SLinus Torvalds sock->state = SS_UNCONNECTED;
7641da177e4SLinus Torvalds goto out;
7651da177e4SLinus Torvalds }
7661da177e4SLinus Torvalds
7671da177e4SLinus Torvalds rc = -EISCONN; /* No reconnect on a seqpacket socket */
7681da177e4SLinus Torvalds if (sk->sk_state == TCP_ESTABLISHED)
7691da177e4SLinus Torvalds goto out;
7701da177e4SLinus Torvalds
771e21dba7aSMartin Schiller rc = -EALREADY; /* Do nothing if call is already in progress */
772e21dba7aSMartin Schiller if (sk->sk_state == TCP_SYN_SENT)
773e21dba7aSMartin Schiller goto out;
774e21dba7aSMartin Schiller
7751da177e4SLinus Torvalds sk->sk_state = TCP_CLOSE;
7761da177e4SLinus Torvalds sock->state = SS_UNCONNECTED;
7771da177e4SLinus Torvalds
7781da177e4SLinus Torvalds rc = -EINVAL;
7791da177e4SLinus Torvalds if (addr_len != sizeof(struct sockaddr_x25) ||
7806ee50c8eSDan Carpenter addr->sx25_family != AF_X25 ||
7816ee50c8eSDan Carpenter strnlen(addr->sx25_addr.x25_addr, X25_ADDR_LEN) == X25_ADDR_LEN)
7821da177e4SLinus Torvalds goto out;
7831da177e4SLinus Torvalds
7841da177e4SLinus Torvalds rc = -ENETUNREACH;
7851da177e4SLinus Torvalds rt = x25_get_route(&addr->sx25_addr);
7861da177e4SLinus Torvalds if (!rt)
7871da177e4SLinus Torvalds goto out;
7881da177e4SLinus Torvalds
7891da177e4SLinus Torvalds x25->neighbour = x25_get_neigh(rt->dev);
7901da177e4SLinus Torvalds if (!x25->neighbour)
7911da177e4SLinus Torvalds goto out_put_route;
7921da177e4SLinus Torvalds
7931da177e4SLinus Torvalds x25_limit_facilities(&x25->facilities, x25->neighbour);
7941da177e4SLinus Torvalds
7951da177e4SLinus Torvalds x25->lci = x25_new_lci(x25->neighbour);
7961da177e4SLinus Torvalds if (!x25->lci)
7971da177e4SLinus Torvalds goto out_put_neigh;
7981da177e4SLinus Torvalds
7991da177e4SLinus Torvalds rc = -EINVAL;
8001da177e4SLinus Torvalds if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
8011da177e4SLinus Torvalds goto out_put_neigh;
8021da177e4SLinus Torvalds
8031da177e4SLinus Torvalds if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
8041da177e4SLinus Torvalds memset(&x25->source_addr, '\0', X25_ADDR_LEN);
8051da177e4SLinus Torvalds
8061da177e4SLinus Torvalds x25->dest_addr = addr->sx25_addr;
8071da177e4SLinus Torvalds
8081da177e4SLinus Torvalds /* Move to connecting socket, start sending Connect Requests */
8091da177e4SLinus Torvalds sock->state = SS_CONNECTING;
8101da177e4SLinus Torvalds sk->sk_state = TCP_SYN_SENT;
8111da177e4SLinus Torvalds
8121da177e4SLinus Torvalds x25->state = X25_STATE_1;
8131da177e4SLinus Torvalds
8141da177e4SLinus Torvalds x25_write_internal(sk, X25_CALL_REQUEST);
8151da177e4SLinus Torvalds
8161da177e4SLinus Torvalds x25_start_heartbeat(sk);
8171da177e4SLinus Torvalds x25_start_t21timer(sk);
8181da177e4SLinus Torvalds
8191da177e4SLinus Torvalds /* Now the loop */
8201da177e4SLinus Torvalds rc = -EINPROGRESS;
8211da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
822e21dba7aSMartin Schiller goto out;
8231da177e4SLinus Torvalds
8241da177e4SLinus Torvalds rc = x25_wait_for_connection_establishment(sk);
8251da177e4SLinus Torvalds if (rc)
8261da177e4SLinus Torvalds goto out_put_neigh;
8271da177e4SLinus Torvalds
8281da177e4SLinus Torvalds sock->state = SS_CONNECTED;
8291da177e4SLinus Torvalds rc = 0;
8301da177e4SLinus Torvalds out_put_neigh:
83136118230SMartin Schiller if (rc && x25->neighbour) {
83295d6ebd5SEric Dumazet read_lock_bh(&x25_list_lock);
8331da177e4SLinus Torvalds x25_neigh_put(x25->neighbour);
83495d6ebd5SEric Dumazet x25->neighbour = NULL;
83595d6ebd5SEric Dumazet read_unlock_bh(&x25_list_lock);
836ee74d0bdSEric Dumazet x25->state = X25_STATE_0;
83795d6ebd5SEric Dumazet }
8381da177e4SLinus Torvalds out_put_route:
8391da177e4SLinus Torvalds x25_route_put(rt);
8401da177e4SLinus Torvalds out:
8411da177e4SLinus Torvalds release_sock(sk);
8421da177e4SLinus Torvalds return rc;
8431da177e4SLinus Torvalds }
8441da177e4SLinus Torvalds
x25_wait_for_data(struct sock * sk,long timeout)845bac37ec8SShaun Pereira static int x25_wait_for_data(struct sock *sk, long timeout)
8461da177e4SLinus Torvalds {
8471da177e4SLinus Torvalds DECLARE_WAITQUEUE(wait, current);
8481da177e4SLinus Torvalds int rc = 0;
8491da177e4SLinus Torvalds
850aa395145SEric Dumazet add_wait_queue_exclusive(sk_sleep(sk), &wait);
8511da177e4SLinus Torvalds for (;;) {
8521da177e4SLinus Torvalds __set_current_state(TASK_INTERRUPTIBLE);
8531da177e4SLinus Torvalds if (sk->sk_shutdown & RCV_SHUTDOWN)
8541da177e4SLinus Torvalds break;
8551da177e4SLinus Torvalds rc = -ERESTARTSYS;
8561da177e4SLinus Torvalds if (signal_pending(current))
8571da177e4SLinus Torvalds break;
8581da177e4SLinus Torvalds rc = -EAGAIN;
8591da177e4SLinus Torvalds if (!timeout)
8601da177e4SLinus Torvalds break;
8611da177e4SLinus Torvalds rc = 0;
8621da177e4SLinus Torvalds if (skb_queue_empty(&sk->sk_receive_queue)) {
8631da177e4SLinus Torvalds release_sock(sk);
8641da177e4SLinus Torvalds timeout = schedule_timeout(timeout);
8651da177e4SLinus Torvalds lock_sock(sk);
8661da177e4SLinus Torvalds } else
8671da177e4SLinus Torvalds break;
8681da177e4SLinus Torvalds }
8691da177e4SLinus Torvalds __set_current_state(TASK_RUNNING);
870aa395145SEric Dumazet remove_wait_queue(sk_sleep(sk), &wait);
8711da177e4SLinus Torvalds return rc;
8721da177e4SLinus Torvalds }
8731da177e4SLinus Torvalds
x25_accept(struct socket * sock,struct socket * newsock,int flags,bool kern)874cdfbabfbSDavid Howells static int x25_accept(struct socket *sock, struct socket *newsock, int flags,
875cdfbabfbSDavid Howells bool kern)
8761da177e4SLinus Torvalds {
8771da177e4SLinus Torvalds struct sock *sk = sock->sk;
8781da177e4SLinus Torvalds struct sock *newsk;
8791da177e4SLinus Torvalds struct sk_buff *skb;
8801da177e4SLinus Torvalds int rc = -EINVAL;
8811da177e4SLinus Torvalds
882141646ceSAndrew Hendry if (!sk)
8831da177e4SLinus Torvalds goto out;
8841da177e4SLinus Torvalds
8851da177e4SLinus Torvalds rc = -EOPNOTSUPP;
8861da177e4SLinus Torvalds if (sk->sk_type != SOCK_SEQPACKET)
8871da177e4SLinus Torvalds goto out;
8881da177e4SLinus Torvalds
8891da177e4SLinus Torvalds lock_sock(sk);
890141646ceSAndrew Hendry rc = -EINVAL;
891141646ceSAndrew Hendry if (sk->sk_state != TCP_LISTEN)
892141646ceSAndrew Hendry goto out2;
893141646ceSAndrew Hendry
8941da177e4SLinus Torvalds rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
8951da177e4SLinus Torvalds if (rc)
8961da177e4SLinus Torvalds goto out2;
8971da177e4SLinus Torvalds skb = skb_dequeue(&sk->sk_receive_queue);
8981da177e4SLinus Torvalds rc = -EINVAL;
8991da177e4SLinus Torvalds if (!skb->sk)
9001da177e4SLinus Torvalds goto out2;
9011da177e4SLinus Torvalds newsk = skb->sk;
902b61d38e0SDavid S. Miller sock_graft(newsk, newsock);
9031da177e4SLinus Torvalds
9041da177e4SLinus Torvalds /* Now attach up the new socket */
9051da177e4SLinus Torvalds skb->sk = NULL;
9061da177e4SLinus Torvalds kfree_skb(skb);
9077976a11bSEric Dumazet sk_acceptq_removed(sk);
9081da177e4SLinus Torvalds newsock->state = SS_CONNECTED;
9091da177e4SLinus Torvalds rc = 0;
9101da177e4SLinus Torvalds out2:
9111da177e4SLinus Torvalds release_sock(sk);
9121da177e4SLinus Torvalds out:
9131da177e4SLinus Torvalds return rc;
9141da177e4SLinus Torvalds }
9151da177e4SLinus Torvalds
x25_getname(struct socket * sock,struct sockaddr * uaddr,int peer)9161da177e4SLinus Torvalds static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
9179b2c45d4SDenys Vlasenko int peer)
9181da177e4SLinus Torvalds {
9191da177e4SLinus Torvalds struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
9201da177e4SLinus Torvalds struct sock *sk = sock->sk;
9211da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk);
92291774904SArnd Bergmann int rc = 0;
9231da177e4SLinus Torvalds
9241da177e4SLinus Torvalds if (peer) {
92591774904SArnd Bergmann if (sk->sk_state != TCP_ESTABLISHED) {
92691774904SArnd Bergmann rc = -ENOTCONN;
92791774904SArnd Bergmann goto out;
92891774904SArnd Bergmann }
9291da177e4SLinus Torvalds sx25->sx25_addr = x25->dest_addr;
9301da177e4SLinus Torvalds } else
9311da177e4SLinus Torvalds sx25->sx25_addr = x25->source_addr;
9321da177e4SLinus Torvalds
9331da177e4SLinus Torvalds sx25->sx25_family = AF_X25;
9349b2c45d4SDenys Vlasenko rc = sizeof(*sx25);
9351da177e4SLinus Torvalds
93691774904SArnd Bergmann out:
93791774904SArnd Bergmann return rc;
93891774904SArnd Bergmann }
93991774904SArnd Bergmann
x25_rx_call_request(struct sk_buff * skb,struct x25_neigh * nb,unsigned int lci)9401da177e4SLinus Torvalds int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
9411da177e4SLinus Torvalds unsigned int lci)
9421da177e4SLinus Torvalds {
9431da177e4SLinus Torvalds struct sock *sk;
9441da177e4SLinus Torvalds struct sock *make;
9451da177e4SLinus Torvalds struct x25_sock *makex25;
9461da177e4SLinus Torvalds struct x25_address source_addr, dest_addr;
9471da177e4SLinus Torvalds struct x25_facilities facilities;
948a64b7b93SShaun Pereira struct x25_dte_facilities dte_facilities;
94995a9dc43SAndrew Hendry int len, addr_len, rc;
9501da177e4SLinus Torvalds
9511da177e4SLinus Torvalds /*
9521da177e4SLinus Torvalds * Remove the LCI and frame type.
9531da177e4SLinus Torvalds */
9541da177e4SLinus Torvalds skb_pull(skb, X25_STD_MIN_LEN);
9551da177e4SLinus Torvalds
9561da177e4SLinus Torvalds /*
9571da177e4SLinus Torvalds * Extract the X.25 addresses and convert them to ASCII strings,
9581da177e4SLinus Torvalds * and remove them.
959f5eb917bSJohn Hughes *
960f5eb917bSJohn Hughes * Address block is mandatory in call request packets
9611da177e4SLinus Torvalds */
962f5eb917bSJohn Hughes addr_len = x25_parse_address_block(skb, &source_addr, &dest_addr);
963f5eb917bSJohn Hughes if (addr_len <= 0)
964f5eb917bSJohn Hughes goto out_clear_request;
96595a9dc43SAndrew Hendry skb_pull(skb, addr_len);
9661da177e4SLinus Torvalds
9671da177e4SLinus Torvalds /*
9681da177e4SLinus Torvalds * Get the length of the facilities, skip past them for the moment
9691da177e4SLinus Torvalds * get the call user data because this is needed to determine
9701da177e4SLinus Torvalds * the correct listener
971f5eb917bSJohn Hughes *
972f5eb917bSJohn Hughes * Facilities length is mandatory in call request packets
9731da177e4SLinus Torvalds */
974cb101ed2SMatthew Daley if (!pskb_may_pull(skb, 1))
975f5eb917bSJohn Hughes goto out_clear_request;
9761da177e4SLinus Torvalds len = skb->data[0] + 1;
977cb101ed2SMatthew Daley if (!pskb_may_pull(skb, len))
978f5eb917bSJohn Hughes goto out_clear_request;
9791da177e4SLinus Torvalds skb_pull(skb,len);
9801da177e4SLinus Torvalds
9811da177e4SLinus Torvalds /*
982c7fd0d48SMatthew Daley * Ensure that the amount of call user data is valid.
983c7fd0d48SMatthew Daley */
984c7fd0d48SMatthew Daley if (skb->len > X25_MAX_CUD_LEN)
985c7fd0d48SMatthew Daley goto out_clear_request;
986c7fd0d48SMatthew Daley
987c7fd0d48SMatthew Daley /*
988cb101ed2SMatthew Daley * Get all the call user data so it can be used in
989cb101ed2SMatthew Daley * x25_find_listener and skb_copy_from_linear_data up ahead.
990cb101ed2SMatthew Daley */
991cb101ed2SMatthew Daley if (!pskb_may_pull(skb, skb->len))
992cb101ed2SMatthew Daley goto out_clear_request;
993cb101ed2SMatthew Daley
994cb101ed2SMatthew Daley /*
9951da177e4SLinus Torvalds * Find a listener for the particular address/cud pair.
9961da177e4SLinus Torvalds */
997cb65d506SShaun Pereira sk = x25_find_listener(&source_addr,skb);
998cb65d506SShaun Pereira skb_push(skb,len);
9991da177e4SLinus Torvalds
100095a9dc43SAndrew Hendry if (sk != NULL && sk_acceptq_is_full(sk)) {
100195a9dc43SAndrew Hendry goto out_sock_put;
100295a9dc43SAndrew Hendry }
100395a9dc43SAndrew Hendry
10041da177e4SLinus Torvalds /*
100595a9dc43SAndrew Hendry * We dont have any listeners for this incoming call.
100695a9dc43SAndrew Hendry * Try forwarding it.
10071da177e4SLinus Torvalds */
100895a9dc43SAndrew Hendry if (sk == NULL) {
100995a9dc43SAndrew Hendry skb_push(skb, addr_len + X25_STD_MIN_LEN);
101039e21c0dSAndrew Hendry if (sysctl_x25_forward &&
101139e21c0dSAndrew Hendry x25_forward_call(&dest_addr, nb, skb, lci) > 0)
101295a9dc43SAndrew Hendry {
101395a9dc43SAndrew Hendry /* Call was forwarded, dont process it any more */
101495a9dc43SAndrew Hendry kfree_skb(skb);
101595a9dc43SAndrew Hendry rc = 1;
101695a9dc43SAndrew Hendry goto out;
101795a9dc43SAndrew Hendry } else {
101895a9dc43SAndrew Hendry /* No listeners, can't forward, clear the call */
10191da177e4SLinus Torvalds goto out_clear_request;
102095a9dc43SAndrew Hendry }
102195a9dc43SAndrew Hendry }
10221da177e4SLinus Torvalds
10231da177e4SLinus Torvalds /*
10241da177e4SLinus Torvalds * Try to reach a compromise on the requested facilities.
10251da177e4SLinus Torvalds */
1026a64b7b93SShaun Pereira len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities);
1027a64b7b93SShaun Pereira if (len == -1)
10281da177e4SLinus Torvalds goto out_sock_put;
10291da177e4SLinus Torvalds
10301da177e4SLinus Torvalds /*
10311da177e4SLinus Torvalds * current neighbour/link might impose additional limits
10328406d38fSBhaskar Chowdhury * on certain facilities
10331da177e4SLinus Torvalds */
10341da177e4SLinus Torvalds
10351da177e4SLinus Torvalds x25_limit_facilities(&facilities, nb);
10361da177e4SLinus Torvalds
10371da177e4SLinus Torvalds /*
10381da177e4SLinus Torvalds * Try to create a new socket.
10391da177e4SLinus Torvalds */
10401da177e4SLinus Torvalds make = x25_make_new(sk);
10411da177e4SLinus Torvalds if (!make)
10421da177e4SLinus Torvalds goto out_sock_put;
10431da177e4SLinus Torvalds
10441da177e4SLinus Torvalds /*
10451da177e4SLinus Torvalds * Remove the facilities
10461da177e4SLinus Torvalds */
10471da177e4SLinus Torvalds skb_pull(skb, len);
10481da177e4SLinus Torvalds
10491da177e4SLinus Torvalds skb->sk = make;
10501da177e4SLinus Torvalds make->sk_state = TCP_ESTABLISHED;
10511da177e4SLinus Torvalds
10521da177e4SLinus Torvalds makex25 = x25_sk(make);
10531da177e4SLinus Torvalds makex25->lci = lci;
10541da177e4SLinus Torvalds makex25->dest_addr = dest_addr;
10551da177e4SLinus Torvalds makex25->source_addr = source_addr;
10564ee18c17SXie He x25_neigh_hold(nb);
10571da177e4SLinus Torvalds makex25->neighbour = nb;
10581da177e4SLinus Torvalds makex25->facilities = facilities;
1059a64b7b93SShaun Pereira makex25->dte_facilities= dte_facilities;
10601da177e4SLinus Torvalds makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
1061cb65d506SShaun Pereira /* ensure no reverse facil on accept */
1062cb65d506SShaun Pereira makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
1063a64b7b93SShaun Pereira /* ensure no calling address extension on accept */
1064a64b7b93SShaun Pereira makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE;
1065cb65d506SShaun Pereira makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
10661da177e4SLinus Torvalds
106737cda787Sandrew hendry /* Normally all calls are accepted immediately */
106837cda787Sandrew hendry if (test_bit(X25_ACCPT_APPRV_FLAG, &makex25->flags)) {
10691da177e4SLinus Torvalds x25_write_internal(make, X25_CALL_ACCEPTED);
10701da177e4SLinus Torvalds makex25->state = X25_STATE_3;
1071f8fc57e8SMartin Schiller } else {
1072f8fc57e8SMartin Schiller makex25->state = X25_STATE_5;
1073ebc3f64bSShaun Pereira }
10741da177e4SLinus Torvalds
1075cb65d506SShaun Pereira /*
1076cb65d506SShaun Pereira * Incoming Call User Data.
1077cb65d506SShaun Pereira */
1078d626f62bSArnaldo Carvalho de Melo skb_copy_from_linear_data(skb, makex25->calluserdata.cuddata, skb->len);
1079cb65d506SShaun Pereira makex25->calluserdata.cudlength = skb->len;
1080cb65d506SShaun Pereira
10817976a11bSEric Dumazet sk_acceptq_added(sk);
10821da177e4SLinus Torvalds
10831da177e4SLinus Torvalds x25_insert_socket(make);
10841da177e4SLinus Torvalds
10851da177e4SLinus Torvalds skb_queue_head(&sk->sk_receive_queue, skb);
10861da177e4SLinus Torvalds
10871da177e4SLinus Torvalds x25_start_heartbeat(make);
10881da177e4SLinus Torvalds
10891da177e4SLinus Torvalds if (!sock_flag(sk, SOCK_DEAD))
1090676d2369SDavid S. Miller sk->sk_data_ready(sk);
10911da177e4SLinus Torvalds rc = 1;
10921da177e4SLinus Torvalds sock_put(sk);
10931da177e4SLinus Torvalds out:
10941da177e4SLinus Torvalds return rc;
10951da177e4SLinus Torvalds out_sock_put:
10961da177e4SLinus Torvalds sock_put(sk);
10971da177e4SLinus Torvalds out_clear_request:
10981da177e4SLinus Torvalds rc = 0;
10991da177e4SLinus Torvalds x25_transmit_clear_request(nb, lci, 0x01);
11001da177e4SLinus Torvalds goto out;
11011da177e4SLinus Torvalds }
11021da177e4SLinus Torvalds
x25_sendmsg(struct socket * sock,struct msghdr * msg,size_t len)11031b784140SYing Xue static int x25_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
11041da177e4SLinus Torvalds {
11051da177e4SLinus Torvalds struct sock *sk = sock->sk;
11061da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk);
1107342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_x25 *, usx25, msg->msg_name);
11081da177e4SLinus Torvalds struct sockaddr_x25 sx25;
11091da177e4SLinus Torvalds struct sk_buff *skb;
11101da177e4SLinus Torvalds unsigned char *asmptr;
11111da177e4SLinus Torvalds int noblock = msg->msg_flags & MSG_DONTWAIT;
11121da177e4SLinus Torvalds size_t size;
11131da177e4SLinus Torvalds int qbit = 0, rc = -EINVAL;
11141da177e4SLinus Torvalds
111577b22836SArnd Bergmann lock_sock(sk);
11161da177e4SLinus Torvalds if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
11171da177e4SLinus Torvalds goto out;
11181da177e4SLinus Torvalds
11191da177e4SLinus Torvalds /* we currently don't support segmented records at the user interface */
11201da177e4SLinus Torvalds if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
11211da177e4SLinus Torvalds goto out;
11221da177e4SLinus Torvalds
11231da177e4SLinus Torvalds rc = -EADDRNOTAVAIL;
11241da177e4SLinus Torvalds if (sock_flag(sk, SOCK_ZAPPED))
11251da177e4SLinus Torvalds goto out;
11261da177e4SLinus Torvalds
11271da177e4SLinus Torvalds rc = -EPIPE;
11281da177e4SLinus Torvalds if (sk->sk_shutdown & SEND_SHUTDOWN) {
11291da177e4SLinus Torvalds send_sig(SIGPIPE, current, 0);
11301da177e4SLinus Torvalds goto out;
11311da177e4SLinus Torvalds }
11321da177e4SLinus Torvalds
11331da177e4SLinus Torvalds rc = -ENETUNREACH;
11341da177e4SLinus Torvalds if (!x25->neighbour)
11351da177e4SLinus Torvalds goto out;
11361da177e4SLinus Torvalds
11371da177e4SLinus Torvalds if (usx25) {
11381da177e4SLinus Torvalds rc = -EINVAL;
11391da177e4SLinus Torvalds if (msg->msg_namelen < sizeof(sx25))
11401da177e4SLinus Torvalds goto out;
11411da177e4SLinus Torvalds memcpy(&sx25, usx25, sizeof(sx25));
11421da177e4SLinus Torvalds rc = -EISCONN;
11431da177e4SLinus Torvalds if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
11441da177e4SLinus Torvalds goto out;
11451da177e4SLinus Torvalds rc = -EINVAL;
11461da177e4SLinus Torvalds if (sx25.sx25_family != AF_X25)
11471da177e4SLinus Torvalds goto out;
11481da177e4SLinus Torvalds } else {
11491da177e4SLinus Torvalds /*
11501da177e4SLinus Torvalds * FIXME 1003.1g - if the socket is like this because
11511da177e4SLinus Torvalds * it has become closed (not started closed) we ought
11521da177e4SLinus Torvalds * to SIGPIPE, EPIPE;
11531da177e4SLinus Torvalds */
11541da177e4SLinus Torvalds rc = -ENOTCONN;
11551da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED)
11561da177e4SLinus Torvalds goto out;
11571da177e4SLinus Torvalds
11581da177e4SLinus Torvalds sx25.sx25_family = AF_X25;
11591da177e4SLinus Torvalds sx25.sx25_addr = x25->dest_addr;
11601da177e4SLinus Torvalds }
11611da177e4SLinus Torvalds
116283e0bbcbSAlan Cox /* Sanity check the packet size */
116383e0bbcbSAlan Cox if (len > 65535) {
116483e0bbcbSAlan Cox rc = -EMSGSIZE;
116583e0bbcbSAlan Cox goto out;
116683e0bbcbSAlan Cox }
116783e0bbcbSAlan Cox
11681da177e4SLinus Torvalds SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
11691da177e4SLinus Torvalds
11701da177e4SLinus Torvalds /* Build a packet */
11711da177e4SLinus Torvalds SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
11721da177e4SLinus Torvalds
11731da177e4SLinus Torvalds if ((msg->msg_flags & MSG_OOB) && len > 32)
11741da177e4SLinus Torvalds len = 32;
11751da177e4SLinus Torvalds
11761da177e4SLinus Torvalds size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
11771da177e4SLinus Torvalds
117877b22836SArnd Bergmann release_sock(sk);
11791da177e4SLinus Torvalds skb = sock_alloc_send_skb(sk, size, noblock, &rc);
118077b22836SArnd Bergmann lock_sock(sk);
11811da177e4SLinus Torvalds if (!skb)
11821da177e4SLinus Torvalds goto out;
11831da177e4SLinus Torvalds X25_SKB_CB(skb)->flags = msg->msg_flags;
11841da177e4SLinus Torvalds
11851da177e4SLinus Torvalds skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
11861da177e4SLinus Torvalds
11871da177e4SLinus Torvalds /*
11881da177e4SLinus Torvalds * Put the data on the end
11891da177e4SLinus Torvalds */
11901da177e4SLinus Torvalds SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
11911da177e4SLinus Torvalds
1192eeeb0374SArnaldo Carvalho de Melo skb_reset_transport_header(skb);
1193eeeb0374SArnaldo Carvalho de Melo skb_put(skb, len);
11941da177e4SLinus Torvalds
11956ce8e9ceSAl Viro rc = memcpy_from_msg(skb_transport_header(skb), msg, len);
11961da177e4SLinus Torvalds if (rc)
11971da177e4SLinus Torvalds goto out_kfree_skb;
11981da177e4SLinus Torvalds
11991da177e4SLinus Torvalds /*
12001da177e4SLinus Torvalds * If the Q BIT Include socket option is in force, the first
12011da177e4SLinus Torvalds * byte of the user data is the logical value of the Q Bit.
12021da177e4SLinus Torvalds */
1203cb863ffdSandrew hendry if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
1204cb101ed2SMatthew Daley if (!pskb_may_pull(skb, 1))
1205cb101ed2SMatthew Daley goto out_kfree_skb;
1206cb101ed2SMatthew Daley
12071da177e4SLinus Torvalds qbit = skb->data[0];
12081da177e4SLinus Torvalds skb_pull(skb, 1);
12091da177e4SLinus Torvalds }
12101da177e4SLinus Torvalds
12111da177e4SLinus Torvalds /*
12121da177e4SLinus Torvalds * Push down the X.25 header
12131da177e4SLinus Torvalds */
12141da177e4SLinus Torvalds SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
12151da177e4SLinus Torvalds
12161da177e4SLinus Torvalds if (msg->msg_flags & MSG_OOB) {
12171da177e4SLinus Torvalds if (x25->neighbour->extended) {
12181da177e4SLinus Torvalds asmptr = skb_push(skb, X25_STD_MIN_LEN);
12191da177e4SLinus Torvalds *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
12201da177e4SLinus Torvalds *asmptr++ = (x25->lci >> 0) & 0xFF;
12211da177e4SLinus Torvalds *asmptr++ = X25_INTERRUPT;
12221da177e4SLinus Torvalds } else {
12231da177e4SLinus Torvalds asmptr = skb_push(skb, X25_STD_MIN_LEN);
12241da177e4SLinus Torvalds *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
12251da177e4SLinus Torvalds *asmptr++ = (x25->lci >> 0) & 0xFF;
12261da177e4SLinus Torvalds *asmptr++ = X25_INTERRUPT;
12271da177e4SLinus Torvalds }
12281da177e4SLinus Torvalds } else {
12291da177e4SLinus Torvalds if (x25->neighbour->extended) {
12301da177e4SLinus Torvalds /* Build an Extended X.25 header */
12311da177e4SLinus Torvalds asmptr = skb_push(skb, X25_EXT_MIN_LEN);
12321da177e4SLinus Torvalds *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
12331da177e4SLinus Torvalds *asmptr++ = (x25->lci >> 0) & 0xFF;
12341da177e4SLinus Torvalds *asmptr++ = X25_DATA;
12351da177e4SLinus Torvalds *asmptr++ = X25_DATA;
12361da177e4SLinus Torvalds } else {
12371da177e4SLinus Torvalds /* Build an Standard X.25 header */
12381da177e4SLinus Torvalds asmptr = skb_push(skb, X25_STD_MIN_LEN);
12391da177e4SLinus Torvalds *asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
12401da177e4SLinus Torvalds *asmptr++ = (x25->lci >> 0) & 0xFF;
12411da177e4SLinus Torvalds *asmptr++ = X25_DATA;
12421da177e4SLinus Torvalds }
12431da177e4SLinus Torvalds
12441da177e4SLinus Torvalds if (qbit)
12451da177e4SLinus Torvalds skb->data[0] |= X25_Q_BIT;
12461da177e4SLinus Torvalds }
12471da177e4SLinus Torvalds
12481da177e4SLinus Torvalds SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
12491da177e4SLinus Torvalds SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
12501da177e4SLinus Torvalds
12511da177e4SLinus Torvalds rc = -ENOTCONN;
12521da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED)
12531da177e4SLinus Torvalds goto out_kfree_skb;
12541da177e4SLinus Torvalds
12551da177e4SLinus Torvalds if (msg->msg_flags & MSG_OOB)
12561da177e4SLinus Torvalds skb_queue_tail(&x25->interrupt_out_queue, skb);
12571da177e4SLinus Torvalds else {
12588db09f26SRoel Kluin rc = x25_output(sk, skb);
12598db09f26SRoel Kluin len = rc;
12608db09f26SRoel Kluin if (rc < 0)
12611da177e4SLinus Torvalds kfree_skb(skb);
1262cb863ffdSandrew hendry else if (test_bit(X25_Q_BIT_FLAG, &x25->flags))
12631da177e4SLinus Torvalds len++;
12641da177e4SLinus Torvalds }
12651da177e4SLinus Torvalds
12661da177e4SLinus Torvalds x25_kick(sk);
12671da177e4SLinus Torvalds rc = len;
12681da177e4SLinus Torvalds out:
126977b22836SArnd Bergmann release_sock(sk);
12701da177e4SLinus Torvalds return rc;
12711da177e4SLinus Torvalds out_kfree_skb:
12721da177e4SLinus Torvalds kfree_skb(skb);
12731da177e4SLinus Torvalds goto out;
12741da177e4SLinus Torvalds }
12751da177e4SLinus Torvalds
12761da177e4SLinus Torvalds
x25_recvmsg(struct socket * sock,struct msghdr * msg,size_t size,int flags)12771b784140SYing Xue static int x25_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
12781da177e4SLinus Torvalds int flags)
12791da177e4SLinus Torvalds {
12801da177e4SLinus Torvalds struct sock *sk = sock->sk;
12811da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk);
1282342dfc30SSteffen Hurrle DECLARE_SOCKADDR(struct sockaddr_x25 *, sx25, msg->msg_name);
12831da177e4SLinus Torvalds size_t copied;
1284501e89d3SDave Jones int qbit, header_len;
12851da177e4SLinus Torvalds struct sk_buff *skb;
12861da177e4SLinus Torvalds unsigned char *asmptr;
12871da177e4SLinus Torvalds int rc = -ENOTCONN;
12881da177e4SLinus Torvalds
128977b22836SArnd Bergmann lock_sock(sk);
1290501e89d3SDave Jones
1291501e89d3SDave Jones if (x25->neighbour == NULL)
1292501e89d3SDave Jones goto out;
1293501e89d3SDave Jones
1294501e89d3SDave Jones header_len = x25->neighbour->extended ?
1295501e89d3SDave Jones X25_EXT_MIN_LEN : X25_STD_MIN_LEN;
1296501e89d3SDave Jones
12971da177e4SLinus Torvalds /*
12981da177e4SLinus Torvalds * This works for seqpacket too. The receiver has ordered the queue for
12991da177e4SLinus Torvalds * us! We do one quick check first though
13001da177e4SLinus Torvalds */
13011da177e4SLinus Torvalds if (sk->sk_state != TCP_ESTABLISHED)
13021da177e4SLinus Torvalds goto out;
13031da177e4SLinus Torvalds
13041da177e4SLinus Torvalds if (flags & MSG_OOB) {
13051da177e4SLinus Torvalds rc = -EINVAL;
13061da177e4SLinus Torvalds if (sock_flag(sk, SOCK_URGINLINE) ||
13071da177e4SLinus Torvalds !skb_peek(&x25->interrupt_in_queue))
13081da177e4SLinus Torvalds goto out;
13091da177e4SLinus Torvalds
13101da177e4SLinus Torvalds skb = skb_dequeue(&x25->interrupt_in_queue);
13111da177e4SLinus Torvalds
1312cb101ed2SMatthew Daley if (!pskb_may_pull(skb, X25_STD_MIN_LEN))
1313cb101ed2SMatthew Daley goto out_free_dgram;
1314cb101ed2SMatthew Daley
13151da177e4SLinus Torvalds skb_pull(skb, X25_STD_MIN_LEN);
13161da177e4SLinus Torvalds
13171da177e4SLinus Torvalds /*
13181da177e4SLinus Torvalds * No Q bit information on Interrupt data.
13191da177e4SLinus Torvalds */
1320cb863ffdSandrew hendry if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
13211da177e4SLinus Torvalds asmptr = skb_push(skb, 1);
13221da177e4SLinus Torvalds *asmptr = 0x00;
13231da177e4SLinus Torvalds }
13241da177e4SLinus Torvalds
13251da177e4SLinus Torvalds msg->msg_flags |= MSG_OOB;
13261da177e4SLinus Torvalds } else {
13271da177e4SLinus Torvalds /* Now we can treat all alike */
132877b22836SArnd Bergmann release_sock(sk);
1329f4b41f06SOliver Hartkopp skb = skb_recv_datagram(sk, flags, &rc);
133077b22836SArnd Bergmann lock_sock(sk);
13311da177e4SLinus Torvalds if (!skb)
13321da177e4SLinus Torvalds goto out;
13331da177e4SLinus Torvalds
1334cb101ed2SMatthew Daley if (!pskb_may_pull(skb, header_len))
1335cb101ed2SMatthew Daley goto out_free_dgram;
1336cb101ed2SMatthew Daley
13371da177e4SLinus Torvalds qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
13381da177e4SLinus Torvalds
1339cb101ed2SMatthew Daley skb_pull(skb, header_len);
13401da177e4SLinus Torvalds
1341cb863ffdSandrew hendry if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
13421da177e4SLinus Torvalds asmptr = skb_push(skb, 1);
13431da177e4SLinus Torvalds *asmptr = qbit;
13441da177e4SLinus Torvalds }
13451da177e4SLinus Torvalds }
13461da177e4SLinus Torvalds
1347badff6d0SArnaldo Carvalho de Melo skb_reset_transport_header(skb);
13481da177e4SLinus Torvalds copied = skb->len;
13491da177e4SLinus Torvalds
13501da177e4SLinus Torvalds if (copied > size) {
13511da177e4SLinus Torvalds copied = size;
13521da177e4SLinus Torvalds msg->msg_flags |= MSG_TRUNC;
13531da177e4SLinus Torvalds }
13541da177e4SLinus Torvalds
13551da177e4SLinus Torvalds /* Currently, each datagram always contains a complete record */
13561da177e4SLinus Torvalds msg->msg_flags |= MSG_EOR;
13571da177e4SLinus Torvalds
135851f3d02bSDavid S. Miller rc = skb_copy_datagram_msg(skb, 0, msg, copied);
13591da177e4SLinus Torvalds if (rc)
13601da177e4SLinus Torvalds goto out_free_dgram;
13611da177e4SLinus Torvalds
13621da177e4SLinus Torvalds if (sx25) {
13631da177e4SLinus Torvalds sx25->sx25_family = AF_X25;
13641da177e4SLinus Torvalds sx25->sx25_addr = x25->dest_addr;
1365f3d33426SHannes Frederic Sowa msg->msg_namelen = sizeof(*sx25);
13661da177e4SLinus Torvalds }
13671da177e4SLinus Torvalds
13681da177e4SLinus Torvalds x25_check_rbuf(sk);
13691da177e4SLinus Torvalds rc = copied;
13701da177e4SLinus Torvalds out_free_dgram:
13711da177e4SLinus Torvalds skb_free_datagram(sk, skb);
13721da177e4SLinus Torvalds out:
137377b22836SArnd Bergmann release_sock(sk);
13741da177e4SLinus Torvalds return rc;
13751da177e4SLinus Torvalds }
13761da177e4SLinus Torvalds
13771da177e4SLinus Torvalds
x25_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)13781da177e4SLinus Torvalds static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
13791da177e4SLinus Torvalds {
13801da177e4SLinus Torvalds struct sock *sk = sock->sk;
13811da177e4SLinus Torvalds struct x25_sock *x25 = x25_sk(sk);
13821da177e4SLinus Torvalds void __user *argp = (void __user *)arg;
13831da177e4SLinus Torvalds int rc;
13841da177e4SLinus Torvalds
13851da177e4SLinus Torvalds switch (cmd) {
13861da177e4SLinus Torvalds case TIOCOUTQ: {
138770be998cSandrew hendry int amount;
138831e6d363SEric Dumazet
138970be998cSandrew hendry amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
13901da177e4SLinus Torvalds if (amount < 0)
13911da177e4SLinus Torvalds amount = 0;
13921da177e4SLinus Torvalds rc = put_user(amount, (unsigned int __user *)argp);
13931da177e4SLinus Torvalds break;
13941da177e4SLinus Torvalds }
13951da177e4SLinus Torvalds
13961da177e4SLinus Torvalds case TIOCINQ: {
13971da177e4SLinus Torvalds struct sk_buff *skb;
13981da177e4SLinus Torvalds int amount = 0;
13991da177e4SLinus Torvalds /*
14001da177e4SLinus Torvalds * These two are safe on a single CPU system as
14011da177e4SLinus Torvalds * only user tasks fiddle here
14021da177e4SLinus Torvalds */
140354aafbd4Sandrew hendry lock_sock(sk);
14041da177e4SLinus Torvalds if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
14051da177e4SLinus Torvalds amount = skb->len;
140654aafbd4Sandrew hendry release_sock(sk);
14071da177e4SLinus Torvalds rc = put_user(amount, (unsigned int __user *)argp);
14081da177e4SLinus Torvalds break;
14091da177e4SLinus Torvalds }
14101da177e4SLinus Torvalds
14111da177e4SLinus Torvalds case SIOCGIFADDR:
14121da177e4SLinus Torvalds case SIOCSIFADDR:
14131da177e4SLinus Torvalds case SIOCGIFDSTADDR:
14141da177e4SLinus Torvalds case SIOCSIFDSTADDR:
14151da177e4SLinus Torvalds case SIOCGIFBRDADDR:
14161da177e4SLinus Torvalds case SIOCSIFBRDADDR:
14171da177e4SLinus Torvalds case SIOCGIFNETMASK:
14181da177e4SLinus Torvalds case SIOCSIFNETMASK:
14191da177e4SLinus Torvalds case SIOCGIFMETRIC:
14201da177e4SLinus Torvalds case SIOCSIFMETRIC:
14211da177e4SLinus Torvalds rc = -EINVAL;
14221da177e4SLinus Torvalds break;
14231da177e4SLinus Torvalds case SIOCADDRT:
14241da177e4SLinus Torvalds case SIOCDELRT:
14251da177e4SLinus Torvalds rc = -EPERM;
14261da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN))
14271da177e4SLinus Torvalds break;
14281da177e4SLinus Torvalds rc = x25_route_ioctl(cmd, argp);
14291da177e4SLinus Torvalds break;
14301da177e4SLinus Torvalds case SIOCX25GSUBSCRIP:
14311da177e4SLinus Torvalds rc = x25_subscr_ioctl(cmd, argp);
14321da177e4SLinus Torvalds break;
14331da177e4SLinus Torvalds case SIOCX25SSUBSCRIP:
14341da177e4SLinus Torvalds rc = -EPERM;
14351da177e4SLinus Torvalds if (!capable(CAP_NET_ADMIN))
14361da177e4SLinus Torvalds break;
14371da177e4SLinus Torvalds rc = x25_subscr_ioctl(cmd, argp);
14381da177e4SLinus Torvalds break;
14391da177e4SLinus Torvalds case SIOCX25GFACILITIES: {
1440f90de660Sandrew hendry lock_sock(sk);
1441f90de660Sandrew hendry rc = copy_to_user(argp, &x25->facilities,
1442f90de660Sandrew hendry sizeof(x25->facilities))
1443f90de660Sandrew hendry ? -EFAULT : 0;
1444f90de660Sandrew hendry release_sock(sk);
14451da177e4SLinus Torvalds break;
14461da177e4SLinus Torvalds }
14471da177e4SLinus Torvalds
14481da177e4SLinus Torvalds case SIOCX25SFACILITIES: {
14491da177e4SLinus Torvalds struct x25_facilities facilities;
14501da177e4SLinus Torvalds rc = -EFAULT;
1451fddc5f3eSJoe Perches if (copy_from_user(&facilities, argp, sizeof(facilities)))
14521da177e4SLinus Torvalds break;
14531da177e4SLinus Torvalds rc = -EINVAL;
1454f90de660Sandrew hendry lock_sock(sk);
14551da177e4SLinus Torvalds if (sk->sk_state != TCP_LISTEN &&
14561da177e4SLinus Torvalds sk->sk_state != TCP_CLOSE)
1457f90de660Sandrew hendry goto out_fac_release;
14581da177e4SLinus Torvalds if (facilities.pacsize_in < X25_PS16 ||
14591da177e4SLinus Torvalds facilities.pacsize_in > X25_PS4096)
1460f90de660Sandrew hendry goto out_fac_release;
14611da177e4SLinus Torvalds if (facilities.pacsize_out < X25_PS16 ||
14621da177e4SLinus Torvalds facilities.pacsize_out > X25_PS4096)
1463f90de660Sandrew hendry goto out_fac_release;
14641da177e4SLinus Torvalds if (facilities.winsize_in < 1 ||
14651da177e4SLinus Torvalds facilities.winsize_in > 127)
1466f90de660Sandrew hendry goto out_fac_release;
1467ddd0451fSJohn Hughes if (facilities.throughput) {
1468ddd0451fSJohn Hughes int out = facilities.throughput & 0xf0;
1469ddd0451fSJohn Hughes int in = facilities.throughput & 0x0f;
1470ddd0451fSJohn Hughes if (!out)
1471ddd0451fSJohn Hughes facilities.throughput |=
1472ddd0451fSJohn Hughes X25_DEFAULT_THROUGHPUT << 4;
1473ddd0451fSJohn Hughes else if (out < 0x30 || out > 0xD0)
1474f90de660Sandrew hendry goto out_fac_release;
1475ddd0451fSJohn Hughes if (!in)
1476ddd0451fSJohn Hughes facilities.throughput |=
1477ddd0451fSJohn Hughes X25_DEFAULT_THROUGHPUT;
1478ddd0451fSJohn Hughes else if (in < 0x03 || in > 0x0D)
1479f90de660Sandrew hendry goto out_fac_release;
1480ddd0451fSJohn Hughes }
1481ebc3f64bSShaun Pereira if (facilities.reverse &&
148206a96b33Sroel kluin (facilities.reverse & 0x81) != 0x81)
1483f90de660Sandrew hendry goto out_fac_release;
14841da177e4SLinus Torvalds x25->facilities = facilities;
14851da177e4SLinus Torvalds rc = 0;
1486f90de660Sandrew hendry out_fac_release:
1487f90de660Sandrew hendry release_sock(sk);
14881da177e4SLinus Torvalds break;
14891da177e4SLinus Torvalds }
14901da177e4SLinus Torvalds
1491a64b7b93SShaun Pereira case SIOCX25GDTEFACILITIES: {
1492f90de660Sandrew hendry lock_sock(sk);
1493a64b7b93SShaun Pereira rc = copy_to_user(argp, &x25->dte_facilities,
1494a64b7b93SShaun Pereira sizeof(x25->dte_facilities));
1495f90de660Sandrew hendry release_sock(sk);
1496a64b7b93SShaun Pereira if (rc)
1497a64b7b93SShaun Pereira rc = -EFAULT;
1498a64b7b93SShaun Pereira break;
1499a64b7b93SShaun Pereira }
1500a64b7b93SShaun Pereira
1501a64b7b93SShaun Pereira case SIOCX25SDTEFACILITIES: {
1502a64b7b93SShaun Pereira struct x25_dte_facilities dtefacs;
1503a64b7b93SShaun Pereira rc = -EFAULT;
1504a64b7b93SShaun Pereira if (copy_from_user(&dtefacs, argp, sizeof(dtefacs)))
1505a64b7b93SShaun Pereira break;
1506a64b7b93SShaun Pereira rc = -EINVAL;
1507f90de660Sandrew hendry lock_sock(sk);
1508a64b7b93SShaun Pereira if (sk->sk_state != TCP_LISTEN &&
1509a64b7b93SShaun Pereira sk->sk_state != TCP_CLOSE)
1510f90de660Sandrew hendry goto out_dtefac_release;
1511a64b7b93SShaun Pereira if (dtefacs.calling_len > X25_MAX_AE_LEN)
1512f90de660Sandrew hendry goto out_dtefac_release;
1513a64b7b93SShaun Pereira if (dtefacs.called_len > X25_MAX_AE_LEN)
1514f90de660Sandrew hendry goto out_dtefac_release;
1515a64b7b93SShaun Pereira x25->dte_facilities = dtefacs;
1516a64b7b93SShaun Pereira rc = 0;
1517f90de660Sandrew hendry out_dtefac_release:
1518f90de660Sandrew hendry release_sock(sk);
1519a64b7b93SShaun Pereira break;
1520a64b7b93SShaun Pereira }
1521a64b7b93SShaun Pereira
15221da177e4SLinus Torvalds case SIOCX25GCALLUSERDATA: {
15235b7958dfSandrew hendry lock_sock(sk);
15245b7958dfSandrew hendry rc = copy_to_user(argp, &x25->calluserdata,
15255b7958dfSandrew hendry sizeof(x25->calluserdata))
15265b7958dfSandrew hendry ? -EFAULT : 0;
15275b7958dfSandrew hendry release_sock(sk);
15281da177e4SLinus Torvalds break;
15291da177e4SLinus Torvalds }
15301da177e4SLinus Torvalds
15311da177e4SLinus Torvalds case SIOCX25SCALLUSERDATA: {
15321da177e4SLinus Torvalds struct x25_calluserdata calluserdata;
15331da177e4SLinus Torvalds
15341da177e4SLinus Torvalds rc = -EFAULT;
1535fddc5f3eSJoe Perches if (copy_from_user(&calluserdata, argp, sizeof(calluserdata)))
15361da177e4SLinus Torvalds break;
15371da177e4SLinus Torvalds rc = -EINVAL;
15381da177e4SLinus Torvalds if (calluserdata.cudlength > X25_MAX_CUD_LEN)
15391da177e4SLinus Torvalds break;
15405b7958dfSandrew hendry lock_sock(sk);
15411da177e4SLinus Torvalds x25->calluserdata = calluserdata;
15425b7958dfSandrew hendry release_sock(sk);
15431da177e4SLinus Torvalds rc = 0;
15441da177e4SLinus Torvalds break;
15451da177e4SLinus Torvalds }
15461da177e4SLinus Torvalds
15471da177e4SLinus Torvalds case SIOCX25GCAUSEDIAG: {
154874a7e440Sandrew hendry lock_sock(sk);
1549fddc5f3eSJoe Perches rc = copy_to_user(argp, &x25->causediag, sizeof(x25->causediag))
155074a7e440Sandrew hendry ? -EFAULT : 0;
155174a7e440Sandrew hendry release_sock(sk);
15521da177e4SLinus Torvalds break;
15531da177e4SLinus Torvalds }
15541da177e4SLinus Torvalds
1555386e50ccSAndrew Hendry case SIOCX25SCAUSEDIAG: {
1556386e50ccSAndrew Hendry struct x25_causediag causediag;
1557386e50ccSAndrew Hendry rc = -EFAULT;
1558386e50ccSAndrew Hendry if (copy_from_user(&causediag, argp, sizeof(causediag)))
1559386e50ccSAndrew Hendry break;
156074a7e440Sandrew hendry lock_sock(sk);
1561386e50ccSAndrew Hendry x25->causediag = causediag;
156274a7e440Sandrew hendry release_sock(sk);
1563386e50ccSAndrew Hendry rc = 0;
1564386e50ccSAndrew Hendry break;
1565386e50ccSAndrew Hendry
1566386e50ccSAndrew Hendry }
1567386e50ccSAndrew Hendry
1568cb65d506SShaun Pereira case SIOCX25SCUDMATCHLEN: {
1569cb65d506SShaun Pereira struct x25_subaddr sub_addr;
1570cb65d506SShaun Pereira rc = -EINVAL;
15713f0a069aSandrew hendry lock_sock(sk);
1572cb65d506SShaun Pereira if(sk->sk_state != TCP_CLOSE)
15733f0a069aSandrew hendry goto out_cud_release;
1574cb65d506SShaun Pereira rc = -EFAULT;
1575cb65d506SShaun Pereira if (copy_from_user(&sub_addr, argp,
1576cb65d506SShaun Pereira sizeof(sub_addr)))
15773f0a069aSandrew hendry goto out_cud_release;
1578cb65d506SShaun Pereira rc = -EINVAL;
1579cb65d506SShaun Pereira if (sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
15803f0a069aSandrew hendry goto out_cud_release;
1581cb65d506SShaun Pereira x25->cudmatchlength = sub_addr.cudmatchlength;
1582cb65d506SShaun Pereira rc = 0;
15833f0a069aSandrew hendry out_cud_release:
15843f0a069aSandrew hendry release_sock(sk);
1585cb65d506SShaun Pereira break;
1586cb65d506SShaun Pereira }
1587cb65d506SShaun Pereira
1588ebc3f64bSShaun Pereira case SIOCX25CALLACCPTAPPRV: {
1589ebc3f64bSShaun Pereira rc = -EINVAL;
159077b22836SArnd Bergmann lock_sock(sk);
15914ccb93ceSDave Jones if (sk->sk_state == TCP_CLOSE) {
159237cda787Sandrew hendry clear_bit(X25_ACCPT_APPRV_FLAG, &x25->flags);
1593ebc3f64bSShaun Pereira rc = 0;
15944ccb93ceSDave Jones }
15954ccb93ceSDave Jones release_sock(sk);
1596ebc3f64bSShaun Pereira break;
1597ebc3f64bSShaun Pereira }
1598ebc3f64bSShaun Pereira
1599ebc3f64bSShaun Pereira case SIOCX25SENDCALLACCPT: {
1600ebc3f64bSShaun Pereira rc = -EINVAL;
160177b22836SArnd Bergmann lock_sock(sk);
1602ebc3f64bSShaun Pereira if (sk->sk_state != TCP_ESTABLISHED)
16034ccb93ceSDave Jones goto out_sendcallaccpt_release;
160437cda787Sandrew hendry /* must call accptapprv above */
160537cda787Sandrew hendry if (test_bit(X25_ACCPT_APPRV_FLAG, &x25->flags))
16064ccb93ceSDave Jones goto out_sendcallaccpt_release;
1607ebc3f64bSShaun Pereira x25_write_internal(sk, X25_CALL_ACCEPTED);
1608ebc3f64bSShaun Pereira x25->state = X25_STATE_3;
1609ebc3f64bSShaun Pereira rc = 0;
16104ccb93ceSDave Jones out_sendcallaccpt_release:
16114ccb93ceSDave Jones release_sock(sk);
1612ebc3f64bSShaun Pereira break;
1613ebc3f64bSShaun Pereira }
1614ebc3f64bSShaun Pereira
16151da177e4SLinus Torvalds default:
1616b5e5fa5eSChristoph Hellwig rc = -ENOIOCTLCMD;
16171da177e4SLinus Torvalds break;
16181da177e4SLinus Torvalds }
16191da177e4SLinus Torvalds
16201da177e4SLinus Torvalds return rc;
16211da177e4SLinus Torvalds }
16221da177e4SLinus Torvalds
1623ec1b4cf7SStephen Hemminger static const struct net_proto_family x25_family_ops = {
16241da177e4SLinus Torvalds .family = AF_X25,
16251da177e4SLinus Torvalds .create = x25_create,
16261da177e4SLinus Torvalds .owner = THIS_MODULE,
16271da177e4SLinus Torvalds };
16281da177e4SLinus Torvalds
16291b06e6baSShaun Pereira #ifdef CONFIG_COMPAT
compat_x25_subscr_ioctl(unsigned int cmd,struct compat_x25_subscrip_struct __user * x25_subscr32)16301b06e6baSShaun Pereira static int compat_x25_subscr_ioctl(unsigned int cmd,
16311b06e6baSShaun Pereira struct compat_x25_subscrip_struct __user *x25_subscr32)
16321b06e6baSShaun Pereira {
16331b06e6baSShaun Pereira struct compat_x25_subscrip_struct x25_subscr;
16341b06e6baSShaun Pereira struct x25_neigh *nb;
16351b06e6baSShaun Pereira struct net_device *dev;
16361b06e6baSShaun Pereira int rc = -EINVAL;
16371b06e6baSShaun Pereira
16381b06e6baSShaun Pereira rc = -EFAULT;
16391b06e6baSShaun Pereira if (copy_from_user(&x25_subscr, x25_subscr32, sizeof(*x25_subscr32)))
16401b06e6baSShaun Pereira goto out;
16411b06e6baSShaun Pereira
16421b06e6baSShaun Pereira rc = -EINVAL;
16431b06e6baSShaun Pereira dev = x25_dev_get(x25_subscr.device);
16441b06e6baSShaun Pereira if (dev == NULL)
16451b06e6baSShaun Pereira goto out;
16461b06e6baSShaun Pereira
16471b06e6baSShaun Pereira nb = x25_get_neigh(dev);
16481b06e6baSShaun Pereira if (nb == NULL)
16491b06e6baSShaun Pereira goto out_dev_put;
16501b06e6baSShaun Pereira
16511b06e6baSShaun Pereira dev_put(dev);
16521b06e6baSShaun Pereira
16531b06e6baSShaun Pereira if (cmd == SIOCX25GSUBSCRIP) {
16545595a1a5Sandrew hendry read_lock_bh(&x25_neigh_list_lock);
16551b06e6baSShaun Pereira x25_subscr.extended = nb->extended;
16561b06e6baSShaun Pereira x25_subscr.global_facil_mask = nb->global_facil_mask;
16575595a1a5Sandrew hendry read_unlock_bh(&x25_neigh_list_lock);
16581b06e6baSShaun Pereira rc = copy_to_user(x25_subscr32, &x25_subscr,
16591b06e6baSShaun Pereira sizeof(*x25_subscr32)) ? -EFAULT : 0;
16601b06e6baSShaun Pereira } else {
16611b06e6baSShaun Pereira rc = -EINVAL;
16621b06e6baSShaun Pereira if (x25_subscr.extended == 0 || x25_subscr.extended == 1) {
16631b06e6baSShaun Pereira rc = 0;
16645595a1a5Sandrew hendry write_lock_bh(&x25_neigh_list_lock);
16651b06e6baSShaun Pereira nb->extended = x25_subscr.extended;
16661b06e6baSShaun Pereira nb->global_facil_mask = x25_subscr.global_facil_mask;
16675595a1a5Sandrew hendry write_unlock_bh(&x25_neigh_list_lock);
16681b06e6baSShaun Pereira }
16691b06e6baSShaun Pereira }
16701b06e6baSShaun Pereira x25_neigh_put(nb);
16711b06e6baSShaun Pereira out:
16721b06e6baSShaun Pereira return rc;
16731b06e6baSShaun Pereira out_dev_put:
16741b06e6baSShaun Pereira dev_put(dev);
16751b06e6baSShaun Pereira goto out;
16761b06e6baSShaun Pereira }
16771b06e6baSShaun Pereira
compat_x25_ioctl(struct socket * sock,unsigned int cmd,unsigned long arg)16781b06e6baSShaun Pereira static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
16791b06e6baSShaun Pereira unsigned long arg)
16801b06e6baSShaun Pereira {
16811b06e6baSShaun Pereira void __user *argp = compat_ptr(arg);
16821b06e6baSShaun Pereira int rc = -ENOIOCTLCMD;
16831b06e6baSShaun Pereira
16841b06e6baSShaun Pereira switch(cmd) {
16851b06e6baSShaun Pereira case TIOCOUTQ:
16861b06e6baSShaun Pereira case TIOCINQ:
16871b06e6baSShaun Pereira rc = x25_ioctl(sock, cmd, (unsigned long)argp);
16881b06e6baSShaun Pereira break;
16891b06e6baSShaun Pereira case SIOCGIFADDR:
16901b06e6baSShaun Pereira case SIOCSIFADDR:
16911b06e6baSShaun Pereira case SIOCGIFDSTADDR:
16921b06e6baSShaun Pereira case SIOCSIFDSTADDR:
16931b06e6baSShaun Pereira case SIOCGIFBRDADDR:
16941b06e6baSShaun Pereira case SIOCSIFBRDADDR:
16951b06e6baSShaun Pereira case SIOCGIFNETMASK:
16961b06e6baSShaun Pereira case SIOCSIFNETMASK:
16971b06e6baSShaun Pereira case SIOCGIFMETRIC:
16981b06e6baSShaun Pereira case SIOCSIFMETRIC:
16991b06e6baSShaun Pereira rc = -EINVAL;
17001b06e6baSShaun Pereira break;
17011b06e6baSShaun Pereira case SIOCADDRT:
17021b06e6baSShaun Pereira case SIOCDELRT:
17031b06e6baSShaun Pereira rc = -EPERM;
17041b06e6baSShaun Pereira if (!capable(CAP_NET_ADMIN))
17051b06e6baSShaun Pereira break;
17061b06e6baSShaun Pereira rc = x25_route_ioctl(cmd, argp);
17071b06e6baSShaun Pereira break;
17081b06e6baSShaun Pereira case SIOCX25GSUBSCRIP:
17091b06e6baSShaun Pereira rc = compat_x25_subscr_ioctl(cmd, argp);
17101b06e6baSShaun Pereira break;
17111b06e6baSShaun Pereira case SIOCX25SSUBSCRIP:
17121b06e6baSShaun Pereira rc = -EPERM;
17131b06e6baSShaun Pereira if (!capable(CAP_NET_ADMIN))
17141b06e6baSShaun Pereira break;
17151b06e6baSShaun Pereira rc = compat_x25_subscr_ioctl(cmd, argp);
17161b06e6baSShaun Pereira break;
17171b06e6baSShaun Pereira case SIOCX25GFACILITIES:
17181b06e6baSShaun Pereira case SIOCX25SFACILITIES:
17199a6b9f2eSShaun Pereira case SIOCX25GDTEFACILITIES:
17209a6b9f2eSShaun Pereira case SIOCX25SDTEFACILITIES:
17211b06e6baSShaun Pereira case SIOCX25GCALLUSERDATA:
17221b06e6baSShaun Pereira case SIOCX25SCALLUSERDATA:
17231b06e6baSShaun Pereira case SIOCX25GCAUSEDIAG:
1724386e50ccSAndrew Hendry case SIOCX25SCAUSEDIAG:
17251b06e6baSShaun Pereira case SIOCX25SCUDMATCHLEN:
17261b06e6baSShaun Pereira case SIOCX25CALLACCPTAPPRV:
17271b06e6baSShaun Pereira case SIOCX25SENDCALLACCPT:
17281b06e6baSShaun Pereira rc = x25_ioctl(sock, cmd, (unsigned long)argp);
17291b06e6baSShaun Pereira break;
17301b06e6baSShaun Pereira default:
17311b06e6baSShaun Pereira rc = -ENOIOCTLCMD;
17321b06e6baSShaun Pereira break;
17331b06e6baSShaun Pereira }
17341b06e6baSShaun Pereira return rc;
17351b06e6baSShaun Pereira }
17361b06e6baSShaun Pereira #endif
17371b06e6baSShaun Pereira
173891774904SArnd Bergmann static const struct proto_ops x25_proto_ops = {
17391da177e4SLinus Torvalds .family = AF_X25,
17401da177e4SLinus Torvalds .owner = THIS_MODULE,
17411da177e4SLinus Torvalds .release = x25_release,
17421da177e4SLinus Torvalds .bind = x25_bind,
17431da177e4SLinus Torvalds .connect = x25_connect,
17441da177e4SLinus Torvalds .socketpair = sock_no_socketpair,
17451da177e4SLinus Torvalds .accept = x25_accept,
17461da177e4SLinus Torvalds .getname = x25_getname,
1747a11e1d43SLinus Torvalds .poll = datagram_poll,
17481da177e4SLinus Torvalds .ioctl = x25_ioctl,
17491b06e6baSShaun Pereira #ifdef CONFIG_COMPAT
17501b06e6baSShaun Pereira .compat_ioctl = compat_x25_ioctl,
17511b06e6baSShaun Pereira #endif
1752c7cbdbf2SArnd Bergmann .gettstamp = sock_gettstamp,
17531da177e4SLinus Torvalds .listen = x25_listen,
17541da177e4SLinus Torvalds .shutdown = sock_no_shutdown,
17551da177e4SLinus Torvalds .setsockopt = x25_setsockopt,
17561da177e4SLinus Torvalds .getsockopt = x25_getsockopt,
17571da177e4SLinus Torvalds .sendmsg = x25_sendmsg,
17581da177e4SLinus Torvalds .recvmsg = x25_recvmsg,
17591da177e4SLinus Torvalds .mmap = sock_no_mmap,
17601da177e4SLinus Torvalds };
17611da177e4SLinus Torvalds
17627546dd97SStephen Hemminger static struct packet_type x25_packet_type __read_mostly = {
176309640e63SHarvey Harrison .type = cpu_to_be16(ETH_P_X25),
17641da177e4SLinus Torvalds .func = x25_lapb_receive_frame,
17651da177e4SLinus Torvalds };
17661da177e4SLinus Torvalds
17671da177e4SLinus Torvalds static struct notifier_block x25_dev_notifier = {
17681da177e4SLinus Torvalds .notifier_call = x25_device_event,
17691da177e4SLinus Torvalds };
17701da177e4SLinus Torvalds
x25_kill_by_neigh(struct x25_neigh * nb)17711da177e4SLinus Torvalds void x25_kill_by_neigh(struct x25_neigh *nb)
17721da177e4SLinus Torvalds {
17731da177e4SLinus Torvalds struct sock *s;
17741da177e4SLinus Torvalds
17751da177e4SLinus Torvalds write_lock_bh(&x25_list_lock);
17761da177e4SLinus Torvalds
177777816079SDuoming Zhou sk_for_each(s, &x25_list) {
177877816079SDuoming Zhou if (x25_sk(s)->neighbour == nb) {
177977816079SDuoming Zhou write_unlock_bh(&x25_list_lock);
178077816079SDuoming Zhou lock_sock(s);
17811da177e4SLinus Torvalds x25_disconnect(s, ENETUNREACH, 0, 0);
178277816079SDuoming Zhou release_sock(s);
178377816079SDuoming Zhou write_lock_bh(&x25_list_lock);
178477816079SDuoming Zhou }
178577816079SDuoming Zhou }
17861da177e4SLinus Torvalds write_unlock_bh(&x25_list_lock);
178795a9dc43SAndrew Hendry
178895a9dc43SAndrew Hendry /* Remove any related forwards */
178995a9dc43SAndrew Hendry x25_clear_forward_by_dev(nb->dev);
17901da177e4SLinus Torvalds }
17911da177e4SLinus Torvalds
x25_init(void)17921da177e4SLinus Torvalds static int __init x25_init(void)
17931da177e4SLinus Torvalds {
179464df6d52Slinzhang int rc;
17951da177e4SLinus Torvalds
179664df6d52Slinzhang rc = proto_register(&x25_proto, 0);
179764df6d52Slinzhang if (rc)
17981da177e4SLinus Torvalds goto out;
17991da177e4SLinus Torvalds
18001fd975a0Sandrew hendry rc = sock_register(&x25_family_ops);
180164df6d52Slinzhang if (rc)
18021fd975a0Sandrew hendry goto out_proto;
18031da177e4SLinus Torvalds
18041da177e4SLinus Torvalds dev_add_pack(&x25_packet_type);
18051da177e4SLinus Torvalds
18061fd975a0Sandrew hendry rc = register_netdevice_notifier(&x25_dev_notifier);
180764df6d52Slinzhang if (rc)
18081fd975a0Sandrew hendry goto out_sock;
18091da177e4SLinus Torvalds
181064df6d52Slinzhang rc = x25_register_sysctl();
181164df6d52Slinzhang if (rc)
181264df6d52Slinzhang goto out_dev;
181364df6d52Slinzhang
181464df6d52Slinzhang rc = x25_proc_init();
181564df6d52Slinzhang if (rc)
181664df6d52Slinzhang goto out_sysctl;
181764df6d52Slinzhang
1818b73e9e3cSwangweidong pr_info("Linux Version 0.2\n");
18191da177e4SLinus Torvalds
18201da177e4SLinus Torvalds out:
18211da177e4SLinus Torvalds return rc;
182264df6d52Slinzhang out_sysctl:
182364df6d52Slinzhang x25_unregister_sysctl();
18241fd975a0Sandrew hendry out_dev:
18251fd975a0Sandrew hendry unregister_netdevice_notifier(&x25_dev_notifier);
18261fd975a0Sandrew hendry out_sock:
182764df6d52Slinzhang dev_remove_pack(&x25_packet_type);
18281fd975a0Sandrew hendry sock_unregister(AF_X25);
18291fd975a0Sandrew hendry out_proto:
18301fd975a0Sandrew hendry proto_unregister(&x25_proto);
18311fd975a0Sandrew hendry goto out;
18321da177e4SLinus Torvalds }
18331da177e4SLinus Torvalds module_init(x25_init);
18341da177e4SLinus Torvalds
x25_exit(void)18351da177e4SLinus Torvalds static void __exit x25_exit(void)
18361da177e4SLinus Torvalds {
18371da177e4SLinus Torvalds x25_proc_exit();
18381da177e4SLinus Torvalds x25_link_free();
18391da177e4SLinus Torvalds x25_route_free();
18401da177e4SLinus Torvalds
18411da177e4SLinus Torvalds x25_unregister_sysctl();
18421da177e4SLinus Torvalds
18431da177e4SLinus Torvalds unregister_netdevice_notifier(&x25_dev_notifier);
18441da177e4SLinus Torvalds
18451da177e4SLinus Torvalds dev_remove_pack(&x25_packet_type);
18461da177e4SLinus Torvalds
18471da177e4SLinus Torvalds sock_unregister(AF_X25);
18481da177e4SLinus Torvalds proto_unregister(&x25_proto);
18491da177e4SLinus Torvalds }
18501da177e4SLinus Torvalds module_exit(x25_exit);
18511da177e4SLinus Torvalds
18521da177e4SLinus Torvalds MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
18531da177e4SLinus Torvalds MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
18541da177e4SLinus Torvalds MODULE_LICENSE("GPL");
18551da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_X25);
1856