xref: /openbmc/linux/net/x25/af_x25.c (revision f8fc57e8d7c5d95f4180b127d3b167de403557c0)
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 
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 
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 
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  */
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  *	Kill all bound sockets on a dropped device.
2041da177e4SLinus Torvalds  */
2051da177e4SLinus Torvalds static void x25_kill_by_device(struct net_device *dev)
2061da177e4SLinus Torvalds {
2071da177e4SLinus Torvalds 	struct sock *s;
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 	write_lock_bh(&x25_list_lock);
2101da177e4SLinus Torvalds 
211b67bfe0dSSasha Levin 	sk_for_each(s, &x25_list)
2121da177e4SLinus Torvalds 		if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev)
2131da177e4SLinus Torvalds 			x25_disconnect(s, ENETUNREACH, 0, 0);
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 	write_unlock_bh(&x25_list_lock);
2161da177e4SLinus Torvalds }
2171da177e4SLinus Torvalds 
2181da177e4SLinus Torvalds /*
2191da177e4SLinus Torvalds  *	Handle device status changes.
2201da177e4SLinus Torvalds  */
2211da177e4SLinus Torvalds static int x25_device_event(struct notifier_block *this, unsigned long event,
2221da177e4SLinus Torvalds 			    void *ptr)
2231da177e4SLinus Torvalds {
224351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2251da177e4SLinus Torvalds 	struct x25_neigh *nb;
2261da177e4SLinus Torvalds 
227721499e8SYOSHIFUJI Hideaki 	if (!net_eq(dev_net(dev), &init_net))
228e9dc8653SEric W. Biederman 		return NOTIFY_DONE;
229e9dc8653SEric W. Biederman 
2301da177e4SLinus Torvalds 	if (dev->type == ARPHRD_X25
23129c36262SIgor Maravić #if IS_ENABLED(CONFIG_LLC)
2321da177e4SLinus Torvalds 	 || dev->type == ARPHRD_ETHER
2331da177e4SLinus Torvalds #endif
2341da177e4SLinus Torvalds 	 ) {
2351da177e4SLinus Torvalds 		switch (event) {
2361da177e4SLinus Torvalds 		case NETDEV_UP:
2371da177e4SLinus Torvalds 			x25_link_device_up(dev);
2381da177e4SLinus Torvalds 			break;
2391da177e4SLinus Torvalds 		case NETDEV_GOING_DOWN:
2401da177e4SLinus Torvalds 			nb = x25_get_neigh(dev);
2411da177e4SLinus Torvalds 			if (nb) {
2421da177e4SLinus Torvalds 				x25_terminate_link(nb);
2431da177e4SLinus Torvalds 				x25_neigh_put(nb);
2441da177e4SLinus Torvalds 			}
2451da177e4SLinus Torvalds 			break;
2461da177e4SLinus Torvalds 		case NETDEV_DOWN:
2471da177e4SLinus Torvalds 			x25_kill_by_device(dev);
2481da177e4SLinus Torvalds 			x25_route_device_down(dev);
2491da177e4SLinus Torvalds 			x25_link_device_down(dev);
2501da177e4SLinus Torvalds 			break;
2511da177e4SLinus Torvalds 		}
2521da177e4SLinus Torvalds 	}
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	return NOTIFY_DONE;
2551da177e4SLinus Torvalds }
2561da177e4SLinus Torvalds 
2571da177e4SLinus Torvalds /*
2581da177e4SLinus Torvalds  *	Add a socket to the bound sockets list.
2591da177e4SLinus Torvalds  */
2601da177e4SLinus Torvalds static void x25_insert_socket(struct sock *sk)
2611da177e4SLinus Torvalds {
2621da177e4SLinus Torvalds 	write_lock_bh(&x25_list_lock);
2631da177e4SLinus Torvalds 	sk_add_node(sk, &x25_list);
2641da177e4SLinus Torvalds 	write_unlock_bh(&x25_list_lock);
2651da177e4SLinus Torvalds }
2661da177e4SLinus Torvalds 
2671da177e4SLinus Torvalds /*
2681da177e4SLinus Torvalds  *	Find a socket that wants to accept the Call Request we just
2691da177e4SLinus Torvalds  *	received. Check the full list for an address/cud match.
2701da177e4SLinus Torvalds  *	If no cuds match return the next_best thing, an address match.
2711da177e4SLinus Torvalds  *	Note: if a listening socket has cud set it must only get calls
2721da177e4SLinus Torvalds  *	with matching cud.
2731da177e4SLinus Torvalds  */
274cb65d506SShaun Pereira static struct sock *x25_find_listener(struct x25_address *addr,
275cb65d506SShaun Pereira 					struct sk_buff *skb)
2761da177e4SLinus Torvalds {
2771da177e4SLinus Torvalds 	struct sock *s;
2781da177e4SLinus Torvalds 	struct sock *next_best;
2791da177e4SLinus Torvalds 
2801da177e4SLinus Torvalds 	read_lock_bh(&x25_list_lock);
2811da177e4SLinus Torvalds 	next_best = NULL;
2821da177e4SLinus Torvalds 
283b67bfe0dSSasha Levin 	sk_for_each(s, &x25_list)
2841da177e4SLinus Torvalds 		if ((!strcmp(addr->x25_addr,
2851da177e4SLinus Torvalds 			x25_sk(s)->source_addr.x25_addr) ||
28606137619SMartin Schiller 				!strcmp(x25_sk(s)->source_addr.x25_addr,
2871da177e4SLinus Torvalds 					null_x25_address.x25_addr)) &&
2881da177e4SLinus Torvalds 					s->sk_state == TCP_LISTEN) {
2891da177e4SLinus Torvalds 			/*
2901da177e4SLinus Torvalds 			 * Found a listening socket, now check the incoming
2911da177e4SLinus Torvalds 			 * call user data vs this sockets call user data
2921da177e4SLinus Torvalds 			 */
2937f81e25bSMatthew Daley 			if (x25_sk(s)->cudmatchlength > 0 &&
2947f81e25bSMatthew Daley 				skb->len >= x25_sk(s)->cudmatchlength) {
295cb65d506SShaun Pereira 				if((memcmp(x25_sk(s)->calluserdata.cuddata,
296cb65d506SShaun Pereira 					skb->data,
297cb65d506SShaun Pereira 					x25_sk(s)->cudmatchlength)) == 0) {
2981da177e4SLinus Torvalds 					sock_hold(s);
2991da177e4SLinus Torvalds 					goto found;
3001da177e4SLinus Torvalds 				 }
301cb65d506SShaun Pereira 			} else
3021da177e4SLinus Torvalds 				next_best = s;
3031da177e4SLinus Torvalds 		}
3041da177e4SLinus Torvalds 	if (next_best) {
3051da177e4SLinus Torvalds 		s = next_best;
3061da177e4SLinus Torvalds 		sock_hold(s);
3071da177e4SLinus Torvalds 		goto found;
3081da177e4SLinus Torvalds 	}
3091da177e4SLinus Torvalds 	s = NULL;
3101da177e4SLinus Torvalds found:
3111da177e4SLinus Torvalds 	read_unlock_bh(&x25_list_lock);
3121da177e4SLinus Torvalds 	return s;
3131da177e4SLinus Torvalds }
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds /*
3161da177e4SLinus Torvalds  *	Find a connected X.25 socket given my LCI and neighbour.
3171da177e4SLinus Torvalds  */
3181da177e4SLinus Torvalds static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
3191da177e4SLinus Torvalds {
3201da177e4SLinus Torvalds 	struct sock *s;
3211da177e4SLinus Torvalds 
322b67bfe0dSSasha Levin 	sk_for_each(s, &x25_list)
3231da177e4SLinus Torvalds 		if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
3241da177e4SLinus Torvalds 			sock_hold(s);
3251da177e4SLinus Torvalds 			goto found;
3261da177e4SLinus Torvalds 		}
3271da177e4SLinus Torvalds 	s = NULL;
3281da177e4SLinus Torvalds found:
3291da177e4SLinus Torvalds 	return s;
3301da177e4SLinus Torvalds }
3311da177e4SLinus Torvalds 
3321da177e4SLinus Torvalds struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
3331da177e4SLinus Torvalds {
3341da177e4SLinus Torvalds 	struct sock *s;
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	read_lock_bh(&x25_list_lock);
3371da177e4SLinus Torvalds 	s = __x25_find_socket(lci, nb);
3381da177e4SLinus Torvalds 	read_unlock_bh(&x25_list_lock);
3391da177e4SLinus Torvalds 	return s;
3401da177e4SLinus Torvalds }
3411da177e4SLinus Torvalds 
3421da177e4SLinus Torvalds /*
3431da177e4SLinus Torvalds  *	Find a unique LCI for a given device.
3441da177e4SLinus Torvalds  */
3451da177e4SLinus Torvalds static unsigned int x25_new_lci(struct x25_neigh *nb)
3461da177e4SLinus Torvalds {
3471da177e4SLinus Torvalds 	unsigned int lci = 1;
3481da177e4SLinus Torvalds 	struct sock *sk;
3491da177e4SLinus Torvalds 
350cf657d22SEric Dumazet 	while ((sk = x25_find_socket(lci, nb)) != NULL) {
3511da177e4SLinus Torvalds 		sock_put(sk);
3521da177e4SLinus Torvalds 		if (++lci == 4096) {
3531da177e4SLinus Torvalds 			lci = 0;
3541da177e4SLinus Torvalds 			break;
3551da177e4SLinus Torvalds 		}
356cf657d22SEric Dumazet 		cond_resched();
3571da177e4SLinus Torvalds 	}
3581da177e4SLinus Torvalds 
3591da177e4SLinus Torvalds 	return lci;
3601da177e4SLinus Torvalds }
3611da177e4SLinus Torvalds 
3621da177e4SLinus Torvalds /*
3631da177e4SLinus Torvalds  *	Deferred destroy.
3641da177e4SLinus Torvalds  */
36514ebaf81SDavid S. Miller static void __x25_destroy_socket(struct sock *);
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds /*
3681da177e4SLinus Torvalds  *	handler for deferred kills.
3691da177e4SLinus Torvalds  */
37099767f27SKees Cook static void x25_destroy_timer(struct timer_list *t)
3711da177e4SLinus Torvalds {
37299767f27SKees Cook 	struct sock *sk = from_timer(sk, t, sk_timer);
37399767f27SKees Cook 
37499767f27SKees Cook 	x25_destroy_socket_from_timer(sk);
3751da177e4SLinus Torvalds }
3761da177e4SLinus Torvalds 
3771da177e4SLinus Torvalds /*
3781da177e4SLinus Torvalds  *	This is called from user mode and the timers. Thus it protects itself
3791da177e4SLinus Torvalds  *	against interrupt users but doesn't worry about being called during
3801da177e4SLinus Torvalds  *	work. Once it is removed from the queue no interrupt or bottom half
3811da177e4SLinus Torvalds  *	will touch it and we are (fairly 8-) ) safe.
3821da177e4SLinus Torvalds  *	Not static as it's used by the timer
3831da177e4SLinus Torvalds  */
38414ebaf81SDavid S. Miller static void __x25_destroy_socket(struct sock *sk)
3851da177e4SLinus Torvalds {
3861da177e4SLinus Torvalds 	struct sk_buff *skb;
3871da177e4SLinus Torvalds 
3881da177e4SLinus Torvalds 	x25_stop_heartbeat(sk);
3891da177e4SLinus Torvalds 	x25_stop_timer(sk);
3901da177e4SLinus Torvalds 
3911da177e4SLinus Torvalds 	x25_remove_socket(sk);
3921da177e4SLinus Torvalds 	x25_clear_queues(sk);		/* Flush the queues */
3931da177e4SLinus Torvalds 
3941da177e4SLinus Torvalds 	while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
3951da177e4SLinus Torvalds 		if (skb->sk != sk) {		/* A pending connection */
3961da177e4SLinus Torvalds 			/*
3971da177e4SLinus Torvalds 			 * Queue the unaccepted socket for death
3981da177e4SLinus Torvalds 			 */
3992cec6b01Sandrew hendry 			skb->sk->sk_state = TCP_LISTEN;
4001da177e4SLinus Torvalds 			sock_set_flag(skb->sk, SOCK_DEAD);
4011da177e4SLinus Torvalds 			x25_start_heartbeat(skb->sk);
4021da177e4SLinus Torvalds 			x25_sk(skb->sk)->state = X25_STATE_0;
4031da177e4SLinus Torvalds 		}
4041da177e4SLinus Torvalds 
4051da177e4SLinus Torvalds 		kfree_skb(skb);
4061da177e4SLinus Torvalds 	}
4071da177e4SLinus Torvalds 
408c564039fSEric Dumazet 	if (sk_has_allocations(sk)) {
4091da177e4SLinus Torvalds 		/* Defer: outstanding buffers */
4101da177e4SLinus Torvalds 		sk->sk_timer.expires  = jiffies + 10 * HZ;
411841b86f3SKees Cook 		sk->sk_timer.function = x25_destroy_timer;
4121da177e4SLinus Torvalds 		add_timer(&sk->sk_timer);
4131da177e4SLinus Torvalds 	} else {
4141da177e4SLinus Torvalds 		/* drop last reference so sock_put will free */
4151da177e4SLinus Torvalds 		__sock_put(sk);
4161da177e4SLinus Torvalds 	}
41714ebaf81SDavid S. Miller }
4181da177e4SLinus Torvalds 
41914ebaf81SDavid S. Miller void x25_destroy_socket_from_timer(struct sock *sk)
42014ebaf81SDavid S. Miller {
42114ebaf81SDavid S. Miller 	sock_hold(sk);
42214ebaf81SDavid S. Miller 	bh_lock_sock(sk);
42314ebaf81SDavid S. Miller 	__x25_destroy_socket(sk);
42414ebaf81SDavid S. Miller 	bh_unlock_sock(sk);
42514ebaf81SDavid S. Miller 	sock_put(sk);
42614ebaf81SDavid S. Miller }
42714ebaf81SDavid S. Miller 
4281da177e4SLinus Torvalds /*
4291da177e4SLinus Torvalds  *	Handling for system calls applied via the various interfaces to a
4301da177e4SLinus Torvalds  *	X.25 socket object.
4311da177e4SLinus Torvalds  */
4321da177e4SLinus Torvalds 
4331da177e4SLinus Torvalds static int x25_setsockopt(struct socket *sock, int level, int optname,
434b7058842SDavid S. Miller 			  char __user *optval, unsigned int optlen)
4351da177e4SLinus Torvalds {
4361da177e4SLinus Torvalds 	int opt;
4371da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4381da177e4SLinus Torvalds 	int rc = -ENOPROTOOPT;
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds 	if (level != SOL_X25 || optname != X25_QBITINCL)
4411da177e4SLinus Torvalds 		goto out;
4421da177e4SLinus Torvalds 
4431da177e4SLinus Torvalds 	rc = -EINVAL;
4441da177e4SLinus Torvalds 	if (optlen < sizeof(int))
4451da177e4SLinus Torvalds 		goto out;
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	rc = -EFAULT;
4481da177e4SLinus Torvalds 	if (get_user(opt, (int __user *)optval))
4491da177e4SLinus Torvalds 		goto out;
4501da177e4SLinus Torvalds 
451cb863ffdSandrew hendry 	if (opt)
452cb863ffdSandrew hendry 		set_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
453cb863ffdSandrew hendry 	else
454cb863ffdSandrew hendry 		clear_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
4551da177e4SLinus Torvalds 	rc = 0;
4561da177e4SLinus Torvalds out:
4571da177e4SLinus Torvalds 	return rc;
4581da177e4SLinus Torvalds }
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds static int x25_getsockopt(struct socket *sock, int level, int optname,
4611da177e4SLinus Torvalds 			  char __user *optval, int __user *optlen)
4621da177e4SLinus Torvalds {
4631da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4641da177e4SLinus Torvalds 	int val, len, rc = -ENOPROTOOPT;
4651da177e4SLinus Torvalds 
4661da177e4SLinus Torvalds 	if (level != SOL_X25 || optname != X25_QBITINCL)
4671da177e4SLinus Torvalds 		goto out;
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds 	rc = -EFAULT;
4701da177e4SLinus Torvalds 	if (get_user(len, optlen))
4711da177e4SLinus Torvalds 		goto out;
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	len = min_t(unsigned int, len, sizeof(int));
4741da177e4SLinus Torvalds 
4751da177e4SLinus Torvalds 	rc = -EINVAL;
4761da177e4SLinus Torvalds 	if (len < 0)
4771da177e4SLinus Torvalds 		goto out;
4781da177e4SLinus Torvalds 
4791da177e4SLinus Torvalds 	rc = -EFAULT;
4801da177e4SLinus Torvalds 	if (put_user(len, optlen))
4811da177e4SLinus Torvalds 		goto out;
4821da177e4SLinus Torvalds 
483cb863ffdSandrew hendry 	val = test_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
4841da177e4SLinus Torvalds 	rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
4851da177e4SLinus Torvalds out:
4861da177e4SLinus Torvalds 	return rc;
4871da177e4SLinus Torvalds }
4881da177e4SLinus Torvalds 
4891da177e4SLinus Torvalds static int x25_listen(struct socket *sock, int backlog)
4901da177e4SLinus Torvalds {
4911da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4921da177e4SLinus Torvalds 	int rc = -EOPNOTSUPP;
4931da177e4SLinus Torvalds 
49425aa4efeSandrew hendry 	lock_sock(sk);
4951da177e4SLinus Torvalds 	if (sk->sk_state != TCP_LISTEN) {
4961da177e4SLinus Torvalds 		memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
4971da177e4SLinus Torvalds 		sk->sk_max_ack_backlog = backlog;
4981da177e4SLinus Torvalds 		sk->sk_state           = TCP_LISTEN;
4991da177e4SLinus Torvalds 		rc = 0;
5001da177e4SLinus Torvalds 	}
50125aa4efeSandrew hendry 	release_sock(sk);
5021da177e4SLinus Torvalds 
5031da177e4SLinus Torvalds 	return rc;
5041da177e4SLinus Torvalds }
5051da177e4SLinus Torvalds 
5061da177e4SLinus Torvalds static struct proto x25_proto = {
5071da177e4SLinus Torvalds 	.name	  = "X25",
5081da177e4SLinus Torvalds 	.owner	  = THIS_MODULE,
5091da177e4SLinus Torvalds 	.obj_size = sizeof(struct x25_sock),
5101da177e4SLinus Torvalds };
5111da177e4SLinus Torvalds 
51211aa9c28SEric W. Biederman static struct sock *x25_alloc_socket(struct net *net, int kern)
5131da177e4SLinus Torvalds {
5141da177e4SLinus Torvalds 	struct x25_sock *x25;
51511aa9c28SEric W. Biederman 	struct sock *sk = sk_alloc(net, AF_X25, GFP_ATOMIC, &x25_proto, kern);
5161da177e4SLinus Torvalds 
5171da177e4SLinus Torvalds 	if (!sk)
5181da177e4SLinus Torvalds 		goto out;
5191da177e4SLinus Torvalds 
5201da177e4SLinus Torvalds 	sock_init_data(NULL, sk);
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	x25 = x25_sk(sk);
5231da177e4SLinus Torvalds 	skb_queue_head_init(&x25->ack_queue);
5241da177e4SLinus Torvalds 	skb_queue_head_init(&x25->fragment_queue);
5251da177e4SLinus Torvalds 	skb_queue_head_init(&x25->interrupt_in_queue);
5261da177e4SLinus Torvalds 	skb_queue_head_init(&x25->interrupt_out_queue);
5271da177e4SLinus Torvalds out:
5281da177e4SLinus Torvalds 	return sk;
5291da177e4SLinus Torvalds }
5301da177e4SLinus Torvalds 
5313f378b68SEric Paris static int x25_create(struct net *net, struct socket *sock, int protocol,
5323f378b68SEric Paris 		      int kern)
5331da177e4SLinus Torvalds {
5341da177e4SLinus Torvalds 	struct sock *sk;
5351da177e4SLinus Torvalds 	struct x25_sock *x25;
536b18e7a06Sandrew hendry 	int rc = -EAFNOSUPPORT;
5371da177e4SLinus Torvalds 
53809ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net))
5391da177e4SLinus Torvalds 		goto out;
5401da177e4SLinus Torvalds 
541b18e7a06Sandrew hendry 	rc = -ESOCKTNOSUPPORT;
542b18e7a06Sandrew hendry 	if (sock->type != SOCK_SEQPACKET)
543b18e7a06Sandrew hendry 		goto out;
544b18e7a06Sandrew hendry 
545b18e7a06Sandrew hendry 	rc = -EINVAL;
546b18e7a06Sandrew hendry 	if (protocol)
547b18e7a06Sandrew hendry 		goto out;
548b18e7a06Sandrew hendry 
549b18e7a06Sandrew hendry 	rc = -ENOBUFS;
55011aa9c28SEric W. Biederman 	if ((sk = x25_alloc_socket(net, kern)) == NULL)
5511da177e4SLinus Torvalds 		goto out;
5521da177e4SLinus Torvalds 
5531da177e4SLinus Torvalds 	x25 = x25_sk(sk);
5541da177e4SLinus Torvalds 
5551da177e4SLinus Torvalds 	sock_init_data(sock, sk);
5561da177e4SLinus Torvalds 
5571da177e4SLinus Torvalds 	x25_init_timers(sk);
5581da177e4SLinus Torvalds 
5591da177e4SLinus Torvalds 	sock->ops    = &x25_proto_ops;
5601da177e4SLinus Torvalds 	sk->sk_protocol = protocol;
5611da177e4SLinus Torvalds 	sk->sk_backlog_rcv = x25_backlog_rcv;
5621da177e4SLinus Torvalds 
5631da177e4SLinus Torvalds 	x25->t21   = sysctl_x25_call_request_timeout;
5641da177e4SLinus Torvalds 	x25->t22   = sysctl_x25_reset_request_timeout;
5651da177e4SLinus Torvalds 	x25->t23   = sysctl_x25_clear_request_timeout;
5661da177e4SLinus Torvalds 	x25->t2    = sysctl_x25_ack_holdback_timeout;
5671da177e4SLinus Torvalds 	x25->state = X25_STATE_0;
568cb65d506SShaun Pereira 	x25->cudmatchlength = 0;
56937cda787Sandrew hendry 	set_bit(X25_ACCPT_APPRV_FLAG, &x25->flags);	/* normally no cud  */
570ebc3f64bSShaun Pereira 							/* on call accept   */
5711da177e4SLinus Torvalds 
5721da177e4SLinus Torvalds 	x25->facilities.winsize_in  = X25_DEFAULT_WINDOW_SIZE;
5731da177e4SLinus Torvalds 	x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
5741da177e4SLinus Torvalds 	x25->facilities.pacsize_in  = X25_DEFAULT_PACKET_SIZE;
5751da177e4SLinus Torvalds 	x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
576ddd0451fSJohn Hughes 	x25->facilities.throughput  = 0;	/* by default don't negotiate
577ddd0451fSJohn Hughes 						   throughput */
5781da177e4SLinus Torvalds 	x25->facilities.reverse     = X25_DEFAULT_REVERSE;
579a64b7b93SShaun Pereira 	x25->dte_facilities.calling_len = 0;
580a64b7b93SShaun Pereira 	x25->dte_facilities.called_len = 0;
581a64b7b93SShaun Pereira 	memset(x25->dte_facilities.called_ae, '\0',
582a64b7b93SShaun Pereira 			sizeof(x25->dte_facilities.called_ae));
583a64b7b93SShaun Pereira 	memset(x25->dte_facilities.calling_ae, '\0',
584a64b7b93SShaun Pereira 			sizeof(x25->dte_facilities.calling_ae));
585a64b7b93SShaun Pereira 
5861da177e4SLinus Torvalds 	rc = 0;
5871da177e4SLinus Torvalds out:
5881da177e4SLinus Torvalds 	return rc;
5891da177e4SLinus Torvalds }
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds static struct sock *x25_make_new(struct sock *osk)
5921da177e4SLinus Torvalds {
5931da177e4SLinus Torvalds 	struct sock *sk = NULL;
5941da177e4SLinus Torvalds 	struct x25_sock *x25, *ox25;
5951da177e4SLinus Torvalds 
5961da177e4SLinus Torvalds 	if (osk->sk_type != SOCK_SEQPACKET)
5971da177e4SLinus Torvalds 		goto out;
5981da177e4SLinus Torvalds 
59911aa9c28SEric W. Biederman 	if ((sk = x25_alloc_socket(sock_net(osk), 0)) == NULL)
6001da177e4SLinus Torvalds 		goto out;
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	x25 = x25_sk(sk);
6031da177e4SLinus Torvalds 
6041da177e4SLinus Torvalds 	sk->sk_type        = osk->sk_type;
6051da177e4SLinus Torvalds 	sk->sk_priority    = osk->sk_priority;
6061da177e4SLinus Torvalds 	sk->sk_protocol    = osk->sk_protocol;
6071da177e4SLinus Torvalds 	sk->sk_rcvbuf      = osk->sk_rcvbuf;
6081da177e4SLinus Torvalds 	sk->sk_sndbuf      = osk->sk_sndbuf;
6091da177e4SLinus Torvalds 	sk->sk_state       = TCP_ESTABLISHED;
6101da177e4SLinus Torvalds 	sk->sk_backlog_rcv = osk->sk_backlog_rcv;
611a20a8554SShaun Pereira 	sock_copy_flags(sk, osk);
6121da177e4SLinus Torvalds 
6131da177e4SLinus Torvalds 	ox25 = x25_sk(osk);
6141da177e4SLinus Torvalds 	x25->t21        = ox25->t21;
6151da177e4SLinus Torvalds 	x25->t22        = ox25->t22;
6161da177e4SLinus Torvalds 	x25->t23        = ox25->t23;
6171da177e4SLinus Torvalds 	x25->t2         = ox25->t2;
618cb863ffdSandrew hendry 	x25->flags	= ox25->flags;
6191da177e4SLinus Torvalds 	x25->facilities = ox25->facilities;
620a64b7b93SShaun Pereira 	x25->dte_facilities = ox25->dte_facilities;
621cb65d506SShaun Pereira 	x25->cudmatchlength = ox25->cudmatchlength;
6221da177e4SLinus Torvalds 
623b7792e34Sandrew hendry 	clear_bit(X25_INTERRUPT_FLAG, &x25->flags);
6241da177e4SLinus Torvalds 	x25_init_timers(sk);
6251da177e4SLinus Torvalds out:
6261da177e4SLinus Torvalds 	return sk;
6271da177e4SLinus Torvalds }
6281da177e4SLinus Torvalds 
6291da177e4SLinus Torvalds static int x25_release(struct socket *sock)
6301da177e4SLinus Torvalds {
6311da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
6321da177e4SLinus Torvalds 	struct x25_sock *x25;
6331da177e4SLinus Torvalds 
6341da177e4SLinus Torvalds 	if (!sk)
63577b22836SArnd Bergmann 		return 0;
6361da177e4SLinus Torvalds 
6371da177e4SLinus Torvalds 	x25 = x25_sk(sk);
6381da177e4SLinus Torvalds 
63977b22836SArnd Bergmann 	sock_hold(sk);
64077b22836SArnd Bergmann 	lock_sock(sk);
6411da177e4SLinus Torvalds 	switch (x25->state) {
6421da177e4SLinus Torvalds 
6431da177e4SLinus Torvalds 		case X25_STATE_0:
6441da177e4SLinus Torvalds 		case X25_STATE_2:
6451da177e4SLinus Torvalds 			x25_disconnect(sk, 0, 0, 0);
64677b22836SArnd Bergmann 			__x25_destroy_socket(sk);
6471da177e4SLinus Torvalds 			goto out;
6481da177e4SLinus Torvalds 
6491da177e4SLinus Torvalds 		case X25_STATE_1:
6501da177e4SLinus Torvalds 		case X25_STATE_3:
6511da177e4SLinus Torvalds 		case X25_STATE_4:
6521da177e4SLinus Torvalds 			x25_clear_queues(sk);
6531da177e4SLinus Torvalds 			x25_write_internal(sk, X25_CLEAR_REQUEST);
6541da177e4SLinus Torvalds 			x25_start_t23timer(sk);
6551da177e4SLinus Torvalds 			x25->state = X25_STATE_2;
6561da177e4SLinus Torvalds 			sk->sk_state	= TCP_CLOSE;
6571da177e4SLinus Torvalds 			sk->sk_shutdown	|= SEND_SHUTDOWN;
6581da177e4SLinus Torvalds 			sk->sk_state_change(sk);
6591da177e4SLinus Torvalds 			sock_set_flag(sk, SOCK_DEAD);
6601da177e4SLinus Torvalds 			sock_set_flag(sk, SOCK_DESTROY);
6611da177e4SLinus Torvalds 			break;
662*f8fc57e8SMartin Schiller 
663*f8fc57e8SMartin Schiller 		case X25_STATE_5:
664*f8fc57e8SMartin Schiller 			x25_write_internal(sk, X25_CLEAR_REQUEST);
665*f8fc57e8SMartin Schiller 			x25_disconnect(sk, 0, 0, 0);
666*f8fc57e8SMartin Schiller 			__x25_destroy_socket(sk);
667*f8fc57e8SMartin Schiller 			goto out;
6681da177e4SLinus Torvalds 	}
6691da177e4SLinus Torvalds 
670c751e4f8SDavid S. Miller 	sock_orphan(sk);
6711da177e4SLinus Torvalds out:
67277b22836SArnd Bergmann 	release_sock(sk);
67377b22836SArnd Bergmann 	sock_put(sk);
6741da177e4SLinus Torvalds 	return 0;
6751da177e4SLinus Torvalds }
6761da177e4SLinus Torvalds 
6771da177e4SLinus Torvalds static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
6781da177e4SLinus Torvalds {
6791da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
6801da177e4SLinus Torvalds 	struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
681a9288525Sandrew hendry 	int len, i, rc = 0;
6821da177e4SLinus Torvalds 
683797a22bdSEric Dumazet 	if (addr_len != sizeof(struct sockaddr_x25) ||
68491774904SArnd Bergmann 	    addr->sx25_family != AF_X25) {
68591774904SArnd Bergmann 		rc = -EINVAL;
68691774904SArnd Bergmann 		goto out;
68791774904SArnd Bergmann 	}
6881da177e4SLinus Torvalds 
68906137619SMartin Schiller 	/* check for the null_x25_address */
69006137619SMartin Schiller 	if (strcmp(addr->sx25_addr.x25_addr, null_x25_address.x25_addr)) {
69106137619SMartin Schiller 
692a9288525Sandrew hendry 		len = strlen(addr->sx25_addr.x25_addr);
693a9288525Sandrew hendry 		for (i = 0; i < len; i++) {
694a9288525Sandrew hendry 			if (!isdigit(addr->sx25_addr.x25_addr[i])) {
695a9288525Sandrew hendry 				rc = -EINVAL;
696a9288525Sandrew hendry 				goto out;
697a9288525Sandrew hendry 			}
698a9288525Sandrew hendry 		}
69906137619SMartin Schiller 	}
700a9288525Sandrew hendry 
70190c27297Sandrew hendry 	lock_sock(sk);
702797a22bdSEric Dumazet 	if (sock_flag(sk, SOCK_ZAPPED)) {
7031da177e4SLinus Torvalds 		x25_sk(sk)->source_addr = addr->sx25_addr;
7041da177e4SLinus Torvalds 		x25_insert_socket(sk);
7051da177e4SLinus Torvalds 		sock_reset_flag(sk, SOCK_ZAPPED);
706797a22bdSEric Dumazet 	} else {
707797a22bdSEric Dumazet 		rc = -EINVAL;
708797a22bdSEric Dumazet 	}
70990c27297Sandrew hendry 	release_sock(sk);
7101da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
71191774904SArnd Bergmann out:
71291774904SArnd Bergmann 	return rc;
7131da177e4SLinus Torvalds }
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds static int x25_wait_for_connection_establishment(struct sock *sk)
7161da177e4SLinus Torvalds {
7171da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
7181da177e4SLinus Torvalds 	int rc;
7191da177e4SLinus Torvalds 
720aa395145SEric Dumazet 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
7211da177e4SLinus Torvalds 	for (;;) {
7221da177e4SLinus Torvalds 		__set_current_state(TASK_INTERRUPTIBLE);
7231da177e4SLinus Torvalds 		rc = -ERESTARTSYS;
7241da177e4SLinus Torvalds 		if (signal_pending(current))
7251da177e4SLinus Torvalds 			break;
7261da177e4SLinus Torvalds 		rc = sock_error(sk);
7271da177e4SLinus Torvalds 		if (rc) {
7281da177e4SLinus Torvalds 			sk->sk_socket->state = SS_UNCONNECTED;
7291da177e4SLinus Torvalds 			break;
7301da177e4SLinus Torvalds 		}
7311da177e4SLinus Torvalds 		rc = 0;
7321da177e4SLinus Torvalds 		if (sk->sk_state != TCP_ESTABLISHED) {
7331da177e4SLinus Torvalds 			release_sock(sk);
7341da177e4SLinus Torvalds 			schedule();
7351da177e4SLinus Torvalds 			lock_sock(sk);
7361da177e4SLinus Torvalds 		} else
7371da177e4SLinus Torvalds 			break;
7381da177e4SLinus Torvalds 	}
7391da177e4SLinus Torvalds 	__set_current_state(TASK_RUNNING);
740aa395145SEric Dumazet 	remove_wait_queue(sk_sleep(sk), &wait);
7411da177e4SLinus Torvalds 	return rc;
7421da177e4SLinus Torvalds }
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
7451da177e4SLinus Torvalds 		       int addr_len, int flags)
7461da177e4SLinus Torvalds {
7471da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
7481da177e4SLinus Torvalds 	struct x25_sock *x25 = x25_sk(sk);
7491da177e4SLinus Torvalds 	struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
7501da177e4SLinus Torvalds 	struct x25_route *rt;
7511da177e4SLinus Torvalds 	int rc = 0;
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	lock_sock(sk);
7541da177e4SLinus Torvalds 	if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
7551da177e4SLinus Torvalds 		sock->state = SS_CONNECTED;
7561da177e4SLinus Torvalds 		goto out; /* Connect completed during a ERESTARTSYS event */
7571da177e4SLinus Torvalds 	}
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	rc = -ECONNREFUSED;
7601da177e4SLinus Torvalds 	if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
7611da177e4SLinus Torvalds 		sock->state = SS_UNCONNECTED;
7621da177e4SLinus Torvalds 		goto out;
7631da177e4SLinus Torvalds 	}
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	rc = -EISCONN;	/* No reconnect on a seqpacket socket */
7661da177e4SLinus Torvalds 	if (sk->sk_state == TCP_ESTABLISHED)
7671da177e4SLinus Torvalds 		goto out;
7681da177e4SLinus Torvalds 
7691da177e4SLinus Torvalds 	sk->sk_state   = TCP_CLOSE;
7701da177e4SLinus Torvalds 	sock->state = SS_UNCONNECTED;
7711da177e4SLinus Torvalds 
7721da177e4SLinus Torvalds 	rc = -EINVAL;
7731da177e4SLinus Torvalds 	if (addr_len != sizeof(struct sockaddr_x25) ||
7741da177e4SLinus Torvalds 	    addr->sx25_family != AF_X25)
7751da177e4SLinus Torvalds 		goto out;
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 	rc = -ENETUNREACH;
7781da177e4SLinus Torvalds 	rt = x25_get_route(&addr->sx25_addr);
7791da177e4SLinus Torvalds 	if (!rt)
7801da177e4SLinus Torvalds 		goto out;
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	x25->neighbour = x25_get_neigh(rt->dev);
7831da177e4SLinus Torvalds 	if (!x25->neighbour)
7841da177e4SLinus Torvalds 		goto out_put_route;
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	x25_limit_facilities(&x25->facilities, x25->neighbour);
7871da177e4SLinus Torvalds 
7881da177e4SLinus Torvalds 	x25->lci = x25_new_lci(x25->neighbour);
7891da177e4SLinus Torvalds 	if (!x25->lci)
7901da177e4SLinus Torvalds 		goto out_put_neigh;
7911da177e4SLinus Torvalds 
7921da177e4SLinus Torvalds 	rc = -EINVAL;
7931da177e4SLinus Torvalds 	if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
7941da177e4SLinus Torvalds 		goto out_put_neigh;
7951da177e4SLinus Torvalds 
7961da177e4SLinus Torvalds 	if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
7971da177e4SLinus Torvalds 		memset(&x25->source_addr, '\0', X25_ADDR_LEN);
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	x25->dest_addr = addr->sx25_addr;
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	/* Move to connecting socket, start sending Connect Requests */
8021da177e4SLinus Torvalds 	sock->state   = SS_CONNECTING;
8031da177e4SLinus Torvalds 	sk->sk_state  = TCP_SYN_SENT;
8041da177e4SLinus Torvalds 
8051da177e4SLinus Torvalds 	x25->state = X25_STATE_1;
8061da177e4SLinus Torvalds 
8071da177e4SLinus Torvalds 	x25_write_internal(sk, X25_CALL_REQUEST);
8081da177e4SLinus Torvalds 
8091da177e4SLinus Torvalds 	x25_start_heartbeat(sk);
8101da177e4SLinus Torvalds 	x25_start_t21timer(sk);
8111da177e4SLinus Torvalds 
8121da177e4SLinus Torvalds 	/* Now the loop */
8131da177e4SLinus Torvalds 	rc = -EINPROGRESS;
8141da177e4SLinus Torvalds 	if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
8151da177e4SLinus Torvalds 		goto out_put_neigh;
8161da177e4SLinus Torvalds 
8171da177e4SLinus Torvalds 	rc = x25_wait_for_connection_establishment(sk);
8181da177e4SLinus Torvalds 	if (rc)
8191da177e4SLinus Torvalds 		goto out_put_neigh;
8201da177e4SLinus Torvalds 
8211da177e4SLinus Torvalds 	sock->state = SS_CONNECTED;
8221da177e4SLinus Torvalds 	rc = 0;
8231da177e4SLinus Torvalds out_put_neigh:
82495d6ebd5SEric Dumazet 	if (rc) {
82595d6ebd5SEric Dumazet 		read_lock_bh(&x25_list_lock);
8261da177e4SLinus Torvalds 		x25_neigh_put(x25->neighbour);
82795d6ebd5SEric Dumazet 		x25->neighbour = NULL;
82895d6ebd5SEric Dumazet 		read_unlock_bh(&x25_list_lock);
829ee74d0bdSEric Dumazet 		x25->state = X25_STATE_0;
83095d6ebd5SEric Dumazet 	}
8311da177e4SLinus Torvalds out_put_route:
8321da177e4SLinus Torvalds 	x25_route_put(rt);
8331da177e4SLinus Torvalds out:
8341da177e4SLinus Torvalds 	release_sock(sk);
8351da177e4SLinus Torvalds 	return rc;
8361da177e4SLinus Torvalds }
8371da177e4SLinus Torvalds 
838bac37ec8SShaun Pereira static int x25_wait_for_data(struct sock *sk, long timeout)
8391da177e4SLinus Torvalds {
8401da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
8411da177e4SLinus Torvalds 	int rc = 0;
8421da177e4SLinus Torvalds 
843aa395145SEric Dumazet 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
8441da177e4SLinus Torvalds 	for (;;) {
8451da177e4SLinus Torvalds 		__set_current_state(TASK_INTERRUPTIBLE);
8461da177e4SLinus Torvalds 		if (sk->sk_shutdown & RCV_SHUTDOWN)
8471da177e4SLinus Torvalds 			break;
8481da177e4SLinus Torvalds 		rc = -ERESTARTSYS;
8491da177e4SLinus Torvalds 		if (signal_pending(current))
8501da177e4SLinus Torvalds 			break;
8511da177e4SLinus Torvalds 		rc = -EAGAIN;
8521da177e4SLinus Torvalds 		if (!timeout)
8531da177e4SLinus Torvalds 			break;
8541da177e4SLinus Torvalds 		rc = 0;
8551da177e4SLinus Torvalds 		if (skb_queue_empty(&sk->sk_receive_queue)) {
8561da177e4SLinus Torvalds 			release_sock(sk);
8571da177e4SLinus Torvalds 			timeout = schedule_timeout(timeout);
8581da177e4SLinus Torvalds 			lock_sock(sk);
8591da177e4SLinus Torvalds 		} else
8601da177e4SLinus Torvalds 			break;
8611da177e4SLinus Torvalds 	}
8621da177e4SLinus Torvalds 	__set_current_state(TASK_RUNNING);
863aa395145SEric Dumazet 	remove_wait_queue(sk_sleep(sk), &wait);
8641da177e4SLinus Torvalds 	return rc;
8651da177e4SLinus Torvalds }
8661da177e4SLinus Torvalds 
867cdfbabfbSDavid Howells static int x25_accept(struct socket *sock, struct socket *newsock, int flags,
868cdfbabfbSDavid Howells 		      bool kern)
8691da177e4SLinus Torvalds {
8701da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
8711da177e4SLinus Torvalds 	struct sock *newsk;
8721da177e4SLinus Torvalds 	struct sk_buff *skb;
8731da177e4SLinus Torvalds 	int rc = -EINVAL;
8741da177e4SLinus Torvalds 
875141646ceSAndrew Hendry 	if (!sk)
8761da177e4SLinus Torvalds 		goto out;
8771da177e4SLinus Torvalds 
8781da177e4SLinus Torvalds 	rc = -EOPNOTSUPP;
8791da177e4SLinus Torvalds 	if (sk->sk_type != SOCK_SEQPACKET)
8801da177e4SLinus Torvalds 		goto out;
8811da177e4SLinus Torvalds 
8821da177e4SLinus Torvalds 	lock_sock(sk);
883141646ceSAndrew Hendry 	rc = -EINVAL;
884141646ceSAndrew Hendry 	if (sk->sk_state != TCP_LISTEN)
885141646ceSAndrew Hendry 		goto out2;
886141646ceSAndrew Hendry 
8871da177e4SLinus Torvalds 	rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
8881da177e4SLinus Torvalds 	if (rc)
8891da177e4SLinus Torvalds 		goto out2;
8901da177e4SLinus Torvalds 	skb = skb_dequeue(&sk->sk_receive_queue);
8911da177e4SLinus Torvalds 	rc = -EINVAL;
8921da177e4SLinus Torvalds 	if (!skb->sk)
8931da177e4SLinus Torvalds 		goto out2;
8941da177e4SLinus Torvalds 	newsk		 = skb->sk;
895b61d38e0SDavid S. Miller 	sock_graft(newsk, newsock);
8961da177e4SLinus Torvalds 
8971da177e4SLinus Torvalds 	/* Now attach up the new socket */
8981da177e4SLinus Torvalds 	skb->sk = NULL;
8991da177e4SLinus Torvalds 	kfree_skb(skb);
9007976a11bSEric Dumazet 	sk_acceptq_removed(sk);
9011da177e4SLinus Torvalds 	newsock->state = SS_CONNECTED;
9021da177e4SLinus Torvalds 	rc = 0;
9031da177e4SLinus Torvalds out2:
9041da177e4SLinus Torvalds 	release_sock(sk);
9051da177e4SLinus Torvalds out:
9061da177e4SLinus Torvalds 	return rc;
9071da177e4SLinus Torvalds }
9081da177e4SLinus Torvalds 
9091da177e4SLinus Torvalds static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
9109b2c45d4SDenys Vlasenko 		       int peer)
9111da177e4SLinus Torvalds {
9121da177e4SLinus Torvalds 	struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
9131da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
9141da177e4SLinus Torvalds 	struct x25_sock *x25 = x25_sk(sk);
91591774904SArnd Bergmann 	int rc = 0;
9161da177e4SLinus Torvalds 
9171da177e4SLinus Torvalds 	if (peer) {
91891774904SArnd Bergmann 		if (sk->sk_state != TCP_ESTABLISHED) {
91991774904SArnd Bergmann 			rc = -ENOTCONN;
92091774904SArnd Bergmann 			goto out;
92191774904SArnd Bergmann 		}
9221da177e4SLinus Torvalds 		sx25->sx25_addr = x25->dest_addr;
9231da177e4SLinus Torvalds 	} else
9241da177e4SLinus Torvalds 		sx25->sx25_addr = x25->source_addr;
9251da177e4SLinus Torvalds 
9261da177e4SLinus Torvalds 	sx25->sx25_family = AF_X25;
9279b2c45d4SDenys Vlasenko 	rc = sizeof(*sx25);
9281da177e4SLinus Torvalds 
92991774904SArnd Bergmann out:
93091774904SArnd Bergmann 	return rc;
93191774904SArnd Bergmann }
93291774904SArnd Bergmann 
9331da177e4SLinus Torvalds int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
9341da177e4SLinus Torvalds 			unsigned int lci)
9351da177e4SLinus Torvalds {
9361da177e4SLinus Torvalds 	struct sock *sk;
9371da177e4SLinus Torvalds 	struct sock *make;
9381da177e4SLinus Torvalds 	struct x25_sock *makex25;
9391da177e4SLinus Torvalds 	struct x25_address source_addr, dest_addr;
9401da177e4SLinus Torvalds 	struct x25_facilities facilities;
941a64b7b93SShaun Pereira 	struct x25_dte_facilities dte_facilities;
94295a9dc43SAndrew Hendry 	int len, addr_len, rc;
9431da177e4SLinus Torvalds 
9441da177e4SLinus Torvalds 	/*
9451da177e4SLinus Torvalds 	 *	Remove the LCI and frame type.
9461da177e4SLinus Torvalds 	 */
9471da177e4SLinus Torvalds 	skb_pull(skb, X25_STD_MIN_LEN);
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	/*
9501da177e4SLinus Torvalds 	 *	Extract the X.25 addresses and convert them to ASCII strings,
9511da177e4SLinus Torvalds 	 *	and remove them.
952f5eb917bSJohn Hughes 	 *
953f5eb917bSJohn Hughes 	 *	Address block is mandatory in call request packets
9541da177e4SLinus Torvalds 	 */
955f5eb917bSJohn Hughes 	addr_len = x25_parse_address_block(skb, &source_addr, &dest_addr);
956f5eb917bSJohn Hughes 	if (addr_len <= 0)
957f5eb917bSJohn Hughes 		goto out_clear_request;
95895a9dc43SAndrew Hendry 	skb_pull(skb, addr_len);
9591da177e4SLinus Torvalds 
9601da177e4SLinus Torvalds 	/*
9611da177e4SLinus Torvalds 	 *	Get the length of the facilities, skip past them for the moment
9621da177e4SLinus Torvalds 	 *	get the call user data because this is needed to determine
9631da177e4SLinus Torvalds 	 *	the correct listener
964f5eb917bSJohn Hughes 	 *
965f5eb917bSJohn Hughes 	 *	Facilities length is mandatory in call request packets
9661da177e4SLinus Torvalds 	 */
967cb101ed2SMatthew Daley 	if (!pskb_may_pull(skb, 1))
968f5eb917bSJohn Hughes 		goto out_clear_request;
9691da177e4SLinus Torvalds 	len = skb->data[0] + 1;
970cb101ed2SMatthew Daley 	if (!pskb_may_pull(skb, len))
971f5eb917bSJohn Hughes 		goto out_clear_request;
9721da177e4SLinus Torvalds 	skb_pull(skb,len);
9731da177e4SLinus Torvalds 
9741da177e4SLinus Torvalds 	/*
975c7fd0d48SMatthew Daley 	 *	Ensure that the amount of call user data is valid.
976c7fd0d48SMatthew Daley 	 */
977c7fd0d48SMatthew Daley 	if (skb->len > X25_MAX_CUD_LEN)
978c7fd0d48SMatthew Daley 		goto out_clear_request;
979c7fd0d48SMatthew Daley 
980c7fd0d48SMatthew Daley 	/*
981cb101ed2SMatthew Daley 	 *	Get all the call user data so it can be used in
982cb101ed2SMatthew Daley 	 *	x25_find_listener and skb_copy_from_linear_data up ahead.
983cb101ed2SMatthew Daley 	 */
984cb101ed2SMatthew Daley 	if (!pskb_may_pull(skb, skb->len))
985cb101ed2SMatthew Daley 		goto out_clear_request;
986cb101ed2SMatthew Daley 
987cb101ed2SMatthew Daley 	/*
9881da177e4SLinus Torvalds 	 *	Find a listener for the particular address/cud pair.
9891da177e4SLinus Torvalds 	 */
990cb65d506SShaun Pereira 	sk = x25_find_listener(&source_addr,skb);
991cb65d506SShaun Pereira 	skb_push(skb,len);
9921da177e4SLinus Torvalds 
99395a9dc43SAndrew Hendry 	if (sk != NULL && sk_acceptq_is_full(sk)) {
99495a9dc43SAndrew Hendry 		goto out_sock_put;
99595a9dc43SAndrew Hendry 	}
99695a9dc43SAndrew Hendry 
9971da177e4SLinus Torvalds 	/*
99895a9dc43SAndrew Hendry 	 *	We dont have any listeners for this incoming call.
99995a9dc43SAndrew Hendry 	 *	Try forwarding it.
10001da177e4SLinus Torvalds 	 */
100195a9dc43SAndrew Hendry 	if (sk == NULL) {
100295a9dc43SAndrew Hendry 		skb_push(skb, addr_len + X25_STD_MIN_LEN);
100339e21c0dSAndrew Hendry 		if (sysctl_x25_forward &&
100439e21c0dSAndrew Hendry 				x25_forward_call(&dest_addr, nb, skb, lci) > 0)
100595a9dc43SAndrew Hendry 		{
100695a9dc43SAndrew Hendry 			/* Call was forwarded, dont process it any more */
100795a9dc43SAndrew Hendry 			kfree_skb(skb);
100895a9dc43SAndrew Hendry 			rc = 1;
100995a9dc43SAndrew Hendry 			goto out;
101095a9dc43SAndrew Hendry 		} else {
101195a9dc43SAndrew Hendry 			/* No listeners, can't forward, clear the call */
10121da177e4SLinus Torvalds 			goto out_clear_request;
101395a9dc43SAndrew Hendry 		}
101495a9dc43SAndrew Hendry 	}
10151da177e4SLinus Torvalds 
10161da177e4SLinus Torvalds 	/*
10171da177e4SLinus Torvalds 	 *	Try to reach a compromise on the requested facilities.
10181da177e4SLinus Torvalds 	 */
1019a64b7b93SShaun Pereira 	len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities);
1020a64b7b93SShaun Pereira 	if (len == -1)
10211da177e4SLinus Torvalds 		goto out_sock_put;
10221da177e4SLinus Torvalds 
10231da177e4SLinus Torvalds 	/*
10241da177e4SLinus Torvalds 	 * current neighbour/link might impose additional limits
10251da177e4SLinus Torvalds 	 * on certain facilties
10261da177e4SLinus Torvalds 	 */
10271da177e4SLinus Torvalds 
10281da177e4SLinus Torvalds 	x25_limit_facilities(&facilities, nb);
10291da177e4SLinus Torvalds 
10301da177e4SLinus Torvalds 	/*
10311da177e4SLinus Torvalds 	 *	Try to create a new socket.
10321da177e4SLinus Torvalds 	 */
10331da177e4SLinus Torvalds 	make = x25_make_new(sk);
10341da177e4SLinus Torvalds 	if (!make)
10351da177e4SLinus Torvalds 		goto out_sock_put;
10361da177e4SLinus Torvalds 
10371da177e4SLinus Torvalds 	/*
10381da177e4SLinus Torvalds 	 *	Remove the facilities
10391da177e4SLinus Torvalds 	 */
10401da177e4SLinus Torvalds 	skb_pull(skb, len);
10411da177e4SLinus Torvalds 
10421da177e4SLinus Torvalds 	skb->sk     = make;
10431da177e4SLinus Torvalds 	make->sk_state = TCP_ESTABLISHED;
10441da177e4SLinus Torvalds 
10451da177e4SLinus Torvalds 	makex25 = x25_sk(make);
10461da177e4SLinus Torvalds 	makex25->lci           = lci;
10471da177e4SLinus Torvalds 	makex25->dest_addr     = dest_addr;
10481da177e4SLinus Torvalds 	makex25->source_addr   = source_addr;
10491da177e4SLinus Torvalds 	makex25->neighbour     = nb;
10501da177e4SLinus Torvalds 	makex25->facilities    = facilities;
1051a64b7b93SShaun Pereira 	makex25->dte_facilities= dte_facilities;
10521da177e4SLinus Torvalds 	makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
1053cb65d506SShaun Pereira 	/* ensure no reverse facil on accept */
1054cb65d506SShaun Pereira 	makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
1055a64b7b93SShaun Pereira 	/* ensure no calling address extension on accept */
1056a64b7b93SShaun Pereira 	makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE;
1057cb65d506SShaun Pereira 	makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
10581da177e4SLinus Torvalds 
105937cda787Sandrew hendry 	/* Normally all calls are accepted immediately */
106037cda787Sandrew hendry 	if (test_bit(X25_ACCPT_APPRV_FLAG, &makex25->flags)) {
10611da177e4SLinus Torvalds 		x25_write_internal(make, X25_CALL_ACCEPTED);
10621da177e4SLinus Torvalds 		makex25->state = X25_STATE_3;
1063*f8fc57e8SMartin Schiller 	} else {
1064*f8fc57e8SMartin Schiller 		makex25->state = X25_STATE_5;
1065ebc3f64bSShaun Pereira 	}
10661da177e4SLinus Torvalds 
1067cb65d506SShaun Pereira 	/*
1068cb65d506SShaun Pereira 	 *	Incoming Call User Data.
1069cb65d506SShaun Pereira 	 */
1070d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, makex25->calluserdata.cuddata, skb->len);
1071cb65d506SShaun Pereira 	makex25->calluserdata.cudlength = skb->len;
1072cb65d506SShaun Pereira 
10737976a11bSEric Dumazet 	sk_acceptq_added(sk);
10741da177e4SLinus Torvalds 
10751da177e4SLinus Torvalds 	x25_insert_socket(make);
10761da177e4SLinus Torvalds 
10771da177e4SLinus Torvalds 	skb_queue_head(&sk->sk_receive_queue, skb);
10781da177e4SLinus Torvalds 
10791da177e4SLinus Torvalds 	x25_start_heartbeat(make);
10801da177e4SLinus Torvalds 
10811da177e4SLinus Torvalds 	if (!sock_flag(sk, SOCK_DEAD))
1082676d2369SDavid S. Miller 		sk->sk_data_ready(sk);
10831da177e4SLinus Torvalds 	rc = 1;
10841da177e4SLinus Torvalds 	sock_put(sk);
10851da177e4SLinus Torvalds out:
10861da177e4SLinus Torvalds 	return rc;
10871da177e4SLinus Torvalds out_sock_put:
10881da177e4SLinus Torvalds 	sock_put(sk);
10891da177e4SLinus Torvalds out_clear_request:
10901da177e4SLinus Torvalds 	rc = 0;
10911da177e4SLinus Torvalds 	x25_transmit_clear_request(nb, lci, 0x01);
10921da177e4SLinus Torvalds 	goto out;
10931da177e4SLinus Torvalds }
10941da177e4SLinus Torvalds 
10951b784140SYing Xue static int x25_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
10961da177e4SLinus Torvalds {
10971da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
10981da177e4SLinus Torvalds 	struct x25_sock *x25 = x25_sk(sk);
1099342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_x25 *, usx25, msg->msg_name);
11001da177e4SLinus Torvalds 	struct sockaddr_x25 sx25;
11011da177e4SLinus Torvalds 	struct sk_buff *skb;
11021da177e4SLinus Torvalds 	unsigned char *asmptr;
11031da177e4SLinus Torvalds 	int noblock = msg->msg_flags & MSG_DONTWAIT;
11041da177e4SLinus Torvalds 	size_t size;
11051da177e4SLinus Torvalds 	int qbit = 0, rc = -EINVAL;
11061da177e4SLinus Torvalds 
110777b22836SArnd Bergmann 	lock_sock(sk);
11081da177e4SLinus Torvalds 	if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
11091da177e4SLinus Torvalds 		goto out;
11101da177e4SLinus Torvalds 
11111da177e4SLinus Torvalds 	/* we currently don't support segmented records at the user interface */
11121da177e4SLinus Torvalds 	if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
11131da177e4SLinus Torvalds 		goto out;
11141da177e4SLinus Torvalds 
11151da177e4SLinus Torvalds 	rc = -EADDRNOTAVAIL;
11161da177e4SLinus Torvalds 	if (sock_flag(sk, SOCK_ZAPPED))
11171da177e4SLinus Torvalds 		goto out;
11181da177e4SLinus Torvalds 
11191da177e4SLinus Torvalds 	rc = -EPIPE;
11201da177e4SLinus Torvalds 	if (sk->sk_shutdown & SEND_SHUTDOWN) {
11211da177e4SLinus Torvalds 		send_sig(SIGPIPE, current, 0);
11221da177e4SLinus Torvalds 		goto out;
11231da177e4SLinus Torvalds 	}
11241da177e4SLinus Torvalds 
11251da177e4SLinus Torvalds 	rc = -ENETUNREACH;
11261da177e4SLinus Torvalds 	if (!x25->neighbour)
11271da177e4SLinus Torvalds 		goto out;
11281da177e4SLinus Torvalds 
11291da177e4SLinus Torvalds 	if (usx25) {
11301da177e4SLinus Torvalds 		rc = -EINVAL;
11311da177e4SLinus Torvalds 		if (msg->msg_namelen < sizeof(sx25))
11321da177e4SLinus Torvalds 			goto out;
11331da177e4SLinus Torvalds 		memcpy(&sx25, usx25, sizeof(sx25));
11341da177e4SLinus Torvalds 		rc = -EISCONN;
11351da177e4SLinus Torvalds 		if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
11361da177e4SLinus Torvalds 			goto out;
11371da177e4SLinus Torvalds 		rc = -EINVAL;
11381da177e4SLinus Torvalds 		if (sx25.sx25_family != AF_X25)
11391da177e4SLinus Torvalds 			goto out;
11401da177e4SLinus Torvalds 	} else {
11411da177e4SLinus Torvalds 		/*
11421da177e4SLinus Torvalds 		 *	FIXME 1003.1g - if the socket is like this because
11431da177e4SLinus Torvalds 		 *	it has become closed (not started closed) we ought
11441da177e4SLinus Torvalds 		 *	to SIGPIPE, EPIPE;
11451da177e4SLinus Torvalds 		 */
11461da177e4SLinus Torvalds 		rc = -ENOTCONN;
11471da177e4SLinus Torvalds 		if (sk->sk_state != TCP_ESTABLISHED)
11481da177e4SLinus Torvalds 			goto out;
11491da177e4SLinus Torvalds 
11501da177e4SLinus Torvalds 		sx25.sx25_family = AF_X25;
11511da177e4SLinus Torvalds 		sx25.sx25_addr   = x25->dest_addr;
11521da177e4SLinus Torvalds 	}
11531da177e4SLinus Torvalds 
115483e0bbcbSAlan Cox 	/* Sanity check the packet size */
115583e0bbcbSAlan Cox 	if (len > 65535) {
115683e0bbcbSAlan Cox 		rc = -EMSGSIZE;
115783e0bbcbSAlan Cox 		goto out;
115883e0bbcbSAlan Cox 	}
115983e0bbcbSAlan Cox 
11601da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
11611da177e4SLinus Torvalds 
11621da177e4SLinus Torvalds 	/* Build a packet */
11631da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
11641da177e4SLinus Torvalds 
11651da177e4SLinus Torvalds 	if ((msg->msg_flags & MSG_OOB) && len > 32)
11661da177e4SLinus Torvalds 		len = 32;
11671da177e4SLinus Torvalds 
11681da177e4SLinus Torvalds 	size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
11691da177e4SLinus Torvalds 
117077b22836SArnd Bergmann 	release_sock(sk);
11711da177e4SLinus Torvalds 	skb = sock_alloc_send_skb(sk, size, noblock, &rc);
117277b22836SArnd Bergmann 	lock_sock(sk);
11731da177e4SLinus Torvalds 	if (!skb)
11741da177e4SLinus Torvalds 		goto out;
11751da177e4SLinus Torvalds 	X25_SKB_CB(skb)->flags = msg->msg_flags;
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 	skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
11781da177e4SLinus Torvalds 
11791da177e4SLinus Torvalds 	/*
11801da177e4SLinus Torvalds 	 *	Put the data on the end
11811da177e4SLinus Torvalds 	 */
11821da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
11831da177e4SLinus Torvalds 
1184eeeb0374SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
1185eeeb0374SArnaldo Carvalho de Melo 	skb_put(skb, len);
11861da177e4SLinus Torvalds 
11876ce8e9ceSAl Viro 	rc = memcpy_from_msg(skb_transport_header(skb), msg, len);
11881da177e4SLinus Torvalds 	if (rc)
11891da177e4SLinus Torvalds 		goto out_kfree_skb;
11901da177e4SLinus Torvalds 
11911da177e4SLinus Torvalds 	/*
11921da177e4SLinus Torvalds 	 *	If the Q BIT Include socket option is in force, the first
11931da177e4SLinus Torvalds 	 *	byte of the user data is the logical value of the Q Bit.
11941da177e4SLinus Torvalds 	 */
1195cb863ffdSandrew hendry 	if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
1196cb101ed2SMatthew Daley 		if (!pskb_may_pull(skb, 1))
1197cb101ed2SMatthew Daley 			goto out_kfree_skb;
1198cb101ed2SMatthew Daley 
11991da177e4SLinus Torvalds 		qbit = skb->data[0];
12001da177e4SLinus Torvalds 		skb_pull(skb, 1);
12011da177e4SLinus Torvalds 	}
12021da177e4SLinus Torvalds 
12031da177e4SLinus Torvalds 	/*
12041da177e4SLinus Torvalds 	 *	Push down the X.25 header
12051da177e4SLinus Torvalds 	 */
12061da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
12071da177e4SLinus Torvalds 
12081da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_OOB) {
12091da177e4SLinus Torvalds 		if (x25->neighbour->extended) {
12101da177e4SLinus Torvalds 			asmptr    = skb_push(skb, X25_STD_MIN_LEN);
12111da177e4SLinus Torvalds 			*asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
12121da177e4SLinus Torvalds 			*asmptr++ = (x25->lci >> 0) & 0xFF;
12131da177e4SLinus Torvalds 			*asmptr++ = X25_INTERRUPT;
12141da177e4SLinus Torvalds 		} else {
12151da177e4SLinus Torvalds 			asmptr    = skb_push(skb, X25_STD_MIN_LEN);
12161da177e4SLinus Torvalds 			*asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
12171da177e4SLinus Torvalds 			*asmptr++ = (x25->lci >> 0) & 0xFF;
12181da177e4SLinus Torvalds 			*asmptr++ = X25_INTERRUPT;
12191da177e4SLinus Torvalds 		}
12201da177e4SLinus Torvalds 	} else {
12211da177e4SLinus Torvalds 		if (x25->neighbour->extended) {
12221da177e4SLinus Torvalds 			/* Build an Extended X.25 header */
12231da177e4SLinus Torvalds 			asmptr    = skb_push(skb, X25_EXT_MIN_LEN);
12241da177e4SLinus Torvalds 			*asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
12251da177e4SLinus Torvalds 			*asmptr++ = (x25->lci >> 0) & 0xFF;
12261da177e4SLinus Torvalds 			*asmptr++ = X25_DATA;
12271da177e4SLinus Torvalds 			*asmptr++ = X25_DATA;
12281da177e4SLinus Torvalds 		} else {
12291da177e4SLinus Torvalds 			/* Build an Standard X.25 header */
12301da177e4SLinus Torvalds 			asmptr    = skb_push(skb, X25_STD_MIN_LEN);
12311da177e4SLinus Torvalds 			*asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
12321da177e4SLinus Torvalds 			*asmptr++ = (x25->lci >> 0) & 0xFF;
12331da177e4SLinus Torvalds 			*asmptr++ = X25_DATA;
12341da177e4SLinus Torvalds 		}
12351da177e4SLinus Torvalds 
12361da177e4SLinus Torvalds 		if (qbit)
12371da177e4SLinus Torvalds 			skb->data[0] |= X25_Q_BIT;
12381da177e4SLinus Torvalds 	}
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
12411da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
12421da177e4SLinus Torvalds 
12431da177e4SLinus Torvalds 	rc = -ENOTCONN;
12441da177e4SLinus Torvalds 	if (sk->sk_state != TCP_ESTABLISHED)
12451da177e4SLinus Torvalds 		goto out_kfree_skb;
12461da177e4SLinus Torvalds 
12471da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_OOB)
12481da177e4SLinus Torvalds 		skb_queue_tail(&x25->interrupt_out_queue, skb);
12491da177e4SLinus Torvalds 	else {
12508db09f26SRoel Kluin 		rc = x25_output(sk, skb);
12518db09f26SRoel Kluin 		len = rc;
12528db09f26SRoel Kluin 		if (rc < 0)
12531da177e4SLinus Torvalds 			kfree_skb(skb);
1254cb863ffdSandrew hendry 		else if (test_bit(X25_Q_BIT_FLAG, &x25->flags))
12551da177e4SLinus Torvalds 			len++;
12561da177e4SLinus Torvalds 	}
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 	x25_kick(sk);
12591da177e4SLinus Torvalds 	rc = len;
12601da177e4SLinus Torvalds out:
126177b22836SArnd Bergmann 	release_sock(sk);
12621da177e4SLinus Torvalds 	return rc;
12631da177e4SLinus Torvalds out_kfree_skb:
12641da177e4SLinus Torvalds 	kfree_skb(skb);
12651da177e4SLinus Torvalds 	goto out;
12661da177e4SLinus Torvalds }
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds 
12691b784140SYing Xue static int x25_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
12701da177e4SLinus Torvalds 		       int flags)
12711da177e4SLinus Torvalds {
12721da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
12731da177e4SLinus Torvalds 	struct x25_sock *x25 = x25_sk(sk);
1274342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_x25 *, sx25, msg->msg_name);
12751da177e4SLinus Torvalds 	size_t copied;
1276501e89d3SDave Jones 	int qbit, header_len;
12771da177e4SLinus Torvalds 	struct sk_buff *skb;
12781da177e4SLinus Torvalds 	unsigned char *asmptr;
12791da177e4SLinus Torvalds 	int rc = -ENOTCONN;
12801da177e4SLinus Torvalds 
128177b22836SArnd Bergmann 	lock_sock(sk);
1282501e89d3SDave Jones 
1283501e89d3SDave Jones 	if (x25->neighbour == NULL)
1284501e89d3SDave Jones 		goto out;
1285501e89d3SDave Jones 
1286501e89d3SDave Jones 	header_len = x25->neighbour->extended ?
1287501e89d3SDave Jones 		X25_EXT_MIN_LEN : X25_STD_MIN_LEN;
1288501e89d3SDave Jones 
12891da177e4SLinus Torvalds 	/*
12901da177e4SLinus Torvalds 	 * This works for seqpacket too. The receiver has ordered the queue for
12911da177e4SLinus Torvalds 	 * us! We do one quick check first though
12921da177e4SLinus Torvalds 	 */
12931da177e4SLinus Torvalds 	if (sk->sk_state != TCP_ESTABLISHED)
12941da177e4SLinus Torvalds 		goto out;
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 	if (flags & MSG_OOB) {
12971da177e4SLinus Torvalds 		rc = -EINVAL;
12981da177e4SLinus Torvalds 		if (sock_flag(sk, SOCK_URGINLINE) ||
12991da177e4SLinus Torvalds 		    !skb_peek(&x25->interrupt_in_queue))
13001da177e4SLinus Torvalds 			goto out;
13011da177e4SLinus Torvalds 
13021da177e4SLinus Torvalds 		skb = skb_dequeue(&x25->interrupt_in_queue);
13031da177e4SLinus Torvalds 
1304cb101ed2SMatthew Daley 		if (!pskb_may_pull(skb, X25_STD_MIN_LEN))
1305cb101ed2SMatthew Daley 			goto out_free_dgram;
1306cb101ed2SMatthew Daley 
13071da177e4SLinus Torvalds 		skb_pull(skb, X25_STD_MIN_LEN);
13081da177e4SLinus Torvalds 
13091da177e4SLinus Torvalds 		/*
13101da177e4SLinus Torvalds 		 *	No Q bit information on Interrupt data.
13111da177e4SLinus Torvalds 		 */
1312cb863ffdSandrew hendry 		if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
13131da177e4SLinus Torvalds 			asmptr  = skb_push(skb, 1);
13141da177e4SLinus Torvalds 			*asmptr = 0x00;
13151da177e4SLinus Torvalds 		}
13161da177e4SLinus Torvalds 
13171da177e4SLinus Torvalds 		msg->msg_flags |= MSG_OOB;
13181da177e4SLinus Torvalds 	} else {
13191da177e4SLinus Torvalds 		/* Now we can treat all alike */
132077b22836SArnd Bergmann 		release_sock(sk);
13211da177e4SLinus Torvalds 		skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
13221da177e4SLinus Torvalds 					flags & MSG_DONTWAIT, &rc);
132377b22836SArnd Bergmann 		lock_sock(sk);
13241da177e4SLinus Torvalds 		if (!skb)
13251da177e4SLinus Torvalds 			goto out;
13261da177e4SLinus Torvalds 
1327cb101ed2SMatthew Daley 		if (!pskb_may_pull(skb, header_len))
1328cb101ed2SMatthew Daley 			goto out_free_dgram;
1329cb101ed2SMatthew Daley 
13301da177e4SLinus Torvalds 		qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
13311da177e4SLinus Torvalds 
1332cb101ed2SMatthew Daley 		skb_pull(skb, header_len);
13331da177e4SLinus Torvalds 
1334cb863ffdSandrew hendry 		if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
13351da177e4SLinus Torvalds 			asmptr  = skb_push(skb, 1);
13361da177e4SLinus Torvalds 			*asmptr = qbit;
13371da177e4SLinus Torvalds 		}
13381da177e4SLinus Torvalds 	}
13391da177e4SLinus Torvalds 
1340badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
13411da177e4SLinus Torvalds 	copied = skb->len;
13421da177e4SLinus Torvalds 
13431da177e4SLinus Torvalds 	if (copied > size) {
13441da177e4SLinus Torvalds 		copied = size;
13451da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
13461da177e4SLinus Torvalds 	}
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds 	/* Currently, each datagram always contains a complete record */
13491da177e4SLinus Torvalds 	msg->msg_flags |= MSG_EOR;
13501da177e4SLinus Torvalds 
135151f3d02bSDavid S. Miller 	rc = skb_copy_datagram_msg(skb, 0, msg, copied);
13521da177e4SLinus Torvalds 	if (rc)
13531da177e4SLinus Torvalds 		goto out_free_dgram;
13541da177e4SLinus Torvalds 
13551da177e4SLinus Torvalds 	if (sx25) {
13561da177e4SLinus Torvalds 		sx25->sx25_family = AF_X25;
13571da177e4SLinus Torvalds 		sx25->sx25_addr   = x25->dest_addr;
1358f3d33426SHannes Frederic Sowa 		msg->msg_namelen = sizeof(*sx25);
13591da177e4SLinus Torvalds 	}
13601da177e4SLinus Torvalds 
13611da177e4SLinus Torvalds 	x25_check_rbuf(sk);
13621da177e4SLinus Torvalds 	rc = copied;
13631da177e4SLinus Torvalds out_free_dgram:
13641da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
13651da177e4SLinus Torvalds out:
136677b22836SArnd Bergmann 	release_sock(sk);
13671da177e4SLinus Torvalds 	return rc;
13681da177e4SLinus Torvalds }
13691da177e4SLinus Torvalds 
13701da177e4SLinus Torvalds 
13711da177e4SLinus Torvalds static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
13721da177e4SLinus Torvalds {
13731da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
13741da177e4SLinus Torvalds 	struct x25_sock *x25 = x25_sk(sk);
13751da177e4SLinus Torvalds 	void __user *argp = (void __user *)arg;
13761da177e4SLinus Torvalds 	int rc;
13771da177e4SLinus Torvalds 
13781da177e4SLinus Torvalds 	switch (cmd) {
13791da177e4SLinus Torvalds 	case TIOCOUTQ: {
138070be998cSandrew hendry 		int amount;
138131e6d363SEric Dumazet 
138270be998cSandrew hendry 		amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
13831da177e4SLinus Torvalds 		if (amount < 0)
13841da177e4SLinus Torvalds 			amount = 0;
13851da177e4SLinus Torvalds 		rc = put_user(amount, (unsigned int __user *)argp);
13861da177e4SLinus Torvalds 		break;
13871da177e4SLinus Torvalds 	}
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds 	case TIOCINQ: {
13901da177e4SLinus Torvalds 		struct sk_buff *skb;
13911da177e4SLinus Torvalds 		int amount = 0;
13921da177e4SLinus Torvalds 		/*
13931da177e4SLinus Torvalds 		 * These two are safe on a single CPU system as
13941da177e4SLinus Torvalds 		 * only user tasks fiddle here
13951da177e4SLinus Torvalds 		 */
139654aafbd4Sandrew hendry 		lock_sock(sk);
13971da177e4SLinus Torvalds 		if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
13981da177e4SLinus Torvalds 			amount = skb->len;
139954aafbd4Sandrew hendry 		release_sock(sk);
14001da177e4SLinus Torvalds 		rc = put_user(amount, (unsigned int __user *)argp);
14011da177e4SLinus Torvalds 		break;
14021da177e4SLinus Torvalds 	}
14031da177e4SLinus Torvalds 
14041da177e4SLinus Torvalds 	case SIOCGIFADDR:
14051da177e4SLinus Torvalds 	case SIOCSIFADDR:
14061da177e4SLinus Torvalds 	case SIOCGIFDSTADDR:
14071da177e4SLinus Torvalds 	case SIOCSIFDSTADDR:
14081da177e4SLinus Torvalds 	case SIOCGIFBRDADDR:
14091da177e4SLinus Torvalds 	case SIOCSIFBRDADDR:
14101da177e4SLinus Torvalds 	case SIOCGIFNETMASK:
14111da177e4SLinus Torvalds 	case SIOCSIFNETMASK:
14121da177e4SLinus Torvalds 	case SIOCGIFMETRIC:
14131da177e4SLinus Torvalds 	case SIOCSIFMETRIC:
14141da177e4SLinus Torvalds 		rc = -EINVAL;
14151da177e4SLinus Torvalds 		break;
14161da177e4SLinus Torvalds 	case SIOCADDRT:
14171da177e4SLinus Torvalds 	case SIOCDELRT:
14181da177e4SLinus Torvalds 		rc = -EPERM;
14191da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
14201da177e4SLinus Torvalds 			break;
14211da177e4SLinus Torvalds 		rc = x25_route_ioctl(cmd, argp);
14221da177e4SLinus Torvalds 		break;
14231da177e4SLinus Torvalds 	case SIOCX25GSUBSCRIP:
14241da177e4SLinus Torvalds 		rc = x25_subscr_ioctl(cmd, argp);
14251da177e4SLinus Torvalds 		break;
14261da177e4SLinus Torvalds 	case SIOCX25SSUBSCRIP:
14271da177e4SLinus Torvalds 		rc = -EPERM;
14281da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
14291da177e4SLinus Torvalds 			break;
14301da177e4SLinus Torvalds 		rc = x25_subscr_ioctl(cmd, argp);
14311da177e4SLinus Torvalds 		break;
14321da177e4SLinus Torvalds 	case SIOCX25GFACILITIES: {
1433f90de660Sandrew hendry 		lock_sock(sk);
1434f90de660Sandrew hendry 		rc = copy_to_user(argp, &x25->facilities,
1435f90de660Sandrew hendry 				  sizeof(x25->facilities))
1436f90de660Sandrew hendry 			? -EFAULT : 0;
1437f90de660Sandrew hendry 		release_sock(sk);
14381da177e4SLinus Torvalds 		break;
14391da177e4SLinus Torvalds 	}
14401da177e4SLinus Torvalds 
14411da177e4SLinus Torvalds 	case SIOCX25SFACILITIES: {
14421da177e4SLinus Torvalds 		struct x25_facilities facilities;
14431da177e4SLinus Torvalds 		rc = -EFAULT;
1444fddc5f3eSJoe Perches 		if (copy_from_user(&facilities, argp, sizeof(facilities)))
14451da177e4SLinus Torvalds 			break;
14461da177e4SLinus Torvalds 		rc = -EINVAL;
1447f90de660Sandrew hendry 		lock_sock(sk);
14481da177e4SLinus Torvalds 		if (sk->sk_state != TCP_LISTEN &&
14491da177e4SLinus Torvalds 		    sk->sk_state != TCP_CLOSE)
1450f90de660Sandrew hendry 			goto out_fac_release;
14511da177e4SLinus Torvalds 		if (facilities.pacsize_in < X25_PS16 ||
14521da177e4SLinus Torvalds 		    facilities.pacsize_in > X25_PS4096)
1453f90de660Sandrew hendry 			goto out_fac_release;
14541da177e4SLinus Torvalds 		if (facilities.pacsize_out < X25_PS16 ||
14551da177e4SLinus Torvalds 		    facilities.pacsize_out > X25_PS4096)
1456f90de660Sandrew hendry 			goto out_fac_release;
14571da177e4SLinus Torvalds 		if (facilities.winsize_in < 1 ||
14581da177e4SLinus Torvalds 		    facilities.winsize_in > 127)
1459f90de660Sandrew hendry 			goto out_fac_release;
1460ddd0451fSJohn Hughes 		if (facilities.throughput) {
1461ddd0451fSJohn Hughes 			int out = facilities.throughput & 0xf0;
1462ddd0451fSJohn Hughes 			int in  = facilities.throughput & 0x0f;
1463ddd0451fSJohn Hughes 			if (!out)
1464ddd0451fSJohn Hughes 				facilities.throughput |=
1465ddd0451fSJohn Hughes 					X25_DEFAULT_THROUGHPUT << 4;
1466ddd0451fSJohn Hughes 			else if (out < 0x30 || out > 0xD0)
1467f90de660Sandrew hendry 				goto out_fac_release;
1468ddd0451fSJohn Hughes 			if (!in)
1469ddd0451fSJohn Hughes 				facilities.throughput |=
1470ddd0451fSJohn Hughes 					X25_DEFAULT_THROUGHPUT;
1471ddd0451fSJohn Hughes 			else if (in < 0x03 || in > 0x0D)
1472f90de660Sandrew hendry 				goto out_fac_release;
1473ddd0451fSJohn Hughes 		}
1474ebc3f64bSShaun Pereira 		if (facilities.reverse &&
147506a96b33Sroel kluin 		    (facilities.reverse & 0x81) != 0x81)
1476f90de660Sandrew hendry 			goto out_fac_release;
14771da177e4SLinus Torvalds 		x25->facilities = facilities;
14781da177e4SLinus Torvalds 		rc = 0;
1479f90de660Sandrew hendry out_fac_release:
1480f90de660Sandrew hendry 		release_sock(sk);
14811da177e4SLinus Torvalds 		break;
14821da177e4SLinus Torvalds 	}
14831da177e4SLinus Torvalds 
1484a64b7b93SShaun Pereira 	case SIOCX25GDTEFACILITIES: {
1485f90de660Sandrew hendry 		lock_sock(sk);
1486a64b7b93SShaun Pereira 		rc = copy_to_user(argp, &x25->dte_facilities,
1487a64b7b93SShaun Pereira 				  sizeof(x25->dte_facilities));
1488f90de660Sandrew hendry 		release_sock(sk);
1489a64b7b93SShaun Pereira 		if (rc)
1490a64b7b93SShaun Pereira 			rc = -EFAULT;
1491a64b7b93SShaun Pereira 		break;
1492a64b7b93SShaun Pereira 	}
1493a64b7b93SShaun Pereira 
1494a64b7b93SShaun Pereira 	case SIOCX25SDTEFACILITIES: {
1495a64b7b93SShaun Pereira 		struct x25_dte_facilities dtefacs;
1496a64b7b93SShaun Pereira 		rc = -EFAULT;
1497a64b7b93SShaun Pereira 		if (copy_from_user(&dtefacs, argp, sizeof(dtefacs)))
1498a64b7b93SShaun Pereira 			break;
1499a64b7b93SShaun Pereira 		rc = -EINVAL;
1500f90de660Sandrew hendry 		lock_sock(sk);
1501a64b7b93SShaun Pereira 		if (sk->sk_state != TCP_LISTEN &&
1502a64b7b93SShaun Pereira 		    sk->sk_state != TCP_CLOSE)
1503f90de660Sandrew hendry 			goto out_dtefac_release;
1504a64b7b93SShaun Pereira 		if (dtefacs.calling_len > X25_MAX_AE_LEN)
1505f90de660Sandrew hendry 			goto out_dtefac_release;
1506a64b7b93SShaun Pereira 		if (dtefacs.called_len > X25_MAX_AE_LEN)
1507f90de660Sandrew hendry 			goto out_dtefac_release;
1508a64b7b93SShaun Pereira 		x25->dte_facilities = dtefacs;
1509a64b7b93SShaun Pereira 		rc = 0;
1510f90de660Sandrew hendry out_dtefac_release:
1511f90de660Sandrew hendry 		release_sock(sk);
1512a64b7b93SShaun Pereira 		break;
1513a64b7b93SShaun Pereira 	}
1514a64b7b93SShaun Pereira 
15151da177e4SLinus Torvalds 	case SIOCX25GCALLUSERDATA: {
15165b7958dfSandrew hendry 		lock_sock(sk);
15175b7958dfSandrew hendry 		rc = copy_to_user(argp, &x25->calluserdata,
15185b7958dfSandrew hendry 				  sizeof(x25->calluserdata))
15195b7958dfSandrew hendry 			? -EFAULT : 0;
15205b7958dfSandrew hendry 		release_sock(sk);
15211da177e4SLinus Torvalds 		break;
15221da177e4SLinus Torvalds 	}
15231da177e4SLinus Torvalds 
15241da177e4SLinus Torvalds 	case SIOCX25SCALLUSERDATA: {
15251da177e4SLinus Torvalds 		struct x25_calluserdata calluserdata;
15261da177e4SLinus Torvalds 
15271da177e4SLinus Torvalds 		rc = -EFAULT;
1528fddc5f3eSJoe Perches 		if (copy_from_user(&calluserdata, argp, sizeof(calluserdata)))
15291da177e4SLinus Torvalds 			break;
15301da177e4SLinus Torvalds 		rc = -EINVAL;
15311da177e4SLinus Torvalds 		if (calluserdata.cudlength > X25_MAX_CUD_LEN)
15321da177e4SLinus Torvalds 			break;
15335b7958dfSandrew hendry 		lock_sock(sk);
15341da177e4SLinus Torvalds 		x25->calluserdata = calluserdata;
15355b7958dfSandrew hendry 		release_sock(sk);
15361da177e4SLinus Torvalds 		rc = 0;
15371da177e4SLinus Torvalds 		break;
15381da177e4SLinus Torvalds 	}
15391da177e4SLinus Torvalds 
15401da177e4SLinus Torvalds 	case SIOCX25GCAUSEDIAG: {
154174a7e440Sandrew hendry 		lock_sock(sk);
1542fddc5f3eSJoe Perches 		rc = copy_to_user(argp, &x25->causediag, sizeof(x25->causediag))
154374a7e440Sandrew hendry 			? -EFAULT : 0;
154474a7e440Sandrew hendry 		release_sock(sk);
15451da177e4SLinus Torvalds 		break;
15461da177e4SLinus Torvalds 	}
15471da177e4SLinus Torvalds 
1548386e50ccSAndrew Hendry 	case SIOCX25SCAUSEDIAG: {
1549386e50ccSAndrew Hendry 		struct x25_causediag causediag;
1550386e50ccSAndrew Hendry 		rc = -EFAULT;
1551386e50ccSAndrew Hendry 		if (copy_from_user(&causediag, argp, sizeof(causediag)))
1552386e50ccSAndrew Hendry 			break;
155374a7e440Sandrew hendry 		lock_sock(sk);
1554386e50ccSAndrew Hendry 		x25->causediag = causediag;
155574a7e440Sandrew hendry 		release_sock(sk);
1556386e50ccSAndrew Hendry 		rc = 0;
1557386e50ccSAndrew Hendry 		break;
1558386e50ccSAndrew Hendry 
1559386e50ccSAndrew Hendry 	}
1560386e50ccSAndrew Hendry 
1561cb65d506SShaun Pereira 	case SIOCX25SCUDMATCHLEN: {
1562cb65d506SShaun Pereira 		struct x25_subaddr sub_addr;
1563cb65d506SShaun Pereira 		rc = -EINVAL;
15643f0a069aSandrew hendry 		lock_sock(sk);
1565cb65d506SShaun Pereira 		if(sk->sk_state != TCP_CLOSE)
15663f0a069aSandrew hendry 			goto out_cud_release;
1567cb65d506SShaun Pereira 		rc = -EFAULT;
1568cb65d506SShaun Pereira 		if (copy_from_user(&sub_addr, argp,
1569cb65d506SShaun Pereira 				   sizeof(sub_addr)))
15703f0a069aSandrew hendry 			goto out_cud_release;
1571cb65d506SShaun Pereira 		rc = -EINVAL;
1572cb65d506SShaun Pereira 		if (sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
15733f0a069aSandrew hendry 			goto out_cud_release;
1574cb65d506SShaun Pereira 		x25->cudmatchlength = sub_addr.cudmatchlength;
1575cb65d506SShaun Pereira 		rc = 0;
15763f0a069aSandrew hendry out_cud_release:
15773f0a069aSandrew hendry 		release_sock(sk);
1578cb65d506SShaun Pereira 		break;
1579cb65d506SShaun Pereira 	}
1580cb65d506SShaun Pereira 
1581ebc3f64bSShaun Pereira 	case SIOCX25CALLACCPTAPPRV: {
1582ebc3f64bSShaun Pereira 		rc = -EINVAL;
158377b22836SArnd Bergmann 		lock_sock(sk);
15844ccb93ceSDave Jones 		if (sk->sk_state == TCP_CLOSE) {
158537cda787Sandrew hendry 			clear_bit(X25_ACCPT_APPRV_FLAG, &x25->flags);
1586ebc3f64bSShaun Pereira 			rc = 0;
15874ccb93ceSDave Jones 		}
15884ccb93ceSDave Jones 		release_sock(sk);
1589ebc3f64bSShaun Pereira 		break;
1590ebc3f64bSShaun Pereira 	}
1591ebc3f64bSShaun Pereira 
1592ebc3f64bSShaun Pereira 	case SIOCX25SENDCALLACCPT:  {
1593ebc3f64bSShaun Pereira 		rc = -EINVAL;
159477b22836SArnd Bergmann 		lock_sock(sk);
1595ebc3f64bSShaun Pereira 		if (sk->sk_state != TCP_ESTABLISHED)
15964ccb93ceSDave Jones 			goto out_sendcallaccpt_release;
159737cda787Sandrew hendry 		/* must call accptapprv above */
159837cda787Sandrew hendry 		if (test_bit(X25_ACCPT_APPRV_FLAG, &x25->flags))
15994ccb93ceSDave Jones 			goto out_sendcallaccpt_release;
1600ebc3f64bSShaun Pereira 		x25_write_internal(sk, X25_CALL_ACCEPTED);
1601ebc3f64bSShaun Pereira 		x25->state = X25_STATE_3;
1602ebc3f64bSShaun Pereira 		rc = 0;
16034ccb93ceSDave Jones out_sendcallaccpt_release:
16044ccb93ceSDave Jones 		release_sock(sk);
1605ebc3f64bSShaun Pereira 		break;
1606ebc3f64bSShaun Pereira 	}
1607ebc3f64bSShaun Pereira 
16081da177e4SLinus Torvalds 	default:
1609b5e5fa5eSChristoph Hellwig 		rc = -ENOIOCTLCMD;
16101da177e4SLinus Torvalds 		break;
16111da177e4SLinus Torvalds 	}
16121da177e4SLinus Torvalds 
16131da177e4SLinus Torvalds 	return rc;
16141da177e4SLinus Torvalds }
16151da177e4SLinus Torvalds 
1616ec1b4cf7SStephen Hemminger static const struct net_proto_family x25_family_ops = {
16171da177e4SLinus Torvalds 	.family =	AF_X25,
16181da177e4SLinus Torvalds 	.create =	x25_create,
16191da177e4SLinus Torvalds 	.owner	=	THIS_MODULE,
16201da177e4SLinus Torvalds };
16211da177e4SLinus Torvalds 
16221b06e6baSShaun Pereira #ifdef CONFIG_COMPAT
16231b06e6baSShaun Pereira static int compat_x25_subscr_ioctl(unsigned int cmd,
16241b06e6baSShaun Pereira 		struct compat_x25_subscrip_struct __user *x25_subscr32)
16251b06e6baSShaun Pereira {
16261b06e6baSShaun Pereira 	struct compat_x25_subscrip_struct x25_subscr;
16271b06e6baSShaun Pereira 	struct x25_neigh *nb;
16281b06e6baSShaun Pereira 	struct net_device *dev;
16291b06e6baSShaun Pereira 	int rc = -EINVAL;
16301b06e6baSShaun Pereira 
16311b06e6baSShaun Pereira 	rc = -EFAULT;
16321b06e6baSShaun Pereira 	if (copy_from_user(&x25_subscr, x25_subscr32, sizeof(*x25_subscr32)))
16331b06e6baSShaun Pereira 		goto out;
16341b06e6baSShaun Pereira 
16351b06e6baSShaun Pereira 	rc = -EINVAL;
16361b06e6baSShaun Pereira 	dev = x25_dev_get(x25_subscr.device);
16371b06e6baSShaun Pereira 	if (dev == NULL)
16381b06e6baSShaun Pereira 		goto out;
16391b06e6baSShaun Pereira 
16401b06e6baSShaun Pereira 	nb = x25_get_neigh(dev);
16411b06e6baSShaun Pereira 	if (nb == NULL)
16421b06e6baSShaun Pereira 		goto out_dev_put;
16431b06e6baSShaun Pereira 
16441b06e6baSShaun Pereira 	dev_put(dev);
16451b06e6baSShaun Pereira 
16461b06e6baSShaun Pereira 	if (cmd == SIOCX25GSUBSCRIP) {
16475595a1a5Sandrew hendry 		read_lock_bh(&x25_neigh_list_lock);
16481b06e6baSShaun Pereira 		x25_subscr.extended = nb->extended;
16491b06e6baSShaun Pereira 		x25_subscr.global_facil_mask = nb->global_facil_mask;
16505595a1a5Sandrew hendry 		read_unlock_bh(&x25_neigh_list_lock);
16511b06e6baSShaun Pereira 		rc = copy_to_user(x25_subscr32, &x25_subscr,
16521b06e6baSShaun Pereira 				sizeof(*x25_subscr32)) ? -EFAULT : 0;
16531b06e6baSShaun Pereira 	} else {
16541b06e6baSShaun Pereira 		rc = -EINVAL;
16551b06e6baSShaun Pereira 		if (x25_subscr.extended == 0 || x25_subscr.extended == 1) {
16561b06e6baSShaun Pereira 			rc = 0;
16575595a1a5Sandrew hendry 			write_lock_bh(&x25_neigh_list_lock);
16581b06e6baSShaun Pereira 			nb->extended = x25_subscr.extended;
16591b06e6baSShaun Pereira 			nb->global_facil_mask = x25_subscr.global_facil_mask;
16605595a1a5Sandrew hendry 			write_unlock_bh(&x25_neigh_list_lock);
16611b06e6baSShaun Pereira 		}
16621b06e6baSShaun Pereira 	}
16631b06e6baSShaun Pereira 	x25_neigh_put(nb);
16641b06e6baSShaun Pereira out:
16651b06e6baSShaun Pereira 	return rc;
16661b06e6baSShaun Pereira out_dev_put:
16671b06e6baSShaun Pereira 	dev_put(dev);
16681b06e6baSShaun Pereira 	goto out;
16691b06e6baSShaun Pereira }
16701b06e6baSShaun Pereira 
16711b06e6baSShaun Pereira static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
16721b06e6baSShaun Pereira 				unsigned long arg)
16731b06e6baSShaun Pereira {
16741b06e6baSShaun Pereira 	void __user *argp = compat_ptr(arg);
16751b06e6baSShaun Pereira 	int rc = -ENOIOCTLCMD;
16761b06e6baSShaun Pereira 
16771b06e6baSShaun Pereira 	switch(cmd) {
16781b06e6baSShaun Pereira 	case TIOCOUTQ:
16791b06e6baSShaun Pereira 	case TIOCINQ:
16801b06e6baSShaun Pereira 		rc = x25_ioctl(sock, cmd, (unsigned long)argp);
16811b06e6baSShaun Pereira 		break;
16821b06e6baSShaun Pereira 	case SIOCGIFADDR:
16831b06e6baSShaun Pereira 	case SIOCSIFADDR:
16841b06e6baSShaun Pereira 	case SIOCGIFDSTADDR:
16851b06e6baSShaun Pereira 	case SIOCSIFDSTADDR:
16861b06e6baSShaun Pereira 	case SIOCGIFBRDADDR:
16871b06e6baSShaun Pereira 	case SIOCSIFBRDADDR:
16881b06e6baSShaun Pereira 	case SIOCGIFNETMASK:
16891b06e6baSShaun Pereira 	case SIOCSIFNETMASK:
16901b06e6baSShaun Pereira 	case SIOCGIFMETRIC:
16911b06e6baSShaun Pereira 	case SIOCSIFMETRIC:
16921b06e6baSShaun Pereira 		rc = -EINVAL;
16931b06e6baSShaun Pereira 		break;
16941b06e6baSShaun Pereira 	case SIOCADDRT:
16951b06e6baSShaun Pereira 	case SIOCDELRT:
16961b06e6baSShaun Pereira 		rc = -EPERM;
16971b06e6baSShaun Pereira 		if (!capable(CAP_NET_ADMIN))
16981b06e6baSShaun Pereira 			break;
16991b06e6baSShaun Pereira 		rc = x25_route_ioctl(cmd, argp);
17001b06e6baSShaun Pereira 		break;
17011b06e6baSShaun Pereira 	case SIOCX25GSUBSCRIP:
17021b06e6baSShaun Pereira 		rc = compat_x25_subscr_ioctl(cmd, argp);
17031b06e6baSShaun Pereira 		break;
17041b06e6baSShaun Pereira 	case SIOCX25SSUBSCRIP:
17051b06e6baSShaun Pereira 		rc = -EPERM;
17061b06e6baSShaun Pereira 		if (!capable(CAP_NET_ADMIN))
17071b06e6baSShaun Pereira 			break;
17081b06e6baSShaun Pereira 		rc = compat_x25_subscr_ioctl(cmd, argp);
17091b06e6baSShaun Pereira 		break;
17101b06e6baSShaun Pereira 	case SIOCX25GFACILITIES:
17111b06e6baSShaun Pereira 	case SIOCX25SFACILITIES:
17129a6b9f2eSShaun Pereira 	case SIOCX25GDTEFACILITIES:
17139a6b9f2eSShaun Pereira 	case SIOCX25SDTEFACILITIES:
17141b06e6baSShaun Pereira 	case SIOCX25GCALLUSERDATA:
17151b06e6baSShaun Pereira 	case SIOCX25SCALLUSERDATA:
17161b06e6baSShaun Pereira 	case SIOCX25GCAUSEDIAG:
1717386e50ccSAndrew Hendry 	case SIOCX25SCAUSEDIAG:
17181b06e6baSShaun Pereira 	case SIOCX25SCUDMATCHLEN:
17191b06e6baSShaun Pereira 	case SIOCX25CALLACCPTAPPRV:
17201b06e6baSShaun Pereira 	case SIOCX25SENDCALLACCPT:
17211b06e6baSShaun Pereira 		rc = x25_ioctl(sock, cmd, (unsigned long)argp);
17221b06e6baSShaun Pereira 		break;
17231b06e6baSShaun Pereira 	default:
17241b06e6baSShaun Pereira 		rc = -ENOIOCTLCMD;
17251b06e6baSShaun Pereira 		break;
17261b06e6baSShaun Pereira 	}
17271b06e6baSShaun Pereira 	return rc;
17281b06e6baSShaun Pereira }
17291b06e6baSShaun Pereira #endif
17301b06e6baSShaun Pereira 
173191774904SArnd Bergmann static const struct proto_ops x25_proto_ops = {
17321da177e4SLinus Torvalds 	.family =	AF_X25,
17331da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
17341da177e4SLinus Torvalds 	.release =	x25_release,
17351da177e4SLinus Torvalds 	.bind =		x25_bind,
17361da177e4SLinus Torvalds 	.connect =	x25_connect,
17371da177e4SLinus Torvalds 	.socketpair =	sock_no_socketpair,
17381da177e4SLinus Torvalds 	.accept =	x25_accept,
17391da177e4SLinus Torvalds 	.getname =	x25_getname,
1740a11e1d43SLinus Torvalds 	.poll =		datagram_poll,
17411da177e4SLinus Torvalds 	.ioctl =	x25_ioctl,
17421b06e6baSShaun Pereira #ifdef CONFIG_COMPAT
17431b06e6baSShaun Pereira 	.compat_ioctl = compat_x25_ioctl,
17441b06e6baSShaun Pereira #endif
1745c7cbdbf2SArnd Bergmann 	.gettstamp =	sock_gettstamp,
17461da177e4SLinus Torvalds 	.listen =	x25_listen,
17471da177e4SLinus Torvalds 	.shutdown =	sock_no_shutdown,
17481da177e4SLinus Torvalds 	.setsockopt =	x25_setsockopt,
17491da177e4SLinus Torvalds 	.getsockopt =	x25_getsockopt,
17501da177e4SLinus Torvalds 	.sendmsg =	x25_sendmsg,
17511da177e4SLinus Torvalds 	.recvmsg =	x25_recvmsg,
17521da177e4SLinus Torvalds 	.mmap =		sock_no_mmap,
17531da177e4SLinus Torvalds 	.sendpage =	sock_no_sendpage,
17541da177e4SLinus Torvalds };
17551da177e4SLinus Torvalds 
17567546dd97SStephen Hemminger static struct packet_type x25_packet_type __read_mostly = {
175709640e63SHarvey Harrison 	.type =	cpu_to_be16(ETH_P_X25),
17581da177e4SLinus Torvalds 	.func =	x25_lapb_receive_frame,
17591da177e4SLinus Torvalds };
17601da177e4SLinus Torvalds 
17611da177e4SLinus Torvalds static struct notifier_block x25_dev_notifier = {
17621da177e4SLinus Torvalds 	.notifier_call = x25_device_event,
17631da177e4SLinus Torvalds };
17641da177e4SLinus Torvalds 
17651da177e4SLinus Torvalds void x25_kill_by_neigh(struct x25_neigh *nb)
17661da177e4SLinus Torvalds {
17671da177e4SLinus Torvalds 	struct sock *s;
17681da177e4SLinus Torvalds 
17691da177e4SLinus Torvalds 	write_lock_bh(&x25_list_lock);
17701da177e4SLinus Torvalds 
1771b67bfe0dSSasha Levin 	sk_for_each(s, &x25_list)
17721da177e4SLinus Torvalds 		if (x25_sk(s)->neighbour == nb)
17731da177e4SLinus Torvalds 			x25_disconnect(s, ENETUNREACH, 0, 0);
17741da177e4SLinus Torvalds 
17751da177e4SLinus Torvalds 	write_unlock_bh(&x25_list_lock);
177695a9dc43SAndrew Hendry 
177795a9dc43SAndrew Hendry 	/* Remove any related forwards */
177895a9dc43SAndrew Hendry 	x25_clear_forward_by_dev(nb->dev);
17791da177e4SLinus Torvalds }
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds static int __init x25_init(void)
17821da177e4SLinus Torvalds {
178364df6d52Slinzhang 	int rc;
17841da177e4SLinus Torvalds 
178564df6d52Slinzhang 	rc = proto_register(&x25_proto, 0);
178664df6d52Slinzhang 	if (rc)
17871da177e4SLinus Torvalds 		goto out;
17881da177e4SLinus Torvalds 
17891fd975a0Sandrew hendry 	rc = sock_register(&x25_family_ops);
179064df6d52Slinzhang 	if (rc)
17911fd975a0Sandrew hendry 		goto out_proto;
17921da177e4SLinus Torvalds 
17931da177e4SLinus Torvalds 	dev_add_pack(&x25_packet_type);
17941da177e4SLinus Torvalds 
17951fd975a0Sandrew hendry 	rc = register_netdevice_notifier(&x25_dev_notifier);
179664df6d52Slinzhang 	if (rc)
17971fd975a0Sandrew hendry 		goto out_sock;
17981da177e4SLinus Torvalds 
179964df6d52Slinzhang 	rc = x25_register_sysctl();
180064df6d52Slinzhang 	if (rc)
180164df6d52Slinzhang 		goto out_dev;
180264df6d52Slinzhang 
180364df6d52Slinzhang 	rc = x25_proc_init();
180464df6d52Slinzhang 	if (rc)
180564df6d52Slinzhang 		goto out_sysctl;
180664df6d52Slinzhang 
1807b73e9e3cSwangweidong 	pr_info("Linux Version 0.2\n");
18081da177e4SLinus Torvalds 
18091da177e4SLinus Torvalds out:
18101da177e4SLinus Torvalds 	return rc;
181164df6d52Slinzhang out_sysctl:
181264df6d52Slinzhang 	x25_unregister_sysctl();
18131fd975a0Sandrew hendry out_dev:
18141fd975a0Sandrew hendry 	unregister_netdevice_notifier(&x25_dev_notifier);
18151fd975a0Sandrew hendry out_sock:
181664df6d52Slinzhang 	dev_remove_pack(&x25_packet_type);
18171fd975a0Sandrew hendry 	sock_unregister(AF_X25);
18181fd975a0Sandrew hendry out_proto:
18191fd975a0Sandrew hendry 	proto_unregister(&x25_proto);
18201fd975a0Sandrew hendry 	goto out;
18211da177e4SLinus Torvalds }
18221da177e4SLinus Torvalds module_init(x25_init);
18231da177e4SLinus Torvalds 
18241da177e4SLinus Torvalds static void __exit x25_exit(void)
18251da177e4SLinus Torvalds {
18261da177e4SLinus Torvalds 	x25_proc_exit();
18271da177e4SLinus Torvalds 	x25_link_free();
18281da177e4SLinus Torvalds 	x25_route_free();
18291da177e4SLinus Torvalds 
18301da177e4SLinus Torvalds 	x25_unregister_sysctl();
18311da177e4SLinus Torvalds 
18321da177e4SLinus Torvalds 	unregister_netdevice_notifier(&x25_dev_notifier);
18331da177e4SLinus Torvalds 
18341da177e4SLinus Torvalds 	dev_remove_pack(&x25_packet_type);
18351da177e4SLinus Torvalds 
18361da177e4SLinus Torvalds 	sock_unregister(AF_X25);
18371da177e4SLinus Torvalds 	proto_unregister(&x25_proto);
18381da177e4SLinus Torvalds }
18391da177e4SLinus Torvalds module_exit(x25_exit);
18401da177e4SLinus Torvalds 
18411da177e4SLinus Torvalds MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
18421da177e4SLinus Torvalds MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
18431da177e4SLinus Torvalds MODULE_LICENSE("GPL");
18441da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_X25);
1845