xref: /openbmc/linux/net/x25/af_x25.c (revision b18e7a06857833d2c7c8c8457e5a3a7c3327f643)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *	X.25 Packet Layer release 002
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *	This is ALPHA test software. This code may break your machine,
51da177e4SLinus Torvalds  *	randomly fail to work with new releases, misbehave and/or generally
61da177e4SLinus Torvalds  *	screw up. It might even work.
71da177e4SLinus Torvalds  *
81da177e4SLinus Torvalds  *	This code REQUIRES 2.1.15 or higher
91da177e4SLinus Torvalds  *
101da177e4SLinus Torvalds  *	This module:
111da177e4SLinus Torvalds  *		This module is free software; you can redistribute it and/or
121da177e4SLinus Torvalds  *		modify it under the terms of the GNU General Public License
131da177e4SLinus Torvalds  *		as published by the Free Software Foundation; either version
141da177e4SLinus Torvalds  *		2 of the License, or (at your option) any later version.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  *	History
171da177e4SLinus Torvalds  *	X.25 001	Jonathan Naylor	Started coding.
181da177e4SLinus Torvalds  *	X.25 002	Jonathan Naylor	Centralised disconnect handling.
191da177e4SLinus Torvalds  *					New timer architecture.
201da177e4SLinus Torvalds  *	2000-03-11	Henner Eisen	MSG_EOR handling more POSIX compliant.
211da177e4SLinus Torvalds  *	2000-03-22	Daniela Squassoni Allowed disabling/enabling of
221da177e4SLinus Torvalds  *					  facilities negotiation and increased
231da177e4SLinus Torvalds  *					  the throughput upper limit.
241da177e4SLinus Torvalds  *	2000-08-27	Arnaldo C. Melo s/suser/capable/ + micro cleanups
251da177e4SLinus Torvalds  *	2000-09-04	Henner Eisen	Set sock->state in x25_accept().
261da177e4SLinus Torvalds  *					Fixed x25_output() related skb leakage.
271da177e4SLinus Torvalds  *	2000-10-02	Henner Eisen	Made x25_kick() single threaded per socket.
281da177e4SLinus Torvalds  *	2000-10-27	Henner Eisen    MSG_DONTWAIT for fragment allocation.
291da177e4SLinus Torvalds  *	2000-11-14	Henner Eisen    Closing datalink from NETDEV_GOING_DOWN
301da177e4SLinus Torvalds  *	2002-10-06	Arnaldo C. Melo Get rid of cli/sti, move proc stuff to
311da177e4SLinus Torvalds  *					x25_proc.c, using seq_file
32cb65d506SShaun Pereira  *	2005-04-02	Shaun Pereira	Selective sub address matching
33cb65d506SShaun Pereira  *					with call user data
34ebc3f64bSShaun Pereira  *	2005-04-15	Shaun Pereira	Fast select with no restriction on
35ebc3f64bSShaun Pereira  *					response
361da177e4SLinus Torvalds  */
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds #include <linux/module.h>
394fc268d2SRandy Dunlap #include <linux/capability.h>
401da177e4SLinus Torvalds #include <linux/errno.h>
411da177e4SLinus Torvalds #include <linux/kernel.h>
421da177e4SLinus Torvalds #include <linux/sched.h>
43405f5571SAlexey Dobriyan #include <linux/smp_lock.h>
441da177e4SLinus Torvalds #include <linux/timer.h>
451da177e4SLinus Torvalds #include <linux/string.h>
461da177e4SLinus Torvalds #include <linux/net.h>
471da177e4SLinus Torvalds #include <linux/netdevice.h>
481da177e4SLinus Torvalds #include <linux/if_arp.h>
491da177e4SLinus Torvalds #include <linux/skbuff.h>
501da177e4SLinus Torvalds #include <net/sock.h>
51c752f073SArnaldo Carvalho de Melo #include <net/tcp_states.h>
521da177e4SLinus Torvalds #include <asm/uaccess.h>
531da177e4SLinus Torvalds #include <linux/fcntl.h>
541da177e4SLinus Torvalds #include <linux/termios.h>	/* For TIOCINQ/OUTQ */
551da177e4SLinus Torvalds #include <linux/notifier.h>
561da177e4SLinus Torvalds #include <linux/init.h>
571b06e6baSShaun Pereira #include <linux/compat.h>
581b06e6baSShaun Pereira 
591da177e4SLinus Torvalds #include <net/x25.h>
601b06e6baSShaun Pereira #include <net/compat.h>
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds int sysctl_x25_restart_request_timeout = X25_DEFAULT_T20;
631da177e4SLinus Torvalds int sysctl_x25_call_request_timeout    = X25_DEFAULT_T21;
641da177e4SLinus Torvalds int sysctl_x25_reset_request_timeout   = X25_DEFAULT_T22;
651da177e4SLinus Torvalds int sysctl_x25_clear_request_timeout   = X25_DEFAULT_T23;
661da177e4SLinus Torvalds int sysctl_x25_ack_holdback_timeout    = X25_DEFAULT_T2;
6739e21c0dSAndrew Hendry int sysctl_x25_forward                 = 0;
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds HLIST_HEAD(x25_list);
701da177e4SLinus Torvalds DEFINE_RWLOCK(x25_list_lock);
711da177e4SLinus Torvalds 
7290ddc4f0SEric Dumazet static const struct proto_ops x25_proto_ops;
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds static struct x25_address null_x25_address = {"               "};
751da177e4SLinus Torvalds 
761b06e6baSShaun Pereira #ifdef CONFIG_COMPAT
771b06e6baSShaun Pereira struct compat_x25_subscrip_struct {
781b06e6baSShaun Pereira 	char device[200-sizeof(compat_ulong_t)];
791b06e6baSShaun Pereira 	compat_ulong_t global_facil_mask;
801b06e6baSShaun Pereira 	compat_uint_t extended;
811b06e6baSShaun Pereira };
821b06e6baSShaun Pereira #endif
831b06e6baSShaun Pereira 
841da177e4SLinus Torvalds int x25_addr_ntoa(unsigned char *p, struct x25_address *called_addr,
851da177e4SLinus Torvalds 		  struct x25_address *calling_addr)
861da177e4SLinus Torvalds {
876bf1574eSEric Dumazet 	unsigned int called_len, calling_len;
881da177e4SLinus Torvalds 	char *called, *calling;
896bf1574eSEric Dumazet 	unsigned int i;
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	called_len  = (*p >> 0) & 0x0F;
921da177e4SLinus Torvalds 	calling_len = (*p >> 4) & 0x0F;
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 	called  = called_addr->x25_addr;
951da177e4SLinus Torvalds 	calling = calling_addr->x25_addr;
961da177e4SLinus Torvalds 	p++;
971da177e4SLinus Torvalds 
981da177e4SLinus Torvalds 	for (i = 0; i < (called_len + calling_len); i++) {
991da177e4SLinus Torvalds 		if (i < called_len) {
1001da177e4SLinus Torvalds 			if (i % 2 != 0) {
1011da177e4SLinus Torvalds 				*called++ = ((*p >> 0) & 0x0F) + '0';
1021da177e4SLinus Torvalds 				p++;
1031da177e4SLinus Torvalds 			} else {
1041da177e4SLinus Torvalds 				*called++ = ((*p >> 4) & 0x0F) + '0';
1051da177e4SLinus Torvalds 			}
1061da177e4SLinus Torvalds 		} else {
1071da177e4SLinus Torvalds 			if (i % 2 != 0) {
1081da177e4SLinus Torvalds 				*calling++ = ((*p >> 0) & 0x0F) + '0';
1091da177e4SLinus Torvalds 				p++;
1101da177e4SLinus Torvalds 			} else {
1111da177e4SLinus Torvalds 				*calling++ = ((*p >> 4) & 0x0F) + '0';
1121da177e4SLinus Torvalds 			}
1131da177e4SLinus Torvalds 		}
1141da177e4SLinus Torvalds 	}
1151da177e4SLinus Torvalds 
1161da177e4SLinus Torvalds 	*called = *calling = '\0';
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds 	return 1 + (called_len + calling_len + 1) / 2;
1191da177e4SLinus Torvalds }
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds int x25_addr_aton(unsigned char *p, struct x25_address *called_addr,
1221da177e4SLinus Torvalds 		  struct x25_address *calling_addr)
1231da177e4SLinus Torvalds {
1241da177e4SLinus Torvalds 	unsigned int called_len, calling_len;
1251da177e4SLinus Torvalds 	char *called, *calling;
1261da177e4SLinus Torvalds 	int i;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 	called  = called_addr->x25_addr;
1291da177e4SLinus Torvalds 	calling = calling_addr->x25_addr;
1301da177e4SLinus Torvalds 
1311da177e4SLinus Torvalds 	called_len  = strlen(called);
1321da177e4SLinus Torvalds 	calling_len = strlen(calling);
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds 	*p++ = (calling_len << 4) | (called_len << 0);
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 				*p |= (*called++ - '0') << 0;
1401da177e4SLinus Torvalds 				p++;
1411da177e4SLinus Torvalds 			} else {
1421da177e4SLinus Torvalds 				*p = 0x00;
1431da177e4SLinus Torvalds 				*p |= (*called++ - '0') << 4;
1441da177e4SLinus Torvalds 			}
1451da177e4SLinus Torvalds 		} else {
1461da177e4SLinus Torvalds 			if (i % 2 != 0) {
1471da177e4SLinus Torvalds 				*p |= (*calling++ - '0') << 0;
1481da177e4SLinus Torvalds 				p++;
1491da177e4SLinus Torvalds 			} else {
1501da177e4SLinus Torvalds 				*p = 0x00;
1511da177e4SLinus Torvalds 				*p |= (*calling++ - '0') << 4;
1521da177e4SLinus Torvalds 			}
1531da177e4SLinus Torvalds 		}
1541da177e4SLinus Torvalds 	}
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 	return 1 + (called_len + calling_len + 1) / 2;
1571da177e4SLinus Torvalds }
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds /*
1601da177e4SLinus Torvalds  *	Socket removal during an interrupt is now safe.
1611da177e4SLinus Torvalds  */
1621da177e4SLinus Torvalds static void x25_remove_socket(struct sock *sk)
1631da177e4SLinus Torvalds {
1641da177e4SLinus Torvalds 	write_lock_bh(&x25_list_lock);
1651da177e4SLinus Torvalds 	sk_del_node_init(sk);
1661da177e4SLinus Torvalds 	write_unlock_bh(&x25_list_lock);
1671da177e4SLinus Torvalds }
1681da177e4SLinus Torvalds 
1691da177e4SLinus Torvalds /*
1701da177e4SLinus Torvalds  *	Kill all bound sockets on a dropped device.
1711da177e4SLinus Torvalds  */
1721da177e4SLinus Torvalds static void x25_kill_by_device(struct net_device *dev)
1731da177e4SLinus Torvalds {
1741da177e4SLinus Torvalds 	struct sock *s;
1751da177e4SLinus Torvalds 	struct hlist_node *node;
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	write_lock_bh(&x25_list_lock);
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds 	sk_for_each(s, node, &x25_list)
1801da177e4SLinus Torvalds 		if (x25_sk(s)->neighbour && x25_sk(s)->neighbour->dev == dev)
1811da177e4SLinus Torvalds 			x25_disconnect(s, ENETUNREACH, 0, 0);
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	write_unlock_bh(&x25_list_lock);
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds /*
1871da177e4SLinus Torvalds  *	Handle device status changes.
1881da177e4SLinus Torvalds  */
1891da177e4SLinus Torvalds static int x25_device_event(struct notifier_block *this, unsigned long event,
1901da177e4SLinus Torvalds 			    void *ptr)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	struct net_device *dev = ptr;
1931da177e4SLinus Torvalds 	struct x25_neigh *nb;
1941da177e4SLinus Torvalds 
195721499e8SYOSHIFUJI Hideaki 	if (!net_eq(dev_net(dev), &init_net))
196e9dc8653SEric W. Biederman 		return NOTIFY_DONE;
197e9dc8653SEric W. Biederman 
1981da177e4SLinus Torvalds 	if (dev->type == ARPHRD_X25
1991da177e4SLinus Torvalds #if defined(CONFIG_LLC) || defined(CONFIG_LLC_MODULE)
2001da177e4SLinus Torvalds 	 || dev->type == ARPHRD_ETHER
2011da177e4SLinus Torvalds #endif
2021da177e4SLinus Torvalds 	 ) {
2031da177e4SLinus Torvalds 		switch (event) {
2041da177e4SLinus Torvalds 			case NETDEV_UP:
2051da177e4SLinus Torvalds 				x25_link_device_up(dev);
2061da177e4SLinus Torvalds 				break;
2071da177e4SLinus Torvalds 			case NETDEV_GOING_DOWN:
2081da177e4SLinus Torvalds 				nb = x25_get_neigh(dev);
2091da177e4SLinus Torvalds 				if (nb) {
2101da177e4SLinus Torvalds 					x25_terminate_link(nb);
2111da177e4SLinus Torvalds 					x25_neigh_put(nb);
2121da177e4SLinus Torvalds 				}
2131da177e4SLinus Torvalds 				break;
2141da177e4SLinus Torvalds 			case NETDEV_DOWN:
2151da177e4SLinus Torvalds 				x25_kill_by_device(dev);
2161da177e4SLinus Torvalds 				x25_route_device_down(dev);
2171da177e4SLinus Torvalds 				x25_link_device_down(dev);
2181da177e4SLinus Torvalds 				break;
2191da177e4SLinus Torvalds 		}
2201da177e4SLinus Torvalds 	}
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	return NOTIFY_DONE;
2231da177e4SLinus Torvalds }
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds /*
2261da177e4SLinus Torvalds  *	Add a socket to the bound sockets list.
2271da177e4SLinus Torvalds  */
2281da177e4SLinus Torvalds static void x25_insert_socket(struct sock *sk)
2291da177e4SLinus Torvalds {
2301da177e4SLinus Torvalds 	write_lock_bh(&x25_list_lock);
2311da177e4SLinus Torvalds 	sk_add_node(sk, &x25_list);
2321da177e4SLinus Torvalds 	write_unlock_bh(&x25_list_lock);
2331da177e4SLinus Torvalds }
2341da177e4SLinus Torvalds 
2351da177e4SLinus Torvalds /*
2361da177e4SLinus Torvalds  *	Find a socket that wants to accept the Call Request we just
2371da177e4SLinus Torvalds  *	received. Check the full list for an address/cud match.
2381da177e4SLinus Torvalds  *	If no cuds match return the next_best thing, an address match.
2391da177e4SLinus Torvalds  *	Note: if a listening socket has cud set it must only get calls
2401da177e4SLinus Torvalds  *	with matching cud.
2411da177e4SLinus Torvalds  */
242cb65d506SShaun Pereira static struct sock *x25_find_listener(struct x25_address *addr,
243cb65d506SShaun Pereira 					struct sk_buff *skb)
2441da177e4SLinus Torvalds {
2451da177e4SLinus Torvalds 	struct sock *s;
2461da177e4SLinus Torvalds 	struct sock *next_best;
2471da177e4SLinus Torvalds 	struct hlist_node *node;
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	read_lock_bh(&x25_list_lock);
2501da177e4SLinus Torvalds 	next_best = NULL;
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 	sk_for_each(s, node, &x25_list)
2531da177e4SLinus Torvalds 		if ((!strcmp(addr->x25_addr,
2541da177e4SLinus Torvalds 			x25_sk(s)->source_addr.x25_addr) ||
2551da177e4SLinus Torvalds 				!strcmp(addr->x25_addr,
2561da177e4SLinus Torvalds 					null_x25_address.x25_addr)) &&
2571da177e4SLinus Torvalds 					s->sk_state == TCP_LISTEN) {
2581da177e4SLinus Torvalds 			/*
2591da177e4SLinus Torvalds 			 * Found a listening socket, now check the incoming
2601da177e4SLinus Torvalds 			 * call user data vs this sockets call user data
2611da177e4SLinus Torvalds 			 */
262cb65d506SShaun Pereira 			if(skb->len > 0 && x25_sk(s)->cudmatchlength > 0) {
263cb65d506SShaun Pereira 				if((memcmp(x25_sk(s)->calluserdata.cuddata,
264cb65d506SShaun Pereira 					skb->data,
265cb65d506SShaun Pereira 					x25_sk(s)->cudmatchlength)) == 0) {
2661da177e4SLinus Torvalds 					sock_hold(s);
2671da177e4SLinus Torvalds 					goto found;
2681da177e4SLinus Torvalds 				 }
269cb65d506SShaun Pereira 			} else
2701da177e4SLinus Torvalds 				next_best = s;
2711da177e4SLinus Torvalds 		}
2721da177e4SLinus Torvalds 	if (next_best) {
2731da177e4SLinus Torvalds 		s = next_best;
2741da177e4SLinus Torvalds 		sock_hold(s);
2751da177e4SLinus Torvalds 		goto found;
2761da177e4SLinus Torvalds 	}
2771da177e4SLinus Torvalds 	s = NULL;
2781da177e4SLinus Torvalds found:
2791da177e4SLinus Torvalds 	read_unlock_bh(&x25_list_lock);
2801da177e4SLinus Torvalds 	return s;
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds /*
2841da177e4SLinus Torvalds  *	Find a connected X.25 socket given my LCI and neighbour.
2851da177e4SLinus Torvalds  */
2861da177e4SLinus Torvalds static struct sock *__x25_find_socket(unsigned int lci, struct x25_neigh *nb)
2871da177e4SLinus Torvalds {
2881da177e4SLinus Torvalds 	struct sock *s;
2891da177e4SLinus Torvalds 	struct hlist_node *node;
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds 	sk_for_each(s, node, &x25_list)
2921da177e4SLinus Torvalds 		if (x25_sk(s)->lci == lci && x25_sk(s)->neighbour == nb) {
2931da177e4SLinus Torvalds 			sock_hold(s);
2941da177e4SLinus Torvalds 			goto found;
2951da177e4SLinus Torvalds 		}
2961da177e4SLinus Torvalds 	s = NULL;
2971da177e4SLinus Torvalds found:
2981da177e4SLinus Torvalds 	return s;
2991da177e4SLinus Torvalds }
3001da177e4SLinus Torvalds 
3011da177e4SLinus Torvalds struct sock *x25_find_socket(unsigned int lci, struct x25_neigh *nb)
3021da177e4SLinus Torvalds {
3031da177e4SLinus Torvalds 	struct sock *s;
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds 	read_lock_bh(&x25_list_lock);
3061da177e4SLinus Torvalds 	s = __x25_find_socket(lci, nb);
3071da177e4SLinus Torvalds 	read_unlock_bh(&x25_list_lock);
3081da177e4SLinus Torvalds 	return s;
3091da177e4SLinus Torvalds }
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds /*
3121da177e4SLinus Torvalds  *	Find a unique LCI for a given device.
3131da177e4SLinus Torvalds  */
3141da177e4SLinus Torvalds static unsigned int x25_new_lci(struct x25_neigh *nb)
3151da177e4SLinus Torvalds {
3161da177e4SLinus Torvalds 	unsigned int lci = 1;
3171da177e4SLinus Torvalds 	struct sock *sk;
3181da177e4SLinus Torvalds 
3191da177e4SLinus Torvalds 	read_lock_bh(&x25_list_lock);
3201da177e4SLinus Torvalds 
3211da177e4SLinus Torvalds 	while ((sk = __x25_find_socket(lci, nb)) != NULL) {
3221da177e4SLinus Torvalds 		sock_put(sk);
3231da177e4SLinus Torvalds 		if (++lci == 4096) {
3241da177e4SLinus Torvalds 			lci = 0;
3251da177e4SLinus Torvalds 			break;
3261da177e4SLinus Torvalds 		}
3271da177e4SLinus Torvalds 	}
3281da177e4SLinus Torvalds 
3291da177e4SLinus Torvalds 	read_unlock_bh(&x25_list_lock);
3301da177e4SLinus Torvalds 	return lci;
3311da177e4SLinus Torvalds }
3321da177e4SLinus Torvalds 
3331da177e4SLinus Torvalds /*
3341da177e4SLinus Torvalds  *	Deferred destroy.
3351da177e4SLinus Torvalds  */
33614ebaf81SDavid S. Miller static void __x25_destroy_socket(struct sock *);
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds /*
3391da177e4SLinus Torvalds  *	handler for deferred kills.
3401da177e4SLinus Torvalds  */
3411da177e4SLinus Torvalds static void x25_destroy_timer(unsigned long data)
3421da177e4SLinus Torvalds {
34314ebaf81SDavid S. Miller 	x25_destroy_socket_from_timer((struct sock *)data);
3441da177e4SLinus Torvalds }
3451da177e4SLinus Torvalds 
3461da177e4SLinus Torvalds /*
3471da177e4SLinus Torvalds  *	This is called from user mode and the timers. Thus it protects itself
3481da177e4SLinus Torvalds  *	against interrupt users but doesn't worry about being called during
3491da177e4SLinus Torvalds  *	work. Once it is removed from the queue no interrupt or bottom half
3501da177e4SLinus Torvalds  *	will touch it and we are (fairly 8-) ) safe.
3511da177e4SLinus Torvalds  *	Not static as it's used by the timer
3521da177e4SLinus Torvalds  */
35314ebaf81SDavid S. Miller static void __x25_destroy_socket(struct sock *sk)
3541da177e4SLinus Torvalds {
3551da177e4SLinus Torvalds 	struct sk_buff *skb;
3561da177e4SLinus Torvalds 
3571da177e4SLinus Torvalds 	x25_stop_heartbeat(sk);
3581da177e4SLinus Torvalds 	x25_stop_timer(sk);
3591da177e4SLinus Torvalds 
3601da177e4SLinus Torvalds 	x25_remove_socket(sk);
3611da177e4SLinus Torvalds 	x25_clear_queues(sk);		/* Flush the queues */
3621da177e4SLinus Torvalds 
3631da177e4SLinus Torvalds 	while ((skb = skb_dequeue(&sk->sk_receive_queue)) != NULL) {
3641da177e4SLinus Torvalds 		if (skb->sk != sk) {		/* A pending connection */
3651da177e4SLinus Torvalds 			/*
3661da177e4SLinus Torvalds 			 * Queue the unaccepted socket for death
3671da177e4SLinus Torvalds 			 */
3681da177e4SLinus Torvalds 			sock_set_flag(skb->sk, SOCK_DEAD);
3691da177e4SLinus Torvalds 			x25_start_heartbeat(skb->sk);
3701da177e4SLinus Torvalds 			x25_sk(skb->sk)->state = X25_STATE_0;
3711da177e4SLinus Torvalds 		}
3721da177e4SLinus Torvalds 
3731da177e4SLinus Torvalds 		kfree_skb(skb);
3741da177e4SLinus Torvalds 	}
3751da177e4SLinus Torvalds 
376c564039fSEric Dumazet 	if (sk_has_allocations(sk)) {
3771da177e4SLinus Torvalds 		/* Defer: outstanding buffers */
3781da177e4SLinus Torvalds 		sk->sk_timer.expires  = jiffies + 10 * HZ;
3791da177e4SLinus Torvalds 		sk->sk_timer.function = x25_destroy_timer;
3801da177e4SLinus Torvalds 		sk->sk_timer.data = (unsigned long)sk;
3811da177e4SLinus Torvalds 		add_timer(&sk->sk_timer);
3821da177e4SLinus Torvalds 	} else {
3831da177e4SLinus Torvalds 		/* drop last reference so sock_put will free */
3841da177e4SLinus Torvalds 		__sock_put(sk);
3851da177e4SLinus Torvalds 	}
38614ebaf81SDavid S. Miller }
3871da177e4SLinus Torvalds 
38814ebaf81SDavid S. Miller void x25_destroy_socket_from_timer(struct sock *sk)
38914ebaf81SDavid S. Miller {
39014ebaf81SDavid S. Miller 	sock_hold(sk);
39114ebaf81SDavid S. Miller 	bh_lock_sock(sk);
39214ebaf81SDavid S. Miller 	__x25_destroy_socket(sk);
39314ebaf81SDavid S. Miller 	bh_unlock_sock(sk);
39414ebaf81SDavid S. Miller 	sock_put(sk);
39514ebaf81SDavid S. Miller }
39614ebaf81SDavid S. Miller 
39714ebaf81SDavid S. Miller static void x25_destroy_socket(struct sock *sk)
39814ebaf81SDavid S. Miller {
39914ebaf81SDavid S. Miller 	sock_hold(sk);
40014ebaf81SDavid S. Miller 	lock_sock(sk);
40114ebaf81SDavid S. Miller 	__x25_destroy_socket(sk);
4021da177e4SLinus Torvalds 	release_sock(sk);
4031da177e4SLinus Torvalds 	sock_put(sk);
4041da177e4SLinus Torvalds }
4051da177e4SLinus Torvalds 
4061da177e4SLinus Torvalds /*
4071da177e4SLinus Torvalds  *	Handling for system calls applied via the various interfaces to a
4081da177e4SLinus Torvalds  *	X.25 socket object.
4091da177e4SLinus Torvalds  */
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds static int x25_setsockopt(struct socket *sock, int level, int optname,
412b7058842SDavid S. Miller 			  char __user *optval, unsigned int optlen)
4131da177e4SLinus Torvalds {
4141da177e4SLinus Torvalds 	int opt;
4151da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4161da177e4SLinus Torvalds 	int rc = -ENOPROTOOPT;
4171da177e4SLinus Torvalds 
41891774904SArnd Bergmann 	lock_kernel();
4191da177e4SLinus Torvalds 	if (level != SOL_X25 || optname != X25_QBITINCL)
4201da177e4SLinus Torvalds 		goto out;
4211da177e4SLinus Torvalds 
4221da177e4SLinus Torvalds 	rc = -EINVAL;
4231da177e4SLinus Torvalds 	if (optlen < sizeof(int))
4241da177e4SLinus Torvalds 		goto out;
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds 	rc = -EFAULT;
4271da177e4SLinus Torvalds 	if (get_user(opt, (int __user *)optval))
4281da177e4SLinus Torvalds 		goto out;
4291da177e4SLinus Torvalds 
4301da177e4SLinus Torvalds 	x25_sk(sk)->qbitincl = !!opt;
4311da177e4SLinus Torvalds 	rc = 0;
4321da177e4SLinus Torvalds out:
43391774904SArnd Bergmann 	unlock_kernel();
4341da177e4SLinus Torvalds 	return rc;
4351da177e4SLinus Torvalds }
4361da177e4SLinus Torvalds 
4371da177e4SLinus Torvalds static int x25_getsockopt(struct socket *sock, int level, int optname,
4381da177e4SLinus Torvalds 			  char __user *optval, int __user *optlen)
4391da177e4SLinus Torvalds {
4401da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4411da177e4SLinus Torvalds 	int val, len, rc = -ENOPROTOOPT;
4421da177e4SLinus Torvalds 
44391774904SArnd Bergmann 	lock_kernel();
4441da177e4SLinus Torvalds 	if (level != SOL_X25 || optname != X25_QBITINCL)
4451da177e4SLinus Torvalds 		goto out;
4461da177e4SLinus Torvalds 
4471da177e4SLinus Torvalds 	rc = -EFAULT;
4481da177e4SLinus Torvalds 	if (get_user(len, optlen))
4491da177e4SLinus Torvalds 		goto out;
4501da177e4SLinus Torvalds 
4511da177e4SLinus Torvalds 	len = min_t(unsigned int, len, sizeof(int));
4521da177e4SLinus Torvalds 
4531da177e4SLinus Torvalds 	rc = -EINVAL;
4541da177e4SLinus Torvalds 	if (len < 0)
4551da177e4SLinus Torvalds 		goto out;
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 	rc = -EFAULT;
4581da177e4SLinus Torvalds 	if (put_user(len, optlen))
4591da177e4SLinus Torvalds 		goto out;
4601da177e4SLinus Torvalds 
4611da177e4SLinus Torvalds 	val = x25_sk(sk)->qbitincl;
4621da177e4SLinus Torvalds 	rc = copy_to_user(optval, &val, len) ? -EFAULT : 0;
4631da177e4SLinus Torvalds out:
46491774904SArnd Bergmann 	unlock_kernel();
4651da177e4SLinus Torvalds 	return rc;
4661da177e4SLinus Torvalds }
4671da177e4SLinus Torvalds 
4681da177e4SLinus Torvalds static int x25_listen(struct socket *sock, int backlog)
4691da177e4SLinus Torvalds {
4701da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
4711da177e4SLinus Torvalds 	int rc = -EOPNOTSUPP;
4721da177e4SLinus Torvalds 
47391774904SArnd Bergmann 	lock_kernel();
4741da177e4SLinus Torvalds 	if (sk->sk_state != TCP_LISTEN) {
4751da177e4SLinus Torvalds 		memset(&x25_sk(sk)->dest_addr, 0, X25_ADDR_LEN);
4761da177e4SLinus Torvalds 		sk->sk_max_ack_backlog = backlog;
4771da177e4SLinus Torvalds 		sk->sk_state           = TCP_LISTEN;
4781da177e4SLinus Torvalds 		rc = 0;
4791da177e4SLinus Torvalds 	}
48091774904SArnd Bergmann 	unlock_kernel();
4811da177e4SLinus Torvalds 
4821da177e4SLinus Torvalds 	return rc;
4831da177e4SLinus Torvalds }
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds static struct proto x25_proto = {
4861da177e4SLinus Torvalds 	.name	  = "X25",
4871da177e4SLinus Torvalds 	.owner	  = THIS_MODULE,
4881da177e4SLinus Torvalds 	.obj_size = sizeof(struct x25_sock),
4891da177e4SLinus Torvalds };
4901da177e4SLinus Torvalds 
4911b8d7ae4SEric W. Biederman static struct sock *x25_alloc_socket(struct net *net)
4921da177e4SLinus Torvalds {
4931da177e4SLinus Torvalds 	struct x25_sock *x25;
4946257ff21SPavel Emelyanov 	struct sock *sk = sk_alloc(net, AF_X25, GFP_ATOMIC, &x25_proto);
4951da177e4SLinus Torvalds 
4961da177e4SLinus Torvalds 	if (!sk)
4971da177e4SLinus Torvalds 		goto out;
4981da177e4SLinus Torvalds 
4991da177e4SLinus Torvalds 	sock_init_data(NULL, sk);
5001da177e4SLinus Torvalds 
5011da177e4SLinus Torvalds 	x25 = x25_sk(sk);
5021da177e4SLinus Torvalds 	skb_queue_head_init(&x25->ack_queue);
5031da177e4SLinus Torvalds 	skb_queue_head_init(&x25->fragment_queue);
5041da177e4SLinus Torvalds 	skb_queue_head_init(&x25->interrupt_in_queue);
5051da177e4SLinus Torvalds 	skb_queue_head_init(&x25->interrupt_out_queue);
5061da177e4SLinus Torvalds out:
5071da177e4SLinus Torvalds 	return sk;
5081da177e4SLinus Torvalds }
5091da177e4SLinus Torvalds 
5103f378b68SEric Paris static int x25_create(struct net *net, struct socket *sock, int protocol,
5113f378b68SEric Paris 		      int kern)
5121da177e4SLinus Torvalds {
5131da177e4SLinus Torvalds 	struct sock *sk;
5141da177e4SLinus Torvalds 	struct x25_sock *x25;
515*b18e7a06Sandrew hendry 	int rc = -EAFNOSUPPORT;
5161da177e4SLinus Torvalds 
51709ad9bc7SOctavian Purdila 	if (!net_eq(net, &init_net))
5181da177e4SLinus Torvalds 		goto out;
5191da177e4SLinus Torvalds 
520*b18e7a06Sandrew hendry 	rc = -ESOCKTNOSUPPORT;
521*b18e7a06Sandrew hendry 	if (sock->type != SOCK_SEQPACKET)
522*b18e7a06Sandrew hendry 		goto out;
523*b18e7a06Sandrew hendry 
524*b18e7a06Sandrew hendry 	rc = -EINVAL;
525*b18e7a06Sandrew hendry 	if (protocol)
526*b18e7a06Sandrew hendry 		goto out;
527*b18e7a06Sandrew hendry 
528*b18e7a06Sandrew hendry 	rc = -ENOBUFS;
5291b8d7ae4SEric W. Biederman 	if ((sk = x25_alloc_socket(net)) == NULL)
5301da177e4SLinus Torvalds 		goto out;
5311da177e4SLinus Torvalds 
5321da177e4SLinus Torvalds 	x25 = x25_sk(sk);
5331da177e4SLinus Torvalds 
5341da177e4SLinus Torvalds 	sock_init_data(sock, sk);
5351da177e4SLinus Torvalds 
5361da177e4SLinus Torvalds 	x25_init_timers(sk);
5371da177e4SLinus Torvalds 
5381da177e4SLinus Torvalds 	sock->ops    = &x25_proto_ops;
5391da177e4SLinus Torvalds 	sk->sk_protocol = protocol;
5401da177e4SLinus Torvalds 	sk->sk_backlog_rcv = x25_backlog_rcv;
5411da177e4SLinus Torvalds 
5421da177e4SLinus Torvalds 	x25->t21   = sysctl_x25_call_request_timeout;
5431da177e4SLinus Torvalds 	x25->t22   = sysctl_x25_reset_request_timeout;
5441da177e4SLinus Torvalds 	x25->t23   = sysctl_x25_clear_request_timeout;
5451da177e4SLinus Torvalds 	x25->t2    = sysctl_x25_ack_holdback_timeout;
5461da177e4SLinus Torvalds 	x25->state = X25_STATE_0;
547cb65d506SShaun Pereira 	x25->cudmatchlength = 0;
548ebc3f64bSShaun Pereira 	x25->accptapprv = X25_DENY_ACCPT_APPRV;		/* normally no cud  */
549ebc3f64bSShaun Pereira 							/* on call accept   */
5501da177e4SLinus Torvalds 
5511da177e4SLinus Torvalds 	x25->facilities.winsize_in  = X25_DEFAULT_WINDOW_SIZE;
5521da177e4SLinus Torvalds 	x25->facilities.winsize_out = X25_DEFAULT_WINDOW_SIZE;
5531da177e4SLinus Torvalds 	x25->facilities.pacsize_in  = X25_DEFAULT_PACKET_SIZE;
5541da177e4SLinus Torvalds 	x25->facilities.pacsize_out = X25_DEFAULT_PACKET_SIZE;
5551da177e4SLinus Torvalds 	x25->facilities.throughput  = X25_DEFAULT_THROUGHPUT;
5561da177e4SLinus Torvalds 	x25->facilities.reverse     = X25_DEFAULT_REVERSE;
557a64b7b93SShaun Pereira 	x25->dte_facilities.calling_len = 0;
558a64b7b93SShaun Pereira 	x25->dte_facilities.called_len = 0;
559a64b7b93SShaun Pereira 	memset(x25->dte_facilities.called_ae, '\0',
560a64b7b93SShaun Pereira 			sizeof(x25->dte_facilities.called_ae));
561a64b7b93SShaun Pereira 	memset(x25->dte_facilities.calling_ae, '\0',
562a64b7b93SShaun Pereira 			sizeof(x25->dte_facilities.calling_ae));
563a64b7b93SShaun Pereira 
5641da177e4SLinus Torvalds 	rc = 0;
5651da177e4SLinus Torvalds out:
5661da177e4SLinus Torvalds 	return rc;
5671da177e4SLinus Torvalds }
5681da177e4SLinus Torvalds 
5691da177e4SLinus Torvalds static struct sock *x25_make_new(struct sock *osk)
5701da177e4SLinus Torvalds {
5711da177e4SLinus Torvalds 	struct sock *sk = NULL;
5721da177e4SLinus Torvalds 	struct x25_sock *x25, *ox25;
5731da177e4SLinus Torvalds 
5741da177e4SLinus Torvalds 	if (osk->sk_type != SOCK_SEQPACKET)
5751da177e4SLinus Torvalds 		goto out;
5761da177e4SLinus Torvalds 
5773b1e0a65SYOSHIFUJI Hideaki 	if ((sk = x25_alloc_socket(sock_net(osk))) == NULL)
5781da177e4SLinus Torvalds 		goto out;
5791da177e4SLinus Torvalds 
5801da177e4SLinus Torvalds 	x25 = x25_sk(sk);
5811da177e4SLinus Torvalds 
5821da177e4SLinus Torvalds 	sk->sk_type        = osk->sk_type;
5831da177e4SLinus Torvalds 	sk->sk_priority    = osk->sk_priority;
5841da177e4SLinus Torvalds 	sk->sk_protocol    = osk->sk_protocol;
5851da177e4SLinus Torvalds 	sk->sk_rcvbuf      = osk->sk_rcvbuf;
5861da177e4SLinus Torvalds 	sk->sk_sndbuf      = osk->sk_sndbuf;
5871da177e4SLinus Torvalds 	sk->sk_state       = TCP_ESTABLISHED;
5881da177e4SLinus Torvalds 	sk->sk_backlog_rcv = osk->sk_backlog_rcv;
589a20a8554SShaun Pereira 	sock_copy_flags(sk, osk);
5901da177e4SLinus Torvalds 
5911da177e4SLinus Torvalds 	ox25 = x25_sk(osk);
5921da177e4SLinus Torvalds 	x25->t21        = ox25->t21;
5931da177e4SLinus Torvalds 	x25->t22        = ox25->t22;
5941da177e4SLinus Torvalds 	x25->t23        = ox25->t23;
5951da177e4SLinus Torvalds 	x25->t2         = ox25->t2;
5961da177e4SLinus Torvalds 	x25->facilities = ox25->facilities;
5971da177e4SLinus Torvalds 	x25->qbitincl   = ox25->qbitincl;
598a64b7b93SShaun Pereira 	x25->dte_facilities = ox25->dte_facilities;
599cb65d506SShaun Pereira 	x25->cudmatchlength = ox25->cudmatchlength;
600ebc3f64bSShaun Pereira 	x25->accptapprv = ox25->accptapprv;
6011da177e4SLinus Torvalds 
6021da177e4SLinus Torvalds 	x25_init_timers(sk);
6031da177e4SLinus Torvalds out:
6041da177e4SLinus Torvalds 	return sk;
6051da177e4SLinus Torvalds }
6061da177e4SLinus Torvalds 
6071da177e4SLinus Torvalds static int x25_release(struct socket *sock)
6081da177e4SLinus Torvalds {
6091da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
6101da177e4SLinus Torvalds 	struct x25_sock *x25;
6111da177e4SLinus Torvalds 
61291774904SArnd Bergmann 	lock_kernel();
6131da177e4SLinus Torvalds 	if (!sk)
6141da177e4SLinus Torvalds 		goto out;
6151da177e4SLinus Torvalds 
6161da177e4SLinus Torvalds 	x25 = x25_sk(sk);
6171da177e4SLinus Torvalds 
6181da177e4SLinus Torvalds 	switch (x25->state) {
6191da177e4SLinus Torvalds 
6201da177e4SLinus Torvalds 		case X25_STATE_0:
6211da177e4SLinus Torvalds 		case X25_STATE_2:
6221da177e4SLinus Torvalds 			x25_disconnect(sk, 0, 0, 0);
6231da177e4SLinus Torvalds 			x25_destroy_socket(sk);
6241da177e4SLinus Torvalds 			goto out;
6251da177e4SLinus Torvalds 
6261da177e4SLinus Torvalds 		case X25_STATE_1:
6271da177e4SLinus Torvalds 		case X25_STATE_3:
6281da177e4SLinus Torvalds 		case X25_STATE_4:
6291da177e4SLinus Torvalds 			x25_clear_queues(sk);
6301da177e4SLinus Torvalds 			x25_write_internal(sk, X25_CLEAR_REQUEST);
6311da177e4SLinus Torvalds 			x25_start_t23timer(sk);
6321da177e4SLinus Torvalds 			x25->state = X25_STATE_2;
6331da177e4SLinus Torvalds 			sk->sk_state	= TCP_CLOSE;
6341da177e4SLinus Torvalds 			sk->sk_shutdown	|= SEND_SHUTDOWN;
6351da177e4SLinus Torvalds 			sk->sk_state_change(sk);
6361da177e4SLinus Torvalds 			sock_set_flag(sk, SOCK_DEAD);
6371da177e4SLinus Torvalds 			sock_set_flag(sk, SOCK_DESTROY);
6381da177e4SLinus Torvalds 			break;
6391da177e4SLinus Torvalds 	}
6401da177e4SLinus Torvalds 
641c751e4f8SDavid S. Miller 	sock_orphan(sk);
6421da177e4SLinus Torvalds out:
64391774904SArnd Bergmann 	unlock_kernel();
6441da177e4SLinus Torvalds 	return 0;
6451da177e4SLinus Torvalds }
6461da177e4SLinus Torvalds 
6471da177e4SLinus Torvalds static int x25_bind(struct socket *sock, struct sockaddr *uaddr, int addr_len)
6481da177e4SLinus Torvalds {
6491da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
6501da177e4SLinus Torvalds 	struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
65191774904SArnd Bergmann 	int rc = 0;
6521da177e4SLinus Torvalds 
65391774904SArnd Bergmann 	lock_kernel();
6541da177e4SLinus Torvalds 	if (!sock_flag(sk, SOCK_ZAPPED) ||
6551da177e4SLinus Torvalds 	    addr_len != sizeof(struct sockaddr_x25) ||
65691774904SArnd Bergmann 	    addr->sx25_family != AF_X25) {
65791774904SArnd Bergmann 		rc = -EINVAL;
65891774904SArnd Bergmann 		goto out;
65991774904SArnd Bergmann 	}
6601da177e4SLinus Torvalds 
6611da177e4SLinus Torvalds 	x25_sk(sk)->source_addr = addr->sx25_addr;
6621da177e4SLinus Torvalds 	x25_insert_socket(sk);
6631da177e4SLinus Torvalds 	sock_reset_flag(sk, SOCK_ZAPPED);
6641da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_bind: socket is bound\n");
66591774904SArnd Bergmann out:
66691774904SArnd Bergmann 	unlock_kernel();
66791774904SArnd Bergmann 	return rc;
6681da177e4SLinus Torvalds }
6691da177e4SLinus Torvalds 
6701da177e4SLinus Torvalds static int x25_wait_for_connection_establishment(struct sock *sk)
6711da177e4SLinus Torvalds {
6721da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
6731da177e4SLinus Torvalds 	int rc;
6741da177e4SLinus Torvalds 
6751da177e4SLinus Torvalds 	add_wait_queue_exclusive(sk->sk_sleep, &wait);
6761da177e4SLinus Torvalds 	for (;;) {
6771da177e4SLinus Torvalds 		__set_current_state(TASK_INTERRUPTIBLE);
6781da177e4SLinus Torvalds 		rc = -ERESTARTSYS;
6791da177e4SLinus Torvalds 		if (signal_pending(current))
6801da177e4SLinus Torvalds 			break;
6811da177e4SLinus Torvalds 		rc = sock_error(sk);
6821da177e4SLinus Torvalds 		if (rc) {
6831da177e4SLinus Torvalds 			sk->sk_socket->state = SS_UNCONNECTED;
6841da177e4SLinus Torvalds 			break;
6851da177e4SLinus Torvalds 		}
6861da177e4SLinus Torvalds 		rc = 0;
6871da177e4SLinus Torvalds 		if (sk->sk_state != TCP_ESTABLISHED) {
6881da177e4SLinus Torvalds 			release_sock(sk);
6891da177e4SLinus Torvalds 			schedule();
6901da177e4SLinus Torvalds 			lock_sock(sk);
6911da177e4SLinus Torvalds 		} else
6921da177e4SLinus Torvalds 			break;
6931da177e4SLinus Torvalds 	}
6941da177e4SLinus Torvalds 	__set_current_state(TASK_RUNNING);
6951da177e4SLinus Torvalds 	remove_wait_queue(sk->sk_sleep, &wait);
6961da177e4SLinus Torvalds 	return rc;
6971da177e4SLinus Torvalds }
6981da177e4SLinus Torvalds 
6991da177e4SLinus Torvalds static int x25_connect(struct socket *sock, struct sockaddr *uaddr,
7001da177e4SLinus Torvalds 		       int addr_len, int flags)
7011da177e4SLinus Torvalds {
7021da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
7031da177e4SLinus Torvalds 	struct x25_sock *x25 = x25_sk(sk);
7041da177e4SLinus Torvalds 	struct sockaddr_x25 *addr = (struct sockaddr_x25 *)uaddr;
7051da177e4SLinus Torvalds 	struct x25_route *rt;
7061da177e4SLinus Torvalds 	int rc = 0;
7071da177e4SLinus Torvalds 
70891774904SArnd Bergmann 	lock_kernel();
7091da177e4SLinus Torvalds 	lock_sock(sk);
7101da177e4SLinus Torvalds 	if (sk->sk_state == TCP_ESTABLISHED && sock->state == SS_CONNECTING) {
7111da177e4SLinus Torvalds 		sock->state = SS_CONNECTED;
7121da177e4SLinus Torvalds 		goto out; /* Connect completed during a ERESTARTSYS event */
7131da177e4SLinus Torvalds 	}
7141da177e4SLinus Torvalds 
7151da177e4SLinus Torvalds 	rc = -ECONNREFUSED;
7161da177e4SLinus Torvalds 	if (sk->sk_state == TCP_CLOSE && sock->state == SS_CONNECTING) {
7171da177e4SLinus Torvalds 		sock->state = SS_UNCONNECTED;
7181da177e4SLinus Torvalds 		goto out;
7191da177e4SLinus Torvalds 	}
7201da177e4SLinus Torvalds 
7211da177e4SLinus Torvalds 	rc = -EISCONN;	/* No reconnect on a seqpacket socket */
7221da177e4SLinus Torvalds 	if (sk->sk_state == TCP_ESTABLISHED)
7231da177e4SLinus Torvalds 		goto out;
7241da177e4SLinus Torvalds 
7251da177e4SLinus Torvalds 	sk->sk_state   = TCP_CLOSE;
7261da177e4SLinus Torvalds 	sock->state = SS_UNCONNECTED;
7271da177e4SLinus Torvalds 
7281da177e4SLinus Torvalds 	rc = -EINVAL;
7291da177e4SLinus Torvalds 	if (addr_len != sizeof(struct sockaddr_x25) ||
7301da177e4SLinus Torvalds 	    addr->sx25_family != AF_X25)
7311da177e4SLinus Torvalds 		goto out;
7321da177e4SLinus Torvalds 
7331da177e4SLinus Torvalds 	rc = -ENETUNREACH;
7341da177e4SLinus Torvalds 	rt = x25_get_route(&addr->sx25_addr);
7351da177e4SLinus Torvalds 	if (!rt)
7361da177e4SLinus Torvalds 		goto out;
7371da177e4SLinus Torvalds 
7381da177e4SLinus Torvalds 	x25->neighbour = x25_get_neigh(rt->dev);
7391da177e4SLinus Torvalds 	if (!x25->neighbour)
7401da177e4SLinus Torvalds 		goto out_put_route;
7411da177e4SLinus Torvalds 
7421da177e4SLinus Torvalds 	x25_limit_facilities(&x25->facilities, x25->neighbour);
7431da177e4SLinus Torvalds 
7441da177e4SLinus Torvalds 	x25->lci = x25_new_lci(x25->neighbour);
7451da177e4SLinus Torvalds 	if (!x25->lci)
7461da177e4SLinus Torvalds 		goto out_put_neigh;
7471da177e4SLinus Torvalds 
7481da177e4SLinus Torvalds 	rc = -EINVAL;
7491da177e4SLinus Torvalds 	if (sock_flag(sk, SOCK_ZAPPED)) /* Must bind first - autobinding does not work */
7501da177e4SLinus Torvalds 		goto out_put_neigh;
7511da177e4SLinus Torvalds 
7521da177e4SLinus Torvalds 	if (!strcmp(x25->source_addr.x25_addr, null_x25_address.x25_addr))
7531da177e4SLinus Torvalds 		memset(&x25->source_addr, '\0', X25_ADDR_LEN);
7541da177e4SLinus Torvalds 
7551da177e4SLinus Torvalds 	x25->dest_addr = addr->sx25_addr;
7561da177e4SLinus Torvalds 
7571da177e4SLinus Torvalds 	/* Move to connecting socket, start sending Connect Requests */
7581da177e4SLinus Torvalds 	sock->state   = SS_CONNECTING;
7591da177e4SLinus Torvalds 	sk->sk_state  = TCP_SYN_SENT;
7601da177e4SLinus Torvalds 
7611da177e4SLinus Torvalds 	x25->state = X25_STATE_1;
7621da177e4SLinus Torvalds 
7631da177e4SLinus Torvalds 	x25_write_internal(sk, X25_CALL_REQUEST);
7641da177e4SLinus Torvalds 
7651da177e4SLinus Torvalds 	x25_start_heartbeat(sk);
7661da177e4SLinus Torvalds 	x25_start_t21timer(sk);
7671da177e4SLinus Torvalds 
7681da177e4SLinus Torvalds 	/* Now the loop */
7691da177e4SLinus Torvalds 	rc = -EINPROGRESS;
7701da177e4SLinus Torvalds 	if (sk->sk_state != TCP_ESTABLISHED && (flags & O_NONBLOCK))
7711da177e4SLinus Torvalds 		goto out_put_neigh;
7721da177e4SLinus Torvalds 
7731da177e4SLinus Torvalds 	rc = x25_wait_for_connection_establishment(sk);
7741da177e4SLinus Torvalds 	if (rc)
7751da177e4SLinus Torvalds 		goto out_put_neigh;
7761da177e4SLinus Torvalds 
7771da177e4SLinus Torvalds 	sock->state = SS_CONNECTED;
7781da177e4SLinus Torvalds 	rc = 0;
7791da177e4SLinus Torvalds out_put_neigh:
7801da177e4SLinus Torvalds 	if (rc)
7811da177e4SLinus Torvalds 		x25_neigh_put(x25->neighbour);
7821da177e4SLinus Torvalds out_put_route:
7831da177e4SLinus Torvalds 	x25_route_put(rt);
7841da177e4SLinus Torvalds out:
7851da177e4SLinus Torvalds 	release_sock(sk);
78691774904SArnd Bergmann 	unlock_kernel();
7871da177e4SLinus Torvalds 	return rc;
7881da177e4SLinus Torvalds }
7891da177e4SLinus Torvalds 
790bac37ec8SShaun Pereira static int x25_wait_for_data(struct sock *sk, long timeout)
7911da177e4SLinus Torvalds {
7921da177e4SLinus Torvalds 	DECLARE_WAITQUEUE(wait, current);
7931da177e4SLinus Torvalds 	int rc = 0;
7941da177e4SLinus Torvalds 
7951da177e4SLinus Torvalds 	add_wait_queue_exclusive(sk->sk_sleep, &wait);
7961da177e4SLinus Torvalds 	for (;;) {
7971da177e4SLinus Torvalds 		__set_current_state(TASK_INTERRUPTIBLE);
7981da177e4SLinus Torvalds 		if (sk->sk_shutdown & RCV_SHUTDOWN)
7991da177e4SLinus Torvalds 			break;
8001da177e4SLinus Torvalds 		rc = -ERESTARTSYS;
8011da177e4SLinus Torvalds 		if (signal_pending(current))
8021da177e4SLinus Torvalds 			break;
8031da177e4SLinus Torvalds 		rc = -EAGAIN;
8041da177e4SLinus Torvalds 		if (!timeout)
8051da177e4SLinus Torvalds 			break;
8061da177e4SLinus Torvalds 		rc = 0;
8071da177e4SLinus Torvalds 		if (skb_queue_empty(&sk->sk_receive_queue)) {
8081da177e4SLinus Torvalds 			release_sock(sk);
8091da177e4SLinus Torvalds 			timeout = schedule_timeout(timeout);
8101da177e4SLinus Torvalds 			lock_sock(sk);
8111da177e4SLinus Torvalds 		} else
8121da177e4SLinus Torvalds 			break;
8131da177e4SLinus Torvalds 	}
8141da177e4SLinus Torvalds 	__set_current_state(TASK_RUNNING);
8151da177e4SLinus Torvalds 	remove_wait_queue(sk->sk_sleep, &wait);
8161da177e4SLinus Torvalds 	return rc;
8171da177e4SLinus Torvalds }
8181da177e4SLinus Torvalds 
8191da177e4SLinus Torvalds static int x25_accept(struct socket *sock, struct socket *newsock, int flags)
8201da177e4SLinus Torvalds {
8211da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
8221da177e4SLinus Torvalds 	struct sock *newsk;
8231da177e4SLinus Torvalds 	struct sk_buff *skb;
8241da177e4SLinus Torvalds 	int rc = -EINVAL;
8251da177e4SLinus Torvalds 
82691774904SArnd Bergmann 	lock_kernel();
8271da177e4SLinus Torvalds 	if (!sk || sk->sk_state != TCP_LISTEN)
8281da177e4SLinus Torvalds 		goto out;
8291da177e4SLinus Torvalds 
8301da177e4SLinus Torvalds 	rc = -EOPNOTSUPP;
8311da177e4SLinus Torvalds 	if (sk->sk_type != SOCK_SEQPACKET)
8321da177e4SLinus Torvalds 		goto out;
8331da177e4SLinus Torvalds 
8341da177e4SLinus Torvalds 	lock_sock(sk);
8351da177e4SLinus Torvalds 	rc = x25_wait_for_data(sk, sk->sk_rcvtimeo);
8361da177e4SLinus Torvalds 	if (rc)
8371da177e4SLinus Torvalds 		goto out2;
8381da177e4SLinus Torvalds 	skb = skb_dequeue(&sk->sk_receive_queue);
8391da177e4SLinus Torvalds 	rc = -EINVAL;
8401da177e4SLinus Torvalds 	if (!skb->sk)
8411da177e4SLinus Torvalds 		goto out2;
8421da177e4SLinus Torvalds 	newsk		 = skb->sk;
843b61d38e0SDavid S. Miller 	sock_graft(newsk, newsock);
8441da177e4SLinus Torvalds 
8451da177e4SLinus Torvalds 	/* Now attach up the new socket */
8461da177e4SLinus Torvalds 	skb->sk = NULL;
8471da177e4SLinus Torvalds 	kfree_skb(skb);
8481da177e4SLinus Torvalds 	sk->sk_ack_backlog--;
8491da177e4SLinus Torvalds 	newsock->state = SS_CONNECTED;
8501da177e4SLinus Torvalds 	rc = 0;
8511da177e4SLinus Torvalds out2:
8521da177e4SLinus Torvalds 	release_sock(sk);
8531da177e4SLinus Torvalds out:
85491774904SArnd Bergmann 	unlock_kernel();
8551da177e4SLinus Torvalds 	return rc;
8561da177e4SLinus Torvalds }
8571da177e4SLinus Torvalds 
8581da177e4SLinus Torvalds static int x25_getname(struct socket *sock, struct sockaddr *uaddr,
8591da177e4SLinus Torvalds 		       int *uaddr_len, int peer)
8601da177e4SLinus Torvalds {
8611da177e4SLinus Torvalds 	struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)uaddr;
8621da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
8631da177e4SLinus Torvalds 	struct x25_sock *x25 = x25_sk(sk);
86491774904SArnd Bergmann 	int rc = 0;
8651da177e4SLinus Torvalds 
86691774904SArnd Bergmann 	lock_kernel();
8671da177e4SLinus Torvalds 	if (peer) {
86891774904SArnd Bergmann 		if (sk->sk_state != TCP_ESTABLISHED) {
86991774904SArnd Bergmann 			rc = -ENOTCONN;
87091774904SArnd Bergmann 			goto out;
87191774904SArnd Bergmann 		}
8721da177e4SLinus Torvalds 		sx25->sx25_addr = x25->dest_addr;
8731da177e4SLinus Torvalds 	} else
8741da177e4SLinus Torvalds 		sx25->sx25_addr = x25->source_addr;
8751da177e4SLinus Torvalds 
8761da177e4SLinus Torvalds 	sx25->sx25_family = AF_X25;
8771da177e4SLinus Torvalds 	*uaddr_len = sizeof(*sx25);
8781da177e4SLinus Torvalds 
87991774904SArnd Bergmann out:
88091774904SArnd Bergmann 	unlock_kernel();
88191774904SArnd Bergmann 	return rc;
88291774904SArnd Bergmann }
88391774904SArnd Bergmann 
88491774904SArnd Bergmann static unsigned int x25_datagram_poll(struct file *file, struct socket *sock,
88591774904SArnd Bergmann 			   poll_table *wait)
88691774904SArnd Bergmann {
88791774904SArnd Bergmann 	int rc;
88891774904SArnd Bergmann 
88991774904SArnd Bergmann 	lock_kernel();
89091774904SArnd Bergmann 	rc = datagram_poll(file, sock, wait);
89191774904SArnd Bergmann 	unlock_kernel();
89291774904SArnd Bergmann 
89391774904SArnd Bergmann 	return rc;
8941da177e4SLinus Torvalds }
8951da177e4SLinus Torvalds 
8961da177e4SLinus Torvalds int x25_rx_call_request(struct sk_buff *skb, struct x25_neigh *nb,
8971da177e4SLinus Torvalds 			unsigned int lci)
8981da177e4SLinus Torvalds {
8991da177e4SLinus Torvalds 	struct sock *sk;
9001da177e4SLinus Torvalds 	struct sock *make;
9011da177e4SLinus Torvalds 	struct x25_sock *makex25;
9021da177e4SLinus Torvalds 	struct x25_address source_addr, dest_addr;
9031da177e4SLinus Torvalds 	struct x25_facilities facilities;
904a64b7b93SShaun Pereira 	struct x25_dte_facilities dte_facilities;
90595a9dc43SAndrew Hendry 	int len, addr_len, rc;
9061da177e4SLinus Torvalds 
9071da177e4SLinus Torvalds 	/*
9081da177e4SLinus Torvalds 	 *	Remove the LCI and frame type.
9091da177e4SLinus Torvalds 	 */
9101da177e4SLinus Torvalds 	skb_pull(skb, X25_STD_MIN_LEN);
9111da177e4SLinus Torvalds 
9121da177e4SLinus Torvalds 	/*
9131da177e4SLinus Torvalds 	 *	Extract the X.25 addresses and convert them to ASCII strings,
9141da177e4SLinus Torvalds 	 *	and remove them.
9151da177e4SLinus Torvalds 	 */
91695a9dc43SAndrew Hendry 	addr_len = x25_addr_ntoa(skb->data, &source_addr, &dest_addr);
91795a9dc43SAndrew Hendry 	skb_pull(skb, addr_len);
9181da177e4SLinus Torvalds 
9191da177e4SLinus Torvalds 	/*
9201da177e4SLinus Torvalds 	 *	Get the length of the facilities, skip past them for the moment
9211da177e4SLinus Torvalds 	 *	get the call user data because this is needed to determine
9221da177e4SLinus Torvalds 	 *	the correct listener
9231da177e4SLinus Torvalds 	 */
9241da177e4SLinus Torvalds 	len = skb->data[0] + 1;
9251da177e4SLinus Torvalds 	skb_pull(skb,len);
9261da177e4SLinus Torvalds 
9271da177e4SLinus Torvalds 	/*
9281da177e4SLinus Torvalds 	 *	Find a listener for the particular address/cud pair.
9291da177e4SLinus Torvalds 	 */
930cb65d506SShaun Pereira 	sk = x25_find_listener(&source_addr,skb);
931cb65d506SShaun Pereira 	skb_push(skb,len);
9321da177e4SLinus Torvalds 
93395a9dc43SAndrew Hendry 	if (sk != NULL && sk_acceptq_is_full(sk)) {
93495a9dc43SAndrew Hendry 		goto out_sock_put;
93595a9dc43SAndrew Hendry 	}
93695a9dc43SAndrew Hendry 
9371da177e4SLinus Torvalds 	/*
93895a9dc43SAndrew Hendry 	 *	We dont have any listeners for this incoming call.
93995a9dc43SAndrew Hendry 	 *	Try forwarding it.
9401da177e4SLinus Torvalds 	 */
94195a9dc43SAndrew Hendry 	if (sk == NULL) {
94295a9dc43SAndrew Hendry 		skb_push(skb, addr_len + X25_STD_MIN_LEN);
94339e21c0dSAndrew Hendry 		if (sysctl_x25_forward &&
94439e21c0dSAndrew Hendry 				x25_forward_call(&dest_addr, nb, skb, lci) > 0)
94595a9dc43SAndrew Hendry 		{
94695a9dc43SAndrew Hendry 			/* Call was forwarded, dont process it any more */
94795a9dc43SAndrew Hendry 			kfree_skb(skb);
94895a9dc43SAndrew Hendry 			rc = 1;
94995a9dc43SAndrew Hendry 			goto out;
95095a9dc43SAndrew Hendry 		} else {
95195a9dc43SAndrew Hendry 			/* No listeners, can't forward, clear the call */
9521da177e4SLinus Torvalds 			goto out_clear_request;
95395a9dc43SAndrew Hendry 		}
95495a9dc43SAndrew Hendry 	}
9551da177e4SLinus Torvalds 
9561da177e4SLinus Torvalds 	/*
9571da177e4SLinus Torvalds 	 *	Try to reach a compromise on the requested facilities.
9581da177e4SLinus Torvalds 	 */
959a64b7b93SShaun Pereira 	len = x25_negotiate_facilities(skb, sk, &facilities, &dte_facilities);
960a64b7b93SShaun Pereira 	if (len == -1)
9611da177e4SLinus Torvalds 		goto out_sock_put;
9621da177e4SLinus Torvalds 
9631da177e4SLinus Torvalds 	/*
9641da177e4SLinus Torvalds 	 * current neighbour/link might impose additional limits
9651da177e4SLinus Torvalds 	 * on certain facilties
9661da177e4SLinus Torvalds 	 */
9671da177e4SLinus Torvalds 
9681da177e4SLinus Torvalds 	x25_limit_facilities(&facilities, nb);
9691da177e4SLinus Torvalds 
9701da177e4SLinus Torvalds 	/*
9711da177e4SLinus Torvalds 	 *	Try to create a new socket.
9721da177e4SLinus Torvalds 	 */
9731da177e4SLinus Torvalds 	make = x25_make_new(sk);
9741da177e4SLinus Torvalds 	if (!make)
9751da177e4SLinus Torvalds 		goto out_sock_put;
9761da177e4SLinus Torvalds 
9771da177e4SLinus Torvalds 	/*
9781da177e4SLinus Torvalds 	 *	Remove the facilities
9791da177e4SLinus Torvalds 	 */
9801da177e4SLinus Torvalds 	skb_pull(skb, len);
9811da177e4SLinus Torvalds 
9821da177e4SLinus Torvalds 	skb->sk     = make;
9831da177e4SLinus Torvalds 	make->sk_state = TCP_ESTABLISHED;
9841da177e4SLinus Torvalds 
9851da177e4SLinus Torvalds 	makex25 = x25_sk(make);
9861da177e4SLinus Torvalds 	makex25->lci           = lci;
9871da177e4SLinus Torvalds 	makex25->dest_addr     = dest_addr;
9881da177e4SLinus Torvalds 	makex25->source_addr   = source_addr;
9891da177e4SLinus Torvalds 	makex25->neighbour     = nb;
9901da177e4SLinus Torvalds 	makex25->facilities    = facilities;
991a64b7b93SShaun Pereira 	makex25->dte_facilities= dte_facilities;
9921da177e4SLinus Torvalds 	makex25->vc_facil_mask = x25_sk(sk)->vc_facil_mask;
993cb65d506SShaun Pereira 	/* ensure no reverse facil on accept */
994cb65d506SShaun Pereira 	makex25->vc_facil_mask &= ~X25_MASK_REVERSE;
995a64b7b93SShaun Pereira 	/* ensure no calling address extension on accept */
996a64b7b93SShaun Pereira 	makex25->vc_facil_mask &= ~X25_MASK_CALLING_AE;
997cb65d506SShaun Pereira 	makex25->cudmatchlength = x25_sk(sk)->cudmatchlength;
9981da177e4SLinus Torvalds 
999ebc3f64bSShaun Pereira 	/* Normally all calls are accepted immediatly */
1000ebc3f64bSShaun Pereira 	if(makex25->accptapprv & X25_DENY_ACCPT_APPRV) {
10011da177e4SLinus Torvalds 		x25_write_internal(make, X25_CALL_ACCEPTED);
10021da177e4SLinus Torvalds 		makex25->state = X25_STATE_3;
1003ebc3f64bSShaun Pereira 	}
10041da177e4SLinus Torvalds 
1005cb65d506SShaun Pereira 	/*
1006cb65d506SShaun Pereira 	 *	Incoming Call User Data.
1007cb65d506SShaun Pereira 	 */
1008d626f62bSArnaldo Carvalho de Melo 	skb_copy_from_linear_data(skb, makex25->calluserdata.cuddata, skb->len);
1009cb65d506SShaun Pereira 	makex25->calluserdata.cudlength = skb->len;
1010cb65d506SShaun Pereira 
10111da177e4SLinus Torvalds 	sk->sk_ack_backlog++;
10121da177e4SLinus Torvalds 
10131da177e4SLinus Torvalds 	x25_insert_socket(make);
10141da177e4SLinus Torvalds 
10151da177e4SLinus Torvalds 	skb_queue_head(&sk->sk_receive_queue, skb);
10161da177e4SLinus Torvalds 
10171da177e4SLinus Torvalds 	x25_start_heartbeat(make);
10181da177e4SLinus Torvalds 
10191da177e4SLinus Torvalds 	if (!sock_flag(sk, SOCK_DEAD))
10201da177e4SLinus Torvalds 		sk->sk_data_ready(sk, skb->len);
10211da177e4SLinus Torvalds 	rc = 1;
10221da177e4SLinus Torvalds 	sock_put(sk);
10231da177e4SLinus Torvalds out:
10241da177e4SLinus Torvalds 	return rc;
10251da177e4SLinus Torvalds out_sock_put:
10261da177e4SLinus Torvalds 	sock_put(sk);
10271da177e4SLinus Torvalds out_clear_request:
10281da177e4SLinus Torvalds 	rc = 0;
10291da177e4SLinus Torvalds 	x25_transmit_clear_request(nb, lci, 0x01);
10301da177e4SLinus Torvalds 	goto out;
10311da177e4SLinus Torvalds }
10321da177e4SLinus Torvalds 
10331da177e4SLinus Torvalds static int x25_sendmsg(struct kiocb *iocb, struct socket *sock,
10341da177e4SLinus Torvalds 		       struct msghdr *msg, size_t len)
10351da177e4SLinus Torvalds {
10361da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
10371da177e4SLinus Torvalds 	struct x25_sock *x25 = x25_sk(sk);
10381da177e4SLinus Torvalds 	struct sockaddr_x25 *usx25 = (struct sockaddr_x25 *)msg->msg_name;
10391da177e4SLinus Torvalds 	struct sockaddr_x25 sx25;
10401da177e4SLinus Torvalds 	struct sk_buff *skb;
10411da177e4SLinus Torvalds 	unsigned char *asmptr;
10421da177e4SLinus Torvalds 	int noblock = msg->msg_flags & MSG_DONTWAIT;
10431da177e4SLinus Torvalds 	size_t size;
10441da177e4SLinus Torvalds 	int qbit = 0, rc = -EINVAL;
10451da177e4SLinus Torvalds 
104691774904SArnd Bergmann 	lock_kernel();
10471da177e4SLinus Torvalds 	if (msg->msg_flags & ~(MSG_DONTWAIT|MSG_OOB|MSG_EOR|MSG_CMSG_COMPAT))
10481da177e4SLinus Torvalds 		goto out;
10491da177e4SLinus Torvalds 
10501da177e4SLinus Torvalds 	/* we currently don't support segmented records at the user interface */
10511da177e4SLinus Torvalds 	if (!(msg->msg_flags & (MSG_EOR|MSG_OOB)))
10521da177e4SLinus Torvalds 		goto out;
10531da177e4SLinus Torvalds 
10541da177e4SLinus Torvalds 	rc = -EADDRNOTAVAIL;
10551da177e4SLinus Torvalds 	if (sock_flag(sk, SOCK_ZAPPED))
10561da177e4SLinus Torvalds 		goto out;
10571da177e4SLinus Torvalds 
10581da177e4SLinus Torvalds 	rc = -EPIPE;
10591da177e4SLinus Torvalds 	if (sk->sk_shutdown & SEND_SHUTDOWN) {
10601da177e4SLinus Torvalds 		send_sig(SIGPIPE, current, 0);
10611da177e4SLinus Torvalds 		goto out;
10621da177e4SLinus Torvalds 	}
10631da177e4SLinus Torvalds 
10641da177e4SLinus Torvalds 	rc = -ENETUNREACH;
10651da177e4SLinus Torvalds 	if (!x25->neighbour)
10661da177e4SLinus Torvalds 		goto out;
10671da177e4SLinus Torvalds 
10681da177e4SLinus Torvalds 	if (usx25) {
10691da177e4SLinus Torvalds 		rc = -EINVAL;
10701da177e4SLinus Torvalds 		if (msg->msg_namelen < sizeof(sx25))
10711da177e4SLinus Torvalds 			goto out;
10721da177e4SLinus Torvalds 		memcpy(&sx25, usx25, sizeof(sx25));
10731da177e4SLinus Torvalds 		rc = -EISCONN;
10741da177e4SLinus Torvalds 		if (strcmp(x25->dest_addr.x25_addr, sx25.sx25_addr.x25_addr))
10751da177e4SLinus Torvalds 			goto out;
10761da177e4SLinus Torvalds 		rc = -EINVAL;
10771da177e4SLinus Torvalds 		if (sx25.sx25_family != AF_X25)
10781da177e4SLinus Torvalds 			goto out;
10791da177e4SLinus Torvalds 	} else {
10801da177e4SLinus Torvalds 		/*
10811da177e4SLinus Torvalds 		 *	FIXME 1003.1g - if the socket is like this because
10821da177e4SLinus Torvalds 		 *	it has become closed (not started closed) we ought
10831da177e4SLinus Torvalds 		 *	to SIGPIPE, EPIPE;
10841da177e4SLinus Torvalds 		 */
10851da177e4SLinus Torvalds 		rc = -ENOTCONN;
10861da177e4SLinus Torvalds 		if (sk->sk_state != TCP_ESTABLISHED)
10871da177e4SLinus Torvalds 			goto out;
10881da177e4SLinus Torvalds 
10891da177e4SLinus Torvalds 		sx25.sx25_family = AF_X25;
10901da177e4SLinus Torvalds 		sx25.sx25_addr   = x25->dest_addr;
10911da177e4SLinus Torvalds 	}
10921da177e4SLinus Torvalds 
109383e0bbcbSAlan Cox 	/* Sanity check the packet size */
109483e0bbcbSAlan Cox 	if (len > 65535) {
109583e0bbcbSAlan Cox 		rc = -EMSGSIZE;
109683e0bbcbSAlan Cox 		goto out;
109783e0bbcbSAlan Cox 	}
109883e0bbcbSAlan Cox 
10991da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: sendto: Addresses built.\n");
11001da177e4SLinus Torvalds 
11011da177e4SLinus Torvalds 	/* Build a packet */
11021da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: sendto: building packet.\n");
11031da177e4SLinus Torvalds 
11041da177e4SLinus Torvalds 	if ((msg->msg_flags & MSG_OOB) && len > 32)
11051da177e4SLinus Torvalds 		len = 32;
11061da177e4SLinus Torvalds 
11071da177e4SLinus Torvalds 	size = len + X25_MAX_L2_LEN + X25_EXT_MIN_LEN;
11081da177e4SLinus Torvalds 
11091da177e4SLinus Torvalds 	skb = sock_alloc_send_skb(sk, size, noblock, &rc);
11101da177e4SLinus Torvalds 	if (!skb)
11111da177e4SLinus Torvalds 		goto out;
11121da177e4SLinus Torvalds 	X25_SKB_CB(skb)->flags = msg->msg_flags;
11131da177e4SLinus Torvalds 
11141da177e4SLinus Torvalds 	skb_reserve(skb, X25_MAX_L2_LEN + X25_EXT_MIN_LEN);
11151da177e4SLinus Torvalds 
11161da177e4SLinus Torvalds 	/*
11171da177e4SLinus Torvalds 	 *	Put the data on the end
11181da177e4SLinus Torvalds 	 */
11191da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: Copying user data\n");
11201da177e4SLinus Torvalds 
1121eeeb0374SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
1122eeeb0374SArnaldo Carvalho de Melo 	skb_put(skb, len);
11231da177e4SLinus Torvalds 
1124eeeb0374SArnaldo Carvalho de Melo 	rc = memcpy_fromiovec(skb_transport_header(skb), msg->msg_iov, len);
11251da177e4SLinus Torvalds 	if (rc)
11261da177e4SLinus Torvalds 		goto out_kfree_skb;
11271da177e4SLinus Torvalds 
11281da177e4SLinus Torvalds 	/*
11291da177e4SLinus Torvalds 	 *	If the Q BIT Include socket option is in force, the first
11301da177e4SLinus Torvalds 	 *	byte of the user data is the logical value of the Q Bit.
11311da177e4SLinus Torvalds 	 */
11321da177e4SLinus Torvalds 	if (x25->qbitincl) {
11331da177e4SLinus Torvalds 		qbit = skb->data[0];
11341da177e4SLinus Torvalds 		skb_pull(skb, 1);
11351da177e4SLinus Torvalds 	}
11361da177e4SLinus Torvalds 
11371da177e4SLinus Torvalds 	/*
11381da177e4SLinus Torvalds 	 *	Push down the X.25 header
11391da177e4SLinus Torvalds 	 */
11401da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: Building X.25 Header.\n");
11411da177e4SLinus Torvalds 
11421da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_OOB) {
11431da177e4SLinus Torvalds 		if (x25->neighbour->extended) {
11441da177e4SLinus Torvalds 			asmptr    = skb_push(skb, X25_STD_MIN_LEN);
11451da177e4SLinus Torvalds 			*asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
11461da177e4SLinus Torvalds 			*asmptr++ = (x25->lci >> 0) & 0xFF;
11471da177e4SLinus Torvalds 			*asmptr++ = X25_INTERRUPT;
11481da177e4SLinus Torvalds 		} else {
11491da177e4SLinus Torvalds 			asmptr    = skb_push(skb, X25_STD_MIN_LEN);
11501da177e4SLinus Torvalds 			*asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
11511da177e4SLinus Torvalds 			*asmptr++ = (x25->lci >> 0) & 0xFF;
11521da177e4SLinus Torvalds 			*asmptr++ = X25_INTERRUPT;
11531da177e4SLinus Torvalds 		}
11541da177e4SLinus Torvalds 	} else {
11551da177e4SLinus Torvalds 		if (x25->neighbour->extended) {
11561da177e4SLinus Torvalds 			/* Build an Extended X.25 header */
11571da177e4SLinus Torvalds 			asmptr    = skb_push(skb, X25_EXT_MIN_LEN);
11581da177e4SLinus Torvalds 			*asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_EXTSEQ;
11591da177e4SLinus Torvalds 			*asmptr++ = (x25->lci >> 0) & 0xFF;
11601da177e4SLinus Torvalds 			*asmptr++ = X25_DATA;
11611da177e4SLinus Torvalds 			*asmptr++ = X25_DATA;
11621da177e4SLinus Torvalds 		} else {
11631da177e4SLinus Torvalds 			/* Build an Standard X.25 header */
11641da177e4SLinus Torvalds 			asmptr    = skb_push(skb, X25_STD_MIN_LEN);
11651da177e4SLinus Torvalds 			*asmptr++ = ((x25->lci >> 8) & 0x0F) | X25_GFI_STDSEQ;
11661da177e4SLinus Torvalds 			*asmptr++ = (x25->lci >> 0) & 0xFF;
11671da177e4SLinus Torvalds 			*asmptr++ = X25_DATA;
11681da177e4SLinus Torvalds 		}
11691da177e4SLinus Torvalds 
11701da177e4SLinus Torvalds 		if (qbit)
11711da177e4SLinus Torvalds 			skb->data[0] |= X25_Q_BIT;
11721da177e4SLinus Torvalds 	}
11731da177e4SLinus Torvalds 
11741da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: Built header.\n");
11751da177e4SLinus Torvalds 	SOCK_DEBUG(sk, "x25_sendmsg: Transmitting buffer\n");
11761da177e4SLinus Torvalds 
11771da177e4SLinus Torvalds 	rc = -ENOTCONN;
11781da177e4SLinus Torvalds 	if (sk->sk_state != TCP_ESTABLISHED)
11791da177e4SLinus Torvalds 		goto out_kfree_skb;
11801da177e4SLinus Torvalds 
11811da177e4SLinus Torvalds 	if (msg->msg_flags & MSG_OOB)
11821da177e4SLinus Torvalds 		skb_queue_tail(&x25->interrupt_out_queue, skb);
11831da177e4SLinus Torvalds 	else {
11848db09f26SRoel Kluin 		rc = x25_output(sk, skb);
11858db09f26SRoel Kluin 		len = rc;
11868db09f26SRoel Kluin 		if (rc < 0)
11871da177e4SLinus Torvalds 			kfree_skb(skb);
11881da177e4SLinus Torvalds 		else if (x25->qbitincl)
11891da177e4SLinus Torvalds 			len++;
11901da177e4SLinus Torvalds 	}
11911da177e4SLinus Torvalds 
11921da177e4SLinus Torvalds 	/*
11931da177e4SLinus Torvalds 	 * lock_sock() is currently only used to serialize this x25_kick()
11941da177e4SLinus Torvalds 	 * against input-driven x25_kick() calls. It currently only blocks
11951da177e4SLinus Torvalds 	 * incoming packets for this socket and does not protect against
11961da177e4SLinus Torvalds 	 * any other socket state changes and is not called from anywhere
11971da177e4SLinus Torvalds 	 * else. As x25_kick() cannot block and as long as all socket
11981da177e4SLinus Torvalds 	 * operations are BKL-wrapped, we don't need take to care about
11991da177e4SLinus Torvalds 	 * purging the backlog queue in x25_release().
12001da177e4SLinus Torvalds 	 *
12011da177e4SLinus Torvalds 	 * Using lock_sock() to protect all socket operations entirely
12021da177e4SLinus Torvalds 	 * (and making the whole x25 stack SMP aware) unfortunately would
12031da177e4SLinus Torvalds 	 * require major changes to {send,recv}msg and skb allocation methods.
12041da177e4SLinus Torvalds 	 * -> 2.5 ;)
12051da177e4SLinus Torvalds 	 */
12061da177e4SLinus Torvalds 	lock_sock(sk);
12071da177e4SLinus Torvalds 	x25_kick(sk);
12081da177e4SLinus Torvalds 	release_sock(sk);
12091da177e4SLinus Torvalds 	rc = len;
12101da177e4SLinus Torvalds out:
121191774904SArnd Bergmann 	unlock_kernel();
12121da177e4SLinus Torvalds 	return rc;
12131da177e4SLinus Torvalds out_kfree_skb:
12141da177e4SLinus Torvalds 	kfree_skb(skb);
12151da177e4SLinus Torvalds 	goto out;
12161da177e4SLinus Torvalds }
12171da177e4SLinus Torvalds 
12181da177e4SLinus Torvalds 
12191da177e4SLinus Torvalds static int x25_recvmsg(struct kiocb *iocb, struct socket *sock,
12201da177e4SLinus Torvalds 		       struct msghdr *msg, size_t size,
12211da177e4SLinus Torvalds 		       int flags)
12221da177e4SLinus Torvalds {
12231da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
12241da177e4SLinus Torvalds 	struct x25_sock *x25 = x25_sk(sk);
12251da177e4SLinus Torvalds 	struct sockaddr_x25 *sx25 = (struct sockaddr_x25 *)msg->msg_name;
12261da177e4SLinus Torvalds 	size_t copied;
12271da177e4SLinus Torvalds 	int qbit;
12281da177e4SLinus Torvalds 	struct sk_buff *skb;
12291da177e4SLinus Torvalds 	unsigned char *asmptr;
12301da177e4SLinus Torvalds 	int rc = -ENOTCONN;
12311da177e4SLinus Torvalds 
123291774904SArnd Bergmann 	lock_kernel();
12331da177e4SLinus Torvalds 	/*
12341da177e4SLinus Torvalds 	 * This works for seqpacket too. The receiver has ordered the queue for
12351da177e4SLinus Torvalds 	 * us! We do one quick check first though
12361da177e4SLinus Torvalds 	 */
12371da177e4SLinus Torvalds 	if (sk->sk_state != TCP_ESTABLISHED)
12381da177e4SLinus Torvalds 		goto out;
12391da177e4SLinus Torvalds 
12401da177e4SLinus Torvalds 	if (flags & MSG_OOB) {
12411da177e4SLinus Torvalds 		rc = -EINVAL;
12421da177e4SLinus Torvalds 		if (sock_flag(sk, SOCK_URGINLINE) ||
12431da177e4SLinus Torvalds 		    !skb_peek(&x25->interrupt_in_queue))
12441da177e4SLinus Torvalds 			goto out;
12451da177e4SLinus Torvalds 
12461da177e4SLinus Torvalds 		skb = skb_dequeue(&x25->interrupt_in_queue);
12471da177e4SLinus Torvalds 
12481da177e4SLinus Torvalds 		skb_pull(skb, X25_STD_MIN_LEN);
12491da177e4SLinus Torvalds 
12501da177e4SLinus Torvalds 		/*
12511da177e4SLinus Torvalds 		 *	No Q bit information on Interrupt data.
12521da177e4SLinus Torvalds 		 */
12531da177e4SLinus Torvalds 		if (x25->qbitincl) {
12541da177e4SLinus Torvalds 			asmptr  = skb_push(skb, 1);
12551da177e4SLinus Torvalds 			*asmptr = 0x00;
12561da177e4SLinus Torvalds 		}
12571da177e4SLinus Torvalds 
12581da177e4SLinus Torvalds 		msg->msg_flags |= MSG_OOB;
12591da177e4SLinus Torvalds 	} else {
12601da177e4SLinus Torvalds 		/* Now we can treat all alike */
12611da177e4SLinus Torvalds 		skb = skb_recv_datagram(sk, flags & ~MSG_DONTWAIT,
12621da177e4SLinus Torvalds 					flags & MSG_DONTWAIT, &rc);
12631da177e4SLinus Torvalds 		if (!skb)
12641da177e4SLinus Torvalds 			goto out;
12651da177e4SLinus Torvalds 
12661da177e4SLinus Torvalds 		qbit = (skb->data[0] & X25_Q_BIT) == X25_Q_BIT;
12671da177e4SLinus Torvalds 
12681da177e4SLinus Torvalds 		skb_pull(skb, x25->neighbour->extended ?
12691da177e4SLinus Torvalds 				X25_EXT_MIN_LEN : X25_STD_MIN_LEN);
12701da177e4SLinus Torvalds 
12711da177e4SLinus Torvalds 		if (x25->qbitincl) {
12721da177e4SLinus Torvalds 			asmptr  = skb_push(skb, 1);
12731da177e4SLinus Torvalds 			*asmptr = qbit;
12741da177e4SLinus Torvalds 		}
12751da177e4SLinus Torvalds 	}
12761da177e4SLinus Torvalds 
1277badff6d0SArnaldo Carvalho de Melo 	skb_reset_transport_header(skb);
12781da177e4SLinus Torvalds 	copied = skb->len;
12791da177e4SLinus Torvalds 
12801da177e4SLinus Torvalds 	if (copied > size) {
12811da177e4SLinus Torvalds 		copied = size;
12821da177e4SLinus Torvalds 		msg->msg_flags |= MSG_TRUNC;
12831da177e4SLinus Torvalds 	}
12841da177e4SLinus Torvalds 
12851da177e4SLinus Torvalds 	/* Currently, each datagram always contains a complete record */
12861da177e4SLinus Torvalds 	msg->msg_flags |= MSG_EOR;
12871da177e4SLinus Torvalds 
12881da177e4SLinus Torvalds 	rc = skb_copy_datagram_iovec(skb, 0, msg->msg_iov, copied);
12891da177e4SLinus Torvalds 	if (rc)
12901da177e4SLinus Torvalds 		goto out_free_dgram;
12911da177e4SLinus Torvalds 
12921da177e4SLinus Torvalds 	if (sx25) {
12931da177e4SLinus Torvalds 		sx25->sx25_family = AF_X25;
12941da177e4SLinus Torvalds 		sx25->sx25_addr   = x25->dest_addr;
12951da177e4SLinus Torvalds 	}
12961da177e4SLinus Torvalds 
12971da177e4SLinus Torvalds 	msg->msg_namelen = sizeof(struct sockaddr_x25);
12981da177e4SLinus Torvalds 
12991da177e4SLinus Torvalds 	lock_sock(sk);
13001da177e4SLinus Torvalds 	x25_check_rbuf(sk);
13011da177e4SLinus Torvalds 	release_sock(sk);
13021da177e4SLinus Torvalds 	rc = copied;
13031da177e4SLinus Torvalds out_free_dgram:
13041da177e4SLinus Torvalds 	skb_free_datagram(sk, skb);
13051da177e4SLinus Torvalds out:
130691774904SArnd Bergmann 	unlock_kernel();
13071da177e4SLinus Torvalds 	return rc;
13081da177e4SLinus Torvalds }
13091da177e4SLinus Torvalds 
13101da177e4SLinus Torvalds 
13111da177e4SLinus Torvalds static int x25_ioctl(struct socket *sock, unsigned int cmd, unsigned long arg)
13121da177e4SLinus Torvalds {
13131da177e4SLinus Torvalds 	struct sock *sk = sock->sk;
13141da177e4SLinus Torvalds 	struct x25_sock *x25 = x25_sk(sk);
13151da177e4SLinus Torvalds 	void __user *argp = (void __user *)arg;
13161da177e4SLinus Torvalds 	int rc;
13171da177e4SLinus Torvalds 
131891774904SArnd Bergmann 	lock_kernel();
13191da177e4SLinus Torvalds 	switch (cmd) {
13201da177e4SLinus Torvalds 		case TIOCOUTQ: {
132131e6d363SEric Dumazet 			int amount = sk->sk_sndbuf - sk_wmem_alloc_get(sk);
132231e6d363SEric Dumazet 
13231da177e4SLinus Torvalds 			if (amount < 0)
13241da177e4SLinus Torvalds 				amount = 0;
13251da177e4SLinus Torvalds 			rc = put_user(amount, (unsigned int __user *)argp);
13261da177e4SLinus Torvalds 			break;
13271da177e4SLinus Torvalds 		}
13281da177e4SLinus Torvalds 
13291da177e4SLinus Torvalds 		case TIOCINQ: {
13301da177e4SLinus Torvalds 			struct sk_buff *skb;
13311da177e4SLinus Torvalds 			int amount = 0;
13321da177e4SLinus Torvalds 			/*
13331da177e4SLinus Torvalds 			 * These two are safe on a single CPU system as
13341da177e4SLinus Torvalds 			 * only user tasks fiddle here
13351da177e4SLinus Torvalds 			 */
13361da177e4SLinus Torvalds 			if ((skb = skb_peek(&sk->sk_receive_queue)) != NULL)
13371da177e4SLinus Torvalds 				amount = skb->len;
13381da177e4SLinus Torvalds 			rc = put_user(amount, (unsigned int __user *)argp);
13391da177e4SLinus Torvalds 			break;
13401da177e4SLinus Torvalds 		}
13411da177e4SLinus Torvalds 
13421da177e4SLinus Torvalds 		case SIOCGSTAMP:
13431da177e4SLinus Torvalds 			rc = -EINVAL;
13441da177e4SLinus Torvalds 			if (sk)
13451da177e4SLinus Torvalds 				rc = sock_get_timestamp(sk,
13461da177e4SLinus Torvalds 						(struct timeval __user *)argp);
13471da177e4SLinus Torvalds 			break;
1348ae40eb1eSEric Dumazet 		case SIOCGSTAMPNS:
1349ae40eb1eSEric Dumazet 			rc = -EINVAL;
1350ae40eb1eSEric Dumazet 			if (sk)
1351ae40eb1eSEric Dumazet 				rc = sock_get_timestampns(sk,
1352ae40eb1eSEric Dumazet 						(struct timespec __user *)argp);
1353ae40eb1eSEric Dumazet 			break;
13541da177e4SLinus Torvalds 		case SIOCGIFADDR:
13551da177e4SLinus Torvalds 		case SIOCSIFADDR:
13561da177e4SLinus Torvalds 		case SIOCGIFDSTADDR:
13571da177e4SLinus Torvalds 		case SIOCSIFDSTADDR:
13581da177e4SLinus Torvalds 		case SIOCGIFBRDADDR:
13591da177e4SLinus Torvalds 		case SIOCSIFBRDADDR:
13601da177e4SLinus Torvalds 		case SIOCGIFNETMASK:
13611da177e4SLinus Torvalds 		case SIOCSIFNETMASK:
13621da177e4SLinus Torvalds 		case SIOCGIFMETRIC:
13631da177e4SLinus Torvalds 		case SIOCSIFMETRIC:
13641da177e4SLinus Torvalds 			rc = -EINVAL;
13651da177e4SLinus Torvalds 			break;
13661da177e4SLinus Torvalds 		case SIOCADDRT:
13671da177e4SLinus Torvalds 		case SIOCDELRT:
13681da177e4SLinus Torvalds 			rc = -EPERM;
13691da177e4SLinus Torvalds 			if (!capable(CAP_NET_ADMIN))
13701da177e4SLinus Torvalds 				break;
13711da177e4SLinus Torvalds 			rc = x25_route_ioctl(cmd, argp);
13721da177e4SLinus Torvalds 			break;
13731da177e4SLinus Torvalds 		case SIOCX25GSUBSCRIP:
13741da177e4SLinus Torvalds 			rc = x25_subscr_ioctl(cmd, argp);
13751da177e4SLinus Torvalds 			break;
13761da177e4SLinus Torvalds 		case SIOCX25SSUBSCRIP:
13771da177e4SLinus Torvalds 			rc = -EPERM;
13781da177e4SLinus Torvalds 			if (!capable(CAP_NET_ADMIN))
13791da177e4SLinus Torvalds 				break;
13801da177e4SLinus Torvalds 			rc = x25_subscr_ioctl(cmd, argp);
13811da177e4SLinus Torvalds 			break;
13821da177e4SLinus Torvalds 		case SIOCX25GFACILITIES: {
13831da177e4SLinus Torvalds 			struct x25_facilities fac = x25->facilities;
13841da177e4SLinus Torvalds 			rc = copy_to_user(argp, &fac,
13851da177e4SLinus Torvalds 					  sizeof(fac)) ? -EFAULT : 0;
13861da177e4SLinus Torvalds 			break;
13871da177e4SLinus Torvalds 		}
13881da177e4SLinus Torvalds 
13891da177e4SLinus Torvalds 		case SIOCX25SFACILITIES: {
13901da177e4SLinus Torvalds 			struct x25_facilities facilities;
13911da177e4SLinus Torvalds 			rc = -EFAULT;
13921da177e4SLinus Torvalds 			if (copy_from_user(&facilities, argp,
13931da177e4SLinus Torvalds 					   sizeof(facilities)))
13941da177e4SLinus Torvalds 				break;
13951da177e4SLinus Torvalds 			rc = -EINVAL;
13961da177e4SLinus Torvalds 			if (sk->sk_state != TCP_LISTEN &&
13971da177e4SLinus Torvalds 			    sk->sk_state != TCP_CLOSE)
13981da177e4SLinus Torvalds 				break;
13991da177e4SLinus Torvalds 			if (facilities.pacsize_in < X25_PS16 ||
14001da177e4SLinus Torvalds 			    facilities.pacsize_in > X25_PS4096)
14011da177e4SLinus Torvalds 				break;
14021da177e4SLinus Torvalds 			if (facilities.pacsize_out < X25_PS16 ||
14031da177e4SLinus Torvalds 			    facilities.pacsize_out > X25_PS4096)
14041da177e4SLinus Torvalds 				break;
14051da177e4SLinus Torvalds 			if (facilities.winsize_in < 1 ||
14061da177e4SLinus Torvalds 			    facilities.winsize_in > 127)
14071da177e4SLinus Torvalds 				break;
14081da177e4SLinus Torvalds 			if (facilities.throughput < 0x03 ||
14091da177e4SLinus Torvalds 			    facilities.throughput > 0xDD)
14101da177e4SLinus Torvalds 				break;
1411ebc3f64bSShaun Pereira 			if (facilities.reverse &&
141206a96b33Sroel kluin 				(facilities.reverse & 0x81) != 0x81)
14131da177e4SLinus Torvalds 				break;
14141da177e4SLinus Torvalds 			x25->facilities = facilities;
14151da177e4SLinus Torvalds 			rc = 0;
14161da177e4SLinus Torvalds 			break;
14171da177e4SLinus Torvalds 		}
14181da177e4SLinus Torvalds 
1419a64b7b93SShaun Pereira 		case SIOCX25GDTEFACILITIES: {
1420a64b7b93SShaun Pereira 			rc = copy_to_user(argp, &x25->dte_facilities,
1421a64b7b93SShaun Pereira 						sizeof(x25->dte_facilities));
1422a64b7b93SShaun Pereira 			if (rc)
1423a64b7b93SShaun Pereira 				rc = -EFAULT;
1424a64b7b93SShaun Pereira 			break;
1425a64b7b93SShaun Pereira 		}
1426a64b7b93SShaun Pereira 
1427a64b7b93SShaun Pereira 		case SIOCX25SDTEFACILITIES: {
1428a64b7b93SShaun Pereira 			struct x25_dte_facilities dtefacs;
1429a64b7b93SShaun Pereira 			rc = -EFAULT;
1430a64b7b93SShaun Pereira 			if (copy_from_user(&dtefacs, argp, sizeof(dtefacs)))
1431a64b7b93SShaun Pereira 				break;
1432a64b7b93SShaun Pereira 			rc = -EINVAL;
1433a64b7b93SShaun Pereira 			if (sk->sk_state != TCP_LISTEN &&
1434a64b7b93SShaun Pereira 					sk->sk_state != TCP_CLOSE)
1435a64b7b93SShaun Pereira 				break;
1436a64b7b93SShaun Pereira 			if (dtefacs.calling_len > X25_MAX_AE_LEN)
1437a64b7b93SShaun Pereira 				break;
1438a64b7b93SShaun Pereira 			if (dtefacs.calling_ae == NULL)
1439a64b7b93SShaun Pereira 				break;
1440a64b7b93SShaun Pereira 			if (dtefacs.called_len > X25_MAX_AE_LEN)
1441a64b7b93SShaun Pereira 				break;
1442a64b7b93SShaun Pereira 			if (dtefacs.called_ae == NULL)
1443a64b7b93SShaun Pereira 				break;
1444a64b7b93SShaun Pereira 			x25->dte_facilities = dtefacs;
1445a64b7b93SShaun Pereira 			rc = 0;
1446a64b7b93SShaun Pereira 			break;
1447a64b7b93SShaun Pereira 		}
1448a64b7b93SShaun Pereira 
14491da177e4SLinus Torvalds 		case SIOCX25GCALLUSERDATA: {
14501da177e4SLinus Torvalds 			struct x25_calluserdata cud = x25->calluserdata;
14511da177e4SLinus Torvalds 			rc = copy_to_user(argp, &cud,
14521da177e4SLinus Torvalds 					  sizeof(cud)) ? -EFAULT : 0;
14531da177e4SLinus Torvalds 			break;
14541da177e4SLinus Torvalds 		}
14551da177e4SLinus Torvalds 
14561da177e4SLinus Torvalds 		case SIOCX25SCALLUSERDATA: {
14571da177e4SLinus Torvalds 			struct x25_calluserdata calluserdata;
14581da177e4SLinus Torvalds 
14591da177e4SLinus Torvalds 			rc = -EFAULT;
14601da177e4SLinus Torvalds 			if (copy_from_user(&calluserdata, argp,
14611da177e4SLinus Torvalds 					   sizeof(calluserdata)))
14621da177e4SLinus Torvalds 				break;
14631da177e4SLinus Torvalds 			rc = -EINVAL;
14641da177e4SLinus Torvalds 			if (calluserdata.cudlength > X25_MAX_CUD_LEN)
14651da177e4SLinus Torvalds 				break;
14661da177e4SLinus Torvalds 			x25->calluserdata = calluserdata;
14671da177e4SLinus Torvalds 			rc = 0;
14681da177e4SLinus Torvalds 			break;
14691da177e4SLinus Torvalds 		}
14701da177e4SLinus Torvalds 
14711da177e4SLinus Torvalds 		case SIOCX25GCAUSEDIAG: {
14721da177e4SLinus Torvalds 			struct x25_causediag causediag;
14731da177e4SLinus Torvalds 			causediag = x25->causediag;
14741da177e4SLinus Torvalds 			rc = copy_to_user(argp, &causediag,
14751da177e4SLinus Torvalds 					  sizeof(causediag)) ? -EFAULT : 0;
14761da177e4SLinus Torvalds 			break;
14771da177e4SLinus Torvalds 		}
14781da177e4SLinus Torvalds 
1479386e50ccSAndrew Hendry 		case SIOCX25SCAUSEDIAG: {
1480386e50ccSAndrew Hendry 			struct x25_causediag causediag;
1481386e50ccSAndrew Hendry 			rc = -EFAULT;
1482386e50ccSAndrew Hendry 			if (copy_from_user(&causediag, argp, sizeof(causediag)))
1483386e50ccSAndrew Hendry 				break;
1484386e50ccSAndrew Hendry 			x25->causediag = causediag;
1485386e50ccSAndrew Hendry 			rc = 0;
1486386e50ccSAndrew Hendry 			break;
1487386e50ccSAndrew Hendry 
1488386e50ccSAndrew Hendry 		}
1489386e50ccSAndrew Hendry 
1490cb65d506SShaun Pereira 		case SIOCX25SCUDMATCHLEN: {
1491cb65d506SShaun Pereira 			struct x25_subaddr sub_addr;
1492cb65d506SShaun Pereira 			rc = -EINVAL;
1493cb65d506SShaun Pereira 			if(sk->sk_state != TCP_CLOSE)
1494cb65d506SShaun Pereira 				break;
1495cb65d506SShaun Pereira 			rc = -EFAULT;
1496cb65d506SShaun Pereira 			if (copy_from_user(&sub_addr, argp,
1497cb65d506SShaun Pereira 					sizeof(sub_addr)))
1498cb65d506SShaun Pereira 				break;
1499cb65d506SShaun Pereira 			rc = -EINVAL;
1500cb65d506SShaun Pereira 			if(sub_addr.cudmatchlength > X25_MAX_CUD_LEN)
1501cb65d506SShaun Pereira 				break;
1502cb65d506SShaun Pereira 			x25->cudmatchlength = sub_addr.cudmatchlength;
1503cb65d506SShaun Pereira 			rc = 0;
1504cb65d506SShaun Pereira 			break;
1505cb65d506SShaun Pereira 		}
1506cb65d506SShaun Pereira 
1507ebc3f64bSShaun Pereira 		case SIOCX25CALLACCPTAPPRV: {
1508ebc3f64bSShaun Pereira 			rc = -EINVAL;
1509ebc3f64bSShaun Pereira 			if (sk->sk_state != TCP_CLOSE)
1510ebc3f64bSShaun Pereira 				break;
1511ebc3f64bSShaun Pereira 			x25->accptapprv = X25_ALLOW_ACCPT_APPRV;
1512ebc3f64bSShaun Pereira 			rc = 0;
1513ebc3f64bSShaun Pereira 			break;
1514ebc3f64bSShaun Pereira 		}
1515ebc3f64bSShaun Pereira 
1516ebc3f64bSShaun Pereira 		case SIOCX25SENDCALLACCPT:  {
1517ebc3f64bSShaun Pereira 			rc = -EINVAL;
1518ebc3f64bSShaun Pereira 			if (sk->sk_state != TCP_ESTABLISHED)
1519ebc3f64bSShaun Pereira 				break;
1520ebc3f64bSShaun Pereira 			if (x25->accptapprv)	/* must call accptapprv above */
1521ebc3f64bSShaun Pereira 				break;
1522ebc3f64bSShaun Pereira 			x25_write_internal(sk, X25_CALL_ACCEPTED);
1523ebc3f64bSShaun Pereira 			x25->state = X25_STATE_3;
1524ebc3f64bSShaun Pereira 			rc = 0;
1525ebc3f64bSShaun Pereira 			break;
1526ebc3f64bSShaun Pereira 		}
1527ebc3f64bSShaun Pereira 
15281da177e4SLinus Torvalds 		default:
1529b5e5fa5eSChristoph Hellwig 			rc = -ENOIOCTLCMD;
15301da177e4SLinus Torvalds 			break;
15311da177e4SLinus Torvalds 	}
153291774904SArnd Bergmann 	unlock_kernel();
15331da177e4SLinus Torvalds 
15341da177e4SLinus Torvalds 	return rc;
15351da177e4SLinus Torvalds }
15361da177e4SLinus Torvalds 
1537ec1b4cf7SStephen Hemminger static const struct net_proto_family x25_family_ops = {
15381da177e4SLinus Torvalds 	.family =	AF_X25,
15391da177e4SLinus Torvalds 	.create =	x25_create,
15401da177e4SLinus Torvalds 	.owner	=	THIS_MODULE,
15411da177e4SLinus Torvalds };
15421da177e4SLinus Torvalds 
15431b06e6baSShaun Pereira #ifdef CONFIG_COMPAT
15441b06e6baSShaun Pereira static int compat_x25_subscr_ioctl(unsigned int cmd,
15451b06e6baSShaun Pereira 		struct compat_x25_subscrip_struct __user *x25_subscr32)
15461b06e6baSShaun Pereira {
15471b06e6baSShaun Pereira 	struct compat_x25_subscrip_struct x25_subscr;
15481b06e6baSShaun Pereira 	struct x25_neigh *nb;
15491b06e6baSShaun Pereira 	struct net_device *dev;
15501b06e6baSShaun Pereira 	int rc = -EINVAL;
15511b06e6baSShaun Pereira 
15521b06e6baSShaun Pereira 	rc = -EFAULT;
15531b06e6baSShaun Pereira 	if (copy_from_user(&x25_subscr, x25_subscr32, sizeof(*x25_subscr32)))
15541b06e6baSShaun Pereira 		goto out;
15551b06e6baSShaun Pereira 
15561b06e6baSShaun Pereira 	rc = -EINVAL;
15571b06e6baSShaun Pereira 	dev = x25_dev_get(x25_subscr.device);
15581b06e6baSShaun Pereira 	if (dev == NULL)
15591b06e6baSShaun Pereira 		goto out;
15601b06e6baSShaun Pereira 
15611b06e6baSShaun Pereira 	nb = x25_get_neigh(dev);
15621b06e6baSShaun Pereira 	if (nb == NULL)
15631b06e6baSShaun Pereira 		goto out_dev_put;
15641b06e6baSShaun Pereira 
15651b06e6baSShaun Pereira 	dev_put(dev);
15661b06e6baSShaun Pereira 
15671b06e6baSShaun Pereira 	if (cmd == SIOCX25GSUBSCRIP) {
15681b06e6baSShaun Pereira 		x25_subscr.extended = nb->extended;
15691b06e6baSShaun Pereira 		x25_subscr.global_facil_mask = nb->global_facil_mask;
15701b06e6baSShaun Pereira 		rc = copy_to_user(x25_subscr32, &x25_subscr,
15711b06e6baSShaun Pereira 				sizeof(*x25_subscr32)) ? -EFAULT : 0;
15721b06e6baSShaun Pereira 	} else {
15731b06e6baSShaun Pereira 		rc = -EINVAL;
15741b06e6baSShaun Pereira 		if (x25_subscr.extended == 0 || x25_subscr.extended == 1) {
15751b06e6baSShaun Pereira 			rc = 0;
15761b06e6baSShaun Pereira 			nb->extended = x25_subscr.extended;
15771b06e6baSShaun Pereira 			nb->global_facil_mask = x25_subscr.global_facil_mask;
15781b06e6baSShaun Pereira 		}
15791b06e6baSShaun Pereira 	}
15801b06e6baSShaun Pereira 	x25_neigh_put(nb);
15811b06e6baSShaun Pereira out:
15821b06e6baSShaun Pereira 	return rc;
15831b06e6baSShaun Pereira out_dev_put:
15841b06e6baSShaun Pereira 	dev_put(dev);
15851b06e6baSShaun Pereira 	goto out;
15861b06e6baSShaun Pereira }
15871b06e6baSShaun Pereira 
15881b06e6baSShaun Pereira static int compat_x25_ioctl(struct socket *sock, unsigned int cmd,
15891b06e6baSShaun Pereira 				unsigned long arg)
15901b06e6baSShaun Pereira {
15911b06e6baSShaun Pereira 	void __user *argp = compat_ptr(arg);
15921b06e6baSShaun Pereira 	struct sock *sk = sock->sk;
15931b06e6baSShaun Pereira 
15941b06e6baSShaun Pereira 	int rc = -ENOIOCTLCMD;
15951b06e6baSShaun Pereira 
15961b06e6baSShaun Pereira 	switch(cmd) {
15971b06e6baSShaun Pereira 	case TIOCOUTQ:
15981b06e6baSShaun Pereira 	case TIOCINQ:
15991b06e6baSShaun Pereira 		rc = x25_ioctl(sock, cmd, (unsigned long)argp);
16001b06e6baSShaun Pereira 		break;
16011b06e6baSShaun Pereira 	case SIOCGSTAMP:
16021b06e6baSShaun Pereira 		rc = -EINVAL;
160391774904SArnd Bergmann 		lock_kernel();
16041b06e6baSShaun Pereira 		if (sk)
16051b06e6baSShaun Pereira 			rc = compat_sock_get_timestamp(sk,
16061b06e6baSShaun Pereira 					(struct timeval __user*)argp);
160791774904SArnd Bergmann 		unlock_kernel();
16081b06e6baSShaun Pereira 		break;
1609ae40eb1eSEric Dumazet 	case SIOCGSTAMPNS:
1610ae40eb1eSEric Dumazet 		rc = -EINVAL;
161191774904SArnd Bergmann 		lock_kernel();
1612ae40eb1eSEric Dumazet 		if (sk)
1613ae40eb1eSEric Dumazet 			rc = compat_sock_get_timestampns(sk,
1614ae40eb1eSEric Dumazet 					(struct timespec __user*)argp);
161591774904SArnd Bergmann 		unlock_kernel();
1616ae40eb1eSEric Dumazet 		break;
16171b06e6baSShaun Pereira 	case SIOCGIFADDR:
16181b06e6baSShaun Pereira 	case SIOCSIFADDR:
16191b06e6baSShaun Pereira 	case SIOCGIFDSTADDR:
16201b06e6baSShaun Pereira 	case SIOCSIFDSTADDR:
16211b06e6baSShaun Pereira 	case SIOCGIFBRDADDR:
16221b06e6baSShaun Pereira 	case SIOCSIFBRDADDR:
16231b06e6baSShaun Pereira 	case SIOCGIFNETMASK:
16241b06e6baSShaun Pereira 	case SIOCSIFNETMASK:
16251b06e6baSShaun Pereira 	case SIOCGIFMETRIC:
16261b06e6baSShaun Pereira 	case SIOCSIFMETRIC:
16271b06e6baSShaun Pereira 		rc = -EINVAL;
16281b06e6baSShaun Pereira 		break;
16291b06e6baSShaun Pereira 	case SIOCADDRT:
16301b06e6baSShaun Pereira 	case SIOCDELRT:
16311b06e6baSShaun Pereira 		rc = -EPERM;
16321b06e6baSShaun Pereira 		if (!capable(CAP_NET_ADMIN))
16331b06e6baSShaun Pereira 			break;
163491774904SArnd Bergmann 		lock_kernel();
16351b06e6baSShaun Pereira 		rc = x25_route_ioctl(cmd, argp);
163691774904SArnd Bergmann 		unlock_kernel();
16371b06e6baSShaun Pereira 		break;
16381b06e6baSShaun Pereira 	case SIOCX25GSUBSCRIP:
163991774904SArnd Bergmann 		lock_kernel();
16401b06e6baSShaun Pereira 		rc = compat_x25_subscr_ioctl(cmd, argp);
164191774904SArnd Bergmann 		unlock_kernel();
16421b06e6baSShaun Pereira 		break;
16431b06e6baSShaun Pereira 	case SIOCX25SSUBSCRIP:
16441b06e6baSShaun Pereira 		rc = -EPERM;
16451b06e6baSShaun Pereira 		if (!capable(CAP_NET_ADMIN))
16461b06e6baSShaun Pereira 			break;
164791774904SArnd Bergmann 		lock_kernel();
16481b06e6baSShaun Pereira 		rc = compat_x25_subscr_ioctl(cmd, argp);
164991774904SArnd Bergmann 		unlock_kernel();
16501b06e6baSShaun Pereira 		break;
16511b06e6baSShaun Pereira 	case SIOCX25GFACILITIES:
16521b06e6baSShaun Pereira 	case SIOCX25SFACILITIES:
16539a6b9f2eSShaun Pereira 	case SIOCX25GDTEFACILITIES:
16549a6b9f2eSShaun Pereira 	case SIOCX25SDTEFACILITIES:
16551b06e6baSShaun Pereira 	case SIOCX25GCALLUSERDATA:
16561b06e6baSShaun Pereira 	case SIOCX25SCALLUSERDATA:
16571b06e6baSShaun Pereira 	case SIOCX25GCAUSEDIAG:
1658386e50ccSAndrew Hendry 	case SIOCX25SCAUSEDIAG:
16591b06e6baSShaun Pereira 	case SIOCX25SCUDMATCHLEN:
16601b06e6baSShaun Pereira 	case SIOCX25CALLACCPTAPPRV:
16611b06e6baSShaun Pereira 	case SIOCX25SENDCALLACCPT:
16621b06e6baSShaun Pereira 		rc = x25_ioctl(sock, cmd, (unsigned long)argp);
16631b06e6baSShaun Pereira 		break;
16641b06e6baSShaun Pereira 	default:
16651b06e6baSShaun Pereira 		rc = -ENOIOCTLCMD;
16661b06e6baSShaun Pereira 		break;
16671b06e6baSShaun Pereira 	}
16681b06e6baSShaun Pereira 	return rc;
16691b06e6baSShaun Pereira }
16701b06e6baSShaun Pereira #endif
16711b06e6baSShaun Pereira 
167291774904SArnd Bergmann static const struct proto_ops x25_proto_ops = {
16731da177e4SLinus Torvalds 	.family =	AF_X25,
16741da177e4SLinus Torvalds 	.owner =	THIS_MODULE,
16751da177e4SLinus Torvalds 	.release =	x25_release,
16761da177e4SLinus Torvalds 	.bind =		x25_bind,
16771da177e4SLinus Torvalds 	.connect =	x25_connect,
16781da177e4SLinus Torvalds 	.socketpair =	sock_no_socketpair,
16791da177e4SLinus Torvalds 	.accept =	x25_accept,
16801da177e4SLinus Torvalds 	.getname =	x25_getname,
168191774904SArnd Bergmann 	.poll =		x25_datagram_poll,
16821da177e4SLinus Torvalds 	.ioctl =	x25_ioctl,
16831b06e6baSShaun Pereira #ifdef CONFIG_COMPAT
16841b06e6baSShaun Pereira 	.compat_ioctl = compat_x25_ioctl,
16851b06e6baSShaun Pereira #endif
16861da177e4SLinus Torvalds 	.listen =	x25_listen,
16871da177e4SLinus Torvalds 	.shutdown =	sock_no_shutdown,
16881da177e4SLinus Torvalds 	.setsockopt =	x25_setsockopt,
16891da177e4SLinus Torvalds 	.getsockopt =	x25_getsockopt,
16901da177e4SLinus Torvalds 	.sendmsg =	x25_sendmsg,
16911da177e4SLinus Torvalds 	.recvmsg =	x25_recvmsg,
16921da177e4SLinus Torvalds 	.mmap =		sock_no_mmap,
16931da177e4SLinus Torvalds 	.sendpage =	sock_no_sendpage,
16941da177e4SLinus Torvalds };
16951da177e4SLinus Torvalds 
16967546dd97SStephen Hemminger static struct packet_type x25_packet_type __read_mostly = {
169709640e63SHarvey Harrison 	.type =	cpu_to_be16(ETH_P_X25),
16981da177e4SLinus Torvalds 	.func =	x25_lapb_receive_frame,
16991da177e4SLinus Torvalds };
17001da177e4SLinus Torvalds 
17011da177e4SLinus Torvalds static struct notifier_block x25_dev_notifier = {
17021da177e4SLinus Torvalds 	.notifier_call = x25_device_event,
17031da177e4SLinus Torvalds };
17041da177e4SLinus Torvalds 
17051da177e4SLinus Torvalds void x25_kill_by_neigh(struct x25_neigh *nb)
17061da177e4SLinus Torvalds {
17071da177e4SLinus Torvalds 	struct sock *s;
17081da177e4SLinus Torvalds 	struct hlist_node *node;
17091da177e4SLinus Torvalds 
17101da177e4SLinus Torvalds 	write_lock_bh(&x25_list_lock);
17111da177e4SLinus Torvalds 
17121da177e4SLinus Torvalds 	sk_for_each(s, node, &x25_list)
17131da177e4SLinus Torvalds 		if (x25_sk(s)->neighbour == nb)
17141da177e4SLinus Torvalds 			x25_disconnect(s, ENETUNREACH, 0, 0);
17151da177e4SLinus Torvalds 
17161da177e4SLinus Torvalds 	write_unlock_bh(&x25_list_lock);
171795a9dc43SAndrew Hendry 
171895a9dc43SAndrew Hendry 	/* Remove any related forwards */
171995a9dc43SAndrew Hendry 	x25_clear_forward_by_dev(nb->dev);
17201da177e4SLinus Torvalds }
17211da177e4SLinus Torvalds 
17221da177e4SLinus Torvalds static int __init x25_init(void)
17231da177e4SLinus Torvalds {
17241da177e4SLinus Torvalds 	int rc = proto_register(&x25_proto, 0);
17251da177e4SLinus Torvalds 
17261da177e4SLinus Torvalds 	if (rc != 0)
17271da177e4SLinus Torvalds 		goto out;
17281da177e4SLinus Torvalds 
17291fd975a0Sandrew hendry 	rc = sock_register(&x25_family_ops);
17301fd975a0Sandrew hendry 	if (rc != 0)
17311fd975a0Sandrew hendry 		goto out_proto;
17321da177e4SLinus Torvalds 
17331da177e4SLinus Torvalds 	dev_add_pack(&x25_packet_type);
17341da177e4SLinus Torvalds 
17351fd975a0Sandrew hendry 	rc = register_netdevice_notifier(&x25_dev_notifier);
17361fd975a0Sandrew hendry 	if (rc != 0)
17371fd975a0Sandrew hendry 		goto out_sock;
17381da177e4SLinus Torvalds 
1739a44562e4Smaximilian attems 	printk(KERN_INFO "X.25 for Linux Version 0.2\n");
17401da177e4SLinus Torvalds 
17411da177e4SLinus Torvalds 	x25_register_sysctl();
17421fd975a0Sandrew hendry 	rc = x25_proc_init();
17431fd975a0Sandrew hendry 	if (rc != 0)
17441fd975a0Sandrew hendry 		goto out_dev;
17451da177e4SLinus Torvalds out:
17461da177e4SLinus Torvalds 	return rc;
17471fd975a0Sandrew hendry out_dev:
17481fd975a0Sandrew hendry 	unregister_netdevice_notifier(&x25_dev_notifier);
17491fd975a0Sandrew hendry out_sock:
17501fd975a0Sandrew hendry 	sock_unregister(AF_X25);
17511fd975a0Sandrew hendry out_proto:
17521fd975a0Sandrew hendry 	proto_unregister(&x25_proto);
17531fd975a0Sandrew hendry 	goto out;
17541da177e4SLinus Torvalds }
17551da177e4SLinus Torvalds module_init(x25_init);
17561da177e4SLinus Torvalds 
17571da177e4SLinus Torvalds static void __exit x25_exit(void)
17581da177e4SLinus Torvalds {
17591da177e4SLinus Torvalds 	x25_proc_exit();
17601da177e4SLinus Torvalds 	x25_link_free();
17611da177e4SLinus Torvalds 	x25_route_free();
17621da177e4SLinus Torvalds 
17631da177e4SLinus Torvalds 	x25_unregister_sysctl();
17641da177e4SLinus Torvalds 
17651da177e4SLinus Torvalds 	unregister_netdevice_notifier(&x25_dev_notifier);
17661da177e4SLinus Torvalds 
17671da177e4SLinus Torvalds 	dev_remove_pack(&x25_packet_type);
17681da177e4SLinus Torvalds 
17691da177e4SLinus Torvalds 	sock_unregister(AF_X25);
17701da177e4SLinus Torvalds 	proto_unregister(&x25_proto);
17711da177e4SLinus Torvalds }
17721da177e4SLinus Torvalds module_exit(x25_exit);
17731da177e4SLinus Torvalds 
17741da177e4SLinus Torvalds MODULE_AUTHOR("Jonathan Naylor <g4klx@g4klx.demon.co.uk>");
17751da177e4SLinus Torvalds MODULE_DESCRIPTION("The X.25 Packet Layer network layer protocol");
17761da177e4SLinus Torvalds MODULE_LICENSE("GPL");
17771da177e4SLinus Torvalds MODULE_ALIAS_NETPROTO(PF_X25);
1778