xref: /openbmc/linux/net/x25/af_x25.c (revision 9b2c45d479d0fb8647c9e83359df69162b5fbe5f)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	X.25 Packet Layer release 002
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *	This is ALPHA test software. This code may break your machine,
51da177e4SLinus Torvalds  *	randomly fail to work with new releases, misbehave and/or generally
61da177e4SLinus Torvalds  *	screw up. It might even work.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *	This code REQUIRES 2.1.15 or higher
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *	This module:
111da177e4SLinus Torvalds  *		This module is free software; you can redistribute it and/or
121da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
131da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
141da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *	History
171da177e4SLinus Torvalds  *	X.25 001	Jonathan Naylor	Started coding.
181da177e4SLinus Torvalds  *	X.25 002	Jonathan Naylor	Centralised disconnect handling.
191da177e4SLinus Torvalds  *					New timer architecture.
201da177e4SLinus Torvalds  *	2000-03-11	Henner Eisen	MSG_EOR handling more POSIX compliant.
211da177e4SLinus Torvalds  *	2000-03-22	Daniela Squassoni Allowed disabling/enabling of
221da177e4SLinus Torvalds  *					  facilities negotiation and increased
231da177e4SLinus Torvalds  *					  the throughput upper limit.
241da177e4SLinus Torvalds  *	2000-08-27	Arnaldo C. Melo s/suser/capable/ + micro cleanups
251da177e4SLinus Torvalds  *	2000-09-04	Henner Eisen	Set sock->state in x25_accept().
261da177e4SLinus Torvalds  *					Fixed x25_output() related skb leakage.
271da177e4SLinus Torvalds  *	2000-10-02	Henner Eisen	Made x25_kick() single threaded per socket.
281da177e4SLinus Torvalds  *	2000-10-27	Henner Eisen    MSG_DONTWAIT for fragment allocation.
291da177e4SLinus Torvalds  *	2000-11-14	Henner Eisen    Closing datalink from NETDEV_GOING_DOWN
301da177e4SLinus Torvalds  *	2002-10-06	Arnaldo C. Melo Get rid of cli/sti, move proc stuff to
311da177e4SLinus Torvalds  *					x25_proc.c, using seq_file
32cb65d506SShaun Pereira  *	2005-04-02	Shaun Pereira	Selective sub address matching
33cb65d506SShaun Pereira  *					with call user data
34ebc3f64bSShaun Pereira  *	2005-04-15	Shaun Pereira	Fast select with no restriction on
35ebc3f64bSShaun Pereira  *					response
361da177e4SLinus Torvalds  */
371da177e4SLinus Torvalds 
38b73e9e3cSwangweidong #define pr_fmt(fmt) "X25: " fmt
39b73e9e3cSwangweidong 
401da177e4SLinus Torvalds #include <linux/module.h>
414fc268d2SRandy Dunlap #include <linux/capability.h>
421da177e4SLinus Torvalds #include <linux/errno.h>
431da177e4SLinus Torvalds #include <linux/kernel.h>
443f07c014SIngo Molnar #include <linux/sched/signal.h>
451da177e4SLinus Torvalds #include <linux/timer.h>
461da177e4SLinus Torvalds #include <linux/string.h>
471da177e4SLinus Torvalds #include <linux/net.h>
481da177e4SLinus Torvalds #include <linux/netdevice.h>
491da177e4SLinus Torvalds #include <linux/if_arp.h>
501da177e4SLinus Torvalds #include <linux/skbuff.h>
515a0e3ad6STejun Heo #include <linux/slab.h>
521da177e4SLinus Torvalds #include <net/sock.h>
53c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h>
547c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
551da177e4SLinus Torvalds #include <linux/fcntl.h>
561da177e4SLinus Torvalds #include <linux/termios.h>	/* For TIOCINQ/OUTQ */
571da177e4SLinus Torvalds #include <linux/notifier.h>
581da177e4SLinus Torvalds #include <linux/init.h>
591b06e6baSShaun Pereira #include <linux/compat.h>
60a9288525Sandrew hendry #include <linux/ctype.h>
611b06e6baSShaun Pereira 
621da177e4SLinus Torvalds #include <net/x25.h>
631b06e6baSShaun Pereira #include <net/compat.h>
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
661da177e4SLinus Torvalds int sysctl_x25_call_request_timeout    = X25_DEFAULT_T21;
671da177e4SLinus Torvalds int sysctl_x25_reset_request_timeout   = X25_DEFAULT_T22;
681da177e4SLinus Torvalds int sysctl_x25_clear_request_timeout   = X25_DEFAULT_T23;
691da177e4SLinus Torvalds int sysctl_x25_ack_holdback_timeout    = X25_DEFAULT_T2;
7039e21c0dSAndrew Hendry int sysctl_x25_forward                 = 0;
711da177e4SLinus Torvalds 
721da177e4SLinus Torvalds HLIST_HEAD(x25_list);
731da177e4SLinus Torvalds DEFINE_RWLOCK(x25_list_lock);
741da177e4SLinus Torvalds 
7590ddc4f0SEric Dumazet static const struct proto_ops x25_proto_ops;
761da177e4SLinus Torvalds 
77e9e6c232SJulia Lawall static const struct x25_address null_x25_address = {"               "};
781da177e4SLinus Torvalds 
791b06e6baSShaun Pereira #ifdef CONFIG_COMPAT
801b06e6baSShaun Pereira struct compat_x25_subscrip_struct {
811b06e6baSShaun Pereira 	char device[200-sizeof(compat_ulong_t)];
821b06e6baSShaun Pereira 	compat_ulong_t global_facil_mask;
831b06e6baSShaun Pereira 	compat_uint_t extended;
841b06e6baSShaun Pereira };
851b06e6baSShaun Pereira #endif
861b06e6baSShaun Pereira 
87f5eb917bSJohn Hughes 
88f5eb917bSJohn Hughes int x25_parse_address_block(struct sk_buff *skb,
89f5eb917bSJohn Hughes 		struct x25_address *called_addr,
90f5eb917bSJohn Hughes 		struct x25_address *calling_addr)
91f5eb917bSJohn Hughes {
92f5eb917bSJohn Hughes 	unsigned char len;
93f5eb917bSJohn Hughes 	int needed;
94f5eb917bSJohn Hughes 	int rc;
95f5eb917bSJohn Hughes 
96cb101ed2SMatthew Daley 	if (!pskb_may_pull(skb, 1)) {
97f5eb917bSJohn Hughes 		/* packet has no address block */
98f5eb917bSJohn Hughes 		rc = 0;
99f5eb917bSJohn Hughes 		goto empty;
100f5eb917bSJohn Hughes 	}
101f5eb917bSJohn Hughes 
102f5eb917bSJohn Hughes 	len = *skb->data;
103f5eb917bSJohn Hughes 	needed = 1 + (len >> 4) + (len & 0x0f);
104f5eb917bSJohn Hughes 
105cb101ed2SMatthew Daley 	if (!pskb_may_pull(skb, needed)) {
106f5eb917bSJohn Hughes 		/* packet is too short to hold the addresses it claims
107f5eb917bSJohn Hughes 		   to hold */
108f5eb917bSJohn Hughes 		rc = -1;
109f5eb917bSJohn Hughes 		goto empty;
110f5eb917bSJohn Hughes 	}
111f5eb917bSJohn Hughes 
112f5eb917bSJohn Hughes 	return x25_addr_ntoa(skb->data, called_addr, calling_addr);
113f5eb917bSJohn Hughes 
114f5eb917bSJohn Hughes empty:
115f5eb917bSJohn Hughes 	*called_addr->x25_addr = 0;
116f5eb917bSJohn Hughes 	*calling_addr->x25_addr = 0;
117f5eb917bSJohn Hughes 
118f5eb917bSJohn Hughes 	return rc;
119f5eb917bSJohn Hughes }
120f5eb917bSJohn Hughes 
121f5eb917bSJohn Hughes 
1221da177e4SLinus Torvalds int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr,
1231da177e4SLinus Torvalds 		  struct x25_address *calling_addr)
1241da177e4SLinus Torvalds {
1256bf1574eSEric Dumazet 	unsigned int called_len, calling_len;
1261da177e4SLinus Torvalds 	char *called, *calling;
1276bf1574eSEric Dumazet 	unsigned int i;
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds 	called_len  = (*p >> 0) & 0x0F;
1301da177e4SLinus Torvalds 	calling_len = (*p >> 4) & 0x0F;
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 	called  = called_addr->x25_addr;
1331da177e4SLinus Torvalds 	calling = calling_addr->x25_addr;
1341da177e4SLinus Torvalds 	p++;
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	for (i = 0; i < (called_len + calling_len); i++) {
1371da177e4SLinus Torvalds 		if (i < called_len) {
1381da177e4SLinus Torvalds 			if (i % 2 != 0) {
1391da177e4SLinus Torvalds 				*called++ = ((*p >> 0) & 0x0F) + '0';
1401da177e4SLinus Torvalds 				p++;
1411da177e4SLinus Torvalds 			} else {
1421da177e4SLinus Torvalds 				*called++ = ((*p >> 4) & 0x0F) + '0';
1431da177e4SLinus Torvalds 			}
1441da177e4SLinus Torvalds 		} else {
1451da177e4SLinus Torvalds 			if (i % 2 != 0) {
1461da177e4SLinus Torvalds 				*calling++ = ((*p >> 0) & 0x0F) + '0';
1471da177e4SLinus Torvalds 				p++;
1481da177e4SLinus Torvalds 			} else {
1491da177e4SLinus Torvalds 				*calling++ = ((*p >> 4) & 0x0F) + '0';
1501da177e4SLinus Torvalds 			}
1511da177e4SLinus Torvalds 		}
1521da177e4SLinus Torvalds 	}
1531da177e4SLinus Torvalds 
1541da177e4SLinus Torvalds 	*called = *calling = '\0';
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	return 1 + (called_len + calling_len + 1) / 2;
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds int x25_addr_aton(unsigned char *p, struct x25_address *called_addr,
1601da177e4SLinus Torvalds 		  struct x25_address *calling_addr)
1611da177e4SLinus Torvalds {
1621da177e4SLinus Torvalds 	unsigned int called_len, calling_len;
1631da177e4SLinus Torvalds 	char *called, *calling;
1641da177e4SLinus Torvalds 	int i;
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	called  = called_addr->x25_addr;
1671da177e4SLinus Torvalds 	calling = calling_addr->x25_addr;
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds 	called_len  = strlen(called);
1701da177e4SLinus Torvalds 	calling_len = strlen(calling);
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	*p++ = (calling_len << 4) | (called_len << 0);
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	for (i = 0; i < (called_len + calling_len); i++) {
1751da177e4SLinus Torvalds 		if (i < called_len) {
1761da177e4SLinus Torvalds 			if (i % 2 != 0) {
1771da177e4SLinus Torvalds 				*p |= (*called++ - '0') << 0;
1781da177e4SLinus Torvalds 				p++;
1791da177e4SLinus Torvalds 			} else {
1801da177e4SLinus Torvalds 				*p = 0x00;
1811da177e4SLinus Torvalds 				*p |= (*called++ - '0') << 4;
1821da177e4SLinus Torvalds 			}
1831da177e4SLinus Torvalds 		} else {
1841da177e4SLinus Torvalds 			if (i % 2 != 0) {
1851da177e4SLinus Torvalds 				*p |= (*calling++ - '0') << 0;
1861da177e4SLinus Torvalds 				p++;
1871da177e4SLinus Torvalds 			} else {
1881da177e4SLinus Torvalds 				*p = 0x00;
1891da177e4SLinus Torvalds 				*p |= (*calling++ - '0') << 4;
1901da177e4SLinus Torvalds 			}
1911da177e4SLinus Torvalds 		}
1921da177e4SLinus Torvalds 	}
1931da177e4SLinus Torvalds 
1941da177e4SLinus Torvalds 	return 1 + (called_len + calling_len + 1) / 2;
1951da177e4SLinus Torvalds }
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds /*
1981da177e4SLinus Torvalds  *	Socket removal during an interrupt is now safe.
1991da177e4SLinus Torvalds  */
2001da177e4SLinus Torvalds static void x25_remove_socket(struct sock *sk)
2011da177e4SLinus Torvalds {
2021da177e4SLinus Torvalds 	write_lock_bh(&x25_list_lock);
2031da177e4SLinus Torvalds 	sk_del_node_init(sk);
2041da177e4SLinus Torvalds 	write_unlock_bh(&x25_list_lock);
2051da177e4SLinus Torvalds }
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds /*
2081da177e4SLinus Torvalds  *	Kill all bound sockets on a dropped device.
2091da177e4SLinus Torvalds  */
2101da177e4SLinus Torvalds static void x25_kill_by_device(struct net_device *dev)
2111da177e4SLinus Torvalds {
2121da177e4SLinus Torvalds 	struct sock *s;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	write_lock_bh(&x25_list_lock);
2151da177e4SLinus Torvalds 
216b67bfe0dSSasha Levin 	sk_for_each(s, &x25_list)
2171da177e4SLinus Torvalds 		if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev)
2181da177e4SLinus Torvalds 			x25_disconnect(s, ENETUNREACH, 0, 0);
2191da177e4SLinus Torvalds 
2201da177e4SLinus Torvalds 	write_unlock_bh(&x25_list_lock);
2211da177e4SLinus Torvalds }
2221da177e4SLinus Torvalds 
2231da177e4SLinus Torvalds /*
2241da177e4SLinus Torvalds  *	Handle device status changes.
2251da177e4SLinus Torvalds  */
2261da177e4SLinus Torvalds static int x25_device_event(struct notifier_block *this, unsigned long event,
2271da177e4SLinus Torvalds 			    void *ptr)
2281da177e4SLinus Torvalds {
229351638e7SJiri Pirko 	struct net_device *dev = netdev_notifier_info_to_dev(ptr);
2301da177e4SLinus Torvalds 	struct x25_neigh *nb;
2311da177e4SLinus Torvalds 
232721499e8SYOSHIFUJI Hideaki 	if (!net_eq(dev_net(dev), &init_net))
233e9dc8653SEric W. Biederman 		return NOTIFY_DONE;
234e9dc8653SEric W. Biederman 
2351da177e4SLinus Torvalds 	if (dev->type == ARPHRD_X25
23629c36262SIgor Maravić #if IS_ENABLED(CONFIG_LLC)
2371da177e4SLinus Torvalds 	 || dev->type == ARPHRD_ETHER
2381da177e4SLinus Torvalds #endif
2391da177e4SLinus Torvalds 	 ) {
2401da177e4SLinus Torvalds 		switch (event) {
2411da177e4SLinus Torvalds 		case NETDEV_UP:
2421da177e4SLinus Torvalds 			x25_link_device_up(dev);
2431da177e4SLinus Torvalds 			break;
2441da177e4SLinus Torvalds 		case NETDEV_GOING_DOWN:
2451da177e4SLinus Torvalds 			nb = x25_get_neigh(dev);
2461da177e4SLinus Torvalds 			if (nb) {
2471da177e4SLinus Torvalds 				x25_terminate_link(nb);
2481da177e4SLinus Torvalds 				x25_neigh_put(nb);
2491da177e4SLinus Torvalds 			}
2501da177e4SLinus Torvalds 			break;
2511da177e4SLinus Torvalds 		case NETDEV_DOWN:
2521da177e4SLinus Torvalds 			x25_kill_by_device(dev);
2531da177e4SLinus Torvalds 			x25_route_device_down(dev);
2541da177e4SLinus Torvalds 			x25_link_device_down(dev);
2551da177e4SLinus Torvalds 			break;
2561da177e4SLinus Torvalds 		}
2571da177e4SLinus Torvalds 	}
2581da177e4SLinus Torvalds 
2591da177e4SLinus Torvalds 	return NOTIFY_DONE;
2601da177e4SLinus Torvalds }
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds /*
2631da177e4SLinus Torvalds  *	Add a socket to the bound sockets list.
2641da177e4SLinus Torvalds  */
2651da177e4SLinus Torvalds static void x25_insert_socket(struct sock *sk)
2661da177e4SLinus Torvalds {
2671da177e4SLinus Torvalds 	write_lock_bh(&x25_list_lock);
2681da177e4SLinus Torvalds 	sk_add_node(sk, &x25_list);
2691da177e4SLinus Torvalds 	write_unlock_bh(&x25_list_lock);
2701da177e4SLinus Torvalds }
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds /*
2731da177e4SLinus Torvalds  *	Find a socket that wants to accept the Call Request we just
2741da177e4SLinus Torvalds  *	received. Check the full list for an address/cud match.
2751da177e4SLinus Torvalds  *	If no cuds match return the next_best thing, an address match.
2761da177e4SLinus Torvalds  *	Note: if a listening socket has cud set it must only get calls
2771da177e4SLinus Torvalds  *	with matching cud.
2781da177e4SLinus Torvalds  */
279cb65d506SShaun Pereira static struct sock *x25_find_listener(struct x25_address *addr,
280cb65d506SShaun Pereira 					struct sk_buff *skb)
2811da177e4SLinus Torvalds {
2821da177e4SLinus Torvalds 	struct sock *s;
2831da177e4SLinus Torvalds 	struct sock *next_best;
2841da177e4SLinus Torvalds 
2851da177e4SLinus Torvalds 	read_lock_bh(&x25_list_lock);
2861da177e4SLinus Torvalds 	next_best = NULL;
2871da177e4SLinus Torvalds 
288b67bfe0dSSasha Levin 	sk_for_each(s, &x25_list)
2891da177e4SLinus Torvalds 		if ((!strcmp(addr->x25_addr,
2901da177e4SLinus Torvalds 			x25_sk(s)->source_addr.x25_addr) ||
2911da177e4SLinus Torvalds 				!strcmp(addr->x25_addr,
2921da177e4SLinus Torvalds 					null_x25_address.x25_addr)) &&
2931da177e4SLinus Torvalds 					s->sk_state == TCP_LISTEN) {
2941da177e4SLinus Torvalds 			/*
2951da177e4SLinus Torvalds 			 * Found a listening socket, now check the incoming
2961da177e4SLinus Torvalds 			 * call user data vs this sockets call user data
2971da177e4SLinus Torvalds 			 */
2987f81e25bSMatthew Daley 			if (x25_sk(s)->cudmatchlength > 0 &&
2997f81e25bSMatthew Daley 				skb->len >= x25_sk(s)->cudmatchlength) {
300cb65d506SShaun Pereira 				if((memcmp(x25_sk(s)->calluserdata.cuddata,
301cb65d506SShaun Pereira 					skb->data,
302cb65d506SShaun Pereira 					x25_sk(s)->cudmatchlength)) == 0) {
3031da177e4SLinus Torvalds 					sock_hold(s);
3041da177e4SLinus Torvalds 					goto found;
3051da177e4SLinus Torvalds 				 }
306cb65d506SShaun Pereira 			} else
3071da177e4SLinus Torvalds 				next_best = s;
3081da177e4SLinus Torvalds 		}
3091da177e4SLinus Torvalds 	if (next_best) {
3101da177e4SLinus Torvalds 		s = next_best;
3111da177e4SLinus Torvalds 		sock_hold(s);
3121da177e4SLinus Torvalds 		goto found;
3131da177e4SLinus Torvalds 	}
3141da177e4SLinus Torvalds 	s = NULL;
3151da177e4SLinus Torvalds found:
3161da177e4SLinus Torvalds 	read_unlock_bh(&x25_list_lock);
3171da177e4SLinus Torvalds 	return s;
3181da177e4SLinus Torvalds }
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds /*
3211da177e4SLinus Torvalds  *	Find a connected X.25 socket given my LCI and neighbour.
3221da177e4SLinus Torvalds  */
3231da177e4SLinus Torvalds static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
3241da177e4SLinus Torvalds {
3251da177e4SLinus Torvalds 	struct sock *s;
3261da177e4SLinus Torvalds 
327b67bfe0dSSasha Levin 	sk_for_each(s, &x25_list)
3281da177e4SLinus Torvalds 		if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
3291da177e4SLinus Torvalds 			sock_hold(s);
3301da177e4SLinus Torvalds 			goto found;
3311da177e4SLinus Torvalds 		}
3321da177e4SLinus Torvalds 	s = NULL;
3331da177e4SLinus Torvalds found:
3341da177e4SLinus Torvalds 	return s;
3351da177e4SLinus Torvalds }
3361da177e4SLinus Torvalds 
3371da177e4SLinus Torvalds struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
3381da177e4SLinus Torvalds {
3391da177e4SLinus Torvalds 	struct sock *s;
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	read_lock_bh(&x25_list_lock);
3421da177e4SLinus Torvalds 	s = __x25_find_socket(lci, nb);
3431da177e4SLinus Torvalds 	read_unlock_bh(&x25_list_lock);
3441da177e4SLinus Torvalds 	return s;
3451da177e4SLinus Torvalds }
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds /*
3481da177e4SLinus Torvalds  *	Find a unique LCI for a given device.
3491da177e4SLinus Torvalds  */
3501da177e4SLinus Torvalds static unsigned int x25_new_lci(struct x25_neigh *nb)
3511da177e4SLinus Torvalds {
3521da177e4SLinus Torvalds 	unsigned int lci = 1;
3531da177e4SLinus Torvalds 	struct sock *sk;
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	read_lock_bh(&x25_list_lock);
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	while ((sk = __x25_find_socket(lci, nb)) != NULL) {
3581da177e4SLinus Torvalds 		sock_put(sk);
3591da177e4SLinus Torvalds 		if (++lci == 4096) {
3601da177e4SLinus Torvalds 			lci = 0;
3611da177e4SLinus Torvalds 			break;
3621da177e4SLinus Torvalds 		}
3631da177e4SLinus Torvalds 	}
3641da177e4SLinus Torvalds 
3651da177e4SLinus Torvalds 	read_unlock_bh(&x25_list_lock);
3661da177e4SLinus Torvalds 	return lci;
3671da177e4SLinus Torvalds }
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds /*
3701da177e4SLinus Torvalds  *	Deferred destroy.
3711da177e4SLinus Torvalds  */
37214ebaf81SDavid S. Miller static void __x25_destroy_socket(struct sock *);
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds /*
3751da177e4SLinus Torvalds  *	handler for deferred kills.
3761da177e4SLinus Torvalds  */
37799767f27SKees Cook static void x25_destroy_timer(struct timer_list *t)
3781da177e4SLinus Torvalds {
37999767f27SKees Cook 	struct sock *sk = from_timer(sk, t, sk_timer);
38099767f27SKees Cook 
38199767f27SKees Cook 	x25_destroy_socket_from_timer(sk);
3821da177e4SLinus Torvalds }
3831da177e4SLinus Torvalds 
3841da177e4SLinus Torvalds /*
3851da177e4SLinus Torvalds  *	This is called from user mode and the timers. Thus it protects itself
3861da177e4SLinus Torvalds  *	against interrupt users but doesn't worry about being called during
3871da177e4SLinus Torvalds  *	work. Once it is removed from the queue no interrupt or bottom half
3881da177e4SLinus Torvalds  *	will touch it and we are (fairly 8-) ) safe.
3891da177e4SLinus Torvalds  *	Not static as it's used by the timer
3901da177e4SLinus Torvalds  */
39114ebaf81SDavid S. Miller static void __x25_destroy_socket(struct sock *sk)
3921da177e4SLinus Torvalds {
3931da177e4SLinus Torvalds 	struct sk_buff *skb;
3941da177e4SLinus Torvalds 
3951da177e4SLinus Torvalds 	x25_stop_heartbeat(sk);
3961da177e4SLinus Torvalds 	x25_stop_timer(sk);
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds 	x25_remove_socket(sk);
3991da177e4SLinus Torvalds 	x25_clear_queues(sk);		/* Flush the queues */
4001da177e4SLinus Torvalds 
4011da177e4SLinus Torvalds 	while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
4021da177e4SLinus Torvalds 		if (skb->sk != sk) {		/* A pending connection */
4031da177e4SLinus Torvalds 			/*
4041da177e4SLinus Torvalds 			 * Queue the unaccepted socket for death
4051da177e4SLinus Torvalds 			 */
4062cec6b01Sandrew hendry 			skb->sk->sk_state = TCP_LISTEN;
4071da177e4SLinus Torvalds 			sock_set_flag(skb->sk, SOCK_DEAD);
4081da177e4SLinus Torvalds 			x25_start_heartbeat(skb->sk);
4091da177e4SLinus Torvalds 			x25_sk(skb->sk)->state = X25_STATE_0;
4101da177e4SLinus Torvalds 		}
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 		kfree_skb(skb);
4131da177e4SLinus Torvalds 	}
4141da177e4SLinus Torvalds 
415c564039fSEric Dumazet 	if (sk_has_allocations(sk)) {
4161da177e4SLinus Torvalds 		/* Defer: outstanding buffers */
4171da177e4SLinus Torvalds 		sk->sk_timer.expires  = jiffies + 10 * HZ;
418841b86f3SKees Cook 		sk->sk_timer.function = x25_destroy_timer;
4191da177e4SLinus Torvalds 		add_timer(&sk->sk_timer);
4201da177e4SLinus Torvalds 	} else {
4211da177e4SLinus Torvalds 		/* drop last reference so sock_put will free */
4221da177e4SLinus Torvalds 		__sock_put(sk);
4231da177e4SLinus Torvalds 	}
42414ebaf81SDavid S. Miller }
4251da177e4SLinus Torvalds 
42614ebaf81SDavid S. Miller void x25_destroy_socket_from_timer(struct sock *sk)
42714ebaf81SDavid S. Miller {
42814ebaf81SDavid S. Miller 	sock_hold(sk);
42914ebaf81SDavid S. Miller 	bh_lock_sock(sk);
43014ebaf81SDavid S. Miller 	__x25_destroy_socket(sk);
43114ebaf81SDavid S. Miller 	bh_unlock_sock(sk);
43214ebaf81SDavid S. Miller 	sock_put(sk);
43314ebaf81SDavid S. Miller }
43414ebaf81SDavid S. Miller 
4351da177e4SLinus Torvalds /*
4361da177e4SLinus Torvalds  *	Handling for system calls applied via the various interfaces to a
4371da177e4SLinus Torvalds  *	X.25 socket object.
4381da177e4SLinus Torvalds  */
4391da177e4SLinus Torvalds 
4401da177e4SLinus Torvalds static int x25_setsockopt(struct socket *sock, int level, int optname,
441b7058842SDavid S. Miller 			  char __user *optval, unsigned int optlen)
4421da177e4SLinus Torvalds {
4431da177e4SLinus Torvalds 	int opt;
4441da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4451da177e4SLinus Torvalds 	int rc = -ENOPROTOOPT;
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	if (level != SOL_X25 || optname != X25_QBITINCL)
4481da177e4SLinus Torvalds 		goto out;
4491da177e4SLinus Torvalds 
4501da177e4SLinus Torvalds 	rc = -EINVAL;
4511da177e4SLinus Torvalds 	if (optlen < sizeof(int))
4521da177e4SLinus Torvalds 		goto out;
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 	rc = -EFAULT;
4551da177e4SLinus Torvalds 	if (get_user(opt, (int __user *)optval))
4561da177e4SLinus Torvalds 		goto out;
4571da177e4SLinus Torvalds 
458cb863ffdSandrew hendry 	if (opt)
459cb863ffdSandrew hendry 		set_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
460cb863ffdSandrew hendry 	else
461cb863ffdSandrew hendry 		clear_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
4621da177e4SLinus Torvalds 	rc = 0;
4631da177e4SLinus Torvalds out:
4641da177e4SLinus Torvalds 	return rc;
4651da177e4SLinus Torvalds }
4661da177e4SLinus Torvalds 
4671da177e4SLinus Torvalds static int x25_getsockopt(struct socket *sock, int level, int optname,
4681da177e4SLinus Torvalds 			  char __user *optval, int __user *optlen)
4691da177e4SLinus Torvalds {
4701da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4711da177e4SLinus Torvalds 	int val, len, rc = -ENOPROTOOPT;
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds 	if (level != SOL_X25 || optname != X25_QBITINCL)
4741da177e4SLinus Torvalds 		goto out;
4751da177e4SLinus Torvalds 
4761da177e4SLinus Torvalds 	rc = -EFAULT;
4771da177e4SLinus Torvalds 	if (get_user(len, optlen))
4781da177e4SLinus Torvalds 		goto out;
4791da177e4SLinus Torvalds 
4801da177e4SLinus Torvalds 	len = min_t(unsigned int, len, sizeof(int));
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 	rc = -EINVAL;
4831da177e4SLinus Torvalds 	if (len < 0)
4841da177e4SLinus Torvalds 		goto out;
4851da177e4SLinus Torvalds 
4861da177e4SLinus Torvalds 	rc = -EFAULT;
4871da177e4SLinus Torvalds 	if (put_user(len, optlen))
4881da177e4SLinus Torvalds 		goto out;
4891da177e4SLinus Torvalds 
490cb863ffdSandrew hendry 	val = test_bit(X25_Q_BIT_FLAG, &x25_sk(sk)->flags);
4911da177e4SLinus Torvalds 	rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
4921da177e4SLinus Torvalds out:
4931da177e4SLinus Torvalds 	return rc;
4941da177e4SLinus Torvalds }
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds static int x25_listen(struct socket *sock, int backlog)
4971da177e4SLinus Torvalds {
4981da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4991da177e4SLinus Torvalds 	int rc = -EOPNOTSUPP;
5001da177e4SLinus Torvalds 
50125aa4efeSandrew hendry 	lock_sock(sk);
5021da177e4SLinus Torvalds 	if (sk->sk_state != TCP_LISTEN) {
5031da177e4SLinus Torvalds 		memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
5041da177e4SLinus Torvalds 		sk->sk_max_ack_backlog = backlog;
5051da177e4SLinus Torvalds 		sk->sk_state           = TCP_LISTEN;
5061da177e4SLinus Torvalds 		rc = 0;
5071da177e4SLinus Torvalds 	}
50825aa4efeSandrew hendry 	release_sock(sk);
5091da177e4SLinus Torvalds 
5101da177e4SLinus Torvalds 	return rc;
5111da177e4SLinus Torvalds }
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds static struct proto x25_proto = {
5141da177e4SLinus Torvalds 	.name	  = "X25",
5151da177e4SLinus Torvalds 	.owner	  = THIS_MODULE,
5161da177e4SLinus Torvalds 	.obj_size = sizeof(struct x25_sock),
5171da177e4SLinus Torvalds };
5181da177e4SLinus Torvalds 
51911aa9c28SEric W. Biederman static struct sock *x25_alloc_socket(struct net *net, int kern)
5201da177e4SLinus Torvalds {
5211da177e4SLinus Torvalds 	struct x25_sock *x25;
52211aa9c28SEric W. Biederman 	struct sock *sk = sk_alloc(net, AF_X25, GFP_ATOMIC, &x25_proto, kern);
5231da177e4SLinus Torvalds 
5241da177e4SLinus Torvalds 	if (!sk)
5251da177e4SLinus Torvalds 		goto out;
5261da177e4SLinus Torvalds 
5271da177e4SLinus Torvalds 	sock_init_data(NULL, sk);
5281da177e4SLinus Torvalds 
5291da177e4SLinus Torvalds 	x25 = x25_sk(sk);
5301da177e4SLinus Torvalds 	skb_queue_head_init(&x25->ack_queue);
5311da177e4SLinus Torvalds 	skb_queue_head_init(&x25->fragment_queue);
5321da177e4SLinus Torvalds 	skb_queue_head_init(&x25->interrupt_in_queue);
5331da177e4SLinus Torvalds 	skb_queue_head_init(&x25->interrupt_out_queue);
5341da177e4SLinus Torvalds out:
5351da177e4SLinus Torvalds 	return sk;
5361da177e4SLinus Torvalds }
5371da177e4SLinus Torvalds 
5383f378b68SEric Paris static int x25_create(struct net *net, struct socket *sock, int protocol,
5393f378b68SEric Paris 		      int kern)
5401da177e4SLinus Torvalds {
5411da177e4SLinus Torvalds 	struct sock *sk;
5421da177e4SLinus Torvalds 	struct x25_sock *x25;
543b18e7a06Sandrew hendry 	int rc = -EAFNOSUPPORT;
5441da177e4SLinus Torvalds 
54509ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net))
5461da177e4SLinus Torvalds 		goto out;
5471da177e4SLinus Torvalds 
548b18e7a06Sandrew hendry 	rc = -ESOCKTNOSUPPORT;
549b18e7a06Sandrew hendry 	if (sock->type != SOCK_SEQPACKET)
550b18e7a06Sandrew hendry 		goto out;
551b18e7a06Sandrew hendry 
552b18e7a06Sandrew hendry 	rc = -EINVAL;
553b18e7a06Sandrew hendry 	if (protocol)
554b18e7a06Sandrew hendry 		goto out;
555b18e7a06Sandrew hendry 
556b18e7a06Sandrew hendry 	rc = -ENOBUFS;
55711aa9c28SEric W. Biederman 	if ((sk = x25_alloc_socket(net, kern)) == NULL)
5581da177e4SLinus Torvalds 		goto out;
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds 	x25 = x25_sk(sk);
5611da177e4SLinus Torvalds 
5621da177e4SLinus Torvalds 	sock_init_data(sock, sk);
5631da177e4SLinus Torvalds 
5641da177e4SLinus Torvalds 	x25_init_timers(sk);
5651da177e4SLinus Torvalds 
5661da177e4SLinus Torvalds 	sock->ops    = &x25_proto_ops;
5671da177e4SLinus Torvalds 	sk->sk_protocol = protocol;
5681da177e4SLinus Torvalds 	sk->sk_backlog_rcv = x25_backlog_rcv;
5691da177e4SLinus Torvalds 
5701da177e4SLinus Torvalds 	x25->t21   = sysctl_x25_call_request_timeout;
5711da177e4SLinus Torvalds 	x25->t22   = sysctl_x25_reset_request_timeout;
5721da177e4SLinus Torvalds 	x25->t23   = sysctl_x25_clear_request_timeout;
5731da177e4SLinus Torvalds 	x25->t2    = sysctl_x25_ack_holdback_timeout;
5741da177e4SLinus Torvalds 	x25->state = X25_STATE_0;
575cb65d506SShaun Pereira 	x25->cudmatchlength = 0;
57637cda787Sandrew hendry 	set_bit(X25_ACCPT_APPRV_FLAG, &x25->flags);	/* normally no cud  */
577ebc3f64bSShaun Pereira 							/* on call accept   */
5781da177e4SLinus Torvalds 
5791da177e4SLinus Torvalds 	x25->facilities.winsize_in  = X25_DEFAULT_WINDOW_SIZE;
5801da177e4SLinus Torvalds 	x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
5811da177e4SLinus Torvalds 	x25->facilities.pacsize_in  = X25_DEFAULT_PACKET_SIZE;
5821da177e4SLinus Torvalds 	x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
583ddd0451fSJohn Hughes 	x25->facilities.throughput  = 0;	/* by default don't negotiate
584ddd0451fSJohn Hughes 						   throughput */
5851da177e4SLinus Torvalds 	x25->facilities.reverse     = X25_DEFAULT_REVERSE;
586a64b7b93SShaun Pereira 	x25->dte_facilities.calling_len = 0;
587a64b7b93SShaun Pereira 	x25->dte_facilities.called_len = 0;
588a64b7b93SShaun Pereira 	memset(x25->dte_facilities.called_ae, '\0',
589a64b7b93SShaun Pereira 			sizeof(x25->dte_facilities.called_ae));
590a64b7b93SShaun Pereira 	memset(x25->dte_facilities.calling_ae, '\0',
591a64b7b93SShaun Pereira 			sizeof(x25->dte_facilities.calling_ae));
592a64b7b93SShaun Pereira 
5931da177e4SLinus Torvalds 	rc = 0;
5941da177e4SLinus Torvalds out:
5951da177e4SLinus Torvalds 	return rc;
5961da177e4SLinus Torvalds }
5971da177e4SLinus Torvalds 
5981da177e4SLinus Torvalds static struct sock *x25_make_new(struct sock *osk)
5991da177e4SLinus Torvalds {
6001da177e4SLinus Torvalds 	struct sock *sk = NULL;
6011da177e4SLinus Torvalds 	struct x25_sock *x25, *ox25;
6021da177e4SLinus Torvalds 
6031da177e4SLinus Torvalds 	if (osk->sk_type != SOCK_SEQPACKET)
6041da177e4SLinus Torvalds 		goto out;
6051da177e4SLinus Torvalds 
60611aa9c28SEric W. Biederman 	if ((sk = x25_alloc_socket(sock_net(osk), 0)) == NULL)
6071da177e4SLinus Torvalds 		goto out;
6081da177e4SLinus Torvalds 
6091da177e4SLinus Torvalds 	x25 = x25_sk(sk);
6101da177e4SLinus Torvalds 
6111da177e4SLinus Torvalds 	sk->sk_type        = osk->sk_type;
6121da177e4SLinus Torvalds 	sk->sk_priority    = osk->sk_priority;
6131da177e4SLinus Torvalds 	sk->sk_protocol    = osk->sk_protocol;
6141da177e4SLinus Torvalds 	sk->sk_rcvbuf      = osk->sk_rcvbuf;
6151da177e4SLinus Torvalds 	sk->sk_sndbuf      = osk->sk_sndbuf;
6161da177e4SLinus Torvalds 	sk->sk_state       = TCP_ESTABLISHED;
6171da177e4SLinus Torvalds 	sk->sk_backlog_rcv = osk->sk_backlog_rcv;
618a20a8554SShaun Pereira 	sock_copy_flags(sk, osk);
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 	ox25 = x25_sk(osk);
6211da177e4SLinus Torvalds 	x25->t21        = ox25->t21;
6221da177e4SLinus Torvalds 	x25->t22        = ox25->t22;
6231da177e4SLinus Torvalds 	x25->t23        = ox25->t23;
6241da177e4SLinus Torvalds 	x25->t2         = ox25->t2;
625cb863ffdSandrew hendry 	x25->flags	= ox25->flags;
6261da177e4SLinus Torvalds 	x25->facilities = ox25->facilities;
627a64b7b93SShaun Pereira 	x25->dte_facilities = ox25->dte_facilities;
628cb65d506SShaun Pereira 	x25->cudmatchlength = ox25->cudmatchlength;
6291da177e4SLinus Torvalds 
630b7792e34Sandrew hendry 	clear_bit(X25_INTERRUPT_FLAG, &x25->flags);
6311da177e4SLinus Torvalds 	x25_init_timers(sk);
6321da177e4SLinus Torvalds out:
6331da177e4SLinus Torvalds 	return sk;
6341da177e4SLinus Torvalds }
6351da177e4SLinus Torvalds 
6361da177e4SLinus Torvalds static int x25_release(struct socket *sock)
6371da177e4SLinus Torvalds {
6381da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
6391da177e4SLinus Torvalds 	struct x25_sock *x25;
6401da177e4SLinus Torvalds 
6411da177e4SLinus Torvalds 	if (!sk)
64277b22836SArnd Bergmann 		return 0;
6431da177e4SLinus Torvalds 
6441da177e4SLinus Torvalds 	x25 = x25_sk(sk);
6451da177e4SLinus Torvalds 
64677b22836SArnd Bergmann 	sock_hold(sk);
64777b22836SArnd Bergmann 	lock_sock(sk);
6481da177e4SLinus Torvalds 	switch (x25->state) {
6491da177e4SLinus Torvalds 
6501da177e4SLinus Torvalds 		case X25_STATE_0:
6511da177e4SLinus Torvalds 		case X25_STATE_2:
6521da177e4SLinus Torvalds 			x25_disconnect(sk, 0, 0, 0);
65377b22836SArnd Bergmann 			__x25_destroy_socket(sk);
6541da177e4SLinus Torvalds 			goto out;
6551da177e4SLinus Torvalds 
6561da177e4SLinus Torvalds 		case X25_STATE_1:
6571da177e4SLinus Torvalds 		case X25_STATE_3:
6581da177e4SLinus Torvalds 		case X25_STATE_4:
6591da177e4SLinus Torvalds 			x25_clear_queues(sk);
6601da177e4SLinus Torvalds 			x25_write_internal(sk, X25_CLEAR_REQUEST);
6611da177e4SLinus Torvalds 			x25_start_t23timer(sk);
6621da177e4SLinus Torvalds 			x25->state = X25_STATE_2;
6631da177e4SLinus Torvalds 			sk->sk_state	= TCP_CLOSE;
6641da177e4SLinus Torvalds 			sk->sk_shutdown	|= SEND_SHUTDOWN;
6651da177e4SLinus Torvalds 			sk->sk_state_change(sk);
6661da177e4SLinus Torvalds 			sock_set_flag(sk, SOCK_DEAD);
6671da177e4SLinus Torvalds 			sock_set_flag(sk, SOCK_DESTROY);
6681da177e4SLinus Torvalds 			break;
6691da177e4SLinus Torvalds 	}
6701da177e4SLinus Torvalds 
671c751e4f8SDavid S. Miller 	sock_orphan(sk);
6721da177e4SLinus Torvalds out:
67377b22836SArnd Bergmann 	release_sock(sk);
67477b22836SArnd Bergmann 	sock_put(sk);
6751da177e4SLinus Torvalds 	return 0;
6761da177e4SLinus Torvalds }
6771da177e4SLinus Torvalds 
6781da177e4SLinus Torvalds static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
6791da177e4SLinus Torvalds {
6801da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
6811da177e4SLinus Torvalds 	struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
682a9288525Sandrew hendry 	int len, i, rc = 0;
6831da177e4SLinus Torvalds 
6841da177e4SLinus Torvalds 	if (!sock_flag(sk, SOCK_ZAPPED) ||
6851da177e4SLinus Torvalds 	    addr_len != sizeof(struct sockaddr_x25) ||
68691774904SArnd Bergmann 	    addr->sx25_family != AF_X25) {
68791774904SArnd Bergmann 		rc = -EINVAL;
68891774904SArnd Bergmann 		goto out;
68991774904SArnd Bergmann 	}
6901da177e4SLinus Torvalds 
691a9288525Sandrew hendry 	len = strlen(addr->sx25_addr.x25_addr);
692a9288525Sandrew hendry 	for (i = 0; i < len; i++) {
693a9288525Sandrew hendry 		if (!isdigit(addr->sx25_addr.x25_addr[i])) {
694a9288525Sandrew hendry 			rc = -EINVAL;
695a9288525Sandrew hendry 			goto out;
696a9288525Sandrew hendry 		}
697a9288525Sandrew hendry 	}
698a9288525Sandrew hendry 
69990c27297Sandrew hendry 	lock_sock(sk);
7001da177e4SLinus Torvalds 	x25_sk(sk)->source_addr = addr->sx25_addr;
7011da177e4SLinus Torvalds 	x25_insert_socket(sk);
7021da177e4SLinus Torvalds 	sock_reset_flag(sk, SOCK_ZAPPED);
70390c27297Sandrew hendry 	release_sock(sk);
7041da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
70591774904SArnd Bergmann out:
70691774904SArnd Bergmann 	return rc;
7071da177e4SLinus Torvalds }
7081da177e4SLinus Torvalds 
7091da177e4SLinus Torvalds static int x25_wait_for_connection_establishment(struct sock *sk)
7101da177e4SLinus Torvalds {
7111da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
7121da177e4SLinus Torvalds 	int rc;
7131da177e4SLinus Torvalds 
714aa395145SEric Dumazet 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
7151da177e4SLinus Torvalds 	for (;;) {
7161da177e4SLinus Torvalds 		__set_current_state(TASK_INTERRUPTIBLE);
7171da177e4SLinus Torvalds 		rc = -ERESTARTSYS;
7181da177e4SLinus Torvalds 		if (signal_pending(current))
7191da177e4SLinus Torvalds 			break;
7201da177e4SLinus Torvalds 		rc = sock_error(sk);
7211da177e4SLinus Torvalds 		if (rc) {
7221da177e4SLinus Torvalds 			sk->sk_socket->state = SS_UNCONNECTED;
7231da177e4SLinus Torvalds 			break;
7241da177e4SLinus Torvalds 		}
7251da177e4SLinus Torvalds 		rc = 0;
7261da177e4SLinus Torvalds 		if (sk->sk_state != TCP_ESTABLISHED) {
7271da177e4SLinus Torvalds 			release_sock(sk);
7281da177e4SLinus Torvalds 			schedule();
7291da177e4SLinus Torvalds 			lock_sock(sk);
7301da177e4SLinus Torvalds 		} else
7311da177e4SLinus Torvalds 			break;
7321da177e4SLinus Torvalds 	}
7331da177e4SLinus Torvalds 	__set_current_state(TASK_RUNNING);
734aa395145SEric Dumazet 	remove_wait_queue(sk_sleep(sk), &wait);
7351da177e4SLinus Torvalds 	return rc;
7361da177e4SLinus Torvalds }
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
7391da177e4SLinus Torvalds 		       int addr_len, int flags)
7401da177e4SLinus Torvalds {
7411da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
7421da177e4SLinus Torvalds 	struct x25_sock *x25 = x25_sk(sk);
7431da177e4SLinus Torvalds 	struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
7441da177e4SLinus Torvalds 	struct x25_route *rt;
7451da177e4SLinus Torvalds 	int rc = 0;
7461da177e4SLinus Torvalds 
7471da177e4SLinus Torvalds 	lock_sock(sk);
7481da177e4SLinus Torvalds 	if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
7491da177e4SLinus Torvalds 		sock->state = SS_CONNECTED;
7501da177e4SLinus Torvalds 		goto out; /* Connect completed during a ERESTARTSYS event */
7511da177e4SLinus Torvalds 	}
7521da177e4SLinus Torvalds 
7531da177e4SLinus Torvalds 	rc = -ECONNREFUSED;
7541da177e4SLinus Torvalds 	if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
7551da177e4SLinus Torvalds 		sock->state = SS_UNCONNECTED;
7561da177e4SLinus Torvalds 		goto out;
7571da177e4SLinus Torvalds 	}
7581da177e4SLinus Torvalds 
7591da177e4SLinus Torvalds 	rc = -EISCONN;	/* No reconnect on a seqpacket socket */
7601da177e4SLinus Torvalds 	if (sk->sk_state == TCP_ESTABLISHED)
7611da177e4SLinus Torvalds 		goto out;
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	sk->sk_state   = TCP_CLOSE;
7641da177e4SLinus Torvalds 	sock->state = SS_UNCONNECTED;
7651da177e4SLinus Torvalds 
7661da177e4SLinus Torvalds 	rc = -EINVAL;
7671da177e4SLinus Torvalds 	if (addr_len != sizeof(struct sockaddr_x25) ||
7681da177e4SLinus Torvalds 	    addr->sx25_family != AF_X25)
7691da177e4SLinus Torvalds 		goto out;
7701da177e4SLinus Torvalds 
7711da177e4SLinus Torvalds 	rc = -ENETUNREACH;
7721da177e4SLinus Torvalds 	rt = x25_get_route(&addr->sx25_addr);
7731da177e4SLinus Torvalds 	if (!rt)
7741da177e4SLinus Torvalds 		goto out;
7751da177e4SLinus Torvalds 
7761da177e4SLinus Torvalds 	x25->neighbour = x25_get_neigh(rt->dev);
7771da177e4SLinus Torvalds 	if (!x25->neighbour)
7781da177e4SLinus Torvalds 		goto out_put_route;
7791da177e4SLinus Torvalds 
7801da177e4SLinus Torvalds 	x25_limit_facilities(&x25->facilities, x25->neighbour);
7811da177e4SLinus Torvalds 
7821da177e4SLinus Torvalds 	x25->lci = x25_new_lci(x25->neighbour);
7831da177e4SLinus Torvalds 	if (!x25->lci)
7841da177e4SLinus Torvalds 		goto out_put_neigh;
7851da177e4SLinus Torvalds 
7861da177e4SLinus Torvalds 	rc = -EINVAL;
7871da177e4SLinus Torvalds 	if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
7881da177e4SLinus Torvalds 		goto out_put_neigh;
7891da177e4SLinus Torvalds 
7901da177e4SLinus Torvalds 	if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
7911da177e4SLinus Torvalds 		memset(&x25->source_addr, '\0', X25_ADDR_LEN);
7921da177e4SLinus Torvalds 
7931da177e4SLinus Torvalds 	x25->dest_addr = addr->sx25_addr;
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 	/* Move to connecting socket, start sending Connect Requests */
7961da177e4SLinus Torvalds 	sock->state   = SS_CONNECTING;
7971da177e4SLinus Torvalds 	sk->sk_state  = TCP_SYN_SENT;
7981da177e4SLinus Torvalds 
7991da177e4SLinus Torvalds 	x25->state = X25_STATE_1;
8001da177e4SLinus Torvalds 
8011da177e4SLinus Torvalds 	x25_write_internal(sk, X25_CALL_REQUEST);
8021da177e4SLinus Torvalds 
8031da177e4SLinus Torvalds 	x25_start_heartbeat(sk);
8041da177e4SLinus Torvalds 	x25_start_t21timer(sk);
8051da177e4SLinus Torvalds 
8061da177e4SLinus Torvalds 	/* Now the loop */
8071da177e4SLinus Torvalds 	rc = -EINPROGRESS;
8081da177e4SLinus Torvalds 	if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
8091da177e4SLinus Torvalds 		goto out_put_neigh;
8101da177e4SLinus Torvalds 
8111da177e4SLinus Torvalds 	rc = x25_wait_for_connection_establishment(sk);
8121da177e4SLinus Torvalds 	if (rc)
8131da177e4SLinus Torvalds 		goto out_put_neigh;
8141da177e4SLinus Torvalds 
8151da177e4SLinus Torvalds 	sock->state = SS_CONNECTED;
8161da177e4SLinus Torvalds 	rc = 0;
8171da177e4SLinus Torvalds out_put_neigh:
8181da177e4SLinus Torvalds 	if (rc)
8191da177e4SLinus Torvalds 		x25_neigh_put(x25->neighbour);
8201da177e4SLinus Torvalds out_put_route:
8211da177e4SLinus Torvalds 	x25_route_put(rt);
8221da177e4SLinus Torvalds out:
8231da177e4SLinus Torvalds 	release_sock(sk);
8241da177e4SLinus Torvalds 	return rc;
8251da177e4SLinus Torvalds }
8261da177e4SLinus Torvalds 
827bac37ec8SShaun Pereira static int x25_wait_for_data(struct sock *sk, long timeout)
8281da177e4SLinus Torvalds {
8291da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
8301da177e4SLinus Torvalds 	int rc = 0;
8311da177e4SLinus Torvalds 
832aa395145SEric Dumazet 	add_wait_queue_exclusive(sk_sleep(sk), &wait);
8331da177e4SLinus Torvalds 	for (;;) {
8341da177e4SLinus Torvalds 		__set_current_state(TASK_INTERRUPTIBLE);
8351da177e4SLinus Torvalds 		if (sk->sk_shutdown & RCV_SHUTDOWN)
8361da177e4SLinus Torvalds 			break;
8371da177e4SLinus Torvalds 		rc = -ERESTARTSYS;
8381da177e4SLinus Torvalds 		if (signal_pending(current))
8391da177e4SLinus Torvalds 			break;
8401da177e4SLinus Torvalds 		rc = -EAGAIN;
8411da177e4SLinus Torvalds 		if (!timeout)
8421da177e4SLinus Torvalds 			break;
8431da177e4SLinus Torvalds 		rc = 0;
8441da177e4SLinus Torvalds 		if (skb_queue_empty(&sk->sk_receive_queue)) {
8451da177e4SLinus Torvalds 			release_sock(sk);
8461da177e4SLinus Torvalds 			timeout = schedule_timeout(timeout);
8471da177e4SLinus Torvalds 			lock_sock(sk);
8481da177e4SLinus Torvalds 		} else
8491da177e4SLinus Torvalds 			break;
8501da177e4SLinus Torvalds 	}
8511da177e4SLinus Torvalds 	__set_current_state(TASK_RUNNING);
852aa395145SEric Dumazet 	remove_wait_queue(sk_sleep(sk), &wait);
8531da177e4SLinus Torvalds 	return rc;
8541da177e4SLinus Torvalds }
8551da177e4SLinus Torvalds 
856cdfbabfbSDavid Howells static int x25_accept(struct socket *sock, struct socket *newsock, int flags,
857cdfbabfbSDavid Howells 		      bool kern)
8581da177e4SLinus Torvalds {
8591da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
8601da177e4SLinus Torvalds 	struct sock *newsk;
8611da177e4SLinus Torvalds 	struct sk_buff *skb;
8621da177e4SLinus Torvalds 	int rc = -EINVAL;
8631da177e4SLinus Torvalds 
864141646ceSAndrew Hendry 	if (!sk)
8651da177e4SLinus Torvalds 		goto out;
8661da177e4SLinus Torvalds 
8671da177e4SLinus Torvalds 	rc = -EOPNOTSUPP;
8681da177e4SLinus Torvalds 	if (sk->sk_type != SOCK_SEQPACKET)
8691da177e4SLinus Torvalds 		goto out;
8701da177e4SLinus Torvalds 
8711da177e4SLinus Torvalds 	lock_sock(sk);
872141646ceSAndrew Hendry 	rc = -EINVAL;
873141646ceSAndrew Hendry 	if (sk->sk_state != TCP_LISTEN)
874141646ceSAndrew Hendry 		goto out2;
875141646ceSAndrew Hendry 
8761da177e4SLinus Torvalds 	rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
8771da177e4SLinus Torvalds 	if (rc)
8781da177e4SLinus Torvalds 		goto out2;
8791da177e4SLinus Torvalds 	skb = skb_dequeue(&sk->sk_receive_queue);
8801da177e4SLinus Torvalds 	rc = -EINVAL;
8811da177e4SLinus Torvalds 	if (!skb->sk)
8821da177e4SLinus Torvalds 		goto out2;
8831da177e4SLinus Torvalds 	newsk		 = skb->sk;
884b61d38e0SDavid S. Miller 	sock_graft(newsk, newsock);
8851da177e4SLinus Torvalds 
8861da177e4SLinus Torvalds 	/* Now attach up the new socket */
8871da177e4SLinus Torvalds 	skb->sk = NULL;
8881da177e4SLinus Torvalds 	kfree_skb(skb);
8891da177e4SLinus Torvalds 	sk->sk_ack_backlog--;
8901da177e4SLinus Torvalds 	newsock->state = SS_CONNECTED;
8911da177e4SLinus Torvalds 	rc = 0;
8921da177e4SLinus Torvalds out2:
8931da177e4SLinus Torvalds 	release_sock(sk);
8941da177e4SLinus Torvalds out:
8951da177e4SLinus Torvalds 	return rc;
8961da177e4SLinus Torvalds }
8971da177e4SLinus Torvalds 
8981da177e4SLinus Torvalds static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
899*9b2c45d4SDenys Vlasenko 		       int peer)
9001da177e4SLinus Torvalds {
9011da177e4SLinus Torvalds 	struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
9021da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
9031da177e4SLinus Torvalds 	struct x25_sock *x25 = x25_sk(sk);
90491774904SArnd Bergmann 	int rc = 0;
9051da177e4SLinus Torvalds 
9061da177e4SLinus Torvalds 	if (peer) {
90791774904SArnd Bergmann 		if (sk->sk_state != TCP_ESTABLISHED) {
90891774904SArnd Bergmann 			rc = -ENOTCONN;
90991774904SArnd Bergmann 			goto out;
91091774904SArnd Bergmann 		}
9111da177e4SLinus Torvalds 		sx25->sx25_addr = x25->dest_addr;
9121da177e4SLinus Torvalds 	} else
9131da177e4SLinus Torvalds 		sx25->sx25_addr = x25->source_addr;
9141da177e4SLinus Torvalds 
9151da177e4SLinus Torvalds 	sx25->sx25_family = AF_X25;
916*9b2c45d4SDenys Vlasenko 	rc = sizeof(*sx25);
9171da177e4SLinus Torvalds 
91891774904SArnd Bergmann out:
91991774904SArnd Bergmann 	return rc;
92091774904SArnd Bergmann }
92191774904SArnd Bergmann 
9221da177e4SLinus Torvalds int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
9231da177e4SLinus Torvalds 			unsigned int lci)
9241da177e4SLinus Torvalds {
9251da177e4SLinus Torvalds 	struct sock *sk;
9261da177e4SLinus Torvalds 	struct sock *make;
9271da177e4SLinus Torvalds 	struct x25_sock *makex25;
9281da177e4SLinus Torvalds 	struct x25_address source_addr, dest_addr;
9291da177e4SLinus Torvalds 	struct x25_facilities facilities;
930a64b7b93SShaun Pereira 	struct x25_dte_facilities dte_facilities;
93195a9dc43SAndrew Hendry 	int len, addr_len, rc;
9321da177e4SLinus Torvalds 
9331da177e4SLinus Torvalds 	/*
9341da177e4SLinus Torvalds 	 *	Remove the LCI and frame type.
9351da177e4SLinus Torvalds 	 */
9361da177e4SLinus Torvalds 	skb_pull(skb, X25_STD_MIN_LEN);
9371da177e4SLinus Torvalds 
9381da177e4SLinus Torvalds 	/*
9391da177e4SLinus Torvalds 	 *	Extract the X.25 addresses and convert them to ASCII strings,
9401da177e4SLinus Torvalds 	 *	and remove them.
941f5eb917bSJohn Hughes 	 *
942f5eb917bSJohn Hughes 	 *	Address block is mandatory in call request packets
9431da177e4SLinus Torvalds 	 */
944f5eb917bSJohn Hughes 	addr_len = x25_parse_address_block(skb, &source_addr, &dest_addr);
945f5eb917bSJohn Hughes 	if (addr_len <= 0)
946f5eb917bSJohn Hughes 		goto out_clear_request;
94795a9dc43SAndrew Hendry 	skb_pull(skb, addr_len);
9481da177e4SLinus Torvalds 
9491da177e4SLinus Torvalds 	/*
9501da177e4SLinus Torvalds 	 *	Get the length of the facilities, skip past them for the moment
9511da177e4SLinus Torvalds 	 *	get the call user data because this is needed to determine
9521da177e4SLinus Torvalds 	 *	the correct listener
953f5eb917bSJohn Hughes 	 *
954f5eb917bSJohn Hughes 	 *	Facilities length is mandatory in call request packets
9551da177e4SLinus Torvalds 	 */
956cb101ed2SMatthew Daley 	if (!pskb_may_pull(skb, 1))
957f5eb917bSJohn Hughes 		goto out_clear_request;
9581da177e4SLinus Torvalds 	len = skb->data[0] + 1;
959cb101ed2SMatthew Daley 	if (!pskb_may_pull(skb, len))
960f5eb917bSJohn Hughes 		goto out_clear_request;
9611da177e4SLinus Torvalds 	skb_pull(skb,len);
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	/*
964c7fd0d48SMatthew Daley 	 *	Ensure that the amount of call user data is valid.
965c7fd0d48SMatthew Daley 	 */
966c7fd0d48SMatthew Daley 	if (skb->len > X25_MAX_CUD_LEN)
967c7fd0d48SMatthew Daley 		goto out_clear_request;
968c7fd0d48SMatthew Daley 
969c7fd0d48SMatthew Daley 	/*
970cb101ed2SMatthew Daley 	 *	Get all the call user data so it can be used in
971cb101ed2SMatthew Daley 	 *	x25_find_listener and skb_copy_from_linear_data up ahead.
972cb101ed2SMatthew Daley 	 */
973cb101ed2SMatthew Daley 	if (!pskb_may_pull(skb, skb->len))
974cb101ed2SMatthew Daley 		goto out_clear_request;
975cb101ed2SMatthew Daley 
976cb101ed2SMatthew Daley 	/*
9771da177e4SLinus Torvalds 	 *	Find a listener for the particular address/cud pair.
9781da177e4SLinus Torvalds 	 */
979cb65d506SShaun Pereira 	sk = x25_find_listener(&source_addr,skb);
980cb65d506SShaun Pereira 	skb_push(skb,len);
9811da177e4SLinus Torvalds 
98295a9dc43SAndrew Hendry 	if (sk != NULL && sk_acceptq_is_full(sk)) {
98395a9dc43SAndrew Hendry 		goto out_sock_put;
98495a9dc43SAndrew Hendry 	}
98595a9dc43SAndrew Hendry 
9861da177e4SLinus Torvalds 	/*
98795a9dc43SAndrew Hendry 	 *	We dont have any listeners for this incoming call.
98895a9dc43SAndrew Hendry 	 *	Try forwarding it.
9891da177e4SLinus Torvalds 	 */
99095a9dc43SAndrew Hendry 	if (sk == NULL) {
99195a9dc43SAndrew Hendry 		skb_push(skb, addr_len + X25_STD_MIN_LEN);
99239e21c0dSAndrew Hendry 		if (sysctl_x25_forward &&
99339e21c0dSAndrew Hendry 				x25_forward_call(&dest_addr, nb, skb, lci) > 0)
99495a9dc43SAndrew Hendry 		{
99595a9dc43SAndrew Hendry 			/* Call was forwarded, dont process it any more */
99695a9dc43SAndrew Hendry 			kfree_skb(skb);
99795a9dc43SAndrew Hendry 			rc = 1;
99895a9dc43SAndrew Hendry 			goto out;
99995a9dc43SAndrew Hendry 		} else {
100095a9dc43SAndrew Hendry 			/* No listeners, can't forward, clear the call */
10011da177e4SLinus Torvalds 			goto out_clear_request;
100295a9dc43SAndrew Hendry 		}
100395a9dc43SAndrew Hendry 	}
10041da177e4SLinus Torvalds 
10051da177e4SLinus Torvalds 	/*
10061da177e4SLinus Torvalds 	 *	Try to reach a compromise on the requested facilities.
10071da177e4SLinus Torvalds 	 */
1008a64b7b93SShaun Pereira 	len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities);
1009a64b7b93SShaun Pereira 	if (len == -1)
10101da177e4SLinus Torvalds 		goto out_sock_put;
10111da177e4SLinus Torvalds 
10121da177e4SLinus Torvalds 	/*
10131da177e4SLinus Torvalds 	 * current neighbour/link might impose additional limits
10141da177e4SLinus Torvalds 	 * on certain facilties
10151da177e4SLinus Torvalds 	 */
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	x25_limit_facilities(&facilities, nb);
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds 	/*
10201da177e4SLinus Torvalds 	 *	Try to create a new socket.
10211da177e4SLinus Torvalds 	 */
10221da177e4SLinus Torvalds 	make = x25_make_new(sk);
10231da177e4SLinus Torvalds 	if (!make)
10241da177e4SLinus Torvalds 		goto out_sock_put;
10251da177e4SLinus Torvalds 
10261da177e4SLinus Torvalds 	/*
10271da177e4SLinus Torvalds 	 *	Remove the facilities
10281da177e4SLinus Torvalds 	 */
10291da177e4SLinus Torvalds 	skb_pull(skb, len);
10301da177e4SLinus Torvalds 
10311da177e4SLinus Torvalds 	skb->sk     = make;
10321da177e4SLinus Torvalds 	make->sk_state = TCP_ESTABLISHED;
10331da177e4SLinus Torvalds 
10341da177e4SLinus Torvalds 	makex25 = x25_sk(make);
10351da177e4SLinus Torvalds 	makex25->lci           = lci;
10361da177e4SLinus Torvalds 	makex25->dest_addr     = dest_addr;
10371da177e4SLinus Torvalds 	makex25->source_addr   = source_addr;
10381da177e4SLinus Torvalds 	makex25->neighbour     = nb;
10391da177e4SLinus Torvalds 	makex25->facilities    = facilities;
1040a64b7b93SShaun Pereira 	makex25->dte_facilities= dte_facilities;
10411da177e4SLinus Torvalds 	makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
1042cb65d506SShaun Pereira 	/* ensure no reverse facil on accept */
1043cb65d506SShaun Pereira 	makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
1044a64b7b93SShaun Pereira 	/* ensure no calling address extension on accept */
1045a64b7b93SShaun Pereira 	makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE;
1046cb65d506SShaun Pereira 	makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
10471da177e4SLinus Torvalds 
104837cda787Sandrew hendry 	/* Normally all calls are accepted immediately */
104937cda787Sandrew hendry 	if (test_bit(X25_ACCPT_APPRV_FLAG, &makex25->flags)) {
10501da177e4SLinus Torvalds 		x25_write_internal(make, X25_CALL_ACCEPTED);
10511da177e4SLinus Torvalds 		makex25->state = X25_STATE_3;
1052ebc3f64bSShaun Pereira 	}
10531da177e4SLinus Torvalds 
1054cb65d506SShaun Pereira 	/*
1055cb65d506SShaun Pereira 	 *	Incoming Call User Data.
1056cb65d506SShaun Pereira 	 */
1057d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, makex25->calluserdata.cuddata, skb->len);
1058cb65d506SShaun Pereira 	makex25->calluserdata.cudlength = skb->len;
1059cb65d506SShaun Pereira 
10601da177e4SLinus Torvalds 	sk->sk_ack_backlog++;
10611da177e4SLinus Torvalds 
10621da177e4SLinus Torvalds 	x25_insert_socket(make);
10631da177e4SLinus Torvalds 
10641da177e4SLinus Torvalds 	skb_queue_head(&sk->sk_receive_queue, skb);
10651da177e4SLinus Torvalds 
10661da177e4SLinus Torvalds 	x25_start_heartbeat(make);
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds 	if (!sock_flag(sk, SOCK_DEAD))
1069676d2369SDavid S. Miller 		sk->sk_data_ready(sk);
10701da177e4SLinus Torvalds 	rc = 1;
10711da177e4SLinus Torvalds 	sock_put(sk);
10721da177e4SLinus Torvalds out:
10731da177e4SLinus Torvalds 	return rc;
10741da177e4SLinus Torvalds out_sock_put:
10751da177e4SLinus Torvalds 	sock_put(sk);
10761da177e4SLinus Torvalds out_clear_request:
10771da177e4SLinus Torvalds 	rc = 0;
10781da177e4SLinus Torvalds 	x25_transmit_clear_request(nb, lci, 0x01);
10791da177e4SLinus Torvalds 	goto out;
10801da177e4SLinus Torvalds }
10811da177e4SLinus Torvalds 
10821b784140SYing Xue static int x25_sendmsg(struct socket *sock, struct msghdr *msg, size_t len)
10831da177e4SLinus Torvalds {
10841da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
10851da177e4SLinus Torvalds 	struct x25_sock *x25 = x25_sk(sk);
1086342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_x25 *, usx25, msg->msg_name);
10871da177e4SLinus Torvalds 	struct sockaddr_x25 sx25;
10881da177e4SLinus Torvalds 	struct sk_buff *skb;
10891da177e4SLinus Torvalds 	unsigned char *asmptr;
10901da177e4SLinus Torvalds 	int noblock = msg->msg_flags & MSG_DONTWAIT;
10911da177e4SLinus Torvalds 	size_t size;
10921da177e4SLinus Torvalds 	int qbit = 0, rc = -EINVAL;
10931da177e4SLinus Torvalds 
109477b22836SArnd Bergmann 	lock_sock(sk);
10951da177e4SLinus Torvalds 	if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
10961da177e4SLinus Torvalds 		goto out;
10971da177e4SLinus Torvalds 
10981da177e4SLinus Torvalds 	/* we currently don't support segmented records at the user interface */
10991da177e4SLinus Torvalds 	if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
11001da177e4SLinus Torvalds 		goto out;
11011da177e4SLinus Torvalds 
11021da177e4SLinus Torvalds 	rc = -EADDRNOTAVAIL;
11031da177e4SLinus Torvalds 	if (sock_flag(sk, SOCK_ZAPPED))
11041da177e4SLinus Torvalds 		goto out;
11051da177e4SLinus Torvalds 
11061da177e4SLinus Torvalds 	rc = -EPIPE;
11071da177e4SLinus Torvalds 	if (sk->sk_shutdown & SEND_SHUTDOWN) {
11081da177e4SLinus Torvalds 		send_sig(SIGPIPE, current, 0);
11091da177e4SLinus Torvalds 		goto out;
11101da177e4SLinus Torvalds 	}
11111da177e4SLinus Torvalds 
11121da177e4SLinus Torvalds 	rc = -ENETUNREACH;
11131da177e4SLinus Torvalds 	if (!x25->neighbour)
11141da177e4SLinus Torvalds 		goto out;
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 	if (usx25) {
11171da177e4SLinus Torvalds 		rc = -EINVAL;
11181da177e4SLinus Torvalds 		if (msg->msg_namelen < sizeof(sx25))
11191da177e4SLinus Torvalds 			goto out;
11201da177e4SLinus Torvalds 		memcpy(&sx25, usx25, sizeof(sx25));
11211da177e4SLinus Torvalds 		rc = -EISCONN;
11221da177e4SLinus Torvalds 		if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
11231da177e4SLinus Torvalds 			goto out;
11241da177e4SLinus Torvalds 		rc = -EINVAL;
11251da177e4SLinus Torvalds 		if (sx25.sx25_family != AF_X25)
11261da177e4SLinus Torvalds 			goto out;
11271da177e4SLinus Torvalds 	} else {
11281da177e4SLinus Torvalds 		/*
11291da177e4SLinus Torvalds 		 *	FIXME 1003.1g - if the socket is like this because
11301da177e4SLinus Torvalds 		 *	it has become closed (not started closed) we ought
11311da177e4SLinus Torvalds 		 *	to SIGPIPE, EPIPE;
11321da177e4SLinus Torvalds 		 */
11331da177e4SLinus Torvalds 		rc = -ENOTCONN;
11341da177e4SLinus Torvalds 		if (sk->sk_state != TCP_ESTABLISHED)
11351da177e4SLinus Torvalds 			goto out;
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 		sx25.sx25_family = AF_X25;
11381da177e4SLinus Torvalds 		sx25.sx25_addr   = x25->dest_addr;
11391da177e4SLinus Torvalds 	}
11401da177e4SLinus Torvalds 
114183e0bbcbSAlan Cox 	/* Sanity check the packet size */
114283e0bbcbSAlan Cox 	if (len > 65535) {
114383e0bbcbSAlan Cox 		rc = -EMSGSIZE;
114483e0bbcbSAlan Cox 		goto out;
114583e0bbcbSAlan Cox 	}
114683e0bbcbSAlan Cox 
11471da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
11481da177e4SLinus Torvalds 
11491da177e4SLinus Torvalds 	/* Build a packet */
11501da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
11511da177e4SLinus Torvalds 
11521da177e4SLinus Torvalds 	if ((msg->msg_flags & MSG_OOB) && len > 32)
11531da177e4SLinus Torvalds 		len = 32;
11541da177e4SLinus Torvalds 
11551da177e4SLinus Torvalds 	size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
11561da177e4SLinus Torvalds 
115777b22836SArnd Bergmann 	release_sock(sk);
11581da177e4SLinus Torvalds 	skb = sock_alloc_send_skb(sk, size, noblock, &rc);
115977b22836SArnd Bergmann 	lock_sock(sk);
11601da177e4SLinus Torvalds 	if (!skb)
11611da177e4SLinus Torvalds 		goto out;
11621da177e4SLinus Torvalds 	X25_SKB_CB(skb)->flags = msg->msg_flags;
11631da177e4SLinus Torvalds 
11641da177e4SLinus Torvalds 	skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
11651da177e4SLinus Torvalds 
11661da177e4SLinus Torvalds 	/*
11671da177e4SLinus Torvalds 	 *	Put the data on the end
11681da177e4SLinus Torvalds 	 */
11691da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
11701da177e4SLinus Torvalds 
1171eeeb0374SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
1172eeeb0374SArnaldo Carvalho de Melo 	skb_put(skb, len);
11731da177e4SLinus Torvalds 
11746ce8e9ceSAl Viro 	rc = memcpy_from_msg(skb_transport_header(skb), msg, len);
11751da177e4SLinus Torvalds 	if (rc)
11761da177e4SLinus Torvalds 		goto out_kfree_skb;
11771da177e4SLinus Torvalds 
11781da177e4SLinus Torvalds 	/*
11791da177e4SLinus Torvalds 	 *	If the Q BIT Include socket option is in force, the first
11801da177e4SLinus Torvalds 	 *	byte of the user data is the logical value of the Q Bit.
11811da177e4SLinus Torvalds 	 */
1182cb863ffdSandrew hendry 	if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
1183cb101ed2SMatthew Daley 		if (!pskb_may_pull(skb, 1))
1184cb101ed2SMatthew Daley 			goto out_kfree_skb;
1185cb101ed2SMatthew Daley 
11861da177e4SLinus Torvalds 		qbit = skb->data[0];
11871da177e4SLinus Torvalds 		skb_pull(skb, 1);
11881da177e4SLinus Torvalds 	}
11891da177e4SLinus Torvalds 
11901da177e4SLinus Torvalds 	/*
11911da177e4SLinus Torvalds 	 *	Push down the X.25 header
11921da177e4SLinus Torvalds 	 */
11931da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
11941da177e4SLinus Torvalds 
11951da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_OOB) {
11961da177e4SLinus Torvalds 		if (x25->neighbour->extended) {
11971da177e4SLinus Torvalds 			asmptr    = skb_push(skb, X25_STD_MIN_LEN);
11981da177e4SLinus Torvalds 			*asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
11991da177e4SLinus Torvalds 			*asmptr++ = (x25->lci >> 0) & 0xFF;
12001da177e4SLinus Torvalds 			*asmptr++ = X25_INTERRUPT;
12011da177e4SLinus Torvalds 		} else {
12021da177e4SLinus Torvalds 			asmptr    = skb_push(skb, X25_STD_MIN_LEN);
12031da177e4SLinus Torvalds 			*asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
12041da177e4SLinus Torvalds 			*asmptr++ = (x25->lci >> 0) & 0xFF;
12051da177e4SLinus Torvalds 			*asmptr++ = X25_INTERRUPT;
12061da177e4SLinus Torvalds 		}
12071da177e4SLinus Torvalds 	} else {
12081da177e4SLinus Torvalds 		if (x25->neighbour->extended) {
12091da177e4SLinus Torvalds 			/* Build an Extended X.25 header */
12101da177e4SLinus Torvalds 			asmptr    = skb_push(skb, X25_EXT_MIN_LEN);
12111da177e4SLinus Torvalds 			*asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
12121da177e4SLinus Torvalds 			*asmptr++ = (x25->lci >> 0) & 0xFF;
12131da177e4SLinus Torvalds 			*asmptr++ = X25_DATA;
12141da177e4SLinus Torvalds 			*asmptr++ = X25_DATA;
12151da177e4SLinus Torvalds 		} else {
12161da177e4SLinus Torvalds 			/* Build an Standard X.25 header */
12171da177e4SLinus Torvalds 			asmptr    = skb_push(skb, X25_STD_MIN_LEN);
12181da177e4SLinus Torvalds 			*asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
12191da177e4SLinus Torvalds 			*asmptr++ = (x25->lci >> 0) & 0xFF;
12201da177e4SLinus Torvalds 			*asmptr++ = X25_DATA;
12211da177e4SLinus Torvalds 		}
12221da177e4SLinus Torvalds 
12231da177e4SLinus Torvalds 		if (qbit)
12241da177e4SLinus Torvalds 			skb->data[0] |= X25_Q_BIT;
12251da177e4SLinus Torvalds 	}
12261da177e4SLinus Torvalds 
12271da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
12281da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
12291da177e4SLinus Torvalds 
12301da177e4SLinus Torvalds 	rc = -ENOTCONN;
12311da177e4SLinus Torvalds 	if (sk->sk_state != TCP_ESTABLISHED)
12321da177e4SLinus Torvalds 		goto out_kfree_skb;
12331da177e4SLinus Torvalds 
12341da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_OOB)
12351da177e4SLinus Torvalds 		skb_queue_tail(&x25->interrupt_out_queue, skb);
12361da177e4SLinus Torvalds 	else {
12378db09f26SRoel Kluin 		rc = x25_output(sk, skb);
12388db09f26SRoel Kluin 		len = rc;
12398db09f26SRoel Kluin 		if (rc < 0)
12401da177e4SLinus Torvalds 			kfree_skb(skb);
1241cb863ffdSandrew hendry 		else if (test_bit(X25_Q_BIT_FLAG, &x25->flags))
12421da177e4SLinus Torvalds 			len++;
12431da177e4SLinus Torvalds 	}
12441da177e4SLinus Torvalds 
12451da177e4SLinus Torvalds 	x25_kick(sk);
12461da177e4SLinus Torvalds 	rc = len;
12471da177e4SLinus Torvalds out:
124877b22836SArnd Bergmann 	release_sock(sk);
12491da177e4SLinus Torvalds 	return rc;
12501da177e4SLinus Torvalds out_kfree_skb:
12511da177e4SLinus Torvalds 	kfree_skb(skb);
12521da177e4SLinus Torvalds 	goto out;
12531da177e4SLinus Torvalds }
12541da177e4SLinus Torvalds 
12551da177e4SLinus Torvalds 
12561b784140SYing Xue static int x25_recvmsg(struct socket *sock, struct msghdr *msg, size_t size,
12571da177e4SLinus Torvalds 		       int flags)
12581da177e4SLinus Torvalds {
12591da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
12601da177e4SLinus Torvalds 	struct x25_sock *x25 = x25_sk(sk);
1261342dfc30SSteffen Hurrle 	DECLARE_SOCKADDR(struct sockaddr_x25 *, sx25, msg->msg_name);
12621da177e4SLinus Torvalds 	size_t copied;
1263501e89d3SDave Jones 	int qbit, header_len;
12641da177e4SLinus Torvalds 	struct sk_buff *skb;
12651da177e4SLinus Torvalds 	unsigned char *asmptr;
12661da177e4SLinus Torvalds 	int rc = -ENOTCONN;
12671da177e4SLinus Torvalds 
126877b22836SArnd Bergmann 	lock_sock(sk);
1269501e89d3SDave Jones 
1270501e89d3SDave Jones 	if (x25->neighbour == NULL)
1271501e89d3SDave Jones 		goto out;
1272501e89d3SDave Jones 
1273501e89d3SDave Jones 	header_len = x25->neighbour->extended ?
1274501e89d3SDave Jones 		X25_EXT_MIN_LEN : X25_STD_MIN_LEN;
1275501e89d3SDave Jones 
12761da177e4SLinus Torvalds 	/*
12771da177e4SLinus Torvalds 	 * This works for seqpacket too. The receiver has ordered the queue for
12781da177e4SLinus Torvalds 	 * us! We do one quick check first though
12791da177e4SLinus Torvalds 	 */
12801da177e4SLinus Torvalds 	if (sk->sk_state != TCP_ESTABLISHED)
12811da177e4SLinus Torvalds 		goto out;
12821da177e4SLinus Torvalds 
12831da177e4SLinus Torvalds 	if (flags & MSG_OOB) {
12841da177e4SLinus Torvalds 		rc = -EINVAL;
12851da177e4SLinus Torvalds 		if (sock_flag(sk, SOCK_URGINLINE) ||
12861da177e4SLinus Torvalds 		    !skb_peek(&x25->interrupt_in_queue))
12871da177e4SLinus Torvalds 			goto out;
12881da177e4SLinus Torvalds 
12891da177e4SLinus Torvalds 		skb = skb_dequeue(&x25->interrupt_in_queue);
12901da177e4SLinus Torvalds 
1291cb101ed2SMatthew Daley 		if (!pskb_may_pull(skb, X25_STD_MIN_LEN))
1292cb101ed2SMatthew Daley 			goto out_free_dgram;
1293cb101ed2SMatthew Daley 
12941da177e4SLinus Torvalds 		skb_pull(skb, X25_STD_MIN_LEN);
12951da177e4SLinus Torvalds 
12961da177e4SLinus Torvalds 		/*
12971da177e4SLinus Torvalds 		 *	No Q bit information on Interrupt data.
12981da177e4SLinus Torvalds 		 */
1299cb863ffdSandrew hendry 		if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
13001da177e4SLinus Torvalds 			asmptr  = skb_push(skb, 1);
13011da177e4SLinus Torvalds 			*asmptr = 0x00;
13021da177e4SLinus Torvalds 		}
13031da177e4SLinus Torvalds 
13041da177e4SLinus Torvalds 		msg->msg_flags |= MSG_OOB;
13051da177e4SLinus Torvalds 	} else {
13061da177e4SLinus Torvalds 		/* Now we can treat all alike */
130777b22836SArnd Bergmann 		release_sock(sk);
13081da177e4SLinus Torvalds 		skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
13091da177e4SLinus Torvalds 					flags & MSG_DONTWAIT, &rc);
131077b22836SArnd Bergmann 		lock_sock(sk);
13111da177e4SLinus Torvalds 		if (!skb)
13121da177e4SLinus Torvalds 			goto out;
13131da177e4SLinus Torvalds 
1314cb101ed2SMatthew Daley 		if (!pskb_may_pull(skb, header_len))
1315cb101ed2SMatthew Daley 			goto out_free_dgram;
1316cb101ed2SMatthew Daley 
13171da177e4SLinus Torvalds 		qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
13181da177e4SLinus Torvalds 
1319cb101ed2SMatthew Daley 		skb_pull(skb, header_len);
13201da177e4SLinus Torvalds 
1321cb863ffdSandrew hendry 		if (test_bit(X25_Q_BIT_FLAG, &x25->flags)) {
13221da177e4SLinus Torvalds 			asmptr  = skb_push(skb, 1);
13231da177e4SLinus Torvalds 			*asmptr = qbit;
13241da177e4SLinus Torvalds 		}
13251da177e4SLinus Torvalds 	}
13261da177e4SLinus Torvalds 
1327badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
13281da177e4SLinus Torvalds 	copied = skb->len;
13291da177e4SLinus Torvalds 
13301da177e4SLinus Torvalds 	if (copied > size) {
13311da177e4SLinus Torvalds 		copied = size;
13321da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
13331da177e4SLinus Torvalds 	}
13341da177e4SLinus Torvalds 
13351da177e4SLinus Torvalds 	/* Currently, each datagram always contains a complete record */
13361da177e4SLinus Torvalds 	msg->msg_flags |= MSG_EOR;
13371da177e4SLinus Torvalds 
133851f3d02bSDavid S. Miller 	rc = skb_copy_datagram_msg(skb, 0, msg, copied);
13391da177e4SLinus Torvalds 	if (rc)
13401da177e4SLinus Torvalds 		goto out_free_dgram;
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 	if (sx25) {
13431da177e4SLinus Torvalds 		sx25->sx25_family = AF_X25;
13441da177e4SLinus Torvalds 		sx25->sx25_addr   = x25->dest_addr;
1345f3d33426SHannes Frederic Sowa 		msg->msg_namelen = sizeof(*sx25);
13461da177e4SLinus Torvalds 	}
13471da177e4SLinus Torvalds 
13481da177e4SLinus Torvalds 	x25_check_rbuf(sk);
13491da177e4SLinus Torvalds 	rc = copied;
13501da177e4SLinus Torvalds out_free_dgram:
13511da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
13521da177e4SLinus Torvalds out:
135377b22836SArnd Bergmann 	release_sock(sk);
13541da177e4SLinus Torvalds 	return rc;
13551da177e4SLinus Torvalds }
13561da177e4SLinus Torvalds 
13571da177e4SLinus Torvalds 
13581da177e4SLinus Torvalds static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
13591da177e4SLinus Torvalds {
13601da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
13611da177e4SLinus Torvalds 	struct x25_sock *x25 = x25_sk(sk);
13621da177e4SLinus Torvalds 	void __user *argp = (void __user *)arg;
13631da177e4SLinus Torvalds 	int rc;
13641da177e4SLinus Torvalds 
13651da177e4SLinus Torvalds 	switch (cmd) {
13661da177e4SLinus Torvalds 	case TIOCOUTQ: {
136770be998cSandrew hendry 		int amount;
136831e6d363SEric Dumazet 
136970be998cSandrew hendry 		amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
13701da177e4SLinus Torvalds 		if (amount < 0)
13711da177e4SLinus Torvalds 			amount = 0;
13721da177e4SLinus Torvalds 		rc = put_user(amount, (unsigned int __user *)argp);
13731da177e4SLinus Torvalds 		break;
13741da177e4SLinus Torvalds 	}
13751da177e4SLinus Torvalds 
13761da177e4SLinus Torvalds 	case TIOCINQ: {
13771da177e4SLinus Torvalds 		struct sk_buff *skb;
13781da177e4SLinus Torvalds 		int amount = 0;
13791da177e4SLinus Torvalds 		/*
13801da177e4SLinus Torvalds 		 * These two are safe on a single CPU system as
13811da177e4SLinus Torvalds 		 * only user tasks fiddle here
13821da177e4SLinus Torvalds 		 */
138354aafbd4Sandrew hendry 		lock_sock(sk);
13841da177e4SLinus Torvalds 		if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
13851da177e4SLinus Torvalds 			amount = skb->len;
138654aafbd4Sandrew hendry 		release_sock(sk);
13871da177e4SLinus Torvalds 		rc = put_user(amount, (unsigned int __user *)argp);
13881da177e4SLinus Torvalds 		break;
13891da177e4SLinus Torvalds 	}
13901da177e4SLinus Torvalds 
13911da177e4SLinus Torvalds 	case SIOCGSTAMP:
13921da177e4SLinus Torvalds 		rc = -EINVAL;
13931da177e4SLinus Torvalds 		if (sk)
13941da177e4SLinus Torvalds 			rc = sock_get_timestamp(sk,
13951da177e4SLinus Torvalds 						(struct timeval __user *)argp);
13961da177e4SLinus Torvalds 		break;
1397ae40eb1eSEric Dumazet 	case SIOCGSTAMPNS:
1398ae40eb1eSEric Dumazet 		rc = -EINVAL;
1399ae40eb1eSEric Dumazet 		if (sk)
1400ae40eb1eSEric Dumazet 			rc = sock_get_timestampns(sk,
1401ae40eb1eSEric Dumazet 					(struct timespec __user *)argp);
1402ae40eb1eSEric Dumazet 		break;
14031da177e4SLinus Torvalds 	case SIOCGIFADDR:
14041da177e4SLinus Torvalds 	case SIOCSIFADDR:
14051da177e4SLinus Torvalds 	case SIOCGIFDSTADDR:
14061da177e4SLinus Torvalds 	case SIOCSIFDSTADDR:
14071da177e4SLinus Torvalds 	case SIOCGIFBRDADDR:
14081da177e4SLinus Torvalds 	case SIOCSIFBRDADDR:
14091da177e4SLinus Torvalds 	case SIOCGIFNETMASK:
14101da177e4SLinus Torvalds 	case SIOCSIFNETMASK:
14111da177e4SLinus Torvalds 	case SIOCGIFMETRIC:
14121da177e4SLinus Torvalds 	case SIOCSIFMETRIC:
14131da177e4SLinus Torvalds 		rc = -EINVAL;
14141da177e4SLinus Torvalds 		break;
14151da177e4SLinus Torvalds 	case SIOCADDRT:
14161da177e4SLinus Torvalds 	case SIOCDELRT:
14171da177e4SLinus Torvalds 		rc = -EPERM;
14181da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
14191da177e4SLinus Torvalds 			break;
14201da177e4SLinus Torvalds 		rc = x25_route_ioctl(cmd, argp);
14211da177e4SLinus Torvalds 		break;
14221da177e4SLinus Torvalds 	case SIOCX25GSUBSCRIP:
14231da177e4SLinus Torvalds 		rc = x25_subscr_ioctl(cmd, argp);
14241da177e4SLinus Torvalds 		break;
14251da177e4SLinus Torvalds 	case SIOCX25SSUBSCRIP:
14261da177e4SLinus Torvalds 		rc = -EPERM;
14271da177e4SLinus Torvalds 		if (!capable(CAP_NET_ADMIN))
14281da177e4SLinus Torvalds 			break;
14291da177e4SLinus Torvalds 		rc = x25_subscr_ioctl(cmd, argp);
14301da177e4SLinus Torvalds 		break;
14311da177e4SLinus Torvalds 	case SIOCX25GFACILITIES: {
1432f90de660Sandrew hendry 		lock_sock(sk);
1433f90de660Sandrew hendry 		rc = copy_to_user(argp, &x25->facilities,
1434f90de660Sandrew hendry 				  sizeof(x25->facilities))
1435f90de660Sandrew hendry 			? -EFAULT : 0;
1436f90de660Sandrew hendry 		release_sock(sk);
14371da177e4SLinus Torvalds 		break;
14381da177e4SLinus Torvalds 	}
14391da177e4SLinus Torvalds 
14401da177e4SLinus Torvalds 	case SIOCX25SFACILITIES: {
14411da177e4SLinus Torvalds 		struct x25_facilities facilities;
14421da177e4SLinus Torvalds 		rc = -EFAULT;
1443fddc5f3eSJoe Perches 		if (copy_from_user(&facilities, argp, sizeof(facilities)))
14441da177e4SLinus Torvalds 			break;
14451da177e4SLinus Torvalds 		rc = -EINVAL;
1446f90de660Sandrew hendry 		lock_sock(sk);
14471da177e4SLinus Torvalds 		if (sk->sk_state != TCP_LISTEN &&
14481da177e4SLinus Torvalds 		    sk->sk_state != TCP_CLOSE)
1449f90de660Sandrew hendry 			goto out_fac_release;
14501da177e4SLinus Torvalds 		if (facilities.pacsize_in < X25_PS16 ||
14511da177e4SLinus Torvalds 		    facilities.pacsize_in > X25_PS4096)
1452f90de660Sandrew hendry 			goto out_fac_release;
14531da177e4SLinus Torvalds 		if (facilities.pacsize_out < X25_PS16 ||
14541da177e4SLinus Torvalds 		    facilities.pacsize_out > X25_PS4096)
1455f90de660Sandrew hendry 			goto out_fac_release;
14561da177e4SLinus Torvalds 		if (facilities.winsize_in < 1 ||
14571da177e4SLinus Torvalds 		    facilities.winsize_in > 127)
1458f90de660Sandrew hendry 			goto out_fac_release;
1459ddd0451fSJohn Hughes 		if (facilities.throughput) {
1460ddd0451fSJohn Hughes 			int out = facilities.throughput & 0xf0;
1461ddd0451fSJohn Hughes 			int in  = facilities.throughput & 0x0f;
1462ddd0451fSJohn Hughes 			if (!out)
1463ddd0451fSJohn Hughes 				facilities.throughput |=
1464ddd0451fSJohn Hughes 					X25_DEFAULT_THROUGHPUT << 4;
1465ddd0451fSJohn Hughes 			else if (out < 0x30 || out > 0xD0)
1466f90de660Sandrew hendry 				goto out_fac_release;
1467ddd0451fSJohn Hughes 			if (!in)
1468ddd0451fSJohn Hughes 				facilities.throughput |=
1469ddd0451fSJohn Hughes 					X25_DEFAULT_THROUGHPUT;
1470ddd0451fSJohn Hughes 			else if (in < 0x03 || in > 0x0D)
1471f90de660Sandrew hendry 				goto out_fac_release;
1472ddd0451fSJohn Hughes 		}
1473ebc3f64bSShaun Pereira 		if (facilities.reverse &&
147406a96b33Sroel kluin 		    (facilities.reverse & 0x81) != 0x81)
1475f90de660Sandrew hendry 			goto out_fac_release;
14761da177e4SLinus Torvalds 		x25->facilities = facilities;
14771da177e4SLinus Torvalds 		rc = 0;
1478f90de660Sandrew hendry out_fac_release:
1479f90de660Sandrew hendry 		release_sock(sk);
14801da177e4SLinus Torvalds 		break;
14811da177e4SLinus Torvalds 	}
14821da177e4SLinus Torvalds 
1483a64b7b93SShaun Pereira 	case SIOCX25GDTEFACILITIES: {
1484f90de660Sandrew hendry 		lock_sock(sk);
1485a64b7b93SShaun Pereira 		rc = copy_to_user(argp, &x25->dte_facilities,
1486a64b7b93SShaun Pereira 				  sizeof(x25->dte_facilities));
1487f90de660Sandrew hendry 		release_sock(sk);
1488a64b7b93SShaun Pereira 		if (rc)
1489a64b7b93SShaun Pereira 			rc = -EFAULT;
1490a64b7b93SShaun Pereira 		break;
1491a64b7b93SShaun Pereira 	}
1492a64b7b93SShaun Pereira 
1493a64b7b93SShaun Pereira 	case SIOCX25SDTEFACILITIES: {
1494a64b7b93SShaun Pereira 		struct x25_dte_facilities dtefacs;
1495a64b7b93SShaun Pereira 		rc = -EFAULT;
1496a64b7b93SShaun Pereira 		if (copy_from_user(&dtefacs, argp, sizeof(dtefacs)))
1497a64b7b93SShaun Pereira 			break;
1498a64b7b93SShaun Pereira 		rc = -EINVAL;
1499f90de660Sandrew hendry 		lock_sock(sk);
1500a64b7b93SShaun Pereira 		if (sk->sk_state != TCP_LISTEN &&
1501a64b7b93SShaun Pereira 		    sk->sk_state != TCP_CLOSE)
1502f90de660Sandrew hendry 			goto out_dtefac_release;
1503a64b7b93SShaun Pereira 		if (dtefacs.calling_len > X25_MAX_AE_LEN)
1504f90de660Sandrew hendry 			goto out_dtefac_release;
1505a64b7b93SShaun Pereira 		if (dtefacs.called_len > X25_MAX_AE_LEN)
1506f90de660Sandrew hendry 			goto out_dtefac_release;
1507a64b7b93SShaun Pereira 		x25->dte_facilities = dtefacs;
1508a64b7b93SShaun Pereira 		rc = 0;
1509f90de660Sandrew hendry out_dtefac_release:
1510f90de660Sandrew hendry 		release_sock(sk);
1511a64b7b93SShaun Pereira 		break;
1512a64b7b93SShaun Pereira 	}
1513a64b7b93SShaun Pereira 
15141da177e4SLinus Torvalds 	case SIOCX25GCALLUSERDATA: {
15155b7958dfSandrew hendry 		lock_sock(sk);
15165b7958dfSandrew hendry 		rc = copy_to_user(argp, &x25->calluserdata,
15175b7958dfSandrew hendry 				  sizeof(x25->calluserdata))
15185b7958dfSandrew hendry 			? -EFAULT : 0;
15195b7958dfSandrew hendry 		release_sock(sk);
15201da177e4SLinus Torvalds 		break;
15211da177e4SLinus Torvalds 	}
15221da177e4SLinus Torvalds 
15231da177e4SLinus Torvalds 	case SIOCX25SCALLUSERDATA: {
15241da177e4SLinus Torvalds 		struct x25_calluserdata calluserdata;
15251da177e4SLinus Torvalds 
15261da177e4SLinus Torvalds 		rc = -EFAULT;
1527fddc5f3eSJoe Perches 		if (copy_from_user(&calluserdata, argp, sizeof(calluserdata)))
15281da177e4SLinus Torvalds 			break;
15291da177e4SLinus Torvalds 		rc = -EINVAL;
15301da177e4SLinus Torvalds 		if (calluserdata.cudlength > X25_MAX_CUD_LEN)
15311da177e4SLinus Torvalds 			break;
15325b7958dfSandrew hendry 		lock_sock(sk);
15331da177e4SLinus Torvalds 		x25->calluserdata = calluserdata;
15345b7958dfSandrew hendry 		release_sock(sk);
15351da177e4SLinus Torvalds 		rc = 0;
15361da177e4SLinus Torvalds 		break;
15371da177e4SLinus Torvalds 	}
15381da177e4SLinus Torvalds 
15391da177e4SLinus Torvalds 	case SIOCX25GCAUSEDIAG: {
154074a7e440Sandrew hendry 		lock_sock(sk);
1541fddc5f3eSJoe Perches 		rc = copy_to_user(argp, &x25->causediag, sizeof(x25->causediag))
154274a7e440Sandrew hendry 			? -EFAULT : 0;
154374a7e440Sandrew hendry 		release_sock(sk);
15441da177e4SLinus Torvalds 		break;
15451da177e4SLinus Torvalds 	}
15461da177e4SLinus Torvalds 
1547386e50ccSAndrew Hendry 	case SIOCX25SCAUSEDIAG: {
1548386e50ccSAndrew Hendry 		struct x25_causediag causediag;
1549386e50ccSAndrew Hendry 		rc = -EFAULT;
1550386e50ccSAndrew Hendry 		if (copy_from_user(&causediag, argp, sizeof(causediag)))
1551386e50ccSAndrew Hendry 			break;
155274a7e440Sandrew hendry 		lock_sock(sk);
1553386e50ccSAndrew Hendry 		x25->causediag = causediag;
155474a7e440Sandrew hendry 		release_sock(sk);
1555386e50ccSAndrew Hendry 		rc = 0;
1556386e50ccSAndrew Hendry 		break;
1557386e50ccSAndrew Hendry 
1558386e50ccSAndrew Hendry 	}
1559386e50ccSAndrew Hendry 
1560cb65d506SShaun Pereira 	case SIOCX25SCUDMATCHLEN: {
1561cb65d506SShaun Pereira 		struct x25_subaddr sub_addr;
1562cb65d506SShaun Pereira 		rc = -EINVAL;
15633f0a069aSandrew hendry 		lock_sock(sk);
1564cb65d506SShaun Pereira 		if(sk->sk_state != TCP_CLOSE)
15653f0a069aSandrew hendry 			goto out_cud_release;
1566cb65d506SShaun Pereira 		rc = -EFAULT;
1567cb65d506SShaun Pereira 		if (copy_from_user(&sub_addr, argp,
1568cb65d506SShaun Pereira 				   sizeof(sub_addr)))
15693f0a069aSandrew hendry 			goto out_cud_release;
1570cb65d506SShaun Pereira 		rc = -EINVAL;
1571cb65d506SShaun Pereira 		if (sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
15723f0a069aSandrew hendry 			goto out_cud_release;
1573cb65d506SShaun Pereira 		x25->cudmatchlength = sub_addr.cudmatchlength;
1574cb65d506SShaun Pereira 		rc = 0;
15753f0a069aSandrew hendry out_cud_release:
15763f0a069aSandrew hendry 		release_sock(sk);
1577cb65d506SShaun Pereira 		break;
1578cb65d506SShaun Pereira 	}
1579cb65d506SShaun Pereira 
1580ebc3f64bSShaun Pereira 	case SIOCX25CALLACCPTAPPRV: {
1581ebc3f64bSShaun Pereira 		rc = -EINVAL;
158277b22836SArnd Bergmann 		lock_sock(sk);
15834ccb93ceSDave Jones 		if (sk->sk_state == TCP_CLOSE) {
158437cda787Sandrew hendry 			clear_bit(X25_ACCPT_APPRV_FLAG, &x25->flags);
1585ebc3f64bSShaun Pereira 			rc = 0;
15864ccb93ceSDave Jones 		}
15874ccb93ceSDave Jones 		release_sock(sk);
1588ebc3f64bSShaun Pereira 		break;
1589ebc3f64bSShaun Pereira 	}
1590ebc3f64bSShaun Pereira 
1591ebc3f64bSShaun Pereira 	case SIOCX25SENDCALLACCPT:  {
1592ebc3f64bSShaun Pereira 		rc = -EINVAL;
159377b22836SArnd Bergmann 		lock_sock(sk);
1594ebc3f64bSShaun Pereira 		if (sk->sk_state != TCP_ESTABLISHED)
15954ccb93ceSDave Jones 			goto out_sendcallaccpt_release;
159637cda787Sandrew hendry 		/* must call accptapprv above */
159737cda787Sandrew hendry 		if (test_bit(X25_ACCPT_APPRV_FLAG, &x25->flags))
15984ccb93ceSDave Jones 			goto out_sendcallaccpt_release;
1599ebc3f64bSShaun Pereira 		x25_write_internal(sk, X25_CALL_ACCEPTED);
1600ebc3f64bSShaun Pereira 		x25->state = X25_STATE_3;
1601ebc3f64bSShaun Pereira 		rc = 0;
16024ccb93ceSDave Jones out_sendcallaccpt_release:
16034ccb93ceSDave Jones 		release_sock(sk);
1604ebc3f64bSShaun Pereira 		break;
1605ebc3f64bSShaun Pereira 	}
1606ebc3f64bSShaun Pereira 
16071da177e4SLinus Torvalds 	default:
1608b5e5fa5eSChristoph Hellwig 		rc = -ENOIOCTLCMD;
16091da177e4SLinus Torvalds 		break;
16101da177e4SLinus Torvalds 	}
16111da177e4SLinus Torvalds 
16121da177e4SLinus Torvalds 	return rc;
16131da177e4SLinus Torvalds }
16141da177e4SLinus Torvalds 
1615ec1b4cf7SStephen Hemminger static const struct net_proto_family x25_family_ops = {
16161da177e4SLinus Torvalds 	.family =	AF_X25,
16171da177e4SLinus Torvalds 	.create =	x25_create,
16181da177e4SLinus Torvalds 	.owner	=	THIS_MODULE,
16191da177e4SLinus Torvalds };
16201da177e4SLinus Torvalds 
16211b06e6baSShaun Pereira #ifdef CONFIG_COMPAT
16221b06e6baSShaun Pereira static int compat_x25_subscr_ioctl(unsigned int cmd,
16231b06e6baSShaun Pereira 		struct compat_x25_subscrip_struct __user *x25_subscr32)
16241b06e6baSShaun Pereira {
16251b06e6baSShaun Pereira 	struct compat_x25_subscrip_struct x25_subscr;
16261b06e6baSShaun Pereira 	struct x25_neigh *nb;
16271b06e6baSShaun Pereira 	struct net_device *dev;
16281b06e6baSShaun Pereira 	int rc = -EINVAL;
16291b06e6baSShaun Pereira 
16301b06e6baSShaun Pereira 	rc = -EFAULT;
16311b06e6baSShaun Pereira 	if (copy_from_user(&x25_subscr, x25_subscr32, sizeof(*x25_subscr32)))
16321b06e6baSShaun Pereira 		goto out;
16331b06e6baSShaun Pereira 
16341b06e6baSShaun Pereira 	rc = -EINVAL;
16351b06e6baSShaun Pereira 	dev = x25_dev_get(x25_subscr.device);
16361b06e6baSShaun Pereira 	if (dev == NULL)
16371b06e6baSShaun Pereira 		goto out;
16381b06e6baSShaun Pereira 
16391b06e6baSShaun Pereira 	nb = x25_get_neigh(dev);
16401b06e6baSShaun Pereira 	if (nb == NULL)
16411b06e6baSShaun Pereira 		goto out_dev_put;
16421b06e6baSShaun Pereira 
16431b06e6baSShaun Pereira 	dev_put(dev);
16441b06e6baSShaun Pereira 
16451b06e6baSShaun Pereira 	if (cmd == SIOCX25GSUBSCRIP) {
16465595a1a5Sandrew hendry 		read_lock_bh(&x25_neigh_list_lock);
16471b06e6baSShaun Pereira 		x25_subscr.extended = nb->extended;
16481b06e6baSShaun Pereira 		x25_subscr.global_facil_mask = nb->global_facil_mask;
16495595a1a5Sandrew hendry 		read_unlock_bh(&x25_neigh_list_lock);
16501b06e6baSShaun Pereira 		rc = copy_to_user(x25_subscr32, &x25_subscr,
16511b06e6baSShaun Pereira 				sizeof(*x25_subscr32)) ? -EFAULT : 0;
16521b06e6baSShaun Pereira 	} else {
16531b06e6baSShaun Pereira 		rc = -EINVAL;
16541b06e6baSShaun Pereira 		if (x25_subscr.extended == 0 || x25_subscr.extended == 1) {
16551b06e6baSShaun Pereira 			rc = 0;
16565595a1a5Sandrew hendry 			write_lock_bh(&x25_neigh_list_lock);
16571b06e6baSShaun Pereira 			nb->extended = x25_subscr.extended;
16581b06e6baSShaun Pereira 			nb->global_facil_mask = x25_subscr.global_facil_mask;
16595595a1a5Sandrew hendry 			write_unlock_bh(&x25_neigh_list_lock);
16601b06e6baSShaun Pereira 		}
16611b06e6baSShaun Pereira 	}
16621b06e6baSShaun Pereira 	x25_neigh_put(nb);
16631b06e6baSShaun Pereira out:
16641b06e6baSShaun Pereira 	return rc;
16651b06e6baSShaun Pereira out_dev_put:
16661b06e6baSShaun Pereira 	dev_put(dev);
16671b06e6baSShaun Pereira 	goto out;
16681b06e6baSShaun Pereira }
16691b06e6baSShaun Pereira 
16701b06e6baSShaun Pereira static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
16711b06e6baSShaun Pereira 				unsigned long arg)
16721b06e6baSShaun Pereira {
16731b06e6baSShaun Pereira 	void __user *argp = compat_ptr(arg);
16741b06e6baSShaun Pereira 	struct sock *sk = sock->sk;
16751b06e6baSShaun Pereira 
16761b06e6baSShaun Pereira 	int rc = -ENOIOCTLCMD;
16771b06e6baSShaun Pereira 
16781b06e6baSShaun Pereira 	switch(cmd) {
16791b06e6baSShaun Pereira 	case TIOCOUTQ:
16801b06e6baSShaun Pereira 	case TIOCINQ:
16811b06e6baSShaun Pereira 		rc = x25_ioctl(sock, cmd, (unsigned long)argp);
16821b06e6baSShaun Pereira 		break;
16831b06e6baSShaun Pereira 	case SIOCGSTAMP:
16841b06e6baSShaun Pereira 		rc = -EINVAL;
16851b06e6baSShaun Pereira 		if (sk)
16861b06e6baSShaun Pereira 			rc = compat_sock_get_timestamp(sk,
16871b06e6baSShaun Pereira 					(struct timeval __user*)argp);
16881b06e6baSShaun Pereira 		break;
1689ae40eb1eSEric Dumazet 	case SIOCGSTAMPNS:
1690ae40eb1eSEric Dumazet 		rc = -EINVAL;
1691ae40eb1eSEric Dumazet 		if (sk)
1692ae40eb1eSEric Dumazet 			rc = compat_sock_get_timestampns(sk,
1693ae40eb1eSEric Dumazet 					(struct timespec __user*)argp);
1694ae40eb1eSEric Dumazet 		break;
16951b06e6baSShaun Pereira 	case SIOCGIFADDR:
16961b06e6baSShaun Pereira 	case SIOCSIFADDR:
16971b06e6baSShaun Pereira 	case SIOCGIFDSTADDR:
16981b06e6baSShaun Pereira 	case SIOCSIFDSTADDR:
16991b06e6baSShaun Pereira 	case SIOCGIFBRDADDR:
17001b06e6baSShaun Pereira 	case SIOCSIFBRDADDR:
17011b06e6baSShaun Pereira 	case SIOCGIFNETMASK:
17021b06e6baSShaun Pereira 	case SIOCSIFNETMASK:
17031b06e6baSShaun Pereira 	case SIOCGIFMETRIC:
17041b06e6baSShaun Pereira 	case SIOCSIFMETRIC:
17051b06e6baSShaun Pereira 		rc = -EINVAL;
17061b06e6baSShaun Pereira 		break;
17071b06e6baSShaun Pereira 	case SIOCADDRT:
17081b06e6baSShaun Pereira 	case SIOCDELRT:
17091b06e6baSShaun Pereira 		rc = -EPERM;
17101b06e6baSShaun Pereira 		if (!capable(CAP_NET_ADMIN))
17111b06e6baSShaun Pereira 			break;
17121b06e6baSShaun Pereira 		rc = x25_route_ioctl(cmd, argp);
17131b06e6baSShaun Pereira 		break;
17141b06e6baSShaun Pereira 	case SIOCX25GSUBSCRIP:
17151b06e6baSShaun Pereira 		rc = compat_x25_subscr_ioctl(cmd, argp);
17161b06e6baSShaun Pereira 		break;
17171b06e6baSShaun Pereira 	case SIOCX25SSUBSCRIP:
17181b06e6baSShaun Pereira 		rc = -EPERM;
17191b06e6baSShaun Pereira 		if (!capable(CAP_NET_ADMIN))
17201b06e6baSShaun Pereira 			break;
17211b06e6baSShaun Pereira 		rc = compat_x25_subscr_ioctl(cmd, argp);
17221b06e6baSShaun Pereira 		break;
17231b06e6baSShaun Pereira 	case SIOCX25GFACILITIES:
17241b06e6baSShaun Pereira 	case SIOCX25SFACILITIES:
17259a6b9f2eSShaun Pereira 	case SIOCX25GDTEFACILITIES:
17269a6b9f2eSShaun Pereira 	case SIOCX25SDTEFACILITIES:
17271b06e6baSShaun Pereira 	case SIOCX25GCALLUSERDATA:
17281b06e6baSShaun Pereira 	case SIOCX25SCALLUSERDATA:
17291b06e6baSShaun Pereira 	case SIOCX25GCAUSEDIAG:
1730386e50ccSAndrew Hendry 	case SIOCX25SCAUSEDIAG:
17311b06e6baSShaun Pereira 	case SIOCX25SCUDMATCHLEN:
17321b06e6baSShaun Pereira 	case SIOCX25CALLACCPTAPPRV:
17331b06e6baSShaun Pereira 	case SIOCX25SENDCALLACCPT:
17341b06e6baSShaun Pereira 		rc = x25_ioctl(sock, cmd, (unsigned long)argp);
17351b06e6baSShaun Pereira 		break;
17361b06e6baSShaun Pereira 	default:
17371b06e6baSShaun Pereira 		rc = -ENOIOCTLCMD;
17381b06e6baSShaun Pereira 		break;
17391b06e6baSShaun Pereira 	}
17401b06e6baSShaun Pereira 	return rc;
17411b06e6baSShaun Pereira }
17421b06e6baSShaun Pereira #endif
17431b06e6baSShaun Pereira 
174491774904SArnd Bergmann static const struct proto_ops x25_proto_ops = {
17451da177e4SLinus Torvalds 	.family =	AF_X25,
17461da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
17471da177e4SLinus Torvalds 	.release =	x25_release,
17481da177e4SLinus Torvalds 	.bind =		x25_bind,
17491da177e4SLinus Torvalds 	.connect =	x25_connect,
17501da177e4SLinus Torvalds 	.socketpair =	sock_no_socketpair,
17511da177e4SLinus Torvalds 	.accept =	x25_accept,
17521da177e4SLinus Torvalds 	.getname =	x25_getname,
1753768190fdSandrew hendry 	.poll =		datagram_poll,
17541da177e4SLinus Torvalds 	.ioctl =	x25_ioctl,
17551b06e6baSShaun Pereira #ifdef CONFIG_COMPAT
17561b06e6baSShaun Pereira 	.compat_ioctl = compat_x25_ioctl,
17571b06e6baSShaun Pereira #endif
17581da177e4SLinus Torvalds 	.listen =	x25_listen,
17591da177e4SLinus Torvalds 	.shutdown =	sock_no_shutdown,
17601da177e4SLinus Torvalds 	.setsockopt =	x25_setsockopt,
17611da177e4SLinus Torvalds 	.getsockopt =	x25_getsockopt,
17621da177e4SLinus Torvalds 	.sendmsg =	x25_sendmsg,
17631da177e4SLinus Torvalds 	.recvmsg =	x25_recvmsg,
17641da177e4SLinus Torvalds 	.mmap =		sock_no_mmap,
17651da177e4SLinus Torvalds 	.sendpage =	sock_no_sendpage,
17661da177e4SLinus Torvalds };
17671da177e4SLinus Torvalds 
17687546dd97SStephen Hemminger static struct packet_type x25_packet_type __read_mostly = {
176909640e63SHarvey Harrison 	.type =	cpu_to_be16(ETH_P_X25),
17701da177e4SLinus Torvalds 	.func =	x25_lapb_receive_frame,
17711da177e4SLinus Torvalds };
17721da177e4SLinus Torvalds 
17731da177e4SLinus Torvalds static struct notifier_block x25_dev_notifier = {
17741da177e4SLinus Torvalds 	.notifier_call = x25_device_event,
17751da177e4SLinus Torvalds };
17761da177e4SLinus Torvalds 
17771da177e4SLinus Torvalds void x25_kill_by_neigh(struct x25_neigh *nb)
17781da177e4SLinus Torvalds {
17791da177e4SLinus Torvalds 	struct sock *s;
17801da177e4SLinus Torvalds 
17811da177e4SLinus Torvalds 	write_lock_bh(&x25_list_lock);
17821da177e4SLinus Torvalds 
1783b67bfe0dSSasha Levin 	sk_for_each(s, &x25_list)
17841da177e4SLinus Torvalds 		if (x25_sk(s)->neighbour == nb)
17851da177e4SLinus Torvalds 			x25_disconnect(s, ENETUNREACH, 0, 0);
17861da177e4SLinus Torvalds 
17871da177e4SLinus Torvalds 	write_unlock_bh(&x25_list_lock);
178895a9dc43SAndrew Hendry 
178995a9dc43SAndrew Hendry 	/* Remove any related forwards */
179095a9dc43SAndrew Hendry 	x25_clear_forward_by_dev(nb->dev);
17911da177e4SLinus Torvalds }
17921da177e4SLinus Torvalds 
17931da177e4SLinus Torvalds static int __init x25_init(void)
17941da177e4SLinus Torvalds {
179564df6d52Slinzhang 	int rc;
17961da177e4SLinus Torvalds 
179764df6d52Slinzhang 	rc = proto_register(&x25_proto, 0);
179864df6d52Slinzhang 	if (rc)
17991da177e4SLinus Torvalds 		goto out;
18001da177e4SLinus Torvalds 
18011fd975a0Sandrew hendry 	rc = sock_register(&x25_family_ops);
180264df6d52Slinzhang 	if (rc)
18031fd975a0Sandrew hendry 		goto out_proto;
18041da177e4SLinus Torvalds 
18051da177e4SLinus Torvalds 	dev_add_pack(&x25_packet_type);
18061da177e4SLinus Torvalds 
18071fd975a0Sandrew hendry 	rc = register_netdevice_notifier(&x25_dev_notifier);
180864df6d52Slinzhang 	if (rc)
18091fd975a0Sandrew hendry 		goto out_sock;
18101da177e4SLinus Torvalds 
181164df6d52Slinzhang 	rc = x25_register_sysctl();
181264df6d52Slinzhang 	if (rc)
181364df6d52Slinzhang 		goto out_dev;
181464df6d52Slinzhang 
181564df6d52Slinzhang 	rc = x25_proc_init();
181664df6d52Slinzhang 	if (rc)
181764df6d52Slinzhang 		goto out_sysctl;
181864df6d52Slinzhang 
1819b73e9e3cSwangweidong 	pr_info("Linux Version 0.2\n");
18201da177e4SLinus Torvalds 
18211da177e4SLinus Torvalds out:
18221da177e4SLinus Torvalds 	return rc;
182364df6d52Slinzhang out_sysctl:
182464df6d52Slinzhang 	x25_unregister_sysctl();
18251fd975a0Sandrew hendry out_dev:
18261fd975a0Sandrew hendry 	unregister_netdevice_notifier(&x25_dev_notifier);
18271fd975a0Sandrew hendry out_sock:
182864df6d52Slinzhang 	dev_remove_pack(&x25_packet_type);
18291fd975a0Sandrew hendry 	sock_unregister(AF_X25);
18301fd975a0Sandrew hendry out_proto:
18311fd975a0Sandrew hendry 	proto_unregister(&x25_proto);
18321fd975a0Sandrew hendry 	goto out;
18331da177e4SLinus Torvalds }
18341da177e4SLinus Torvalds module_init(x25_init);
18351da177e4SLinus Torvalds 
18361da177e4SLinus Torvalds static void __exit x25_exit(void)
18371da177e4SLinus Torvalds {
18381da177e4SLinus Torvalds 	x25_proc_exit();
18391da177e4SLinus Torvalds 	x25_link_free();
18401da177e4SLinus Torvalds 	x25_route_free();
18411da177e4SLinus Torvalds 
18421da177e4SLinus Torvalds 	x25_unregister_sysctl();
18431da177e4SLinus Torvalds 
18441da177e4SLinus Torvalds 	unregister_netdevice_notifier(&x25_dev_notifier);
18451da177e4SLinus Torvalds 
18461da177e4SLinus Torvalds 	dev_remove_pack(&x25_packet_type);
18471da177e4SLinus Torvalds 
18481da177e4SLinus Torvalds 	sock_unregister(AF_X25);
18491da177e4SLinus Torvalds 	proto_unregister(&x25_proto);
18501da177e4SLinus Torvalds }
18511da177e4SLinus Torvalds module_exit(x25_exit);
18521da177e4SLinus Torvalds 
18531da177e4SLinus Torvalds MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
18541da177e4SLinus Torvalds MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
18551da177e4SLinus Torvalds MODULE_LICENSE("GPL");
18561da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_X25);
1857